Skip to content

Commit 913f7e9

Browse files
Jakob-Koschelmelver
authored andcommitted
[SanitizerCoverage] Add fallback DebugLocation for instrumented calls
When building the kernel with LTO, KCOV & debug information enabled, multiple inlinable SanitizerCoverage functions require debug information present. In such cases we repurpose the InstrumentationIRBuilder that ensures the necessary debug information is added if necessary. This has been done analogous to the work for the ThreadSanitizer in D124937. Bug: ClangBuiltLinux/linux#1721 Reviewed By: melver Differential Revision: https://reviews.llvm.org/D155377
1 parent 4a8b124 commit 913f7e9

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ void ModuleSanitizerCoverage::InjectCoverageForIndirectCalls(
803803
assert(Options.TracePC || Options.TracePCGuard ||
804804
Options.Inline8bitCounters || Options.InlineBoolFlag);
805805
for (auto *I : IndirCalls) {
806-
IRBuilder<> IRB(I);
806+
InstrumentationIRBuilder IRB(I);
807807
CallBase &CB = cast<CallBase>(*I);
808808
Value *Callee = CB.getCalledOperand();
809809
if (isa<InlineAsm>(Callee))
@@ -820,7 +820,7 @@ void ModuleSanitizerCoverage::InjectTraceForSwitch(
820820
Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
821821
for (auto *I : SwitchTraceTargets) {
822822
if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
823-
IRBuilder<> IRB(I);
823+
InstrumentationIRBuilder IRB(I);
824824
SmallVector<Constant *, 16> Initializers;
825825
Value *Cond = SI->getCondition();
826826
if (Cond->getType()->getScalarSizeInBits() >
@@ -858,7 +858,7 @@ void ModuleSanitizerCoverage::InjectTraceForSwitch(
858858
void ModuleSanitizerCoverage::InjectTraceForDiv(
859859
Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
860860
for (auto *BO : DivTraceTargets) {
861-
IRBuilder<> IRB(BO);
861+
InstrumentationIRBuilder IRB(BO);
862862
Value *A1 = BO->getOperand(1);
863863
if (isa<ConstantInt>(A1)) continue;
864864
if (!A1->getType()->isIntegerTy())
@@ -876,7 +876,7 @@ void ModuleSanitizerCoverage::InjectTraceForDiv(
876876
void ModuleSanitizerCoverage::InjectTraceForGep(
877877
Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
878878
for (auto *GEP : GepTraceTargets) {
879-
IRBuilder<> IRB(GEP);
879+
InstrumentationIRBuilder IRB(GEP);
880880
for (Use &Idx : GEP->indices())
881881
if (!isa<ConstantInt>(Idx) && Idx->getType()->isIntegerTy())
882882
IRB.CreateCall(SanCovTraceGepFunction,
@@ -898,7 +898,7 @@ void ModuleSanitizerCoverage::InjectTraceForLoadsAndStores(
898898
Type *PointerType[5] = {Int8PtrTy, Int16PtrTy, Int32PtrTy, Int64PtrTy,
899899
Int128PtrTy};
900900
for (auto *LI : Loads) {
901-
IRBuilder<> IRB(LI);
901+
InstrumentationIRBuilder IRB(LI);
902902
auto Ptr = LI->getPointerOperand();
903903
int Idx = CallbackIdx(LI->getType());
904904
if (Idx < 0)
@@ -907,7 +907,7 @@ void ModuleSanitizerCoverage::InjectTraceForLoadsAndStores(
907907
IRB.CreatePointerCast(Ptr, PointerType[Idx]));
908908
}
909909
for (auto *SI : Stores) {
910-
IRBuilder<> IRB(SI);
910+
InstrumentationIRBuilder IRB(SI);
911911
auto Ptr = SI->getPointerOperand();
912912
int Idx = CallbackIdx(SI->getValueOperand()->getType());
913913
if (Idx < 0)
@@ -921,7 +921,7 @@ void ModuleSanitizerCoverage::InjectTraceForCmp(
921921
Function &, ArrayRef<Instruction *> CmpTraceTargets) {
922922
for (auto *I : CmpTraceTargets) {
923923
if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
924-
IRBuilder<> IRB(ICMP);
924+
InstrumentationIRBuilder IRB(ICMP);
925925
Value *A0 = ICMP->getOperand(0);
926926
Value *A1 = ICMP->getOperand(1);
927927
if (!A0->getType()->isIntegerTy())
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=2 -S | FileCheck %s
2+
3+
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"
4+
5+
define i32 @with_dbg(ptr %a, ptr %b) !dbg !3 {
6+
entry:
7+
%tmp1 = load i32, ptr %a, align 4
8+
%cmp = icmp eq i32 %tmp1, 42
9+
br i1 %cmp, label %0, label %1
10+
0:
11+
store i32 %tmp1, ptr %b
12+
br label %1
13+
1:
14+
ret i32 %tmp1
15+
}
16+
; CHECK-LABEL: @with_dbg
17+
; CHECK-NEXT: entry:
18+
; CHECK: call void @__sanitizer_cov_trace_pc_guard(ptr @__sancov_gen_) #1, !dbg [[DBG1:![0-9]+]]
19+
; CHECK: call void @__sanitizer_cov_trace_pc_guard(ptr inttoptr (i64 add (i64 ptrtoint (ptr @__sancov_gen_ to i64), i64 4) to ptr)) #1, !dbg [[DBG2:![0-9]+]]
20+
21+
define i32 @without_dbg(ptr %a, ptr %b) {
22+
entry:
23+
%tmp1 = load i32, ptr %a, align 4
24+
%cmp = icmp eq i32 %tmp1, 42
25+
br i1 %cmp, label %0, label %1
26+
0:
27+
store i32 %tmp1, ptr %b
28+
br label %1
29+
1:
30+
ret i32 %tmp1
31+
}
32+
; CHECK-LABEL: @without_dbg
33+
; CHECK-NEXT: entry:
34+
; CHECK: call void @__sanitizer_cov_trace_pc_guard(ptr @__sancov_gen_.1) #1
35+
; CHECK: call void @__sanitizer_cov_trace_pc_guard(ptr inttoptr (i64 add (i64 ptrtoint (ptr @__sancov_gen_.1 to i64), i64 4) to ptr)) #1
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.module.flags = !{!2}
39+
40+
!0 = distinct !DICompileUnit(language: DW_LANG_C89, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
41+
!1 = !DIFile(filename: "foo.c", directory: "")
42+
!2 = !{i32 2, !"Debug Info Version", i32 3}
43+
!3 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 190, type: !4, scopeLine: 192, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
44+
!4 = !DISubroutineType(types: !5)
45+
!5 = !{}
46+
!6 = !DILocation(line: 192, scope: !3)
47+
!7 = !DILocation(line: 0, scope: !3)
48+
49+
; CHECK: [[DBG1]] = !DILocation(line: 192, scope: !3)
50+
; CHECK: [[DBG2]] = !DILocation(line: 0, scope: !3)

0 commit comments

Comments
 (0)