Skip to content

[HWASAN][UBSAN] Reverse random logic #88070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ static cl::opt<int> ClHotPercentileCutoff("hwasan-percentile-cutoff-hot",
cl::desc("Hot percentile cuttoff."));

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

STATISTIC(NumTotalFuncs, "Number of total funcs");
STATISTIC(NumInstrumentedFuncs, "Number of instrumented funcs");
Expand Down Expand Up @@ -1496,7 +1496,7 @@ bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
Function &F, FunctionAnalysisManager &FAM) const {
if (ClRandomSkipRate.getNumOccurrences()) {
std::bernoulli_distribution D(ClRandomSkipRate);
return (D(*Rng));
return !D(*Rng);
}
if (!ClHotPercentileCutoff.getNumOccurrences())
return false;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static cl::opt<int>
static cl::opt<float>
RandomRate("lower-allow-check-random-rate",
cl::desc("Probability value in the range [0.0, 1.0] of "
"unconditional pseudo-random checks removal."));
"unconditional pseudo-random checks."));

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

for (BasicBlock &BB : F) {
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Instrumentation/HWAddressSanitizer/pgo-opt-out.ll
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=700000 | FileCheck %s --check-prefix=HOT70
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=990000 | FileCheck %s --check-prefix=HOT99
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=0.0 | FileCheck %s --check-prefix=RANDOM0
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=1.0 | FileCheck %s --check-prefix=RANDOM1
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-rate=1.0 | FileCheck %s --check-prefix=ALL
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-rate=0.0 | FileCheck %s --check-prefix=NONE

; HOT70: @sanitized
; HOT70-NEXT: @__hwasan_tls

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

; RANDOM0: @sanitized
; RANDOM0-NEXT: @__hwasan_tls
; ALL: @sanitized
; ALL-NEXT: @__hwasan_tls

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

declare void @use(ptr)

Expand Down
154 changes: 77 additions & 77 deletions llvm/test/Transforms/lower-builtin-allow-check.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt < %s -passes='function(lower-allow-check)' -S | FileCheck %s --check-prefixes=NOPROFILE
; RUN: opt < %s -passes='function(lower-allow-check)' -lower-allow-check-random-rate=1 -S | FileCheck %s --check-prefixes=ALL
; RUN: opt < %s -passes='function(lower-allow-check)' -lower-allow-check-random-rate=0 -S | FileCheck %s --check-prefixes=NONE
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=990000 -S | FileCheck %s --check-prefixes=HOT99
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=700000 -S | FileCheck %s --check-prefixes=HOT70

Expand All @@ -23,18 +23,18 @@ define dso_local noundef i32 @simple(ptr noundef readonly %0) {
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NOPROFILE-NEXT: ret i32 [[TMP5]]
;
; ALL-LABEL: define dso_local noundef i32 @simple(
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) {
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; ALL: 3:
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
; ALL-NEXT: unreachable
; ALL: 4:
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; ALL-NEXT: ret i32 [[TMP5]]
; NONE-LABEL: define dso_local noundef i32 @simple(
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) {
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; NONE: 3:
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
; NONE-NEXT: unreachable
; NONE: 4:
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NONE-NEXT: ret i32 [[TMP5]]
;
; HOT99-LABEL: define dso_local noundef i32 @simple(
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) {
Expand Down Expand Up @@ -92,18 +92,18 @@ define dso_local noundef i32 @hot(ptr noundef readonly %0) !prof !36 {
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NOPROFILE-NEXT: ret i32 [[TMP5]]
;
; ALL-LABEL: define dso_local noundef i32 @hot(
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; ALL: 3:
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
; ALL-NEXT: unreachable
; ALL: 4:
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; ALL-NEXT: ret i32 [[TMP5]]
; NONE-LABEL: define dso_local noundef i32 @hot(
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; NONE: 3:
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
; NONE-NEXT: unreachable
; NONE: 4:
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NONE-NEXT: ret i32 [[TMP5]]
;
; HOT99-LABEL: define dso_local noundef i32 @hot(
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF16:![0-9]+]] {
Expand Down Expand Up @@ -160,18 +160,18 @@ define dso_local noundef i32 @veryHot(ptr noundef readonly %0) !prof !39 {
; NOPROFILE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NOPROFILE-NEXT: ret i32 [[TMP5]]
;
; ALL-LABEL: define dso_local noundef i32 @veryHot(
; ALL-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
; ALL-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
; ALL-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; ALL-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; ALL: 3:
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
; ALL-NEXT: unreachable
; ALL: 4:
; ALL-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; ALL-NEXT: ret i32 [[TMP5]]
; NONE-LABEL: define dso_local noundef i32 @veryHot(
; NONE-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
; NONE-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
; NONE-NEXT: [[TMP6:%.*]] = or i1 [[TMP2]], [[HOT]]
; NONE-NEXT: br i1 [[TMP6]], label [[TMP3:%.*]], label [[TMP4:%.*]]
; NONE: 3:
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
; NONE-NEXT: unreachable
; NONE: 4:
; NONE-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP0]], align 4
; NONE-NEXT: ret i32 [[TMP5]]
;
; HOT99-LABEL: define dso_local noundef i32 @veryHot(
; HOT99-SAME: ptr noundef readonly [[TMP0:%.*]]) !prof [[PROF17:![0-9]+]] {
Expand Down Expand Up @@ -235,24 +235,24 @@ define dso_local noundef i32 @branchColdFnHot(i32 noundef %0, ptr noundef readon
; NOPROFILE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; NOPROFILE-NEXT: ret i32 [[TMP10]]
;
; ALL-LABEL: define dso_local noundef i32 @branchColdFnHot(
; ALL-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
; ALL-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
; ALL-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF18:![0-9]+]]
; ALL: 4:
; ALL-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
; ALL-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
; ALL-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
; ALL: 6:
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
; ALL-NEXT: unreachable
; ALL: 7:
; ALL-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
; ALL-NEXT: br label [[TMP9]]
; ALL: 9:
; ALL-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; ALL-NEXT: ret i32 [[TMP10]]
; NONE-LABEL: define dso_local noundef i32 @branchColdFnHot(
; NONE-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
; NONE-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
; NONE-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF18:![0-9]+]]
; NONE: 4:
; NONE-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
; NONE-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
; NONE-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
; NONE: 6:
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
; NONE-NEXT: unreachable
; NONE: 7:
; NONE-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
; NONE-NEXT: br label [[TMP9]]
; NONE: 9:
; NONE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; NONE-NEXT: ret i32 [[TMP10]]
;
; HOT99-LABEL: define dso_local noundef i32 @branchColdFnHot(
; HOT99-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF17]] {
Expand Down Expand Up @@ -335,24 +335,24 @@ define dso_local noundef i32 @branchHotFnCold(i32 noundef %0, ptr noundef readon
; NOPROFILE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; NOPROFILE-NEXT: ret i32 [[TMP10]]
;
; ALL-LABEL: define dso_local noundef i32 @branchHotFnCold(
; ALL-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
; ALL-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
; ALL-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF19:![0-9]+]]
; ALL: 4:
; ALL-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
; ALL-NEXT: [[HOT:%.*]] = xor i1 false, true
; ALL-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
; ALL-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
; ALL: 6:
; ALL-NEXT: tail call void @llvm.ubsantrap(i8 22)
; ALL-NEXT: unreachable
; ALL: 7:
; ALL-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
; ALL-NEXT: br label [[TMP9]]
; ALL: 9:
; ALL-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; ALL-NEXT: ret i32 [[TMP10]]
; NONE-LABEL: define dso_local noundef i32 @branchHotFnCold(
; NONE-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
; NONE-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 0
; NONE-NEXT: br i1 [[TMP3]], label [[TMP9:%.*]], label [[TMP4:%.*]], !prof [[PROF19:![0-9]+]]
; NONE: 4:
; NONE-NEXT: [[TMP5:%.*]] = icmp eq ptr [[TMP1]], null
; NONE-NEXT: [[HOT:%.*]] = xor i1 false, true
; NONE-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[HOT]]
; NONE-NEXT: br i1 [[TMP11]], label [[TMP6:%.*]], label [[TMP7:%.*]]
; NONE: 6:
; NONE-NEXT: tail call void @llvm.ubsantrap(i8 22)
; NONE-NEXT: unreachable
; NONE: 7:
; NONE-NEXT: [[TMP8:%.*]] = load i32, ptr [[TMP1]], align 4
; NONE-NEXT: br label [[TMP9]]
; NONE: 9:
; NONE-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP8]], [[TMP7]] ], [ 0, [[TMP2:%.*]] ]
; NONE-NEXT: ret i32 [[TMP10]]
;
; HOT99-LABEL: define dso_local noundef i32 @branchHotFnCold(
; HOT99-SAME: i32 noundef [[TMP0:%.*]], ptr noundef readonly [[TMP1:%.*]]) !prof [[PROF16]] {
Expand Down Expand Up @@ -445,10 +445,10 @@ define dso_local noundef i32 @branchHotFnCold(i32 noundef %0, ptr noundef readon
; NOPROFILE: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
; NOPROFILE: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
;.
; ALL: [[PROF16]] = !{!"function_entry_count", i64 1000}
; ALL: [[PROF17]] = !{!"function_entry_count", i64 7000}
; ALL: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
; ALL: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
; NONE: [[PROF16]] = !{!"function_entry_count", i64 1000}
; NONE: [[PROF17]] = !{!"function_entry_count", i64 7000}
; NONE: [[PROF18]] = !{!"branch_weights", i32 1000, i32 1}
; NONE: [[PROF19]] = !{!"branch_weights", i32 1, i32 1000}
;.
; HOT99: [[PROF16]] = !{!"function_entry_count", i64 1000}
; HOT99: [[PROF17]] = !{!"function_entry_count", i64 7000}
Expand Down