35#define DEBUG_TYPE "bounds-checking"
38 cl::desc(
"Use one trap block per function"));
42STATISTIC(ChecksUnable,
"Bounds checks unable to add");
76 Type *IndexTy =
DL.getIndexType(
Ptr->getType());
91 Value *Cmp2 = SizeRange.getUnsignedMin().uge(OffsetRange.getUnsignedMax())
94 Value *Cmp3 = SizeRange.sub(OffsetRange)
96 .uge(NeededSizeRange.getUnsignedMax())
101 !SizeRange.getSignedMin().isNonNegative()) {
114 Intrinsic::ubsantrap, {},
123 B.addAttribute(llvm::Attribute::NoUnwind);
125 B.addAttribute(llvm::Attribute::NoReturn);
138template <
typename GetTrapBBT>
145 if (!
C->getZExtValue())
170 bool MayReturn =
false;
171 bool UseTrap =
false;
172 bool MinRuntime =
false;
173 bool MayMerge =
true;
178 case BoundsCheckingPass::ReportingMode::Trap:
181 case BoundsCheckingPass::ReportingMode::MinRuntime:
182 Name =
"__ubsan_handle_local_out_of_bounds_minimal";
186 case BoundsCheckingPass::ReportingMode::MinRuntimeAbort:
187 Name =
"__ubsan_handle_local_out_of_bounds_minimal_abort";
190 case BoundsCheckingPass::ReportingMode::FullRuntime:
191 Name =
"__ubsan_handle_local_out_of_bounds";
194 case BoundsCheckingPass::ReportingMode::FullRuntimeAbort:
195 Name =
"__ubsan_handle_local_out_of_bounds_abort";
205 if (
F.hasFnAttribute(Attribute::NoSanitizeBounds))
220 if (
LoadInst *LI = dyn_cast<LoadInst>(&
I)) {
221 if (!LI->isVolatile())
223 ObjSizeEval, IRB, SE);
224 }
else if (
StoreInst *SI = dyn_cast<StoreInst>(&
I)) {
225 if (!SI->isVolatile())
227 DL, TLI, ObjSizeEval, IRB, SE);
229 if (!AI->isVolatile())
232 DL, TLI, ObjSizeEval, IRB, SE);
234 if (!AI->isVolatile())
236 DL, TLI, ObjSizeEval, IRB, SE);
248 auto DebugLoc = IRB.getCurrentDebugLocation();
258 IRB.SetInsertPoint(TrapBB);
265 TrapCall->
addFnAttr(llvm::Attribute::NoMerge);
273 IRB.CreateUnreachable();
277 ReuseTrapBB = TrapBB;
282 for (
const auto &Entry : TrapInfo) {
288 return !TrapInfo.
empty();
305 OS, MapClassName2PassName);
306 switch (Options.
Mode) {
314 OS <<
"<min-rt-abort";
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static void insertBoundsCheck(Value *Or, BuilderTy &IRB, GetTrapBBT GetTrapBB)
Adds run-time bounds checks to memory accessing instructions.
static CallInst * InsertCall(BuilderTy &IRB, bool MayReturn, StringRef Name)
static Value * getBoundsCheckCond(Value *Ptr, Value *InstVal, const DataLayout &DL, TargetLibraryInfo &TLI, ObjectSizeOffsetEvaluator &ObjSizeEval, BuilderTy &IRB, ScalarEvolution &SE)
Gets the conditions under which memory accessing instructions will overflow.
static CallInst * InsertTrap(BuilderTy &IRB, bool DebugTrapBB)
static cl::opt< bool > SingleTrapBB("bounds-checking-single-trap", cl::desc("Use one trap block per function"))
static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI, ScalarEvolution &SE, const ReportingOpts &Opts)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
bool slt(const APInt &RHS) const
Signed less than comparison.
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
static ConstantInt * getFalse(LLVMContext &Context)
const APInt & getValue() const
Return the constant as an APInt value reference.
A parsed version of the target data layout string in and methods for querying it.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Module * getParent()
Get the module that this global value is contained inside of...
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
BasicBlock::iterator GetInsertPoint() const
Value * CreateTypeSize(Type *DstType, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
BasicBlock * GetInsertBlock() const
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Evaluate the size and offset of an object pointed to by a Value*.
SizeOffsetValue compute(Value *V)
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
TargetFolder - Create constants with target dependent folding.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
static Type * getVoidTy(LLVMContext &C)
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
This class implements an extremely fast bulk output stream that can only output to a stream.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ Or
Bitwise or logical OR of integers.
ReportingOpts(BoundsCheckingPass::ReportingMode Mode, bool Merge)
BoundsCheckingOptions(ReportingMode Mode, bool Merge)
Various options to control the behavior of getObjectSize.
Mode EvalMode
How we want to evaluate this object's size.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
@ ExactUnderlyingSizeAndOffset
All branches must be known and have the same underlying size and offset to be merged.
A CRTP mix-in to automatically provide informational APIs needed for passes.