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

Julian Lettner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 16 11:03:10 PST 2019


yln created this revision.
Herald added subscribers: llvm-commits, Sanitizers, jfb.
Herald added projects: Sanitizers, LLVM.
yln added reviewers: kubamracek, delcypher, dcoughlin.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71559

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


Index: compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
===================================================================
--- compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
+++ compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
@@ -75,9 +75,14 @@
 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);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71559.234107.patch
Type: text/x-patch
Size: 986 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191216/6be40d81/attachment.bin>


More information about the llvm-commits mailing list