[compiler-rt] f9a706a - [TSan][Darwin] Avoid calling pthread_self() before libpthread is initialized

Julian Lettner via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 16 13:11:50 PST 2019


Author: Julian Lettner
Date: 2019-12-16T13:10:51-08:00
New Revision: f9a706a36ae5dcb87b0b6649d0c80e989400f48e

URL: https://github.com/llvm/llvm-project/commit/f9a706a36ae5dcb87b0b6649d0c80e989400f48e
DIFF: https://github.com/llvm/llvm-project/commit/f9a706a36ae5dcb87b0b6649d0c80e989400f48e.diff

LOG: [TSan][Darwin] Avoid calling pthread_self() before libpthread is initialized

This skips calling `pthread_self` when `main_thread_identity` hasn't
been initialized yet.  `main_thread_identity` is only ever assigned in
`__tsan::InitializePlatform`.  This change should be relatively safe; we
are not changing behavior other than skipping the call to `pthread_self`
when `main_thread_identity == 0`.

rdar://57822138

Reviewed By: kubamracek

Differential Revision: https://reviews.llvm.org/D71559

Added: 
    

Modified: 
    compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
index 326ca8532e52..ae65dd3fd995 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
@@ -75,9 +75,14 @@ static uptr main_thread_identity = 0;
 ALIGNED(64) static char main_thread_state[sizeof(ThreadState)];
 static ThreadState *main_thread_state_loc = (ThreadState *)main_thread_state;
 
+// We cannot use pthread_self() before libpthread has been initialized.  Our
+// current heuristic for guarding this is checking `main_thread_identity` which
+// is only assigned in `__tsan::InitializePlatform`.
 static ThreadState **cur_thread_location() {
+  if (main_thread_identity == 0)
+    return &main_thread_state_loc;
   uptr thread_identity = (uptr)pthread_self();
-  if (thread_identity == main_thread_identity || main_thread_identity == 0)
+  if (thread_identity == main_thread_identity)
     return &main_thread_state_loc;
   return (ThreadState **)MemToShadow(thread_identity);
 }


        


More information about the llvm-commits mailing list