Skip to content

Commit b52f9fe

Browse files
committed
[sancov] Fix stack-depth tracking to use debug locations
As fixed in commits 913f7e9, 4a8b124, and 4eef2e3, also fix the stack-depth tracking code to use InstrumentationIRBuilder, and set the Call's Debug location to EntryLoc. ClangBuiltLinux/linux#2125
1 parent f015c7f commit b52f9fe

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Test that SanitizerCoverage preserves debug locations when inserting stack depth tracking
2+
// This is a regression test for GitHub issue ClangBuiltLinux/linux#2125
3+
//
4+
// The bug was that IRBuilder<> was used instead of InstrumentationIRBuilder in SanitizerCoverage,
5+
// causing inserted instructions to lack !dbg metadata. This caused LTO builds with debug info
6+
// to fail verification with:
7+
// "inlinable function call in a function with debug info must have a !dbg location"
8+
//
9+
// Test the lowest-stack tracking path (default stack-depth mode)
10+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s \
11+
// RUN: -fsanitize-coverage-type=1 -fsanitize-coverage-stack-depth -debug-info-kind=limited \
12+
// RUN: | FileCheck %s --check-prefix=CHECK-STORE
13+
//
14+
// Test the callback path (stack-depth with callback-min threshold)
15+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s \
16+
// RUN: -fsanitize-coverage-type=1 -mllvm -sanitizer-coverage-stack-depth \
17+
// RUN: -mllvm -sanitizer-coverage-stack-depth-callback-min=1 -debug-info-kind=limited \
18+
// RUN: | FileCheck %s --check-prefix=CHECK-CALLBACK
19+
//
20+
// Verify the store to __sancov_lowest_stack has a debug location
21+
// CHECK-STORE: store i64 %{{.*}}, ptr @__sancov_lowest_stack, align 8, !dbg !{{[0-9]+}}, {{.*}}!nosanitize
22+
//
23+
// Verify the call to __sanitizer_cov_stack_depth has a debug location
24+
// CHECK-CALLBACK: call void @__sanitizer_cov_stack_depth(){{.*}}, !dbg !{{[0-9]+}}
25+
26+
extern void external_func(void);
27+
28+
// Mark as always_inline to ensure the bug condition is met
29+
__attribute__((always_inline))
30+
static inline void inline_helper(void) {
31+
external_func();
32+
}
33+
34+
void foo(int a) {
35+
int local[4]; // Stack allocation to trigger stack depth tracking
36+
if (a > 0) {
37+
inline_helper();
38+
}
39+
local[0] = a;
40+
}

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
10841084
auto ThenTerm = SplitBlockAndInsertIfThen(
10851085
IRB.CreateIsNull(Load), &*IP, false,
10861086
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
1087-
IRBuilder<> ThenIRB(ThenTerm);
1087+
InstrumentationIRBuilder ThenIRB(ThenTerm);
10881088
auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr);
10891089
Load->setNoSanitizeMetadata();
10901090
Store->setNoSanitizeMetadata();
@@ -1131,7 +1131,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
11311131
EstimatedStackSize >= Options.StackDepthCallbackMin) {
11321132
if (InsertBefore)
11331133
IRB.SetInsertPoint(InsertBefore);
1134-
IRB.CreateCall(SanCovStackDepthCallback)->setCannotMerge();
1134+
auto Call = IRB.CreateCall(SanCovStackDepthCallback);
1135+
if (EntryLoc)
1136+
Call->setDebugLoc(EntryLoc);
1137+
Call->setCannotMerge();
11351138
}
11361139
} else {
11371140
// Check stack depth. If it's the deepest so far, record it.
@@ -1144,7 +1147,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
11441147
auto ThenTerm = SplitBlockAndInsertIfThen(
11451148
IsStackLower, &*IP, false,
11461149
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
1147-
IRBuilder<> ThenIRB(ThenTerm);
1150+
InstrumentationIRBuilder ThenIRB(ThenTerm);
11481151
auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
11491152
LowestStack->setNoSanitizeMetadata();
11501153
Store->setNoSanitizeMetadata();

0 commit comments

Comments
 (0)