[llvm-branch-commits] [NFC][TSan] Move ObtainCurrentStack out of ThreadRegistryLock scope (PR #228794)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Oct 3 17:48:23 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Vitaly Buka (vitalybuka)

<details>
<summary>Changes</summary>

ObtainCurrentStack only reads the current thread's shadow stack and
allocates a VarSizeStackTrace buffer, which does not require
ThreadRegistryLock (and already runs outside ThreadRegistryLock in
ReportRace, SignalUnsafeCall, and ReportErrnoSpoiling).

Move ObtainCurrentStack (and dummy_pc in ReportDeadlock) before
ScopedReport in ReportMutexHeldWrongContext, ReportMutexMisuse,
ReportDeadlock, and ReportDestroyLocked so the stack trace buffers also
outlive ScopedReport and OutputReport.

Assisted-by: Gemini


---
Full diff: https://github.com/llvm/llvm-project/pull/228794.diff


2 Files Affected:

- (modified) compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp (+2-2) 
- (modified) compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp (+6-5) 


``````````diff
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp b/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp
index e280f5b91b61d..11311ce8e901f 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp
@@ -441,6 +441,8 @@ void __tsan_mutex_post_divert(void *addr, unsigned flagz) {
 }
 
 static void ReportMutexHeldWrongContext(ThreadState *thr, uptr pc) {
+  VarSizeStackTrace trace;
+  ObtainCurrentStack(thr, pc, &trace);
   ScopedReport rep(ReportTypeMutexHeldWrongContext);
   // Release locks before symbolizing and outputting the report to avoid
   // deadlocks.
@@ -450,8 +452,6 @@ static void ReportMutexHeldWrongContext(ThreadState *thr, uptr pc) {
       MutexSet::Desc desc = thr->mset.Get(i);
       rep.AddMutex(desc.addr, desc.stack_id);
     }
-    VarSizeStackTrace trace;
-    ObtainCurrentStack(thr, pc, &trace);
     rep.AddStack(trace, true);
   }
   OutputReport(thr, rep);
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
index 013c1d471ff2e..8ae9c1f4b20e9 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp
@@ -55,14 +55,14 @@ static void ReportMutexMisuse(ThreadState *thr, uptr pc, ReportType typ,
     return;
   if (!ShouldReport(thr, typ))
     return;
+  VarSizeStackTrace trace;
+  ObtainCurrentStack(thr, pc, &trace);
   ScopedReport rep(typ);
   // Release locks before symbolizing and outputting the report to avoid
   // deadlocks.
   {
     ThreadRegistryLock l(&ctx->thread_registry);
     rep.AddMutex(addr, creation_stack_id);
-    VarSizeStackTrace trace;
-    ObtainCurrentStack(thr, pc, &trace);
     rep.AddStack(trace, true);
     rep.AddLocation(addr, 1);
   }
@@ -532,6 +532,7 @@ void AfterSleep(ThreadState *thr, uptr pc) {
 void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r) {
   if (r == 0 || !ShouldReport(thr, ReportTypeDeadlock))
     return;
+  uptr dummy_pc = 0x42;
   ScopedReport rep(ReportTypeDeadlock);
   // Release locks before symbolizing and outputting the report to avoid
   // deadlocks.
@@ -542,7 +543,6 @@ void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r) {
       rep.AddUniqueTid((int)r->loop[i].thr_ctx);
       rep.AddThread((int)r->loop[i].thr_ctx);
     }
-    uptr dummy_pc = 0x42;
     for (int i = 0; i < r->n; i++) {
       for (int j = 0; j < (flags()->second_deadlock_stack ? 2 : 1); j++) {
         u32 stk = r->loop[i].stk[j];
@@ -579,14 +579,15 @@ void ReportDestroyLocked(ThreadState *thr, uptr pc, uptr addr,
       return;
   }
 
+  VarSizeStackTrace trace;
+  ObtainCurrentStack(thr, pc, &trace);
+
   ScopedReport rep(ReportTypeMutexDestroyLocked);
   // Release locks before symbolizing and outputting the report to avoid
   // deadlocks.
   {
     ThreadRegistryLock l0(&ctx->thread_registry);
     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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/228794


More information about the llvm-branch-commits mailing list