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 | } |
| 68 | }; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 69 | |
| 70 | } // end anonymous namespace |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 71 | |
| 72 | char BoundsChecking::ID = 0; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 73 | |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 74 | INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking", |
| 75 | false, false) |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 76 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 77 | /// Adds run-time bounds checks to memory accessing instructions. |
| 78 | /// |
| 79 | /// \p Ptr is the pointer that will be read/written, and \p InstVal is either |
| 80 | /// the result from the load or the value being stored. It is used to determine |
| 81 | /// the size of memory block that is touched. |
| 82 | /// |
| 83 | /// \p GetTrapBB is a callable that returns the trap BB to use on failure. |
| 84 | /// |
Nuno Lopes | 59e9df7 | 2012-05-22 22:02:19 +0000 | [diff] [blame] | 85 | /// Returns true if any change was made to the IR, false otherwise. |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 86 | template <typename GetTrapBBT> |
| 87 | static bool instrumentMemAccess(Value *Ptr, Value *InstVal, |
| 88 | const DataLayout &DL, TargetLibraryInfo &TLI, |
| 89 | ObjectSizeOffsetEvaluator &ObjSizeEval, |
| 90 | BuilderTy &IRB, |
| 91 | GetTrapBBT GetTrapBB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 92 | uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType()); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 93 | DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize) |
| 94 | << " bytes\n"); |
| 95 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 96 | SizeOffsetEvalType SizeOffset = ObjSizeEval.compute(Ptr); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 98 | if (!ObjSizeEval.bothKnown(SizeOffset)) { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 99 | ++ChecksUnable; |
| 100 | return false; |
| 101 | } |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 102 | |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 103 | Value *Size = SizeOffset.first; |
| 104 | Value *Offset = SizeOffset.second; |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 105 | ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 106 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 107 | Type *IntTy = DL.getIntPtrType(Ptr->getType()); |
Nuno Lopes | 0e967e0 | 2012-06-21 15:59:53 +0000 | [diff] [blame] | 108 | Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize); |
| 109 | |
Nuno Lopes | 7d00061 | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 110 | // three checks are required to ensure safety: |
| 111 | // . Offset >= 0 (since the offset is given from the base ptr) |
| 112 | // . Size >= Offset (unsigned) |
| 113 | // . Size - Offset >= NeededSize (unsigned) |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 114 | // |
| 115 | // optimization: if Size >= 0 (signed), skip 1st check |
Nuno Lopes | 7d00061 | 2012-05-31 22:45:40 +0000 | [diff] [blame] | 116 | // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 117 | Value *ObjSize = IRB.CreateSub(Size, Offset); |
| 118 | Value *Cmp2 = IRB.CreateICmpULT(Size, Offset); |
| 119 | Value *Cmp3 = IRB.CreateICmpULT(ObjSize, NeededSizeVal); |
| 120 | Value *Or = IRB.CreateOr(Cmp2, Cmp3); |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 121 | if (!SizeCI || SizeCI->getValue().slt(0)) { |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 122 | Value *Cmp1 = IRB.CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0)); |
| 123 | Or = IRB.CreateOr(Cmp1, Or); |
Nuno Lopes | 1e8dffd | 2012-07-03 17:30:18 +0000 | [diff] [blame] | 124 | } |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 125 | |
Chandler Carruth | 3f0e056 | 2017-10-18 22:42:36 +0000 | [diff] [blame] | 126 | // check if the comparison is always false |
| 127 | ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or); |
| 128 | if (C) { |
| 129 | ++ChecksSkipped; |
| 130 | // If non-zero, nothing to do. |
| 131 | if (!C->getZExtValue()) |
| 132 | return true; |
| 133 | } |
| 134 | ++ChecksAdded; |
| 135 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 136 | BasicBlock::iterator SplitI = IRB.GetInsertPoint(); |
Chandler Carruth | 3f0e056 | 2017-10-18 22:42:36 +0000 | [diff] [blame] | 137 | BasicBlock *OldBB = SplitI->getParent(); |
| 138 | BasicBlock *Cont = OldBB->splitBasicBlock(SplitI); |
| 139 | OldBB->getTerminator()->eraseFromParent(); |
| 140 | |
| 141 | if (C) { |
| 142 | // If we have a constant zero, unconditionally branch. |
| 143 | // FIXME: We should really handle this differently to bypass the splitting |
| 144 | // the block. |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 145 | BranchInst::Create(GetTrapBB(IRB), OldBB); |
Chandler Carruth | 3f0e056 | 2017-10-18 22:42:36 +0000 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | // Create the conditional branch. |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 150 | BranchInst::Create(GetTrapBB(IRB), Cont, Or, OldBB); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool BoundsChecking::runOnFunction(Function &F) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 155 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 156 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 157 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 158 | ObjectSizeOffsetEvaluator ObjSizeEval(DL, &TLI, F.getContext(), |
Nuno Lopes | 340b046 | 2013-10-24 09:17:24 +0000 | [diff] [blame] | 159 | /*RoundToAlign=*/true); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 160 | |
| 161 | // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory |
| 162 | // touching instructions |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 163 | std::vector<Instruction *> WorkList; |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 164 | for (Instruction &I : instructions(F)) { |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 165 | if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) || |
| 166 | isa<AtomicRMWInst>(I)) |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 167 | WorkList.push_back(&I); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 170 | // Create a trapping basic block on demand using a callback. Depending on |
| 171 | // flags, this will either create a single block for the entire function or |
| 172 | // will create a fresh block every time it is called. |
| 173 | BasicBlock *TrapBB = nullptr; |
| 174 | auto GetTrapBB = [&TrapBB](BuilderTy &IRB) { |
| 175 | if (TrapBB && SingleTrapBB) |
| 176 | return TrapBB; |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 177 | |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 178 | Function *Fn = IRB.GetInsertBlock()->getParent(); |
| 179 | // FIXME: This debug location doesn't make a lot of sense in the |
| 180 | // `SingleTrapBB` case. |
| 181 | auto DebugLoc = IRB.getCurrentDebugLocation(); |
| 182 | IRBuilder<>::InsertPointGuard Guard(IRB); |
| 183 | TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn); |
| 184 | IRB.SetInsertPoint(TrapBB); |
| 185 | |
| 186 | auto *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap); |
| 187 | CallInst *TrapCall = IRB.CreateCall(F, {}); |
| 188 | TrapCall->setDoesNotReturn(); |
| 189 | TrapCall->setDoesNotThrow(); |
| 190 | TrapCall->setDebugLoc(DebugLoc); |
| 191 | IRB.CreateUnreachable(); |
| 192 | |
| 193 | return TrapBB; |
| 194 | }; |
| 195 | |
| 196 | bool MadeChange = false; |
| 197 | for (Instruction *Inst : WorkList) { |
| 198 | BuilderTy IRB(Inst->getParent(), BasicBlock::iterator(Inst), TargetFolder(DL)); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 199 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 200 | MadeChange |= instrumentMemAccess(LI->getPointerOperand(), LI, DL, TLI, |
| 201 | ObjSizeEval, IRB, GetTrapBB); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 202 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 203 | MadeChange |= |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 204 | instrumentMemAccess(SI->getPointerOperand(), SI->getValueOperand(), |
| 205 | DL, TLI, ObjSizeEval, IRB, GetTrapBB); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 206 | } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 207 | MadeChange |= |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 208 | instrumentMemAccess(AI->getPointerOperand(), AI->getCompareOperand(), |
| 209 | DL, TLI, ObjSizeEval, IRB, GetTrapBB); |
Nuno Lopes | de8c6fb | 2012-06-23 00:12:34 +0000 | [diff] [blame] | 210 | } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 211 | MadeChange |= |
Chandler Carruth | 1594fee | 2017-11-14 01:13:59 +0000 | [diff] [blame^] | 212 | instrumentMemAccess(AI->getPointerOperand(), AI->getValOperand(), DL, |
| 213 | TLI, ObjSizeEval, IRB, GetTrapBB); |
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 | } |