Skip to content

Commit 4a8b124

Browse files
Jakob-Koschelmelver
authored andcommitted
[AddressSanitizer] Add fallback DebugLocation for instrumented calls
When building the kernel with LTO, KASAN & debug information enabled, multiple inlinable AddressSanitizer 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/D155376
1 parent fda45d9 commit 4a8b124

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
12301230

12311231
// Instrument memset/memmove/memcpy
12321232
void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
1233-
IRBuilder<> IRB(MI);
1233+
InstrumentationIRBuilder IRB(MI);
12341234
if (isa<MemTransferInst>(MI)) {
12351235
IRB.CreateCall(
12361236
isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
@@ -1642,7 +1642,7 @@ Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore,
16421642
size_t AccessSizeIndex,
16431643
Value *SizeArgument,
16441644
uint32_t Exp) {
1645-
IRBuilder<> IRB(InsertBefore);
1645+
InstrumentationIRBuilder IRB(InsertBefore);
16461646
Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp);
16471647
CallInst *Call = nullptr;
16481648
if (SizeArgument) {
@@ -1719,7 +1719,7 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
17191719
return;
17201720
}
17211721

1722-
IRBuilder<> IRB(InsertBefore);
1722+
InstrumentationIRBuilder IRB(InsertBefore);
17231723
size_t AccessSizeIndex = TypeStoreSizeToSizeIndex(TypeStoreSize);
17241724
const ASanAccessInfo AccessInfo(IsWrite, CompileKernel, AccessSizeIndex);
17251725

@@ -1781,7 +1781,8 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
17811781

17821782
Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite,
17831783
AccessSizeIndex, SizeArgument, Exp);
1784-
Crash->setDebugLoc(OrigIns->getDebugLoc());
1784+
if (OrigIns->getDebugLoc())
1785+
Crash->setDebugLoc(OrigIns->getDebugLoc());
17851786
}
17861787

17871788
// Instrument unusual size or unusual alignment.
@@ -1791,7 +1792,7 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
17911792
void AddressSanitizer::instrumentUnusualSizeOrAlignment(
17921793
Instruction *I, Instruction *InsertBefore, Value *Addr, TypeSize TypeStoreSize,
17931794
bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp) {
1794-
IRBuilder<> IRB(InsertBefore);
1795+
InstrumentationIRBuilder IRB(InsertBefore);
17951796
Value *NumBits = IRB.CreateTypeSize(IntptrTy, TypeStoreSize);
17961797
Value *Size = IRB.CreateLShr(NumBits, ConstantInt::get(IntptrTy, 3));
17971798

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; RUN: opt < %s -passes=asan -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) sanitize_address !dbg !3 {
6+
entry:
7+
%tmp1 = load i32, ptr %a, align 4
8+
store i32 32, ptr %b
9+
ret i32 %tmp1
10+
}
11+
; CHECK-LABEL: @with_dbg
12+
; CHECK-NEXT: entry:
13+
; CHECK: call void @__asan_report_load4(i64 %0) #3, !dbg [[DBG:![0-9]+]]
14+
; CHECK: call void @__asan_report_store4(i64 %13) #3, !dbg [[DBG]]
15+
16+
define i32 @without_dbg(ptr %a, ptr %b) sanitize_address {
17+
entry:
18+
%tmp1 = load i32, ptr %a, align 4
19+
store i32 32, ptr %b
20+
ret i32 %tmp1
21+
}
22+
; CHECK-LABEL: @without_dbg
23+
; CHECK-NEXT: entry:
24+
; CHECK: call void @__asan_report_load4(i64 %0) #3
25+
; CHECK: call void @__asan_report_store4(i64 %13) #3
26+
27+
!llvm.dbg.cu = !{!0}
28+
!llvm.module.flags = !{!2}
29+
30+
!0 = distinct !DICompileUnit(language: DW_LANG_C89, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
31+
!1 = !DIFile(filename: "foo.c", directory: "")
32+
!2 = !{i32 2, !"Debug Info Version", i32 3}
33+
!3 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 190, type: !4, scopeLine: 192, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
34+
!4 = !DISubroutineType(types: !5)
35+
!5 = !{}
36+
37+
; CHECK: [[DBG]] = !DILocation(line: 0, scope: !3)

0 commit comments

Comments
 (0)