Skip to content

Commit 0646344

Browse files
authored
[HWASAN][UBSAN] Reverse random logic (#88070)
It feels more intuitive to make higher P to keep more checks.
1 parent 1c4ec8d commit 0646344

File tree

4 files changed

+88
-88
lines changed

4 files changed

+88
-88
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ static cl::opt<int> ClHotPercentileCutoff("hwasan-percentile-cutoff-hot",
186186
cl::desc("Hot percentile cuttoff."));
187187

188188
static cl::opt<float>
189-
ClRandomSkipRate("hwasan-random-skip-rate",
189+
ClRandomSkipRate("hwasan-random-rate",
190190
cl::desc("Probability value in the range [0.0, 1.0] "
191-
"to skip instrumentation of a function."));
191+
"to keep instrumentation of a function."));
192192

193193
STATISTIC(NumTotalFuncs, "Number of total funcs");
194194
STATISTIC(NumInstrumentedFuncs, "Number of instrumented funcs");
@@ -1496,7 +1496,7 @@ bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
14961496
Function &F, FunctionAnalysisManager &FAM) const {
14971497
if (ClRandomSkipRate.getNumOccurrences()) {
14981498
std::bernoulli_distribution D(ClRandomSkipRate);
1499-
return (D(*Rng));
1499+
return !D(*Rng);
15001500
}
15011501
if (!ClHotPercentileCutoff.getNumOccurrences())
15021502
return false;

llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static cl::opt<int>
3030
static cl::opt<float>
3131
RandomRate("lower-allow-check-random-rate",
3232
cl::desc("Probability value in the range [0.0, 1.0] of "
33-
"unconditional pseudo-random checks removal."));
33+
"unconditional pseudo-random checks."));
3434

3535
STATISTIC(NumChecksTotal, "Number of checks");
3636
STATISTIC(NumChecksRemoved, "Number of removed checks");
@@ -48,7 +48,7 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
4848
if (!Rng)
4949
Rng = F.getParent()->createRNG(F.getName());
5050
std::bernoulli_distribution D(RandomRate);
51-
return D(*Rng);
51+
return !D(*Rng);
5252
};
5353

5454
for (BasicBlock &BB : F) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=700000 | FileCheck %s --check-prefix=HOT70
22
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=990000 | FileCheck %s --check-prefix=HOT99
3-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=0.0 | FileCheck %s --check-prefix=RANDOM0
4-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=1.0 | FileCheck %s --check-prefix=RANDOM1
3+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-rate=1.0 | FileCheck %s --check-prefix=ALL
4+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-rate=0.0 | FileCheck %s --check-prefix=NONE
55

66
; HOT70: @sanitized
77
; HOT70-NEXT: @__hwasan_tls
88

99
; HOT99: @sanitized
1010
; HOT99-NEXT: %x = alloca i8, i64 4
1111

12-
; RANDOM0: @sanitized
13-
; RANDOM0-NEXT: @__hwasan_tls
12+
; ALL: @sanitized
13+
; ALL-NEXT: @__hwasan_tls
1414

15-
; RANDOM1: @sanitized
16-
; RANDOM1-NEXT: %x = alloca i8, i64 4
15+
; NONE: @sanitized
16+
; NONE-NEXT: %x = alloca i8, i64 4
1717

1818
declare void @use(ptr)
1919

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

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
22
; RUN: opt < %s -passes='function(lower-allow-check)' -S | FileCheck %s --check-prefixes=NOPROFILE
3-
; RUN: opt < %s -passes='function(lower-allow-check)' -lower-allow-check-random-rate=1 -S | FileCheck %s --check-prefixes=ALL
3+
; RUN: opt < %s -passes='function(lower-allow-check)' -lower-allow-check-random-rate=0 -S | FileCheck %s --check-prefixes=NONE
44
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=990000 -S | FileCheck %s --check-prefixes=HOT99
55
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=700000 -S | FileCheck %s --check-prefixes=HOT70
66

