This is an archive of the discontinued LLVM Phabricator instance.

sanitizer_common: Use plain thread_local for __sancov_lowest_stack definition.
ClosedPublic

Authored by pcc on Mar 17 2023, 9:03 PM.

Details

Summary

We can't use initial-exec for the definition of __sancov_lowest_stack
because it gets linked into shared libraries such as the UBSan and
HWASan runtimes on Android. I think we can expect plain thread_local
to work unconditionally in sanitizer_common as several other sanitizers
are already using it, so drop the platform-dependent macro and just use
thread_local in the definition.

Diff Detail

Event Timeline

pcc created this revision.Mar 17 2023, 9:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2023, 9:03 PM
pcc requested review of this revision.Mar 17 2023, 9:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2023, 9:03 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
hctim accepted this revision.Mar 20 2023, 10:28 AM

Any ideas why this was IE in the first place? Don't see any notes in the patch that added it (https://reviews.llvm.org/D37156)?

This revision is now accepted and ready to land.Mar 20 2023, 10:28 AM
vitalybuka accepted this revision.EditedMar 20 2023, 11:10 AM

I guess Android was that main why we didn't use it before. Probably older API levels. Probably our buildbot is still on them. Wide use of thread_local may simplify sanitizers.

As I see thread_local is used in msan, which is not on Android, fuzzers, and asan fuchsia and netbsd.
However it's used in scudo, which is on Android

+Apple folks for any concerns.

yln added a subscriber: thetruestblue.

Hi, thanks Vitaly!

AFAIR, thread_local (and TLS in general) require macOS 10.12 / iOS 10 / watchOS 3 or greater.

Therefore, can we hold off on this a bit?

@thetruestblue is looking into bumping the minimal sanitizer deployment targets for Apple platforms. Blue, when you do that, can you circle back here and apply this patch? This change is a good example for a sanity check: it doesn't work right now but should work afterwards.

This revision now requires review to proceed.Mar 20 2023, 12:19 PM
pcc added a comment.Mar 20 2023, 1:11 PM

Hi, thanks Vitaly!

AFAIR, thread_local (and TLS in general) require macOS 10.12 / iOS 10 / watchOS 3 or greater.

Therefore, can we hold off on this a bit?

@thetruestblue is looking into bumping the minimal sanitizer deployment targets for Apple platforms. Blue, when you do that, can you circle back here and apply this patch? This change is a good example for a sanity check: it doesn't work right now but should work afterwards.

__sancov_lowest_stack is only used by the sancov instrumentation pass when -fsanitize-coverage=stack-depth is passed, which is not the default. This option can't possibly work if SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE was defined to an empty string, because the instrumentation pass will unconditionally define the variable with TLS: https://github.com/llvm/llvm-project/blob/f67b481098cc30567d0f50a2b21f8f57b92052bd/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp#L489

So I think this is broken on the platforms where SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE is an empty string. That means we can wrap the definition of __sancov_lowest_stack in an #if !SANITIZER_APPLE and the code will work on Apple platforms as well as it did before (i.e. the runtime will build, but the compiler flag won't work). I'll update the patch to do that.

pcc updated this revision to Diff 506703.Mar 20 2023, 1:46 PM

Add #if !SANITIZER_APPLE

This revision was not accepted when it landed; it landed in state Needs Review.Mar 20 2023, 4:26 PM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.