Skip to content

[compiler-rt] Don't rely on automatic codesigning with Apple's linker #91681

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

Merged
merged 1 commit into from
May 30, 2024

Conversation

bdash
Copy link
Contributor

@bdash bdash commented May 9, 2024

In #88323, I changed the logic within add_compiler_rt_runtime to only explicitly code sign the resulting library if an older version of Apple's ld64 was in use. This was based on the assumption that newer versions of ld64 and the new Apple linker always ad-hoc sign their output binaries. This is true in most cases, but not when using Apple's new linker with the -darwin-target-variant flag to build Mac binaries that are compatible with Catalyst.

Rather than adding increasingly complicated logic to detect the exact scenarios that require explicit code signing, I've opted to always explicitly code sign when using any Apple linker. We instead detect and use the 'linker-signed' codesigning option when possible to match the signatures that the linker would otherwise create. This avoids having non-'linker-signed' ad-hoc signatures which was the underlying problem that #88323 was intended to address.

In llvm#88323, I changed the logic
within `add_compiler_rt_runtime` to only explicitly code sign the
resulting library if an older version of Apple's ld64 was in use. This
was based on the assumption that newer versions of ld64 and the new
Apple linker always ad-hoc sign their output binaries. This is true in
most cases, but not when using Apple's new linker with the
'-darwin-target-variant' flag to build Mac binaries that are compatible
with Catalyst.

Rather than adding increasingly complicated logic to detect the exact
scenarios that require explicit code signing, I've opted to always
explicitly code sign when using any Apple linker. We instead detect and
use the 'linker-signed' codesigning option when possible to match the
signatures that the linker would otherwise create. This avoids having
non-'linker-signed' ad-hoc signatures which was the underlying problem that
llvm#88323 intended to address.
@bdash
Copy link
Contributor Author

bdash commented May 9, 2024

@cjappl, this should address the issue you mentioned in #88323.

@bdash
Copy link
Contributor Author

bdash commented May 9, 2024

@yln, would you mind reviewing this follow-up to #88323?

@cjappl
Copy link
Contributor

cjappl commented May 10, 2024

I can confirm that this fixes the problem I reported. I am now able to build and run check-asan and others with no code-signing issues on the two macs I have access to.

Much appreciated @bdash

COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
RESULT_VARIABLE HAD_ERROR
OUTPUT_VARIABLE LD_V_OUTPUT
COMMAND sh -c "codesign -f -s - -o linker-signed this-does-not-exist 2>&1 | grep -q linker-signed"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is clever! Hopefully not too brittle? Only other thing I can think of is man codesign | grep linker-signed, but I like that less because it uses a completely other command (man).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wish there were a more direct way to test for support, but this was the best I could come up with. I've verified the format of errors for unknown -o values is the same from codesign on macOS 10.15 through macOS 14.4, so hopefully it'll stay consistent going forwards too.

# most situations, except:
# 1. Versions of ld64 prior to ld64-609 in Xcode 12 predate this behavior.
# 2. Apple's new linker does not when building with `-darwin-target-variant`
# to support macOS Catalyst.
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to this (and assuming we can drop support for the old linker) would be to ensure the linker behaves consistently and always codesigns, right?

(Not asking for this in this PR, just wanted to make sure I understand.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this would all need to remain even if support is dropped for the old linker, purely for Catalyst scenario. The detection of -o linker-signed will be able to go away at that point since all versions of the developer tools that support the new linker also support that option.

@yln
Copy link
Collaborator

yln commented May 15, 2024

LGTM, thanks @bdash!

@bdash
Copy link
Contributor Author

bdash commented May 24, 2024

@yln What needs to be done to merge this? Does someone with commit access need to hit the merge button?

@yln
Copy link
Collaborator

yln commented May 24, 2024

Ah, I didn't realize to you don't have PR merge privileges / commit access.

I will merge this for you on Tue morning (Mo is holiday in US) to avoid landing this just before the weekend reduce friction. Feel free to ping me if I haven't done this by Tue evening.

If something goes wrong, people will comment here (and are allowed to revert our change). Please respond in a timely matter.

Adding reviewers for visibility: @thetruestblue @wrotki

@yln yln requested review from rsundahl, wrotki and thetruestblue May 24, 2024 21:01
@bdash
Copy link
Contributor Author

bdash commented May 24, 2024

Sounds good, thank you!

@bdash
Copy link
Contributor Author

bdash commented May 29, 2024

@yln Any chance you'll be able to get it in this week?

@yln yln merged commit 815250b into llvm:main May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants