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

Fix segfaults when static linking with musl #9238

Merged
merged 1 commit into from
May 7, 2020

Conversation

waj
Copy link
Member

@waj waj commented May 6, 2020

This is a workaround for segfaults that are showing up when raising exceptions in musl and the binary was statically linked.

The issue is with pthread_once. In musl is defined as a weak symbol:

$ nm /usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/libgcc.a
...
                 w pthread_once
...

The unwind API implementation uses this function, but through a wrapper __gthread_once: https://github.com/gcc-mirror/gcc/blob/8d9254fc8aa32619f640efb01cfe87cc6cdc9ce1/libgcc/unwind-dw2.c#L1597-L1600

But the implementation of __gthread_once refers to the weak version of the function:
https://github.com/gcc-mirror/gcc/blob/8d9254fc8aa32619f640efb01cfe87cc6cdc9ce1/libgcc/gthr-posix.h#L696-L703

For some reason I don't fully understand yet, that makes the linker to not include that function in the final binary.

Note also that __gthread_once checks __gthread_active_p to see if the current code is multithreaded or not. It's a hack that looks for other existing symbols within the same binary. That's why when linking with the GC it failed but it doesn't when compiling with -Dgc_none: the GC uses multithreaded code and thus links to pthread_create and other symbols.

Thank you very much to @ggiraldez for help with this debugging!!!

NOTE: the backtraces still cannot be decoded with static linking, but that's a different issue

Fixes: #4276, #6934

@bcardiff bcardiff added kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:linux-musl topic:stdlib:runtime labels May 6, 2020
@bcardiff bcardiff added this to the 0.35.0 milestone May 6, 2020
Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's amazing debugging, thank you!!

@bcardiff
Copy link
Member

bcardiff commented May 7, 2020

I smoke tested the build process and shards binary without the hack on 64/32 and everything seems to work fine.

@j8r
Copy link
Contributor

j8r commented May 7, 2020

Should we now create a new issue to track the empty strack traces problem for static linking against musl?

@waj
Copy link
Member Author

waj commented May 7, 2020

@j8r sure, I'll do it. It's a totally unrelated issue. I've been doing some research and I have a possible solution already.

@j8r
Copy link
Contributor

j8r commented May 7, 2020

That's a really great work @waj , thanks a lot.

I didn't see Crystal reaching 1.0 without this essential issues solved, static linking is a very good selling point for the language: easy application distribution.
Go has it too, but it is not as easy to link statically. Last time, I had to set LDFLAGS='-extldflags "-static -fuse-ld=bfd"' (?!).

@rdp
Copy link
Contributor

rdp commented May 8, 2020

Should this also be reported upstream somewhere? Just wondering...

@waj
Copy link
Member Author

waj commented May 10, 2020

@j8r well... I didn't open the issue, but just sent a PR instead: #9267 😉

@waj
Copy link
Member Author

waj commented May 10, 2020

@rdp I'm not sure where the bug actually is. Anyway, I have a really small example in plain C to reproduce the issue, so I might send it to Alpine.

@waj waj deleted the fix/musl-static-segfault branch June 8, 2020 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:linux-musl topic:stdlib:runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static linking + unwinding are flat-out broken under Alpine Linux
6 participants