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 
getOptions(int LegacyCoverageLevel)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 
OverrideFromCL(SanitizerCoverageOptions Options)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:
SanitizerCoverageModule(const SanitizerCoverageOptions & Options=SanitizerCoverageOptions ())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
getPassName() const187   StringRef getPassName() const override { return "SanitizerCoverageModule"; }
188 
getAnalysisUsage(AnalysisUsage & AU) const189   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<Value *, Value *> CreateSecStartEnd(Module &M, const char *Section,
216                                                 Type *Ty);
217 
SetNoSanitizeMetadata(Instruction * I)218   void SetNoSanitizeMetadata(Instruction *I) {
219     I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
220                    MDNode::get(*C, None));
221   }
222 
223   std::string getSectionName(const std::string &Section) const;
224   std::string getSectionStart(const std::string &Section) const;
225   std::string getSectionEnd(const std::string &Section) const;
226   Function *SanCovTracePCIndir;
227   Function *SanCovTracePC, *SanCovTracePCGuard;
228   Function *SanCovTraceCmpFunction[4];
229   Function *SanCovTraceConstCmpFunction[4];
230   Function *SanCovTraceDivFunction[2];
231   Function *SanCovTraceGepFunction;
232   Function *SanCovTraceSwitchFunction;
233   GlobalVariable *SanCovLowestStack;
234   InlineAsm *EmptyAsm;
235   Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
236       *Int16Ty, *Int8Ty, *Int8PtrTy;
237   Module *CurModule;
238   std::string CurModuleUniqueId;
239   Triple TargetTriple;
240   LLVMContext *C;
241   const DataLayout *DL;
242 
243   GlobalVariable *FunctionGuardArray;  // for trace-pc-guard.
244   GlobalVariable *Function8bitCounterArray;  // for inline-8bit-counters.
245   GlobalVariable *FunctionPCsArray;  // for pc-table.
246   SmallVector<GlobalValue *, 20> GlobalsToAppendToUsed;
247   SmallVector<GlobalValue *, 20> GlobalsToAppendToCompilerUsed;
248 
249   SanitizerCoverageOptions Options;
250 };
251 
252 } // namespace
253 
254 std::pair<Value *, Value *>
CreateSecStartEnd(Module & M,const char * Section,Type * Ty)255 SanitizerCoverageModule::CreateSecStartEnd(Module &M, const char *Section,
256                                            Type *Ty) {
257   GlobalVariable *SecStart =
258       new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
259                          getSectionStart(Section));
260   SecStart->setVisibility(GlobalValue::HiddenVisibility);
261   GlobalVariable *SecEnd =
262       new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
263                          nullptr, getSectionEnd(Section));
264   SecEnd->setVisibility(GlobalValue::HiddenVisibility);
265   IRBuilder<> IRB(M.getContext());
266   Value *SecEndPtr = IRB.CreatePointerCast(SecEnd, Ty);
267   if (!TargetTriple.isOSBinFormatCOFF())
268     return std::make_pair(IRB.CreatePointerCast(SecStart, Ty), SecEndPtr);
269 
270   // Account for the fact that on windows-msvc __start_* symbols actually
271   // point to a uint64_t before the start of the array.
272   auto SecStartI8Ptr = IRB.CreatePointerCast(SecStart, Int8PtrTy);
273   auto GEP = IRB.CreateGEP(SecStartI8Ptr,
274                            ConstantInt::get(IntptrTy, sizeof(uint64_t)));
275   return std::make_pair(IRB.CreatePointerCast(GEP, Ty), SecEndPtr);
276 }
277 
CreateInitCallsForSections(Module & M,const char * InitFunctionName,Type * Ty,const char * Section)278 Function *SanitizerCoverageModule::CreateInitCallsForSections(
279     Module &M, const char *InitFunctionName, Type *Ty,
280     const char *Section) {
281   auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
282   auto SecStart = SecStartEnd.first;
283   auto SecEnd = SecStartEnd.second;
284   Function *CtorFunc;
285   std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
286       M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd});
287 
288   if (TargetTriple.supportsCOMDAT()) {
289     // Use comdat to dedup CtorFunc.
290     CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName));
291     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
292   } else {
293     appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
294   }
295 
296   if (TargetTriple.isOSBinFormatCOFF()) {
297     // In COFF files, if the contructors are set as COMDAT (they are because
298     // COFF supports COMDAT) and the linker flag /OPT:REF (strip unreferenced
299     // functions and data) is used, the constructors get stripped. To prevent
300     // this, give the constructors weak ODR linkage and ensure the linker knows
301     // to include the sancov constructor. This way the linker can deduplicate
302     // the constructors but always leave one copy.
303     CtorFunc->setLinkage(GlobalValue::WeakODRLinkage);
304     appendToUsed(M, CtorFunc);
305   }
306   return CtorFunc;
307 }
308 
runOnModule(Module & M)309 bool SanitizerCoverageModule::runOnModule(Module &M) {
310   if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
311     return false;
312   C = &(M.getContext());
313   DL = &M.getDataLayout();
314   CurModule = &M;
315   CurModuleUniqueId = getUniqueModuleId(CurModule);
316   TargetTriple = Triple(M.getTargetTriple());
317   FunctionGuardArray = nullptr;
318   Function8bitCounterArray = nullptr;
319   FunctionPCsArray = nullptr;
320   IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
321   IntptrPtrTy = PointerType::getUnqual(IntptrTy);
322   Type *VoidTy = Type::getVoidTy(*C);
323   IRBuilder<> IRB(*C);
324   Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty());
325   Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
326   Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
327   Int64Ty = IRB.getInt64Ty();
328   Int32Ty = IRB.getInt32Ty();
329   Int16Ty = IRB.getInt16Ty();
330   Int8Ty = IRB.getInt8Ty();
331 
332   SanCovTracePCIndir = checkSanitizerInterfaceFunction(
333       M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
334   SanCovTraceCmpFunction[0] =
335       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
336           SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
337   SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
338       M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
339                             IRB.getInt16Ty()));
340   SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
341       M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
342                             IRB.getInt32Ty()));
343   SanCovTraceCmpFunction[3] =
344       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
345           SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
346 
347   SanCovTraceConstCmpFunction[0] =
348       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
349           SanCovTraceConstCmp1, VoidTy, Int8Ty, Int8Ty));
350   SanCovTraceConstCmpFunction[1] =
351       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
352           SanCovTraceConstCmp2, VoidTy, Int16Ty, Int16Ty));
353   SanCovTraceConstCmpFunction[2] =
354       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
355           SanCovTraceConstCmp4, VoidTy, Int32Ty, Int32Ty));
356   SanCovTraceConstCmpFunction[3] =
357       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
358           SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty));
359 
360   SanCovTraceDivFunction[0] =
361       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
362           SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
363   SanCovTraceDivFunction[1] =
364       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
365           SanCovTraceDiv8, VoidTy, Int64Ty));
366   SanCovTraceGepFunction =
367       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
368           SanCovTraceGep, VoidTy, IntptrTy));
369   SanCovTraceSwitchFunction =
370       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
371           SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
372 
373   Constant *SanCovLowestStackConstant =
374       M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
375   SanCovLowestStack = cast<GlobalVariable>(SanCovLowestStackConstant);
376   SanCovLowestStack->setThreadLocalMode(
377       GlobalValue::ThreadLocalMode::InitialExecTLSModel);
378   if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
379     SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
380 
381   // Make sure smaller parameters are zero-extended to i64 as required by the
382   // x86_64 ABI.
383   if (TargetTriple.getArch() == Triple::x86_64) {
384     for (int i = 0; i < 3; i++) {
385       SanCovTraceCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
386       SanCovTraceCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
387       SanCovTraceConstCmpFunction[i]->addParamAttr(0, Attribute::ZExt);
388       SanCovTraceConstCmpFunction[i]->addParamAttr(1, Attribute::ZExt);
389     }
390     SanCovTraceDivFunction[0]->addParamAttr(0, Attribute::ZExt);
391   }
392 
393 
394   // We insert an empty inline asm after cov callbacks to avoid callback merge.
395   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
396                             StringRef(""), StringRef(""),
397                             /*hasSideEffects=*/true);
398 
399   SanCovTracePC = checkSanitizerInterfaceFunction(
400       M.getOrInsertFunction(SanCovTracePCName, VoidTy));
401   SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
402       SanCovTracePCGuardName, VoidTy, Int32PtrTy));
403 
404   for (auto &F : M)
405     runOnFunction(F);
406 
407   Function *Ctor = nullptr;
408 
409   if (FunctionGuardArray)
410     Ctor = CreateInitCallsForSections(M, SanCovTracePCGuardInitName, Int32PtrTy,
411                                       SanCovGuardsSectionName);
412   if (Function8bitCounterArray)
413     Ctor = CreateInitCallsForSections(M, SanCov8bitCountersInitName, Int8PtrTy,
414                                       SanCovCountersSectionName);
415   if (Ctor && Options.PCTable) {
416     auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
417     Function *InitFunction = declareSanitizerInitFunction(
418         M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
419     IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
420     IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
421   }
422   // We don't reference these arrays directly in any of our runtime functions,
423   // so we need to prevent them from being dead stripped.
424   if (TargetTriple.isOSBinFormatMachO())
425     appendToUsed(M, GlobalsToAppendToUsed);
426   appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed);
427   return true;
428 }
429 
430 // True if block has successors and it dominates all of them.
isFullDominator(const BasicBlock * BB,const DominatorTree * DT)431 static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) {
432   if (succ_begin(BB) == succ_end(BB))
433     return false;
434 
435   for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) {
436     if (!DT->dominates(BB, SUCC))
437       return false;
438   }
439 
440   return true;
441 }
442 
443 // True if block has predecessors and it postdominates all of them.
isFullPostDominator(const BasicBlock * BB,const PostDominatorTree * PDT)444 static bool isFullPostDominator(const BasicBlock *BB,
445                                 const PostDominatorTree *PDT) {
446   if (pred_begin(BB) == pred_end(BB))
447     return false;
448 
449   for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) {
450     if (!PDT->dominates(BB, PRED))
451       return false;
452   }
453 
454   return true;
455 }
456 
shouldInstrumentBlock(const Function & F,const BasicBlock * BB,const DominatorTree * DT,const PostDominatorTree * PDT,const SanitizerCoverageOptions & Options)457 static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
458                                   const DominatorTree *DT,
459                                   const PostDominatorTree *PDT,
460                                   const SanitizerCoverageOptions &Options) {
461   // Don't insert coverage for unreachable blocks: we will never call
462   // __sanitizer_cov() for them, so counting them in
463   // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
464   // percentage. Also, unreachable instructions frequently have no debug
465   // locations.
466   if (isa<UnreachableInst>(BB->getTerminator()))
467     return false;
468 
469   // Don't insert coverage into blocks without a valid insertion point
470   // (catchswitch blocks).
471   if (BB->getFirstInsertionPt() == BB->end())
472     return false;
473 
474   if (Options.NoPrune || &F.getEntryBlock() == BB)
475     return true;
476 
477   if (Options.CoverageType == SanitizerCoverageOptions::SCK_Function &&
478       &F.getEntryBlock() != BB)
479     return false;
480 
481   // Do not instrument full dominators, or full post-dominators with multiple
482   // predecessors.
483   return !isFullDominator(BB, DT)
484     && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
485 }
486 
runOnFunction(Function & F)487 bool SanitizerCoverageModule::runOnFunction(Function &F) {
488   if (F.empty())
489     return false;
490   if (F.getName().find(".module_ctor") != std::string::npos)
491     return false; // Should not instrument sanitizer init functions.
492   if (F.getName().startswith("__sanitizer_"))
493     return false;  // Don't instrument __sanitizer_* callbacks.
494   // Don't touch available_externally functions, their actual body is elewhere.
495   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
496     return false;
497   // Don't instrument MSVC CRT configuration helpers. They may run before normal
498   // initialization.
499   if (F.getName() == "__local_stdio_printf_options" ||
500       F.getName() == "__local_stdio_scanf_options")
501     return false;
502   if (isa<UnreachableInst>(F.getEntryBlock().getTerminator()))
503     return false;
504   // Don't instrument functions using SEH for now. Splitting basic blocks like
505   // we do for coverage breaks WinEHPrepare.
506   // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
507   if (F.hasPersonalityFn() &&
508       isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
509     return false;
510   if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
511     SplitAllCriticalEdges(F);
512   SmallVector<Instruction *, 8> IndirCalls;
513   SmallVector<BasicBlock *, 16> BlocksToInstrument;
514   SmallVector<Instruction *, 8> CmpTraceTargets;
515   SmallVector<Instruction *, 8> SwitchTraceTargets;
516   SmallVector<BinaryOperator *, 8> DivTraceTargets;
517   SmallVector<GetElementPtrInst *, 8> GepTraceTargets;
518 
519   const DominatorTree *DT =
520       &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
521   const PostDominatorTree *PDT =
522       &getAnalysis<PostDominatorTreeWrapperPass>(F).getPostDomTree();
523   bool IsLeafFunc = true;
524 
525   for (auto &BB : F) {
526     if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
527       BlocksToInstrument.push_back(&BB);
528     for (auto &Inst : BB) {
529       if (Options.IndirectCalls) {
530         CallSite CS(&Inst);
531         if (CS && !CS.getCalledFunction())
532           IndirCalls.push_back(&Inst);
533       }
534       if (Options.TraceCmp) {
535         if (isa<ICmpInst>(&Inst))
536           CmpTraceTargets.push_back(&Inst);
537         if (isa<SwitchInst>(&Inst))
538           SwitchTraceTargets.push_back(&Inst);
539       }
540       if (Options.TraceDiv)
541         if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst))
542           if (BO->getOpcode() == Instruction::SDiv ||
543               BO->getOpcode() == Instruction::UDiv)
544             DivTraceTargets.push_back(BO);
545       if (Options.TraceGep)
546         if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst))
547           GepTraceTargets.push_back(GEP);
548       if (Options.StackDepth)
549         if (isa<InvokeInst>(Inst) ||
550             (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst)))
551           IsLeafFunc = false;
552     }
553   }
554 
555   InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
556   InjectCoverageForIndirectCalls(F, IndirCalls);
557   InjectTraceForCmp(F, CmpTraceTargets);
558   InjectTraceForSwitch(F, SwitchTraceTargets);
559   InjectTraceForDiv(F, DivTraceTargets);
560   InjectTraceForGep(F, GepTraceTargets);
561   return true;
562 }
563 
CreateFunctionLocalArrayInSection(size_t NumElements,Function & F,Type * Ty,const char * Section)564 GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
565     size_t NumElements, Function &F, Type *Ty, const char *Section) {
566   ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
567   auto Array = new GlobalVariable(
568       *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
569       Constant::getNullValue(ArrayTy), "__sancov_gen_");
570 
571   if (TargetTriple.supportsCOMDAT() && !F.isInterposable())
572     if (auto Comdat =
573             GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId))
574       Array->setComdat(Comdat);
575   Array->setSection(getSectionName(Section));
576   Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
577                                         : Ty->getPrimitiveSizeInBits() / 8);
578   GlobalsToAppendToUsed.push_back(Array);
579   GlobalsToAppendToCompilerUsed.push_back(Array);
580   MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
581   Array->addMetadata(LLVMContext::MD_associated, *MD);
582 
583   return Array;
584 }
585 
586 GlobalVariable *
CreatePCArray(Function & F,ArrayRef<BasicBlock * > AllBlocks)587 SanitizerCoverageModule::CreatePCArray(Function &F,
588                                        ArrayRef<BasicBlock *> AllBlocks) {
589   size_t N = AllBlocks.size();
590   assert(N);
591   SmallVector<Constant *, 32> PCs;
592   IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
593   for (size_t i = 0; i < N; i++) {
594     if (&F.getEntryBlock() == AllBlocks[i]) {
595       PCs.push_back((Constant *)IRB.CreatePointerCast(&F, IntptrPtrTy));
596       PCs.push_back((Constant *)IRB.CreateIntToPtr(
597           ConstantInt::get(IntptrTy, 1), IntptrPtrTy));
598     } else {
599       PCs.push_back((Constant *)IRB.CreatePointerCast(
600           BlockAddress::get(AllBlocks[i]), IntptrPtrTy));
601       PCs.push_back((Constant *)IRB.CreateIntToPtr(
602           ConstantInt::get(IntptrTy, 0), IntptrPtrTy));
603     }
604   }
605   auto *PCArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy,
606                                                     SanCovPCsSectionName);
607   PCArray->setInitializer(
608       ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs));
609   PCArray->setConstant(true);
610 
611   return PCArray;
612 }
613 
CreateFunctionLocalArrays(Function & F,ArrayRef<BasicBlock * > AllBlocks)614 void SanitizerCoverageModule::CreateFunctionLocalArrays(
615     Function &F, ArrayRef<BasicBlock *> AllBlocks) {
616   if (Options.TracePCGuard)
617     FunctionGuardArray = CreateFunctionLocalArrayInSection(
618         AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
619 
620   if (Options.Inline8bitCounters)
621     Function8bitCounterArray = CreateFunctionLocalArrayInSection(
622         AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
623 
624   if (Options.PCTable)
625     FunctionPCsArray = CreatePCArray(F, AllBlocks);
626 }
627 
InjectCoverage(Function & F,ArrayRef<BasicBlock * > AllBlocks,bool IsLeafFunc)628 bool SanitizerCoverageModule::InjectCoverage(Function &F,
629                                              ArrayRef<BasicBlock *> AllBlocks,
630                                              bool IsLeafFunc) {
631   if (AllBlocks.empty()) return false;
632   CreateFunctionLocalArrays(F, AllBlocks);
633   for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
634     InjectCoverageAtBlock(F, *AllBlocks[i], i, IsLeafFunc);
635   return true;
636 }
637 
638 // On every indirect call we call a run-time function
639 // __sanitizer_cov_indir_call* with two parameters:
640 //   - callee address,
641 //   - global cache array that contains CacheSize pointers (zero-initialized).
642 //     The cache is used to speed up recording the caller-callee pairs.
643 // The address of the caller is passed implicitly via caller PC.
644 // CacheSize is encoded in the name of the run-time function.
InjectCoverageForIndirectCalls(Function & F,ArrayRef<Instruction * > IndirCalls)645 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
646     Function &F, ArrayRef<Instruction *> IndirCalls) {
647   if (IndirCalls.empty())
648     return;
649   assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters);
650   for (auto I : IndirCalls) {
651     IRBuilder<> IRB(I);
652     CallSite CS(I);
653     Value *Callee = CS.getCalledValue();
654     if (isa<InlineAsm>(Callee))
655       continue;
656     IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
657   }
658 }
659 
660 // For every switch statement we insert a call:
661 // __sanitizer_cov_trace_switch(CondValue,
662 //      {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
663 
InjectTraceForSwitch(Function &,ArrayRef<Instruction * > SwitchTraceTargets)664 void SanitizerCoverageModule::InjectTraceForSwitch(
665     Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
666   for (auto I : SwitchTraceTargets) {
667     if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
668       IRBuilder<> IRB(I);
669       SmallVector<Constant *, 16> Initializers;
670       Value *Cond = SI->getCondition();
671       if (Cond->getType()->getScalarSizeInBits() >
672           Int64Ty->getScalarSizeInBits())
673         continue;
674       Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
675       Initializers.push_back(
676           ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
677       if (Cond->getType()->getScalarSizeInBits() <
678           Int64Ty->getScalarSizeInBits())
679         Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
680       for (auto It : SI->cases()) {
681         Constant *C = It.getCaseValue();
682         if (C->getType()->getScalarSizeInBits() <
683             Int64Ty->getScalarSizeInBits())
684           C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
685         Initializers.push_back(C);
686       }
687       llvm::sort(Initializers.begin() + 2, Initializers.end(),
688                  [](const Constant *A, const Constant *B) {
689                    return cast<ConstantInt>(A)->getLimitedValue() <
690                           cast<ConstantInt>(B)->getLimitedValue();
691                  });
692       ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
693       GlobalVariable *GV = new GlobalVariable(
694           *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
695           ConstantArray::get(ArrayOfInt64Ty, Initializers),
696           "__sancov_gen_cov_switch_values");
697       IRB.CreateCall(SanCovTraceSwitchFunction,
698                      {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)});
699     }
700   }
701 }
702 
InjectTraceForDiv(Function &,ArrayRef<BinaryOperator * > DivTraceTargets)703 void SanitizerCoverageModule::InjectTraceForDiv(
704     Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
705   for (auto BO : DivTraceTargets) {
706     IRBuilder<> IRB(BO);
707     Value *A1 = BO->getOperand(1);
708     if (isa<ConstantInt>(A1)) continue;
709     if (!A1->getType()->isIntegerTy())
710       continue;
711     uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
712     int CallbackIdx = TypeSize == 32 ? 0 :
713         TypeSize == 64 ? 1 : -1;
714     if (CallbackIdx < 0) continue;
715     auto Ty = Type::getIntNTy(*C, TypeSize);
716     IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
717                    {IRB.CreateIntCast(A1, Ty, true)});
718   }
719 }
720 
InjectTraceForGep(Function &,ArrayRef<GetElementPtrInst * > GepTraceTargets)721 void SanitizerCoverageModule::InjectTraceForGep(
722     Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
723   for (auto GEP : GepTraceTargets) {
724     IRBuilder<> IRB(GEP);
725     for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I)
726       if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy())
727         IRB.CreateCall(SanCovTraceGepFunction,
728                        {IRB.CreateIntCast(*I, IntptrTy, true)});
729   }
730 }
731 
InjectTraceForCmp(Function &,ArrayRef<Instruction * > CmpTraceTargets)732 void SanitizerCoverageModule::InjectTraceForCmp(
733     Function &, ArrayRef<Instruction *> CmpTraceTargets) {
734   for (auto I : CmpTraceTargets) {
735     if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
736       IRBuilder<> IRB(ICMP);
737       Value *A0 = ICMP->getOperand(0);
738       Value *A1 = ICMP->getOperand(1);
739       if (!A0->getType()->isIntegerTy())
740         continue;
741       uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
742       int CallbackIdx = TypeSize == 8 ? 0 :
743                         TypeSize == 16 ? 1 :
744                         TypeSize == 32 ? 2 :
745                         TypeSize == 64 ? 3 : -1;
746       if (CallbackIdx < 0) continue;
747       // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
748       auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
749       bool FirstIsConst = isa<ConstantInt>(A0);
750       bool SecondIsConst = isa<ConstantInt>(A1);
751       // If both are const, then we don't need such a comparison.
752       if (FirstIsConst && SecondIsConst) continue;
753       // If only one is const, then make it the first callback argument.
754       if (FirstIsConst || SecondIsConst) {
755         CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
756         if (SecondIsConst)
757           std::swap(A0, A1);
758       }
759 
760       auto Ty = Type::getIntNTy(*C, TypeSize);
761       IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true),
762               IRB.CreateIntCast(A1, Ty, true)});
763     }
764   }
765 }
766 
InjectCoverageAtBlock(Function & F,BasicBlock & BB,size_t Idx,bool IsLeafFunc)767 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
768                                                     size_t Idx,
769                                                     bool IsLeafFunc) {
770   BasicBlock::iterator IP = BB.getFirstInsertionPt();
771   bool IsEntryBB = &BB == &F.getEntryBlock();
772   DebugLoc EntryLoc;
773   if (IsEntryBB) {
774     if (auto SP = F.getSubprogram())
775       EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
776     // Keep static allocas and llvm.localescape calls in the entry block.  Even
777     // if we aren't splitting the block, it's nice for allocas to be before
778     // calls.
779     IP = PrepareToSplitEntryBlock(BB, IP);
780   } else {
781     EntryLoc = IP->getDebugLoc();
782   }
783 
784   IRBuilder<> IRB(&*IP);
785   IRB.SetCurrentDebugLocation(EntryLoc);
786   if (Options.TracePC) {
787     IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
788     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
789   }
790   if (Options.TracePCGuard) {
791     auto GuardPtr = IRB.CreateIntToPtr(
792         IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
793                       ConstantInt::get(IntptrTy, Idx * 4)),
794         Int32PtrTy);
795     IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
796     IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
797   }
798   if (Options.Inline8bitCounters) {
799     auto CounterPtr = IRB.CreateGEP(
800         Function8bitCounterArray,
801         {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
802     auto Load = IRB.CreateLoad(CounterPtr);
803     auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
804     auto Store = IRB.CreateStore(Inc, CounterPtr);
805     SetNoSanitizeMetadata(Load);
806     SetNoSanitizeMetadata(Store);
807   }
808   if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
809     // Check stack depth.  If it's the deepest so far, record it.
810     Function *GetFrameAddr =
811         Intrinsic::getDeclaration(F.getParent(), Intrinsic::frameaddress);
812     auto FrameAddrPtr =
813         IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
814     auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
815     auto LowestStack = IRB.CreateLoad(SanCovLowestStack);
816     auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
817     auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
818     IRBuilder<> ThenIRB(ThenTerm);
819     auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
820     SetNoSanitizeMetadata(LowestStack);
821     SetNoSanitizeMetadata(Store);
822   }
823 }
824 
825 std::string
getSectionName(const std::string & Section) const826 SanitizerCoverageModule::getSectionName(const std::string &Section) const {
827   if (TargetTriple.isOSBinFormatCOFF()) {
828     if (Section == SanCovCountersSectionName)
829       return ".SCOV$CM";
830     if (Section == SanCovPCsSectionName)
831       return ".SCOVP$M";
832     return ".SCOV$GM"; // For SanCovGuardsSectionName.
833   }
834   if (TargetTriple.isOSBinFormatMachO())
835     return "__DATA,__" + Section;
836   return "__" + Section;
837 }
838 
839 std::string
getSectionStart(const std::string & Section) const840 SanitizerCoverageModule::getSectionStart(const std::string &Section) const {
841   if (TargetTriple.isOSBinFormatMachO())
842     return "\1section$start$__DATA$__" + Section;
843   return "__start___" + Section;
844 }
845 
846 std::string
getSectionEnd(const std::string & Section) const847 SanitizerCoverageModule::getSectionEnd(const std::string &Section) const {
848   if (TargetTriple.isOSBinFormatMachO())
849     return "\1section$end$__DATA$__" + Section;
850   return "__stop___" + Section;
851 }
852 
853 
854 char SanitizerCoverageModule::ID = 0;
855 INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov",
856                       "SanitizerCoverage: TODO."
857                       "ModulePass",
858                       false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)859 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
860 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
861 INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov",
862                     "SanitizerCoverage: TODO."
863                     "ModulePass",
864                     false, false)
865 ModulePass *llvm::createSanitizerCoverageModulePass(
866     const SanitizerCoverageOptions &Options) {
867   return new SanitizerCoverageModule(Options);
868 }
869