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 //
10 // This file implements a pass that instruments the code to perform run-time
11 // bounds checking on loads, stores, and other memory intrinsics.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "bounds-checking"
31 
32 static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
33  cl::desc("Use one trap block per function"));
34 
35 STATISTIC(ChecksAdded, "Bounds checks added");
36 STATISTIC(ChecksSkipped, "Bounds checks skipped");
37 STATISTIC(ChecksUnable, "Bounds checks unable to add");
38 
40 
41 namespace {
42  struct BoundsChecking : public FunctionPass {
43  static char ID;
44 
45  BoundsChecking() : FunctionPass(ID) {
47  }
48 
49  bool runOnFunction(Function &F) override;
50 
51  void getAnalysisUsage(AnalysisUsage &AU) const override {
53  }
54 
55  private:
56  const TargetLibraryInfo *TLI;
57  ObjectSizeOffsetEvaluator *ObjSizeEval;
58  BuilderTy *Builder;
59  Instruction *Inst;
60  BasicBlock *TrapBB;
61 
62  BasicBlock *getTrapBB();
63  void emitBranchToTrap(Value *Cmp = nullptr);
64  bool instrument(Value *Ptr, Value *Val, const DataLayout &DL);
65  };
66 }
67 
68 char BoundsChecking::ID = 0;
69 INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking",
70  false, false)
71 
72 
73 /// getTrapBB - create a basic block that traps. All overflowing conditions
74 /// branch to this block. There's only one trap block per function.
75 BasicBlock *BoundsChecking::getTrapBB() {
76  if (TrapBB && SingleTrapBB)
77  return TrapBB;
78 
79  Function *Fn = Inst->getParent()->getParent();
80  IRBuilder<>::InsertPointGuard Guard(*Builder);
81  TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
82  Builder->SetInsertPoint(TrapBB);
83 
84  llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
85  CallInst *TrapCall = Builder->CreateCall(F, {});
86  TrapCall->setDoesNotReturn();
87  TrapCall->setDoesNotThrow();
88  TrapCall->setDebugLoc(Inst->getDebugLoc());
89  Builder->CreateUnreachable();
90 
91  return TrapBB;
92 }
93 
94 
95 /// emitBranchToTrap - emit a branch instruction to a trap block.
96 /// If Cmp is non-null, perform a jump only if its value evaluates to true.
97 void BoundsChecking::emitBranchToTrap(Value *Cmp) {
98  // check if the comparison is always false
99  ConstantInt *C = dyn_cast_or_null<ConstantInt>(Cmp);
100  if (C) {
101  ++ChecksSkipped;
102  if (!C->getZExtValue())
103  return;
104  else
105  Cmp = nullptr; // unconditional branch
106  }
107  ++ChecksAdded;
108 
109  BasicBlock::iterator Inst = Builder->GetInsertPoint();
110  BasicBlock *OldBB = Inst->getParent();
111  BasicBlock *Cont = OldBB->splitBasicBlock(Inst);
112  OldBB->getTerminator()->eraseFromParent();
113 
114  if (Cmp)
115  BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB);
116  else
117  BranchInst::Create(getTrapBB(), OldBB);
118 }
119 
120 
121 /// instrument - adds run-time bounds checks to memory accessing instructions.
122 /// Ptr is the pointer that will be read/written, and InstVal is either the
123 /// result from the load or the value being stored. It is used to determine the
124 /// size of memory block that is touched.
125 /// Returns true if any change was made to the IR, false otherwise.
126 bool BoundsChecking::instrument(Value *Ptr, Value *InstVal,
127  const DataLayout &DL) {
128  uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
129  DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
130  << " bytes\n");
131 
132  SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr);
133 
134  if (!ObjSizeEval->bothKnown(SizeOffset)) {
135  ++ChecksUnable;
136  return false;
137  }
138 
139  Value *Size = SizeOffset.first;
140  Value *Offset = SizeOffset.second;
141  ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
142 
143  Type *IntTy = DL.getIntPtrType(Ptr->getType());
144  Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
145 
146  // three checks are required to ensure safety:
147  // . Offset >= 0 (since the offset is given from the base ptr)
148  // . Size >= Offset (unsigned)
149  // . Size - Offset >= NeededSize (unsigned)
150  //
151  // optimization: if Size >= 0 (signed), skip 1st check
152  // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
153  Value *ObjSize = Builder->CreateSub(Size, Offset);
154  Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
155  Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
156  Value *Or = Builder->CreateOr(Cmp2, Cmp3);
157  if (!SizeCI || SizeCI->getValue().slt(0)) {
158  Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
159  Or = Builder->CreateOr(Cmp1, Or);
160  }
161  emitBranchToTrap(Or);
162 
163  return true;
164 }
165 
166 bool BoundsChecking::runOnFunction(Function &F) {
167  const DataLayout &DL = F.getParent()->getDataLayout();
168  TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
169 
170  TrapBB = nullptr;
171  BuilderTy TheBuilder(F.getContext(), TargetFolder(DL));
172  Builder = &TheBuilder;
173  ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(),
174  /*RoundToAlign=*/true);
175  ObjSizeEval = &TheObjSizeEval;
176 
177  // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
178  // touching instructions
179  std::vector<Instruction*> WorkList;
180  for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
181  Instruction *I = &*i;
182  if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
183  isa<AtomicRMWInst>(I))
184  WorkList.push_back(I);
185  }
186 
187  bool MadeChange = false;
188  for (Instruction *i : WorkList) {
189  Inst = i;
190 
191  Builder->SetInsertPoint(Inst);
192  if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
193  MadeChange |= instrument(LI->getPointerOperand(), LI, DL);
194  } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
195  MadeChange |=
196  instrument(SI->getPointerOperand(), SI->getValueOperand(), DL);
197  } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
198  MadeChange |=
199  instrument(AI->getPointerOperand(), AI->getCompareOperand(), DL);
200  } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
201  MadeChange |=
202  instrument(AI->getPointerOperand(), AI->getValOperand(), DL);
203  } else {
204  llvm_unreachable("unknown Instruction type");
205  }
206  }
207  return MadeChange;
208 }
209 
211  return new BoundsChecking();
212 }
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...
FunctionPass * createBoundsCheckingPass()
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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
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")
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
AnalysisUsage & addRequired()
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:132
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"))
INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking", false, false) BasicBlock *BoundsChecking
getTrapBB - create a basic block that traps.
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
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:975
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
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
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
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
IRBuilder< TargetFolder > BuilderTy
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
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
#define I(x, y, z)
Definition: MD5.cpp:58
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()
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
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:388
#define DEBUG(X)
Definition: Debug.h:118
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:133
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
void initializeBoundsCheckingPass(PassRegistry &)