SanitizerCoverage: Use `createSanitizerCtor` to create ctor and call init
[oota-llvm.git] / lib / Transforms / Instrumentation / SanitizerCoverage.cpp
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 (SCK_Function)
15 // or all blocks (SCK_BB):
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 SCK_Edge we also split critical edges this effectively
23 // instrumenting all edges.
24 //
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.
28 //
29 //===----------------------------------------------------------------------===//
30
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"
49
50 using namespace llvm;
51
52 #define DEBUG_TYPE "sancov"
53
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;
63
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));
69
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));
75
76 static 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
82 static cl::opt<bool>
83     ClExperimentalCMPTracing("sanitizer-coverage-experimental-trace-compares",
84                              cl::desc("Experimental tracing of CMP and similar "
85                                       "instructions"),
86                              cl::Hidden, cl::init(false));
87
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));
97
98 namespace {
99
100 SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
101   SanitizerCoverageOptions Res;
102   switch (LegacyCoverageLevel) {
103   case 0:
104     Res.CoverageType = SanitizerCoverageOptions::SCK_None;
105     break;
106   case 1:
107     Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
108     break;
109   case 2:
110     Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
111     break;
112   case 3:
113     Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
114     break;
115   case 4:
116     Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
117     Res.IndirectCalls = true;
118     break;
119   }
120   return Res;
121 }
122
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;
131   return Options;
132 }
133
134 class SanitizerCoverageModule : public ModulePass {
135  public:
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";
144   }
145
146  private:
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();
155   }
156   Function *SanCovFunction;
157   Function *SanCovWithCheckFunction;
158   Function *SanCovIndirCallFunction;
159   Function *SanCovTraceEnter, *SanCovTraceBB;
160   Function *SanCovTraceCmpFunction;
161   InlineAsm *EmptyAsm;
162   Type *IntptrTy, *Int64Ty;
163   LLVMContext *C;
164   const DataLayout *DL;
165
166   GlobalVariable *GuardArray;
167   GlobalVariable *EightBitCounterArray;
168
169   SanitizerCoverageOptions Options;
170 };
171
172 }  // namespace
173
174 bool SanitizerCoverageModule::runOnModule(Module &M) {
175   if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
176     return false;
177   C = &(M.getContext());
178   DL = &M.getDataLayout();
179   IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
180   Type *VoidTy = Type::getVoidTy(*C);
181   IRBuilder<> IRB(*C);
182   Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
183   Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
184   Int64Ty = IRB.getInt64Ty();
185
186   SanCovFunction = checkSanitizerInterfaceFunction(
187       M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
188   SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
189       M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
190   SanCovIndirCallFunction =
191       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
192           kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
193   SanCovTraceCmpFunction =
194       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
195           kSanCovTraceCmp, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr));
196
197   // We insert an empty inline asm after cov callbacks to avoid callback merge.
198   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
199                             StringRef(""), StringRef(""),
200                             /*hasSideEffects=*/true);
201
202   if (Options.TraceBB) {
203     SanCovTraceEnter = checkSanitizerInterfaceFunction(
204         M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
205     SanCovTraceBB = checkSanitizerInterfaceFunction(
206         M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr));
207   }
208
209   // At this point we create a dummy array of guards because we don't
210   // know how many elements we will need.
211   Type *Int32Ty = IRB.getInt32Ty();
212   Type *Int8Ty = IRB.getInt8Ty();
213
214   GuardArray =
215       new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
216                          nullptr, "__sancov_gen_cov_tmp");
217   if (Options.Use8bitCounters)
218     EightBitCounterArray =
219         new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
220                            nullptr, "__sancov_gen_cov_tmp");
221
222   for (auto &F : M)
223     runOnFunction(F);
224
225   auto N = NumberOfInstrumentedBlocks();
226
227   // Now we know how many elements we need. Create an array of guards
228   // with one extra element at the beginning for the size.
229   Type *Int32ArrayNTy = ArrayType::get(Int32Ty, N + 1);
230   GlobalVariable *RealGuardArray = new GlobalVariable(
231       M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
232       Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
233
234
235   // Replace the dummy array with the real one.
236   GuardArray->replaceAllUsesWith(
237       IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
238   GuardArray->eraseFromParent();
239
240   GlobalVariable *RealEightBitCounterArray;
241   if (Options.Use8bitCounters) {
242     // Make sure the array is 16-aligned.
243     static const int kCounterAlignment = 16;
244     Type *Int8ArrayNTy =
245         ArrayType::get(Int8Ty, RoundUpToAlignment(N, kCounterAlignment));
246     RealEightBitCounterArray = new GlobalVariable(
247         M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage,
248         Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter");
249     RealEightBitCounterArray->setAlignment(kCounterAlignment);
250     EightBitCounterArray->replaceAllUsesWith(
251         IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy));
252     EightBitCounterArray->eraseFromParent();
253   }
254
255   // Create variable for module (compilation unit) name
256   Constant *ModNameStrConst =
257       ConstantDataArray::getString(M.getContext(), M.getName(), true);
258   GlobalVariable *ModuleName =
259       new GlobalVariable(M, ModNameStrConst->getType(), true,
260                          GlobalValue::PrivateLinkage, ModNameStrConst);
261
262   ArrayRef<Value *> InitArgs = {
263       IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
264       ConstantInt::get(IntptrTy, N),
265       Options.Use8bitCounters
266           ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
267           : Constant::getNullValue(Int8PtrTy),
268       IRB.CreatePointerCast(ModuleName, Int8PtrTy)};
269
270   Function *CtorFunc;
271   std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
272       M, kSanCovModuleCtorName, kSanCovModuleInitName,
273       {Int32PtrTy, IntptrTy, Int8PtrTy, Int8PtrTy}, InitArgs);
274
275   appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
276
277   return true;
278 }
279
280 bool SanitizerCoverageModule::runOnFunction(Function &F) {
281   if (F.empty()) return false;
282   if (F.getName().find(".module_ctor") != std::string::npos)
283     return false;  // Should not instrument sanitizer init functions.
284   if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
285     SplitAllCriticalEdges(F);
286   SmallVector<Instruction*, 8> IndirCalls;
287   SmallVector<BasicBlock*, 16> AllBlocks;
288   SmallVector<Instruction*, 8> CmpTraceTargets;
289   for (auto &BB : F) {
290     AllBlocks.push_back(&BB);
291     for (auto &Inst : BB) {
292       if (Options.IndirectCalls) {
293         CallSite CS(&Inst);
294         if (CS && !CS.getCalledFunction())
295           IndirCalls.push_back(&Inst);
296       }
297       if (Options.TraceCmp && isa<ICmpInst>(&Inst))
298         CmpTraceTargets.push_back(&Inst);
299     }
300   }
301   InjectCoverage(F, AllBlocks);
302   InjectCoverageForIndirectCalls(F, IndirCalls);
303   InjectTraceForCmp(F, CmpTraceTargets);
304   return true;
305 }
306
307 bool SanitizerCoverageModule::InjectCoverage(Function &F,
308                                              ArrayRef<BasicBlock *> AllBlocks) {
309   switch (Options.CoverageType) {
310   case SanitizerCoverageOptions::SCK_None:
311     return false;
312   case SanitizerCoverageOptions::SCK_Function:
313     InjectCoverageAtBlock(F, F.getEntryBlock(), false);
314     return true;
315   default: {
316     bool UseCalls = ClCoverageBlockThreshold < AllBlocks.size();
317     for (auto BB : AllBlocks)
318       InjectCoverageAtBlock(F, *BB, UseCalls);
319     return true;
320   }
321   }
322 }
323
324 // On every indirect call we call a run-time function
325 // __sanitizer_cov_indir_call* with two parameters:
326 //   - callee address,
327 //   - global cache array that contains kCacheSize pointers (zero-initialized).
328 //     The cache is used to speed up recording the caller-callee pairs.
329 // The address of the caller is passed implicitly via caller PC.
330 // kCacheSize is encoded in the name of the run-time function.
331 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
332     Function &F, ArrayRef<Instruction *> IndirCalls) {
333   if (IndirCalls.empty()) return;
334   const int kCacheSize = 16;
335   const int kCacheAlignment = 64;  // Align for better performance.
336   Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
337   for (auto I : IndirCalls) {
338     IRBuilder<> IRB(I);
339     CallSite CS(I);
340     Value *Callee = CS.getCalledValue();
341     if (isa<InlineAsm>(Callee)) continue;
342     GlobalVariable *CalleeCache = new GlobalVariable(
343         *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
344         Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
345     CalleeCache->setAlignment(kCacheAlignment);
346     IRB.CreateCall2(SanCovIndirCallFunction,
347                     IRB.CreatePointerCast(Callee, IntptrTy),
348                     IRB.CreatePointerCast(CalleeCache, IntptrTy));
349   }
350 }
351
352 void SanitizerCoverageModule::InjectTraceForCmp(
353     Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
354   for (auto I : CmpTraceTargets) {
355     if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
356       IRBuilder<> IRB(ICMP);
357       Value *A0 = ICMP->getOperand(0);
358       Value *A1 = ICMP->getOperand(1);
359       if (!A0->getType()->isIntegerTy()) continue;
360       uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
361       // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
362       IRB.CreateCall3(
363           SanCovTraceCmpFunction,
364           ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()),
365           IRB.CreateIntCast(A0, Int64Ty, true),
366           IRB.CreateIntCast(A1, Int64Ty, true));
367     }
368   }
369 }
370
371 void SanitizerCoverageModule::SetNoSanitizeMetadata(Instruction *I) {
372   I->setMetadata(
373       I->getParent()->getParent()->getParent()->getMDKindID("nosanitize"),
374       MDNode::get(*C, None));
375 }
376
377 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
378                                                     bool UseCalls) {
379   BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
380   // Skip static allocas at the top of the entry block so they don't become
381   // dynamic when we split the block.  If we used our optimized stack layout,
382   // then there will only be one alloca and it will come first.
383   for (; IP != BE; ++IP) {
384     AllocaInst *AI = dyn_cast<AllocaInst>(IP);
385     if (!AI || !AI->isStaticAlloca())
386       break;
387   }
388
389   bool IsEntryBB = &BB == &F.getEntryBlock();
390   DebugLoc EntryLoc = IsEntryBB && IP->getDebugLoc()
391                           ? IP->getDebugLoc().getFnDebugLoc()
392                           : IP->getDebugLoc();
393   IRBuilder<> IRB(IP);
394   IRB.SetCurrentDebugLocation(EntryLoc);
395   SmallVector<Value *, 1> Indices;
396   Value *GuardP = IRB.CreateAdd(
397       IRB.CreatePointerCast(GuardArray, IntptrTy),
398       ConstantInt::get(IntptrTy, (1 + NumberOfInstrumentedBlocks()) * 4));
399   Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
400   GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
401   if (UseCalls) {
402     IRB.CreateCall(SanCovWithCheckFunction, GuardP);
403   } else {
404     LoadInst *Load = IRB.CreateLoad(GuardP);
405     Load->setAtomic(Monotonic);
406     Load->setAlignment(4);
407     SetNoSanitizeMetadata(Load);
408     Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
409     Instruction *Ins = SplitBlockAndInsertIfThen(
410         Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
411     IRB.SetInsertPoint(Ins);
412     IRB.SetCurrentDebugLocation(EntryLoc);
413     // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
414     IRB.CreateCall(SanCovFunction, GuardP);
415     IRB.CreateCall(EmptyAsm);  // Avoids callback merge.
416   }
417
418   if (Options.Use8bitCounters) {
419     IRB.SetInsertPoint(IP);
420     Value *P = IRB.CreateAdd(
421         IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
422         ConstantInt::get(IntptrTy, NumberOfInstrumentedBlocks() - 1));
423     P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy());
424     LoadInst *LI = IRB.CreateLoad(P);
425     Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1));
426     StoreInst *SI = IRB.CreateStore(Inc, P);
427     SetNoSanitizeMetadata(LI);
428     SetNoSanitizeMetadata(SI);
429   }
430
431   if (Options.TraceBB) {
432     // Experimental support for tracing.
433     // Insert a callback with the same guard variable as used for coverage.
434     IRB.SetInsertPoint(IP);
435     IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP);
436   }
437 }
438
439 char SanitizerCoverageModule::ID = 0;
440 INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
441     "SanitizerCoverage: TODO."
442     "ModulePass", false, false)
443 ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
444   return createSanitizerCoverageModulePass(getOptions(CoverageLevel));
445 }
446 ModulePass *llvm::createSanitizerCoverageModulePass(
447     const SanitizerCoverageOptions &Options) {
448   return new SanitizerCoverageModule(Options);
449 }