zig icon indicating copy to clipboard operation
zig copied to clipboard

Compiler segfault when an array is assigned to a temporary array

Open DerryAlex opened this issue 3 years ago • 1 comments

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

const std = @import("std");

var pair: [2]usize = .{ 1, 2 };

noinline fn foo(x: usize, y: usize) void { // This is _NOT_ OK
    pair = [2]usize{ x, y };
}

noinline fn bar(x: usize, y: usize) void { // This is OK
    var tmp: [2]usize = .{ x, y };
    pair = tmp;
}

pub fn main() void {
    foo(3, 4);
    bar(5, 6);
    std.log.info("{any}", .{pair});
}

Actual Behavior:

$ zig build-exe bug.zig 
Segmentation fault

Trace:

$ ~/workplace/zig-bootstrap/zig/stage3/bin/zig build-exe bug.zig 
thread 99355 panic: Segmentation fault at address 0x0
/home/chief/workplace/zig-bootstrap/zig/src/value.zig:389:13: 0x7e7f680 in castTag__anon_96246 (zig)
        if (self.ptr_otherwise.tag == t)
            ^
codegen/llvm.zig:3198:27: 0x7cd2e2e in lowerValue (zig)
codegen/llvm.zig:3408:58: 0x7cd5cff in lowerValue (zig)
codegen/llvm.zig:4401:48: 0x83be368 in resolveValue (zig)
codegen/llvm.zig:4394:34: 0x83be92b in resolveInst (zig)
codegen/llvm.zig:8017:53: 0x83f412a in airStore (zig)
codegen/llvm.zig:4553:53: 0x8031487 in genBody (zig)
codegen/llvm.zig:1225:35: 0x802c2c8 in updateFunc (zig)
link/Elf.zig:2411:74: 0x80345e2 in updateFunc (zig)
/home/chief/workplace/zig-bootstrap/zig/src/link.zig:558:77: 0x7e3709d in updateFunc (zig)
            .elf   => return @fieldParentPtr(Elf,   "base", base).updateFunc(module, func, air, liveness),
                                                                            ^
/home/chief/workplace/zig-bootstrap/zig/src/Module.zig:4385:37: 0x7ca7624 in ensureFuncBodyAnalyzed (zig)
            comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) {
                                    ^
/home/chief/workplace/zig-bootstrap/zig/src/Compilation.zig:3065:42: 0x7ca4f1d in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/chief/workplace/zig-bootstrap/zig/src/Compilation.zig:3003:30: 0x7b8acae in performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/home/chief/workplace/zig-bootstrap/zig/src/Compilation.zig:2331:31: 0x7b86c35 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/chief/workplace/zig-bootstrap/zig/src/main.zig:3337:20: 0x7bb3cb6 in updateModule (zig)
    try comp.update();
                   ^
/home/chief/workplace/zig-bootstrap/zig/src/main.zig:3008:17: 0x7a9debc in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/chief/workplace/zig-bootstrap/zig/src/main.zig:230:31: 0x7a77ed2 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/home/chief/workplace/zig-bootstrap/zig/src/main.zig:174:20: 0x7a775f0 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/chief/workplace/zig-bootstrap/zig/lib/std/start.zig:606:37: 0x7a79b81 in main (zig)
            const result = root.main() catch |err| {
                                    ^
Aborted

Expected Behavior

$ zig build-exe bug.zig -fstage1
$ ./bug 
info: { 5, 6 }

DerryAlex avatar Nov 03 '22 08:11 DerryAlex

(gdb) p self.ptr_otherwise.tag
Cannot access memory at address 0xaaaaaaaaaaaaaaaa
(gdb) p self.ptr_otherwise
$2 = (struct value.Value.Payload *) 0xaaaaaaaaaaaaaaaa
(gdb) p self
$3 = {tag_if_small_enough = -6148914691236517206, ptr_otherwise = 0xaaaaaaaaaaaaaaaa, ... 

nektro avatar Nov 03 '22 08:11 nektro