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