Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 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 | #define DEBUG_TYPE "bounds-checking" |
| 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/MemoryBuiltins.h" |
Nuno Lopes | 1cbf2be | 2012-05-31 22:58:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/InstIterator.h" |
| 22 | #include "llvm/Support/IRBuilder.h" |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetFolder.h" |
| 25 | #include "llvm/Target/TargetData.h" |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
| 28 | using namespace llvm; |
| 29 | |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 30 | static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap", |
| 31 | cl::desc("Use one trap block per function")); |
Nuno Lopes | 1cbf2be | 2012-05-31 22:58:48 +0000 | [diff] [blame] | 32 | |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 33 | STATISTIC(ChecksAdded, "Bounds checks added"); |
| 34 | STATISTIC(ChecksSkipped, "Bounds checks skipped"); |
| 35 | STATISTIC(ChecksUnable, "Bounds checks unable to add"); |
| 36 | |
| 37 | typedef IRBuilder<true, TargetFolder> BuilderTy; |
| 38 | |
| 39 | namespace { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 40 | struct BoundsChecking : public FunctionPass { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 41 | static char ID; |
| 42 | |
| 43 | BoundsChecking(unsigned _Penalty = 5) : FunctionPass(ID), Penalty(_Penalty){ |
| 44 | initializeBoundsCheckingPass(*PassRegistry::getPassRegistry()); |
| 45 | } |
| 46 | |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 47 | virtual bool runOnFunction(Function &F); |
| 48 | |
| 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 50 | AU.addRequired<TargetData>(); |
| 51 | } |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | const TargetData *TD; |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 55 | ObjectSizeOffsetEvaluator *ObjSizeEval; |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 56 | BuilderTy *Builder; |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 57 | Instruction *Inst; |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 58 | BasicBlock *TrapBB; |
| 59 | unsigned Penalty; |
| 60 | |
| 61 | BasicBlock *getTrapBB(); |
Nuno Lopes | 2b52630 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 62 | void emitBranchToTrap(Value *Cmp = 0); |
Nuno Lopes | 0463cce | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 63 | bool computeAllocSize(Value *Ptr, APInt &Offset, Value* &OffsetValue, |
| 64 | APInt &Size, Value* &SizeValue); |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 65 | bool instrument(Value *Ptr, Value *Val); |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 66 | }; |
| 67 | } |
| 68 | |
| 69 | char BoundsChecking::ID = 0; |
Nuno Lopes | 988a089 | 2012-05-29 22:32:51 +0000 | [diff] [blame] | 70 | INITIALIZE_PASS_BEGIN(BoundsChecking, "bounds-checking", |
| 71 | "Run-time bounds checking", false, false) |
| 72 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 73 | INITIALIZE_PASS_END(BoundsChecking, "bounds-checking", |
| 74 | "Run-time bounds checking", false, false) |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | /// getTrapBB - create a basic block that traps. All overflowing conditions |
| 78 | /// branch to this block. There's only one trap block per function. |
| 79 | BasicBlock *BoundsChecking::getTrapBB() { |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 80 | if (TrapBB && SingleTrapBB) |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 81 | return TrapBB; |
| 82 | |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 83 | Function *Fn = Inst->getParent()->getParent(); |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 84 | BasicBlock::iterator PrevInsertPoint = Builder->GetInsertPoint(); |
| 85 | TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn); |
| 86 | Builder->SetInsertPoint(TrapBB); |
| 87 | |
| 88 | llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap); |
| 89 | CallInst *TrapCall = Builder->CreateCall(F); |
| 90 | TrapCall->setDoesNotReturn(); |
| 91 | TrapCall->setDoesNotThrow(); |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 92 | TrapCall->setDebugLoc(Inst->getDebugLoc()); |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 93 | Builder->CreateUnreachable(); |
| 94 | |
| 95 | Builder->SetInsertPoint(PrevInsertPoint); |
| 96 | return TrapBB; |
| 97 | } |
| 98 | |
| 99 | |
Nuno Lopes | 2b52630 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 100 | /// emitBranchToTrap - emit a branch instruction to a trap block. |
| 101 | /// If Cmp is non-null, perform a jump only if its value evaluates to true. |
| 102 | void BoundsChecking::emitBranchToTrap(Value *Cmp) { |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 103 | // check if the comparison is always false |
| 104 | ConstantInt *C = dyn_cast_or_null<ConstantInt>(Cmp); |
| 105 | if (C) { |
| 106 | ++ChecksSkipped; |
| 107 | if (!C->getZExtValue()) |
| 108 | return; |
| 109 | else |
| 110 | Cmp = 0; // unconditional branch |
| 111 | } |
| 112 | |
Nuno Lopes | 2b52630 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 113 | Instruction *Inst = Builder->GetInsertPoint(); |
| 114 | BasicBlock *OldBB = Inst->getParent(); |
| 115 | BasicBlock *Cont = OldBB->splitBasicBlock(Inst); |
| 116 | OldBB->getTerminator()->eraseFromParent(); |
| 117 | |
Nuno Lopes | 2b52630 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 118 | if (Cmp) |
| 119 | BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB); |
| 120 | else |
| 121 | BranchInst::Create(getTrapBB(), OldBB); |
| 122 | } |
| 123 | |
| 124 | |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 125 | /// instrument - adds run-time bounds checks to memory accessing instructions. |
| 126 | /// Ptr is the pointer that will be read/written, and InstVal is either the |
| 127 | /// result from the load or the value being stored. It is used to determine the |
| 128 | /// size of memory block that is touched. |
| 129 | /// Returns true if any change was made to the IR, false otherwise. |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 130 | bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) { |
| 131 | uint64_t NeededSize = TD->getTypeStoreSize(InstVal->getType()); |
| 132 | DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize) |
| 133 | << " bytes\n"); |
| 134 | |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 135 | SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr); |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 136 | |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 137 | if (!ObjSizeEval->bothKnown(SizeOffset)) { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 138 | ++ChecksUnable; |
| 139 | return false; |
| 140 | } |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 141 | |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 142 | Value *Size = SizeOffset.first; |
| 143 | Value *Offset = SizeOffset.second; |
| 144 | |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 145 | IntegerType *IntTy = TD->getIntPtrType(Inst->getContext()); |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 146 | Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize); |
| 147 | |
Nuno Lopes | 0463cce | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 148 | // three checks are required to ensure safety: |
| 149 | // . Offset >= 0 (since the offset is given from the base ptr) |
| 150 | // . Size >= Offset (unsigned) |
| 151 | // . Size - Offset >= NeededSize (unsigned) |
Nuno Lopes | 0463cce | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 152 | // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 153 | Value *ObjSize = Builder->CreateSub(Size, Offset); |
| 154 | Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0)); |
| 155 | Value *Cmp2 = Builder->CreateICmpULT(Size, Offset); |
| 156 | Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal); |
| 157 | Value *Or = Builder->CreateOr(Cmp1, Builder->CreateOr(Cmp2, Cmp3)); |
Nuno Lopes | 2b52630 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 158 | emitBranchToTrap(Or); |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 159 | |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 160 | ++ChecksAdded; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool BoundsChecking::runOnFunction(Function &F) { |
| 165 | TD = &getAnalysis<TargetData>(); |
| 166 | |
| 167 | TrapBB = 0; |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 168 | BuilderTy TheBuilder(F.getContext(), TargetFolder(TD)); |
| 169 | Builder = &TheBuilder; |
Nuno Lopes | 7f58472 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 170 | ObjectSizeOffsetEvaluator TheObjSizeEval(TD, F.getContext()); |
| 171 | ObjSizeEval = &TheObjSizeEval; |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 172 | |
| 173 | // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory |
| 174 | // touching instructions |
| 175 | std::vector<Instruction*> WorkList; |
| 176 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { |
| 177 | Instruction *I = &*i; |
| 178 | if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) || |
| 179 | isa<AtomicRMWInst>(I)) |
| 180 | WorkList.push_back(I); |
| 181 | } |
| 182 | |
| 183 | bool MadeChange = false; |
Nuno Lopes | 2f0a748 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 184 | for (std::vector<Instruction*>::iterator i = WorkList.begin(), |
| 185 | e = WorkList.end(); i != e; ++i) { |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 186 | Inst = *i; |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 187 | |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 188 | Builder->SetInsertPoint(Inst); |
| 189 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 190 | MadeChange |= instrument(LI->getPointerOperand(), LI); |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 191 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 192 | MadeChange |= instrument(SI->getPointerOperand(), SI->getValueOperand()); |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 193 | } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 194 | MadeChange |= instrument(AI->getPointerOperand(),AI->getCompareOperand()); |
Nuno Lopes | 512be1f | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 195 | } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) { |
Nuno Lopes | 5c525b5 | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 196 | MadeChange |= instrument(AI->getPointerOperand(), AI->getValOperand()); |
| 197 | } else { |
| 198 | llvm_unreachable("unknown Instruction type"); |
| 199 | } |
| 200 | } |
| 201 | return MadeChange; |
| 202 | } |
| 203 | |
| 204 | FunctionPass *llvm::createBoundsCheckingPass(unsigned Penalty) { |
| 205 | return new BoundsChecking(Penalty); |
| 206 | } |