2012-05-23 01:19:09 +08:00
|
|
|
//===- BoundsChecking.cpp - Instrumentation for run-time bounds checking --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a pass that instruments the code to perform run-time
|
|
|
|
// bounds checking on loads, stores, and other memory intrinsics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "bounds-checking"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/IRBuilder.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
#include "llvm/Pass.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-06-21 23:59:53 +08:00
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2012-06-01 06:58:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/InstIterator.h"
|
|
|
|
#include "llvm/Support/TargetFolder.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-06-21 23:59:53 +08:00
|
|
|
static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
|
|
|
|
cl::desc("Use one trap block per function"));
|
2012-06-01 06:58:48 +08:00
|
|
|
|
2012-05-23 01:19:09 +08:00
|
|
|
STATISTIC(ChecksAdded, "Bounds checks added");
|
|
|
|
STATISTIC(ChecksSkipped, "Bounds checks skipped");
|
|
|
|
STATISTIC(ChecksUnable, "Bounds checks unable to add");
|
|
|
|
|
|
|
|
typedef IRBuilder<true, TargetFolder> BuilderTy;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BoundsChecking : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
BoundsChecking(unsigned _Penalty = 5) : FunctionPass(ID), Penalty(_Penalty){
|
|
|
|
initializeBoundsCheckingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
}
|
2012-05-23 06:02:19 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const TargetData *TD;
|
2012-06-21 23:59:53 +08:00
|
|
|
ObjectSizeOffsetEvaluator *ObjSizeEval;
|
2012-05-23 06:02:19 +08:00
|
|
|
BuilderTy *Builder;
|
2012-06-23 08:12:34 +08:00
|
|
|
Instruction *Inst;
|
2012-05-23 06:02:19 +08:00
|
|
|
BasicBlock *TrapBB;
|
|
|
|
unsigned Penalty;
|
|
|
|
|
|
|
|
BasicBlock *getTrapBB();
|
2012-05-24 00:24:52 +08:00
|
|
|
void emitBranchToTrap(Value *Cmp = 0);
|
2012-06-01 06:45:40 +08:00
|
|
|
bool computeAllocSize(Value *Ptr, APInt &Offset, Value* &OffsetValue,
|
|
|
|
APInt &Size, Value* &SizeValue);
|
2012-05-23 06:02:19 +08:00
|
|
|
bool instrument(Value *Ptr, Value *Val);
|
2012-05-23 01:19:09 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char BoundsChecking::ID = 0;
|
2012-05-30 06:32:51 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(BoundsChecking, "bounds-checking",
|
|
|
|
"Run-time bounds checking", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
|
|
|
INITIALIZE_PASS_END(BoundsChecking, "bounds-checking",
|
|
|
|
"Run-time bounds checking", false, false)
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// getTrapBB - create a basic block that traps. All overflowing conditions
|
|
|
|
/// branch to this block. There's only one trap block per function.
|
|
|
|
BasicBlock *BoundsChecking::getTrapBB() {
|
2012-06-21 23:59:53 +08:00
|
|
|
if (TrapBB && SingleTrapBB)
|
2012-05-23 01:19:09 +08:00
|
|
|
return TrapBB;
|
|
|
|
|
2012-06-23 08:12:34 +08:00
|
|
|
Function *Fn = Inst->getParent()->getParent();
|
2012-05-23 01:19:09 +08:00
|
|
|
BasicBlock::iterator PrevInsertPoint = Builder->GetInsertPoint();
|
|
|
|
TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
|
|
|
|
Builder->SetInsertPoint(TrapBB);
|
|
|
|
|
|
|
|
llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
|
|
|
|
CallInst *TrapCall = Builder->CreateCall(F);
|
|
|
|
TrapCall->setDoesNotReturn();
|
|
|
|
TrapCall->setDoesNotThrow();
|
2012-06-23 08:12:34 +08:00
|
|
|
TrapCall->setDebugLoc(Inst->getDebugLoc());
|
2012-05-23 01:19:09 +08:00
|
|
|
Builder->CreateUnreachable();
|
|
|
|
|
|
|
|
Builder->SetInsertPoint(PrevInsertPoint);
|
|
|
|
return TrapBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-24 00:24:52 +08:00
|
|
|
/// emitBranchToTrap - emit a branch instruction to a trap block.
|
|
|
|
/// If Cmp is non-null, perform a jump only if its value evaluates to true.
|
|
|
|
void BoundsChecking::emitBranchToTrap(Value *Cmp) {
|
2012-06-21 23:59:53 +08:00
|
|
|
// check if the comparison is always false
|
|
|
|
ConstantInt *C = dyn_cast_or_null<ConstantInt>(Cmp);
|
|
|
|
if (C) {
|
|
|
|
++ChecksSkipped;
|
|
|
|
if (!C->getZExtValue())
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
Cmp = 0; // unconditional branch
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:24:52 +08:00
|
|
|
Instruction *Inst = Builder->GetInsertPoint();
|
|
|
|
BasicBlock *OldBB = Inst->getParent();
|
|
|
|
BasicBlock *Cont = OldBB->splitBasicBlock(Inst);
|
|
|
|
OldBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
if (Cmp)
|
|
|
|
BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB);
|
|
|
|
else
|
|
|
|
BranchInst::Create(getTrapBB(), OldBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-23 06:02:19 +08:00
|
|
|
/// instrument - adds run-time bounds checks to memory accessing instructions.
|
|
|
|
/// Ptr is the pointer that will be read/written, and InstVal is either the
|
|
|
|
/// result from the load or the value being stored. It is used to determine the
|
|
|
|
/// size of memory block that is touched.
|
|
|
|
/// Returns true if any change was made to the IR, false otherwise.
|
2012-05-23 01:19:09 +08:00
|
|
|
bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) {
|
|
|
|
uint64_t NeededSize = TD->getTypeStoreSize(InstVal->getType());
|
|
|
|
DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
|
|
|
|
<< " bytes\n");
|
|
|
|
|
2012-06-21 23:59:53 +08:00
|
|
|
SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr);
|
2012-06-02 01:43:31 +08:00
|
|
|
|
2012-06-21 23:59:53 +08:00
|
|
|
if (!ObjSizeEval->bothKnown(SizeOffset)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
++ChecksUnable;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:59:53 +08:00
|
|
|
Value *Size = SizeOffset.first;
|
|
|
|
Value *Offset = SizeOffset.second;
|
|
|
|
|
2012-06-23 08:12:34 +08:00
|
|
|
IntegerType *IntTy = TD->getIntPtrType(Inst->getContext());
|
2012-06-21 23:59:53 +08:00
|
|
|
Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
|
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
// three checks are required to ensure safety:
|
|
|
|
// . Offset >= 0 (since the offset is given from the base ptr)
|
|
|
|
// . Size >= Offset (unsigned)
|
|
|
|
// . Size - Offset >= NeededSize (unsigned)
|
|
|
|
// FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
|
2012-06-21 23:59:53 +08:00
|
|
|
Value *ObjSize = Builder->CreateSub(Size, Offset);
|
|
|
|
Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
|
|
|
|
Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
|
|
|
|
Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
|
|
|
|
Value *Or = Builder->CreateOr(Cmp1, Builder->CreateOr(Cmp2, Cmp3));
|
2012-05-24 00:24:52 +08:00
|
|
|
emitBranchToTrap(Or);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
++ChecksAdded;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoundsChecking::runOnFunction(Function &F) {
|
|
|
|
TD = &getAnalysis<TargetData>();
|
|
|
|
|
|
|
|
TrapBB = 0;
|
|
|
|
BuilderTy TheBuilder(F.getContext(), TargetFolder(TD));
|
|
|
|
Builder = &TheBuilder;
|
2012-06-21 23:59:53 +08:00
|
|
|
ObjectSizeOffsetEvaluator TheObjSizeEval(TD, F.getContext());
|
|
|
|
ObjSizeEval = &TheObjSizeEval;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
// check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
|
|
|
|
// touching instructions
|
|
|
|
std::vector<Instruction*> WorkList;
|
|
|
|
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
|
|
|
|
Instruction *I = &*i;
|
|
|
|
if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
|
|
|
|
isa<AtomicRMWInst>(I))
|
|
|
|
WorkList.push_back(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
2012-05-23 06:02:19 +08:00
|
|
|
for (std::vector<Instruction*>::iterator i = WorkList.begin(),
|
|
|
|
e = WorkList.end(); i != e; ++i) {
|
2012-06-23 08:12:34 +08:00
|
|
|
Inst = *i;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-06-23 08:12:34 +08:00
|
|
|
Builder->SetInsertPoint(Inst);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
MadeChange |= instrument(LI->getPointerOperand(), LI);
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
MadeChange |= instrument(SI->getPointerOperand(), SI->getValueOperand());
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
MadeChange |= instrument(AI->getPointerOperand(),AI->getCompareOperand());
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
MadeChange |= instrument(AI->getPointerOperand(), AI->getValOperand());
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("unknown Instruction type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createBoundsCheckingPass(unsigned Penalty) {
|
|
|
|
return new BoundsChecking(Penalty);
|
|
|
|
}
|