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 santizers on MacOS that require dylibs in the LLVM toolchain. #250

Merged
merged 3 commits into from
Mar 13, 2024

Conversation

timbess
Copy link
Contributor

@timbess timbess commented Feb 3, 2024

Add dylibs to the lib target so that santizers work properly on MacOS.

Should address #192.

@timbess timbess requested a review from fmeum as a code owner March 13, 2024 05:17
@siddharthab
Copy link
Contributor

Thanks for the contribution. I am not knowledgeable enough to be confident if this is the right approach, but let's land this and see what happens.

@siddharthab siddharthab merged commit 3a0a496 into bazel-contrib:master Mar 13, 2024
35 checks passed
link_flags.extend([
"-Bstatic",
"-lunwind",
"-Bdynamic",
Copy link
Member

Choose a reason for hiding this comment

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

This seems to contradict the comment, wouldn't we want to link libraries from the toolchain (not the sysroot) statically instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment says that we should dynamically link libc++ and libc++abi from the sysroot, instead of static linking from the toolchain. I don't have a good memory of why I wanted to do that beyond what the comment says.

@timbess Can you comment a little on why you needed to statically link libunwind here?

Copy link
Member

Choose a reason for hiding this comment

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

note that in general this is wrong for macOS since -Bstatic and -Bdynamic are not supported, but since clang is the linker they are accepted and just ignored. This results in libunwind being linked dynamically but no rpaths being set on the binary and a runtime crash similar to #224

Copy link
Member

Choose a reason for hiding this comment

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

@keith Should we revert or do you see a simple way to fix this?

Copy link
Member

Choose a reason for hiding this comment

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

maybe a better solution for libunwind would be to just exclude it from the glob above on macOS so that the system one has to be used?

Copy link
Member

Choose a reason for hiding this comment

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

I think this wasn't spotted because you actually have to use something from it for it to be an issue, which is the case we hit

Copy link
Contributor Author

@timbess timbess Mar 29, 2024

Choose a reason for hiding this comment

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

Sorry didn't see this until now somehow... If I remember right, I was trying to get all the sanitizers working which it wouldn't find without the dylibs in the glob, but then it would end up dynamically linking libunwind and fail with an RPATH error. There was a static archive in the same directory, but ld64 would prefer the dylib unless I set -Bstatic after which it didn't show as being dynamically linked when I ran otool -L, but maybe @keith is right and I just effectively hid the error until any libunwind symbols are actually used?

Ah ok so I just replicated it. If I comment out the -Bstatic I get this:
image

But with the static linking I get:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

I had some comments about this behaviour in the code before this change. Reverting to some of that may solve the issues here.

Copy link
Contributor Author

@timbess timbess Mar 29, 2024

Choose a reason for hiding this comment

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

Seems like I'm getting libunwind from the actual LLVM toolchain, not the sysroot.
image

I think the issue was that it was dynamically linking to a library that was symlinked into the sandbox, but the Bazel output dir isn't in the RPATH of the final binary, so it'd fail to find libunwind at boot. But it seems to be respecting the static linking flags from what I can tell.

ilyailya added a commit to sorbet/bazel-toolchain that referenced this pull request Apr 22, 2024
jez pushed a commit to sorbet/bazel-toolchain that referenced this pull request Apr 30, 2024
…zel-contrib#250)

Add dylibs to the `lib` target so that santizers work properly on MacOS.

Resolves bazel-contrib#192.

---------

Co-authored-by: Siddhartha Bagaria <starsid@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants