This is an archive of the discontinued LLVM Phabricator instance.

tsan: fix false positives in dynamic libs with static tls
ClosedPublic

Authored by dvyukov on Dec 2 2021, 6:24 AM.

Details

Summary

The added test demonstrates loading a dynamic library with static TLS.
Such static TLS is a hack that allows a dynamic library to have faster TLS,
but it can be loaded only iff all threads happened to allocate some excess
of static TLS space for whatever reason. If it's not the case loading fails with:

dlopen: cannot load any more object with static TLS

We used to produce a false positive because dlopen will write into TLS
of all existing threads to initialize/zero TLS region for the loaded library.
And this appears to be racing with initialization of TLS in the thread
since we model a write into the whole static TLS region (we don't what part
of it is currently unused):

WARNING: ThreadSanitizer: data race (pid=2317365) Write of size 1 at 0x7f1fa9bfcdd7 by main thread: 0 memset 1 init_one_static_tls 2 pthread_init_static_tls [[ this is where main calls dlopen ]] 3 main Previous write of size 8 at 0x7f1fa9bfcdd0 by thread T1: 0 tsan_tls_initialization

Fix this by ignoring accesses during dlopen.

Diff Detail

Event Timeline

dvyukov requested review of this revision.Dec 2 2021, 6:24 AM
dvyukov created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 2 2021, 6:24 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Dec 2 2021, 8:08 AM
melver added inline comments.
compiler-rt/test/tsan/Linux/dlopen_static_tls.cpp
13

"we don't know what.."

But on a whole this makes sense.
Is the reason for not reporting this hack that it's pretty intentional and whoever wants to do this knows the risks?

This revision is now accepted and ready to land.Dec 2 2021, 8:08 AM
dvyukov added inline comments.Dec 2 2021, 8:27 AM
compiler-rt/test/tsan/Linux/dlopen_static_tls.cpp
13

Is the reason for not reporting this hack that it's pretty intentional and whoever wants to do this knows the risks?

It is synchronized inside of the dynamic linker (hopefully), we just don't see that synchronization.
Plus we imitate write to the whole TLS range on thread start in __tsan_tls_initialization, which is formally not happening.
Overall what this test is doing legal as far as I understand.

dvyukov updated this revision to Diff 391340.Dec 2 2021, 8:46 AM

add missing word in the comment

dvyukov marked an inline comment as done.Dec 2 2021, 8:46 AM
dvyukov added inline comments.
compiler-rt/test/tsan/Linux/dlopen_static_tls.cpp
13

"we don't know what.."

Done

This revision was landed with ongoing or failed builds.Dec 2 2021, 8:47 AM
This revision was automatically updated to reflect the committed changes.