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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2014-03-04 19:59:06 +08:00
|
|
|
#include "llvm/Analysis/TargetFolder.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Pass.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"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-05-23 01:19:09 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "bounds-checking"
|
|
|
|
|
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");
|
|
|
|
|
2016-03-14 05:05:13 +08:00
|
|
|
typedef IRBuilder<TargetFolder> BuilderTy;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BoundsChecking : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
|
2012-11-23 18:47:35 +08:00
|
|
|
BoundsChecking() : FunctionPass(ID) {
|
2012-05-23 01:19:09 +08:00
|
|
|
initializeBoundsCheckingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2015-01-15 18:41:28 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
2012-05-23 06:02:19 +08:00
|
|
|
|
|
|
|
private:
|
2012-08-29 23:32:21 +08:00
|
|
|
const TargetLibraryInfo *TLI;
|
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;
|
|
|
|
|
|
|
|
BasicBlock *getTrapBB();
|
2014-04-25 13:29:35 +08:00
|
|
|
void emitBranchToTrap(Value *Cmp = nullptr);
|
2015-03-10 10:37:25 +08:00
|
|
|
bool instrument(Value *Ptr, Value *Val, const DataLayout &DL);
|
2012-05-23 01:19:09 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
char BoundsChecking::ID = 0;
|
2012-07-04 01:30:18 +08:00
|
|
|
INITIALIZE_PASS(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();
|
2013-09-30 23:40:17 +08:00
|
|
|
IRBuilder<>::InsertPointGuard Guard(*Builder);
|
2012-05-23 01:19:09 +08:00
|
|
|
TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
|
2013-09-30 23:52:50 +08:00
|
|
|
Builder->SetInsertPoint(TrapBB);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
|
2015-05-19 06:13:54 +08:00
|
|
|
CallInst *TrapCall = Builder->CreateCall(F, {});
|
2012-05-23 01:19:09 +08:00
|
|
|
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();
|
|
|
|
|
|
|
|
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
|
2014-04-25 13:29:35 +08:00
|
|
|
Cmp = nullptr; // unconditional branch
|
2012-06-21 23:59:53 +08:00
|
|
|
}
|
2012-12-03 18:15:03 +08:00
|
|
|
++ChecksAdded;
|
2012-06-21 23:59:53 +08:00
|
|
|
|
2015-10-14 01:39:10 +08:00
|
|
|
BasicBlock::iterator Inst = Builder->GetInsertPoint();
|
2012-05-24 00:24:52 +08:00
|
|
|
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.
|
2015-03-10 10:37:25 +08:00
|
|
|
bool BoundsChecking::instrument(Value *Ptr, Value *InstVal,
|
|
|
|
const DataLayout &DL) {
|
|
|
|
uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
|
2012-05-23 01:19:09 +08:00
|
|
|
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-07-04 01:30:18 +08:00
|
|
|
ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
|
2012-06-21 23:59:53 +08:00
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
Type *IntTy = DL.getIntPtrType(Ptr->getType());
|
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)
|
2012-07-04 01:30:18 +08:00
|
|
|
//
|
|
|
|
// optimization: if Size >= 0 (signed), skip 1st check
|
2012-06-01 06:45:40 +08:00
|
|
|
// 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 *Cmp2 = Builder->CreateICmpULT(Size, Offset);
|
|
|
|
Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
|
2012-07-04 01:30:18 +08:00
|
|
|
Value *Or = Builder->CreateOr(Cmp2, Cmp3);
|
|
|
|
if (!SizeCI || SizeCI->getValue().slt(0)) {
|
|
|
|
Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
|
|
|
|
Or = Builder->CreateOr(Cmp1, Or);
|
|
|
|
}
|
2012-05-24 00:24:52 +08:00
|
|
|
emitBranchToTrap(Or);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoundsChecking::runOnFunction(Function &F) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-01-15 18:41:28 +08:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
TrapBB = nullptr;
|
2014-02-21 08:06:31 +08:00
|
|
|
BuilderTy TheBuilder(F.getContext(), TargetFolder(DL));
|
2012-05-23 01:19:09 +08:00
|
|
|
Builder = &TheBuilder;
|
2014-02-21 08:06:31 +08:00
|
|
|
ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(),
|
2013-10-24 17:17:24 +08:00
|
|
|
/*RoundToAlign=*/true);
|
2012-06-21 23:59:53 +08:00
|
|
|
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)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
MadeChange |= instrument(LI->getPointerOperand(), LI, DL);
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
MadeChange |=
|
|
|
|
instrument(SI->getPointerOperand(), SI->getValueOperand(), DL);
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
MadeChange |=
|
|
|
|
instrument(AI->getPointerOperand(), AI->getCompareOperand(), DL);
|
2012-06-23 08:12:34 +08:00
|
|
|
} else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
MadeChange |=
|
|
|
|
instrument(AI->getPointerOperand(), AI->getValOperand(), DL);
|
2012-05-23 01:19:09 +08:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("unknown Instruction type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:47:35 +08:00
|
|
|
FunctionPass *llvm::createBoundsCheckingPass() {
|
|
|
|
return new BoundsChecking();
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|