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

threadlocal generates a segfault on Apple Silicon #7527

Closed
kubkon opened this issue Dec 22, 2020 · 0 comments · Fixed by #8282
Closed

threadlocal generates a segfault on Apple Silicon #7527

kubkon opened this issue Dec 22, 2020 · 0 comments · Fixed by #8282
Labels
arch-aarch64 64-bit ARM os-macos upstream An issue with a third party project that Zig uses.
Milestone

Comments

@kubkon
Copy link
Member

kubkon commented Dec 22, 2020

The following snippet:

const std = @import("std");
const assert = std.debug.assert;

threadlocal var x: i32 = 1234;

pub fn main() anyerror!void {
    const thread1 = try std.Thread.spawn({}, testTls);
    const thread2 = try std.Thread.spawn({}, testTls);
    testTls({});
    thread1.wait();
    thread2.wait();
}

fn testTls(context: void) void {
    assert(x == 1234);
    x += 1;
    assert(x == 1235);
}

compiles and cross-compiles fine on aarch64-macos-gnu; however, generates a SEGFAULT when run due to a deficiency in LLVM's LLD linker. The same snippet works fine when linked using Apple's system linker ld or on x86_64 using LLD.

@kubkon kubkon added os-macos upstream An issue with a third party project that Zig uses. arch-aarch64 64-bit ARM labels Dec 22, 2020
kubkon added a commit to kubkon/zig that referenced this issue Dec 22, 2020
kubkon added a commit to kubkon/zig that referenced this issue Dec 23, 2020
@Vexu Vexu added this to the 0.9.0 milestone Dec 23, 2020
kubkon added a commit to kubkon/zig that referenced this issue Jan 20, 2021
This commit brings back stack trace printing on ARM Darwin OSes by
avoiding the use of `threadlocal` for keeping track of panic stages per
thread. Essentially, LLVM/LLD fails to properly emit thread-local variables
which causes a segfault on attempting access. Since the default panic
handler relies on a threadlocal to keep track of panic stages per
thread using a thread-local variable, we ensure at compilation time
that ARM Darwin platforms explicitly opt-out from this in favour of
storing the panic stages per thread in a hash-map behind a mutex.
This is a temporary solution until the underlying issue is fixed
but much welcome one since this brings back stack traces printouts
on ARM macOS without the necessity to resort to the system linker and
still get broken stack traces due to addressing differences between the
system linker and LLD. For a more complete overview of the problem with
`threadlocal` in LLVM, see ziglang#7527.
kubkon added a commit to kubkon/zig that referenced this issue Jan 21, 2021
This temporary patch fixes a segfault caused by miscompilation
by the LLD when generating stubs for initialization of thread local
storage. We effectively bypass TLS in the default panic handler
so that no segfault is generated and the stack trace is correctly
reported back to the user.

Note that, this is linked directly to a bigger issue with LLD
ziglang#7527 and when resolved, we only need to remove the
`comptime` code path introduced with this patch to use the default
panic handler that relies on TLS.

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
kubkon added a commit that referenced this issue Jan 21, 2021
This temporary patch fixes a segfault caused by miscompilation
by the LLD when generating stubs for initialization of thread local
storage. We effectively bypass TLS in the default panic handler
so that no segfault is generated and the stack trace is correctly
reported back to the user.

Note that, this is linked directly to a bigger issue with LLD
#7527 and when resolved, we only need to remove the
`comptime` code path introduced with this patch to use the default
panic handler that relies on TLS.

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
dgbuckley pushed a commit to dgbuckley/zig that referenced this issue Mar 9, 2021
This temporary patch fixes a segfault caused by miscompilation
by the LLD when generating stubs for initialization of thread local
storage. We effectively bypass TLS in the default panic handler
so that no segfault is generated and the stack trace is correctly
reported back to the user.

Note that, this is linked directly to a bigger issue with LLD
ziglang#7527 and when resolved, we only need to remove the
`comptime` code path introduced with this patch to use the default
panic handler that relies on TLS.

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
@kubkon kubkon modified the milestones: 0.9.0, 0.8.0 Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-aarch64 64-bit ARM os-macos upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants