diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp --- a/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp @@ -76,16 +76,19 @@ ALIGNED(64) static char main_thread_state[sizeof(ThreadState)]; static ThreadState *main_thread_state_loc = (ThreadState *)main_thread_state; +static ThreadState **thread_location(uptr thread_identity) { + if (thread_identity == main_thread_identity) + return &main_thread_state_loc; + return (ThreadState **)MemToShadow(thread_identity); +} + // 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) - return &main_thread_state_loc; - return (ThreadState **)MemToShadow(thread_identity); + return thread_location((uptr)pthread_self()); } ThreadState *cur_thread() { @@ -209,6 +212,10 @@ static void my_pthread_introspection_hook(unsigned int event, pthread_t thread, void *addr, size_t size) { if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) { + CHECK_NE(thread, main_thread_identity); + // Reset ThreadState pointer in shadow memory which could contain non-zero + // bytes from unobserved operations (e.g., IOKit). + *thread_location((uptr)thread) = nullptr; if (thread == pthread_self()) { // The current thread is a newly created GCD worker thread. ThreadState *thr = cur_thread();