1 //===-- 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 //
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):
16 // if (Guard < 0) {
17 // __sanitizer_cov(&Guard);
18 // }
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"
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"
51
52 using namespace llvm;
53
54 #define DEBUG_TYPE "sancov"
55
56 static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
57 static const char *const kSanCovName = "__sanitizer_cov";
58 static const char *const kSanCovWithCheckName = "__sanitizer_cov_with_check";
59 static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
60 static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
61 static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
62 static const char *const kSanCovTraceCmp = "__sanitizer_cov_trace_cmp";
63 static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
64 static const uint64_t kSanCtorAndDtorPriority = 2;
65
66 static cl::opt<int> ClCoverageLevel("sanitizer-coverage-level",
67 cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
68 "3: all blocks and critical edges, "
69 "4: above plus indirect calls"),
70 cl::Hidden, cl::init(0));
71
72 static cl::opt<unsigned> ClCoverageBlockThreshold(
73 "sanitizer-coverage-block-threshold",
74 cl::desc("Use a callback with a guard check inside it if there are"
75 " more than this number of blocks."),
76 cl::Hidden, cl::init(500));
77
78 static cl::opt<bool>
79 ClExperimentalTracing("sanitizer-coverage-experimental-tracing",
80 cl::desc("Experimental basic-block tracing: insert "
81 "callbacks at every basic block"),
82 cl::Hidden, cl::init(false));
83
84 static cl::opt<bool>
85 ClExperimentalCMPTracing("sanitizer-coverage-experimental-trace-compares",
86 cl::desc("Experimental tracing of CMP and similar "
87 "instructions"),
88 cl::Hidden, cl::init(false));
89
90 // Experimental 8-bit counters used as an additional search heuristic during
91 // coverage-guided fuzzing.
92 // The counters are not thread-friendly:
93 // - contention on these counters may cause significant slowdown;
94 // - the counter updates are racy and the results may be inaccurate.
95 // They are also inaccurate due to 8-bit integer overflow.
96 static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
97 cl::desc("Experimental 8-bit counters"),
98 cl::Hidden, cl::init(false));
99
100 namespace {
101
102 class SanitizerCoverageModule : public ModulePass {
103 public:
SanitizerCoverageModule(int CoverageLevel=0)104 SanitizerCoverageModule(int CoverageLevel = 0)
105 : ModulePass(ID),
106 CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
107 bool runOnModule(Module &M) override;
108 bool runOnFunction(Function &F);
109 static char ID; // Pass identification, replacement for typeid
getPassName() const110 const char *getPassName() const override {
111 return "SanitizerCoverageModule";
112 }
113
114 private:
115 void InjectCoverageForIndirectCalls(Function &F,
116 ArrayRef<Instruction *> IndirCalls);
117 void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
118 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks);
119 void SetNoSanitizeMetada(Instruction *I);
120 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, bool UseCalls);
NumberOfInstrumentedBlocks()121 unsigned NumberOfInstrumentedBlocks() {
122 return SanCovFunction->getNumUses() + SanCovWithCheckFunction->getNumUses();
123 }
124 Function *SanCovFunction;
125 Function *SanCovWithCheckFunction;
126 Function *SanCovIndirCallFunction;
127 Function *SanCovModuleInit;
128 Function *SanCovTraceEnter, *SanCovTraceBB;
129 Function *SanCovTraceCmpFunction;
130 InlineAsm *EmptyAsm;
131 Type *IntptrTy, *Int64Ty;
132 LLVMContext *C;
133 const DataLayout *DL;
134
135 GlobalVariable *GuardArray;
136 GlobalVariable *EightBitCounterArray;
137
138 int CoverageLevel;
139 };
140
141 } // namespace
142
runOnModule(Module & M)143 bool SanitizerCoverageModule::runOnModule(Module &M) {
144 if (!CoverageLevel) return false;
145 C = &(M.getContext());
146 DL = &M.getDataLayout();
147 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
148 Type *VoidTy = Type::getVoidTy(*C);
149 IRBuilder<> IRB(*C);
150 Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
151 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
152 Int64Ty = IRB.getInt64Ty();
153
154 Function *CtorFunc =
155 Function::Create(FunctionType::get(VoidTy, false),
156 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
157 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
158 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
159
160 SanCovFunction = checkSanitizerInterfaceFunction(
161 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
162 SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
163 M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
164 SanCovIndirCallFunction =
165 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
166 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
167 SanCovTraceCmpFunction =
168 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
169 kSanCovTraceCmp, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr));
170
171 SanCovModuleInit = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
172 kSanCovModuleInitName, Type::getVoidTy(*C), Int32PtrTy, IntptrTy,
173 Int8PtrTy, Int8PtrTy, nullptr));
174 SanCovModuleInit->setLinkage(Function::ExternalLinkage);
175 // We insert an empty inline asm after cov callbacks to avoid callback merge.
176 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
177 StringRef(""), StringRef(""),
178 /*hasSideEffects=*/true);
179
180 if (ClExperimentalTracing) {
181 SanCovTraceEnter = checkSanitizerInterfaceFunction(
182 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
183 SanCovTraceBB = checkSanitizerInterfaceFunction(
184 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr));
185 }
186
187 // At this point we create a dummy array of guards because we don't
188 // know how many elements we will need.
189 Type *Int32Ty = IRB.getInt32Ty();
190 Type *Int8Ty = IRB.getInt8Ty();
191
192 GuardArray =
193 new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
194 nullptr, "__sancov_gen_cov_tmp");
195 if (ClUse8bitCounters)
196 EightBitCounterArray =
197 new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
198 nullptr, "__sancov_gen_cov_tmp");
199
200 for (auto &F : M)
201 runOnFunction(F);
202
203 auto N = NumberOfInstrumentedBlocks();
204
205 // Now we know how many elements we need. Create an array of guards
206 // with one extra element at the beginning for the size.
207 Type *Int32ArrayNTy = ArrayType::get(Int32Ty, N + 1);
208 GlobalVariable *RealGuardArray = new GlobalVariable(
209 M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
210 Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
211
212
213 // Replace the dummy array with the real one.
214 GuardArray->replaceAllUsesWith(
215 IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
216 GuardArray->eraseFromParent();
217
218 GlobalVariable *RealEightBitCounterArray;
219 if (ClUse8bitCounters) {
220 // Make sure the array is 16-aligned.
221 static const int kCounterAlignment = 16;
222 Type *Int8ArrayNTy =
223 ArrayType::get(Int8Ty, RoundUpToAlignment(N, kCounterAlignment));
224 RealEightBitCounterArray = new GlobalVariable(
225 M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage,
226 Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter");
227 RealEightBitCounterArray->setAlignment(kCounterAlignment);
228 EightBitCounterArray->replaceAllUsesWith(
229 IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy));
230 EightBitCounterArray->eraseFromParent();
231 }
232
233 // Create variable for module (compilation unit) name
234 Constant *ModNameStrConst =
235 ConstantDataArray::getString(M.getContext(), M.getName(), true);
236 GlobalVariable *ModuleName =
237 new GlobalVariable(M, ModNameStrConst->getType(), true,
238 GlobalValue::PrivateLinkage, ModNameStrConst);
239
240 // Call __sanitizer_cov_module_init
241 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
242 IRB.CreateCall4(
243 SanCovModuleInit, IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
244 ConstantInt::get(IntptrTy, N),
245 ClUse8bitCounters
246 ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
247 : Constant::getNullValue(Int8PtrTy),
248 IRB.CreatePointerCast(ModuleName, Int8PtrTy));
249 return true;
250 }
251
runOnFunction(Function & F)252 bool SanitizerCoverageModule::runOnFunction(Function &F) {
253 if (F.empty()) return false;
254 if (F.getName().find(".module_ctor") != std::string::npos)
255 return false; // Should not instrument sanitizer init functions.
256 if (CoverageLevel >= 3)
257 SplitAllCriticalEdges(F);
258 SmallVector<Instruction*, 8> IndirCalls;
259 SmallVector<BasicBlock*, 16> AllBlocks;
260 SmallVector<Instruction*, 8> CmpTraceTargets;
261 for (auto &BB : F) {
262 AllBlocks.push_back(&BB);
263 for (auto &Inst : BB) {
264 if (CoverageLevel >= 4) {
265 CallSite CS(&Inst);
266 if (CS && !CS.getCalledFunction())
267 IndirCalls.push_back(&Inst);
268 }
269 if (ClExperimentalCMPTracing)
270 if (isa<ICmpInst>(&Inst))
271 CmpTraceTargets.push_back(&Inst);
272 }
273 }
274 InjectCoverage(F, AllBlocks);
275 InjectCoverageForIndirectCalls(F, IndirCalls);
276 InjectTraceForCmp(F, CmpTraceTargets);
277 return true;
278 }
279
InjectCoverage(Function & F,ArrayRef<BasicBlock * > AllBlocks)280 bool SanitizerCoverageModule::InjectCoverage(Function &F,
281 ArrayRef<BasicBlock *> AllBlocks) {
282 if (!CoverageLevel) return false;
283
284 if (CoverageLevel == 1) {
285 InjectCoverageAtBlock(F, F.getEntryBlock(), false);
286 } else {
287 for (auto BB : AllBlocks)
288 InjectCoverageAtBlock(F, *BB,
289 ClCoverageBlockThreshold < AllBlocks.size());
290 }
291 return true;
292 }
293
294 // On every indirect call we call a run-time function
295 // __sanitizer_cov_indir_call* with two parameters:
296 // - callee address,
297 // - global cache array that contains kCacheSize pointers (zero-initialized).
298 // The cache is used to speed up recording the caller-callee pairs.
299 // The address of the caller is passed implicitly via caller PC.
300 // kCacheSize is encoded in the name of the run-time function.
InjectCoverageForIndirectCalls(Function & F,ArrayRef<Instruction * > IndirCalls)301 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
302 Function &F, ArrayRef<Instruction *> IndirCalls) {
303 if (IndirCalls.empty()) return;
304 const int kCacheSize = 16;
305 const int kCacheAlignment = 64; // Align for better performance.
306 Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
307 for (auto I : IndirCalls) {
308 IRBuilder<> IRB(I);
309 CallSite CS(I);
310 Value *Callee = CS.getCalledValue();
311 if (isa<InlineAsm>(Callee)) continue;
312 GlobalVariable *CalleeCache = new GlobalVariable(
313 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
314 Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
315 CalleeCache->setAlignment(kCacheAlignment);
316 IRB.CreateCall2(SanCovIndirCallFunction,
317 IRB.CreatePointerCast(Callee, IntptrTy),
318 IRB.CreatePointerCast(CalleeCache, IntptrTy));
319 }
320 }
321
InjectTraceForCmp(Function & F,ArrayRef<Instruction * > CmpTraceTargets)322 void SanitizerCoverageModule::InjectTraceForCmp(
323 Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
324 if (!ClExperimentalCMPTracing) return;
325 for (auto I : CmpTraceTargets) {
326 if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
327 IRBuilder<> IRB(ICMP);
328 Value *A0 = ICMP->getOperand(0);
329 Value *A1 = ICMP->getOperand(1);
330 if (!A0->getType()->isIntegerTy()) continue;
331 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
332 // __sanitizer_cov_indir_call((type_size << 32) | predicate, A0, A1);
333 IRB.CreateCall3(
334 SanCovTraceCmpFunction,
335 ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()),
336 IRB.CreateIntCast(A0, Int64Ty, true),
337 IRB.CreateIntCast(A1, Int64Ty, true));
338 }
339 }
340 }
341
SetNoSanitizeMetada(Instruction * I)342 void SanitizerCoverageModule::SetNoSanitizeMetada(Instruction *I) {
343 I->setMetadata(
344 I->getParent()->getParent()->getParent()->getMDKindID("nosanitize"),
345 MDNode::get(*C, None));
346 }
347
InjectCoverageAtBlock(Function & F,BasicBlock & BB,bool UseCalls)348 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
349 bool UseCalls) {
350 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
351 // Skip static allocas at the top of the entry block so they don't become
352 // dynamic when we split the block. If we used our optimized stack layout,
353 // then there will only be one alloca and it will come first.
354 for (; IP != BE; ++IP) {
355 AllocaInst *AI = dyn_cast<AllocaInst>(IP);
356 if (!AI || !AI->isStaticAlloca())
357 break;
358 }
359
360 bool IsEntryBB = &BB == &F.getEntryBlock();
361 DebugLoc EntryLoc = IsEntryBB && IP->getDebugLoc()
362 ? IP->getDebugLoc().getFnDebugLoc()
363 : IP->getDebugLoc();
364 IRBuilder<> IRB(IP);
365 IRB.SetCurrentDebugLocation(EntryLoc);
366 SmallVector<Value *, 1> Indices;
367 Value *GuardP = IRB.CreateAdd(
368 IRB.CreatePointerCast(GuardArray, IntptrTy),
369 ConstantInt::get(IntptrTy, (1 + NumberOfInstrumentedBlocks()) * 4));
370 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
371 GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
372 if (UseCalls) {
373 IRB.CreateCall(SanCovWithCheckFunction, GuardP);
374 } else {
375 LoadInst *Load = IRB.CreateLoad(GuardP);
376 Load->setAtomic(Monotonic);
377 Load->setAlignment(4);
378 SetNoSanitizeMetada(Load);
379 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
380 Instruction *Ins = SplitBlockAndInsertIfThen(
381 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
382 IRB.SetInsertPoint(Ins);
383 IRB.SetCurrentDebugLocation(EntryLoc);
384 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
385 IRB.CreateCall(SanCovFunction, GuardP);
386 IRB.CreateCall(EmptyAsm); // Avoids callback merge.
387 }
388
389 if(ClUse8bitCounters) {
390 IRB.SetInsertPoint(IP);
391 Value *P = IRB.CreateAdd(
392 IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
393 ConstantInt::get(IntptrTy, NumberOfInstrumentedBlocks() - 1));
394 P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy());
395 LoadInst *LI = IRB.CreateLoad(P);
396 Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1));
397 StoreInst *SI = IRB.CreateStore(Inc, P);
398 SetNoSanitizeMetada(LI);
399 SetNoSanitizeMetada(SI);
400 }
401
402 if (ClExperimentalTracing) {
403 // Experimental support for tracing.
404 // Insert a callback with the same guard variable as used for coverage.
405 IRB.SetInsertPoint(IP);
406 IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP);
407 }
408 }
409
410 char SanitizerCoverageModule::ID = 0;
411 INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
412 "SanitizerCoverage: TODO."
413 "ModulePass", false, false)
createSanitizerCoverageModulePass(int CoverageLevel)414 ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
415 return new SanitizerCoverageModule(CoverageLevel);
416 }
417