blob: 9a5cea81f322a288e27bc9c9d97e1733931df8b3 [file] [log] [blame]
Nuno Lopes5c525b52012-05-22 17:19:09 +00001//===- 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 Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Transforms/Instrumentation.h"
16#include "llvm/ADT/Statistic.h"
17#include "llvm/Analysis/MemoryBuiltins.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "llvm/Analysis/TargetFolder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/IRBuilder.h"
Stephen Hines36b56882014-04-23 16:57:46 -070021#include "llvm/IR/InstIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/Intrinsics.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000023#include "llvm/Pass.h"
Nuno Lopes1cbf2be2012-05-31 22:58:48 +000024#include "llvm/Support/CommandLine.h"
Nuno Lopes5c525b52012-05-22 17:19:09 +000025#include "llvm/Support/Debug.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000027#include "llvm/Target/TargetLibraryInfo.h"
Nuno Lopes5c525b52012-05-22 17:19:09 +000028using namespace llvm;
29
Stephen Hinesdce4a402014-05-29 02:49:00 -070030#define DEBUG_TYPE "bounds-checking"
31
Nuno Lopes7f584722012-06-21 15:59:53 +000032static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
33 cl::desc("Use one trap block per function"));
Nuno Lopes1cbf2be2012-05-31 22:58:48 +000034
Nuno Lopes5c525b52012-05-22 17:19:09 +000035STATISTIC(ChecksAdded, "Bounds checks added");
36STATISTIC(ChecksSkipped, "Bounds checks skipped");
37STATISTIC(ChecksUnable, "Bounds checks unable to add");
38
39typedef IRBuilder<true, TargetFolder> BuilderTy;
40
41namespace {
Nuno Lopes5c525b52012-05-22 17:19:09 +000042 struct BoundsChecking : public FunctionPass {
Nuno Lopes5c525b52012-05-22 17:19:09 +000043 static char ID;
44
Joey Goulyf284aef2012-11-23 10:47:35 +000045 BoundsChecking() : FunctionPass(ID) {
Nuno Lopes5c525b52012-05-22 17:19:09 +000046 initializeBoundsCheckingPass(*PassRegistry::getPassRegistry());
47 }
48
Stephen Hines36b56882014-04-23 16:57:46 -070049 bool runOnFunction(Function &F) override;
Nuno Lopes5c525b52012-05-22 17:19:09 +000050
Stephen Hines36b56882014-04-23 16:57:46 -070051 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.addRequired<DataLayoutPass>();
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000053 AU.addRequired<TargetLibraryInfo>();
Nuno Lopes5c525b52012-05-22 17:19:09 +000054 }
Nuno Lopes2f0a7482012-05-22 22:02:19 +000055
56 private:
Stephen Hines36b56882014-04-23 16:57:46 -070057 const DataLayout *DL;
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000058 const TargetLibraryInfo *TLI;
Nuno Lopes7f584722012-06-21 15:59:53 +000059 ObjectSizeOffsetEvaluator *ObjSizeEval;
Nuno Lopes2f0a7482012-05-22 22:02:19 +000060 BuilderTy *Builder;
Nuno Lopes512be1f2012-06-23 00:12:34 +000061 Instruction *Inst;
Nuno Lopes2f0a7482012-05-22 22:02:19 +000062 BasicBlock *TrapBB;
Nuno Lopes2f0a7482012-05-22 22:02:19 +000063
64 BasicBlock *getTrapBB();
Stephen Hinesdce4a402014-05-29 02:49:00 -070065 void emitBranchToTrap(Value *Cmp = nullptr);
Nuno Lopes2f0a7482012-05-22 22:02:19 +000066 bool instrument(Value *Ptr, Value *Val);
Nuno Lopes5c525b52012-05-22 17:19:09 +000067 };
68}
69
70char BoundsChecking::ID = 0;
Nuno Lopes42d80c72012-07-03 17:30:18 +000071INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking",
72 false, false)
Nuno Lopes5c525b52012-05-22 17:19:09 +000073
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.
77BasicBlock *BoundsChecking::getTrapBB() {
Nuno Lopes7f584722012-06-21 15:59:53 +000078 if (TrapBB && SingleTrapBB)
Nuno Lopes5c525b52012-05-22 17:19:09 +000079 return TrapBB;
80
Nuno Lopes512be1f2012-06-23 00:12:34 +000081 Function *Fn = Inst->getParent()->getParent();
Benjamin Kramerd4278822013-09-30 15:40:17 +000082 IRBuilder<>::InsertPointGuard Guard(*Builder);
Nuno Lopes5c525b52012-05-22 17:19:09 +000083 TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
Benjamin Kramerb313a932013-09-30 15:52:50 +000084 Builder->SetInsertPoint(TrapBB);
Nuno Lopes5c525b52012-05-22 17:19:09 +000085
86 llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
87 CallInst *TrapCall = Builder->CreateCall(F);
88 TrapCall->setDoesNotReturn();
89 TrapCall->setDoesNotThrow();
Nuno Lopes512be1f2012-06-23 00:12:34 +000090 TrapCall->setDebugLoc(Inst->getDebugLoc());
Nuno Lopes5c525b52012-05-22 17:19:09 +000091 Builder->CreateUnreachable();
92
Nuno Lopes5c525b52012-05-22 17:19:09 +000093 return TrapBB;
94}
95
96
Nuno Lopes2b526302012-05-23 16:24:52 +000097/// 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.
99void BoundsChecking::emitBranchToTrap(Value *Cmp) {
Nuno Lopes7f584722012-06-21 15:59:53 +0000100 // 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 Hinesdce4a402014-05-29 02:49:00 -0700107 Cmp = nullptr; // unconditional branch
Nuno Lopes7f584722012-06-21 15:59:53 +0000108 }
Nuno Lopes53916aa2012-12-03 10:15:03 +0000109 ++ChecksAdded;
Nuno Lopes7f584722012-06-21 15:59:53 +0000110
Nuno Lopes2b526302012-05-23 16:24:52 +0000111 Instruction *Inst = Builder->GetInsertPoint();
112 BasicBlock *OldBB = Inst->getParent();
113 BasicBlock *Cont = OldBB->splitBasicBlock(Inst);
114 OldBB->getTerminator()->eraseFromParent();
115
Nuno Lopes2b526302012-05-23 16:24:52 +0000116 if (Cmp)
117 BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB);
118 else
119 BranchInst::Create(getTrapBB(), OldBB);
120}
121
122
Nuno Lopes2f0a7482012-05-22 22:02:19 +0000123/// 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 Lopes5c525b52012-05-22 17:19:09 +0000128bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) {
Stephen Hines36b56882014-04-23 16:57:46 -0700129 uint64_t NeededSize = DL->getTypeStoreSize(InstVal->getType());
Nuno Lopes5c525b52012-05-22 17:19:09 +0000130 DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
131 << " bytes\n");
132
Nuno Lopes7f584722012-06-21 15:59:53 +0000133 SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr);
Nuno Lopes5c525b52012-05-22 17:19:09 +0000134
Nuno Lopes7f584722012-06-21 15:59:53 +0000135 if (!ObjSizeEval->bothKnown(SizeOffset)) {
Nuno Lopes5c525b52012-05-22 17:19:09 +0000136 ++ChecksUnable;
137 return false;
138 }
Nuno Lopes5c525b52012-05-22 17:19:09 +0000139
Nuno Lopes7f584722012-06-21 15:59:53 +0000140 Value *Size = SizeOffset.first;
141 Value *Offset = SizeOffset.second;
Nuno Lopes42d80c72012-07-03 17:30:18 +0000142 ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
Nuno Lopes7f584722012-06-21 15:59:53 +0000143
Stephen Hines36b56882014-04-23 16:57:46 -0700144 Type *IntTy = DL->getIntPtrType(Ptr->getType());
Nuno Lopes7f584722012-06-21 15:59:53 +0000145 Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
146
Nuno Lopes0463cce2012-05-31 22:45:40 +0000147 // 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 Lopes42d80c72012-07-03 17:30:18 +0000151 //
152 // optimization: if Size >= 0 (signed), skip 1st check
Nuno Lopes0463cce2012-05-31 22:45:40 +0000153 // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
Nuno Lopes7f584722012-06-21 15:59:53 +0000154 Value *ObjSize = Builder->CreateSub(Size, Offset);
Nuno Lopes7f584722012-06-21 15:59:53 +0000155 Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
156 Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
Nuno Lopes42d80c72012-07-03 17:30:18 +0000157 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 Lopes2b526302012-05-23 16:24:52 +0000162 emitBranchToTrap(Or);
Nuno Lopes5c525b52012-05-22 17:19:09 +0000163
Nuno Lopes5c525b52012-05-22 17:19:09 +0000164 return true;
165}
166
167bool BoundsChecking::runOnFunction(Function &F) {
Stephen Hines36b56882014-04-23 16:57:46 -0700168 DL = &getAnalysis<DataLayoutPass>().getDataLayout();
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000169 TLI = &getAnalysis<TargetLibraryInfo>();
Nuno Lopes5c525b52012-05-22 17:19:09 +0000170
Stephen Hinesdce4a402014-05-29 02:49:00 -0700171 TrapBB = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -0700172 BuilderTy TheBuilder(F.getContext(), TargetFolder(DL));
Nuno Lopes5c525b52012-05-22 17:19:09 +0000173 Builder = &TheBuilder;
Stephen Hines36b56882014-04-23 16:57:46 -0700174 ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(),
Nuno Lopes5e1d0d32013-10-24 09:17:24 +0000175 /*RoundToAlign=*/true);
Nuno Lopes7f584722012-06-21 15:59:53 +0000176 ObjSizeEval = &TheObjSizeEval;
Nuno Lopes5c525b52012-05-22 17:19:09 +0000177
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 Lopes2f0a7482012-05-22 22:02:19 +0000189 for (std::vector<Instruction*>::iterator i = WorkList.begin(),
190 e = WorkList.end(); i != e; ++i) {
Nuno Lopes512be1f2012-06-23 00:12:34 +0000191 Inst = *i;
Nuno Lopes5c525b52012-05-22 17:19:09 +0000192
Nuno Lopes512be1f2012-06-23 00:12:34 +0000193 Builder->SetInsertPoint(Inst);
194 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Nuno Lopes5c525b52012-05-22 17:19:09 +0000195 MadeChange |= instrument(LI->getPointerOperand(), LI);
Nuno Lopes512be1f2012-06-23 00:12:34 +0000196 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Nuno Lopes5c525b52012-05-22 17:19:09 +0000197 MadeChange |= instrument(SI->getPointerOperand(), SI->getValueOperand());
Nuno Lopes512be1f2012-06-23 00:12:34 +0000198 } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
Nuno Lopes5c525b52012-05-22 17:19:09 +0000199 MadeChange |= instrument(AI->getPointerOperand(),AI->getCompareOperand());
Nuno Lopes512be1f2012-06-23 00:12:34 +0000200 } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
Nuno Lopes5c525b52012-05-22 17:19:09 +0000201 MadeChange |= instrument(AI->getPointerOperand(), AI->getValOperand());
202 } else {
203 llvm_unreachable("unknown Instruction type");
204 }
205 }
206 return MadeChange;
207}
208
Joey Goulyf284aef2012-11-23 10:47:35 +0000209FunctionPass *llvm::createBoundsCheckingPass() {
210 return new BoundsChecking();
Nuno Lopes5c525b52012-05-22 17:19:09 +0000211}