zig icon indicating copy to clipboard operation
zig copied to clipboard

C backend crashing when using C source file

Open BratishkaErik opened this issue 3 years ago • 3 comments

Zig Version

0.10.0-dev.4700+5f5a20eba

Steps to Reproduce and Observed Behavior

hello.c:

int main() {
    return 0;
}

Command:

zig build-exe hello.c -ofmt=c

Release:
[1]    20487 segmentation fault  zig build-exe hello.c -ofmt=c

Debug:
thread 32027 panic: attempt to use null value
???:?:?: 0x11f41a5 in ??? (???)
???:?:?: 0x11f4088 in ??? (???)
???:?:?: 0x1017165 in ??? (???)
???:?:?: 0x10168ca in ??? (???)
???:?:?: 0x1019ea5 in ??? (???)
???:?:?: 0x1043096 in ??? (???)
???:?:?: 0xf54071 in ??? (???)
???:?:?: 0xf32942 in ??? (???)
???:?:?: 0x10a5137 in ??? (???)
[1]    32027 IOT instruction  ~/zig-upstream/build/stage3/bin/zig build-exe hello.c -ofmt=c

Expected Behavior

I don't know (it's logically incorrect), but certainly not a panic/segmentation fault.

BratishkaErik avatar Oct 30 '22 03:10 BratishkaErik

~I get a different result:~ oops, I made a mistake testing

Here is with a debug build of the compiler:

[nix-shell:~/dev/zig/build]$ stage3/bin/zig build-exe test.c -ofmt=c
thread 1267225 panic: attempt to use null value
???:?:?: 0x73c14a5 in flushModule (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x73c1388 in flush (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x71e6dc5 in flush (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x71e652a in flush (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x71e9b05 in update (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x7216706 in updateModule (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x71379e1 in buildOutputType (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x71162b2 in mainArgs (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x7288af7 in main (/home/andy/dev/zig/src/zig_llvm.cpp)
???:?:?: 0x7f69b9afd236 in ??? (???)
???:?:?: 0x7ffe1e756e6e in ??? (???)
???:?:?: 0x620067697a2f6e68 in ??? (???)
Aborted (core dumped)

There is a second bug here of the stack trace not printing.

edit: I see you are trying some funny business with the new C backend improvements :laughing:

andrewrk avatar Oct 30 '22 03:10 andrewrk

if it may help:

link.C.flushModule () at link/C.zig:251
link.C.flush () at link/C.zig:239
link.File.flush () at link.zig:794
Compilation.flush () at Compilation.zig:2458
Compilation.update () at Compilation.zig:2439
main.updateModule () at main.zig:3337
main.buildOutputType () at main.zig:3008
main.mainArgs () at main.zig:230
main () at stage1.zig:56

also

 "version": "0.10.0-dev.4733+d02242661",
 "target": "x86_64-linux.6.0.5...6.0.5-gnu.2.36"

might have regressed because it just spins until it runs out of memory

thread 2815 panic: attempt to use null value
Unable to dump stack trace: OutOfMemory

llogick avatar Oct 31 '22 08:10 llogick

Duplicate/related #10612

Vexu avatar Nov 01 '22 18:11 Vexu

Can still reproduce this on 0.12.0-dev.601+f4c884617:

# debug build
$ zig build-exe hello.c -ofmt=c

thread 12057 panic: attempt to use null value
/home/bratishkaerik/github.com/zig/src/link/C.zig:246:44: 0xb988dd in flushModule (zig)
    const module = self.base.options.module.?;
                                           ^
/home/bratishkaerik/github.com/zig/src/link/C.zig:222:28: 0xb9b424 in flush (zig)
    return self.flushModule(comp, prog_node);
                           ^
/home/bratishkaerik/github.com/zig/src/link.zig:794:64: 0x90971e in flush (zig)
            .c => return @fieldParentPtr(C, "base", base).flush(comp, prog_node),
                                                               ^
/home/bratishkaerik/github.com/zig/src/Compilation.zig:2349:24: 0x908e65 in flush (zig)
    comp.bin_file.flush(comp, prog_node) catch |err| switch (err) {
                       ^
/home/bratishkaerik/github.com/zig/src/Compilation.zig:2306:23: 0x9476d7 in update (zig)
        try comp.flush(main_progress_node);
                      ^
/home/bratishkaerik/github.com/zig/src/main.zig:4163:24: 0x975420 in updateModule (zig)
        try comp.update(main_progress_node);
                       ^
/home/bratishkaerik/github.com/zig/src/main.zig:3579:17: 0x9986d2 in buildOutputType (zig)
    updateModule(comp) catch |err| switch (err) {
                ^
/home/bratishkaerik/github.com/zig/src/main.zig:269:31: 0x7f26cf in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/home/bratishkaerik/github.com/zig/src/main.zig:213:20: 0x7efa55 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/bratishkaerik/github.com/zig/lib/std/start.zig:486:37: 0x7ef46e in main (zig)
    std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@as(usize, @intCast(c_argc))];
                                    ^
???:?:?: 0x7fd61744a8c9 in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7fd61744a8c9` was not available, trace may be incomplete

[1]    12057 IOT instruction  ~/github.com/zig/zig-out/bin/zig build-exe hello.c -ofmt=c

BratishkaErik avatar Sep 26 '23 16:09 BratishkaErik