Skip to content

Commit 9faef12

Browse files
keesc-rhodes
authored andcommitted
[sancov] Fix stack-depth tracking to use debug locations (#162428)
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 cc @nathanchance @melver @JustinStitt @bwendling (cherry picked from commit 28b7f66)
1 parent 45afac6 commit 9faef12

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,10 @@ 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);
1089+
if (EntryLoc)
1090+
Store->setDebugLoc(EntryLoc);
10891091
Load->setNoSanitizeMetadata();
10901092
Store->setNoSanitizeMetadata();
10911093
}
@@ -1131,7 +1133,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
11311133
EstimatedStackSize >= Options.StackDepthCallbackMin) {
11321134
if (InsertBefore)
11331135
IRB.SetInsertPoint(InsertBefore);
1134-
IRB.CreateCall(SanCovStackDepthCallback)->setCannotMerge();
1136+
auto Call = IRB.CreateCall(SanCovStackDepthCallback);
1137+
if (EntryLoc)
1138+
Call->setDebugLoc(EntryLoc);
1139+
Call->setCannotMerge();
11351140
}
11361141
} else {
11371142
// Check stack depth. If it's the deepest so far, record it.
@@ -1144,8 +1149,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
11441149
auto ThenTerm = SplitBlockAndInsertIfThen(
11451150
IsStackLower, &*IP, false,
11461151
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
1147-
IRBuilder<> ThenIRB(ThenTerm);
1152+
InstrumentationIRBuilder ThenIRB(ThenTerm);
11481153
auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
1154+
if (EntryLoc)
1155+
Store->setDebugLoc(EntryLoc);
11491156
LowestStack->setNoSanitizeMetadata();
11501157
Store->setNoSanitizeMetadata();
11511158
}

llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=2 -S | FileCheck %s
3+
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=1 -sanitizer-coverage-stack-depth -sanitizer-coverage-stack-depth-callback-min=1 -S | FileCheck %s --check-prefix=CHECK-STACK-CALLBACK
4+
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=1 -sanitizer-coverage-stack-depth -S | FileCheck %s --check-prefix=CHECK-STACK-DEPTH
35

46
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"
57

@@ -55,6 +57,86 @@ entry:
5557
ret i32 %t
5658
}
5759

