blob: afff81cdc594b1a418510a9f84de083be0350bdf [file] [log] [blame]
Kostya Serebryany29a18dc2014-11-11 22:14:37 +00001//===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
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//===----------------------------------------------------------------------===//
9//
10// Coverage instrumentation that works with AddressSanitizer
11// and potentially with other Sanitizers.
12//
Kostya Serebryany9fdeb372014-12-23 22:32:17 +000013// We create a Guard variable with the same linkage
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000014// as the function and inject this code into the entry block (CoverageLevel=1)
15// or all blocks (CoverageLevel>=2):
Kostya Serebryany9fdeb372014-12-23 22:32:17 +000016// if (Guard < 0) {
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +000017// __sanitizer_cov(&Guard);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000018// }
19// The accesses to Guard are atomic. The rest of the logic is
20// in __sanitizer_cov (it's fine to call it more than once).
21//
22// With CoverageLevel>=3 we also split critical edges this effectively
23// instrumenting all edges.
24//
25// CoverageLevel>=4 add indirect call profiling implented as a function call.
26//
27// This coverage implementation provides very limited data:
28// it only tells if a given function (block) was ever executed. No counters.
29// But for many use cases this is what we need and the added slowdown small.
30//
31//===----------------------------------------------------------------------===//
32
33#include "llvm/Transforms/Instrumentation.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/SmallVector.h"
36#include "llvm/IR/CallSite.h"
37#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/Function.h"
39#include "llvm/IR/IRBuilder.h"
Kostya Serebryany73762942014-12-16 21:24:15 +000040#include "llvm/IR/InlineAsm.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000041#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/MDBuilder.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/Type.h"
45#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/raw_ostream.h"
48#include "llvm/Transforms/Scalar.h"
49#include "llvm/Transforms/Utils/BasicBlockUtils.h"
50#include "llvm/Transforms/Utils/ModuleUtils.h"
51
52using namespace llvm;
53
54#define DEBUG_TYPE "sancov"
55
56static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
57static const char *const kSanCovName = "__sanitizer_cov";
58static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
Kostya Serebryanycb45b122014-11-19 00:22:58 +000059static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
60static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000061static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
Evgeniy Stepanov3fdfc7b2015-01-27 15:01:22 +000062static const uint64_t kSanCtorAndDtorPriority = 2;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000063
64static cl::opt<int> ClCoverageLevel("sanitizer-coverage-level",
65 cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
66 "3: all blocks and critical edges, "
67 "4: above plus indirect calls"),
68 cl::Hidden, cl::init(0));
69
70static cl::opt<int> ClCoverageBlockThreshold(
71 "sanitizer-coverage-block-threshold",
72 cl::desc("Add coverage instrumentation only to the entry block if there "
73 "are more than this number of blocks."),
74 cl::Hidden, cl::init(1500));
75
Kostya Serebryanycb45b122014-11-19 00:22:58 +000076static cl::opt<bool>
77 ClExperimentalTracing("sanitizer-coverage-experimental-tracing",
78 cl::desc("Experimental basic-block tracing: insert "
79 "callbacks at every basic block"),
80 cl::Hidden, cl::init(false));
81
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000082namespace {
83
84class SanitizerCoverageModule : public ModulePass {
85 public:
Kostya Serebryanycb45b122014-11-19 00:22:58 +000086 SanitizerCoverageModule(int CoverageLevel = 0)
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000087 : ModulePass(ID),
Kostya Serebryanycb45b122014-11-19 00:22:58 +000088 CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000089 bool runOnModule(Module &M) override;
90 bool runOnFunction(Function &F);
91 static char ID; // Pass identification, replacement for typeid
92 const char *getPassName() const override {
93 return "SanitizerCoverageModule";
94 }
95
96 void getAnalysisUsage(AnalysisUsage &AU) const override {
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000097 AU.addRequired<DataLayoutPass>();
98 }
99
100 private:
101 void InjectCoverageForIndirectCalls(Function &F,
102 ArrayRef<Instruction *> IndirCalls);
103 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
104 ArrayRef<Instruction *> IndirCalls);
105 void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
106 Function *SanCovFunction;
107 Function *SanCovIndirCallFunction;
108 Function *SanCovModuleInit;
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000109 Function *SanCovTraceEnter, *SanCovTraceBB;
Kostya Serebryany73762942014-12-16 21:24:15 +0000110 InlineAsm *EmptyAsm;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000111 Type *IntptrTy;
112 LLVMContext *C;
113
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000114 GlobalVariable *GuardArray;
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000115
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000116 int CoverageLevel;
117};
118
119} // namespace
120
121static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
122 if (Function *F = dyn_cast<Function>(FuncOrBitcast))
123 return F;
124 std::string Err;
125 raw_string_ostream Stream(Err);
126 Stream << "SanitizerCoverage interface function redefined: "
127 << *FuncOrBitcast;
128 report_fatal_error(Err);
129}
130
131bool SanitizerCoverageModule::runOnModule(Module &M) {
132 if (!CoverageLevel) return false;
133 C = &(M.getContext());
134 DataLayoutPass *DLP = &getAnalysis<DataLayoutPass>();
135 IntptrTy = Type::getIntNTy(*C, DLP->getDataLayout().getPointerSizeInBits());
136 Type *VoidTy = Type::getVoidTy(*C);
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000137 IRBuilder<> IRB(*C);
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000138 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000139
140 Function *CtorFunc =
141 Function::Create(FunctionType::get(VoidTy, false),
142 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
143 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
144 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
145
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000146 SanCovFunction = checkInterfaceFunction(
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000147 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000148 SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction(
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000149 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000150 SanCovModuleInit = checkInterfaceFunction(
151 M.getOrInsertFunction(kSanCovModuleInitName, Type::getVoidTy(*C),
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000152 Int32PtrTy, IntptrTy, nullptr));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000153 SanCovModuleInit->setLinkage(Function::ExternalLinkage);
Kostya Serebryany73762942014-12-16 21:24:15 +0000154 // We insert an empty inline asm after cov callbacks to avoid callback merge.
155 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
156 StringRef(""), StringRef(""),
157 /*hasSideEffects=*/true);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000158
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000159 if (ClExperimentalTracing) {
160 SanCovTraceEnter = checkInterfaceFunction(
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000161 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000162 SanCovTraceBB = checkInterfaceFunction(
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000163 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr));
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000164 }
165
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000166 // At this point we create a dummy array of guards because we don't
167 // know how many elements we will need.
168 Type *Int32Ty = IRB.getInt32Ty();
169 GuardArray =
170 new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
171 nullptr, "__sancov_gen_cov_tmp");
172
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000173 for (auto &F : M)
174 runOnFunction(F);
175
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000176 // Now we know how many elements we need. Create an array of guards
177 // with one extra element at the beginning for the size.
178 Type *Int32ArrayNTy =
179 ArrayType::get(Int32Ty, SanCovFunction->getNumUses() + 1);
180 GlobalVariable *RealGuardArray = new GlobalVariable(
181 M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
182 Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
183
184 // Replace the dummy array with the real one.
185 GuardArray->replaceAllUsesWith(
186 IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
187 GuardArray->eraseFromParent();
188
189 // Call __sanitizer_cov_module_init
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000190 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000191 IRB.CreateCall2(SanCovModuleInit,
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000192 IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
193 ConstantInt::get(IntptrTy, SanCovFunction->getNumUses()));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000194 return true;
195}
196
197bool SanitizerCoverageModule::runOnFunction(Function &F) {
198 if (F.empty()) return false;
Kostya Serebryanyfea4fb42014-12-17 21:50:04 +0000199 if (F.getName().find(".module_ctor") != std::string::npos)
200 return false; // Should not instrument sanitizer init functions.
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000201 if (CoverageLevel >= 3)
Chandler Carruth37df2cf2015-01-19 12:09:11 +0000202 SplitAllCriticalEdges(F);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000203 SmallVector<Instruction*, 8> IndirCalls;
204 SmallVector<BasicBlock*, 16> AllBlocks;
205 for (auto &BB : F) {
206 AllBlocks.push_back(&BB);
207 if (CoverageLevel >= 4)
208 for (auto &Inst : BB) {
209 CallSite CS(&Inst);
210 if (CS && !CS.getCalledFunction())
211 IndirCalls.push_back(&Inst);
212 }
213 }
214 InjectCoverage(F, AllBlocks, IndirCalls);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000215 return true;
216}
217
218bool
219SanitizerCoverageModule::InjectCoverage(Function &F,
220 ArrayRef<BasicBlock *> AllBlocks,
221 ArrayRef<Instruction *> IndirCalls) {
222 if (!CoverageLevel) return false;
223
224 if (CoverageLevel == 1 ||
225 (unsigned)ClCoverageBlockThreshold < AllBlocks.size()) {
226 InjectCoverageAtBlock(F, F.getEntryBlock());
227 } else {
228 for (auto BB : AllBlocks)
229 InjectCoverageAtBlock(F, *BB);
230 }
231 InjectCoverageForIndirectCalls(F, IndirCalls);
232 return true;
233}
234
235// On every indirect call we call a run-time function
236// __sanitizer_cov_indir_call* with two parameters:
237// - callee address,
238// - global cache array that contains kCacheSize pointers (zero-initialized).
239// The cache is used to speed up recording the caller-callee pairs.
240// The address of the caller is passed implicitly via caller PC.
241// kCacheSize is encoded in the name of the run-time function.
242void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
243 Function &F, ArrayRef<Instruction *> IndirCalls) {
244 if (IndirCalls.empty()) return;
245 const int kCacheSize = 16;
246 const int kCacheAlignment = 64; // Align for better performance.
247 Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
248 for (auto I : IndirCalls) {
249 IRBuilder<> IRB(I);
250 CallSite CS(I);
251 Value *Callee = CS.getCalledValue();
252 if (dyn_cast<InlineAsm>(Callee)) continue;
253 GlobalVariable *CalleeCache = new GlobalVariable(
254 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
255 Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
256 CalleeCache->setAlignment(kCacheAlignment);
257 IRB.CreateCall2(SanCovIndirCallFunction,
258 IRB.CreatePointerCast(Callee, IntptrTy),
259 IRB.CreatePointerCast(CalleeCache, IntptrTy));
260 }
261}
262
263void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
264 BasicBlock &BB) {
265 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
266 // Skip static allocas at the top of the entry block so they don't become
267 // dynamic when we split the block. If we used our optimized stack layout,
268 // then there will only be one alloca and it will come first.
269 for (; IP != BE; ++IP) {
270 AllocaInst *AI = dyn_cast<AllocaInst>(IP);
271 if (!AI || !AI->isStaticAlloca())
272 break;
273 }
274
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000275 bool IsEntryBB = &BB == &F.getEntryBlock();
276 DebugLoc EntryLoc =
277 IsEntryBB ? IP->getDebugLoc().getFnDebugLoc(*C) : IP->getDebugLoc();
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000278 IRBuilder<> IRB(IP);
279 IRB.SetCurrentDebugLocation(EntryLoc);
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000280 SmallVector<Value *, 1> Indices;
281 Value *GuardP = IRB.CreateAdd(
282 IRB.CreatePointerCast(GuardArray, IntptrTy),
283 ConstantInt::get(IntptrTy, (1 + SanCovFunction->getNumUses()) * 4));
284 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
285 GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
286 LoadInst *Load = IRB.CreateLoad(GuardP);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000287 Load->setAtomic(Monotonic);
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000288 Load->setAlignment(4);
Kostya Serebryany543f3db2014-12-03 23:28:26 +0000289 Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000290 MDNode::get(*C, None));
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000291 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000292 Instruction *Ins = SplitBlockAndInsertIfThen(
293 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
294 IRB.SetInsertPoint(Ins);
295 IRB.SetCurrentDebugLocation(EntryLoc);
296 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
Kostya Serebryanyaa185bf2014-12-30 19:29:28 +0000297 IRB.CreateCall(SanCovFunction, GuardP);
Kostya Serebryany73762942014-12-16 21:24:15 +0000298 IRB.CreateCall(EmptyAsm); // Avoids callback merge.
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000299
300 if (ClExperimentalTracing) {
301 // Experimental support for tracing.
302 // Insert a callback with the same guard variable as used for coverage.
303 IRB.SetInsertPoint(IP);
304 IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP);
305 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000306}
307
308char SanitizerCoverageModule::ID = 0;
309INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
310 "SanitizerCoverage: TODO."
311 "ModulePass", false, false)
312ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
313 return new SanitizerCoverageModule(CoverageLevel);
314}