Nuno Lopes | a2f6cec | 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 | ed0881b | 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" |
Chandler Carruth | 452a007 | 2014-03-04 11:59:06 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetFolder.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 22 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Nuno Lopes | 288e86ff6 | 2012-05-31 22:58:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "bounds-checking" |
| 31 | |
Nuno Lopes | 0e967e0 | 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 | 288e86ff6 | 2012-05-31 22:58:48 +0000 | [diff] [blame] | 34 | |
Nuno Lopes | a2f6cec | 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 | |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 39 | typedef IRBuilder<TargetFolder> BuilderTy; |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 42 | struct BoundsChecking : public FunctionPass { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 43 | static char ID; |
| 44 | |
Joey Gouly | 9519823 | 2012-11-23 10:47:35 +0000 | [diff] [blame] | 45 | BoundsChecking() : FunctionPass(ID) { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 46 | initializeBoundsCheckingPass(*PassRegistry::getPassRegistry()); |
| 47 | } |
| 48 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F) override; |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 50 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 51 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 52 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 53 | } |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 56 | const TargetLibraryInfo *TLI; |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 57 | ObjectSizeOffsetEvaluator *ObjSizeEval; |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 58 | BuilderTy *Builder; |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 59 | Instruction *Inst; |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 60 | BasicBlock *TrapBB; |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 61 | |
| 62 | BasicBlock *getTrapBB(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 63 | void emitBranchToTrap(Value *Cmp = nullptr); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 64 | bool instrument(Value *Ptr, Value *Val, const DataLayout &DL); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 65 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 66 | } |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 67 | |
| 68 | char BoundsChecking::ID = 0; |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 69 | INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking", |
| 70 | false, false) |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 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() { |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 76 | if (TrapBB && SingleTrapBB) |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 77 | return TrapBB; |
| 78 | |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 79 | Function *Fn = Inst->getParent()->getParent(); |
Benjamin Kramer | 6e93152 | 2013-09-30 15:40:17 +0000 | [diff] [blame] | 80 | IRBuilder<>::InsertPointGuard Guard(*Builder); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 81 | TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn); |
Benjamin Kramer | f004729 | 2013-09-30 15:52:50 +0000 | [diff] [blame] | 82 | Builder->SetInsertPoint(TrapBB); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 83 | |
| 84 | llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 85 | CallInst *TrapCall = Builder->CreateCall(F, {}); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 86 | TrapCall->setDoesNotReturn(); |
| 87 | TrapCall->setDoesNotThrow(); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 88 | TrapCall->setDebugLoc(Inst->getDebugLoc()); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 89 | Builder->CreateUnreachable(); |
| 90 | |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 91 | return TrapBB; |
| 92 | } |
| 93 | |
| 94 | |
Nuno Lopes | 10287d8 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 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) { |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 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 |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 105 | Cmp = nullptr; // unconditional branch |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 106 | } |
Nuno Lopes | 5eec267 | 2012-12-03 10:15:03 +0000 | [diff] [blame] | 107 | ++ChecksAdded; |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 108 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 109 | BasicBlock::iterator Inst = Builder->GetInsertPoint(); |
Nuno Lopes | 10287d8 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 110 | BasicBlock *OldBB = Inst->getParent(); |
| 111 | BasicBlock *Cont = OldBB->splitBasicBlock(Inst); |
| 112 | OldBB->getTerminator()->eraseFromParent(); |
| 113 | |
Nuno Lopes | 10287d8 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 114 | if (Cmp) |
| 115 | BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB); |
| 116 | else |
| 117 | BranchInst::Create(getTrapBB(), OldBB); |
| 118 | } |
| 119 | |
| 120 | |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 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. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 126 | bool BoundsChecking::instrument(Value *Ptr, Value *InstVal, |
| 127 | const DataLayout &DL) { |
| 128 | uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType()); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 129 | DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize) |
| 130 | << " bytes\n"); |
| 131 | |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 132 | SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 133 | |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 134 | if (!ObjSizeEval->bothKnown(SizeOffset)) { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 135 | ++ChecksUnable; |
| 136 | return false; |
| 137 | } |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 138 | |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 139 | Value *Size = SizeOffset.first; |
| 140 | Value *Offset = SizeOffset.second; |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 141 | ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 142 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 143 | Type *IntTy = DL.getIntPtrType(Ptr->getType()); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 144 | Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize); |
| 145 | |
Nuno Lopes | 7d00061 | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 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) |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 150 | // |
| 151 | // optimization: if Size >= 0 (signed), skip 1st check |
Nuno Lopes | 7d00061 | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 152 | // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 153 | Value *ObjSize = Builder->CreateSub(Size, Offset); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 154 | Value *Cmp2 = Builder->CreateICmpULT(Size, Offset); |
| 155 | Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal); |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 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 | } |
Nuno Lopes | 10287d8 | 2012-05-23 16:24:52 +0000 | [diff] [blame] | 161 | emitBranchToTrap(Or); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 162 | |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool BoundsChecking::runOnFunction(Function &F) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 167 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 168 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 169 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 170 | TrapBB = nullptr; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 171 | BuilderTy TheBuilder(F.getContext(), TargetFolder(DL)); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 172 | Builder = &TheBuilder; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 173 | ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(), |
Nuno Lopes | 340b046 | 2013-10-24 09:17:24 +0000 | [diff] [blame] | 174 | /*RoundToAlign=*/true); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 175 | ObjSizeEval = &TheObjSizeEval; |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 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; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 188 | for (Instruction *i : WorkList) { |
| 189 | Inst = i; |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 190 | |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 191 | Builder->SetInsertPoint(Inst); |
| 192 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 193 | MadeChange |= instrument(LI->getPointerOperand(), LI, DL); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 194 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 195 | MadeChange |= |
| 196 | instrument(SI->getPointerOperand(), SI->getValueOperand(), DL); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 197 | } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 198 | MadeChange |= |
| 199 | instrument(AI->getPointerOperand(), AI->getCompareOperand(), DL); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 200 | } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 201 | MadeChange |= |
| 202 | instrument(AI->getPointerOperand(), AI->getValOperand(), DL); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 203 | } else { |
| 204 | llvm_unreachable("unknown Instruction type"); |
| 205 | } |
| 206 | } |
| 207 | return MadeChange; |
| 208 | } |
| 209 | |
Joey Gouly | 9519823 | 2012-11-23 10:47:35 +0000 | [diff] [blame] | 210 | FunctionPass *llvm::createBoundsCheckingPass() { |
| 211 | return new BoundsChecking(); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 212 | } |