tsan: print a meaningful frame for stack races
authorDmitry Vyukov <dvyukov@google.com>
Mon, 27 Sep 2021 12:07:28 +0000 (14:07 +0200)
committerDmitry Vyukov <dvyukov@google.com>
Tue, 28 Sep 2021 15:08:00 +0000 (17:08 +0200)
Depends on D110631.

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

compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cpp

index 67b12c8..61133a4 100644 (file)
@@ -138,6 +138,8 @@ void ThreadContext::OnCreated(void *arg) {
   creation_stack_id = CurrentStackId(args->thr, args->pc);
 }
 
+extern "C" void __tsan_stack_initialization() {}
+
 struct OnStartedArgs {
   ThreadState *thr;
   uptr stk_addr;
@@ -173,9 +175,15 @@ void ThreadStart(ThreadState *thr, Tid tid, tid_t os_id,
 #endif
 
 #if !SANITIZER_GO
+  // Don't imitate stack/TLS writes for the main thread,
+  // because its initialization is synchronized with all
+  // subsequent threads anyway.
   if (tid != kMainTid) {
-    if (stk_addr && stk_size)
-      MemoryRangeImitateWrite(thr, /*pc=*/1, stk_addr, stk_size);
+    if (stk_addr && stk_size) {
+      const uptr pc = StackTrace::GetNextInstructionPc(
+          reinterpret_cast<uptr>(__tsan_stack_initialization));
+      MemoryRangeImitateWrite(thr, pc, stk_addr, stk_size);
+    }
 
     if (tls_addr && tls_size)
       ImitateTlsWrite(thr, tls_addr, tls_size);