xref: /llvm-project/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (revision 0b94e880073e3f37fb1df2b31733b2b45b2d8670)
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 done on LLVM IR level, works with Sanitizers.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/Analysis/EHPersonalities.h"
17 #include "llvm/Analysis/PostDominators.h"
18 #include "llvm/IR/CFG.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/MDBuilder.h"
32 #include "llvm/IR/Mangler.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Transforms/Instrumentation.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40 #include "llvm/Transforms/Utils/ModuleUtils.h"
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "sancov"
45 
46 static const char *const SanCovTracePCIndirName =
47     "__sanitizer_cov_trace_pc_indir";
48 static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc";
49 static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1";
50 static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2";
51 static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4";
52 static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8";
53 static const char *const SanCovTraceConstCmp1 =
54     "__sanitizer_cov_trace_const_cmp1";
55 static const char *const SanCovTraceConstCmp2 =
56     "__sanitizer_cov_trace_const_cmp2";
57 static const char *const SanCovTraceConstCmp4 =
58     "__sanitizer_cov_trace_const_cmp4";
59 static const char *const SanCovTraceConstCmp8 =
60     "__sanitizer_cov_trace_const_cmp8";
61 static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4";
62 static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8";
63 static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep";
64 static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
65 static const char *const SanCovModuleCtorName = "sancov.module_ctor";
66 static const uint64_t SanCtorAndDtorPriority = 2;
67 
68 static const char *const SanCovTracePCGuardName =
69     "__sanitizer_cov_trace_pc_guard";
70 static const char *const SanCovTracePCGuardInitName =
71     "__sanitizer_cov_trace_pc_guard_init";
72 static const char *const SanCov8bitCountersInitName =
73     "__sanitizer_cov_8bit_counters_init";
74 static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init";
75 
76 static const char *const SanCovGuardsSectionName = "sancov_guards";
77 static const char *const SanCovCountersSectionName = "sancov_cntrs";
78 static const char *const SanCovPCsSectionName = "sancov_pcs";
79 
80 static const char *const SanCovLowestStackName = "__sancov_lowest_stack";
81 
82 static cl::opt<int> ClCoverageLevel(
83     "sanitizer-coverage-level",
84     cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
85              "3: all blocks and critical edges"),
86     cl::Hidden, cl::init(0));
87 
88 static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc",
89                                cl::desc("Experimental pc tracing"), cl::Hidden,
90                                cl::init(false));
91 
92 static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard",
93                                     cl::desc("pc tracing with a guard"),
94                                     cl::Hidden, cl::init(false));
95 
96 // If true, we create a global variable that contains PCs of all instrumented
97 // BBs, put this global into a named section, and pass this section's bounds
98 // to __sanitizer_cov_pcs_init.
99 // This way the coverage instrumentation does not need to acquire the PCs
100 // at run-time. Works with trace-pc-guard and inline-8bit-counters.
101 static cl::opt<bool> ClCreatePCTable("sanitizer-coverage-pc-table",
102                                      cl::desc("create a static PC table"),
103                                      cl::Hidden, cl::init(false));
104 
105 static cl::opt<bool>
106     ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
107                          cl::desc("increments 8-bit counter for every edge"),
108                          cl::Hidden, cl::init(false));
109 
110 static cl::opt<bool>
111     ClCMPTracing("sanitizer-coverage-trace-compares",
112                  cl::desc("Tracing of CMP and similar instructions"),
113                  cl::Hidden, cl::init(false));
114 
115 static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs",
116                                   cl::desc("Tracing of DIV instructions"),
117                                   cl::Hidden, cl::init(false));
118 
119 static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps",
120                                   cl::desc("Tracing of GEP instructions"),
121                                   cl::Hidden, cl::init(false));
122 
123 static cl::opt<bool>
124     ClPruneBlocks("sanitizer-coverage-prune-blocks",
125                   cl::desc("Reduce the number of instrumented blocks"),
126                   cl::Hidden, cl::init(true));
127 
128 static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth",
129                                   cl::desc("max stack depth tracing"),
130                                   cl::Hidden, cl::init(false));
131 
132 namespace {
133 
134 SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
135   SanitizerCoverageOptions Res;
136   switch (LegacyCoverageLevel) {
137   case 0:
138     Res.CoverageType = SanitizerCoverageOptions::SCK_None;
139     break;
140   case 1:
141     Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
142     break;
143   case 2:
144     Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
145     break;
146   case 3:
147     Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
148     break;
149   case 4:
150     Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
151     Res.IndirectCalls = true;
152     break;
153   }
154   return Res;
155 }
156 
157 SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
158   // Sets CoverageType and IndirectCalls.
159   SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
160   Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
161   Options.IndirectCalls |= CLOpts.IndirectCalls;
162   Options.TraceCmp |= ClCMPTracing;
163   Options.TraceDiv |= ClDIVTracing;
164   Options.TraceGep |= ClGEPTracing;
165   Options.TracePC |= ClTracePC;
166   Options.TracePCGuard |= ClTracePCGuard;
167   Options.Inline8bitCounters |= ClInline8bitCounters;
168   Options.PCTable |= ClCreatePCTable;
169   Options.NoPrune |= !ClPruneBlocks;
170   Options.StackDepth |= ClStackDepth;
171   if (!Options.TracePCGuard && !Options.TracePC &&
172       !Options.Inline8bitCounters && !Options.StackDepth)
173     Options.TracePCGuard = true; // TracePCGuard is default.
174   return Options;
175 }
176 
177 class SanitizerCoverageModule : public ModulePass {
178 public:
179   SanitizerCoverageModule(
180       const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
181       : ModulePass(ID), Options(OverrideFromCL(Options)) {
182     initializeSanitizerCoverageModulePass(*PassRegistry::getPassRegistry());
183   }
184   bool runOnModule(Module &M) override;
185   bool runOnFunction(Function &F);
186   static char ID; // Pass identification, replacement for typeid
187   StringRef getPassName() const override { return "SanitizerCoverageModule"; }
188 
189   void getAnalysisUsage(AnalysisUsage &AU) const override {
190     AU.addRequired<DominatorTreeWrapperPass>();
191     AU.addRequired<PostDominatorTreeWrapperPass>();
192   }
193 
194 private:
195   void InjectCoverageForIndirectCalls(Function &F,
196                                       ArrayRef<Instruction *> IndirCalls);
197   void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
198   void InjectTraceForDiv(Function &F,
199                          ArrayRef<BinaryOperator *> DivTraceTargets);
200   void InjectTraceForGep(Function &F,
201                          ArrayRef<GetElementPtrInst *> GepTraceTargets);
202   void InjectTraceForSwitch(Function &F,
203                             ArrayRef<Instruction *> SwitchTraceTargets);
204   bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
205                       bool IsLeafFunc = true);
206   GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
207                                                     Function &F, Type *Ty,
208                                                     const char *Section);
209   GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
210   void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
211   void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
212                              bool IsLeafFunc = true);
213   Function *CreateInitCallsForSections(Module &M, const char *InitFunctionName,
214                                        Type *Ty, const char *Section);
215   std::pair<GlobalVariable *, GlobalVariable *>
216   CreateSecStartEnd(Module &M, const char *Section, Type *Ty);
217 
218   void SetNoSanitizeMetadata(Instruction *I) {
219     I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
220                    MDNode::get(*C, None));
221   }
222 
223   Comdat *GetOrCreateFunctionComdat(Function &F);
224 
225   std::string getSectionName(const std::string &Section) const;
226   std::string getSectionStart(const std::string &Section) const;
227   std::string getSectionEnd(const std::string &Section) const;
228   Function *SanCovTracePCIndir;
229   Function *SanCovTracePC, *SanCovTracePCGuard;
230   Function *SanCovTraceCmpFunction[4];
231   Function *SanCovTraceConstCmpFunction[4];
232   Function *SanCovTraceDivFunction[2];
233   Function *SanCovTraceGepFunction;
234   Function *SanCovTraceSwitchFunction;
235   GlobalVariable *SanCovLowestStack;
236   InlineAsm *EmptyAsm;
237   Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
238       *Int16Ty, *Int8Ty, *Int8PtrTy;
239   Module *CurModule;
240   std::string CurModuleUniqueId;
241   Triple TargetTriple;
242   LLVMContext *C;
243   const DataLayout *DL;
244 
245   GlobalVariable *FunctionGuardArray;  // for trace-pc-guard.
246   GlobalVariable *Function8bitCounterArray;  // for inline-8bit-counters.
247   GlobalVariable *FunctionPCsArray;  // for pc-table.
248   SmallVector<GlobalValue *, 20> GlobalsToAppendToUsed;
249   SmallVector<GlobalValue *, 20> GlobalsToAppendToCompilerUsed;
250 
251   SanitizerCoverageOptions Options;
252 };
253 
254 } // namespace
255 
256 std::pair<GlobalVariable *, GlobalVariable *>
257 SanitizerCoverageModule::CreateSecStartEnd(Module &M, const char *Section,
258                                            Type *Ty) {
259   GlobalVariable *SecStart =
260       new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
261                          getSectionStart(Section));
262   SecStart->setVisibility(GlobalValue::HiddenVisibility);
263   GlobalVariable *SecEnd =
264       new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
265                          nullptr, getSectionEnd(Section));
266   SecEnd->setVisibility(GlobalValue::HiddenVisibility);
267 
268   return std::make_pair(SecStart, SecEnd);
269 }
270 
271 
272 Function *SanitizerCoverageModule::CreateInitCallsForSections(
273     Module &M, const char *InitFunctionName, Type *Ty,
274     const char *Section) {
275   IRBuilder<> IRB(M.getContext());
276   auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
277   auto SecStart = SecStartEnd.first;
278   auto SecEnd = SecStartEnd.second;
279   Function *CtorFunc;
280   Value *SecStartPtr = nullptr;
281   // Account for the fact that on windows-msvc __start_* symbols actually
282   // point to a uint64_t before the start of the array.
283   if (TargetTriple.getObjectFormat() == Triple::COFF) {
284     auto SecStartI8Ptr = IRB.CreatePointerCast(SecStart, Int8PtrTy);
285     auto GEP = IRB.CreateGEP(SecStartI8Ptr,
286                              ConstantInt::get(IntptrTy, sizeof(uint64_t)));
287     SecStartPtr = IRB.CreatePointerCast(GEP, Ty);
288   } else {
289     SecStartPtr = IRB.CreatePointerCast(SecStart, Ty);
290   }
291   std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
292       M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty},
293       {SecStartPtr, IRB.CreatePointerCast(SecEnd, Ty)});
294 
295   if (TargetTriple.supportsCOMDAT()) {
296     // Use comdat to dedup CtorFunc.
297     CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName));
298     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
299   } else {
300     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
301   }
302 
303   if (TargetTriple.getObjectFormat() == Triple::COFF) {
304     // In COFF files, if the contructors are set as COMDAT (they are because
305     // COFF supports COMDAT) and the linker flag /OPT:REF (strip unreferenced
306     // functions and data) is used, the constructors get stripped. To prevent
307     // this, give the constructors weak ODR linkage and tell the linker to
308     // always include the sancov constructor. This way the linker can
309     // deduplicate the constructors but always leave one copy.
310     CtorFunc->setLinkage(GlobalValue::WeakODRLinkage);
311     SmallString<20> PartialIncDirective("/include:");
312     // Get constructor's mangled name in order to support i386.
313     SmallString<40> MangledName;
314     Mangler().getNameWithPrefix(MangledName, CtorFunc, true);
315     Twine IncDirective = PartialIncDirective + MangledName;
316     Metadata *Args[1] = {MDString::get(*C, IncDirective.str())};
317     MDNode *MetadataNode = MDNode::get(*C, Args);
318     NamedMDNode *NamedMetadata =
319         M.getOrInsertNamedMetadata("llvm.linker.options");
320     NamedMetadata->addOperand(MetadataNode);
321   }
322   return CtorFunc;
323 }
324 
325 bool SanitizerCoverageModule::runOnModule(Module &M) {
326   if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
327     return false;
328   C = &(M.getContext());
329   DL = &M.getDataLayout();
330   CurModule = &M;
331   CurModuleUniqueId = getUniqueModuleId(CurModule);
332   TargetTriple = Triple(M.getTargetTriple());
333   FunctionGuardArray = nullptr;
334   Function8bitCounterArray = nullptr;
335   FunctionPCsArray = nullptr;
336   IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
337   IntptrPtrTy = PointerType::getUnqual(IntptrTy);
338   Type *VoidTy = Type::getVoidTy(*C);
339   IRBuilder<> IRB(*C);
340   Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty());
341   Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
342   Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
343   Int64Ty = IRB.getInt64Ty();
344   Int32Ty = IRB.getInt32Ty();
345   Int16Ty = IRB.getInt16Ty();
346   Int8Ty = IRB.getInt8Ty();
347 
348   SanCovTracePCIndir = checkSanitizerInterfaceFunction(
349       M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
350   SanCovTraceCmpFunction[0] =
351       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
352           SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
353   SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
354       M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
355                             IRB.getInt16Ty()));
356   SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
357       M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
358                             IRB.getInt32Ty()));
359   SanCovTraceCmpFunction[3] =
360       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
361           SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
362 
363   SanCovTraceConstCmpFunction[0] =
364       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
365           SanCovTraceConstCmp1, VoidTy, Int8Ty, Int8Ty));
366   SanCovTraceConstCmpFunction[1] =
367       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
368           SanCovTraceConstCmp2, VoidTy, Int16Ty, Int16Ty));
369   SanCovTraceConstCmpFunction[2] =
370       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
371           SanCovTraceConstCmp4, VoidTy, Int32Ty, Int32Ty));
372   SanCovTraceConstCmpFunction[3] =
373       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
374           SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty));
375 
376   SanCovTraceDivFunction[0] =
377       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
378           SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
379   SanCovTraceDivFunction[1] =
380       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
381           SanCovTraceDiv8, VoidTy, Int64Ty));
382   SanCovTraceGepFunction =
383       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
384           SanCovTraceGep, VoidTy, IntptrTy));
385   SanCovTraceSwitchFunction =
386       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
387           SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
388 
389   Constant *SanCovLowestStackConstant =
390       M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
391   SanCovLowestStack = cast<GlobalVariable>(SanCovLowestStackConstant);
392   SanCovLowestStack->setThreadLocalMode(
393       GlobalValue::ThreadLocalMode::InitialExecTLSModel);
394   if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
395     SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
396 
397   // Make sure smaller parameters are zero-extended to i64 as required by the
398   // x86_64 ABI.
399   if (TargetTriple.getArch() == Triple::x86_64) {
400     for (int i = 0; i < 3; i++) {
401       SanCovTraceCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
402       SanCovTraceCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
403       SanCovTraceConstCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
404       SanCovTraceConstCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
405     }
406     SanCovTraceDivFunction[0]->addParamAttr(0, Attribute::ZExt);
407   }
408 
409 
410   // We insert an empty inline asm after cov callbacks to avoid callback merge.
411   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
412                             StringRef(""), StringRef(""),
413                             /*hasSideEffects=*/true);
414 
415   SanCovTracePC = checkSanitizerInterfaceFunction(
416       M.getOrInsertFunction(SanCovTracePCName, VoidTy));
417   SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
418       SanCovTracePCGuardName, VoidTy, Int32PtrTy));
419 
420   for (auto &F : M)
421     runOnFunction(F);
422 
423   Function *Ctor = nullptr;
424 
425   if (FunctionGuardArray)
426     Ctor = CreateInitCallsForSections(M, SanCovTracePCGuardInitName, Int32PtrTy,
427                                       SanCovGuardsSectionName);
428   if (Function8bitCounterArray)
429     Ctor = CreateInitCallsForSections(M, SanCov8bitCountersInitName, Int8PtrTy,
430                                       SanCovCountersSectionName);
431   if (Ctor && Options.PCTable) {
432     auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
433     Function *InitFunction = declareSanitizerInitFunction(
434         M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
435     IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
436     Value *SecStartPtr = nullptr;
437     // Account for the fact that on windows-msvc __start_pc_table actually
438     // points to a uint64_t before the start of the PC table.
439     if (TargetTriple.getObjectFormat() == Triple::COFF) {
440       auto SecStartI8Ptr = IRB.CreatePointerCast(SecStartEnd.first, Int8PtrTy);
441       auto GEP = IRB.CreateGEP(SecStartI8Ptr,
442                                ConstantInt::get(IntptrTy, sizeof(uint64_t)));
443       SecStartPtr = IRB.CreatePointerCast(GEP, IntptrPtrTy);
444     } else {
445       SecStartPtr = IRB.CreatePointerCast(SecStartEnd.first, IntptrPtrTy);
446     }
447     IRBCtor.CreateCall(
448         InitFunction,
449         {SecStartPtr, IRB.CreatePointerCast(SecStartEnd.second, IntptrPtrTy)});
450   }
451   // We don't reference these arrays directly in any of our runtime functions,
452   // so we need to prevent them from being dead stripped.
453   if (TargetTriple.isOSBinFormatMachO())
454     appendToUsed(M, GlobalsToAppendToUsed);
455   appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed);
456   return true;
457 }
458 
459 // True if block has successors and it dominates all of them.
460 static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) {
461   if (succ_begin(BB) == succ_end(BB))
462     return false;
463 
464   for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) {
465     if (!DT->dominates(BB, SUCC))
466       return false;
467   }
468 
469   return true;
470 }
471 
472 // True if block has predecessors and it postdominates all of them.
473 static bool isFullPostDominator(const BasicBlock *BB,
474                                 const PostDominatorTree *PDT) {
475   if (pred_begin(BB) == pred_end(BB))
476     return false;
477 
478   for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) {
479     if (!PDT->dominates(BB, PRED))
480       return false;
481   }
482 
483   return true;
484 }
485 
486 static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
487                                   const DominatorTree *DT,
488                                   const PostDominatorTree *PDT,
489                                   const SanitizerCoverageOptions &Options) {
490   // Don't insert coverage for unreachable blocks: we will never call
491   // __sanitizer_cov() for them, so counting them in
492   // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
493   // percentage. Also, unreachable instructions frequently have no debug
494   // locations.
495   if (isa<UnreachableInst>(BB->getTerminator()))
496     return false;
497 
498   // Don't insert coverage into blocks without a valid insertion point
499   // (catchswitch blocks).
500   if (BB->getFirstInsertionPt() == BB->end())
501     return false;
502 
503   if (Options.NoPrune || &F.getEntryBlock() == BB)
504     return true;
505 
506   if (Options.CoverageType == SanitizerCoverageOptions::SCK_Function &&
507       &F.getEntryBlock() != BB)
508     return false;
509 
510   // Do not instrument full dominators, or full post-dominators with multiple
511   // predecessors.
512   return !isFullDominator(BB, DT)
513     && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
514 }
515 
516 bool SanitizerCoverageModule::runOnFunction(Function &F) {
517   if (F.empty())
518     return false;
519   if (F.getName().find(".module_ctor") != std::string::npos)
520     return false; // Should not instrument sanitizer init functions.
521   if (F.getName().startswith("__sanitizer_"))
522     return false;  // Don't instrument __sanitizer_* callbacks.
523   // Don't touch available_externally functions, their actual body is elewhere.
524   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
525     return false;
526   // Don't instrument MSVC CRT configuration helpers. They may run before normal
527   // initialization.
528   if (F.getName() == "__local_stdio_printf_options" ||
529       F.getName() == "__local_stdio_scanf_options")
530     return false;
531   if (isa<UnreachableInst>(F.getEntryBlock().getTerminator()))
532     return false;
533   // Don't instrument functions using SEH for now. Splitting basic blocks like
534   // we do for coverage breaks WinEHPrepare.
535   // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
536   if (F.hasPersonalityFn() &&
537       isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
538     return false;
539   if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
540     SplitAllCriticalEdges(F);
541   SmallVector<Instruction *, 8> IndirCalls;
542   SmallVector<BasicBlock *, 16> BlocksToInstrument;
543   SmallVector<Instruction *, 8> CmpTraceTargets;
544   SmallVector<Instruction *, 8> SwitchTraceTargets;
545   SmallVector<BinaryOperator *, 8> DivTraceTargets;
546   SmallVector<GetElementPtrInst *, 8> GepTraceTargets;
547 
548   const DominatorTree *DT =
549       &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
550   const PostDominatorTree *PDT =
551       &getAnalysis<PostDominatorTreeWrapperPass>(F).getPostDomTree();
552   bool IsLeafFunc = true;
553 
554   for (auto &BB : F) {
555     if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
556       BlocksToInstrument.push_back(&BB);
557     for (auto &Inst : BB) {
558       if (Options.IndirectCalls) {
559         CallSite CS(&Inst);
560         if (CS && !CS.getCalledFunction())
561           IndirCalls.push_back(&Inst);
562       }
563       if (Options.TraceCmp) {
564         if (isa<ICmpInst>(&Inst))
565           CmpTraceTargets.push_back(&Inst);
566         if (isa<SwitchInst>(&Inst))
567           SwitchTraceTargets.push_back(&Inst);
568       }
569       if (Options.TraceDiv)
570         if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst))
571           if (BO->getOpcode() == Instruction::SDiv ||
572               BO->getOpcode() == Instruction::UDiv)
573             DivTraceTargets.push_back(BO);
574       if (Options.TraceGep)
575         if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst))
576           GepTraceTargets.push_back(GEP);
577       if (Options.StackDepth)
578         if (isa<InvokeInst>(Inst) ||
579             (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst)))
580           IsLeafFunc = false;
581     }
582   }
583 
584   InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
585   InjectCoverageForIndirectCalls(F, IndirCalls);
586   InjectTraceForCmp(F, CmpTraceTargets);
587   InjectTraceForSwitch(F, SwitchTraceTargets);
588   InjectTraceForDiv(F, DivTraceTargets);
589   InjectTraceForGep(F, GepTraceTargets);
590   return true;
591 }
592 
593 Comdat *SanitizerCoverageModule::GetOrCreateFunctionComdat(Function &F) {
594   if (auto Comdat = F.getComdat()) return Comdat;
595   if (!TargetTriple.isOSBinFormatELF()) return nullptr;
596   assert(F.hasName());
597   std::string Name = F.getName();
598   if (F.hasLocalLinkage()) {
599     if (CurModuleUniqueId.empty()) return nullptr;
600     Name += CurModuleUniqueId;
601   }
602   auto Comdat = CurModule->getOrInsertComdat(Name);
603   F.setComdat(Comdat);
604   return Comdat;
605 }
606 
607 GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
608     size_t NumElements, Function &F, Type *Ty, const char *Section) {
609   ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
610   auto Array = new GlobalVariable(
611       *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
612       Constant::getNullValue(ArrayTy), "__sancov_gen_");
613   if (auto Comdat = GetOrCreateFunctionComdat(F))
614     Array->setComdat(Comdat);
615   Array->setSection(getSectionName(Section));
616   Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
617                                         : Ty->getPrimitiveSizeInBits() / 8);
618   GlobalsToAppendToUsed.push_back(Array);
619   GlobalsToAppendToCompilerUsed.push_back(Array);
620   MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
621   Array->addMetadata(LLVMContext::MD_associated, *MD);
622 
623   return Array;
624 }
625 
626 GlobalVariable *
627 SanitizerCoverageModule::CreatePCArray(Function &F,
628                                        ArrayRef<BasicBlock *> AllBlocks) {
629   size_t N = AllBlocks.size();
630   assert(N);
631   SmallVector<Constant *, 32> PCs;
632   IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
633   for (size_t i = 0; i < N; i++) {
634     if (&F.getEntryBlock() == AllBlocks[i]) {
635       PCs.push_back((Constant *)IRB.CreatePointerCast(&F, IntptrPtrTy));
636       PCs.push_back((Constant *)IRB.CreateIntToPtr(
637           ConstantInt::get(IntptrTy, 1), IntptrPtrTy));
638     } else {
639       PCs.push_back((Constant *)IRB.CreatePointerCast(
640           BlockAddress::get(AllBlocks[i]), IntptrPtrTy));
641       PCs.push_back((Constant *)IRB.CreateIntToPtr(
642           ConstantInt::get(IntptrTy, 0), IntptrPtrTy));
643     }
644   }
645   auto *PCArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy,
646                                                     SanCovPCsSectionName);
647   PCArray->setInitializer(
648       ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs));
649   PCArray->setConstant(true);
650 
651   return PCArray;
652 }
653 
654 void SanitizerCoverageModule::CreateFunctionLocalArrays(
655     Function &F, ArrayRef<BasicBlock *> AllBlocks) {
656   if (Options.TracePCGuard)
657     FunctionGuardArray = CreateFunctionLocalArrayInSection(
658         AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
659 
660   if (Options.Inline8bitCounters)
661     Function8bitCounterArray = CreateFunctionLocalArrayInSection(
662         AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
663 
664   if (Options.PCTable)
665     FunctionPCsArray = CreatePCArray(F, AllBlocks);
666 }
667 
668 bool SanitizerCoverageModule::InjectCoverage(Function &F,
669                                              ArrayRef<BasicBlock *> AllBlocks,
670                                              bool IsLeafFunc) {
671   if (AllBlocks.empty()) return false;
672   CreateFunctionLocalArrays(F, AllBlocks);
673   for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
674     InjectCoverageAtBlock(F, *AllBlocks[i], i, IsLeafFunc);
675   return true;
676 }
677 
678 // On every indirect call we call a run-time function
679 // __sanitizer_cov_indir_call* with two parameters:
680 //   - callee address,
681 //   - global cache array that contains CacheSize pointers (zero-initialized).
682 //     The cache is used to speed up recording the caller-callee pairs.
683 // The address of the caller is passed implicitly via caller PC.
684 // CacheSize is encoded in the name of the run-time function.
685 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
686     Function &F, ArrayRef<Instruction *> IndirCalls) {
687   if (IndirCalls.empty())
688     return;
689   assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters);
690   for (auto I : IndirCalls) {
691     IRBuilder<> IRB(I);
692     CallSite CS(I);
693     Value *Callee = CS.getCalledValue();
694     if (isa<InlineAsm>(Callee))
695       continue;
696     IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
697   }
698 }
699 
700 // For every switch statement we insert a call:
701 // __sanitizer_cov_trace_switch(CondValue,
702 //      {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
703 
704 void SanitizerCoverageModule::InjectTraceForSwitch(
705     Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
706   for (auto I : SwitchTraceTargets) {
707     if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
708       IRBuilder<> IRB(I);
709       SmallVector<Constant *, 16> Initializers;
710       Value *Cond = SI->getCondition();
711       if (Cond->getType()->getScalarSizeInBits() >
712           Int64Ty->getScalarSizeInBits())
713         continue;
714       Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
715       Initializers.push_back(
716           ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
717       if (Cond->getType()->getScalarSizeInBits() <
718           Int64Ty->getScalarSizeInBits())
719         Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
720       for (auto It : SI->cases()) {
721         Constant *C = It.getCaseValue();
722         if (C->getType()->getScalarSizeInBits() <
723             Int64Ty->getScalarSizeInBits())
724           C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
725         Initializers.push_back(C);
726       }
727       llvm::sort(Initializers.begin() + 2, Initializers.end(),
728                  [](const Constant *A, const Constant *B) {
729                    return cast<ConstantInt>(A)->getLimitedValue() <
730                           cast<ConstantInt>(B)->getLimitedValue();
731                  });
732       ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
733       GlobalVariable *GV = new GlobalVariable(
734           *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
735           ConstantArray::get(ArrayOfInt64Ty, Initializers),
736           "__sancov_gen_cov_switch_values");
737       IRB.CreateCall(SanCovTraceSwitchFunction,
738                      {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)});
739     }
740   }
741 }
742 
743 void SanitizerCoverageModule::InjectTraceForDiv(
744     Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
745   for (auto BO : DivTraceTargets) {
746     IRBuilder<> IRB(BO);
747     Value *A1 = BO->getOperand(1);
748     if (isa<ConstantInt>(A1)) continue;
749     if (!A1->getType()->isIntegerTy())
750       continue;
751     uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
752     int CallbackIdx = TypeSize == 32 ? 0 :
753         TypeSize == 64 ? 1 : -1;
754     if (CallbackIdx < 0) continue;
755     auto Ty = Type::getIntNTy(*C, TypeSize);
756     IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
757                    {IRB.CreateIntCast(A1, Ty, true)});
758   }
759 }
760 
761 void SanitizerCoverageModule::InjectTraceForGep(
762     Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
763   for (auto GEP : GepTraceTargets) {
764     IRBuilder<> IRB(GEP);
765     for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I)
766       if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy())
767         IRB.CreateCall(SanCovTraceGepFunction,
768                        {IRB.CreateIntCast(*I, IntptrTy, true)});
769   }
770 }
771 
772 void SanitizerCoverageModule::InjectTraceForCmp(
773     Function &, ArrayRef<Instruction *> CmpTraceTargets) {
774   for (auto I : CmpTraceTargets) {
775     if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
776       IRBuilder<> IRB(ICMP);
777       Value *A0 = ICMP->getOperand(0);
778       Value *A1 = ICMP->getOperand(1);
779       if (!A0->getType()->isIntegerTy())
780         continue;
781       uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
782       int CallbackIdx = TypeSize == 8 ? 0 :
783                         TypeSize == 16 ? 1 :
784                         TypeSize == 32 ? 2 :
785                         TypeSize == 64 ? 3 : -1;
786       if (CallbackIdx < 0) continue;
787       // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
788       auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
789       bool FirstIsConst = isa<ConstantInt>(A0);
790       bool SecondIsConst = isa<ConstantInt>(A1);
791       // If both are const, then we don't need such a comparison.
792       if (FirstIsConst && SecondIsConst) continue;
793       // If only one is const, then make it the first callback argument.
794       if (FirstIsConst || SecondIsConst) {
795         CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
796         if (SecondIsConst)
797           std::swap(A0, A1);
798       }
799 
800       auto Ty = Type::getIntNTy(*C, TypeSize);
801       IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true),
802               IRB.CreateIntCast(A1, Ty, true)});
803     }
804   }
805 }
806 
807 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
808                                                     size_t Idx,
809                                                     bool IsLeafFunc) {
810   BasicBlock::iterator IP = BB.getFirstInsertionPt();
811   bool IsEntryBB = &BB == &F.getEntryBlock();
812   DebugLoc EntryLoc;
813   if (IsEntryBB) {
814     if (auto SP = F.getSubprogram())
815       EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
816     // Keep static allocas and llvm.localescape calls in the entry block.  Even
817     // if we aren't splitting the block, it's nice for allocas to be before
818     // calls.
819     IP = PrepareToSplitEntryBlock(BB, IP);
820   } else {
821     EntryLoc = IP->getDebugLoc();
822   }
823 
824   IRBuilder<> IRB(&*IP);
825   IRB.SetCurrentDebugLocation(EntryLoc);
826   if (Options.TracePC) {
827     IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
828     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
829   }
830   if (Options.TracePCGuard) {
831     auto GuardPtr = IRB.CreateIntToPtr(
832         IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
833                       ConstantInt::get(IntptrTy, Idx * 4)),
834         Int32PtrTy);
835     IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
836     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
837   }
838   if (Options.Inline8bitCounters) {
839     auto CounterPtr = IRB.CreateGEP(
840         Function8bitCounterArray,
841         {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
842     auto Load = IRB.CreateLoad(CounterPtr);
843     auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
844     auto Store = IRB.CreateStore(Inc, CounterPtr);
845     SetNoSanitizeMetadata(Load);
846     SetNoSanitizeMetadata(Store);
847   }
848   if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
849     // Check stack depth.  If it's the deepest so far, record it.
850     Function *GetFrameAddr =
851         Intrinsic::getDeclaration(F.getParent(), Intrinsic::frameaddress);
852     auto FrameAddrPtr =
853         IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
854     auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
855     auto LowestStack = IRB.CreateLoad(SanCovLowestStack);
856     auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
857     auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
858     IRBuilder<> ThenIRB(ThenTerm);
859     auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
860     SetNoSanitizeMetadata(LowestStack);
861     SetNoSanitizeMetadata(Store);
862   }
863 }
864 
865 std::string
866 SanitizerCoverageModule::getSectionName(const std::string &Section) const {
867   if (TargetTriple.getObjectFormat() == Triple::COFF) {
868     if (Section == SanCovCountersSectionName)
869       return ".SCOV$CM";
870     if (Section == SanCovPCsSectionName)
871       return ".SCOVP$M";
872     return ".SCOV$GM"; // For SanCovGuardsSectionName.
873   }
874   if (TargetTriple.isOSBinFormatMachO())
875     return "__DATA,__" + Section;
876   return "__" + Section;
877 }
878 
879 std::string
880 SanitizerCoverageModule::getSectionStart(const std::string &Section) const {
881   if (TargetTriple.isOSBinFormatMachO())
882     return "\1section$start$__DATA$__" + Section;
883   return "__start___" + Section;
884 }
885 
886 std::string
887 SanitizerCoverageModule::getSectionEnd(const std::string &Section) const {
888   if (TargetTriple.isOSBinFormatMachO())
889     return "\1section$end$__DATA$__" + Section;
890   return "__stop___" + Section;
891 }
892 
893 
894 char SanitizerCoverageModule::ID = 0;
895 INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov",
896                       "SanitizerCoverage: TODO."
897                       "ModulePass",
898                       false, false)
899 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
900 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
901 INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov",
902                     "SanitizerCoverage: TODO."
903                     "ModulePass",
904                     false, false)
905 ModulePass *llvm::createSanitizerCoverageModulePass(
906     const SanitizerCoverageOptions &Options) {
907   return new SanitizerCoverageModule(Options);
908 }
909