Skip to content

[TSan, SanitizerBinaryMetadata] Analyze the capture status for alloca rather than arbitrary Addr #132756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions compiler-rt/test/tsan/stack_race3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
#include "test.h"

void *Thread(void *a) {
barrier_wait(&barrier);
((int *)a)[1] = 43;
return 0;
}

int main() {
barrier_init(&barrier, 2);
int Arr[2] = {41, 42};
pthread_t t;
pthread_create(&t, 0, Thread, &Arr[0]);
Arr[1] = 43;
barrier_wait(&barrier);
pthread_join(t, 0);
}

// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Location is stack of main thread.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ bool maybeSharedMutable(const Value *Addr) {
return true;

const AllocaInst *AI = findAllocaForValue(Addr);
if (AI && !PointerMayBeCaptured(Addr, /*ReturnCaptures=*/true))
if (AI && !PointerMayBeCaptured(AI, /*ReturnCaptures=*/true))
return false; // Object is on stack but does not escape.

Addr = Addr->stripInBoundsOffsets();
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ void ThreadSanitizer::chooseInstructionsToInstrument(
}

const AllocaInst *AI = findAllocaForValue(Addr);
if (AI && !PointerMayBeCaptured(Addr, /*ReturnCaptures=*/true)) {
// Instead of Addr, we should check whether its base pointer is captured.
if (AI && !PointerMayBeCaptured(AI, /*ReturnCaptures=*/true)) {
// 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).
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/Instrumentation/ThreadSanitizer/capture.ll
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ entry:
; 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 @notcaptured0() nounwind uwtable sanitize_thread {
entry:
%ptr = alloca i32, align 4
Expand Down
Loading