Skip to content

Commit 0954205

Browse files
authored
[ubsan][hwasan] Let mixing filters (#100680)
Now the check will be enabled only if each filter is satisfied.
1 parent c80c09f commit 0954205

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -1528,11 +1528,7 @@ static void emitRemark(const Function &F, OptimizationRemarkEmitter &ORE,
15281528

15291529
bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
15301530
Function &F, FunctionAnalysisManager &FAM) const {
1531-
bool Skip = [&]() {
1532-
if (ClRandomSkipRate.getNumOccurrences()) {
1533-
std::bernoulli_distribution D(ClRandomSkipRate);
1534-
return !D(*Rng);
1535-
}
1531+
auto SkipHot = [&]() {
15361532
if (!ClHotPercentileCutoff.getNumOccurrences())
15371533
return false;
15381534
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
@@ -1544,7 +1540,16 @@ bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
15441540
}
15451541
return PSI->isFunctionHotInCallGraphNthPercentile(
15461542
ClHotPercentileCutoff, &F, FAM.getResult<BlockFrequencyAnalysis>(F));
1547-
}();
1543+
};
1544+
1545+
auto SkipRandom = [&]() {
1546+
if (!ClRandomSkipRate.getNumOccurrences())
1547+
return false;
1548+
std::bernoulli_distribution D(ClRandomSkipRate);
1549+
return !D(*Rng);
1550+
};
1551+
1552+
bool Skip = SkipRandom() || SkipHot();
15481553
emitRemark(F, FAM.getResult<OptimizationRemarkEmitterAnalysis>(F), Skip);
15491554
return Skip;
15501555
}

llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,25 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
7676
SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;
7777
std::unique_ptr<RandomNumberGenerator> Rng;
7878

79-
auto ShouldRemove = [&](bool IsHot) {
80-
if (!RandomRate.getNumOccurrences())
81-
return IsHot;
79+
auto GetRng = [&]() -> RandomNumberGenerator & {
8280
if (!Rng)
8381
Rng = F.getParent()->createRNG(F.getName());
84-
std::bernoulli_distribution D(RandomRate);
85-
return !D(*Rng);
82+
return *Rng;
83+
};
84+
85+
auto ShouldRemoveHot = [&](const BasicBlock &BB) {
86+
return HotPercentileCutoff.getNumOccurrences() && PSI &&
87+
PSI->isHotCountNthPercentile(
88+
HotPercentileCutoff, BFI.getBlockProfileCount(&BB).value_or(0));
89+
};
90+
91+
auto ShouldRemoveRandom = [&]() {
92+
return RandomRate.getNumOccurrences() &&
93+
!std::bernoulli_distribution(RandomRate)(GetRng());
94+
};
95+
96+
auto ShouldRemove = [&](const BasicBlock &BB) {
97+
return ShouldRemoveRandom() || ShouldRemoveHot(BB);
8698
};
8799

88100
for (BasicBlock &BB : F) {
@@ -96,13 +108,7 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
96108
case Intrinsic::allow_runtime_check: {
97109
++NumChecksTotal;
98110

99-
bool IsHot = false;
100-
if (PSI) {
101-
uint64_t Count = BFI.getBlockProfileCount(&BB).value_or(0);
102-
IsHot = PSI->isHotCountNthPercentile(HotPercentileCutoff, Count);
103-
}
104-
105-
bool ToRemove = ShouldRemove(IsHot);
111+
bool ToRemove = ShouldRemove(BB);
106112
ReplaceWithValue.push_back({
107113
II,
108114
ToRemove,

llvm/test/Instrumentation/HWAddressSanitizer/pgo-opt-out.ll

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -pass-remarks=hwasan -pass-remarks-missed=hwasan -S -hwasan-percentile-cutoff-hot=990000 2>&1 | FileCheck %s --check-prefix=NONE
33
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -pass-remarks=hwasan -pass-remarks-missed=hwasan -S -hwasan-random-rate=1.0 2>&1 | FileCheck %s --check-prefix=ALL
44
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -pass-remarks=hwasan -pass-remarks-missed=hwasan -S -hwasan-random-rate=0.0 2>&1 | FileCheck %s --check-prefix=NONE
5+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -pass-remarks=hwasan -pass-remarks-missed=hwasan -S -hwasan-random-rate=1.0 -hwasan-percentile-cutoff-hot=990000 2>&1 | FileCheck %s --check-prefix=NONE
6+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -pass-remarks=hwasan -pass-remarks-missed=hwasan -S -hwasan-random-rate=0.0 -hwasan-percentile-cutoff-hot=700000 2>&1 | FileCheck %s --check-prefix=NONE
57

68
; ALL: remark: <unknown>:0:0: Sanitized: F=sanitize
79
; ALL: @sanitized

llvm/test/Transforms/lower-builtin-allow-check.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ define dso_local noundef i32 @veryHot(ptr noundef readonly %0) !prof !39 {
309309
; ALL70-LABEL: define dso_local noundef i32 @veryHot(
310310
; ALL70-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
311311
; ALL70-NEXT: [[CHK:%.*]] = icmp eq ptr [[TMP0]], null
312-
; ALL70-NEXT: [[HOT:%.*]] = xor i1 true, true
312+
; ALL70-NEXT: [[HOT:%.*]] = xor i1 false, true
313313
; ALL70-NEXT: [[TMP2:%.*]] = or i1 [[CHK]], [[HOT]]
314314
; ALL70-NEXT: br i1 [[TMP2]], label [[TMP3:%.*]], label [[TMP4:%.*]]
315315
; ALL70: 3:

0 commit comments

Comments
 (0)