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-01 06:45:40 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-05-30 06:32:51 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.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/IRBuilder.h"
|
2012-05-23 06:02:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/Support/TargetFolder.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2012-05-26 00:54:04 +08:00
|
|
|
#include "llvm/Metadata.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
#include "llvm/Operator.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-06-01 06:58:48 +08:00
|
|
|
static cl::opt<bool> ManyTrapBB("bounds-checking-multiple-traps",
|
|
|
|
cl::desc("Use one trap block per assertion"));
|
|
|
|
|
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");
|
2012-05-30 06:32:51 +08:00
|
|
|
STATISTIC(ChecksUnableInterproc, "Bounds checks unable to add (interprocedural)");
|
|
|
|
STATISTIC(ChecksUnableLoad, "Bounds checks unable to add (LoadInst)");
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
typedef IRBuilder<true, TargetFolder> BuilderTy;
|
|
|
|
|
|
|
|
namespace {
|
2012-06-01 06:45:40 +08:00
|
|
|
// FIXME: can use unions here to save space
|
|
|
|
struct CacheData {
|
|
|
|
APInt Offset;
|
|
|
|
Value *OffsetValue;
|
|
|
|
APInt Size;
|
|
|
|
Value *SizeValue;
|
|
|
|
bool ReturnVal;
|
2012-06-02 01:43:31 +08:00
|
|
|
CacheData() {}
|
|
|
|
CacheData(APInt Off, Value *OffVal, APInt Sz, Value *SzVal, bool Ret) :
|
|
|
|
Offset(Off), OffsetValue(OffVal), Size(Sz), SizeValue(SzVal),
|
|
|
|
ReturnVal(Ret) {}
|
2012-05-23 01:19:09 +08:00
|
|
|
};
|
2012-06-01 06:45:40 +08:00
|
|
|
typedef DenseMap<Value*, CacheData> CacheMapTy;
|
2012-06-02 01:43:31 +08:00
|
|
|
typedef SmallPtrSet<Value*, 8> PtrSetTy;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
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-30 06:32:51 +08:00
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addRequired<ScalarEvolution>();
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
2012-05-23 06:02:19 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const TargetData *TD;
|
2012-05-30 06:32:51 +08:00
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
2012-05-23 06:02:19 +08:00
|
|
|
BuilderTy *Builder;
|
|
|
|
Function *Fn;
|
|
|
|
BasicBlock *TrapBB;
|
|
|
|
unsigned Penalty;
|
2012-06-01 06:45:40 +08:00
|
|
|
CacheMapTy CacheMap;
|
2012-06-02 01:43:31 +08:00
|
|
|
PtrSetTy SeenPtrs;
|
2012-05-23 06:02:19 +08:00
|
|
|
|
|
|
|
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-01 06:58:48 +08:00
|
|
|
if (TrapBB && !ManyTrapBB)
|
2012-05-23 01:19:09 +08:00
|
|
|
return TrapBB;
|
|
|
|
|
|
|
|
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();
|
|
|
|
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) {
|
|
|
|
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-06-01 06:45:40 +08:00
|
|
|
#define GET_VALUE(Val, Int) \
|
|
|
|
if (!Val) \
|
|
|
|
Val = ConstantInt::get(IntTy, Int)
|
|
|
|
|
|
|
|
#define RETURN(Val) \
|
|
|
|
do { ReturnVal = Val; goto cache_and_return; } while (0)
|
|
|
|
|
|
|
|
/// computeAllocSize - compute the object size and the offset within the object
|
|
|
|
/// pointed by Ptr. OffsetValue/SizeValue will be null if they are constant, and
|
|
|
|
/// therefore the result is given in Offset/Size variables instead.
|
|
|
|
/// Returns true if the offset and size could be computed within the given
|
|
|
|
/// maximum run-time penalty.
|
|
|
|
bool BoundsChecking::computeAllocSize(Value *Ptr, APInt &Offset,
|
|
|
|
Value* &OffsetValue, APInt &Size,
|
|
|
|
Value* &SizeValue) {
|
|
|
|
Ptr = Ptr->stripPointerCasts();
|
|
|
|
|
|
|
|
// lookup to see if we've seen the Ptr before
|
|
|
|
CacheMapTy::iterator CacheIt = CacheMap.find(Ptr);
|
|
|
|
if (CacheIt != CacheMap.end()) {
|
|
|
|
CacheData &Cache = CacheIt->second;
|
|
|
|
Offset = Cache.Offset;
|
|
|
|
OffsetValue = Cache.OffsetValue;
|
|
|
|
Size = Cache.Size;
|
|
|
|
SizeValue = Cache.SizeValue;
|
|
|
|
return Cache.ReturnVal;
|
|
|
|
}
|
|
|
|
|
2012-06-02 01:43:31 +08:00
|
|
|
// record the pointers that were handled in this run, so that they can be
|
|
|
|
// cleaned later if something fails
|
|
|
|
SeenPtrs.insert(Ptr);
|
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
IntegerType *IntTy = TD->getIntPtrType(Fn->getContext());
|
|
|
|
unsigned IntTyBits = IntTy->getBitWidth();
|
|
|
|
bool ReturnVal;
|
|
|
|
|
|
|
|
// always generate code immediately before the instruction being processed, so
|
|
|
|
// that the generated code dominates the same BBs
|
|
|
|
Instruction *PrevInsertPoint = Builder->GetInsertPoint();
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Ptr))
|
|
|
|
Builder->SetInsertPoint(I);
|
|
|
|
|
|
|
|
// initalize with "don't know" state: offset=0 and size=uintmax
|
|
|
|
Offset = 0;
|
|
|
|
Size = APInt::getMaxValue(TD->getTypeSizeInBits(IntTy));
|
|
|
|
OffsetValue = SizeValue = 0;
|
|
|
|
|
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
|
|
|
|
APInt PtrOffset(IntTyBits, 0);
|
|
|
|
Value *PtrOffsetValue = 0;
|
|
|
|
if (!computeAllocSize(GEP->getPointerOperand(), PtrOffset, PtrOffsetValue,
|
|
|
|
Size, SizeValue))
|
|
|
|
RETURN(false);
|
|
|
|
|
|
|
|
if (GEP->hasAllConstantIndices()) {
|
|
|
|
SmallVector<Value*, 8> Ops(GEP->idx_begin(), GEP->idx_end());
|
|
|
|
Offset = TD->getIndexedOffset(GEP->getPointerOperandType(), Ops);
|
|
|
|
// if PtrOffset is constant, return immediately
|
|
|
|
if (!PtrOffsetValue) {
|
|
|
|
Offset += PtrOffset;
|
|
|
|
RETURN(true);
|
|
|
|
}
|
|
|
|
OffsetValue = ConstantInt::get(IntTy, Offset);
|
2012-06-02 01:43:31 +08:00
|
|
|
} else if (Penalty > 1) {
|
2012-06-01 06:45:40 +08:00
|
|
|
OffsetValue = EmitGEPOffset(Builder, *TD, GEP);
|
2012-06-02 01:43:31 +08:00
|
|
|
GET_VALUE(PtrOffsetValue, PtrOffset);
|
|
|
|
} else
|
|
|
|
RETURN(false);
|
2012-06-01 06:45:40 +08:00
|
|
|
|
|
|
|
OffsetValue = Builder->CreateAdd(PtrOffsetValue, OffsetValue);
|
|
|
|
RETURN(true);
|
2012-05-26 00:54:04 +08:00
|
|
|
|
|
|
|
// global variable with definitive size
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
if (GV->hasDefinitiveInitializer()) {
|
|
|
|
Constant *C = GV->getInitializer();
|
|
|
|
Size = TD->getTypeAllocSize(C->getType());
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true);
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
// stack allocation
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(Ptr)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
if (!AI->getAllocatedType()->isSized())
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
Size = TD->getTypeAllocSize(AI->getAllocatedType());
|
|
|
|
if (!AI->isArrayAllocation())
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true); // we are done
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
Value *ArraySize = AI->getArraySize();
|
|
|
|
if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
|
2012-06-01 06:45:40 +08:00
|
|
|
Size *= C->getValue();
|
|
|
|
RETURN(true);
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Penalty < 2)
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
// VLA: compute size dynamically
|
2012-05-23 01:19:09 +08:00
|
|
|
SizeValue = ConstantInt::get(ArraySize->getType(), Size);
|
|
|
|
SizeValue = Builder->CreateMul(SizeValue, ArraySize);
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-05-26 05:15:17 +08:00
|
|
|
// function arguments
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(Ptr)) {
|
|
|
|
// right now we only support byval arguments, so that no interprocedural
|
|
|
|
// analysis is necessary
|
2012-05-30 06:32:51 +08:00
|
|
|
if (!A->hasByValAttr()) {
|
|
|
|
++ChecksUnableInterproc;
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-30 06:32:51 +08:00
|
|
|
}
|
2012-05-26 05:15:17 +08:00
|
|
|
|
|
|
|
PointerType *PT = cast<PointerType>(A->getType());
|
|
|
|
Size = TD->getTypeAllocSize(PT->getElementType());
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true);
|
2012-05-26 05:15:17 +08:00
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
// ptr = select(ptr1, ptr2)
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (SelectInst *SI = dyn_cast<SelectInst>(Ptr)) {
|
|
|
|
APInt OffsetTrue(IntTyBits, 0), OffsetFalse(IntTyBits, 0);
|
|
|
|
APInt SizeTrue(IntTyBits, 0), SizeFalse(IntTyBits, 0);
|
|
|
|
Value *OffsetValueTrue = 0, *OffsetValueFalse = 0;
|
|
|
|
Value *SizeValueTrue = 0, *SizeValueFalse = 0;
|
|
|
|
|
|
|
|
bool TrueAlloc = computeAllocSize(SI->getTrueValue(), OffsetTrue,
|
|
|
|
OffsetValueTrue, SizeTrue, SizeValueTrue);
|
|
|
|
bool FalseAlloc = computeAllocSize(SI->getFalseValue(), OffsetFalse,
|
|
|
|
OffsetValueFalse, SizeFalse,
|
|
|
|
SizeValueFalse);
|
2012-06-02 01:43:31 +08:00
|
|
|
if (!TrueAlloc || !FalseAlloc)
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
|
|
|
|
|
|
|
// fold constant sizes & offsets if they are equal
|
|
|
|
if (!OffsetValueTrue && !OffsetValueFalse && OffsetTrue == OffsetFalse)
|
|
|
|
Offset = OffsetTrue;
|
|
|
|
else if (Penalty > 1) {
|
|
|
|
GET_VALUE(OffsetValueTrue, OffsetTrue);
|
|
|
|
GET_VALUE(OffsetValueFalse, OffsetFalse);
|
|
|
|
OffsetValue = Builder->CreateSelect(SI->getCondition(), OffsetValueTrue,
|
|
|
|
OffsetValueFalse);
|
|
|
|
} else
|
|
|
|
RETURN(false);
|
|
|
|
|
|
|
|
if (!SizeValueTrue && !SizeValueFalse && SizeTrue == SizeFalse)
|
|
|
|
Size = SizeTrue;
|
|
|
|
else if (Penalty > 1) {
|
|
|
|
GET_VALUE(SizeValueTrue, SizeTrue);
|
|
|
|
GET_VALUE(SizeValueFalse, SizeFalse);
|
|
|
|
SizeValue = Builder->CreateSelect(SI->getCondition(), SizeValueTrue,
|
|
|
|
SizeValueFalse);
|
|
|
|
} else
|
|
|
|
RETURN(false);
|
|
|
|
RETURN(true);
|
2012-05-26 00:54:04 +08:00
|
|
|
|
|
|
|
// call allocation function
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
|
2012-05-26 00:54:04 +08:00
|
|
|
SmallVector<unsigned, 4> Args;
|
|
|
|
|
|
|
|
if (MDNode *MD = CI->getMetadata("alloc_size")) {
|
|
|
|
for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
|
|
|
|
Args.push_back(cast<ConstantInt>(MD->getOperand(i))->getZExtValue());
|
|
|
|
|
|
|
|
} else if (Function *Callee = CI->getCalledFunction()) {
|
|
|
|
FunctionType *FTy = Callee->getFunctionType();
|
|
|
|
|
2012-05-23 06:02:19 +08:00
|
|
|
// alloc(size)
|
2012-05-26 00:54:04 +08:00
|
|
|
if (FTy->getNumParams() == 1 && FTy->getParamType(0)->isIntegerTy()) {
|
|
|
|
if ((Callee->getName() == "malloc" ||
|
|
|
|
Callee->getName() == "valloc" ||
|
|
|
|
Callee->getName() == "_Znwj" || // operator new(unsigned int)
|
|
|
|
Callee->getName() == "_Znwm" || // operator new(unsigned long)
|
|
|
|
Callee->getName() == "_Znaj" || // operator new[](unsigned int)
|
|
|
|
Callee->getName() == "_Znam")) {
|
|
|
|
Args.push_back(0);
|
|
|
|
}
|
|
|
|
} else if (FTy->getNumParams() == 2) {
|
|
|
|
// alloc(_, x)
|
|
|
|
if (FTy->getParamType(1)->isIntegerTy() &&
|
|
|
|
((Callee->getName() == "realloc" ||
|
|
|
|
Callee->getName() == "reallocf"))) {
|
|
|
|
Args.push_back(1);
|
|
|
|
|
|
|
|
// alloc(x, y)
|
|
|
|
} else if (FTy->getParamType(0)->isIntegerTy() &&
|
|
|
|
FTy->getParamType(1)->isIntegerTy() &&
|
|
|
|
Callee->getName() == "calloc") {
|
|
|
|
Args.push_back(0);
|
|
|
|
Args.push_back(1);
|
2012-05-23 06:02:19 +08:00
|
|
|
}
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (FTy->getNumParams() == 3) {
|
|
|
|
// alloc(_, _, x)
|
|
|
|
if (FTy->getParamType(2)->isIntegerTy() &&
|
|
|
|
Callee->getName() == "posix_memalign") {
|
|
|
|
Args.push_back(2);
|
|
|
|
}
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
if (Args.empty())
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-23 06:02:19 +08:00
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
// check if all arguments are constant. if so, the object size is also const
|
|
|
|
bool AllConst = true;
|
|
|
|
for (SmallVectorImpl<unsigned>::iterator I = Args.begin(), E = Args.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!isa<ConstantInt>(CI->getArgOperand(*I))) {
|
|
|
|
AllConst = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-23 06:02:19 +08:00
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
if (AllConst) {
|
|
|
|
Size = 1;
|
|
|
|
for (SmallVectorImpl<unsigned>::iterator I = Args.begin(), E = Args.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ConstantInt *Arg = cast<ConstantInt>(CI->getArgOperand(*I));
|
2012-06-01 06:45:40 +08:00
|
|
|
Size *= Arg->getValue().zextOrSelf(IntTyBits);
|
2012-05-23 06:02:19 +08:00
|
|
|
}
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true);
|
2012-05-26 00:54:04 +08:00
|
|
|
}
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-05-26 00:54:04 +08:00
|
|
|
if (Penalty < 2)
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-26 00:54:04 +08:00
|
|
|
|
|
|
|
// not all arguments are constant, so create a sequence of multiplications
|
|
|
|
for (SmallVectorImpl<unsigned>::iterator I = Args.begin(), E = Args.end();
|
|
|
|
I != E; ++I) {
|
2012-06-01 06:45:40 +08:00
|
|
|
Value *Arg = Builder->CreateZExt(CI->getArgOperand(*I), IntTy);
|
|
|
|
if (!SizeValue) {
|
2012-05-26 00:54:04 +08:00
|
|
|
SizeValue = Arg;
|
|
|
|
continue;
|
2012-05-23 06:02:19 +08:00
|
|
|
}
|
2012-05-26 00:54:04 +08:00
|
|
|
SizeValue = Builder->CreateMul(SizeValue, Arg);
|
2012-05-23 06:02:19 +08:00
|
|
|
}
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(true);
|
2012-05-26 00:54:04 +08:00
|
|
|
|
2012-06-09 00:31:42 +08:00
|
|
|
// TODO: handle more standard functions (+ wchar cousins):
|
2012-05-23 06:02:19 +08:00
|
|
|
// - strdup / strndup
|
|
|
|
// - strcpy / strncpy
|
2012-06-09 00:31:42 +08:00
|
|
|
// - strcat / strncat
|
2012-05-23 06:02:19 +08:00
|
|
|
// - memcpy / memmove
|
|
|
|
// - strcat / strncat
|
2012-06-09 00:31:42 +08:00
|
|
|
// - memset
|
2012-05-30 06:32:51 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
} else if (PHINode *PHI = dyn_cast<PHINode>(Ptr)) {
|
|
|
|
// create 2 PHIs: one for offset and another for size
|
|
|
|
PHINode *OffsetPHI = Builder->CreatePHI(IntTy, PHI->getNumIncomingValues());
|
|
|
|
PHINode *SizePHI = Builder->CreatePHI(IntTy, PHI->getNumIncomingValues());
|
|
|
|
|
|
|
|
// insert right away in the cache to handle recursive PHIs
|
2012-06-09 00:31:42 +08:00
|
|
|
CacheMap[Ptr] = CacheData(APInt(), OffsetPHI, APInt(), SizePHI, true);
|
2012-06-01 06:45:40 +08:00
|
|
|
|
|
|
|
// compute offset/size for each PHI incoming pointer
|
|
|
|
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Builder->SetInsertPoint(PHI->getIncomingBlock(i)->getFirstInsertionPt());
|
|
|
|
|
|
|
|
APInt PhiOffset(IntTyBits, 0), PhiSize(IntTyBits, 0);
|
|
|
|
Value *PhiOffsetValue = 0, *PhiSizeValue = 0;
|
2012-06-02 01:43:31 +08:00
|
|
|
|
|
|
|
if (!computeAllocSize(PHI->getIncomingValue(i), PhiOffset, PhiOffsetValue,
|
|
|
|
PhiSize, PhiSizeValue)) {
|
|
|
|
OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
|
|
|
|
OffsetPHI->eraseFromParent();
|
|
|
|
SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
|
|
|
|
SizePHI->eraseFromParent();
|
|
|
|
RETURN(false);
|
|
|
|
}
|
2012-06-01 06:45:40 +08:00
|
|
|
|
|
|
|
GET_VALUE(PhiOffsetValue, PhiOffset);
|
|
|
|
GET_VALUE(PhiSizeValue, PhiSize);
|
|
|
|
|
|
|
|
OffsetPHI->addIncoming(PhiOffsetValue, PHI->getIncomingBlock(i));
|
|
|
|
SizePHI->addIncoming(PhiSizeValue, PHI->getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
OffsetValue = OffsetPHI;
|
|
|
|
SizeValue = SizePHI;
|
|
|
|
RETURN(true);
|
|
|
|
|
2012-06-09 00:31:42 +08:00
|
|
|
} else if (isa<UndefValue>(Ptr) || isa<ConstantPointerNull>(Ptr)) {
|
2012-06-01 06:45:40 +08:00
|
|
|
Size = 0;
|
|
|
|
RETURN(true);
|
|
|
|
|
|
|
|
} else if (isa<LoadInst>(Ptr)) {
|
2012-05-30 06:32:51 +08:00
|
|
|
++ChecksUnableLoad;
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
2012-06-09 00:31:42 +08:00
|
|
|
DEBUG(dbgs() << "computeAllocSize unhandled value:\n" << *Ptr << "\n");
|
2012-06-01 06:45:40 +08:00
|
|
|
RETURN(false);
|
|
|
|
|
|
|
|
cache_and_return:
|
|
|
|
// cache the result and return
|
2012-06-09 00:31:42 +08:00
|
|
|
CacheMap[Ptr] = CacheData(Offset, OffsetValue, Size, SizeValue, ReturnVal);
|
2012-06-01 06:45:40 +08:00
|
|
|
|
2012-06-02 01:43:31 +08:00
|
|
|
// non-computable results can be safely cached
|
|
|
|
if (!ReturnVal)
|
|
|
|
SeenPtrs.erase(Ptr);
|
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
Builder->SetInsertPoint(PrevInsertPoint);
|
|
|
|
return ReturnVal;
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-01 06:45:40 +08:00
|
|
|
IntegerType *IntTy = TD->getIntPtrType(Fn->getContext());
|
|
|
|
unsigned IntTyBits = IntTy->getBitWidth();
|
2012-05-30 06:32:51 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
APInt Offset(IntTyBits, 0), Size(IntTyBits, 0);
|
|
|
|
Value *OffsetValue = 0, *SizeValue = 0;
|
2012-05-30 06:32:51 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
if (!computeAllocSize(Ptr, Offset, OffsetValue, Size, SizeValue)) {
|
2012-05-30 06:32:51 +08:00
|
|
|
DEBUG(dbgs() << "computeAllocSize failed:\n" << *Ptr << "\n");
|
2012-06-02 01:43:31 +08:00
|
|
|
|
|
|
|
// erase everything that was computed in this iteration from the cache, so
|
|
|
|
// that no dangling references are left behind. We could be a bit smarter if
|
|
|
|
// we kept a dependency graph. It's probably not worth the complexity,
|
|
|
|
// though.
|
|
|
|
for (PtrSetTy::iterator I=SeenPtrs.begin(), E=SeenPtrs.end(); I != E; ++I)
|
|
|
|
CacheMap.erase(*I);
|
|
|
|
SeenPtrs.clear();
|
|
|
|
|
2012-05-23 01:19:09 +08:00
|
|
|
++ChecksUnable;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if (!OffsetValue && !SizeValue) {
|
|
|
|
if (Offset.slt(0) || Size.ult(Offset) || (Size - Offset).ult(NeededSize)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
// Out of bounds
|
2012-05-24 00:24:52 +08:00
|
|
|
emitBranchToTrap();
|
2012-05-23 01:19:09 +08:00
|
|
|
++ChecksAdded;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// in bounds
|
|
|
|
++ChecksSkipped;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
// emit check for offset < 0
|
|
|
|
Value *CmpOffset = 0;
|
|
|
|
if (OffsetValue)
|
|
|
|
CmpOffset = Builder->CreateICmpSLT(OffsetValue, ConstantInt::get(IntTy, 0));
|
|
|
|
else if (Offset.slt(0)) {
|
|
|
|
// offset proved to be negative
|
|
|
|
emitBranchToTrap();
|
|
|
|
++ChecksAdded;
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
// we couldn't determine statically if the memory access is safe; emit a
|
|
|
|
// run-time check
|
|
|
|
GET_VALUE(OffsetValue, Offset);
|
|
|
|
GET_VALUE(SizeValue, Size);
|
2012-05-30 06:32:51 +08:00
|
|
|
|
2012-06-01 06:45:40 +08:00
|
|
|
Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
|
|
|
|
// FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
|
2012-05-23 01:19:09 +08:00
|
|
|
Value *ObjSize = Builder->CreateSub(SizeValue, OffsetValue);
|
|
|
|
Value *Cmp1 = Builder->CreateICmpULT(SizeValue, OffsetValue);
|
|
|
|
Value *Cmp2 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
|
|
|
|
Value *Or = Builder->CreateOr(Cmp1, Cmp2);
|
2012-06-01 06:45:40 +08:00
|
|
|
if (CmpOffset)
|
|
|
|
Or = Builder->CreateOr(CmpOffset, Or);
|
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>();
|
2012-05-30 06:32:51 +08:00
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
TrapBB = 0;
|
|
|
|
Fn = &F;
|
|
|
|
BuilderTy TheBuilder(F.getContext(), TargetFolder(TD));
|
|
|
|
Builder = &TheBuilder;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
Instruction *I = *i;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
Builder->SetInsertPoint(I);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
MadeChange |= instrument(LI->getPointerOperand(), LI);
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
|
|
|
MadeChange |= instrument(SI->getPointerOperand(), SI->getValueOperand());
|
|
|
|
} else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
|
|
|
|
MadeChange |= instrument(AI->getPointerOperand(),AI->getCompareOperand());
|
|
|
|
} else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(I)) {
|
|
|
|
MadeChange |= instrument(AI->getPointerOperand(), AI->getValOperand());
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("unknown Instruction type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createBoundsCheckingPass(unsigned Penalty) {
|
|
|
|
return new BoundsChecking(Penalty);
|
|
|
|
}
|