2012-05-22 19:19:09 +02: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-03 17:50:05 +01:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2014-03-04 12:59:06 +01:00
|
|
|
#include "llvm/Analysis/TargetFolder.h"
|
2015-03-23 20:32:43 +01:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-04 11:30:26 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2012-06-29 14:38:19 +02:00
|
|
|
#include "llvm/Pass.h"
|
2012-06-01 00:58:48 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-05-22 19:19:09 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-06-29 14:38:19 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-05-22 19:19:09 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:55:47 +02:00
|
|
|
#define DEBUG_TYPE "bounds-checking"
|
|
|
|
|
2012-06-21 17:59:53 +02:00
|
|
|
static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
|
|
|
|
cl::desc("Use one trap block per function"));
|
2012-06-01 00:58:48 +02:00
|
|
|
|
2012-05-22 19:19:09 +02:00
|
|
|
STATISTIC(ChecksAdded, "Bounds checks added");
|
|
|
|
STATISTIC(ChecksSkipped, "Bounds checks skipped");
|
|
|
|
STATISTIC(ChecksUnable, "Bounds checks unable to add");
|
|
|
|
|
2016-03-13 22:05:13 +01:00
|
|
|
typedef IRBuilder<TargetFolder> BuilderTy;
|
2012-05-22 19:19:09 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BoundsChecking : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
|
2012-11-23 11:47:35 +01:00
|
|
|
BoundsChecking() : FunctionPass(ID) {
|
2012-05-22 19:19:09 +02:00
|
|
|
initializeBoundsCheckingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
2012-05-22 19:19:09 +02:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2015-01-15 11:41:28 +01:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2012-05-22 19:19:09 +02:00
|
|
|
}
|
2012-05-23 00:02:19 +02:00
|
|
|
|
|
|
|
private:
|
2012-08-29 17:32:21 +02:00
|
|
|
const TargetLibraryInfo *TLI;
|
2012-06-21 17:59:53 +02:00
|
|
|
ObjectSizeOffsetEvaluator *ObjSizeEval;
|
2012-05-23 00:02:19 +02:00
|
|
|
BuilderTy *Builder;
|
2012-06-23 02:12:34 +02:00
|
|
|
Instruction *Inst;
|
2012-05-23 00:02:19 +02:00
|
|
|
BasicBlock *TrapBB;
|
|
|
|
|
|
|
|
BasicBlock *getTrapBB();
|
2014-04-25 07:29:35 +02:00
|
|
|
void emitBranchToTrap(Value *Cmp = nullptr);
|
2015-03-10 03:37:25 +01:00
|
|
|
bool instrument(Value *Ptr, Value *Val, const DataLayout &DL);
|
2012-05-22 19:19:09 +02:00
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2012-05-22 19:19:09 +02:00
|
|
|
|
|
|
|
char BoundsChecking::ID = 0;
|
2012-07-03 19:30:18 +02:00
|
|
|
INITIALIZE_PASS(BoundsChecking, "bounds-checking", "Run-time bounds checking",
|
|
|
|
false, false)
|
2012-05-22 19:19:09 +02: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 17:59:53 +02:00
|
|
|
if (TrapBB && SingleTrapBB)
|
2012-05-22 19:19:09 +02:00
|
|
|
return TrapBB;
|
|
|
|
|
2012-06-23 02:12:34 +02:00
|
|
|
Function *Fn = Inst->getParent()->getParent();
|
2013-09-30 17:40:17 +02:00
|
|
|
IRBuilder<>::InsertPointGuard Guard(*Builder);
|
2012-05-22 19:19:09 +02:00
|
|
|
TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
|
2013-09-30 17:52:50 +02:00
|
|
|
Builder->SetInsertPoint(TrapBB);
|
2012-05-22 19:19:09 +02:00
|
|
|
|
|
|
|
llvm::Value *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
|
2015-05-19 00:13:54 +02:00
|
|
|
CallInst *TrapCall = Builder->CreateCall(F, {});
|
2012-05-22 19:19:09 +02:00
|
|
|
TrapCall->setDoesNotReturn();
|
|
|
|
TrapCall->setDoesNotThrow();
|
2012-06-23 02:12:34 +02:00
|
|
|
TrapCall->setDebugLoc(Inst->getDebugLoc());
|
2012-05-22 19:19:09 +02:00
|
|
|
Builder->CreateUnreachable();
|
|
|
|
|
|
|
|
return TrapBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-23 18:24:52 +02: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 17:59:53 +02: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 07:29:35 +02:00
|
|
|
Cmp = nullptr; // unconditional branch
|
2012-06-21 17:59:53 +02:00
|
|
|
}
|
2012-12-03 11:15:03 +01:00
|
|
|
++ChecksAdded;
|
2012-06-21 17:59:53 +02:00
|
|
|
|
2015-10-13 19:39:10 +02:00
|
|
|
BasicBlock::iterator Inst = Builder->GetInsertPoint();
|
2012-05-23 18:24:52 +02: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 00:02:19 +02: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 03:37:25 +01:00
|
|
|
bool BoundsChecking::instrument(Value *Ptr, Value *InstVal,
|
|
|
|
const DataLayout &DL) {
|
|
|
|
uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
|
2012-05-22 19:19:09 +02:00
|
|
|
DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
|
|
|
|
<< " bytes\n");
|
|
|
|
|
2012-06-21 17:59:53 +02:00
|
|
|
SizeOffsetEvalType SizeOffset = ObjSizeEval->compute(Ptr);
|
2012-06-01 19:43:31 +02:00
|
|
|
|
2012-06-21 17:59:53 +02:00
|
|
|
if (!ObjSizeEval->bothKnown(SizeOffset)) {
|
2012-05-22 19:19:09 +02:00
|
|
|
++ChecksUnable;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-21 17:59:53 +02:00
|
|
|
Value *Size = SizeOffset.first;
|
|
|
|
Value *Offset = SizeOffset.second;
|
2012-07-03 19:30:18 +02:00
|
|
|
ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
|
2012-06-21 17:59:53 +02:00
|
|
|
|
2015-03-10 03:37:25 +01:00
|
|
|
Type *IntTy = DL.getIntPtrType(Ptr->getType());
|
2012-06-21 17:59:53 +02:00
|
|
|
Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
|
|
|
|
|
2012-06-01 00:45:40 +02: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-03 19:30:18 +02:00
|
|
|
//
|
|
|
|
// optimization: if Size >= 0 (signed), skip 1st check
|
2012-06-01 00:45:40 +02:00
|
|
|
// FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
|
2012-06-21 17:59:53 +02:00
|
|
|
Value *ObjSize = Builder->CreateSub(Size, Offset);
|
|
|
|
Value *Cmp2 = Builder->CreateICmpULT(Size, Offset);
|
|
|
|
Value *Cmp3 = Builder->CreateICmpULT(ObjSize, NeededSizeVal);
|
2012-07-03 19:30:18 +02: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-23 18:24:52 +02:00
|
|
|
emitBranchToTrap(Or);
|
2012-05-22 19:19:09 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoundsChecking::runOnFunction(Function &F) {
|
2015-03-10 03:37:25 +01:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-01-15 11:41:28 +01:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2012-05-22 19:19:09 +02:00
|
|
|
|
2014-04-25 07:29:35 +02:00
|
|
|
TrapBB = nullptr;
|
2014-02-21 01:06:31 +01:00
|
|
|
BuilderTy TheBuilder(F.getContext(), TargetFolder(DL));
|
2012-05-22 19:19:09 +02:00
|
|
|
Builder = &TheBuilder;
|
2014-02-21 01:06:31 +01:00
|
|
|
ObjectSizeOffsetEvaluator TheObjSizeEval(DL, TLI, F.getContext(),
|
2013-10-24 11:17:24 +02:00
|
|
|
/*RoundToAlign=*/true);
|
2012-06-21 17:59:53 +02:00
|
|
|
ObjSizeEval = &TheObjSizeEval;
|
2012-05-22 19:19:09 +02: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 00:02:19 +02:00
|
|
|
for (std::vector<Instruction*>::iterator i = WorkList.begin(),
|
|
|
|
e = WorkList.end(); i != e; ++i) {
|
2012-06-23 02:12:34 +02:00
|
|
|
Inst = *i;
|
2012-05-22 19:19:09 +02:00
|
|
|
|
2012-06-23 02:12:34 +02:00
|
|
|
Builder->SetInsertPoint(Inst);
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
|
2015-03-10 03:37:25 +01:00
|
|
|
MadeChange |= instrument(LI->getPointerOperand(), LI, DL);
|
2012-06-23 02:12:34 +02:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2015-03-10 03:37:25 +01:00
|
|
|
MadeChange |=
|
|
|
|
instrument(SI->getPointerOperand(), SI->getValueOperand(), DL);
|
2012-06-23 02:12:34 +02:00
|
|
|
} else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
|
2015-03-10 03:37:25 +01:00
|
|
|
MadeChange |=
|
|
|
|
instrument(AI->getPointerOperand(), AI->getCompareOperand(), DL);
|
2012-06-23 02:12:34 +02:00
|
|
|
} else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
|
2015-03-10 03:37:25 +01:00
|
|
|
MadeChange |=
|
|
|
|
instrument(AI->getPointerOperand(), AI->getValOperand(), DL);
|
2012-05-22 19:19:09 +02:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("unknown Instruction type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:47:35 +01:00
|
|
|
FunctionPass *llvm::createBoundsCheckingPass() {
|
|
|
|
return new BoundsChecking();
|
2012-05-22 19:19:09 +02:00
|
|
|
}
|