1 //===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Coverage instrumentation that works with AddressSanitizer
11 // and potentially with other Sanitizers.
13 // We create a Guard variable with the same linkage
14 // as the function and inject this code into the entry block (CoverageLevel=1)
15 // or all blocks (CoverageLevel>=2):
17 // __sanitizer_cov(&Guard);
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).
22 // With CoverageLevel>=3 we also split critical edges this effectively
23 // instrumenting all edges.
25 // CoverageLevel>=4 add indirect call profiling implented as a function call.
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.
31 //===----------------------------------------------------------------------===//
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"
40 #include "llvm/IR/InlineAsm.h"
41 #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"
54 #define DEBUG_TYPE "sancov"
56 static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
57 static const char *const kSanCovName = "__sanitizer_cov";
58 static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
59 static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
60 static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
61 static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
62 static const uint64_t kSanCtorAndDtorPriority = 1;
64 static 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));
70 static 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));
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));
84 class SanitizerCoverageModule : public ModulePass {
86 SanitizerCoverageModule(int CoverageLevel = 0)
88 CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
89 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";
96 void getAnalysisUsage(AnalysisUsage &AU) const override {
97 AU.addRequired<DataLayoutPass>();
101 void InjectCoverageForIndirectCalls(Function &F,
102 ArrayRef<Instruction *> IndirCalls);
103 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
104 ArrayRef<Instruction *> IndirCalls);
105 bool InjectTracing(Function &F, ArrayRef<BasicBlock *> AllBlocks);
106 void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
107 Function *SanCovFunction;
108 Function *SanCovIndirCallFunction;
109 Function *SanCovModuleInit;
110 Function *SanCovTraceEnter, *SanCovTraceBB;
115 SmallVector<Constant *, 16> Guards;
122 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
123 if (Function *F = dyn_cast<Function>(FuncOrBitcast))
126 raw_string_ostream Stream(Err);
127 Stream << "SanitizerCoverage interface function redefined: "
129 report_fatal_error(Err);
132 bool SanitizerCoverageModule::runOnModule(Module &M) {
133 if (!CoverageLevel) return false;
134 C = &(M.getContext());
135 DataLayoutPass *DLP = &getAnalysis<DataLayoutPass>();
136 IntptrTy = Type::getIntNTy(*C, DLP->getDataLayout().getPointerSizeInBits());
137 Type *VoidTy = Type::getVoidTy(*C);
139 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
140 Type *Int32PtrPtrTy = PointerType::getUnqual(Int32PtrTy);
143 Function::Create(FunctionType::get(VoidTy, false),
144 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
145 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
146 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
148 SanCovFunction = checkInterfaceFunction(
149 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
150 SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction(
151 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
152 SanCovModuleInit = checkInterfaceFunction(
153 M.getOrInsertFunction(kSanCovModuleInitName, Type::getVoidTy(*C),
154 Int32PtrPtrTy, IntptrTy, nullptr));
155 SanCovModuleInit->setLinkage(Function::ExternalLinkage);
156 // We insert an empty inline asm after cov callbacks to avoid callback merge.
157 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
158 StringRef(""), StringRef(""),
159 /*hasSideEffects=*/true);
161 if (ClExperimentalTracing) {
162 SanCovTraceEnter = checkInterfaceFunction(
163 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, IntptrTy, nullptr));
164 SanCovTraceBB = checkInterfaceFunction(
165 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, IntptrTy, nullptr));
171 ArrayType *ArrayOfInt32PtrTy = ArrayType::get(Int32PtrTy, Guards.size());
172 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
173 GlobalVariable *AllGuards = new GlobalVariable(
174 M, ArrayOfInt32PtrTy, false, GlobalVariable::InternalLinkage,
175 ConstantArray::get(ArrayOfInt32PtrTy, Guards), "");
176 assert(SanCovFunction->getNumUses() == Guards.size());
177 IRB.CreateCall2(SanCovModuleInit,
178 IRB.CreatePointerCast(AllGuards, Int32PtrPtrTy),
179 ConstantInt::get(IntptrTy, Guards.size()));
183 bool SanitizerCoverageModule::runOnFunction(Function &F) {
184 if (F.empty()) return false;
185 if (F.getName().find(".module_ctor") != std::string::npos)
186 return false; // Should not instrument sanitizer init functions.
187 if (CoverageLevel >= 3)
188 SplitAllCriticalEdges(F, this);
189 SmallVector<Instruction*, 8> IndirCalls;
190 SmallVector<BasicBlock*, 16> AllBlocks;
192 AllBlocks.push_back(&BB);
193 if (CoverageLevel >= 4)
194 for (auto &Inst : BB) {
196 if (CS && !CS.getCalledFunction())
197 IndirCalls.push_back(&Inst);
200 InjectCoverage(F, AllBlocks, IndirCalls);
201 InjectTracing(F, AllBlocks);
205 // Experimental support for tracing.
206 // Basicaly, insert a callback at the beginning of every basic block.
207 // Every callback gets a pointer to a uniqie global for internal storage.
208 bool SanitizerCoverageModule::InjectTracing(Function &F,
209 ArrayRef<BasicBlock *> AllBlocks) {
210 if (!ClExperimentalTracing) return false;
211 Type *Ty = ArrayType::get(IntptrTy, 1); // May need to use more words later.
212 for (auto BB : AllBlocks) {
213 IRBuilder<> IRB(BB->getFirstInsertionPt());
214 GlobalVariable *TraceCache = new GlobalVariable(
215 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
216 Constant::getNullValue(Ty), "__sancov_gen_trace_cache");
217 IRB.CreateCall(&F.getEntryBlock() == BB ? SanCovTraceEnter : SanCovTraceBB,
218 IRB.CreatePointerCast(TraceCache, IntptrTy));
224 SanitizerCoverageModule::InjectCoverage(Function &F,
225 ArrayRef<BasicBlock *> AllBlocks,
226 ArrayRef<Instruction *> IndirCalls) {
227 if (!CoverageLevel) return false;
229 if (CoverageLevel == 1 ||
230 (unsigned)ClCoverageBlockThreshold < AllBlocks.size()) {
231 InjectCoverageAtBlock(F, F.getEntryBlock());
233 for (auto BB : AllBlocks)
234 InjectCoverageAtBlock(F, *BB);
236 InjectCoverageForIndirectCalls(F, IndirCalls);
240 // On every indirect call we call a run-time function
241 // __sanitizer_cov_indir_call* with two parameters:
243 // - global cache array that contains kCacheSize pointers (zero-initialized).
244 // The cache is used to speed up recording the caller-callee pairs.
245 // The address of the caller is passed implicitly via caller PC.
246 // kCacheSize is encoded in the name of the run-time function.
247 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
248 Function &F, ArrayRef<Instruction *> IndirCalls) {
249 if (IndirCalls.empty()) return;
250 const int kCacheSize = 16;
251 const int kCacheAlignment = 64; // Align for better performance.
252 Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
253 for (auto I : IndirCalls) {
256 Value *Callee = CS.getCalledValue();
257 if (dyn_cast<InlineAsm>(Callee)) continue;
258 GlobalVariable *CalleeCache = new GlobalVariable(
259 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
260 Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
261 CalleeCache->setAlignment(kCacheAlignment);
262 IRB.CreateCall2(SanCovIndirCallFunction,
263 IRB.CreatePointerCast(Callee, IntptrTy),
264 IRB.CreatePointerCast(CalleeCache, IntptrTy));
268 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
270 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
271 // Skip static allocas at the top of the entry block so they don't become
272 // dynamic when we split the block. If we used our optimized stack layout,
273 // then there will only be one alloca and it will come first.
274 for (; IP != BE; ++IP) {
275 AllocaInst *AI = dyn_cast<AllocaInst>(IP);
276 if (!AI || !AI->isStaticAlloca())
280 DebugLoc EntryLoc = &BB == &F.getEntryBlock()
281 ? IP->getDebugLoc().getFnDebugLoc(*C)
284 IRB.SetCurrentDebugLocation(EntryLoc);
285 Type *Int32Ty = IRB.getInt32Ty();
286 GlobalVariable *Guard = new GlobalVariable(
287 *F.getParent(), Int32Ty, false, GlobalValue::PrivateLinkage,
288 Constant::getNullValue(Int32Ty), "__sancov_gen_cov_" + F.getName());
289 LoadInst *Load = IRB.CreateLoad(Guard);
290 Load->setAtomic(Monotonic);
291 Load->setAlignment(4);
292 Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
293 MDNode::get(*C, None));
294 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Int32Ty), Load);
295 Instruction *Ins = SplitBlockAndInsertIfThen(
296 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
297 IRB.SetInsertPoint(Ins);
298 IRB.SetCurrentDebugLocation(EntryLoc);
299 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
300 IRB.CreateCall(SanCovFunction, Guard);
301 IRB.CreateCall(EmptyAsm); // Avoids callback merge.
302 Guards.push_back(Guard); // Save the guard for later.
305 char SanitizerCoverageModule::ID = 0;
306 INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
307 "SanitizerCoverage: TODO."
308 "ModulePass", false, false)
309 ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
310 return new SanitizerCoverageModule(CoverageLevel);