blob: f09081314eb155713e81f4e0e36677b388cdae3a [file] [log] [blame]
Kostya Serebryany29a18dc2014-11-11 22:14:37 +00001//===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Kostya Serebryany29a18dc2014-11-11 22:14:37 +00006//
7//===----------------------------------------------------------------------===//
8//
Kostya Serebryany53b34c82017-05-31 18:27:33 +00009// Coverage instrumentation done on LLVM IR level, works with Sanitizers.
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000010//
11//===----------------------------------------------------------------------===//
12
Leonard Chan007f6742019-07-25 20:53:15 +000013#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/SmallVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000016#include "llvm/Analysis/EHPersonalities.h"
George Karpenkov018472c2017-05-24 00:29:12 +000017#include "llvm/Analysis/PostDominators.h"
Mike Aizatsky5971f182016-02-26 01:17:22 +000018#include "llvm/IR/CFG.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000019#include "llvm/IR/CallSite.h"
Matt Morehouse5c7fc762017-08-18 18:43:30 +000020#include "llvm/IR/Constant.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000021#include "llvm/IR/DataLayout.h"
Alexey Samsonov201733b2015-06-12 01:48:47 +000022#include "llvm/IR/DebugInfo.h"
Mike Aizatsky5971f182016-02-26 01:17:22 +000023#include "llvm/IR/Dominators.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000024#include "llvm/IR/Function.h"
Matt Morehouse5c7fc762017-08-18 18:43:30 +000025#include "llvm/IR/GlobalVariable.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000026#include "llvm/IR/IRBuilder.h"
Kostya Serebryany73762942014-12-16 21:24:15 +000027#include "llvm/IR/InlineAsm.h"
Matt Morehouse034126e2017-08-30 22:49:31 +000028#include "llvm/IR/IntrinsicInst.h"
Matt Morehouse5c7fc762017-08-18 18:43:30 +000029#include "llvm/IR/Intrinsics.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/MDBuilder.h"
Jonathan Metzman0b94e882018-10-12 18:11:47 +000032#include "llvm/IR/Mangler.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000033#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"
Mike Aizatsky5971f182016-02-26 01:17:22 +000038#include "llvm/Transforms/Instrumentation.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000039#include "llvm/Transforms/Utils/BasicBlockUtils.h"
40#include "llvm/Transforms/Utils/ModuleUtils.h"
41
42using namespace llvm;
43
44#define DEBUG_TYPE "sancov"
45
Mike Aizatsky759aca02016-03-18 23:29:29 +000046static const char *const SanCovTracePCIndirName =
47 "__sanitizer_cov_trace_pc_indir";
Mike Aizatsky759aca02016-03-18 23:29:29 +000048static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc";
Kostya Serebryany524c3f32016-08-18 01:25:28 +000049static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1";
50static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2";
51static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4";
52static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8";
Justin Bognerbe757de2017-08-28 23:38:12 +000053static const char *const SanCovTraceConstCmp1 =
Alexander Potapenko52410812017-08-10 15:00:13 +000054 "__sanitizer_cov_trace_const_cmp1";
Justin Bognerbe757de2017-08-28 23:38:12 +000055static const char *const SanCovTraceConstCmp2 =
Alexander Potapenko52410812017-08-10 15:00:13 +000056 "__sanitizer_cov_trace_const_cmp2";
Justin Bognerbe757de2017-08-28 23:38:12 +000057static const char *const SanCovTraceConstCmp4 =
Alexander Potapenko52410812017-08-10 15:00:13 +000058 "__sanitizer_cov_trace_const_cmp4";
Justin Bognerbe757de2017-08-28 23:38:12 +000059static const char *const SanCovTraceConstCmp8 =
Alexander Potapenko52410812017-08-10 15:00:13 +000060 "__sanitizer_cov_trace_const_cmp8";
Kostya Serebryany5ac427b2016-08-30 01:12:10 +000061static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4";
62static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8";
63static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep";
Mike Aizatsky759aca02016-03-18 23:29:29 +000064static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
Fangrui Songa400ca32019-05-07 01:39:37 +000065static const char *const SanCovModuleCtorTracePcGuardName =
66 "sancov.module_ctor_trace_pc_guard";
67static const char *const SanCovModuleCtor8bitCountersName =
68 "sancov.module_ctor_8bit_counters";
Mike Aizatsky759aca02016-03-18 23:29:29 +000069static const uint64_t SanCtorAndDtorPriority = 2;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000070
Kostya Serebryanyda718e52016-09-14 01:39:35 +000071static const char *const SanCovTracePCGuardName =
72 "__sanitizer_cov_trace_pc_guard";
73static const char *const SanCovTracePCGuardInitName =
74 "__sanitizer_cov_trace_pc_guard_init";
Kostya Serebryanyb75d0022017-07-27 23:36:49 +000075static const char *const SanCov8bitCountersInitName =
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000076 "__sanitizer_cov_8bit_counters_init";
Kostya Serebryanyb75d0022017-07-27 23:36:49 +000077static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init";
Kostya Serebryanyda718e52016-09-14 01:39:35 +000078
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +000079static const char *const SanCovGuardsSectionName = "sancov_guards";
George Karpenkov406c1132017-06-14 23:40:25 +000080static const char *const SanCovCountersSectionName = "sancov_cntrs";
Kostya Serebryanyb75d0022017-07-27 23:36:49 +000081static const char *const SanCovPCsSectionName = "sancov_pcs";
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +000082
Matt Morehouse5c7fc762017-08-18 18:43:30 +000083static const char *const SanCovLowestStackName = "__sancov_lowest_stack";
Matt Morehouse5c7fc762017-08-18 18:43:30 +000084
Mike Aizatsky759aca02016-03-18 23:29:29 +000085static cl::opt<int> ClCoverageLevel(
86 "sanitizer-coverage-level",
87 cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
Kostya Serebryanyc5d3d492017-04-19 22:42:11 +000088 "3: all blocks and critical edges"),
Mike Aizatsky759aca02016-03-18 23:29:29 +000089 cl::Hidden, cl::init(0));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000090
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000091static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc",
92 cl::desc("Experimental pc tracing"), cl::Hidden,
93 cl::init(false));
Kostya Serebryanyd4590c72016-02-17 21:34:43 +000094
Kostya Serebryanyda718e52016-09-14 01:39:35 +000095static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard",
96 cl::desc("pc tracing with a guard"),
97 cl::Hidden, cl::init(false));
98
Kostya Serebryanyb75d0022017-07-27 23:36:49 +000099// If true, we create a global variable that contains PCs of all instrumented
100// BBs, put this global into a named section, and pass this section's bounds
101// to __sanitizer_cov_pcs_init.
102// This way the coverage instrumentation does not need to acquire the PCs
103// at run-time. Works with trace-pc-guard and inline-8bit-counters.
Kostya Serebryany063b6522017-07-28 00:09:29 +0000104static cl::opt<bool> ClCreatePCTable("sanitizer-coverage-pc-table",
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000105 cl::desc("create a static PC table"),
106 cl::Hidden, cl::init(false));
107
108static cl::opt<bool>
109 ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
110 cl::desc("increments 8-bit counter for every edge"),
111 cl::Hidden, cl::init(false));
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000112
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000113static cl::opt<bool>
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000114 ClCMPTracing("sanitizer-coverage-trace-compares",
115 cl::desc("Tracing of CMP and similar instructions"),
116 cl::Hidden, cl::init(false));
117
118static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs",
119 cl::desc("Tracing of DIV instructions"),
120 cl::Hidden, cl::init(false));
121
122static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps",
123 cl::desc("Tracing of GEP instructions"),
124 cl::Hidden, cl::init(false));
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000125
Mike Aizatsky70ea4532016-04-06 23:24:37 +0000126static cl::opt<bool>
127 ClPruneBlocks("sanitizer-coverage-prune-blocks",
128 cl::desc("Reduce the number of instrumented blocks"),
129 cl::Hidden, cl::init(true));
Mike Aizatsky5971f182016-02-26 01:17:22 +0000130
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000131static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth",
132 cl::desc("max stack depth tracing"),
133 cl::Hidden, cl::init(false));
134
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000135namespace {
136
Alexey Samsonov3514f272015-05-07 01:00:31 +0000137SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
138 SanitizerCoverageOptions Res;
139 switch (LegacyCoverageLevel) {
140 case 0:
141 Res.CoverageType = SanitizerCoverageOptions::SCK_None;
142 break;
143 case 1:
144 Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
145 break;
146 case 2:
147 Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
148 break;
149 case 3:
150 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
151 break;
152 case 4:
153 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
154 Res.IndirectCalls = true;
155 break;
156 }
157 return Res;
158}
159
160SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
161 // Sets CoverageType and IndirectCalls.
162 SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
163 Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
164 Options.IndirectCalls |= CLOpts.IndirectCalls;
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000165 Options.TraceCmp |= ClCMPTracing;
166 Options.TraceDiv |= ClDIVTracing;
167 Options.TraceGep |= ClGEPTracing;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000168 Options.TracePC |= ClTracePC;
Kostya Serebryanyda718e52016-09-14 01:39:35 +0000169 Options.TracePCGuard |= ClTracePCGuard;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000170 Options.Inline8bitCounters |= ClInline8bitCounters;
Kostya Serebryany063b6522017-07-28 00:09:29 +0000171 Options.PCTable |= ClCreatePCTable;
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000172 Options.NoPrune |= !ClPruneBlocks;
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000173 Options.StackDepth |= ClStackDepth;
174 if (!Options.TracePCGuard && !Options.TracePC &&
175 !Options.Inline8bitCounters && !Options.StackDepth)
176 Options.TracePCGuard = true; // TracePCGuard is default.
Alexey Samsonov3514f272015-05-07 01:00:31 +0000177 return Options;
178}
179
Leonard Chaneca01b02019-09-04 20:30:29 +0000180using DomTreeCallback = function_ref<const DominatorTree *(Function &F)>;
181using PostDomTreeCallback =
182 function_ref<const PostDominatorTree *(Function &F)>;
Leonard Chan5652f352019-07-11 22:35:40 +0000183
Leonard Chan007f6742019-07-25 20:53:15 +0000184class ModuleSanitizerCoverage {
185public:
Leonard Chaneca01b02019-09-04 20:30:29 +0000186 ModuleSanitizerCoverage(
187 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
Leonard Chan007f6742019-07-25 20:53:15 +0000188 : Options(OverrideFromCL(Options)) {}
Leonard Chaneca01b02019-09-04 20:30:29 +0000189 bool instrumentModule(Module &M, DomTreeCallback DTCallback,
190 PostDomTreeCallback PDTCallback);
Chandler Carruth30061152016-03-18 22:43:42 +0000191
192private:
Leonard Chaneca01b02019-09-04 20:30:29 +0000193 void instrumentFunction(Function &F, DomTreeCallback DTCallback,
194 PostDomTreeCallback PDTCallback);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000195 void InjectCoverageForIndirectCalls(Function &F,
196 ArrayRef<Instruction *> IndirCalls);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000197 void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000198 void InjectTraceForDiv(Function &F,
199 ArrayRef<BinaryOperator *> DivTraceTargets);
200 void InjectTraceForGep(Function &F,
201 ArrayRef<GetElementPtrInst *> GepTraceTargets);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000202 void InjectTraceForSwitch(Function &F,
203 ArrayRef<Instruction *> SwitchTraceTargets);
Matt Morehouse034126e2017-08-30 22:49:31 +0000204 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
205 bool IsLeafFunc = true);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000206 GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
207 Function &F, Type *Ty,
208 const char *Section);
Justin Bogner873a0742017-08-28 23:46:11 +0000209 GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000210 void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
Matt Morehouse034126e2017-08-30 22:49:31 +0000211 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
212 bool IsLeafFunc = true);
Leonard Chaneca01b02019-09-04 20:30:29 +0000213 Function *CreateInitCallsForSections(Module &M, const char *CtorName,
214 const char *InitFunctionName, Type *Ty,
215 const char *Section);
216 std::pair<Value *, Value *> CreateSecStartEnd(Module &M, const char *Section,
217 Type *Ty);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000218
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000219 void SetNoSanitizeMetadata(Instruction *I) {
220 I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
221 MDNode::get(*C, None));
222 }
223
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000224 std::string getSectionName(const std::string &Section) const;
Leonard Chanbb147aa2019-07-15 23:18:31 +0000225 std::string getSectionStart(const std::string &Section) const;
226 std::string getSectionEnd(const std::string &Section) const;
James Y Knight13680222019-02-01 02:28:03 +0000227 FunctionCallee SanCovTracePCIndir;
228 FunctionCallee SanCovTracePC, SanCovTracePCGuard;
229 FunctionCallee SanCovTraceCmpFunction[4];
230 FunctionCallee SanCovTraceConstCmpFunction[4];
231 FunctionCallee SanCovTraceDivFunction[2];
232 FunctionCallee SanCovTraceGepFunction;
233 FunctionCallee SanCovTraceSwitchFunction;
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000234 GlobalVariable *SanCovLowestStack;
Kostya Serebryany73762942014-12-16 21:24:15 +0000235 InlineAsm *EmptyAsm;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000236 Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
Alexander Potapenko52410812017-08-10 15:00:13 +0000237 *Int16Ty, *Int8Ty, *Int8PtrTy;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000238 Module *CurModule;
Matt Morehouse3bea25e2018-09-13 21:45:55 +0000239 std::string CurModuleUniqueId;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000240 Triple TargetTriple;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000241 LLVMContext *C;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000242 const DataLayout *DL;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000243
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000244 GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000245 GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
Kostya Serebryany063b6522017-07-28 00:09:29 +0000246 GlobalVariable *FunctionPCsArray; // for pc-table.
Kostya Serebryany4192b962017-09-09 05:30:13 +0000247 SmallVector<GlobalValue *, 20> GlobalsToAppendToUsed;
Matt Morehouse0ea9a902018-06-15 20:12:58 +0000248 SmallVector<GlobalValue *, 20> GlobalsToAppendToCompilerUsed;
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000249
Alexey Samsonov3514f272015-05-07 01:00:31 +0000250 SanitizerCoverageOptions Options;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000251};
252
Leonard Chaneca01b02019-09-04 20:30:29 +0000253class ModuleSanitizerCoverageLegacyPass : public ModulePass {
Leonard Chan007f6742019-07-25 20:53:15 +0000254public:
Leonard Chaneca01b02019-09-04 20:30:29 +0000255 ModuleSanitizerCoverageLegacyPass(
256 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
257 : ModulePass(ID), Options(Options) {
258 initializeModuleSanitizerCoverageLegacyPassPass(
259 *PassRegistry::getPassRegistry());
260 }
261 bool runOnModule(Module &M) override {
262 ModuleSanitizerCoverage ModuleSancov(Options);
263 auto DTCallback = [this](Function &F) -> const DominatorTree * {
264 return &this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
265 };
266 auto PDTCallback = [this](Function &F) -> const PostDominatorTree * {
267 return &this->getAnalysis<PostDominatorTreeWrapperPass>(F)
268 .getPostDomTree();
269 };
270 return ModuleSancov.instrumentModule(M, DTCallback, PDTCallback);
271 }
272
Leonard Chan007f6742019-07-25 20:53:15 +0000273 static char ID; // Pass identification, replacement for typeid
Leonard Chaneca01b02019-09-04 20:30:29 +0000274 StringRef getPassName() const override { return "ModuleSanitizerCoverage"; }
Leonard Chan007f6742019-07-25 20:53:15 +0000275
276 void getAnalysisUsage(AnalysisUsage &AU) const override {
Leonard Chan007f6742019-07-25 20:53:15 +0000277 AU.addRequired<DominatorTreeWrapperPass>();
278 AU.addRequired<PostDominatorTreeWrapperPass>();
279 }
280
281private:
282 SanitizerCoverageOptions Options;
283};
284
Mike Aizatsky759aca02016-03-18 23:29:29 +0000285} // namespace
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000286
Leonard Chan007f6742019-07-25 20:53:15 +0000287PreservedAnalyses ModuleSanitizerCoveragePass::run(Module &M,
Leonard Chaneca01b02019-09-04 20:30:29 +0000288 ModuleAnalysisManager &MAM) {
Leonard Chan007f6742019-07-25 20:53:15 +0000289 ModuleSanitizerCoverage ModuleSancov(Options);
Leonard Chaneca01b02019-09-04 20:30:29 +0000290 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
291 auto DTCallback = [&FAM](Function &F) -> const DominatorTree * {
292 return &FAM.getResult<DominatorTreeAnalysis>(F);
293 };
294 auto PDTCallback = [&FAM](Function &F) -> const PostDominatorTree * {
295 return &FAM.getResult<PostDominatorTreeAnalysis>(F);
296 };
297 if (ModuleSancov.instrumentModule(M, DTCallback, PDTCallback))
Leonard Chan007f6742019-07-25 20:53:15 +0000298 return PreservedAnalyses::none();
299 return PreservedAnalyses::all();
300}
301
Jonathan Metzman5eb8cba2018-10-16 23:43:57 +0000302std::pair<Value *, Value *>
Leonard Chan007f6742019-07-25 20:53:15 +0000303ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000304 Type *Ty) {
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000305 GlobalVariable *SecStart =
306 new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
307 getSectionStart(Section));
308 SecStart->setVisibility(GlobalValue::HiddenVisibility);
309 GlobalVariable *SecEnd =
310 new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
311 nullptr, getSectionEnd(Section));
312 SecEnd->setVisibility(GlobalValue::HiddenVisibility);
Jonathan Metzman5eb8cba2018-10-16 23:43:57 +0000313 IRBuilder<> IRB(M.getContext());
314 Value *SecEndPtr = IRB.CreatePointerCast(SecEnd, Ty);
Jonathan Metzmane159a0d2019-01-14 21:02:02 +0000315 if (!TargetTriple.isOSBinFormatCOFF())
Jonathan Metzman5eb8cba2018-10-16 23:43:57 +0000316 return std::make_pair(IRB.CreatePointerCast(SecStart, Ty), SecEndPtr);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000317
Jonathan Metzman5eb8cba2018-10-16 23:43:57 +0000318 // Account for the fact that on windows-msvc __start_* symbols actually
319 // point to a uint64_t before the start of the array.
320 auto SecStartI8Ptr = IRB.CreatePointerCast(SecStart, Int8PtrTy);
James Y Knight77160752019-02-01 20:44:47 +0000321 auto GEP = IRB.CreateGEP(Int8Ty, SecStartI8Ptr,
Jonathan Metzman5eb8cba2018-10-16 23:43:57 +0000322 ConstantInt::get(IntptrTy, sizeof(uint64_t)));
323 return std::make_pair(IRB.CreatePointerCast(GEP, Ty), SecEndPtr);
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000324}
325
Leonard Chan007f6742019-07-25 20:53:15 +0000326Function *ModuleSanitizerCoverage::CreateInitCallsForSections(
Fangrui Songa400ca32019-05-07 01:39:37 +0000327 Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty,
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000328 const char *Section) {
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000329 auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
330 auto SecStart = SecStartEnd.first;
331 auto SecEnd = SecStartEnd.second;
332 Function *CtorFunc;
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000333 std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
Fangrui Songa400ca32019-05-07 01:39:37 +0000334 M, CtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd});
335 assert(CtorFunc->getName() == CtorName);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000336
337 if (TargetTriple.supportsCOMDAT()) {
338 // Use comdat to dedup CtorFunc.
Fangrui Songa400ca32019-05-07 01:39:37 +0000339 CtorFunc->setComdat(M.getOrInsertComdat(CtorName));
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000340 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
341 } else {
342 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
343 }
Jonathan Metzman0b94e882018-10-12 18:11:47 +0000344
Jonathan Metzmane159a0d2019-01-14 21:02:02 +0000345 if (TargetTriple.isOSBinFormatCOFF()) {
Jonathan Metzman0b94e882018-10-12 18:11:47 +0000346 // In COFF files, if the contructors are set as COMDAT (they are because
347 // COFF supports COMDAT) and the linker flag /OPT:REF (strip unreferenced
348 // functions and data) is used, the constructors get stripped. To prevent
Jonathan Metzmane159a0d2019-01-14 21:02:02 +0000349 // this, give the constructors weak ODR linkage and ensure the linker knows
350 // to include the sancov constructor. This way the linker can deduplicate
351 // the constructors but always leave one copy.
Jonathan Metzman0b94e882018-10-12 18:11:47 +0000352 CtorFunc->setLinkage(GlobalValue::WeakODRLinkage);
Jonathan Metzmane159a0d2019-01-14 21:02:02 +0000353 appendToUsed(M, CtorFunc);
Jonathan Metzman0b94e882018-10-12 18:11:47 +0000354 }
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000355 return CtorFunc;
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000356}
357
Leonard Chaneca01b02019-09-04 20:30:29 +0000358bool ModuleSanitizerCoverage::instrumentModule(
359 Module &M, DomTreeCallback DTCallback, PostDomTreeCallback PDTCallback) {
Alexey Samsonov3514f272015-05-07 01:00:31 +0000360 if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
Leonard Chaneca01b02019-09-04 20:30:29 +0000361 return false;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000362 C = &(M.getContext());
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000363 DL = &M.getDataLayout();
Leonard Chaneca01b02019-09-04 20:30:29 +0000364 CurModule = &M;
Matt Morehouse3bea25e2018-09-13 21:45:55 +0000365 CurModuleUniqueId = getUniqueModuleId(CurModule);
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000366 TargetTriple = Triple(M.getTargetTriple());
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000367 FunctionGuardArray = nullptr;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000368 Function8bitCounterArray = nullptr;
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000369 FunctionPCsArray = nullptr;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000370 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000371 IntptrPtrTy = PointerType::getUnqual(IntptrTy);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000372 Type *VoidTy = Type::getVoidTy(*C);
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000373 IRBuilder<> IRB(*C);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000374 Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty());
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000375 Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000376 Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000377 Int64Ty = IRB.getInt64Ty();
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000378 Int32Ty = IRB.getInt32Ty();
Alexander Potapenko52410812017-08-10 15:00:13 +0000379 Int16Ty = IRB.getInt16Ty();
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000380 Int8Ty = IRB.getInt8Ty();
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000381
James Y Knight13680222019-02-01 02:28:03 +0000382 SanCovTracePCIndir =
383 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy);
384 // Make sure smaller parameters are zero-extended to i64 as required by the
385 // x86_64 ABI.
386 AttributeList SanCovTraceCmpZeroExtAL;
387 if (TargetTriple.getArch() == Triple::x86_64) {
388 SanCovTraceCmpZeroExtAL =
389 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 0, Attribute::ZExt);
390 SanCovTraceCmpZeroExtAL =
391 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 1, Attribute::ZExt);
392 }
393
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000394 SanCovTraceCmpFunction[0] =
James Y Knight13680222019-02-01 02:28:03 +0000395 M.getOrInsertFunction(SanCovTraceCmp1, SanCovTraceCmpZeroExtAL, VoidTy,
396 IRB.getInt8Ty(), IRB.getInt8Ty());
397 SanCovTraceCmpFunction[1] =
398 M.getOrInsertFunction(SanCovTraceCmp2, SanCovTraceCmpZeroExtAL, VoidTy,
399 IRB.getInt16Ty(), IRB.getInt16Ty());
400 SanCovTraceCmpFunction[2] =
401 M.getOrInsertFunction(SanCovTraceCmp4, SanCovTraceCmpZeroExtAL, VoidTy,
402 IRB.getInt32Ty(), IRB.getInt32Ty());
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000403 SanCovTraceCmpFunction[3] =
James Y Knight13680222019-02-01 02:28:03 +0000404 M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty);
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000405
James Y Knight13680222019-02-01 02:28:03 +0000406 SanCovTraceConstCmpFunction[0] = M.getOrInsertFunction(
407 SanCovTraceConstCmp1, SanCovTraceCmpZeroExtAL, VoidTy, Int8Ty, Int8Ty);
408 SanCovTraceConstCmpFunction[1] = M.getOrInsertFunction(
409 SanCovTraceConstCmp2, SanCovTraceCmpZeroExtAL, VoidTy, Int16Ty, Int16Ty);
410 SanCovTraceConstCmpFunction[2] = M.getOrInsertFunction(
411 SanCovTraceConstCmp4, SanCovTraceCmpZeroExtAL, VoidTy, Int32Ty, Int32Ty);
Alexander Potapenko52410812017-08-10 15:00:13 +0000412 SanCovTraceConstCmpFunction[3] =
James Y Knight13680222019-02-01 02:28:03 +0000413 M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty);
Alexander Potapenko52410812017-08-10 15:00:13 +0000414
James Y Knight13680222019-02-01 02:28:03 +0000415 {
416 AttributeList AL;
417 if (TargetTriple.getArch() == Triple::x86_64)
418 AL = AL.addParamAttribute(*C, 0, Attribute::ZExt);
419 SanCovTraceDivFunction[0] =
420 M.getOrInsertFunction(SanCovTraceDiv4, AL, VoidTy, IRB.getInt32Ty());
421 }
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000422 SanCovTraceDivFunction[1] =
James Y Knight13680222019-02-01 02:28:03 +0000423 M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000424 SanCovTraceGepFunction =
James Y Knight13680222019-02-01 02:28:03 +0000425 M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000426 SanCovTraceSwitchFunction =
James Y Knight13680222019-02-01 02:28:03 +0000427 M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy);
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000428
429 Constant *SanCovLowestStackConstant =
430 M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
Julian Lettner29ac3a52019-02-04 22:06:30 +0000431 SanCovLowestStack = dyn_cast<GlobalVariable>(SanCovLowestStackConstant);
Leonard Chaneca01b02019-09-04 20:30:29 +0000432 if (!SanCovLowestStack) {
433 C->emitError(StringRef("'") + SanCovLowestStackName +
434 "' should not be declared by the user");
435 return true;
436 }
Matt Morehouseb1fa8252017-08-22 21:28:29 +0000437 SanCovLowestStack->setThreadLocalMode(
438 GlobalValue::ThreadLocalMode::InitialExecTLSModel);
439 if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
440 SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000441
Kostya Serebryany73762942014-12-16 21:24:15 +0000442 // We insert an empty inline asm after cov callbacks to avoid callback merge.
443 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
444 StringRef(""), StringRef(""),
445 /*hasSideEffects=*/true);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000446
James Y Knight13680222019-02-01 02:28:03 +0000447 SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
448 SanCovTracePCGuard =
449 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy);
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000450
Leonard Chaneca01b02019-09-04 20:30:29 +0000451 for (auto &F : M)
452 instrumentFunction(F, DTCallback, PDTCallback);
453
454 Function *Ctor = nullptr;
455
456 if (FunctionGuardArray)
457 Ctor = CreateInitCallsForSections(M, SanCovModuleCtorTracePcGuardName,
458 SanCovTracePCGuardInitName, Int32PtrTy,
459 SanCovGuardsSectionName);
460 if (Function8bitCounterArray)
461 Ctor = CreateInitCallsForSections(M, SanCovModuleCtor8bitCountersName,
462 SanCov8bitCountersInitName, Int8PtrTy,
463 SanCovCountersSectionName);
464 if (Ctor && Options.PCTable) {
465 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy);
466 FunctionCallee InitFunction = declareSanitizerInitFunction(
467 M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});
468 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
469 IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
470 }
Kostya Serebryany4192b962017-09-09 05:30:13 +0000471 // We don't reference these arrays directly in any of our runtime functions,
472 // so we need to prevent them from being dead stripped.
473 if (TargetTriple.isOSBinFormatMachO())
474 appendToUsed(M, GlobalsToAppendToUsed);
Matt Morehouse0ea9a902018-06-15 20:12:58 +0000475 appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed);
Leonard Chaneca01b02019-09-04 20:30:29 +0000476 return true;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000477}
478
Mike Aizatsky9987f432016-03-23 23:15:03 +0000479// True if block has successors and it dominates all of them.
480static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) {
481 if (succ_begin(BB) == succ_end(BB))
482 return false;
483
484 for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) {
485 if (!DT->dominates(BB, SUCC))
486 return false;
487 }
488
489 return true;
490}
491
George Karpenkov018472c2017-05-24 00:29:12 +0000492// True if block has predecessors and it postdominates all of them.
493static bool isFullPostDominator(const BasicBlock *BB,
494 const PostDominatorTree *PDT) {
495 if (pred_begin(BB) == pred_end(BB))
496 return false;
497
498 for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) {
499 if (!PDT->dominates(BB, PRED))
500 return false;
501 }
502
503 return true;
504}
505
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000506static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
507 const DominatorTree *DT,
George Karpenkov018472c2017-05-24 00:29:12 +0000508 const PostDominatorTree *PDT,
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000509 const SanitizerCoverageOptions &Options) {
Reid Kleckner701593f2019-02-28 22:54:30 +0000510 // Don't insert coverage for blocks containing nothing but unreachable: we
511 // will never call __sanitizer_cov() for them, so counting them in
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000512 // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
513 // percentage. Also, unreachable instructions frequently have no debug
514 // locations.
Reid Kleckner701593f2019-02-28 22:54:30 +0000515 if (isa<UnreachableInst>(BB->getFirstNonPHIOrDbgOrLifetime()))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000516 return false;
517
Reid Kleckner392f0622017-03-23 23:30:41 +0000518 // Don't insert coverage into blocks without a valid insertion point
519 // (catchswitch blocks).
520 if (BB->getFirstInsertionPt() == BB->end())
521 return false;
522
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000523 if (Options.NoPrune || &F.getEntryBlock() == BB)
Mike Aizatsky5971f182016-02-26 01:17:22 +0000524 return true;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000525
Kostya Serebryanyc485ca02017-07-25 02:07:38 +0000526 if (Options.CoverageType == SanitizerCoverageOptions::SCK_Function &&
527 &F.getEntryBlock() != BB)
528 return false;
529
George Karpenkova1c53272017-05-25 01:41:46 +0000530 // Do not instrument full dominators, or full post-dominators with multiple
531 // predecessors.
532 return !isFullDominator(BB, DT)
533 && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
Mike Aizatsky5971f182016-02-26 01:17:22 +0000534}
535
Kostya Serebryanya78a44d2019-01-31 23:43:00 +0000536
537// Returns true iff From->To is a backedge.
538// A twist here is that we treat From->To as a backedge if
539// * To dominates From or
540// * To->UniqueSuccessor dominates From
541static bool IsBackEdge(BasicBlock *From, BasicBlock *To,
542 const DominatorTree *DT) {
543 if (DT->dominates(To, From))
544 return true;
545 if (auto Next = To->getUniqueSuccessor())
546 if (DT->dominates(Next, From))
547 return true;
548 return false;
549}
550
551// Prunes uninteresting Cmp instrumentation:
552// * CMP instructions that feed into loop backedge branch.
553//
554// Note that Cmp pruning is controlled by the same flag as the
555// BB pruning.
556static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree *DT,
557 const SanitizerCoverageOptions &Options) {
558 if (!Options.NoPrune)
559 if (CMP->hasOneUse())
560 if (auto BR = dyn_cast<BranchInst>(CMP->user_back()))
561 for (BasicBlock *B : BR->successors())
562 if (IsBackEdge(BR->getParent(), B, DT))
563 return false;
564 return true;
565}
566
Leonard Chaneca01b02019-09-04 20:30:29 +0000567void ModuleSanitizerCoverage::instrumentFunction(
568 Function &F, DomTreeCallback DTCallback, PostDomTreeCallback PDTCallback) {
569 if (F.empty())
570 return;
571 if (F.getName().find(".module_ctor") != std::string::npos)
572 return; // Should not instrument sanitizer init functions.
573 if (F.getName().startswith("__sanitizer_"))
574 return; // Don't instrument __sanitizer_* callbacks.
575 // Don't touch available_externally functions, their actual body is elewhere.
576 if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
577 return;
578 // Don't instrument MSVC CRT configuration helpers. They may run before normal
579 // initialization.
580 if (F.getName() == "__local_stdio_printf_options" ||
581 F.getName() == "__local_stdio_scanf_options")
582 return;
583 if (isa<UnreachableInst>(F.getEntryBlock().getTerminator()))
584 return;
585 // Don't instrument functions using SEH for now. Splitting basic blocks like
586 // we do for coverage breaks WinEHPrepare.
587 // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
588 if (F.hasPersonalityFn() &&
589 isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
590 return;
Alexey Samsonov3514f272015-05-07 01:00:31 +0000591 if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
Craig Topper03e93f52019-03-12 18:20:25 +0000592 SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions().setIgnoreUnreachableDests());
Mike Aizatsky759aca02016-03-18 23:29:29 +0000593 SmallVector<Instruction *, 8> IndirCalls;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000594 SmallVector<BasicBlock *, 16> BlocksToInstrument;
Mike Aizatsky759aca02016-03-18 23:29:29 +0000595 SmallVector<Instruction *, 8> CmpTraceTargets;
596 SmallVector<Instruction *, 8> SwitchTraceTargets;
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000597 SmallVector<BinaryOperator *, 8> DivTraceTargets;
598 SmallVector<GetElementPtrInst *, 8> GepTraceTargets;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000599
Leonard Chaneca01b02019-09-04 20:30:29 +0000600 const DominatorTree *DT = DTCallback(F);
601 const PostDominatorTree *PDT = PDTCallback(F);
Matt Morehouse034126e2017-08-30 22:49:31 +0000602 bool IsLeafFunc = true;
Mike Aizatsky602f7922016-03-21 23:08:16 +0000603
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000604 for (auto &BB : F) {
George Karpenkov018472c2017-05-24 00:29:12 +0000605 if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
Mike Aizatsky5971f182016-02-26 01:17:22 +0000606 BlocksToInstrument.push_back(&BB);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000607 for (auto &Inst : BB) {
Alexey Samsonov3514f272015-05-07 01:00:31 +0000608 if (Options.IndirectCalls) {
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000609 CallSite CS(&Inst);
610 if (CS && !CS.getCalledFunction())
611 IndirCalls.push_back(&Inst);
612 }
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000613 if (Options.TraceCmp) {
Kostya Serebryanya78a44d2019-01-31 23:43:00 +0000614 if (ICmpInst *CMP = dyn_cast<ICmpInst>(&Inst))
615 if (IsInterestingCmp(CMP, DT, Options))
616 CmpTraceTargets.push_back(&Inst);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000617 if (isa<SwitchInst>(&Inst))
618 SwitchTraceTargets.push_back(&Inst);
619 }
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000620 if (Options.TraceDiv)
621 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst))
622 if (BO->getOpcode() == Instruction::SDiv ||
623 BO->getOpcode() == Instruction::UDiv)
624 DivTraceTargets.push_back(BO);
625 if (Options.TraceGep)
626 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst))
627 GepTraceTargets.push_back(GEP);
Matt Morehouse034126e2017-08-30 22:49:31 +0000628 if (Options.StackDepth)
629 if (isa<InvokeInst>(Inst) ||
630 (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst)))
631 IsLeafFunc = false;
632 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000633 }
Mike Aizatsky5971f182016-02-26 01:17:22 +0000634
Matt Morehouse034126e2017-08-30 22:49:31 +0000635 InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000636 InjectCoverageForIndirectCalls(F, IndirCalls);
637 InjectTraceForCmp(F, CmpTraceTargets);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000638 InjectTraceForSwitch(F, SwitchTraceTargets);
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000639 InjectTraceForDiv(F, DivTraceTargets);
640 InjectTraceForGep(F, GepTraceTargets);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000641}
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000642
Leonard Chaneca01b02019-09-04 20:30:29 +0000643GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000644 size_t NumElements, Function &F, Type *Ty, const char *Section) {
645 ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
646 auto Array = new GlobalVariable(
647 *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
648 Constant::getNullValue(ArrayTy), "__sancov_gen_");
Kostya Serebryanybc504552018-10-12 23:21:48 +0000649
Matt Morehouse19ff35c2019-01-15 21:21:01 +0000650 if (TargetTriple.supportsCOMDAT() && !F.isInterposable())
Reid Klecknerb41b3722018-11-08 00:57:33 +0000651 if (auto Comdat =
652 GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId))
Kostya Serebryanybc504552018-10-12 23:21:48 +0000653 Array->setComdat(Comdat);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000654 Array->setSection(getSectionName(Section));
Kostya Serebryanybb6f0792017-07-31 19:49:45 +0000655 Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
656 : Ty->getPrimitiveSizeInBits() / 8);
Max Moroz4d010ca2018-10-12 13:59:31 +0000657 GlobalsToAppendToUsed.push_back(Array);
Matt Morehouse3bea25e2018-09-13 21:45:55 +0000658 GlobalsToAppendToCompilerUsed.push_back(Array);
659 MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
660 Array->addMetadata(LLVMContext::MD_associated, *MD);
661
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000662 return Array;
663}
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000664
Justin Bogner873a0742017-08-28 23:46:11 +0000665GlobalVariable *
Leonard Chaneca01b02019-09-04 20:30:29 +0000666ModuleSanitizerCoverage::CreatePCArray(Function &F,
667 ArrayRef<BasicBlock *> AllBlocks) {
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000668 size_t N = AllBlocks.size();
669 assert(N);
Kostya Serebryanyd3e4b7e2017-08-25 19:29:47 +0000670 SmallVector<Constant *, 32> PCs;
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000671 IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
Kostya Serebryanyd3e4b7e2017-08-25 19:29:47 +0000672 for (size_t i = 0; i < N; i++) {
673 if (&F.getEntryBlock() == AllBlocks[i]) {
674 PCs.push_back((Constant *)IRB.CreatePointerCast(&F, IntptrPtrTy));
675 PCs.push_back((Constant *)IRB.CreateIntToPtr(
676 ConstantInt::get(IntptrTy, 1), IntptrPtrTy));
677 } else {
678 PCs.push_back((Constant *)IRB.CreatePointerCast(
679 BlockAddress::get(AllBlocks[i]), IntptrPtrTy));
680 PCs.push_back((Constant *)IRB.CreateIntToPtr(
681 ConstantInt::get(IntptrTy, 0), IntptrPtrTy));
682 }
683 }
Justin Bogner873a0742017-08-28 23:46:11 +0000684 auto *PCArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy,
685 SanCovPCsSectionName);
686 PCArray->setInitializer(
Kostya Serebryanyd3e4b7e2017-08-25 19:29:47 +0000687 ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs));
Justin Bogner873a0742017-08-28 23:46:11 +0000688 PCArray->setConstant(true);
Justin Bognerad96ff12017-08-25 01:24:54 +0000689
Justin Bogner873a0742017-08-28 23:46:11 +0000690 return PCArray;
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000691}
692
Leonard Chaneca01b02019-09-04 20:30:29 +0000693void ModuleSanitizerCoverage::CreateFunctionLocalArrays(
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000694 Function &F, ArrayRef<BasicBlock *> AllBlocks) {
Max Moroz4d010ca2018-10-12 13:59:31 +0000695 if (Options.TracePCGuard)
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000696 FunctionGuardArray = CreateFunctionLocalArrayInSection(
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000697 AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
Max Moroz4d010ca2018-10-12 13:59:31 +0000698
Matt Morehouse3bea25e2018-09-13 21:45:55 +0000699 if (Options.Inline8bitCounters)
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000700 Function8bitCounterArray = CreateFunctionLocalArrayInSection(
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000701 AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
Max Moroz4d010ca2018-10-12 13:59:31 +0000702
Matt Morehouse3bea25e2018-09-13 21:45:55 +0000703 if (Options.PCTable)
Justin Bogner873a0742017-08-28 23:46:11 +0000704 FunctionPCsArray = CreatePCArray(F, AllBlocks);
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000705}
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000706
Leonard Chaneca01b02019-09-04 20:30:29 +0000707bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
708 ArrayRef<BasicBlock *> AllBlocks,
709 bool IsLeafFunc) {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000710 if (AllBlocks.empty()) return false;
Kostya Serebryanyb75d0022017-07-27 23:36:49 +0000711 CreateFunctionLocalArrays(F, AllBlocks);
Kostya Serebryanyc485ca02017-07-25 02:07:38 +0000712 for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
Matt Morehouse034126e2017-08-30 22:49:31 +0000713 InjectCoverageAtBlock(F, *AllBlocks[i], i, IsLeafFunc);
Kostya Serebryanyc485ca02017-07-25 02:07:38 +0000714 return true;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000715}
716
717// On every indirect call we call a run-time function
718// __sanitizer_cov_indir_call* with two parameters:
719// - callee address,
Mike Aizatsky759aca02016-03-18 23:29:29 +0000720// - global cache array that contains CacheSize pointers (zero-initialized).
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000721// The cache is used to speed up recording the caller-callee pairs.
722// The address of the caller is passed implicitly via caller PC.
Mike Aizatsky759aca02016-03-18 23:29:29 +0000723// CacheSize is encoded in the name of the run-time function.
Leonard Chaneca01b02019-09-04 20:30:29 +0000724void ModuleSanitizerCoverage::InjectCoverageForIndirectCalls(
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000725 Function &F, ArrayRef<Instruction *> IndirCalls) {
Mike Aizatsky759aca02016-03-18 23:29:29 +0000726 if (IndirCalls.empty())
727 return;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000728 assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000729 for (auto I : IndirCalls) {
730 IRBuilder<> IRB(I);
731 CallSite CS(I);
732 Value *Callee = CS.getCalledValue();
Mike Aizatsky759aca02016-03-18 23:29:29 +0000733 if (isa<InlineAsm>(Callee))
734 continue;
Kostya Serebryanyc5d3d492017-04-19 22:42:11 +0000735 IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000736 }
737}
738
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000739// For every switch statement we insert a call:
740// __sanitizer_cov_trace_switch(CondValue,
741// {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
742
Leonard Chaneca01b02019-09-04 20:30:29 +0000743void ModuleSanitizerCoverage::InjectTraceForSwitch(
Mike Aizatsky759aca02016-03-18 23:29:29 +0000744 Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000745 for (auto I : SwitchTraceTargets) {
746 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
747 IRBuilder<> IRB(I);
748 SmallVector<Constant *, 16> Initializers;
749 Value *Cond = SI->getCondition();
Kostya Serebryany25691182015-08-11 00:24:39 +0000750 if (Cond->getType()->getScalarSizeInBits() >
751 Int64Ty->getScalarSizeInBits())
752 continue;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000753 Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
754 Initializers.push_back(
755 ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
756 if (Cond->getType()->getScalarSizeInBits() <
757 Int64Ty->getScalarSizeInBits())
758 Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000759 for (auto It : SI->cases()) {
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000760 Constant *C = It.getCaseValue();
761 if (C->getType()->getScalarSizeInBits() <
762 Int64Ty->getScalarSizeInBits())
763 C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
764 Initializers.push_back(C);
765 }
Mandeep Singh Grang636d94d2018-04-13 19:47:57 +0000766 llvm::sort(Initializers.begin() + 2, Initializers.end(),
767 [](const Constant *A, const Constant *B) {
768 return cast<ConstantInt>(A)->getLimitedValue() <
769 cast<ConstantInt>(B)->getLimitedValue();
770 });
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000771 ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
772 GlobalVariable *GV = new GlobalVariable(
773 *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
774 ConstantArray::get(ArrayOfInt64Ty, Initializers),
775 "__sancov_gen_cov_switch_values");
776 IRB.CreateCall(SanCovTraceSwitchFunction,
777 {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)});
778 }
779 }
780}
781
Leonard Chaneca01b02019-09-04 20:30:29 +0000782void ModuleSanitizerCoverage::InjectTraceForDiv(
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000783 Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
784 for (auto BO : DivTraceTargets) {
785 IRBuilder<> IRB(BO);
786 Value *A1 = BO->getOperand(1);
787 if (isa<ConstantInt>(A1)) continue;
788 if (!A1->getType()->isIntegerTy())
789 continue;
790 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
791 int CallbackIdx = TypeSize == 32 ? 0 :
792 TypeSize == 64 ? 1 : -1;
793 if (CallbackIdx < 0) continue;
794 auto Ty = Type::getIntNTy(*C, TypeSize);
795 IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
796 {IRB.CreateIntCast(A1, Ty, true)});
797 }
798}
799
Leonard Chaneca01b02019-09-04 20:30:29 +0000800void ModuleSanitizerCoverage::InjectTraceForGep(
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000801 Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
802 for (auto GEP : GepTraceTargets) {
803 IRBuilder<> IRB(GEP);
804 for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I)
Kostya Serebryany45c14472016-09-27 01:55:08 +0000805 if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy())
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000806 IRB.CreateCall(SanCovTraceGepFunction,
807 {IRB.CreateIntCast(*I, IntptrTy, true)});
808 }
809}
810
Leonard Chaneca01b02019-09-04 20:30:29 +0000811void ModuleSanitizerCoverage::InjectTraceForCmp(
Mike Aizatsky759aca02016-03-18 23:29:29 +0000812 Function &, ArrayRef<Instruction *> CmpTraceTargets) {
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000813 for (auto I : CmpTraceTargets) {
814 if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
815 IRBuilder<> IRB(ICMP);
816 Value *A0 = ICMP->getOperand(0);
817 Value *A1 = ICMP->getOperand(1);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000818 if (!A0->getType()->isIntegerTy())
819 continue;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000820 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000821 int CallbackIdx = TypeSize == 8 ? 0 :
822 TypeSize == 16 ? 1 :
823 TypeSize == 32 ? 2 :
824 TypeSize == 64 ? 3 : -1;
825 if (CallbackIdx < 0) continue;
Alexey Samsonov0a648a42015-05-06 21:35:25 +0000826 // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
Alexander Potapenko52410812017-08-10 15:00:13 +0000827 auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
828 bool FirstIsConst = isa<ConstantInt>(A0);
829 bool SecondIsConst = isa<ConstantInt>(A1);
830 // If both are const, then we don't need such a comparison.
831 if (FirstIsConst && SecondIsConst) continue;
832 // If only one is const, then make it the first callback argument.
833 if (FirstIsConst || SecondIsConst) {
834 CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
Justin Bognerbe757de2017-08-28 23:38:12 +0000835 if (SecondIsConst)
Alexander Potapenko52410812017-08-10 15:00:13 +0000836 std::swap(A0, A1);
837 }
838
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000839 auto Ty = Type::getIntNTy(*C, TypeSize);
Justin Bognerbe757de2017-08-28 23:38:12 +0000840 IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true),
Alexander Potapenko52410812017-08-10 15:00:13 +0000841 IRB.CreateIntCast(A1, Ty, true)});
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000842 }
843 }
844}
845
Leonard Chaneca01b02019-09-04 20:30:29 +0000846void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
847 size_t Idx,
848 bool IsLeafFunc) {
Justin Bogner7ae63aa2015-08-14 17:03:45 +0000849 BasicBlock::iterator IP = BB.getFirstInsertionPt();
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000850 bool IsEntryBB = &BB == &F.getEntryBlock();
Alexey Samsonov201733b2015-06-12 01:48:47 +0000851 DebugLoc EntryLoc;
852 if (IsEntryBB) {
Pete Cooperadebb932016-03-11 02:14:16 +0000853 if (auto SP = F.getSubprogram())
Alexey Samsonov201733b2015-06-12 01:48:47 +0000854 EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
Reid Klecknera57d0152015-08-14 16:45:42 +0000855 // Keep static allocas and llvm.localescape calls in the entry block. Even
856 // if we aren't splitting the block, it's nice for allocas to be before
857 // calls.
858 IP = PrepareToSplitEntryBlock(BB, IP);
Alexey Samsonov201733b2015-06-12 01:48:47 +0000859 } else {
860 EntryLoc = IP->getDebugLoc();
861 }
862
Duncan P. N. Exon Smithe82c2862015-10-13 17:39:10 +0000863 IRBuilder<> IRB(&*IP);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000864 IRB.SetCurrentDebugLocation(EntryLoc);
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000865 if (Options.TracePC) {
Kostya Serebryanydd5c7f92016-07-14 17:59:01 +0000866 IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
867 IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000868 }
869 if (Options.TracePCGuard) {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000870 auto GuardPtr = IRB.CreateIntToPtr(
871 IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
872 ConstantInt::get(IntptrTy, Idx * 4)),
873 Int32PtrTy);
Kostya Serebryanyda718e52016-09-14 01:39:35 +0000874 IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
875 IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
Kostya Serebryany77cc7292015-02-04 01:21:45 +0000876 }
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000877 if (Options.Inline8bitCounters) {
878 auto CounterPtr = IRB.CreateGEP(
James Y Knight77160752019-02-01 20:44:47 +0000879 Function8bitCounterArray->getValueType(), Function8bitCounterArray,
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000880 {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
James Y Knight14359ef2019-02-01 20:44:24 +0000881 auto Load = IRB.CreateLoad(Int8Ty, CounterPtr);
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000882 auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
883 auto Store = IRB.CreateStore(Inc, CounterPtr);
884 SetNoSanitizeMetadata(Load);
885 SetNoSanitizeMetadata(Store);
886 }
Matt Morehouse034126e2017-08-30 22:49:31 +0000887 if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000888 // Check stack depth. If it's the deepest so far, record it.
Christudasan Devadasan006cf8c2019-07-22 12:42:48 +0000889 Module *M = F.getParent();
890 Function *GetFrameAddr = Intrinsic::getDeclaration(
891 M, Intrinsic::frameaddress,
892 IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000893 auto FrameAddrPtr =
894 IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
895 auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
James Y Knight14359ef2019-02-01 20:44:24 +0000896 auto LowestStack = IRB.CreateLoad(IntptrTy, SanCovLowestStack);
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000897 auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
898 auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
899 IRBuilder<> ThenIRB(ThenTerm);
Matt Morehouse034126e2017-08-30 22:49:31 +0000900 auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
901 SetNoSanitizeMetadata(LowestStack);
902 SetNoSanitizeMetadata(Store);
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000903 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000904}
905
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000906std::string
Leonard Chaneca01b02019-09-04 20:30:29 +0000907ModuleSanitizerCoverage::getSectionName(const std::string &Section) const {
Jonathan Metzmane159a0d2019-01-14 21:02:02 +0000908 if (TargetTriple.isOSBinFormatCOFF()) {
Matt Morehouse7e042bb2018-08-30 15:54:44 +0000909 if (Section == SanCovCountersSectionName)
910 return ".SCOV$CM";
911 if (Section == SanCovPCsSectionName)
912 return ".SCOVP$M";
913 return ".SCOV$GM"; // For SanCovGuardsSectionName.
914 }
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000915 if (TargetTriple.isOSBinFormatMachO())
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000916 return "__DATA,__" + Section;
917 return "__" + Section;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000918}
919
Leonard Chaneca01b02019-09-04 20:30:29 +0000920std::string
921ModuleSanitizerCoverage::getSectionStart(const std::string &Section) const {
922 if (TargetTriple.isOSBinFormatMachO())
923 return "\1section$start$__DATA$__" + Section;
924 return "__start___" + Section;
925}
Leonard Chanbb147aa2019-07-15 23:18:31 +0000926
Leonard Chaneca01b02019-09-04 20:30:29 +0000927std::string
928ModuleSanitizerCoverage::getSectionEnd(const std::string &Section) const {
929 if (TargetTriple.isOSBinFormatMachO())
930 return "\1section$end$__DATA$__" + Section;
931 return "__stop___" + Section;
932}
933
934char ModuleSanitizerCoverageLegacyPass::ID = 0;
935INITIALIZE_PASS_BEGIN(ModuleSanitizerCoverageLegacyPass, "sancov",
Leonard Chan007f6742019-07-25 20:53:15 +0000936 "Pass for instrumenting coverage on functions", false,
937 false)
Mike Aizatsky90562842016-02-27 05:50:40 +0000938INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
George Karpenkov018472c2017-05-24 00:29:12 +0000939INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Leonard Chaneca01b02019-09-04 20:30:29 +0000940INITIALIZE_PASS_END(ModuleSanitizerCoverageLegacyPass, "sancov",
Leonard Chan007f6742019-07-25 20:53:15 +0000941 "Pass for instrumenting coverage on functions", false,
942 false)
Leonard Chan007f6742019-07-25 20:53:15 +0000943ModulePass *llvm::createModuleSanitizerCoverageLegacyPassPass(
944 const SanitizerCoverageOptions &Options) {
945 return new ModuleSanitizerCoverageLegacyPass(Options);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000946}