60+
define i32 @with_dbg_stack_callback(ptr %a) !dbg !8 {
61+
; CHECK-STACK-CALLBACK-LABEL: define i32 @with_dbg_stack_callback(
62+
; CHECK-STACK-CALLBACK-SAME: ptr [[A:%.*]]) !dbg [[DBG8:![0-9]+]] {
63+
; CHECK-STACK-CALLBACK-NEXT: entry:
64+
; CHECK-STACK-CALLBACK-NEXT: [[BUF:%.*]] = alloca [64 x i8], align 1
65+
; CHECK-STACK-CALLBACK-NEXT: call void @__sanitizer_cov_stack_depth() #[[ATTR1:[0-9]+]], !dbg [[DBG9:![0-9]+]]
66+
; CHECK-STACK-CALLBACK-NEXT: %t = load i32, ptr [[A]], align 4
67+
; CHECK-STACK-CALLBACK-NEXT: call void @external_func()
68+
; CHECK-STACK-CALLBACK-NEXT: ret i32 %t
69+
;
70+
entry:
71+
%buf = alloca [64 x i8], align 1
72+
%t = load i32, ptr %a, align 4
73+
call void @external_func()
74+
ret i32 %t
75+
}
76+
77+
define i32 @with_dbg_stack_depth(ptr %a) !dbg !10 {
78+
; CHECK-STACK-DEPTH-LABEL: define i32 @with_dbg_stack_depth(
79+
; CHECK-STACK-DEPTH-SAME: ptr [[A:%.*]]) !dbg [[DBG10:![0-9]+]] {
80+
; CHECK-STACK-DEPTH-NEXT: entry:
81+
; CHECK-STACK-DEPTH-NEXT: [[BUF:%.*]] = alloca [64 x i8], align 1
82+
; CHECK-STACK-DEPTH-NEXT: [[TMP1:%.*]] = call ptr @llvm.frameaddress.p0(i32 0)
83+
; CHECK-STACK-DEPTH-NEXT: [[TMP2:%.*]] = ptrtoint ptr [[TMP1]] to i64
84+
; CHECK-STACK-DEPTH-NEXT: [[TMP3:%.*]] = load i64, ptr @__sancov_lowest_stack, align 8
85+
; CHECK-STACK-DEPTH-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP2]], [[TMP3]]
86+
; CHECK-STACK-DEPTH-NEXT: br i1 [[TMP4]], label {{%.*}}, label {{%.*}}
87+
; CHECK-STACK-DEPTH: store i64 [[TMP2]], ptr @__sancov_lowest_stack, align 8, !dbg [[DBG11:![0-9]+]], {{.*}}!nosanitize
88+
; CHECK-STACK-DEPTH: %t = load i32, ptr [[A]], align 4
89+
; CHECK-STACK-DEPTH-NEXT: call void @external_func()
90+
; CHECK-STACK-DEPTH-NEXT: ret i32 %t
91+
;
92+
entry:
93+
%buf = alloca [64 x i8], align 1
94+
%t = load i32, ptr %a, align 4
95+
call void @external_func()
96+
ret i32 %t
97+
}
98+
99+
define i32 @without_dbg_stack_callback(ptr %a) {
100+
; CHECK-STACK-CALLBACK-LABEL: define i32 @without_dbg_stack_callback(
101+
; CHECK-STACK-CALLBACK-SAME: ptr [[A:%.*]]) {
102+
; CHECK-STACK-CALLBACK-NEXT: entry:
103+
; CHECK-STACK-CALLBACK-NEXT: [[BUF:%.*]] = alloca [64 x i8], align 1
104+
; CHECK-STACK-CALLBACK-NEXT: call void @__sanitizer_cov_stack_depth() #[[ATTR1]]
105+
; CHECK-STACK-CALLBACK-NEXT: %t = load i32, ptr [[A]], align 4
106+
; CHECK-STACK-CALLBACK-NEXT: call void @external_func()
107+
; CHECK-STACK-CALLBACK-NEXT: ret i32 %t
108+
;
109+
entry:
110+
%buf = alloca [64 x i8], align 1
111+
%t = load i32, ptr %a, align 4
112+
call void @external_func()
113+
ret i32 %t
114+
}
115+
116+
define i32 @without_dbg_stack_depth(ptr %a) {
117+
; CHECK-STACK-DEPTH-LABEL: define i32 @without_dbg_stack_depth(
118+
; CHECK-STACK-DEPTH-SAME: ptr [[A:%.*]]) {
119+
; CHECK-STACK-DEPTH-NEXT: entry:
120+
; CHECK-STACK-DEPTH-NEXT: [[BUF:%.*]] = alloca [64 x i8], align 1
121+
; CHECK-STACK-DEPTH-NEXT: [[TMP1:%.*]] = call ptr @llvm.frameaddress.p0(i32 0)
122+
; CHECK-STACK-DEPTH-NEXT: [[TMP2:%.*]] = ptrtoint ptr [[TMP1]] to i64
123+
; CHECK-STACK-DEPTH-NEXT: [[TMP3:%.*]] = load i64, ptr @__sancov_lowest_stack, align 8
124+
; CHECK-STACK-DEPTH-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP2]], [[TMP3]]
125+
; CHECK-STACK-DEPTH-NEXT: br i1 [[TMP4]], label {{%.*}}, label {{%.*}}
126+
; CHECK-STACK-DEPTH: store i64 [[TMP2]], ptr @__sancov_lowest_stack, align 8, {{.*}}!nosanitize
127+
; CHECK-STACK-DEPTH: %t = load i32, ptr [[A]], align 4
128+
; CHECK-STACK-DEPTH-NEXT: call void @external_func()
129+
; CHECK-STACK-DEPTH-NEXT: ret i32 %t
130+
;
131+
entry:
132+
%buf = alloca [64 x i8], align 1
133+
%t = load i32, ptr %a, align 4
134+
call void @external_func()
135+
ret i32 %t
136+
}
137+
138+
declare void @external_func()
139+
58140
!llvm.dbg.cu = !{!0}
59141
!llvm.module.flags = !{!2}
60142

@@ -66,6 +148,10 @@ entry:
66148
!5 = !{}
67149
!6 = !DILocation(line: 192, scope: !3)
68150
!7 = !DILocation(line: 0, scope: !3)
151+
!8 = distinct !DISubprogram(name: "with_dbg_stack_callback", scope: !1, file: !1, line: 200, type: !4, scopeLine: 200, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
152+
!9 = !DILocation(line: 200, scope: !8)
153+
!10 = distinct !DISubprogram(name: "with_dbg_stack_depth", scope: !1, file: !1, line: 210, type: !4, scopeLine: 210, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
154+
!11 = !DILocation(line: 210, scope: !10)
69155

70156
;.
71157
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C89, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
@@ -76,3 +162,9 @@ entry:
76162
; CHECK: [[DBG6]] = !DILocation(line: 192, scope: [[DBG3]])
77163
; CHECK: [[DBG7]] = !DILocation(line: 0, scope: [[DBG3]])
78164
;.
165+
; CHECK-STACK-CALLBACK: [[DBG8]] = distinct !DISubprogram(name: "with_dbg_stack_callback", scope: {{.*}}, file: {{.*}}, line: 200
166+
; CHECK-STACK-CALLBACK: [[DBG9]] = !DILocation(line: 200, scope: [[DBG8]])
167+
;.
168+
; CHECK-STACK-DEPTH: [[DBG10]] = distinct !DISubprogram(name: "with_dbg_stack_depth", scope: {{.*}}, file: {{.*}}, line: 210
169+
; CHECK-STACK-DEPTH: [[DBG11]] = !DILocation(line: 210, scope: [[DBG10]])
170+
;.

0 commit comments

Comments
 (0)