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