Skip to content

Commit a226858

Browse files
thejhramosian-glider
authored andcommitted
[AddressSanitizer] Instrument byval call arguments
Summary: In the LLVM IR, "call" instructions read memory for each byval operand. For example: ``` $ cat blah.c struct foo { void *a, *b, *c; }; struct bar { struct foo foo; }; void func1(const struct foo); void func2(struct bar *bar) { func1(bar->foo); } $ [...]/bin/clang -S -flto -c blah.c -O2 ; cat blah.s [...] define dso_local void @func2(%struct.bar* %bar) local_unnamed_addr #0 { entry: %foo = getelementptr inbounds %struct.bar, %struct.bar* %bar, i64 0, i32 0 tail call void @func1(%struct.foo* byval(%struct.foo) align 8 %foo) #2 ret void } [...] $ [...]/bin/clang -S -c blah.c -O2 ; cat blah.s [...] func2: # @func2 [...] subq $24, %rsp [...] movq 16(%rdi), %rax movq %rax, 16(%rsp) movups (%rdi), %xmm0 movups %xmm0, (%rsp) callq func1 addq $24, %rsp [...] retq ``` Let ASAN instrument these hidden memory accesses. This is patch 4/4 of a patch series: https://reviews.llvm.org/D77616 [PATCH 1/4] [AddressSanitizer] Refactor ClDebug{Min,Max} handling https://reviews.llvm.org/D77617 [PATCH 2/4] [AddressSanitizer] Split out memory intrinsic handling https://reviews.llvm.org/D77618 [PATCH 3/4] [AddressSanitizer] Refactor: Permit >1 interesting operands per instruction https://reviews.llvm.org/D77619 [PATCH 4/4] [AddressSanitizer] Instrument byval call arguments Reviewers: kcc, glider Reviewed By: glider Subscribers: hiraditya, dexonsmith, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77619
1 parent cfe36e4 commit a226858

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ static cl::opt<bool> ClInstrumentAtomics(
213213
cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
214214
cl::init(true));
215215

216+
static cl::opt<bool>
217+
ClInstrumentByval("asan-instrument-byval",
218+
cl::desc("instrument byval call arguments"), cl::Hidden,
219+
cl::init(true));
220+
216221
static cl::opt<bool> ClAlwaysSlowPath(
217222
"asan-always-slow-path",
218223
cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden,
@@ -1414,6 +1419,14 @@ void AddressSanitizer::getInterestingMemoryOperands(
14141419
Alignment = (unsigned)AlignmentConstant->getZExtValue();
14151420
Value *Mask = CI->getOperand(2 + OpOffset);
14161421
Interesting.emplace_back(I, OpOffset, IsWrite, Ty, Alignment, Mask);
1422+
} else {
1423+
for (unsigned ArgNo = 0; ArgNo < CI->getNumArgOperands(); ArgNo++) {
1424+
if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) ||
1425+
ignoreAccess(CI->getArgOperand(ArgNo)))
1426+
continue;
1427+
Type *Ty = CI->getParamByValType(ArgNo);
1428+
Interesting.emplace_back(I, ArgNo, false, Ty, 1);
1429+
}
14171430
}
14181431
}
14191432
}

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static cl::opt<bool> ClInstrumentAtomics(
9797
cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
9898
cl::init(true));
9999

100+
static cl::opt<bool> ClInstrumentByval("hwasan-instrument-byval",
101+
cl::desc("instrument byval arguments"),
102+
cl::Hidden, cl::init(true));
103+
100104
static cl::opt<bool> ClRecover(
101105
"hwasan-recover",
102106
cl::desc("Enable recovery mode (continue-after-error)."),
@@ -549,6 +553,14 @@ void HWAddressSanitizer::getInterestingMemoryOperands(
549553
return;
550554
Interesting.emplace_back(I, XCHG->getPointerOperandIndex(), true,
551555
XCHG->getCompareOperand()->getType(), 0);
556+
} else if (auto CI = dyn_cast<CallInst>(I)) {
557+
for (unsigned ArgNo = 0; ArgNo < CI->getNumArgOperands(); ArgNo++) {
558+
if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) ||
559+
ignoreAccess(CI->getArgOperand(ArgNo)))
560+
continue;
561+
Type *Ty = CI->getParamByValType(ArgNo);
562+
Interesting.emplace_back(I, ArgNo, false, Ty, 1);
563+
}
552564
}
553565
}
554566

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt < %s -asan -S | FileCheck %s
2+
; Test that for call instructions, the by-value arguments are instrumented.
3+
4+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
%struct.bar = type { %struct.foo }
8+
%struct.foo = type { i8*, i8*, i8* }
9+
define dso_local void @func2(%struct.foo* %foo) sanitize_address {
10+
; CHECK-LABEL: @func2
11+
tail call void @func1(%struct.foo* byval(%struct.foo) align 8 %foo) #2
12+
; CHECK: call void @__asan_report_load
13+
ret void
14+
; CHECK: ret void
15+
}
16+
declare dso_local void @func1(%struct.foo* byval(%struct.foo) align 8)
17+
18+
!0 = !{i32 1, !"wchar_size", i32 4}

0 commit comments

Comments
 (0)