zig icon indicating copy to clipboard operation
zig copied to clipboard

[stage2][miscompilation] threadlocal variable pointer vs comptime

Open xxxbxxx opened this issue 1 year ago • 0 comments

Zig Version

zig-0.10.0-dev.4437+1e963053d

Steps to Reproduce

threadlocal var global: u32 = 23;
threadlocal var global_ptr: *u32 = &global;

pub fn main() !void {
  if (global_ptr.* != 23) unreachable;
}

Expected Behavior

stage1 rejects the code:

./zig  build-exe repro.zig  -fstage1
./repro.zig:2:36: error: cannot store runtime value in compile time variable
threadlocal var global_ptr: *u32 = &global;
                                   ^

Actual Behavior

compiles with stage2, and crashes

Segmentation fault at address 0x8
./repro.zig:5:7: 0x20ff0d in main (repro)
  if (global_ptr.* != 23) unreachable;
      ^
/var/home/xavier/src/zig/build15/stage3/lib/std/start.zig:578:37: 0x20fb49 in posixCallMainAndExit (repro)
            const result = root.main() catch |err| {
                                    ^
/var/home/xavier/src/zig/build15/stage3/lib/std/start.zig:340:5: 0x20f5d2 in _start (repro)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^

xxxbxxx avatar Oct 18 '22 06:10 xxxbxxx