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