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

Vitaly Buka via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Oct 3 18:02:33 PDT 2026


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

>From b8cc242bf96eb036f42c58c3d3c256897cb9be72 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sat, 3 Oct 2026 15:11:02 -0700
Subject: [PATCH] [NFC][TSan] Move ObtainCurrentStack out of ThreadRegistryLock
 scope

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, ReportErrnoSpoiling, and
ReportDestroyLocked).

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

Assisted-by: Gemini

Pull Request: https://github.com/llvm/llvm-project/pull/228794
---
 compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp | 4 ++--
 compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp     | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

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 47dd013b7bf86..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];



More information about the llvm-branch-commits mailing list