Skip to content

Commit

Permalink
Revert "[ubsan] Connect -fsanitize-skip-hot-cutoff to LowerAllowCheck…
Browse files Browse the repository at this point in the history
…Pass<cutoffs>" (#125032)

Reverts llvm/llvm-project#124857 due to buildbot breakage
(https://lab.llvm.org/buildbot/#/builders/46/builds/11310)
  • Loading branch information
thurstond authored Jan 30, 2025
1 parent fd94c85 commit 928cad4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 141 deletions.
4 changes: 0 additions & 4 deletions clang/include/clang/Basic/Sanitizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ class SanitizerMaskCutoffs {

void set(SanitizerMask K, double V);
void clear(SanitizerMask K = SanitizerKind::All);

// Returns nullopt if all the values are zero.
// Otherwise, return value contains a vector of all the scaled values.
std::optional<std::vector<unsigned>> getAllScaled(unsigned ScalingFactor) const;
};

struct SanitizerSet {
Expand Down
22 changes: 0 additions & 22 deletions clang/lib/Basic/Sanitizers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cmath>
#include <optional>

using namespace clang;
Expand All @@ -44,27 +43,6 @@ std::optional<double> SanitizerMaskCutoffs::operator[](unsigned Kind) const {

void SanitizerMaskCutoffs::clear(SanitizerMask K) { set(K, 0); }

std::optional<std::vector<unsigned>>
SanitizerMaskCutoffs::getAllScaled(unsigned ScalingFactor) const {
std::vector<unsigned> ScaledCutoffs;

bool AnyCutoff = false;
for (unsigned int i = 0; i < SanitizerKind::SO_Count; ++i) {
auto C = (*this)[i];
if (C.has_value()) {
ScaledCutoffs.push_back(lround(std::clamp(*C, 0.0, 1.0) * ScalingFactor));
AnyCutoff = true;
} else {
ScaledCutoffs.push_back(0);
}
}

if (AnyCutoff)
return ScaledCutoffs;

return std::nullopt;
}

// Once LLVM switches to C++17, the constexpr variables can be inline and we
// won't need this.
#define SANITIZER(NAME, ID) constexpr SanitizerMask SanitizerKind::ID;
Expand Down
10 changes: 1 addition & 9 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,20 +795,12 @@ static void addSanitizers(const Triple &TargetTriple,
PB.registerOptimizerLastEPCallback(SanitizersCallback);
}

// SanitizeSkipHotCutoffs: doubles with range [0, 1]
// Opts.cutoffs: unsigned ints with range [0, 1000000]
auto ScaledCutoffs = CodeGenOpts.SanitizeSkipHotCutoffs.getAllScaled(1000000);

// TODO: remove IsRequested()
if (LowerAllowCheckPass::IsRequested() || ScaledCutoffs.has_value()) {
if (LowerAllowCheckPass::IsRequested()) {
// We want to call it after inline, which is about OptimizerEarlyEPCallback.
PB.registerOptimizerEarlyEPCallback([&](ModulePassManager &MPM,
OptimizationLevel Level,
ThinOrFullLTOPhase Phase) {
LowerAllowCheckPass::Options Opts;
// TODO: after removing IsRequested(), make this unconditional
if (ScaledCutoffs.has_value())
Opts.cutoffs = ScaledCutoffs.value();
MPM.addPass(createModuleToFunctionPassAdaptor(LowerAllowCheckPass(Opts)));
});
}
Expand Down
28 changes: 12 additions & 16 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3614,33 +3614,29 @@ void CodeGenFunction::EmitCheck(
llvm::Value *RecoverableCond = nullptr;
llvm::Value *TrapCond = nullptr;
bool NoMerge = false;
// Expand checks into:
// (Check1 || !allow_ubsan_check) && (Check2 || !allow_ubsan_check) ...
// We need separate allow_ubsan_check intrinsics because they have separately
// specified cutoffs.
// This expression looks expensive but will be simplified after
// LowerAllowCheckPass.
for (auto &[Check, Ord] : Checked) {
llvm::Value *GuardedCheck = Check;
if (ClSanitizeGuardChecks ||
(CGM.getCodeGenOpts().SanitizeSkipHotCutoffs[Ord] > 0)) {
llvm::Value *Allow = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::allow_ubsan_check),
llvm::ConstantInt::get(CGM.Int8Ty, Ord));
GuardedCheck = Builder.CreateOr(Check, Builder.CreateNot(Allow));
}

// -fsanitize-trap= overrides -fsanitize-recover=.
llvm::Value *&Cond = CGM.getCodeGenOpts().SanitizeTrap.has(Ord) ? TrapCond
: CGM.getCodeGenOpts().SanitizeRecover.has(Ord)
? RecoverableCond
: FatalCond;
Cond = Cond ? Builder.CreateAnd(Cond, GuardedCheck) : GuardedCheck;
Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check;

if (!CGM.getCodeGenOpts().SanitizeMergeHandlers.has(Ord))
NoMerge = true;
}

if (ClSanitizeGuardChecks) {
llvm::Value *Allow =
Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::allow_ubsan_check),
llvm::ConstantInt::get(CGM.Int8Ty, CheckHandler));

for (llvm::Value **Cond : {&FatalCond, &RecoverableCond, &TrapCond}) {
if (*Cond)
*Cond = Builder.CreateOr(*Cond, Builder.CreateNot(Allow));
}
}

if (TrapCond)
EmitTrapCheck(TrapCond, CheckHandler, NoMerge);
if (!FatalCond && !RecoverableCond)
Expand Down
5 changes: 0 additions & 5 deletions clang/test/CodeGen/allow-ubsan-check-inline.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s -fsanitize=signed-integer-overflow -fsanitize-skip-hot-cutoff=signed-integer-overflow=0.000001 -O3 -mllvm -lower-allow-check-random-rate=1 -Rpass=lower-allow-check -Rpass-missed=lower-allow-check -fno-inline 2>&1 | FileCheck %s --check-prefixes=NOINL --implicit-check-not="remark:"
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s -fsanitize=signed-integer-overflow -fsanitize-skip-hot-cutoff=signed-integer-overflow=0.000001 -O3 -mllvm -lower-allow-check-random-rate=1 -Rpass=lower-allow-check -Rpass-missed=lower-allow-check 2>&1 | FileCheck %s --check-prefixes=INLINE --implicit-check-not="remark:"
//
// -ubsan-guard-checks is deprecated and will be removed in the future;
// use -fsanitize-skip-hot-cutoff, as shown above.
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s -fsanitize=signed-integer-overflow -mllvm -ubsan-guard-checks -O3 -mllvm -lower-allow-check-random-rate=1 -Rpass=lower-allow-check -Rpass-missed=lower-allow-check -fno-inline 2>&1 | FileCheck %s --check-prefixes=NOINL --implicit-check-not="remark:"
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s -fsanitize=signed-integer-overflow -mllvm -ubsan-guard-checks -O3 -mllvm -lower-allow-check-random-rate=1 -Rpass=lower-allow-check -Rpass-missed=lower-allow-check 2>&1 | FileCheck %s --check-prefixes=INLINE --implicit-check-not="remark:"

Expand Down
Loading

0 comments on commit 928cad4

Please sign in to comment.