ziglang / zig

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

When using wayland-client functions on LLVM backend, app segfaults on startup #18390

Open Beyley opened 8 months ago

Beyley commented 8 months ago

Zig Version

0.12.0-dev.1856+94c63f31f

Steps to Reproduce and Observed Behavior

Clone https://github.com/Beyley/zwin at commit 037c43f473f9763657fdb4a65476a35f18b3e559, on a linux machine run zig build run App will segfault immediately on startup, with no nice error trace, running through LLDB reveals some weirdness is going on with the generated assembly calling posixCallMainAndExit

stack trace on crash:

Segmentation fault at address 0x0
???:?:?: 0x0 in ??? (???)
(lldb) dis
zwin`:
    0x21cd30 <+0>:  xorl   %ebp, %ebp
    0x21cd32 <+2>:  movq   %rsp, 0x942c7(%rip)       ; start.argc_argv_ptr
->  0x21cd39 <+9>:  andq   $-0x10, %rsp
    0x21cd3d <+13>: callq  0x21cd50                  ; start.posixCallMainAndExit at start.zig:365
    0x21cd42 <+18>: ud2    
(lldb) ni
Process 103110 stopped
* thread #1, name = 'zwin', stop reason = instruction step over
    frame #0: 0x000000000021cd3d zwin`_start at start.zig:253:5
   250          }),
   251          else => {},
   252      }
-> 253      asm volatile (switch (native_arch) {
   254              .x86_64 =>
   255              \\ xorl %%ebp, %%ebp
   256              \\ movq %%rsp, %[argc_argv_ptr]
(lldb) dis
zwin`:
    0x21cd30 <+0>:  xorl   %ebp, %ebp
    0x21cd32 <+2>:  movq   %rsp, 0x942c7(%rip)       ; start.argc_argv_ptr
    0x21cd39 <+9>:  andq   $-0x10, %rsp
->  0x21cd3d <+13>: callq  0x21cd50                  ; start.posixCallMainAndExit at start.zig:365
    0x21cd42 <+18>: ud2    
(lldb) ni
Process 103110 stopped
* thread #1, name = 'zwin', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x0000000000000000
error: memory read failed for 0x0

Expected Behavior

App builds and runs successfully, as it does on the selfhosted backend (uncomment lines 15-16 in build.zig)

┌─[beyley@arch] - [~/Projects/PROGRAMMING/Zig/zwin] - [2023-12-28 12:55:34]
└─[0] <git:(master 037c43f✈) > zig build run
debug: opened wayland display cimport.struct_wl_display@1517b2a0
debug: got display registry cimport.struct_wl_registry@1517f470
debug: got registry event for wl_compositor, id: 1
...
Vexu commented 8 months ago

Wayland-client likely requires libc to be linked.

Beyley commented 8 months ago

Wayland-client likely requires libc to be linked.

Ah, yeah that was it, that thought completely didnt pass my mind, is there no way to provide a better error for that this kind of scenario? an instant segfault at address 0x0 with no extra info is very hard to debug

xdBronch commented 8 months ago

why does the x86 backend provide an error without libc?

Beyley commented 8 months ago

why does the x86 backend provide an error without libc?

It actually runs correctly under the selfhosted backend, no error is produced

xdBronch commented 8 months ago

ah right, I'm getting an error because I don't have Wayland but my question still stands, how is the x86 backend behaving correctly?