Skip to content

[TSan, SanitizerBinaryMetadata] Improve instrument for derived pointers via phis/selects #132752

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 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ bool maybeSharedMutable(const Value *Addr) {
if (!Addr)
return true;

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

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

if (isa<AllocaInst>(getUnderlyingObject(Addr)) &&
!PointerMayBeCaptured(Addr, /*ReturnCaptures=*/true)) {
const AllocaInst *AI = findAllocaForValue(Addr);
if (AI && !PointerMayBeCaptured(Addr, /*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
31 changes: 31 additions & 0 deletions llvm/test/Instrumentation/ThreadSanitizer/capture.ll
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,35 @@ entry:
; 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-NOT: call void @__tsan_write4(ptr %ptr)
; 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-NOT: call void @__tsan_write4(ptr %derived)