ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.26k stars 2.5k forks source link

ICE: deref of null value in llvm.updateExports #20847

Closed kristoff-it closed 3 weeks ago

kristoff-it commented 2 months ago

Zig Version

0.14.0-dev.656+c15755092

Steps to Reproduce and Observed Behavior

git clone https://github.com/kristoff-it/buzz-repro
cd buzz-repro
zig build

Expected Behavior

Not this crash:

Process 38390 launched: '/Users/kristoff/zig-src/build/stage3/bin/zig' (arm64)
Process 38390 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
    frame #0: 0x000000010aeba77c zig`link.MachO.writeLoadCommands + 1380
zig`link.MachO.writeLoadCommands:
->  0x10aeba77c <+1380>: ldr    w1, [x0, #0x10]
    0x10aeba780 <+1384>: add    x0, sp, #0x100
    0x10aeba784 <+1388>: mov    x2, x24
    0x10aeba788 <+1392>: bl     0x10ad1c598    ; link.MachO.Symbol.getFile
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
  * frame #0: 0x000000010aeba77c zig`link.MachO.writeLoadCommands + 1380
    frame #1: 0x000000010ad77720 zig`link.MachO.flushModule + 20324
    frame #2: 0x000000010abcda14 zig`Compilation.flush + 672
    frame #3: 0x000000010abd164c zig`Compilation.update + 9540
    frame #4: 0x000000010abe7b1c zig`main.updateModule + 44
    frame #5: 0x000000010ac18c30 zig`main.buildOutputType + 51300
    frame #6: 0x000000010ab5d230 zig`main + 2568
    frame #7: 0x000000018b3aa0e0 dyld`start + 2360
(lldb)
kubkon commented 2 months ago

Thanks! It would be great if you could re-run with a debug build version of the compiler - we would know exactly where we panic.

EDIT: in the meantime, I'll try to repro using the provided steps.

kubkon commented 2 months ago

This actually might not be a linker issue. Here's the output I got using a debug build of the compiler:

$ stage4/bin/zig build
install
└─ install buzz
   └─ zig build-exe buzz Debug native failure
error: thread 9803510 panic: attempt to use null value
/Users/kubkon/dev/zig/src/codegen/llvm.zig:1731:59: 0x1064e12fb in updateExports (zig)
        const global_index = self.decl_map.get(decl_index).?;
                                                          ^
/Users/kubkon/dev/zig/src/link/MachO.zig:3027:73: 0x1067a7347 in updateExports (zig)
    if (self.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices);
                                                                        ^
/Users/kubkon/dev/zig/src/link.zig:666:85: 0x1064e09e7 in updateExports (zig)
                return @as(*tag.Type(), @fieldParentPtr("base", base)).updateExports(pt, exported, export_indices);
                                                                                    ^
/Users/kubkon/dev/zig/src/Zcu/PerThread.zig:2487:69: 0x1062ae793 in processExportsInner (zig)
        try zcu.handleUpdateExports(export_indices, lf.updateExports(pt, exported, export_indices));
                                                                    ^
/Users/kubkon/dev/zig/src/Zcu/PerThread.zig:2429:35: 0x1062ad80f in processExports (zig)
        try pt.processExportsInner(&symbol_exports, exported, exports_list.items);
                                  ^
/Users/kubkon/dev/zig/src/Compilation.zig:2312:30: 0x1062cfbc3 in update (zig)
        try pt.processExports();
                             ^
/Users/kubkon/dev/zig/src/main.zig:4143:32: 0x10634052f in serve (zig)
                try comp.update(main_progress_node);
                               ^
/Users/kubkon/dev/zig/src/main.zig:3440:22: 0x10635d9ef in buildOutputType (zig)
            try serve(
                     ^
/Users/kubkon/dev/zig/src/main.zig:258:31: 0x1061d240b in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/Users/kubkon/dev/zig/src/main.zig:199:20: 0x1061cf9f7 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/Users/kubkon/dev/zig/lib/std/start.zig:524:37: 0x1061cf6db in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x18850a0df in ??? (???)
???:?:?: 0xbf70ffffffffffff in ??? (???)

error: the following command terminated unexpectedly:
/Users/kubkon/dev/zig/build/stage4/bin/zig build-exe -lmir /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/4e6ae5cf168a12ee39411af8577e1eaa/libbuzz.dylib -rpath /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/4e6ae5cf168a12ee39411af8577e1eaa /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/1a7cfe65de020b43d186f4ec8e539606/liblinenoise.a -ODebug -I /Users/kubkon/dev/zld-examples/buzz-repro/vendors/mir -I /Users/kubkon/dev/zld-examples/buzz-repro/vendors/mimalloc/include -I /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/a3b3c8d3bf72fde89e039bb72531adac -I /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/a3b3c8d3bf72fde89e039bb72531adac -L /Users/kubkon/dev/zld-examples/buzz-repro/vendors/mir --dep build_options -Mroot=/Users/kubkon/dev/zld-examples/buzz-repro/src/main.zig -Mbuild_options=/Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/c/699a757a6853327c812d3a7b2777fbe0/options.zig -lc --cache-dir /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache --global-cache-dir /Users/kubkon/.cache/zig --name buzz --zig-lib-dir /Users/kubkon/dev/zig/lib/ --listen=- 
Build Summary: 51/54 steps succeeded; 1 failed
install transitive failure
├─ install buzz transitive failure
│  └─ zig build-exe buzz Debug native failure
└─ zig build-exe buzz Debug native (+5 more reused dependencies)
error: the following build command failed with exit code 1:
/Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache/o/1413971f5860e1039ccc43edcf6ca0b2/build /Users/kubkon/dev/zig/build/stage4/bin/zig /Users/kubkon/dev/zig/lib /Users/kubkon/dev/zld-examples/buzz-repro /Users/kubkon/dev/zld-examples/buzz-repro/.zig-cache /Users/kubkon/.cache/zig --seed 0x2284378a -Zb9b09a02b18fba0d

Also note that using a release build of the compiler I do hit the same segfault as Loris.

kubkon commented 2 months ago

@mlugg do I remember right you tweaked some of the exports logic in the compiler recently?

mlugg commented 2 months ago

Yep, this'll be my fault! I'll see if I can investigate when I'm done with my current big branch.

giann commented 2 months ago

@mlugg do you think you'll be able to have a look in the coming days?

tau-dev commented 1 month ago

@mlugg If you could provide any hints on the probable cause, I'd be happy to try and fix this too.