LLVM  6.0.0svn
BoundsChecking.cpp
Go to the documentation of this file.
1 //===- BoundsChecking.cpp - Instrumentation for run-time bounds checking --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/ADT/Twine.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/Value.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Debug.h"
33 #include <cstdint>
34 #include <vector>
35 
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "bounds-checking"
39 
40 static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
41  cl::desc("Use one trap block per function"));
42 
43 STATISTIC(ChecksAdded, "Bounds checks added");
44 STATISTIC(ChecksSkipped, "Bounds checks skipped");
45 STATISTIC(ChecksUnable, "Bounds checks unable to add");
46 
48 
49 /// Adds run-time bounds checks to memory accessing instructions.
50 ///
51 /// \p Ptr is the pointer that will be read/written, and \p InstVal is either
52 /// the result from the load or the value being stored. It is used to determine
53 /// the size of memory block that is touched.
54 ///
55 /// \p GetTrapBB is a callable that returns the trap BB to use on failure.
56 ///
57 /// Returns true if any change was made to the IR, false otherwise.
58 template <typename GetTrapBBT>
59 static bool instrumentMemAccess(Value *Ptr, Value *InstVal,
60  const DataLayout &DL, TargetLibraryInfo &TLI,
61  ObjectSizeOffsetEvaluator &ObjSizeEval,
62  BuilderTy &IRB,
63  GetTrapBBT GetTrapBB) {
64  uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
65  DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
66  << " bytes\n");
67 
68  SizeOffsetEvalType SizeOffset = ObjSizeEval.compute(Ptr);
69 
70  if (!ObjSizeEval.bothKnown(SizeOffset)) {
71  ++ChecksUnable;
72  return false;
73  }
74 
75  Value *Size = SizeOffset.first;
76  Value *Offset = SizeOffset.second;
78 
79  Type *IntTy = DL.getIntPtrType(Ptr->getType());
80  Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
81 
82  // three checks are required to ensure safety:
83  // . Offset >= 0 (since the offset is given from the base ptr)
84  // . Size >= Offset (unsigned)
85  // . Size - Offset >= NeededSize (unsigned)
86  //
87  // optimization: if Size >= 0 (signed), skip 1st check
88  // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
89  Value *ObjSize = IRB.CreateSub(Size, Offset);
90  Value *Cmp2 = IRB.CreateICmpULT(Size, Offset);
91  Value *Cmp3 = IRB.CreateICmpULT(ObjSize, NeededSizeVal);
92  Value *Or = IRB.CreateOr(Cmp2, Cmp3);
93  if (!SizeCI || SizeCI->getValue().slt(0)) {
94  Value *Cmp1 = IRB.CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
95  Or = IRB.CreateOr(Cmp1, Or);
96  }
97 
98  // check if the comparison is always false
99  ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or);
100  if (C) {
101  ++ChecksSkipped;
102  // If non-zero, nothing to do.
103  if (!C->getZExtValue())
104  return true;
105  }
106  ++ChecksAdded;
107 
108  BasicBlock::iterator SplitI = IRB.GetInsertPoint();
109  BasicBlock *OldBB = SplitI->getParent();
110  BasicBlock *Cont = OldBB->splitBasicBlock(SplitI);
111  OldBB->getTerminator()->eraseFromParent();
112 
113  if (C) {
114  // If we have a constant zero, unconditionally branch.
115  // FIXME: We should really handle this differently to bypass the splitting
116  // the block.
117  BranchInst::Create(GetTrapBB(IRB), OldBB);
118  return true;
119  }
120 
121  // Create the conditional branch.
122  BranchInst::Create(GetTrapBB(IRB), Cont, Or, OldBB);
123  return true;
124 }
125 
127  const DataLayout &DL = F.getParent()->getDataLayout();
128  ObjectSizeOffsetEvaluator ObjSizeEval(DL, &TLI, F.getContext(),
129  /*RoundToAlign=*/true);
130 
131  // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
132  // touching instructions
133  std::vector<Instruction *> WorkList;
134  for (Instruction &I : instructions(F)) {
135  if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
136  isa<AtomicRMWInst>(I))
137  WorkList.push_back(&I);
138  }
139 
140  // Create a trapping basic block on demand using a callback. Depending on
141  // flags, this will either create a single block for the entire function or
142  // will create a fresh block every time it is called.
143  BasicBlock *TrapBB = nullptr;
144  auto GetTrapBB = [&TrapBB](BuilderTy &IRB) {
145  if (TrapBB && SingleTrapBB)
146  return TrapBB;
147 
148  Function *Fn = IRB.GetInsertBlock()->getParent();
149  // FIXME: This debug location doesn't make a lot of sense in the
150  // `SingleTrapBB` case.
151  auto DebugLoc = IRB.getCurrentDebugLocation();
153  TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
154  IRB.SetInsertPoint(TrapBB);
155 
156  auto *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
157  CallInst *TrapCall = IRB.CreateCall(F, {});
158  TrapCall->setDoesNotReturn();
159  TrapCall->setDoesNotThrow();
160  TrapCall->setDebugLoc(DebugLoc);
161  IRB.CreateUnreachable();
162 
163  return TrapBB;
164  };
165 
166  bool MadeChange = false;
167  for (Instruction *Inst : WorkList) {
168  BuilderTy IRB(Inst->getParent(), BasicBlock::iterator(Inst), TargetFolder(DL));
169  if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
170  MadeChange |= instrumentMemAccess(LI->getPointerOperand(), LI, DL, TLI,
171  ObjSizeEval, IRB, GetTrapBB);
172  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
173  MadeChange |=
174  instrumentMemAccess(SI->getPointerOperand(), SI->getValueOperand(),
175  DL, TLI, ObjSizeEval, IRB, GetTrapBB);
176  } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
177  MadeChange |=
178  instrumentMemAccess(AI->getPointerOperand(), AI->getCompareOperand(),
179  DL, TLI, ObjSizeEval, IRB, GetTrapBB);
180  } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
181  MadeChange |=
182  instrumentMemAccess(AI->getPointerOperand(), AI->getValOperand(), DL,
183  TLI, ObjSizeEval, IRB, GetTrapBB);
184  } else {
185  llvm_unreachable("unknown Instruction type");
186  }
187  }
188  return MadeChange;
189 }
190 
192  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
193 
194  if (!addBoundsChecking(F, TLI))
195  return PreservedAnalyses::all();
196 
197  return PreservedAnalyses::none();
198 }
199 
200 namespace {
201 struct BoundsCheckingLegacyPass : public FunctionPass {
202  static char ID;
203 
204  BoundsCheckingLegacyPass() : FunctionPass(ID) {
206  }
207 
208  bool runOnFunction(Function &F) override {
209  auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
210  return addBoundsChecking(F, TLI);
211  }
212 
213  void getAnalysisUsage(AnalysisUsage &AU) const override {
215  }
216 };
217 } // namespace
218 
220 INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking",
221  "Run-time bounds checking", false, false)
223 INITIALIZE_PASS_END(BoundsCheckingLegacyPass, "bounds-checking",
224  "Run-time bounds checking", false, false)
225 
227  return new BoundsCheckingLegacyPass();
228 }
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:69
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking", "Run-time bounds checking", false, false) INITIALIZE_PASS_END(BoundsCheckingLegacyPass
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1562
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:514
std::pair< Value *, Value * > SizeOffsetEvalType
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1574
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1183
This class represents a function call, abstracting a target machine&#39;s calling convention.
STATISTIC(NumFunctions, "Total number of functions")
A debug info location.
Definition: DebugLoc.h:34
F(f)
An instruction for reading from memory.
Definition: Instructions.h:164
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:677
FunctionPass * createBoundsCheckingLegacyPass()
Legacy pass creation function for the above pass.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static cl::opt< bool > SingleTrapBB("bounds-checking-single-trap", cl::desc("Use one trap block per function"))
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Evaluate the size and offset of an object pointed to by a Value*.
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:915
An instruction for storing to memory.
Definition: Instructions.h:306
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:980
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1083
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:702
static bool runOnFunction(Function &F, bool PostInlining)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bounds checking
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI)
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Provides information about what library functions are available for the current target.
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.
Definition: Constants.cpp:560
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:91
SizeOffsetEvalType compute(Value *V)
#define I(x, y, z)
Definition: MD5.cpp:58
void initializeBoundsCheckingLegacyPassPass(PassRegistry &)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:382
void setDoesNotReturn()
Analysis pass providing the TargetLibraryInfo.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:556
LLVM Value Representation.
Definition: Value.h:73
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:386
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:123
#define DEBUG(X)
Definition: Debug.h:118
inst_range instructions(Function *F)
Definition: InstIterator.h:134
A container for analyses that lazily runs them and caches their results.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
static bool instrumentMemAccess(Value *Ptr, Value *InstVal, const DataLayout &DL, TargetLibraryInfo &TLI, ObjectSizeOffsetEvaluator &ObjSizeEval, BuilderTy &IRB, GetTrapBBT GetTrapBB)
Adds run-time bounds checks to memory accessing instructions.
bool bothKnown(SizeOffsetEvalType SizeOffset)