Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/Sanitizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ 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<int>> getAllScaled(int ScalingFactor) const;
};

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

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

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

std::optional<std::vector<int>>
SanitizerMaskCutoffs::getAllScaled(int ScalingFactor) const {
std::vector<int> scaledCutoffs;

bool anyNonZero = false;
for (unsigned int i = 0; i < SanitizerKind::SO_Count; ++i) {
int scaled = round((operator[](i)).value_or(0) * ScalingFactor);
scaledCutoffs.push_back(scaled);
anyNonZero |= (scaled != 0);
}

if (anyNonZero)
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
20 changes: 19 additions & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,30 @@ static void addSanitizers(const Triple &TargetTriple,
PB.registerOptimizerLastEPCallback(SanitizersCallback);
}

if (LowerAllowCheckPass::IsRequested()) {
// SanitizeSkipHotCutoffs: doubles with range [0, 1]
// Opts.cutoffs: ints with range [0, 1000000]
std::optional<std::vector<int>> scaledCutoffs =
CodeGenOpts.SanitizeSkipHotCutoffs.getAllScaled(1000000);

// TODO: remove IsRequested()
if (LowerAllowCheckPass::IsRequested() || scaledCutoffs.has_value()) {
// 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(), the if case will be unconditional
if (scaledCutoffs.has_value()) {
// Copy from std::vector<int> to std::vector<unsigned int>
Opts.cutoffs = {scaledCutoffs.value().begin(),
scaledCutoffs.value().end()};
} else {
for (unsigned int i = 0; i < SanitizerKind::SO_Count; ++i) {
Opts.cutoffs.push_back(0);
}
}

MPM.addPass(createModuleToFunctionPassAdaptor(LowerAllowCheckPass(Opts)));
});
}
Expand Down
28 changes: 16 additions & 12 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3614,29 +3614,33 @@ 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, Check) : Check;
Cond = Cond ? Builder.CreateAnd(Cond, GuardedCheck) : GuardedCheck;

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: 5 additions & 0 deletions clang/test/CodeGen/allow-ubsan-check-inline.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// 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