[compiler-rt] 1372974 - [NFC][TSan] Move RestoreStack before ScopedReport in ReportDestroyLocked (#228643)

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 19:27:31 PDT 2026


Author: Vitaly Buka
Date: 2026-10-04T02:27:23Z
New Revision: 13729747c5d03646f09a9f7bdecfe491254bead9

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

LOG: [NFC][TSan] Move RestoreStack before ScopedReport in ReportDestroyLocked (#228643)

Run RestoreStack in its own lock scope before constructing ScopedReport
in ReportDestroyLocked (matching ReportRace). This avoids acquiring
ScopedErrorReportLock or symbolizing the current stack if RestoreStack
fails, and avoids holding slot_lock and slot_mtx while populating the
ScopedReport.

Assisted-by: Gemini

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
index da3604a2b840d..271412f4c4088 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
@@ -574,31 +574,34 @@ void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r) {
 
 void ReportDestroyLocked(ThreadState *thr, uptr pc, uptr addr,
                          FastState last_lock, StackID creation_stack_id) {
-  // Use alloca, because malloc during signal handling deadlocks
-  ScopedReport *rep = (ScopedReport *)__builtin_alloca(sizeof(ScopedReport));
-  // Release locks before symbolizing and outputting the report to avoid
-  // deadlocks.
+  VarSizeStackTrace last_lock_stack;
   {
     // We need to lock the slot during RestoreStack because it protects
     // the slot journal.
     Lock slot_lock(&ctx->slots[static_cast<uptr>(last_lock.sid())].mtx);
     ThreadRegistryLock l0(&ctx->thread_registry);
     Lock slots_lock(&ctx->slot_mtx);
-    new (rep) ScopedReport(ReportTypeMutexDestroyLocked);
-    rep->AddMutex(addr, creation_stack_id);
-    VarSizeStackTrace trace;
-    ObtainCurrentStack(thr, pc, &trace);
-    rep->AddStack(trace, true);
-
     Tid tid;
     DynamicMutexSet mset;
     uptr tag;
     if (!RestoreStack(EventType::kLock, last_lock.sid(), last_lock.epoch(),
-                      addr, 0, kAccessWrite, &tid, &trace, mset, &tag)) {
-      rep->~ScopedReport();
+                      addr, 0, kAccessWrite, &tid, &last_lock_stack, mset,
+                      &tag))
       return;
-    }
+  }
+
+  // Use alloca, because malloc during signal handling deadlocks
+  ScopedReport* rep = (ScopedReport*)__builtin_alloca(sizeof(ScopedReport));
+  // Release locks before symbolizing and outputting the report to avoid
+  // deadlocks.
+  {
+    ThreadRegistryLock l0(&ctx->thread_registry);
+    new (rep) ScopedReport(ReportTypeMutexDestroyLocked);
+    rep->AddMutex(addr, creation_stack_id);
+    VarSizeStackTrace trace;
+    ObtainCurrentStack(thr, pc, &trace);
     rep->AddStack(trace, true);
+    rep->AddStack(last_lock_stack, true);
     rep->AddLocation(addr, 1);
   }
   OutputReport(thr, *rep);


        


More information about the llvm-commits mailing list