blob: e13db08e263c7aff89a2448878c2171f6e0ce191 [file] [log] [blame]
Nuno Lopesa2f6cec2012-05-22 17:19:09 +00001//===- BoundsChecking.cpp - Instrumentation for run-time bounds checking --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Nuno Lopesa2f6cec2012-05-22 17:19:09 +00009
Chandler Carruth00a301d2017-11-14 01:30:04 +000010#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/Statistic.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000012#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/Analysis/MemoryBuiltins.h"
Joel Galenson8dbcc582018-07-24 15:21:54 +000014#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth452a0072014-03-04 11:59:06 +000015#include "llvm/Analysis/TargetFolder.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000016#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000017#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000020#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/IRBuilder.h"
Chandler Carruth83948572014-03-04 10:30:26 +000022#include "llvm/IR/InstIterator.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000023#include "llvm/IR/InstrTypes.h"
24#include "llvm/IR/Instruction.h"
25#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Intrinsics.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000027#include "llvm/IR/Value.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000028#include "llvm/Pass.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000029#include "llvm/Support/Casting.h"
Nuno Lopes288e86ff62012-05-31 22:58:48 +000030#include "llvm/Support/CommandLine.h"
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000031#include "llvm/Support/Debug.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000033#include "llvm/Support/raw_ostream.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000034#include <cstdint>
35#include <vector>
36
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000037using namespace llvm;
38
Chandler Carruth964daaa2014-04-22 02:55:47 +000039#define DEBUG_TYPE "bounds-checking"
40
Nuno Lopes0e967e02012-06-21 15:59:53 +000041static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
42 cl::desc("Use one trap block per function"));
Nuno Lopes288e86ff62012-05-31 22:58:48 +000043
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000044STATISTIC(ChecksAdded, "Bounds checks added");
45STATISTIC(ChecksSkipped, "Bounds checks skipped");
46STATISTIC(ChecksUnable, "Bounds checks unable to add");
47
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000048using BuilderTy = IRBuilder<TargetFolder>;
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000049
Chandler Carruth1594fee2017-11-14 01:13:59 +000050/// Adds run-time bounds checks to memory accessing instructions.
51///
52/// \p Ptr is the pointer that will be read/written, and \p InstVal is either
53/// the result from the load or the value being stored. It is used to determine
54/// the size of memory block that is touched.
55///
56/// \p GetTrapBB is a callable that returns the trap BB to use on failure.
57///
Nuno Lopes59e9df72012-05-22 22:02:19 +000058/// Returns true if any change was made to the IR, false otherwise.
Chandler Carruth1594fee2017-11-14 01:13:59 +000059template <typename GetTrapBBT>
60static bool instrumentMemAccess(Value *Ptr, Value *InstVal,
61 const DataLayout &DL, TargetLibraryInfo &TLI,
62 ObjectSizeOffsetEvaluator &ObjSizeEval,
Joel Galenson8dbcc582018-07-24 15:21:54 +000063 BuilderTy &IRB, GetTrapBBT GetTrapBB,
64 ScalarEvolution &SE) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000065 uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
Nicola Zaghend34e60c2018-05-14 12:53:11 +000066 LLVM_DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
67 << " bytes\n");
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000068
Chandler Carruth1594fee2017-11-14 01:13:59 +000069 SizeOffsetEvalType SizeOffset = ObjSizeEval.compute(Ptr);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000070
Chandler Carruth1594fee2017-11-14 01:13:59 +000071 if (!ObjSizeEval.bothKnown(SizeOffset)) {
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000072 ++ChecksUnable;
73 return false;
74 }
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000075
Nuno Lopes0e967e02012-06-21 15:59:53 +000076 Value *Size = SizeOffset.first;
77 Value *Offset = SizeOffset.second;
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000078 ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
Nuno Lopes0e967e02012-06-21 15:59:53 +000079
Mehdi Aminia28d91d2015-03-10 02:37:25 +000080 Type *IntTy = DL.getIntPtrType(Ptr->getType());
Nuno Lopes0e967e02012-06-21 15:59:53 +000081 Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
82
Joel Galenson8dbcc582018-07-24 15:21:54 +000083 auto SizeRange = SE.getUnsignedRange(SE.getSCEV(Size));
84 auto OffsetRange = SE.getUnsignedRange(SE.getSCEV(Offset));
85 auto NeededSizeRange = SE.getUnsignedRange(SE.getSCEV(NeededSizeVal));
86
Nuno Lopes7d000612012-05-31 22:45:40 +000087 // three checks are required to ensure safety:
88 // . Offset >= 0 (since the offset is given from the base ptr)
89 // . Size >= Offset (unsigned)
90 // . Size - Offset >= NeededSize (unsigned)
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000091 //
92 // optimization: if Size >= 0 (signed), skip 1st check
Nuno Lopes7d000612012-05-31 22:45:40 +000093 // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
Chandler Carruth1594fee2017-11-14 01:13:59 +000094 Value *ObjSize = IRB.CreateSub(Size, Offset);
Joel Galenson8dbcc582018-07-24 15:21:54 +000095 Value *Cmp2 = SizeRange.getUnsignedMin().uge(OffsetRange.getUnsignedMax())
96 ? ConstantInt::getFalse(Ptr->getContext())
97 : IRB.CreateICmpULT(Size, Offset);
98 Value *Cmp3 = SizeRange.sub(OffsetRange)
99 .getUnsignedMin()
100 .uge(NeededSizeRange.getUnsignedMax())
101 ? ConstantInt::getFalse(Ptr->getContext())
102 : IRB.CreateICmpULT(ObjSize, NeededSizeVal);
Chandler Carruth1594fee2017-11-14 01:13:59 +0000103 Value *Or = IRB.CreateOr(Cmp2, Cmp3);
Joel Galenson8dbcc582018-07-24 15:21:54 +0000104 if ((!SizeCI || SizeCI->getValue().slt(0)) &&
105 !SizeRange.getSignedMin().isNonNegative()) {
Chandler Carruth1594fee2017-11-14 01:13:59 +0000106 Value *Cmp1 = IRB.CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
107 Or = IRB.CreateOr(Cmp1, Or);
Nuno Lopes1e8dffd2012-07-03 17:30:18 +0000108 }
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000109
Chandler Carruth3f0e0562017-10-18 22:42:36 +0000110 // check if the comparison is always false
111 ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or);
112 if (C) {
113 ++ChecksSkipped;
114 // If non-zero, nothing to do.
115 if (!C->getZExtValue())
116 return true;
117 }
118 ++ChecksAdded;
119
Chandler Carruth1594fee2017-11-14 01:13:59 +0000120 BasicBlock::iterator SplitI = IRB.GetInsertPoint();
Chandler Carruth3f0e0562017-10-18 22:42:36 +0000121 BasicBlock *OldBB = SplitI->getParent();
122 BasicBlock *Cont = OldBB->splitBasicBlock(SplitI);
123 OldBB->getTerminator()->eraseFromParent();
124
125 if (C) {
126 // If we have a constant zero, unconditionally branch.
127 // FIXME: We should really handle this differently to bypass the splitting
128 // the block.
Chandler Carruth1594fee2017-11-14 01:13:59 +0000129 BranchInst::Create(GetTrapBB(IRB), OldBB);
Chandler Carruth3f0e0562017-10-18 22:42:36 +0000130 return true;
131 }
132
133 // Create the conditional branch.
Chandler Carruth1594fee2017-11-14 01:13:59 +0000134 BranchInst::Create(GetTrapBB(IRB), Cont, Or, OldBB);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000135 return true;
136}
137
Joel Galenson8dbcc582018-07-24 15:21:54 +0000138static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI,
139 ScalarEvolution &SE) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000140 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth1594fee2017-11-14 01:13:59 +0000141 ObjectSizeOffsetEvaluator ObjSizeEval(DL, &TLI, F.getContext(),
Nuno Lopes340b0462013-10-24 09:17:24 +0000142 /*RoundToAlign=*/true);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000143
144 // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
145 // touching instructions
Eugene Zelenkobff0ef02017-10-19 22:07:16 +0000146 std::vector<Instruction *> WorkList;
Chandler Carruth1594fee2017-11-14 01:13:59 +0000147 for (Instruction &I : instructions(F)) {
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000148 if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
149 isa<AtomicRMWInst>(I))
Chandler Carruth1594fee2017-11-14 01:13:59 +0000150 WorkList.push_back(&I);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000151 }
152
Chandler Carruth1594fee2017-11-14 01:13:59 +0000153 // Create a trapping basic block on demand using a callback. Depending on
154 // flags, this will either create a single block for the entire function or
155 // will create a fresh block every time it is called.
156 BasicBlock *TrapBB = nullptr;
157 auto GetTrapBB = [&TrapBB](BuilderTy &IRB) {
158 if (TrapBB && SingleTrapBB)
159 return TrapBB;
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000160
Chandler Carruth1594fee2017-11-14 01:13:59 +0000161 Function *Fn = IRB.GetInsertBlock()->getParent();
162 // FIXME: This debug location doesn't make a lot of sense in the
163 // `SingleTrapBB` case.
164 auto DebugLoc = IRB.getCurrentDebugLocation();
165 IRBuilder<>::InsertPointGuard Guard(IRB);
166 TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
167 IRB.SetInsertPoint(TrapBB);
168
169 auto *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
170 CallInst *TrapCall = IRB.CreateCall(F, {});
171 TrapCall->setDoesNotReturn();
172 TrapCall->setDoesNotThrow();
173 TrapCall->setDebugLoc(DebugLoc);
174 IRB.CreateUnreachable();
175
176 return TrapBB;
177 };
178
179 bool MadeChange = false;
180 for (Instruction *Inst : WorkList) {
181 BuilderTy IRB(Inst->getParent(), BasicBlock::iterator(Inst), TargetFolder(DL));
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000182 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chandler Carruth1594fee2017-11-14 01:13:59 +0000183 MadeChange |= instrumentMemAccess(LI->getPointerOperand(), LI, DL, TLI,
Joel Galenson8dbcc582018-07-24 15:21:54 +0000184 ObjSizeEval, IRB, GetTrapBB, SE);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000185 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000186 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000187 instrumentMemAccess(SI->getPointerOperand(), SI->getValueOperand(),
Joel Galenson8dbcc582018-07-24 15:21:54 +0000188 DL, TLI, ObjSizeEval, IRB, GetTrapBB, SE);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000189 } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000190 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000191 instrumentMemAccess(AI->getPointerOperand(), AI->getCompareOperand(),
Joel Galenson8dbcc582018-07-24 15:21:54 +0000192 DL, TLI, ObjSizeEval, IRB, GetTrapBB, SE);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000193 } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000194 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000195 instrumentMemAccess(AI->getPointerOperand(), AI->getValOperand(), DL,
Joel Galenson8dbcc582018-07-24 15:21:54 +0000196 TLI, ObjSizeEval, IRB, GetTrapBB, SE);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000197 } else {
198 llvm_unreachable("unknown Instruction type");
199 }
200 }
201 return MadeChange;
202}
203
Chandler Carruth00a301d2017-11-14 01:30:04 +0000204PreservedAnalyses BoundsCheckingPass::run(Function &F, FunctionAnalysisManager &AM) {
205 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
Joel Galenson8dbcc582018-07-24 15:21:54 +0000206 auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
Chandler Carruth00a301d2017-11-14 01:30:04 +0000207
Joel Galenson8dbcc582018-07-24 15:21:54 +0000208 if (!addBoundsChecking(F, TLI, SE))
Chandler Carruth00a301d2017-11-14 01:30:04 +0000209 return PreservedAnalyses::all();
210
211 return PreservedAnalyses::none();
212}
213
214namespace {
215struct BoundsCheckingLegacyPass : public FunctionPass {
216 static char ID;
217
218 BoundsCheckingLegacyPass() : FunctionPass(ID) {
219 initializeBoundsCheckingLegacyPassPass(*PassRegistry::getPassRegistry());
220 }
221
222 bool runOnFunction(Function &F) override {
223 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Joel Galenson8dbcc582018-07-24 15:21:54 +0000224 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
225 return addBoundsChecking(F, TLI, SE);
Chandler Carruth00a301d2017-11-14 01:30:04 +0000226 }
227
228 void getAnalysisUsage(AnalysisUsage &AU) const override {
229 AU.addRequired<TargetLibraryInfoWrapperPass>();
Joel Galenson8dbcc582018-07-24 15:21:54 +0000230 AU.addRequired<ScalarEvolutionWrapperPass>();
Chandler Carruth00a301d2017-11-14 01:30:04 +0000231 }
232};
233} // namespace
234
235char BoundsCheckingLegacyPass::ID = 0;
236INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking",
237 "Run-time bounds checking", false, false)
238INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
239INITIALIZE_PASS_END(BoundsCheckingLegacyPass, "bounds-checking",
240 "Run-time bounds checking", false, false)
241
242FunctionPass *llvm::createBoundsCheckingLegacyPass() {
243 return new BoundsCheckingLegacyPass();
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000244}