LLVM  10.0.0
SanitizerCoverage.cpp
Go to the documentation of this file.
1 //===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Coverage instrumentation done on LLVM IR level, works with Sanitizers.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/SmallVector.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/InitializePasses.h"
37 #include "llvm/Support/Debug.h"
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "sancov"
46 
47 static const char *const SanCovTracePCIndirName =
48  "__sanitizer_cov_trace_pc_indir";
49 static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc";
50 static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1";
51 static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2";
52 static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4";
53 static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8";
54 static const char *const SanCovTraceConstCmp1 =
55  "__sanitizer_cov_trace_const_cmp1";
56 static const char *const SanCovTraceConstCmp2 =
57  "__sanitizer_cov_trace_const_cmp2";
58 static const char *const SanCovTraceConstCmp4 =
59  "__sanitizer_cov_trace_const_cmp4";
60 static const char *const SanCovTraceConstCmp8 =
61  "__sanitizer_cov_trace_const_cmp8";
62 static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4";
63 static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8";
64 static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep";
65 static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
66 static const char *const SanCovModuleCtorTracePcGuardName =
67  "sancov.module_ctor_trace_pc_guard";
68 static const char *const SanCovModuleCtor8bitCountersName =
69  "sancov.module_ctor_8bit_counters";
70 static const uint64_t SanCtorAndDtorPriority = 2;
71 
72 static const char *const SanCovTracePCGuardName =
73  "__sanitizer_cov_trace_pc_guard";
74 static const char *const SanCovTracePCGuardInitName =
75  "__sanitizer_cov_trace_pc_guard_init";
76 static const char *const SanCov8bitCountersInitName =
77  "__sanitizer_cov_8bit_counters_init";
78 static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init";
79 
80 static const char *const SanCovGuardsSectionName = "sancov_guards";
81 static const char *const SanCovCountersSectionName = "sancov_cntrs";
82 static const char *const SanCovPCsSectionName = "sancov_pcs";
83 
84 static const char *const SanCovLowestStackName = "__sancov_lowest_stack";
85 
87  "sanitizer-coverage-level",
88  cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
89  "3: all blocks and critical edges"),
90  cl::Hidden, cl::init(0));
91 
92 static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc",
93  cl::desc("Experimental pc tracing"), cl::Hidden,
94  cl::init(false));
95 
96 static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard",
97  cl::desc("pc tracing with a guard"),
98  cl::Hidden, cl::init(false));
99 
100 // If true, we create a global variable that contains PCs of all instrumented
101 // BBs, put this global into a named section, and pass this section's bounds
102 // to __sanitizer_cov_pcs_init.
103 // This way the coverage instrumentation does not need to acquire the PCs
104 // at run-time. Works with trace-pc-guard and inline-8bit-counters.
105 static cl::opt<bool> ClCreatePCTable("sanitizer-coverage-pc-table",
106  cl::desc("create a static PC table"),
107  cl::Hidden, cl::init(false));
108 
109 static cl::opt<bool>
110  ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
111  cl::desc("increments 8-bit counter for every edge"),
112  cl::Hidden, cl::init(false));
113 
114 static cl::opt<bool>
115  ClCMPTracing("sanitizer-coverage-trace-compares",
116  cl::desc("Tracing of CMP and similar instructions"),
117  cl::Hidden, cl::init(false));
118 
119 static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs",
120  cl::desc("Tracing of DIV instructions"),
121  cl::Hidden, cl::init(false));
122 
123 static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps",
124  cl::desc("Tracing of GEP instructions"),
125  cl::Hidden, cl::init(false));
126 
127 static cl::opt<bool>
128  ClPruneBlocks("sanitizer-coverage-prune-blocks",
129  cl::desc("Reduce the number of instrumented blocks"),
130  cl::Hidden, cl::init(true));
131 
132 static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth",
133  cl::desc("max stack depth tracing"),
134  cl::Hidden, cl::init(false));
135 
136 namespace {
137 
138 SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
140  switch (LegacyCoverageLevel) {
141  case 0:
143  break;
144  case 1:
146  break;
147  case 2:
149  break;
150  case 3:
152  break;
153  case 4:
155  Res.IndirectCalls = true;
156  break;
157  }
158  return Res;
159 }
160 
161 SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
162  // Sets CoverageType and IndirectCalls.
163  SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
164  Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
165  Options.IndirectCalls |= CLOpts.IndirectCalls;
166  Options.TraceCmp |= ClCMPTracing;
167  Options.TraceDiv |= ClDIVTracing;
168  Options.TraceGep |= ClGEPTracing;
169  Options.TracePC |= ClTracePC;
170  Options.TracePCGuard |= ClTracePCGuard;
172  Options.PCTable |= ClCreatePCTable;
173  Options.NoPrune |= !ClPruneBlocks;
174  Options.StackDepth |= ClStackDepth;
175  if (!Options.TracePCGuard && !Options.TracePC &&
176  !Options.Inline8bitCounters && !Options.StackDepth)
177  Options.TracePCGuard = true; // TracePCGuard is default.
178  return Options;
179 }
180 
182 using PostDomTreeCallback =
184 
185 class ModuleSanitizerCoverage {
186 public:
187  ModuleSanitizerCoverage(
189  : Options(OverrideFromCL(Options)) {}
190  bool instrumentModule(Module &M, DomTreeCallback DTCallback,
191  PostDomTreeCallback PDTCallback);
192 
193 private:
194  void instrumentFunction(Function &F, DomTreeCallback DTCallback,
195  PostDomTreeCallback PDTCallback);
196  void InjectCoverageForIndirectCalls(Function &F,
197  ArrayRef<Instruction *> IndirCalls);
198  void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
199  void InjectTraceForDiv(Function &F,
200  ArrayRef<BinaryOperator *> DivTraceTargets);
201  void InjectTraceForGep(Function &F,
202  ArrayRef<GetElementPtrInst *> GepTraceTargets);
203  void InjectTraceForSwitch(Function &F,
204  ArrayRef<Instruction *> SwitchTraceTargets);
205  bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
206  bool IsLeafFunc = true);
207  GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
208  Function &F, Type *Ty,
209  const char *Section);
210  GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
211  void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
212  void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
213  bool IsLeafFunc = true);
214  Function *CreateInitCallsForSections(Module &M, const char *CtorName,
215  const char *InitFunctionName, Type *Ty,
216  const char *Section);
217  std::pair<Value *, Value *> CreateSecStartEnd(Module &M, const char *Section,
218  Type *Ty);
219 
220  void SetNoSanitizeMetadata(Instruction *I) {
221  I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
222  MDNode::get(*C, None));
223  }
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  FunctionCallee SanCovTracePCIndir;
229  FunctionCallee SanCovTracePC, SanCovTracePCGuard;
230  FunctionCallee SanCovTraceCmpFunction[4];
231  FunctionCallee SanCovTraceConstCmpFunction[4];
232  FunctionCallee SanCovTraceDivFunction[2];
233  FunctionCallee SanCovTraceGepFunction;
234  FunctionCallee 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 class ModuleSanitizerCoverageLegacyPass : public ModulePass {
255 public:
256  ModuleSanitizerCoverageLegacyPass(
258  : ModulePass(ID), Options(Options) {
261  }
262  bool runOnModule(Module &M) override {
263  ModuleSanitizerCoverage ModuleSancov(Options);
264  auto DTCallback = [this](Function &F) -> const DominatorTree * {
265  return &this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
266  };
267  auto PDTCallback = [this](Function &F) -> const PostDominatorTree * {
268  return &this->getAnalysis<PostDominatorTreeWrapperPass>(F)
269  .getPostDomTree();
270  };
271  return ModuleSancov.instrumentModule(M, DTCallback, PDTCallback);
272  }
273 
274  static char ID; // Pass identification, replacement for typeid
275  StringRef getPassName() const override { return "ModuleSanitizerCoverage"; }
276 
277  void getAnalysisUsage(AnalysisUsage &AU) const override {
280  }
281 
282 private:
283  SanitizerCoverageOptions Options;
284 };
285 
286 } // namespace
287 
289  ModuleAnalysisManager &MAM) {
290  ModuleSanitizerCoverage ModuleSancov(Options);
291  auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
292  auto DTCallback = [&FAM](Function &F) -> const DominatorTree * {
293  return &FAM.getResult<DominatorTreeAnalysis>(F);
294  };
295  auto PDTCallback = [&FAM](Function &F) -> const PostDominatorTree * {
296  return &FAM.getResult<PostDominatorTreeAnalysis>(F);
297  };
298  if (ModuleSancov.instrumentModule(M, DTCallback, PDTCallback))
299  return PreservedAnalyses::none();
300  return PreservedAnalyses::all();
301 }
302 
303 std::pair<Value *, Value *>
304 ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
305  Type *Ty) {
306  GlobalVariable *SecStart =
307  new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
308  getSectionStart(Section));
310  GlobalVariable *SecEnd =
312  nullptr, getSectionEnd(Section));
314  IRBuilder<> IRB(M.getContext());
315  Value *SecEndPtr = IRB.CreatePointerCast(SecEnd, Ty);
316  if (!TargetTriple.isOSBinFormatCOFF())
317  return std::make_pair(IRB.CreatePointerCast(SecStart, Ty), SecEndPtr);
318 
319  // Account for the fact that on windows-msvc __start_* symbols actually
320  // point to a uint64_t before the start of the array.
321  auto SecStartI8Ptr = IRB.CreatePointerCast(SecStart, Int8PtrTy);
322  auto GEP = IRB.CreateGEP(Int8Ty, SecStartI8Ptr,
323  ConstantInt::get(IntptrTy, sizeof(uint64_t)));
324  return std::make_pair(IRB.CreatePointerCast(GEP, Ty), SecEndPtr);
325 }
326 
327 Function *ModuleSanitizerCoverage::CreateInitCallsForSections(
328  Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty,
329  const char *Section) {
330  auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
331  auto SecStart = SecStartEnd.first;
332  auto SecEnd = SecStartEnd.second;
333  Function *CtorFunc;
334  std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
335  M, CtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd});
336  assert(CtorFunc->getName() == CtorName);
337 
338  if (TargetTriple.supportsCOMDAT()) {
339  // Use comdat to dedup CtorFunc.
340  CtorFunc->setComdat(M.getOrInsertComdat(CtorName));
341  appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
342  } else {
344  }
345 
346  if (TargetTriple.isOSBinFormatCOFF()) {
347  // In COFF files, if the contructors are set as COMDAT (they are because
348  // COFF supports COMDAT) and the linker flag /OPT:REF (strip unreferenced
349  // functions and data) is used, the constructors get stripped. To prevent
350  // this, give the constructors weak ODR linkage and ensure the linker knows
351  // to include the sancov constructor. This way the linker can deduplicate
352  // the constructors but always leave one copy.
354  appendToUsed(M, CtorFunc);
355  }
356  return CtorFunc;
357 }
358 
359 bool ModuleSanitizerCoverage::instrumentModule(
360  Module &M, DomTreeCallback DTCallback, PostDomTreeCallback PDTCallback) {
362  return false;
363  C = &(M.getContext());
364  DL = &M.getDataLayout();
365  CurModule = &M;
366  CurModuleUniqueId = getUniqueModuleId(CurModule);
367  TargetTriple = Triple(M.getTargetTriple());
368  FunctionGuardArray = nullptr;
369  Function8bitCounterArray = nullptr;
370  FunctionPCsArray = nullptr;
371  IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
372  IntptrPtrTy = PointerType::getUnqual(IntptrTy);
373  Type *VoidTy = Type::getVoidTy(*C);
374  IRBuilder<> IRB(*C);
375  Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty());
376  Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
377  Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
378  Int64Ty = IRB.getInt64Ty();
379  Int32Ty = IRB.getInt32Ty();
380  Int16Ty = IRB.getInt16Ty();
381  Int8Ty = IRB.getInt8Ty();
382 
383  SanCovTracePCIndir =
384  M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy);
385  // Make sure smaller parameters are zero-extended to i64 as required by the
386  // x86_64 ABI.
387  AttributeList SanCovTraceCmpZeroExtAL;
388  if (TargetTriple.getArch() == Triple::x86_64) {
389  SanCovTraceCmpZeroExtAL =
390  SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 0, Attribute::ZExt);
391  SanCovTraceCmpZeroExtAL =
392  SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 1, Attribute::ZExt);
393  }
394 
395  SanCovTraceCmpFunction[0] =
396  M.getOrInsertFunction(SanCovTraceCmp1, SanCovTraceCmpZeroExtAL, VoidTy,
397  IRB.getInt8Ty(), IRB.getInt8Ty());
398  SanCovTraceCmpFunction[1] =
399  M.getOrInsertFunction(SanCovTraceCmp2, SanCovTraceCmpZeroExtAL, VoidTy,
400  IRB.getInt16Ty(), IRB.getInt16Ty());
401  SanCovTraceCmpFunction[2] =
402  M.getOrInsertFunction(SanCovTraceCmp4, SanCovTraceCmpZeroExtAL, VoidTy,
403  IRB.getInt32Ty(), IRB.getInt32Ty());
404  SanCovTraceCmpFunction[3] =
405  M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty);
406 
407  SanCovTraceConstCmpFunction[0] = M.getOrInsertFunction(
408  SanCovTraceConstCmp1, SanCovTraceCmpZeroExtAL, VoidTy, Int8Ty, Int8Ty);
409  SanCovTraceConstCmpFunction[1] = M.getOrInsertFunction(
410  SanCovTraceConstCmp2, SanCovTraceCmpZeroExtAL, VoidTy, Int16Ty, Int16Ty);
411  SanCovTraceConstCmpFunction[2] = M.getOrInsertFunction(
412  SanCovTraceConstCmp4, SanCovTraceCmpZeroExtAL, VoidTy, Int32Ty, Int32Ty);
413  SanCovTraceConstCmpFunction[3] =
414  M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty);
415 
416  {
418  if (TargetTriple.getArch() == Triple::x86_64)
419  AL = AL.addParamAttribute(*C, 0, Attribute::ZExt);
420  SanCovTraceDivFunction[0] =
421  M.getOrInsertFunction(SanCovTraceDiv4, AL, VoidTy, IRB.getInt32Ty());
422  }
423  SanCovTraceDivFunction[1] =
424  M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
425  SanCovTraceGepFunction =
426  M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
427  SanCovTraceSwitchFunction =
428  M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy);
429 
430  Constant *SanCovLowestStackConstant =
431  M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
432  SanCovLowestStack = dyn_cast<GlobalVariable>(SanCovLowestStackConstant);
433  if (!SanCovLowestStack) {
434  C->emitError(StringRef("'") + SanCovLowestStackName +
435  "' should not be declared by the user");
436  return true;
437  }
438  SanCovLowestStack->setThreadLocalMode(
439  GlobalValue::ThreadLocalMode::InitialExecTLSModel);
440  if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
441  SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
442 
443  // We insert an empty inline asm after cov callbacks to avoid callback merge.
444  EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
445  StringRef(""), StringRef(""),
446  /*hasSideEffects=*/true);
447 
448  SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
449  SanCovTracePCGuard =
450  M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy);
451 
452  for (auto &F : M)
453  instrumentFunction(F, DTCallback, PDTCallback);
454 
455  Function *Ctor = nullptr;
456 
457  if (FunctionGuardArray)
458  Ctor = CreateInitCallsForSections(M, SanCovModuleCtorTracePcGuardName,
459  SanCovTracePCGuardInitName, Int32PtrTy,
460  SanCovGuardsSectionName);
461  if (Function8bitCounterArray)
462  Ctor = CreateInitCallsForSections(M, SanCovModuleCtor8bitCountersName,
463  SanCov8bitCountersInitName, Int8PtrTy,
464  SanCovCountersSectionName);
465  if (Ctor && Options.PCTable) {
466  auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
468  M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
469  IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
470  IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
471  }
472  // We don't reference these arrays directly in any of our runtime functions,
473  // so we need to prevent them from being dead stripped.
474  if (TargetTriple.isOSBinFormatMachO())
475  appendToUsed(M, GlobalsToAppendToUsed);
476  appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed);
477  return true;
478 }
479 
480 // True if block has successors and it dominates all of them.
481 static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) {
482  if (succ_begin(BB) == succ_end(BB))
483  return false;
484 
485  for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) {
486  if (!DT->dominates(BB, SUCC))
487  return false;
488  }
489 
490  return true;
491 }
492 
493 // True if block has predecessors and it postdominates all of them.
494 static bool isFullPostDominator(const BasicBlock *BB,
495  const PostDominatorTree *PDT) {
496  if (pred_begin(BB) == pred_end(BB))
497  return false;
498 
499  for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) {
500  if (!PDT->dominates(BB, PRED))
501  return false;
502  }
503 
504  return true;
505 }
506 
507 static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
508  const DominatorTree *DT,
509  const PostDominatorTree *PDT,
510  const SanitizerCoverageOptions &Options) {
511  // Don't insert coverage for blocks containing nothing but unreachable: we
512  // will never call __sanitizer_cov() for them, so counting them in
513  // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
514  // percentage. Also, unreachable instructions frequently have no debug
515  // locations.
516  if (isa<UnreachableInst>(BB->getFirstNonPHIOrDbgOrLifetime()))
517  return false;
518 
519  // Don't insert coverage into blocks without a valid insertion point
520  // (catchswitch blocks).
521  if (BB->getFirstInsertionPt() == BB->end())
522  return false;
523 
524  if (Options.NoPrune || &F.getEntryBlock() == BB)
525  return true;
526 
528  &F.getEntryBlock() != BB)
529  return false;
530 
531  // Do not instrument full dominators, or full post-dominators with multiple
532  // predecessors.
533  return !isFullDominator(BB, DT)
534  && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
535 }
536 
537 
538 // Returns true iff From->To is a backedge.
539 // A twist here is that we treat From->To as a backedge if
540 // * To dominates From or
541 // * To->UniqueSuccessor dominates From
543  const DominatorTree *DT) {
544  if (DT->dominates(To, From))
545  return true;
546  if (auto Next = To->getUniqueSuccessor())
547  if (DT->dominates(Next, From))
548  return true;
549  return false;
550 }
551 
552 // Prunes uninteresting Cmp instrumentation:
553 // * CMP instructions that feed into loop backedge branch.
554 //
555 // Note that Cmp pruning is controlled by the same flag as the
556 // BB pruning.
557 static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree *DT,
558  const SanitizerCoverageOptions &Options) {
559  if (!Options.NoPrune)
560  if (CMP->hasOneUse())
561  if (auto BR = dyn_cast<BranchInst>(CMP->user_back()))
562  for (BasicBlock *B : BR->successors())
563  if (IsBackEdge(BR->getParent(), B, DT))
564  return false;
565  return true;
566 }
567 
568 void ModuleSanitizerCoverage::instrumentFunction(
569  Function &F, DomTreeCallback DTCallback, PostDomTreeCallback PDTCallback) {
570  if (F.empty())
571  return;
572  if (F.getName().find(".module_ctor") != std::string::npos)
573  return; // Should not instrument sanitizer init functions.
574  if (F.getName().startswith("__sanitizer_"))
575  return; // Don't instrument __sanitizer_* callbacks.
576  // Don't touch available_externally functions, their actual body is elewhere.
578  return;
579  // Don't instrument MSVC CRT configuration helpers. They may run before normal
580  // initialization.
581  if (F.getName() == "__local_stdio_printf_options" ||
582  F.getName() == "__local_stdio_scanf_options")
583  return;
584  if (isa<UnreachableInst>(F.getEntryBlock().getTerminator()))
585  return;
586  // Don't instrument functions using SEH for now. Splitting basic blocks like
587  // we do for coverage breaks WinEHPrepare.
588  // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
589  if (F.hasPersonalityFn() &&
591  return;
593  SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions().setIgnoreUnreachableDests());
595  SmallVector<BasicBlock *, 16> BlocksToInstrument;
596  SmallVector<Instruction *, 8> CmpTraceTargets;
597  SmallVector<Instruction *, 8> SwitchTraceTargets;
598  SmallVector<BinaryOperator *, 8> DivTraceTargets;
599  SmallVector<GetElementPtrInst *, 8> GepTraceTargets;
600 
601  const DominatorTree *DT = DTCallback(F);
602  const PostDominatorTree *PDT = PDTCallback(F);
603  bool IsLeafFunc = true;
604 
605  for (auto &BB : F) {
606  if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
607  BlocksToInstrument.push_back(&BB);
608  for (auto &Inst : BB) {
609  if (Options.IndirectCalls) {
610  CallSite CS(&Inst);
611  if (CS && !CS.getCalledFunction())
612  IndirCalls.push_back(&Inst);
613  }
614  if (Options.TraceCmp) {
615  if (ICmpInst *CMP = dyn_cast<ICmpInst>(&Inst))
616  if (IsInterestingCmp(CMP, DT, Options))
617  CmpTraceTargets.push_back(&Inst);
618  if (isa<SwitchInst>(&Inst))
619  SwitchTraceTargets.push_back(&Inst);
620  }
621  if (Options.TraceDiv)
622  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst))
623  if (BO->getOpcode() == Instruction::SDiv ||
624  BO->getOpcode() == Instruction::UDiv)
625  DivTraceTargets.push_back(BO);
626  if (Options.TraceGep)
627  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst))
628  GepTraceTargets.push_back(GEP);
629  if (Options.StackDepth)
630  if (isa<InvokeInst>(Inst) ||
631  (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst)))
632  IsLeafFunc = false;
633  }
634  }
635 
636  InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
637  InjectCoverageForIndirectCalls(F, IndirCalls);
638  InjectTraceForCmp(F, CmpTraceTargets);
639  InjectTraceForSwitch(F, SwitchTraceTargets);
640  InjectTraceForDiv(F, DivTraceTargets);
641  InjectTraceForGep(F, GepTraceTargets);
642 }
643 
644 GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
645  size_t NumElements, Function &F, Type *Ty, const char *Section) {
646  ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
647  auto Array = new GlobalVariable(
648  *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
649  Constant::getNullValue(ArrayTy), "__sancov_gen_");
650 
651  if (TargetTriple.supportsCOMDAT() && !F.isInterposable())
652  if (auto Comdat =
653  GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId))
654  Array->setComdat(Comdat);
655  Array->setSection(getSectionName(Section));
656  Array->setAlignment(Align(Ty->isPointerTy()
657  ? DL->getPointerSize()
658  : Ty->getPrimitiveSizeInBits() / 8));
659  GlobalsToAppendToUsed.push_back(Array);
660  GlobalsToAppendToCompilerUsed.push_back(Array);
662  Array->addMetadata(LLVMContext::MD_associated, *MD);
663 
664  return Array;
665 }
666 
668 ModuleSanitizerCoverage::CreatePCArray(Function &F,
669  ArrayRef<BasicBlock *> AllBlocks) {
670  size_t N = AllBlocks.size();
671  assert(N);
674  for (size_t i = 0; i < N; i++) {
675  if (&F.getEntryBlock() == AllBlocks[i]) {
676  PCs.push_back((Constant *)IRB.CreatePointerCast(&F, IntptrPtrTy));
677  PCs.push_back((Constant *)IRB.CreateIntToPtr(
678  ConstantInt::get(IntptrTy, 1), IntptrPtrTy));
679  } else {
680  PCs.push_back((Constant *)IRB.CreatePointerCast(
681  BlockAddress::get(AllBlocks[i]), IntptrPtrTy));
682  PCs.push_back((Constant *)IRB.CreateIntToPtr(
683  ConstantInt::get(IntptrTy, 0), IntptrPtrTy));
684  }
685  }
686  auto *PCArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy,
687  SanCovPCsSectionName);
688  PCArray->setInitializer(
689  ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs));
690  PCArray->setConstant(true);
691 
692  return PCArray;
693 }
694 
695 void ModuleSanitizerCoverage::CreateFunctionLocalArrays(
696  Function &F, ArrayRef<BasicBlock *> AllBlocks) {
697  if (Options.TracePCGuard)
698  FunctionGuardArray = CreateFunctionLocalArrayInSection(
699  AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
700 
701  if (Options.Inline8bitCounters)
702  Function8bitCounterArray = CreateFunctionLocalArrayInSection(
703  AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
704 
705  if (Options.PCTable)
706  FunctionPCsArray = CreatePCArray(F, AllBlocks);
707 }
708 
709 bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
710  ArrayRef<BasicBlock *> AllBlocks,
711  bool IsLeafFunc) {
712  if (AllBlocks.empty()) return false;
713  CreateFunctionLocalArrays(F, AllBlocks);
714  for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
715  InjectCoverageAtBlock(F, *AllBlocks[i], i, IsLeafFunc);
716  return true;
717 }
718 
719 // On every indirect call we call a run-time function
720 // __sanitizer_cov_indir_call* with two parameters:
721 // - callee address,
722 // - global cache array that contains CacheSize pointers (zero-initialized).
723 // The cache is used to speed up recording the caller-callee pairs.
724 // The address of the caller is passed implicitly via caller PC.
725 // CacheSize is encoded in the name of the run-time function.
726 void ModuleSanitizerCoverage::InjectCoverageForIndirectCalls(
727  Function &F, ArrayRef<Instruction *> IndirCalls) {
728  if (IndirCalls.empty())
729  return;
730  assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters);
731  for (auto I : IndirCalls) {
732  IRBuilder<> IRB(I);
733  CallSite CS(I);
734  Value *Callee = CS.getCalledValue();
735  if (isa<InlineAsm>(Callee))
736  continue;
737  IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
738  }
739 }
740 
741 // For every switch statement we insert a call:
742 // __sanitizer_cov_trace_switch(CondValue,
743 // {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
744 
745 void ModuleSanitizerCoverage::InjectTraceForSwitch(
746  Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
747  for (auto I : SwitchTraceTargets) {
748  if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
749  IRBuilder<> IRB(I);
750  SmallVector<Constant *, 16> Initializers;
751  Value *Cond = SI->getCondition();
752  if (Cond->getType()->getScalarSizeInBits() >
753  Int64Ty->getScalarSizeInBits())
754  continue;
755  Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
756  Initializers.push_back(
757  ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
758  if (Cond->getType()->getScalarSizeInBits() <
759  Int64Ty->getScalarSizeInBits())
760  Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
761  for (auto It : SI->cases()) {
762  Constant *C = It.getCaseValue();
763  if (C->getType()->getScalarSizeInBits() <
764  Int64Ty->getScalarSizeInBits())
765  C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
766  Initializers.push_back(C);
767  }
768  llvm::sort(Initializers.begin() + 2, Initializers.end(),
769  [](const Constant *A, const Constant *B) {
770  return cast<ConstantInt>(A)->getLimitedValue() <
771  cast<ConstantInt>(B)->getLimitedValue();
772  });
773  ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
774  GlobalVariable *GV = new GlobalVariable(
775  *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
776  ConstantArray::get(ArrayOfInt64Ty, Initializers),
777  "__sancov_gen_cov_switch_values");
778  IRB.CreateCall(SanCovTraceSwitchFunction,
779  {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)});
780  }
781  }
782 }
783 
784 void ModuleSanitizerCoverage::InjectTraceForDiv(
785  Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
786  for (auto BO : DivTraceTargets) {
787  IRBuilder<> IRB(BO);
788  Value *A1 = BO->getOperand(1);
789  if (isa<ConstantInt>(A1)) continue;
790  if (!A1->getType()->isIntegerTy())
791  continue;
792  uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
793  int CallbackIdx = TypeSize == 32 ? 0 :
794  TypeSize == 64 ? 1 : -1;
795  if (CallbackIdx < 0) continue;
796  auto Ty = Type::getIntNTy(*C, TypeSize);
797  IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
798  {IRB.CreateIntCast(A1, Ty, true)});
799  }
800 }
801 
802 void ModuleSanitizerCoverage::InjectTraceForGep(
803  Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
804  for (auto GEP : GepTraceTargets) {
805  IRBuilder<> IRB(GEP);
806  for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I)
807  if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy())
808  IRB.CreateCall(SanCovTraceGepFunction,
809  {IRB.CreateIntCast(*I, IntptrTy, true)});
810  }
811 }
812 
813 void ModuleSanitizerCoverage::InjectTraceForCmp(
814  Function &, ArrayRef<Instruction *> CmpTraceTargets) {
815  for (auto I : CmpTraceTargets) {
816  if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
817  IRBuilder<> IRB(ICMP);
818  Value *A0 = ICMP->getOperand(0);
819  Value *A1 = ICMP->getOperand(1);
820  if (!A0->getType()->isIntegerTy())
821  continue;
822  uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
823  int CallbackIdx = TypeSize == 8 ? 0 :
824  TypeSize == 16 ? 1 :
825  TypeSize == 32 ? 2 :
826  TypeSize == 64 ? 3 : -1;
827  if (CallbackIdx < 0) continue;
828  // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
829  auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
830  bool FirstIsConst = isa<ConstantInt>(A0);
831  bool SecondIsConst = isa<ConstantInt>(A1);
832  // If both are const, then we don't need such a comparison.
833  if (FirstIsConst && SecondIsConst) continue;
834  // If only one is const, then make it the first callback argument.
835  if (FirstIsConst || SecondIsConst) {
836  CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
837  if (SecondIsConst)
838  std::swap(A0, A1);
839  }
840 
841  auto Ty = Type::getIntNTy(*C, TypeSize);
842  IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true),
843  IRB.CreateIntCast(A1, Ty, true)});
844  }
845  }
846 }
847 
848 void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
849  size_t Idx,
850  bool IsLeafFunc) {
852  bool IsEntryBB = &BB == &F.getEntryBlock();
853  DebugLoc EntryLoc;
854  if (IsEntryBB) {
855  if (auto SP = F.getSubprogram())
856  EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
857  // Keep static allocas and llvm.localescape calls in the entry block. Even
858  // if we aren't splitting the block, it's nice for allocas to be before
859  // calls.
860  IP = PrepareToSplitEntryBlock(BB, IP);
861  } else {
862  EntryLoc = IP->getDebugLoc();
863  }
864 
865  IRBuilder<> IRB(&*IP);
866  IRB.SetCurrentDebugLocation(EntryLoc);
867  if (Options.TracePC) {
868  IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
869  IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
870  }
871  if (Options.TracePCGuard) {
872  auto GuardPtr = IRB.CreateIntToPtr(
873  IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
874  ConstantInt::get(IntptrTy, Idx * 4)),
875  Int32PtrTy);
876  IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
877  IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
878  }
879  if (Options.Inline8bitCounters) {
880  auto CounterPtr = IRB.CreateGEP(
881  Function8bitCounterArray->getValueType(), Function8bitCounterArray,
882  {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
883  auto Load = IRB.CreateLoad(Int8Ty, CounterPtr);
884  auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
885  auto Store = IRB.CreateStore(Inc, CounterPtr);
886  SetNoSanitizeMetadata(Load);
887  SetNoSanitizeMetadata(Store);
888  }
889  if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
890  // Check stack depth. If it's the deepest so far, record it.
891  Module *M = F.getParent();
892  Function *GetFrameAddr = Intrinsic::getDeclaration(
893  M, Intrinsic::frameaddress,
895  auto FrameAddrPtr =
896  IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
897  auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
898  auto LowestStack = IRB.CreateLoad(IntptrTy, SanCovLowestStack);
899  auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
900  auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
901  IRBuilder<> ThenIRB(ThenTerm);
902  auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
903  SetNoSanitizeMetadata(LowestStack);
904  SetNoSanitizeMetadata(Store);
905  }
906 }
907 
908 std::string
909 ModuleSanitizerCoverage::getSectionName(const std::string &Section) const {
910  if (TargetTriple.isOSBinFormatCOFF()) {
911  if (Section == SanCovCountersSectionName)
912  return ".SCOV$CM";
913  if (Section == SanCovPCsSectionName)
914  return ".SCOVP$M";
915  return ".SCOV$GM"; // For SanCovGuardsSectionName.
916  }
917  if (TargetTriple.isOSBinFormatMachO())
918  return "__DATA,__" + Section;
919  return "__" + Section;
920 }
921 
922 std::string
923 ModuleSanitizerCoverage::getSectionStart(const std::string &Section) const {
924  if (TargetTriple.isOSBinFormatMachO())
925  return "\1section$start$__DATA$__" + Section;
926  return "__start___" + Section;
927 }
928 
929 std::string
930 ModuleSanitizerCoverage::getSectionEnd(const std::string &Section) const {
931  if (TargetTriple.isOSBinFormatMachO())
932  return "\1section$end$__DATA$__" + Section;
933  return "__stop___" + Section;
934 }
935 
937 INITIALIZE_PASS_BEGIN(ModuleSanitizerCoverageLegacyPass, "sancov",
938  "Pass for instrumenting coverage on functions", false,
939  false)
942 INITIALIZE_PASS_END(ModuleSanitizerCoverageLegacyPass, "sancov",
943  "Pass for instrumenting coverage on functions", false,
944  false)
946  const SanitizerCoverageOptions &Options) {
947  return new ModuleSanitizerCoverageLegacyPass(Options);
948 }
IntegerType * getInt16Ty()
Fetch the type representing a 16-bit integer.
Definition: IRBuilder.h:390
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:242
static const char *const SanCovPCsSectionName
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:78
uint64_t CallInst * C
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:241
bool empty() const
Definition: Function.h:697
static const uint64_t SanCtorAndDtorPriority
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static const char *const SanCovModuleCtor8bitCountersName
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static const char *const SanCovTraceConstCmp1
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2262
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1669
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
static const char *const SanCovPCsInitName
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:277
Available for inspection, not emission.
Definition: GlobalValue.h:49
static bool isFullPostDominator(const BasicBlock *BB, const PostDominatorTree *PDT)
static const char *const SanCovGuardsSectionName
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
Externally visible function.
Definition: GlobalValue.h:48
bool isInterposable() const
Return true if this global&#39;s definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:429
static cl::opt< bool > ClStackDepth("sanitizer-coverage-stack-depth", cl::desc("max stack depth tracing"), cl::Hidden, cl::init(false))
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:872
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
F(f)
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:111
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found. ...
static const char *const SanCovTracePCName
Hexagon Common GEP
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:144
static const char *const SanCovTraceSwitchName
INITIALIZE_PASS_BEGIN(ModuleSanitizerCoverageLegacyPass, "sancov", "Pass for instrumenting coverage on functions", false, false) INITIALIZE_PASS_END(ModuleSanitizerCoverageLegacyPass
static const char *const SanCovTraceConstCmp2
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1065
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:318
static const char *const SanCov8bitCountersInitName
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:395
unsigned getAllocaAddrSpace() const
Definition: DataLayout.h:273
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Option class for critical edge splitting.
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:400
static const char *const SanCovTraceCmp2
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
ValTy * getCalledValue() const
Return the pointer to function that is being called.
Definition: CallSite.h:104
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
static const char *const SanCovLowestStackName
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1211
static cl::opt< int > ClCoverageLevel("sanitizer-coverage-level", cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, " "3: all blocks and critical edges"), cl::Hidden, cl::init(0))
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:428
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:102
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1698
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2096
static const char *const SanCovTraceGep
const Instruction * getFirstNonPHIOrDbgOrLifetime() const
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic...
Definition: BasicBlock.cpp:210
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
static cl::opt< bool > ClDIVTracing("sanitizer-coverage-trace-divs", cl::desc("Tracing of DIV instructions"), cl::Hidden, cl::init(false))
Class to represent array types.
Definition: DerivedTypes.h:408
void setComdat(Comdat *C)
Definition: GlobalObject.h:122
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
static cl::opt< bool > ClCMPTracing("sanitizer-coverage-trace-compares", cl::desc("Tracing of CMP and similar instructions"), cl::Hidden, cl::init(false))
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:743
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:156
LinkageTypes getLinkage() const
Definition: GlobalValue.h:460
static const char *const SanCovTracePCGuardName
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:105
const BasicBlock & getEntryBlock() const
Definition: Function.h:675
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:892
static const char *const SanCovTracePCIndirName
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:687
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:223
static const char *const SanCovTraceCmp1
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:240
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:413
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1525
static cl::opt< bool > ClCreatePCTable("sanitizer-coverage-pc-table", cl::desc("create a static PC table"), cl::Hidden, cl::init(false))
static const char *const SanCovTraceDiv4
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1533
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
This is an important base class in LLVM.
Definition: Constant.h:41
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
static const char *const SanCovTraceConstCmp8
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:306
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:165
This instruction compares its operands according to the predicate given to the constructor.
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
static cl::opt< bool > ClPruneBlocks("sanitizer-coverage-prune-blocks", cl::desc("Reduce the number of instrumented blocks"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClTracePCGuard("sanitizer-coverage-trace-pc-guard", cl::desc("pc tracing with a guard"), cl::Hidden, cl::init(false))
static const char *const SanCovTraceCmp4
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:372
std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module&#39;s strong...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:498
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
static const char *const SanCovTraceCmp8
size_t size() const
Definition: SmallVector.h:52
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:433
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1224
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1114
static const char *const SanCovModuleCtorTracePcGuardName
Value * CreateGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1808
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:594
BlockVerifier::State From
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
iterator end()
Definition: BasicBlock.h:275
static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:134
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2170
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:348
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:249
Analysis pass which computes a PostDominatorTree.
Module.h This file contains the declarations for the Module class.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:63
static const char *const SanCovCountersSectionName
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:184
enum llvm::SanitizerCoverageOptions::Type CoverageType
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:64
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB, const DominatorTree *DT, const PostDominatorTree *PDT, const SanitizerCoverageOptions &Options)
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:454
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
amdgpu Simplify well known AMD library false FunctionCallee Callee
Comdat * GetOrCreateFunctionComdat(Function &F, Triple &T, const std::string &ModuleId)
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:385
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1629
void initializeModuleSanitizerCoverageLegacyPassPass(PassRegistry &)
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
static cl::opt< bool > ClTracePC("sanitizer-coverage-trace-pc", cl::desc("Experimental pc tracing"), cl::Hidden, cl::init(false))
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2147
FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type *> InitArgTypes)
static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree *DT, const SanitizerCoverageOptions &Options)
ModulePass * createModuleSanitizerCoverageLegacyPassPass(const SanitizerCoverageOptions &Options=SanitizerCoverageOptions())
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Constant * getOrInsertGlobal(StringRef Name, Type *Ty, function_ref< GlobalVariable *()> CreateGlobalCallback)
Look up the specified global in the module symbol table.
Definition: Module.cpp:204
static cl::opt< bool > ClGEPTracing("sanitizer-coverage-trace-geps", cl::desc("Tracing of GEP instructions"), cl::Hidden, cl::init(false))
static const char *const SanCovTraceDiv8
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
static bool IsBackEdge(BasicBlock *From, BasicBlock *To, const DominatorTree *DT)
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:222
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:587
Pass for instrumenting coverage on functions
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2419
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition: InlineAsm.cpp:42
static cl::opt< bool > ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters", cl::desc("increments 8-bit counter for every edge"), cl::Hidden, cl::init(false))
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2091
Multiway switch.
unsigned getMDKindID(StringRef Name) const
Return a unique non-zero ID for the specified metadata kind.
Definition: Module.cpp:119
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
aarch64 promote const
static const char *const SanCovTracePCGuardInitName
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1484
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:433
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type *> InitArgTypes, ArrayRef< Value *> InitArgs, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function, and calls sanitizer&#39;s init function from it.
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
static const char *const SanCovTraceConstCmp4
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:153
IntegerType * Int32Ty
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:1045
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:283