Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link/macho: segfault in dedupLiterals when using objc symbols #21598

Closed
slimsag opened this issue Oct 4, 2024 · 1 comment · Fixed by #21644
Closed

link/macho: segfault in dedupLiterals when using objc symbols #21598

slimsag opened this issue Oct 4, 2024 · 1 comment · Fixed by #21644
Assignees
Labels
bug Observed behavior contradicts documented or intended behavior linking os-macos
Milestone

Comments

@slimsag
Copy link
Contributor

slimsag commented Oct 4, 2024

Zig Version

0.14.0-dev.1710+8ee52f99c

Steps to Reproduce and Observed Behavior

We updated Mach to the latest Zig nightly version 0.14.0-dev.1710+8ee52f99c and found we were hitting a compiler segfault; I managed to create a minimal reproduction.

my macOS version: 14.1.1 (23B2082)

I do not have xcode installed:

% ls /Applications/Xcode.app
ls: /Applications/Xcode.app: No such file or directory

With this main.zig:

pub fn main() !void {
    const msg_send_fn = @extern(
        *const fn () callconv(.C) usize,
        .{ .name = "objc_msgSend$initWithContentRect:styleMask:backing:defer:screen:" },
    );
    _ = @call(
        .auto,
        msg_send_fn,
        .{},
    );
}

The following build-exe command segfaults:

% zig build-exe -ODebug -Mroot=./main.zig

zsh: segmentation fault  zig build-exe -ODebug -Mroot=./main.zig

Running under lldb shows the following:

% lldb -- zig build-exe -ODebug -Mroot=./main.zig
(lldb) target create "zig"
Current executable set to '/Users/slimsag/.bin/zig' (arm64).
(lldb) settings set -- target.run-args  "build-exe" "-ODebug" "-Mroot=./main.zig"
(lldb) run
Process 30140 launched: '/Users/slimsag/.bin/zig' (arm64)
Process 30140 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x14)
    frame #0: 0x0000000104fc9104 zig`link.MachO.dedupLiterals + 4960
zig`link.MachO.dedupLiterals:
->  0x104fc9104 <+4960>: ldur   x8, [x0, #0x14]
    0x104fc9108 <+4964>: lsr    x9, x8, #32
    0x104fc910c <+4968>: madd   x10, x25, x27, x20
    0x104fc9110 <+4972>: cmp    x25, #0x0
Target 0: (zig) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x14)
  * frame #0: 0x0000000104fc9104 zig`link.MachO.dedupLiterals + 4960
    frame #1: 0x0000000104e47eb0 zig`link.MachO.flushModule + 21672
    frame #2: 0x0000000104cdd0d4 zig`Compilation.flush + 732
    frame #3: 0x0000000104ce30b4 zig`Compilation.update + 17308
    frame #4: 0x0000000104ce50d4 zig`main.updateModule + 44
    frame #5: 0x0000000104d2268c zig`main.buildOutputType + 89460
    frame #6: 0x0000000104c44048 zig`main + 3004
    frame #7: 0x00000001878bd0e0 dyld`start + 2360

Suspecting it is related to #20032

cc @kubkon

Expected Behavior

No compiler segfault.

@slimsag slimsag added the bug Observed behavior contradicts documented or intended behavior label Oct 4, 2024
@slimsag slimsag changed the title link/macho: segfault in dedupLiterals when linking frameworks like AppKit link/macho: segfault in dedupLiterals when using objc symbols Oct 4, 2024
@slimsag
Copy link
Contributor Author

slimsag commented Oct 4, 2024

Updated issue description with an even simpler reproduction.

Also, because I know someone will ask, the extern name:

.name = "objc_msgSend$initWithContentRect:styleMask:backing:defer:screen:"

This is the extern name format that Objective C actually writes for a function like AppKit's function here: https://developer.apple.com/documentation/appkit/nswindow/1419477-initwithcontentrect

In mach-objc we have a code generator to invoke such functions, which boils down to an extern with that name - which you can see here and here for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior linking os-macos
Projects
None yet
3 participants