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/ADT/Statistic.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#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"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/Support/Casting.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"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2017-10-20 06:07:16 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
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");
|
|
|
|
|
2017-10-20 06:07:16 +08:00
|
|
|
using BuilderTy = IRBuilder<TargetFolder>;
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-10-20 06:07:16 +08:00
|
|
|
|
2012-05-23 01:19:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2017-10-20 06:07:16 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
char BoundsChecking::ID = 0;
|
2017-10-20 06:07:16 +08:00
|
|
|
|
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
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
/// Adds run-time bounds checks to memory accessing instructions.
|
|
|
|
///
|
|
|
|
/// \p Ptr is the pointer that will be read/written, and \p 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.
|
|
|
|
///
|
|
|
|
/// \p GetTrapBB is a callable that returns the trap BB to use on failure.
|
|
|
|
///
|
2012-05-23 06:02:19 +08:00
|
|
|
/// Returns true if any change was made to the IR, false otherwise.
|
2017-11-14 09:13:59 +08:00
|
|
|
template <typename GetTrapBBT>
|
|
|
|
static bool instrumentMemAccess(Value *Ptr, Value *InstVal,
|
|
|
|
const DataLayout &DL, TargetLibraryInfo &TLI,
|
|
|
|
ObjectSizeOffsetEvaluator &ObjSizeEval,
|
|
|
|
BuilderTy &IRB,
|
|
|
|
GetTrapBBT GetTrapBB) {
|
2015-03-10 10:37:25 +08:00
|
|
|
uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
|
2012-05-23 01:19:09 +08:00
|
|
|
DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
|
|
|
|
<< " bytes\n");
|
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
SizeOffsetEvalType SizeOffset = ObjSizeEval.compute(Ptr);
|
2012-06-02 01:43:31 +08:00
|
|
|
|
2017-11-14 09:13:59 +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
|
2017-11-14 09:13:59 +08:00
|
|
|
Value *ObjSize = IRB.CreateSub(Size, Offset);
|
|
|
|
Value *Cmp2 = IRB.CreateICmpULT(Size, Offset);
|
|
|
|
Value *Cmp3 = IRB.CreateICmpULT(ObjSize, NeededSizeVal);
|
|
|
|
Value *Or = IRB.CreateOr(Cmp2, Cmp3);
|
2012-07-04 01:30:18 +08:00
|
|
|
if (!SizeCI || SizeCI->getValue().slt(0)) {
|
2017-11-14 09:13:59 +08:00
|
|
|
Value *Cmp1 = IRB.CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
|
|
|
|
Or = IRB.CreateOr(Cmp1, Or);
|
2012-07-04 01:30:18 +08:00
|
|
|
}
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2017-10-19 06:42:36 +08:00
|
|
|
// check if the comparison is always false
|
|
|
|
ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or);
|
|
|
|
if (C) {
|
|
|
|
++ChecksSkipped;
|
|
|
|
// If non-zero, nothing to do.
|
|
|
|
if (!C->getZExtValue())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++ChecksAdded;
|
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
BasicBlock::iterator SplitI = IRB.GetInsertPoint();
|
2017-10-19 06:42:36 +08:00
|
|
|
BasicBlock *OldBB = SplitI->getParent();
|
|
|
|
BasicBlock *Cont = OldBB->splitBasicBlock(SplitI);
|
|
|
|
OldBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
if (C) {
|
|
|
|
// If we have a constant zero, unconditionally branch.
|
|
|
|
// FIXME: We should really handle this differently to bypass the splitting
|
|
|
|
// the block.
|
2017-11-14 09:13:59 +08:00
|
|
|
BranchInst::Create(GetTrapBB(IRB), OldBB);
|
2017-10-19 06:42:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the conditional branch.
|
2017-11-14 09:13:59 +08:00
|
|
|
BranchInst::Create(GetTrapBB(IRB), Cont, Or, OldBB);
|
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();
|
2017-11-14 09:13:59 +08:00
|
|
|
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
ObjectSizeOffsetEvaluator ObjSizeEval(DL, &TLI, F.getContext(),
|
2013-10-24 17:17:24 +08:00
|
|
|
/*RoundToAlign=*/true);
|
2012-05-23 01:19:09 +08:00
|
|
|
|
|
|
|
// check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
|
|
|
|
// touching instructions
|
2017-10-20 06:07:16 +08:00
|
|
|
std::vector<Instruction *> WorkList;
|
2017-11-14 09:13:59 +08:00
|
|
|
for (Instruction &I : instructions(F)) {
|
2012-05-23 01:19:09 +08:00
|
|
|
if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
|
|
|
|
isa<AtomicRMWInst>(I))
|
2017-11-14 09:13:59 +08:00
|
|
|
WorkList.push_back(&I);
|
2012-05-23 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
// Create a trapping basic block on demand using a callback. Depending on
|
|
|
|
// flags, this will either create a single block for the entire function or
|
|
|
|
// will create a fresh block every time it is called.
|
|
|
|
BasicBlock *TrapBB = nullptr;
|
|
|
|
auto GetTrapBB = [&TrapBB](BuilderTy &IRB) {
|
|
|
|
if (TrapBB && SingleTrapBB)
|
|
|
|
return TrapBB;
|
|
|
|
|
|
|
|
Function *Fn = IRB.GetInsertBlock()->getParent();
|
|
|
|
// FIXME: This debug location doesn't make a lot of sense in the
|
|
|
|
// `SingleTrapBB` case.
|
|
|
|
auto DebugLoc = IRB.getCurrentDebugLocation();
|
|
|
|
IRBuilder<>::InsertPointGuard Guard(IRB);
|
|
|
|
TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
|
|
|
|
IRB.SetInsertPoint(TrapBB);
|
|
|
|
|
|
|
|
auto *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
|
|
|
|
CallInst *TrapCall = IRB.CreateCall(F, {});
|
|
|
|
TrapCall->setDoesNotReturn();
|
|
|
|
TrapCall->setDoesNotThrow();
|
|
|
|
TrapCall->setDebugLoc(DebugLoc);
|
|
|
|
IRB.CreateUnreachable();
|
2012-05-23 01:19:09 +08:00
|
|
|
|
2017-11-14 09:13:59 +08:00
|
|
|
return TrapBB;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (Instruction *Inst : WorkList) {
|
|
|
|
BuilderTy IRB(Inst->getParent(), BasicBlock::iterator(Inst), TargetFolder(DL));
|
2012-06-23 08:12:34 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
|
2017-11-14 09:13:59 +08:00
|
|
|
MadeChange |= instrumentMemAccess(LI->getPointerOperand(), LI, DL, TLI,
|
|
|
|
ObjSizeEval, IRB, GetTrapBB);
|
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 |=
|
2017-11-14 09:13:59 +08:00
|
|
|
instrumentMemAccess(SI->getPointerOperand(), SI->getValueOperand(),
|
|
|
|
DL, TLI, ObjSizeEval, IRB, GetTrapBB);
|
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 |=
|
2017-11-14 09:13:59 +08:00
|
|
|
instrumentMemAccess(AI->getPointerOperand(), AI->getCompareOperand(),
|
|
|
|
DL, TLI, ObjSizeEval, IRB, GetTrapBB);
|
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 |=
|
2017-11-14 09:13:59 +08:00
|
|
|
instrumentMemAccess(AI->getPointerOperand(), AI->getValOperand(), DL,
|
|
|
|
TLI, ObjSizeEval, IRB, GetTrapBB);
|
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
|
|
|
}
|