Go to the documentation of this file.
37 #define DEBUG_TYPE "bounds-checking"
40 cl::desc(
"Use one trap block per function"));
42 STATISTIC(ChecksAdded,
"Bounds checks added");
43 STATISTIC(ChecksSkipped,
"Bounds checks skipped");
44 STATISTIC(ChecksUnable,
"Bounds checks unable to add");
70 Value *Size = SizeOffset.first;
71 Value *Offset = SizeOffset.second;
89 Value *Cmp2 = SizeRange.getUnsignedMin().uge(OffsetRange.getUnsignedMax())
92 Value *Cmp3 = SizeRange.sub(OffsetRange)
94 .uge(NeededSizeRange.getUnsignedMax())
99 !SizeRange.getSignedMin().isNonNegative()) {
112 template <
typename GetTrapBBT>
119 if (!
C->getZExtValue())
143 if (
F.hasFnAttribute(Attribute::NoSanitizeBounds))
157 if (
LoadInst *LI = dyn_cast<LoadInst>(&
I)) {
158 if (!LI->isVolatile())
160 ObjSizeEval, IRB, SE);
162 if (!
SI->isVolatile())
164 DL, TLI, ObjSizeEval, IRB, SE);
166 if (!AI->isVolatile())
169 DL, TLI, ObjSizeEval, IRB, SE);
171 if (!AI->isVolatile())
173 DL, TLI, ObjSizeEval, IRB, SE);
176 TrapInfo.push_back(std::make_pair(&
I,
Or));
183 auto GetTrapBB = [&TrapBB](
BuilderTy &IRB) {
190 auto DebugLoc = IRB.getCurrentDebugLocation();
193 IRB.SetInsertPoint(TrapBB);
196 CallInst *TrapCall = IRB.CreateCall(
F, {});
200 IRB.CreateUnreachable();
206 for (
const auto &Entry : TrapInfo) {
212 return !TrapInfo.empty();
234 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
F);
235 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
248 "Run-time bounds checking",
false,
false)
254 return new BoundsCheckingLegacyPass();
A set of analyses that are preserved following a run of a transformation pass.
Analysis pass that exposes the ScalarEvolution for a function.
This is an optimization pass for GlobalISel generic memory operations.
void initializeBoundsCheckingLegacyPassPass(PassRegistry &)
FunctionPass * createBoundsCheckingLegacyPass()
Legacy pass creation function for the above pass.
@ Or
Bitwise or logical OR of integers.
A parsed version of the target data layout string in and methods for querying it.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
InstListType::iterator iterator
Instruction iterators...
const Function * getParent() const
Return the enclosing method, or null if none.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
const APInt & getValue() const
Return the constant as an APInt value reference.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
SizeOffsetEvalType compute(Value *V)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The main scalar evolution driver.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
The instances of the Type class are immutable: once they are created, they are never changed.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking", "Run-time bounds checking", false, false) INITIALIZE_PASS_END(BoundsCheckingLegacyPass
LLVM Basic Block Representation.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
This is the shared class of boolean and integer constants.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
(vector float) vec_cmpeq(*A, *B) C
Represent the analysis usage information of a pass.
static void insertBoundsCheck(Value *Or, BuilderTy &IRB, GetTrapBBT GetTrapBB)
Adds run-time bounds checks to memory accessing instructions.
STATISTIC(NumFunctions, "Total number of functions")
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Evaluate the size and offset of an object pointed to by a Value*.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
bool slt(const APInt &RHS) const
Signed less than comparison.
inst_range instructions(Function *F)
An instruction for storing to memory.
static cl::opt< bool > SingleTrapBB("bounds-checking-single-trap", cl::desc("Use one trap block per function"))
BasicBlock::iterator GetInsertPoint() const
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Module * getParent()
Get the module that this global value is contained inside of...
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI, ScalarEvolution &SE)
StandardInstrumentations SI(Debug, VerifyEach)
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
LLVMContext & getContext() const
All values hold a context through their type.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Various options to control the behavior of getObjectSize.
An instruction for reading from memory.
an instruction that atomically reads a memory location, combines it with another value,...
static ConstantInt * getFalse(LLVMContext &Context)
static bool runOnFunction(Function &F, bool PostInlining)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
TargetFolder - Create constants with target dependent folding.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
std::pair< Value *, Value * > SizeOffsetEvalType
Provides information about what library functions are available for the current target.
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.
const BasicBlock * getParent() const
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...
bool bothKnown(SizeOffsetEvalType SizeOffset)
A container for analyses that lazily runs them and caches their results.
FunctionPass class - This class is used to implement most global optimizations.
This class represents a function call, abstracting a target machine's calling convention.
AnalysisUsage & addRequired()
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
LLVM Value Representation.
An instruction that atomically checks whether a specified value is in a memory location,...
Analysis pass providing the TargetLibraryInfo.