Skip to content

Getting a slice from a threadlocal array on linux provides an invalid pointer #3433

@kprotty

Description

@kprotty

Below, the mem.cpy segfaults as the ptr in the buffer[0..] slice results as 0x0 (seems to only be visible in debuggers while printing out with std.debug.warn gives the correct address). Other code shows the slice ptr being the first byte of the address and not necessarily 0x0.

const std = @import("std");
threadlocal var buffer: [42]u8 = undefined;
pub fn main() void {
    std.mem.copy(u8, buffer[0..], "Hello world");
}

Was not able to reproduce on windows, macosx, and linux with --single-threaded. Getting the slice using the method below appears to provide the correct address however.

const buf = @intToPtr([*]u8, @ptrToInt(&buffer[0]))[0..buffer.len];

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.stage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions