[llvm] [TSan] Add option to ignore capturing behavior when instrumenting (PR #148156)

Yussur Mustafa Oraji via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 24 00:57:28 PDT 2025


https://github.com/N00byKing updated https://github.com/llvm/llvm-project/pull/148156

>From 8962031fc782251ddd1276935a4139a35f36e51c Mon Sep 17 00:00:00 2001
From: Yussur Mustafa Oraji <yussur.oraji at tu-darmstadt.de>
Date: Fri, 11 Jul 2025 10:43:27 +0200
Subject: [PATCH 1/2] [TSan] Add option to ignore capturing behavior when
 instrumenting

Required for some tools depending on the annotation interface
---
 llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
index 5485998164f1e..0d48a350254ee 100644
--- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
@@ -80,6 +80,10 @@ static cl::opt<bool> ClCompoundReadBeforeWrite(
     "tsan-compound-read-before-write", cl::init(false),
     cl::desc("Emit special compound instrumentation for reads-before-writes"),
     cl::Hidden);
+static cl::opt<bool>
+    ClOmitNonCaptured("tsan-omit-by-pointer-capturing", cl::init(true),
+                      cl::desc("Omit accesses due to pointer capturing"),
+                      cl::Hidden);
 
 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
@@ -450,7 +454,8 @@ void ThreadSanitizer::chooseInstructionsToInstrument(
 
     const AllocaInst *AI = findAllocaForValue(Addr);
     // Instead of Addr, we should check whether its base pointer is captured.
-    if (AI && !PointerMayBeCaptured(AI, /*ReturnCaptures=*/true)) {
+    if (AI && !PointerMayBeCaptured(AI, /*ReturnCaptures=*/true) &&
+        ClOmitNonCaptured) {
       // The variable is addressable but not captured, so it cannot be
       // referenced from a different thread and participate in a data race
       // (see llvm/Analysis/CaptureTracking.h for details).

>From b7c93f4b4ab8ca3d889895f8bb699f66e6d3e90e Mon Sep 17 00:00:00 2001
From: Yussur Mustafa Oraji <yussur.oraji at tu-darmstadt.de>
Date: Thu, 24 Jul 2025 09:57:26 +0200
Subject: [PATCH 2/2] [TSan] Add tests for disabled capture omits

---
 .../ThreadSanitizer/capture-no-omit.ll        | 92 +++++++++++++++++++
 .../ThreadSanitizer/capture.ll                |  2 +
 2 files changed, 94 insertions(+)
 create mode 100644 llvm/test/Instrumentation/ThreadSanitizer/capture-no-omit.ll

diff --git a/llvm/test/Instrumentation/ThreadSanitizer/capture-no-omit.ll b/llvm/test/Instrumentation/ThreadSanitizer/capture-no-omit.ll
new file mode 100644
index 0000000000000..cae04936002cd
--- /dev/null
+++ b/llvm/test/Instrumentation/ThreadSanitizer/capture-no-omit.ll
@@ -0,0 +1,92 @@
+; RUN: opt < %s -passes=tsan -tsan-omit-by-pointer-capturing=0 -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
+declare void @escape(ptr)
+
+ at sink = global ptr null, align 4
+
+
+define void @captured2() nounwind uwtable sanitize_thread {
+entry:
+  %ptr = alloca i32, align 4
+  %tmp = alloca ptr, align 8
+  ; transitive escape
+  store ptr %ptr, ptr %tmp, align 8
+  %0 = load ptr, ptr %tmp, align 8
+  store ptr %0, ptr @sink, align 8
+  store i32 42, ptr %ptr, align 4
+  ret void
+}
+; CHECK-LABEL: define void @captured2
+; CHECK: __tsan_write
+; CHECK: __tsan_read
+; CHECK: __tsan_write
+; CHECK: __tsan_write
+; CHECK: ret void
+
+define void @captured3() nounwind uwtable sanitize_thread {
+entry:
+  %stkobj = alloca [2 x i32], align 8
+  ; escapes due to store into global
+  store ptr %stkobj, ptr @sink, align 8
+  ; derived is captured as its base object is captured
+  %derived = getelementptr inbounds i32, ptr %stkobj, i64 1
+  store i32 42, ptr %derived, align 4
+  ret void
+}
+; CHECK-LABEL: define void @captured3
+; CHECK: __tsan_write
+; CHECK: __tsan_write
+; CHECK: ret void
+
+define void @notcaptured2() nounwind uwtable sanitize_thread {
+entry:
+  %ptr = alloca i32, align 4
+  %tmp = alloca ptr, align 8
+  store i32 42, ptr %ptr, align 4
+  ; transitive escape
+  store ptr %ptr, ptr %tmp, align 8
+  %0 = load ptr, ptr %tmp, align 8
+  store ptr %0, ptr @sink, align 8
+  ret void
+}
+; CHECK-LABEL: define void @notcaptured2
+; CHECK: __tsan_write
+; CHECK: __tsan_write
+; CHECK: __tsan_read
+; CHECK: __tsan_write
+; CHECK: ret void
+
+define void @notcaptured3(i1 %cond) nounwind uwtable sanitize_thread {
+entry:
+  %stkobj = alloca [2 x i32], align 8
+  %derived = getelementptr inbounds i32, ptr %stkobj, i64 1
+  %ptr = select i1 %cond, ptr %derived, ptr %stkobj
+  store i32 42, ptr %ptr, align 4
+  ret void
+}
+; CHECK-LABEL: define void @notcaptured3
+; CHECK: __tsan_write
+; CHECK: ret void
+
+define void @notcaptured4() nounwind uwtable sanitize_thread {
+entry:
+  %stkobj = alloca [10 x i8], align 1
+  br label %loop
+
+exit:
+  ret void
+
+loop:
+  %count = phi i32 [ 0, %entry ], [ %addone, %loop ]
+  %derived = phi ptr [ %stkobj, %entry ], [ %ptraddone, %loop ]
+  store i32 %count, ptr %derived, align 4
+  %ptraddone = getelementptr inbounds i32, ptr %derived, i64 1
+  %addone = add nuw nsw i32 %count, 1
+  %eq10 = icmp eq i32 %addone, 10
+  br i1 %eq10, label %exit, label %loop
+}
+; CHECK-LABEL: define void @notcaptured4
+; CHECK: ret void
+; CHECK: __tsan_write
diff --git a/llvm/test/Instrumentation/ThreadSanitizer/capture.ll b/llvm/test/Instrumentation/ThreadSanitizer/capture.ll
index e1b9e03b88446..5083c790011c6 100644
--- a/llvm/test/Instrumentation/ThreadSanitizer/capture.ll
+++ b/llvm/test/Instrumentation/ThreadSanitizer/capture.ll
@@ -45,6 +45,7 @@ entry:
 ; CHECK-LABEL: define void @captured2
 ; CHECK: __tsan_write
 ; CHECK: __tsan_write
+; CHECK-NOT: __tsan_write
 ; CHECK: ret void
 
 define void @captured3() nounwind uwtable sanitize_thread {
@@ -101,6 +102,7 @@ entry:
 ; CHECK-LABEL: define void @notcaptured2
 ; CHECK: __tsan_write
 ; CHECK: __tsan_write
+; CHECK-NOT: __tsan_write
 ; CHECK: ret void
 
 define void @notcaptured3(i1 %cond) nounwind uwtable sanitize_thread {



More information about the llvm-commits mailing list