-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
There was a problem hiding this 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!!
I smoke tested the build process and shards binary without the hack on 64/32 and everything seems to work fine. |
Should we now create a new issue to track the empty strack traces problem for static linking against musl? |
@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. |
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. |
Should this also be reported upstream somewhere? Just wondering... |
@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. |
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: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-L1600But 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 topthread_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