blob: 87b3019c91086155f75a89031770c1d3cc9b4ded [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"
Chandler Carruth452a0072014-03-04 11:59:06 +000014#include "llvm/Analysis/TargetFolder.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000015#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000016#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000019#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/IRBuilder.h"
Chandler Carruth83948572014-03-04 10:30:26 +000021#include "llvm/IR/InstIterator.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000022#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/Instruction.h"
24#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Intrinsics.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000026#include "llvm/IR/Value.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000027#include "llvm/Pass.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000028#include "llvm/Support/Casting.h"
Nuno Lopes288e86ff62012-05-31 22:58:48 +000029#include "llvm/Support/CommandLine.h"
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000030#include "llvm/Support/Debug.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000032#include "llvm/Support/raw_ostream.h"
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000033#include <cstdint>
34#include <vector>
35
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000036using namespace llvm;
37
Chandler Carruth964daaa2014-04-22 02:55:47 +000038#define DEBUG_TYPE "bounds-checking"
39
Nuno Lopes0e967e02012-06-21 15:59:53 +000040static cl::opt<bool> SingleTrapBB("bounds-checking-single-trap",
41 cl::desc("Use one trap block per function"));
Nuno Lopes288e86ff62012-05-31 22:58:48 +000042
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000043STATISTIC(ChecksAdded, "Bounds checks added");
44STATISTIC(ChecksSkipped, "Bounds checks skipped");
45STATISTIC(ChecksUnable, "Bounds checks unable to add");
46
Eugene Zelenkobff0ef02017-10-19 22:07:16 +000047using BuilderTy = IRBuilder<TargetFolder>;
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000048
Chandler Carruth1594fee2017-11-14 01:13:59 +000049/// Adds run-time bounds checks to memory accessing instructions.
50///
51/// \p Ptr is the pointer that will be read/written, and \p InstVal is either
52/// the result from the load or the value being stored. It is used to determine
53/// the size of memory block that is touched.
54///
55/// \p GetTrapBB is a callable that returns the trap BB to use on failure.
56///
Nuno Lopes59e9df72012-05-22 22:02:19 +000057/// Returns true if any change was made to the IR, false otherwise.
Chandler Carruth1594fee2017-11-14 01:13:59 +000058template <typename GetTrapBBT>
59static bool instrumentMemAccess(Value *Ptr, Value *InstVal,
60 const DataLayout &DL, TargetLibraryInfo &TLI,
61 ObjectSizeOffsetEvaluator &ObjSizeEval,
62 BuilderTy &IRB,
63 GetTrapBBT GetTrapBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000064 uint64_t NeededSize = DL.getTypeStoreSize(InstVal->getType());
Nicola Zaghend34e60c2018-05-14 12:53:11 +000065 LLVM_DEBUG(dbgs() << "Instrument " << *Ptr << " for " << Twine(NeededSize)
66 << " bytes\n");
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000067
Chandler Carruth1594fee2017-11-14 01:13:59 +000068 SizeOffsetEvalType SizeOffset = ObjSizeEval.compute(Ptr);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000069
Chandler Carruth1594fee2017-11-14 01:13:59 +000070 if (!ObjSizeEval.bothKnown(SizeOffset)) {
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000071 ++ChecksUnable;
72 return false;
73 }
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000074
Nuno Lopes0e967e02012-06-21 15:59:53 +000075 Value *Size = SizeOffset.first;
76 Value *Offset = SizeOffset.second;
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000077 ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
Nuno Lopes0e967e02012-06-21 15:59:53 +000078
Mehdi Aminia28d91d2015-03-10 02:37:25 +000079 Type *IntTy = DL.getIntPtrType(Ptr->getType());
Nuno Lopes0e967e02012-06-21 15:59:53 +000080 Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
81
Nuno Lopes7d000612012-05-31 22:45:40 +000082 // three checks are required to ensure safety:
83 // . Offset >= 0 (since the offset is given from the base ptr)
84 // . Size >= Offset (unsigned)
85 // . Size - Offset >= NeededSize (unsigned)
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000086 //
87 // optimization: if Size >= 0 (signed), skip 1st check
Nuno Lopes7d000612012-05-31 22:45:40 +000088 // FIXME: add NSW/NUW here? -- we dont care if the subtraction overflows
Chandler Carruth1594fee2017-11-14 01:13:59 +000089 Value *ObjSize = IRB.CreateSub(Size, Offset);
90 Value *Cmp2 = IRB.CreateICmpULT(Size, Offset);
91 Value *Cmp3 = IRB.CreateICmpULT(ObjSize, NeededSizeVal);
92 Value *Or = IRB.CreateOr(Cmp2, Cmp3);
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000093 if (!SizeCI || SizeCI->getValue().slt(0)) {
Chandler Carruth1594fee2017-11-14 01:13:59 +000094 Value *Cmp1 = IRB.CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
95 Or = IRB.CreateOr(Cmp1, Or);
Nuno Lopes1e8dffd2012-07-03 17:30:18 +000096 }
Nuno Lopesa2f6cec2012-05-22 17:19:09 +000097
Chandler Carruth3f0e0562017-10-18 22:42:36 +000098 // check if the comparison is always false
99 ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or);
100 if (C) {
101 ++ChecksSkipped;
102 // If non-zero, nothing to do.
103 if (!C->getZExtValue())
104 return true;
105 }
106 ++ChecksAdded;
107
Chandler Carruth1594fee2017-11-14 01:13:59 +0000108 BasicBlock::iterator SplitI = IRB.GetInsertPoint();
Chandler Carruth3f0e0562017-10-18 22:42:36 +0000109 BasicBlock *OldBB = SplitI->getParent();
110 BasicBlock *Cont = OldBB->splitBasicBlock(SplitI);
111 OldBB->getTerminator()->eraseFromParent();
112
113 if (C) {
114 // If we have a constant zero, unconditionally branch.
115 // FIXME: We should really handle this differently to bypass the splitting
116 // the block.
Chandler Carruth1594fee2017-11-14 01:13:59 +0000117 BranchInst::Create(GetTrapBB(IRB), OldBB);
Chandler Carruth3f0e0562017-10-18 22:42:36 +0000118 return true;
119 }
120
121 // Create the conditional branch.
Chandler Carruth1594fee2017-11-14 01:13:59 +0000122 BranchInst::Create(GetTrapBB(IRB), Cont, Or, OldBB);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000123 return true;
124}
125
Chandler Carruth00a301d2017-11-14 01:30:04 +0000126static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000127 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth1594fee2017-11-14 01:13:59 +0000128 ObjectSizeOffsetEvaluator ObjSizeEval(DL, &TLI, F.getContext(),
Nuno Lopes340b0462013-10-24 09:17:24 +0000129 /*RoundToAlign=*/true);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000130
131 // check HANDLE_MEMORY_INST in include/llvm/Instruction.def for memory
132 // touching instructions
Eugene Zelenkobff0ef02017-10-19 22:07:16 +0000133 std::vector<Instruction *> WorkList;
Chandler Carruth1594fee2017-11-14 01:13:59 +0000134 for (Instruction &I : instructions(F)) {
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000135 if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
136 isa<AtomicRMWInst>(I))
Chandler Carruth1594fee2017-11-14 01:13:59 +0000137 WorkList.push_back(&I);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000138 }
139
Chandler Carruth1594fee2017-11-14 01:13:59 +0000140 // Create a trapping basic block on demand using a callback. Depending on
141 // flags, this will either create a single block for the entire function or
142 // will create a fresh block every time it is called.
143 BasicBlock *TrapBB = nullptr;
144 auto GetTrapBB = [&TrapBB](BuilderTy &IRB) {
145 if (TrapBB && SingleTrapBB)
146 return TrapBB;
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000147
Chandler Carruth1594fee2017-11-14 01:13:59 +0000148 Function *Fn = IRB.GetInsertBlock()->getParent();
149 // FIXME: This debug location doesn't make a lot of sense in the
150 // `SingleTrapBB` case.
151 auto DebugLoc = IRB.getCurrentDebugLocation();
152 IRBuilder<>::InsertPointGuard Guard(IRB);
153 TrapBB = BasicBlock::Create(Fn->getContext(), "trap", Fn);
154 IRB.SetInsertPoint(TrapBB);
155
156 auto *F = Intrinsic::getDeclaration(Fn->getParent(), Intrinsic::trap);
157 CallInst *TrapCall = IRB.CreateCall(F, {});
158 TrapCall->setDoesNotReturn();
159 TrapCall->setDoesNotThrow();
160 TrapCall->setDebugLoc(DebugLoc);
161 IRB.CreateUnreachable();
162
163 return TrapBB;
164 };
165
166 bool MadeChange = false;
167 for (Instruction *Inst : WorkList) {
168 BuilderTy IRB(Inst->getParent(), BasicBlock::iterator(Inst), TargetFolder(DL));
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000169 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chandler Carruth1594fee2017-11-14 01:13:59 +0000170 MadeChange |= instrumentMemAccess(LI->getPointerOperand(), LI, DL, TLI,
171 ObjSizeEval, IRB, GetTrapBB);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000172 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000173 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000174 instrumentMemAccess(SI->getPointerOperand(), SI->getValueOperand(),
175 DL, TLI, ObjSizeEval, IRB, GetTrapBB);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000176 } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000177 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000178 instrumentMemAccess(AI->getPointerOperand(), AI->getCompareOperand(),
179 DL, TLI, ObjSizeEval, IRB, GetTrapBB);
Nuno Lopesde8c6fb2012-06-23 00:12:34 +0000180 } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(Inst)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000181 MadeChange |=
Chandler Carruth1594fee2017-11-14 01:13:59 +0000182 instrumentMemAccess(AI->getPointerOperand(), AI->getValOperand(), DL,
183 TLI, ObjSizeEval, IRB, GetTrapBB);
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000184 } else {
185 llvm_unreachable("unknown Instruction type");
186 }
187 }
188 return MadeChange;
189}
190
Chandler Carruth00a301d2017-11-14 01:30:04 +0000191PreservedAnalyses BoundsCheckingPass::run(Function &F, FunctionAnalysisManager &AM) {
192 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
193
194 if (!addBoundsChecking(F, TLI))
195 return PreservedAnalyses::all();
196
197 return PreservedAnalyses::none();
198}
199
200namespace {
201struct BoundsCheckingLegacyPass : public FunctionPass {
202 static char ID;
203
204 BoundsCheckingLegacyPass() : FunctionPass(ID) {
205 initializeBoundsCheckingLegacyPassPass(*PassRegistry::getPassRegistry());
206 }
207
208 bool runOnFunction(Function &F) override {
209 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
210 return addBoundsChecking(F, TLI);
211 }
212
213 void getAnalysisUsage(AnalysisUsage &AU) const override {
214 AU.addRequired<TargetLibraryInfoWrapperPass>();
215 }
216};
217} // namespace
218
219char BoundsCheckingLegacyPass::ID = 0;
220INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking",
221 "Run-time bounds checking", false, false)
222INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
223INITIALIZE_PASS_END(BoundsCheckingLegacyPass, "bounds-checking",
224 "Run-time bounds checking", false, false)
225
226FunctionPass *llvm::createBoundsCheckingLegacyPass() {
227 return new BoundsCheckingLegacyPass();
Nuno Lopesa2f6cec2012-05-22 17:19:09 +0000228}