LLVM API Documentation

BoundsChecking.cpp
Go to the documentation of this file.
00001 //===- BoundsChecking.cpp - Instrumentation for run-time bounds checking --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a pass that instruments the code to perform run-time
00011 // bounds checking on loads, stores, and other memory intrinsics.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "bounds-checking"
00016 #include "llvm/Transforms/Instrumentation.h"
00017 #include "llvm/ADT/Statistic.h"
00018 #include "llvm/Analysis/MemoryBuiltins.h"
00019 #include "llvm/Analysis/TargetFolder.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/IRBuilder.h"
00022 #include "llvm/IR/InstIterator.h"
00023 #include "llvm/IR/Intrinsics.h"
00024 #include "llvm/Pass.h"
00025 #include "llvm/Support/CommandLine.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 #include "llvm/Target/TargetLibraryInfo.h"
00029 using namespace llvm;
00030 
00031 static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
00032                                   cl::desc("Use one trap block per function"));
00033 
00034 STATISTIC(ChecksAdded, "Bounds checks added");
00035 STATISTIC(ChecksSkipped, "Bounds checks skipped");
00036 STATISTIC(ChecksUnable, "Bounds checks unable to add");
00037 
00038 typedef IRBuilder<true, TargetFolder> BuilderTy;
00039 
00040 namespace {
00041   struct BoundsChecking : public FunctionPass {
00042     static char ID;
00043 
00044     BoundsChecking() : FunctionPass(ID) {
00045       initializeBoundsCheckingPass(*PassRegistry::getPassRegistry());
00046     }
00047 
00048     bool runOnFunction(Function &F) override;
00049 
00050     void getAnalysisUsage(AnalysisUsage &AU) const override {
00051       AU.addRequired<DataLayoutPass>();
00052       AU.addRequired<TargetLibraryInfo>();
00053     }
00054 
00055   private:
00056     const DataLayout *DL;
00057     const TargetLibraryInfo *TLI;
00058     ObjectSizeOffsetEvaluator *ObjSizeEval;
00059     BuilderTy *Builder;
00060     Instruction *Inst;
00061     BasicBlock *TrapBB;
00062 
00063     BasicBlock *getTrapBB();
00064     void emitBranchToTrap(Value *Cmp = 0);
00065     bool instrument(Value *Ptr, Value *Val);
00066  };
00067 }
00068 
00069 char BoundsChecking::ID = 0;
00070 INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking",
00071                 false, false)
00072 
00073 
00074 /// getTrapBB - create a basic block that traps. All overflowing conditions
00075 /// branch to this block. There's only one trap block per function.
00076 BasicBlock *BoundsChecking::getTrapBB() {
00077   if (TrapBB && SingleTrapBB)
00078     return TrapBB;
00079 
00080   Function *Fn = Inst->getParent()->getParent();
00081   IRBuilder<>::InsertPointGuard Guard(*Builder);
00082   TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
00083   Builder->SetInsertPoint(TrapBB);
00084 
00085   llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
00086   CallInst *TrapCall = Builder->CreateCall(F);
00087   TrapCall->setDoesNotReturn();
00088   TrapCall->setDoesNotThrow();
00089   TrapCall->setDebugLoc(Inst->getDebugLoc());
00090   Builder->CreateUnreachable();
00091 
00092   return TrapBB;
00093 }
00094 
00095 
00096 /// emitBranchToTrap - emit a branch instruction to a trap block.
00097 /// If Cmp is non-null, perform a jump only if its value evaluates to true.
00098 void BoundsChecking::emitBranchToTrap(Value *Cmp) {
00099   // check if the comparison is always false
00100   ConstantInt *C = dyn_cast_or_null<ConstantInt>(Cmp);
00101   if (C) {
00102     ++ChecksSkipped;
00103     if (!C->getZExtValue())
00104       return;
00105     else
00106       Cmp = 0; // unconditional branch
00107   }
00108   ++ChecksAdded;
00109 
00110   Instruction *Inst = Builder->GetInsertPoint();
00111   BasicBlock *OldBB = Inst->getParent();
00112   BasicBlock *Cont = OldBB->splitBasicBlock(Inst);
00113   OldBB->getTerminator()->eraseFromParent();
00114 
00115   if (Cmp)
00116     BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB);
00117   else
00118     BranchInst::Create(getTrapBB(), OldBB);
00119 }
00120 
00121 
00122 /// instrument - adds run-time bounds checks to memory accessing instructions.
00123 /// Ptr is the pointer that will be read/written, and InstVal is either the
00124 /// result from the load or the value being stored. It is used to determine the
00125 /// size of memory block that is touched.
00126 /// Returns true if any change was made to the IR, false otherwise.
00127 bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) {
00128   uint64_t NeededSize = DL->getTypeStoreSize(InstVal->getType());
00129   DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
00130               << " bytes\n");
00131 
00132   SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr);
00133 
00134   if (!ObjSizeEval->bothKnown(SizeOffset)) {
00135     ++ChecksUnable;
00136     return false;
00137   }
00138 
00139   Value *Size   = SizeOffset.first;
00140   Value *Offset = SizeOffset.second;
00141   ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
00142 
00143   Type *IntTy = DL->getIntPtrType(Ptr->getType());
00144   Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
00145 
00146   // three checks are required to ensure safety:
00147   // . Offset >= 0  (since the offset is given from the base ptr)
00148   // . Size >= Offset  (unsigned)
00149   // . Size - Offset >= NeededSize  (unsigned)
00150   //
00151   // optimization: if Size >= 0 (signed), skip 1st check
00152   // FIXME: add NSW/NUW here?  -- we dont care if the subtraction overflows
00153   Value *ObjSize = Builder->CreateSub(Size, Offset);
00154   Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
00155   Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
00156   Value *Or = Builder->CreateOr(Cmp2, Cmp3);
00157   if (!SizeCI || SizeCI->getValue().slt(0)) {
00158     Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
00159     Or = Builder->CreateOr(Cmp1, Or);
00160   }
00161   emitBranchToTrap(Or);
00162 
00163   return true;
00164 }
00165 
00166 bool BoundsChecking::runOnFunction(Function &F) {
00167   DL = &getAnalysis<DataLayoutPass>().getDataLayout();
00168   TLI = &getAnalysis<TargetLibraryInfo>();
00169 
00170   TrapBB = 0;
00171   BuilderTy TheBuilder(F.getContext(), TargetFolder(DL));
00172   Builder = &TheBuilder;
00173   ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(),
00174                                            /*RoundToAlign=*/true);
00175   ObjSizeEval = &TheObjSizeEval;
00176 
00177   // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
00178   // touching instructions
00179   std::vector<Instruction*> WorkList;
00180   for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
00181     Instruction *I = &*i;
00182     if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
00183         isa<AtomicRMWInst>(I))
00184         WorkList.push_back(I);
00185   }
00186 
00187   bool MadeChange = false;
00188   for (std::vector<Instruction*>::iterator i = WorkList.begin(),
00189        e = WorkList.end(); i != e; ++i) {
00190     Inst = *i;
00191 
00192     Builder->SetInsertPoint(Inst);
00193     if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
00194       MadeChange |= instrument(LI->getPointerOperand(), LI);
00195     } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
00196       MadeChange |= instrument(SI->getPointerOperand(), SI->getValueOperand());
00197     } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
00198       MadeChange |= instrument(AI->getPointerOperand(),AI->getCompareOperand());
00199     } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
00200       MadeChange |= instrument(AI->getPointerOperand(), AI->getValOperand());
00201     } else {
00202       llvm_unreachable("unknown Instruction type");
00203     }
00204   }
00205   return MadeChange;
00206 }
00207 
00208 FunctionPass *llvm::createBoundsCheckingPass() {
00209   return new BoundsChecking();
00210 }