Skip to content

Commit 223a95f

Browse files
thejhramosian-glider
authored andcommitted
[AddressSanitizer] Split out memory intrinsic handling
Summary: In both AddressSanitizer and HWAddressSanitizer, we first collect instructions whose operands should be instrumented and memory intrinsics, then instrument them. Both during collection and when inserting instrumentation, they are handled separately. Collect them separately and instrument them separately. This is a bit more straightforward, and prepares for collecting operands instead of instructions in a future patch. This is patch 2/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, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77617
1 parent e29996c commit 223a95f

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,7 @@ bool AddressSanitizer::instrumentFunction(Function &F,
26522652
// are calls between uses).
26532653
SmallPtrSet<Value *, 16> TempsToInstrument;
26542654
SmallVector<Instruction *, 16> ToInstrument;
2655+
SmallVector<MemIntrinsic *, 16> IntrinToInstrument;
26552656
SmallVector<Instruction *, 8> NoReturnCalls;
26562657
SmallVector<BasicBlock *, 16> AllBlocks;
26572658
SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
@@ -2688,8 +2689,11 @@ bool AddressSanitizer::instrumentFunction(Function &F,
26882689
isInterestingPointerSubtraction(&Inst))) {
26892690
PointerComparisonsOrSubtracts.push_back(&Inst);
26902691
continue;
2691-
} else if (isa<MemIntrinsic>(Inst)) {
2692+
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst)) {
26922693
// ok, take it.
2694+
IntrinToInstrument.push_back(MI);
2695+
NumInsnsPerBB++;
2696+
continue;
26932697
} else {
26942698
if (isa<AllocaInst>(Inst)) NumAllocas++;
26952699
if (auto *CB = dyn_cast<CallBase>(&Inst)) {
@@ -2708,9 +2712,9 @@ bool AddressSanitizer::instrumentFunction(Function &F,
27082712
}
27092713
}
27102714

2711-
bool UseCalls =
2712-
(ClInstrumentationWithCallsThreshold >= 0 &&
2713-
ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold);
2715+
bool UseCalls = (ClInstrumentationWithCallsThreshold >= 0 &&
2716+
ToInstrument.size() + IntrinToInstrument.size() >
2717+
(unsigned)ClInstrumentationWithCallsThreshold);
27142718
const DataLayout &DL = F.getParent()->getDataLayout();
27152719
ObjectSizeOpts ObjSizeOpts;
27162720
ObjSizeOpts.RoundToAlign = true;
@@ -2723,9 +2727,11 @@ bool AddressSanitizer::instrumentFunction(Function &F,
27232727
if (isInterestingMemoryAccess(Inst, &IsWrite, &TypeSize, &Alignment))
27242728
instrumentMop(ObjSizeVis, Inst, UseCalls,
27252729
F.getParent()->getDataLayout());
2726-
else
2727-
instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
27282730
}
2731+
}
2732+
for (auto Inst : IntrinToInstrument) {
2733+
if (!suppressInstrumentationSiteForDebug(NumInstrumented))
2734+
instrumentMemIntrinsic(Inst);
27292735
FunctionModified = true;
27302736
}
27312737

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,6 @@ bool HWAddressSanitizer::instrumentMemAccess(Instruction *I) {
720720
uint64_t TypeSize = 0;
721721
Value *MaybeMask = nullptr;
722722

723-
if (ClInstrumentMemIntrinsics && isa<MemIntrinsic>(I)) {
724-
instrumentMemIntrinsic(cast<MemIntrinsic>(I));
725-
return true;
726-
}
727-
728723
Value *Addr =
729724
isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment, &MaybeMask);
730725

@@ -1090,6 +1085,7 @@ bool HWAddressSanitizer::sanitizeFunction(Function &F) {
10901085
LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n");
10911086

10921087
SmallVector<Instruction*, 16> ToInstrument;
1088+
SmallVector<MemIntrinsic *, 16> IntrinToInstrument;
10931089
SmallVector<AllocaInst*, 8> AllocasToInstrument;
10941090
SmallVector<Instruction*, 8> RetVec;
10951091
SmallVector<Instruction*, 8> LandingPadVec;
@@ -1121,8 +1117,11 @@ bool HWAddressSanitizer::sanitizeFunction(Function &F) {
11211117
uint64_t TypeSize;
11221118
Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize,
11231119
&Alignment, &MaybeMask);
1124-
if (Addr || isa<MemIntrinsic>(Inst))
1120+
if (Addr)
11251121
ToInstrument.push_back(&Inst);
1122+
1123+
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst))
1124+
IntrinToInstrument.push_back(MI);
11261125
}
11271126
}
11281127

@@ -1138,7 +1137,8 @@ bool HWAddressSanitizer::sanitizeFunction(Function &F) {
11381137
F.setPersonalityFn(nullptr);
11391138
}
11401139

1141-
if (AllocasToInstrument.empty() && ToInstrument.empty())
1140+
if (AllocasToInstrument.empty() && ToInstrument.empty() &&
1141+
IntrinToInstrument.empty())
11421142
return false;
11431143

11441144
assert(!LocalDynamicShadow);
@@ -1219,6 +1219,12 @@ bool HWAddressSanitizer::sanitizeFunction(Function &F) {
12191219
for (auto Inst : ToInstrument)
12201220
Changed |= instrumentMemAccess(Inst);
12211221

1222+
if (ClInstrumentMemIntrinsics && !IntrinToInstrument.empty()) {
1223+
for (auto Inst : IntrinToInstrument)
1224+
instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1225+
Changed = true;
1226+
}
1227+
12221228
LocalDynamicShadow = nullptr;
12231229
StackBaseTag = nullptr;
12241230

0 commit comments

Comments
 (0)