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 (SCK_Function)
15 // or all blocks (SCK_BB):
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 SCK_Edge we also split critical edges this effectively
23 // instrumenting all edges.
25 // This coverage implementation provides very limited data:
26 // it only tells if a given function (block) was ever executed. No counters.
27 // But for many use cases this is what we need and the added slowdown small.
29 //===----------------------------------------------------------------------===//
31 #include "llvm/Transforms/Instrumentation.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/IR/CallSite.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/IRBuilder.h"
38 #include "llvm/IR/InlineAsm.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/IR/MDBuilder.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Transforms/Scalar.h"
47 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
48 #include "llvm/Transforms/Utils/ModuleUtils.h"
52 #define DEBUG_TYPE "sancov"
54 static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
55 static const char *const kSanCovName = "__sanitizer_cov";
56 static const char *const kSanCovWithCheckName = "__sanitizer_cov_with_check";
57 static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
58 static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
59 static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
60 static const char *const kSanCovTraceCmp = "__sanitizer_cov_trace_cmp";
61 static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
62 static const uint64_t kSanCtorAndDtorPriority = 2;
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<unsigned> ClCoverageBlockThreshold(
71 "sanitizer-coverage-block-threshold",
72 cl::desc("Use a callback with a guard check inside it if there are"
73 " more than this number of blocks."),
74 cl::Hidden, cl::init(500));
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));
83 ClExperimentalCMPTracing("sanitizer-coverage-experimental-trace-compares",
84 cl::desc("Experimental tracing of CMP and similar "
86 cl::Hidden, cl::init(false));
88 // Experimental 8-bit counters used as an additional search heuristic during
89 // coverage-guided fuzzing.
90 // The counters are not thread-friendly:
91 // - contention on these counters may cause significant slowdown;
92 // - the counter updates are racy and the results may be inaccurate.
93 // They are also inaccurate due to 8-bit integer overflow.
94 static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
95 cl::desc("Experimental 8-bit counters"),
96 cl::Hidden, cl::init(false));
100 SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
101 SanitizerCoverageOptions Res;
102 switch (LegacyCoverageLevel) {
104 Res.CoverageType = SanitizerCoverageOptions::SCK_None;
107 Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
110 Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
113 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
116 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
117 Res.IndirectCalls = true;
123 SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
124 // Sets CoverageType and IndirectCalls.
125 SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
126 Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
127 Options.IndirectCalls |= CLOpts.IndirectCalls;
128 Options.TraceBB |= ClExperimentalTracing;
129 Options.TraceCmp |= ClExperimentalCMPTracing;
130 Options.Use8bitCounters |= ClUse8bitCounters;
134 class SanitizerCoverageModule : public ModulePass {
136 SanitizerCoverageModule(
137 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
138 : ModulePass(ID), Options(OverrideFromCL(Options)) {}
139 bool runOnModule(Module &M) override;
140 bool runOnFunction(Function &F);
141 static char ID; // Pass identification, replacement for typeid
142 const char *getPassName() const override {
143 return "SanitizerCoverageModule";
147 void InjectCoverageForIndirectCalls(Function &F,
148 ArrayRef<Instruction *> IndirCalls);
149 void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
150 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks);
151 void SetNoSanitizeMetadata(Instruction *I);
152 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, bool UseCalls);
153 unsigned NumberOfInstrumentedBlocks() {
154 return SanCovFunction->getNumUses() + SanCovWithCheckFunction->getNumUses();
156 Function *SanCovFunction;
157 Function *SanCovWithCheckFunction;
158 Function *SanCovIndirCallFunction;
159 Function *SanCovModuleInit;
160 Function *SanCovTraceEnter, *SanCovTraceBB;
161 Function *SanCovTraceCmpFunction;
163 Type *IntptrTy, *Int64Ty;
165 const DataLayout *DL;
167 GlobalVariable *GuardArray;
168 GlobalVariable *EightBitCounterArray;
170 SanitizerCoverageOptions Options;
175 bool SanitizerCoverageModule::runOnModule(Module &M) {
176 if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
178 C = &(M.getContext());
179 DL = &M.getDataLayout();
180 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
181 Type *VoidTy = Type::getVoidTy(*C);
183 Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
184 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
185 Int64Ty = IRB.getInt64Ty();
188 Function::Create(FunctionType::get(VoidTy, false),
189 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
190 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
191 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
193 SanCovFunction = checkSanitizerInterfaceFunction(
194 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
195 SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
196 M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
197 SanCovIndirCallFunction =
198 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
199 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
200 SanCovTraceCmpFunction =
201 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
202 kSanCovTraceCmp, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr));
204 SanCovModuleInit = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
205 kSanCovModuleInitName, VoidTy, Int32PtrTy, IntptrTy,
206 Int8PtrTy, Int8PtrTy, nullptr));
207 SanCovModuleInit->setLinkage(Function::ExternalLinkage);
208 // We insert an empty inline asm after cov callbacks to avoid callback merge.
209 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
210 StringRef(""), StringRef(""),
211 /*hasSideEffects=*/true);
213 if (Options.TraceBB) {
214 SanCovTraceEnter = checkSanitizerInterfaceFunction(
215 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
216 SanCovTraceBB = checkSanitizerInterfaceFunction(
217 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr));
220 // At this point we create a dummy array of guards because we don't
221 // know how many elements we will need.
222 Type *Int32Ty = IRB.getInt32Ty();
223 Type *Int8Ty = IRB.getInt8Ty();
226 new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
227 nullptr, "__sancov_gen_cov_tmp");
228 if (Options.Use8bitCounters)
229 EightBitCounterArray =
230 new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
231 nullptr, "__sancov_gen_cov_tmp");
236 auto N = NumberOfInstrumentedBlocks();
238 // Now we know how many elements we need. Create an array of guards
239 // with one extra element at the beginning for the size.
240 Type *Int32ArrayNTy = ArrayType::get(Int32Ty, N + 1);
241 GlobalVariable *RealGuardArray = new GlobalVariable(
242 M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
243 Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
246 // Replace the dummy array with the real one.
247 GuardArray->replaceAllUsesWith(
248 IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
249 GuardArray->eraseFromParent();
251 GlobalVariable *RealEightBitCounterArray;
252 if (Options.Use8bitCounters) {
253 // Make sure the array is 16-aligned.
254 static const int kCounterAlignment = 16;
256 ArrayType::get(Int8Ty, RoundUpToAlignment(N, kCounterAlignment));
257 RealEightBitCounterArray = new GlobalVariable(
258 M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage,
259 Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter");
260 RealEightBitCounterArray->setAlignment(kCounterAlignment);
261 EightBitCounterArray->replaceAllUsesWith(
262 IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy));
263 EightBitCounterArray->eraseFromParent();
266 // Create variable for module (compilation unit) name
267 Constant *ModNameStrConst =
268 ConstantDataArray::getString(M.getContext(), M.getName(), true);
269 GlobalVariable *ModuleName =
270 new GlobalVariable(M, ModNameStrConst->getType(), true,
271 GlobalValue::PrivateLinkage, ModNameStrConst);
273 // Call __sanitizer_cov_module_init
274 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
276 SanCovModuleInit, IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
277 ConstantInt::get(IntptrTy, N),
278 Options.Use8bitCounters
279 ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
280 : Constant::getNullValue(Int8PtrTy),
281 IRB.CreatePointerCast(ModuleName, Int8PtrTy));
285 bool SanitizerCoverageModule::runOnFunction(Function &F) {
286 if (F.empty()) return false;
287 if (F.getName().find(".module_ctor") != std::string::npos)
288 return false; // Should not instrument sanitizer init functions.
289 if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
290 SplitAllCriticalEdges(F);
291 SmallVector<Instruction*, 8> IndirCalls;
292 SmallVector<BasicBlock*, 16> AllBlocks;
293 SmallVector<Instruction*, 8> CmpTraceTargets;
295 AllBlocks.push_back(&BB);
296 for (auto &Inst : BB) {
297 if (Options.IndirectCalls) {
299 if (CS && !CS.getCalledFunction())
300 IndirCalls.push_back(&Inst);
302 if (Options.TraceCmp && isa<ICmpInst>(&Inst))
303 CmpTraceTargets.push_back(&Inst);
306 InjectCoverage(F, AllBlocks);
307 InjectCoverageForIndirectCalls(F, IndirCalls);
308 InjectTraceForCmp(F, CmpTraceTargets);
312 bool SanitizerCoverageModule::InjectCoverage(Function &F,
313 ArrayRef<BasicBlock *> AllBlocks) {
314 switch (Options.CoverageType) {
315 case SanitizerCoverageOptions::SCK_None:
317 case SanitizerCoverageOptions::SCK_Function:
318 InjectCoverageAtBlock(F, F.getEntryBlock(), false);
321 bool UseCalls = ClCoverageBlockThreshold < AllBlocks.size();
322 for (auto BB : AllBlocks)
323 InjectCoverageAtBlock(F, *BB, UseCalls);
329 // On every indirect call we call a run-time function
330 // __sanitizer_cov_indir_call* with two parameters:
332 // - global cache array that contains kCacheSize pointers (zero-initialized).
333 // The cache is used to speed up recording the caller-callee pairs.
334 // The address of the caller is passed implicitly via caller PC.
335 // kCacheSize is encoded in the name of the run-time function.
336 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
337 Function &F, ArrayRef<Instruction *> IndirCalls) {
338 if (IndirCalls.empty()) return;
339 const int kCacheSize = 16;
340 const int kCacheAlignment = 64; // Align for better performance.
341 Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
342 for (auto I : IndirCalls) {
345 Value *Callee = CS.getCalledValue();
346 if (isa<InlineAsm>(Callee)) continue;
347 GlobalVariable *CalleeCache = new GlobalVariable(
348 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
349 Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
350 CalleeCache->setAlignment(kCacheAlignment);
351 IRB.CreateCall2(SanCovIndirCallFunction,
352 IRB.CreatePointerCast(Callee, IntptrTy),
353 IRB.CreatePointerCast(CalleeCache, IntptrTy));
357 void SanitizerCoverageModule::InjectTraceForCmp(
358 Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
359 for (auto I : CmpTraceTargets) {
360 if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
361 IRBuilder<> IRB(ICMP);
362 Value *A0 = ICMP->getOperand(0);
363 Value *A1 = ICMP->getOperand(1);
364 if (!A0->getType()->isIntegerTy()) continue;
365 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
366 // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
368 SanCovTraceCmpFunction,
369 ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()),
370 IRB.CreateIntCast(A0, Int64Ty, true),
371 IRB.CreateIntCast(A1, Int64Ty, true));
376 void SanitizerCoverageModule::SetNoSanitizeMetadata(Instruction *I) {
378 I->getParent()->getParent()->getParent()->getMDKindID("nosanitize"),
379 MDNode::get(*C, None));
382 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
384 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
385 // Skip static allocas at the top of the entry block so they don't become
386 // dynamic when we split the block. If we used our optimized stack layout,
387 // then there will only be one alloca and it will come first.
388 for (; IP != BE; ++IP) {
389 AllocaInst *AI = dyn_cast<AllocaInst>(IP);
390 if (!AI || !AI->isStaticAlloca())
394 bool IsEntryBB = &BB == &F.getEntryBlock();
395 DebugLoc EntryLoc = IsEntryBB && IP->getDebugLoc()
396 ? IP->getDebugLoc().getFnDebugLoc()
399 IRB.SetCurrentDebugLocation(EntryLoc);
400 SmallVector<Value *, 1> Indices;
401 Value *GuardP = IRB.CreateAdd(
402 IRB.CreatePointerCast(GuardArray, IntptrTy),
403 ConstantInt::get(IntptrTy, (1 + NumberOfInstrumentedBlocks()) * 4));
404 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
405 GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
407 IRB.CreateCall(SanCovWithCheckFunction, GuardP);
409 LoadInst *Load = IRB.CreateLoad(GuardP);
410 Load->setAtomic(Monotonic);
411 Load->setAlignment(4);
412 SetNoSanitizeMetadata(Load);
413 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
414 Instruction *Ins = SplitBlockAndInsertIfThen(
415 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
416 IRB.SetInsertPoint(Ins);
417 IRB.SetCurrentDebugLocation(EntryLoc);
418 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
419 IRB.CreateCall(SanCovFunction, GuardP);
420 IRB.CreateCall(EmptyAsm); // Avoids callback merge.
423 if (Options.Use8bitCounters) {
424 IRB.SetInsertPoint(IP);
425 Value *P = IRB.CreateAdd(
426 IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
427 ConstantInt::get(IntptrTy, NumberOfInstrumentedBlocks() - 1));
428 P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy());
429 LoadInst *LI = IRB.CreateLoad(P);
430 Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1));
431 StoreInst *SI = IRB.CreateStore(Inc, P);
432 SetNoSanitizeMetadata(LI);
433 SetNoSanitizeMetadata(SI);
436 if (Options.TraceBB) {
437 // Experimental support for tracing.
438 // Insert a callback with the same guard variable as used for coverage.
439 IRB.SetInsertPoint(IP);
440 IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP);
444 char SanitizerCoverageModule::ID = 0;
445 INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
446 "SanitizerCoverage: TODO."
447 "ModulePass", false, false)
448 ModulePass *llvm::createSanitizerCoverageModulePass(
449 const SanitizerCoverageOptions &Options) {
450 return new SanitizerCoverageModule(Options);