@@ -23,18 +23,18 @@ define dso_local noundef i32 @simple(ptr noundef readonly %0) {
2323
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
2424
; NOPROFILE-NEXT: ret i32 [[TMP5]]
2525
;
26-
; ALL-LABEL: define dso_local noundef i32 @simple(
27-
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) {
28-
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
29-
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
30-
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
31-
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
32-
; ALL: 3:
33-
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
34-
; ALL-NEXT: unreachable
35-
; ALL: 4:
36-
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
37-
; ALL-NEXT: ret i32 [[TMP5]]
26+
; NONE-LABEL: define dso_local noundef i32 @simple(
27+
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) {
28+
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
29+
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
30+
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
31+
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
32+
; NONE: 3:
33+
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
34+
; NONE-NEXT: unreachable
35+
; NONE: 4:
36+
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
37+
; NONE-NEXT: ret i32 [[TMP5]]
3838
;
3939
; HOT99-LABEL: define dso_local noundef i32 @simple(
4040
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) {
@@ -92,18 +92,18 @@ define dso_local noundef i32 @hot(ptr noundef readonly %0) !prof !36 {
9292
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
9393
; NOPROFILE-NEXT: ret i32 [[TMP5]]
9494
;
95-
; ALL-LABEL: define dso_local noundef i32 @hot(
96-
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
97-
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
98-
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
99-
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
100-
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
101-
; ALL: 3:
102-
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
103-
; ALL-NEXT: unreachable
104-
; ALL: 4:
105-
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
106-
; ALL-NEXT: ret i32 [[TMP5]]
95+
; NONE-LABEL: define dso_local noundef i32 @hot(
96+
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
97+
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
98+
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
99+
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
100+
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
101+
; NONE: 3:
102+
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
103+
; NONE-NEXT: unreachable
104+
; NONE: 4:
105+
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
106+
; NONE-NEXT: ret i32 [[TMP5]]
107107
;
108108
; HOT99-LABEL: define dso_local noundef i32 @hot(
109109
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
@@ -160,18 +160,18 @@ define dso_local noundef i32 @veryHot(ptr noundef readonly %0) !prof !39 {
160160
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
161161
; NOPROFILE-NEXT: ret i32 [[TMP5]]
162162
;
163-
; ALL-LABEL: define dso_local noundef i32 @veryHot(
164-
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
165-
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
166-
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
167-
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
168-
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
169-
; ALL: 3:
170-
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
171-
; ALL-NEXT: unreachable
172-
; ALL: 4:
173-
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
174-
; ALL-NEXT: ret i32 [[TMP5]]
163+
; NONE-LABEL: define dso_local noundef i32 @veryHot(
164+
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
165+
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
166+
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
167+
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
168+
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
169+
; NONE: 3:
170+
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
171+
; NONE-NEXT: unreachable
172+
; NONE: 4:
173+
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
174+
; NONE-NEXT: ret i32 [[TMP5]]
175175
;
176176
; HOT99-LABEL: define dso_local noundef i32 @veryHot(
177177
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
@@ -235,24 +235,24 @@ define dso_local noundef i32 @branchColdFnHot(i32 noundef %0, ptr noundef readon
235235
; NOPROFILE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
236236
; NOPROFILE-NEXT: ret i32 [[TMP10]]
237237
;
238-
; ALL-LABEL: define dso_local noundef i32 @branchColdFnHot(
239-
; ALL-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
240-
; ALL-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
241-
; ALL-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF18:![0-9]+]]
242-
; ALL: 4:
243-
; ALL-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
244-
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
245-
; ALL-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
246-
; ALL-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
247-
; ALL: 6:
248-
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
249-
; ALL-NEXT: unreachable
250-
; ALL: 7:
251-
; ALL-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
252-
; ALL-NEXT: br label [[TMP9]]
253-
; ALL: 9:
254-
; ALL-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
255-
; ALL-NEXT: ret i32 [[TMP10]]
238+
; NONE-LABEL: define dso_local noundef i32 @branchColdFnHot(
239+
; NONE-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
240+
; NONE-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
241+
; NONE-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF18:![0-9]+]]
242+
; NONE: 4:
243+
; NONE-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
244+
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
245+
; NONE-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
246+
; NONE-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
247+
; NONE: 6:
248+
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
249+
; NONE-NEXT: unreachable
250+
; NONE: 7:
251+
; NONE-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
252+
; NONE-NEXT: br label [[TMP9]]
253+
; NONE: 9:
254+
; NONE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
255+
; NONE-NEXT: ret i32 [[TMP10]]
256256
;
257257
; HOT99-LABEL: define dso_local noundef i32 @branchColdFnHot(
258258
; HOT99-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
@@ -335,24 +335,24 @@ define dso_local noundef i32 @branchHotFnCold(i32 noundef %0, ptr noundef readon
335335
; NOPROFILE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
336336
; NOPROFILE-NEXT: ret i32 [[TMP10]]
337337
;
338-
; ALL-LABEL: define dso_local noundef i32 @branchHotFnCold(
339-
; ALL-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
340-
; ALL-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
341-
; ALL-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF19:![0-9]+]]
342-
; ALL: 4:
343-
; ALL-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
344-
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
345-
; ALL-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
346-
; ALL-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
347-
; ALL: 6:
348-
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
349-
; ALL-NEXT: unreachable
350-
; ALL: 7:
351-
; ALL-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
352-
; ALL-NEXT: br label [[TMP9]]
353-
; ALL: 9:
354-
; ALL-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
355-
; ALL-NEXT: ret i32 [[TMP10]]
338+
; NONE-LABEL: define dso_local noundef i32 @branchHotFnCold(
339+
; NONE-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
340+
; NONE-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
341+
; NONE-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF19:![0-9]+]]
342+
; NONE: 4:
343+
; NONE-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
344+
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
345+
; NONE-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
346+
; NONE-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
347+
; NONE: 6:
348+
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
349+
; NONE-NEXT: unreachable
350+
; NONE: 7:
351+
; NONE-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
352+
; NONE-NEXT: br label [[TMP9]]
353+
; NONE: 9:
354+
; NONE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
355+
; NONE-NEXT: ret i32 [[TMP10]]
356356
;
357357
; HOT99-LABEL: define dso_local noundef i32 @branchHotFnCold(
358358
; HOT99-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
@@ -445,10 +445,10 @@ define dso_local noundef i32 @branchHotFnCold(i32 noundef %0, ptr noundef readon
445445
; NOPROFILE: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
446446
; NOPROFILE: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
447447
;.
448-
; ALL: [[PROF16]] = !{!"function_entry_count", i64 1000}
449-
; ALL: [[PROF17]] = !{!"function_entry_count", i64 7000}
450-
; ALL: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
451-
; ALL: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
448+
; NONE: [[PROF16]] = !{!"function_entry_count", i64 1000}
449+
; NONE: [[PROF17]] = !{!"function_entry_count", i64 7000}
450+
; NONE: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
451+
; NONE: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
452452
;.
453453
; HOT99: [[PROF16]] = !{!"function_entry_count", i64 1000}
454454
; HOT99: [[PROF17]] = !{!"function_entry_count", i64 7000}

0 commit comments

Comments
 (0)