blob: e3c36c98ab0db4fd5d0fc115aa596211375d0e3e [file] [log] [blame]
Kostya Serebryany29a18dc2014-11-11 22:14:37 +00001//===-- 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//
Kostya Serebryany53b34c82017-05-31 18:27:33 +000010// Coverage instrumentation done on LLVM IR level, works with Sanitizers.
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000011//
12//===----------------------------------------------------------------------===//
13
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"
20#include "llvm/IR/DataLayout.h"
Alexey Samsonov201733b2015-06-12 01:48:47 +000021#include "llvm/IR/DebugInfo.h"
Mike Aizatsky5971f182016-02-26 01:17:22 +000022#include "llvm/IR/Dominators.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000023#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
Kostya Serebryany73762942014-12-16 21:24:15 +000025#include "llvm/IR/InlineAsm.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000026#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/MDBuilder.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Type.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Mike Aizatsky5971f182016-02-26 01:17:22 +000033#include "llvm/Transforms/Instrumentation.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000034#include "llvm/Transforms/Scalar.h"
35#include "llvm/Transforms/Utils/BasicBlockUtils.h"
36#include "llvm/Transforms/Utils/ModuleUtils.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "sancov"
41
Mike Aizatsky759aca02016-03-18 23:29:29 +000042static const char *const SanCovTracePCIndirName =
43 "__sanitizer_cov_trace_pc_indir";
Mike Aizatsky759aca02016-03-18 23:29:29 +000044static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc";
Kostya Serebryany524c3f32016-08-18 01:25:28 +000045static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1";
46static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2";
47static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4";
48static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8";
Kostya Serebryany5ac427b2016-08-30 01:12:10 +000049static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4";
50static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8";
51static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep";
Mike Aizatsky759aca02016-03-18 23:29:29 +000052static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch";
53static const char *const SanCovModuleCtorName = "sancov.module_ctor";
54static const uint64_t SanCtorAndDtorPriority = 2;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000055
Kostya Serebryanyda718e52016-09-14 01:39:35 +000056static const char *const SanCovTracePCGuardName =
57 "__sanitizer_cov_trace_pc_guard";
58static const char *const SanCovTracePCGuardInitName =
59 "__sanitizer_cov_trace_pc_guard_init";
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000060static const char *const SanCov8bitCountersInitName =
61 "__sanitizer_cov_8bit_counters_init";
Kostya Serebryanyda718e52016-09-14 01:39:35 +000062
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +000063static const char *const SanCovGuardsSectionName = "sancov_guards";
George Karpenkov406c1132017-06-14 23:40:25 +000064static const char *const SanCovCountersSectionName = "sancov_cntrs";
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +000065
Mike Aizatsky759aca02016-03-18 23:29:29 +000066static cl::opt<int> ClCoverageLevel(
67 "sanitizer-coverage-level",
68 cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
Kostya Serebryanyc5d3d492017-04-19 22:42:11 +000069 "3: all blocks and critical edges"),
Mike Aizatsky759aca02016-03-18 23:29:29 +000070 cl::Hidden, cl::init(0));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000071
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000072static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc",
73 cl::desc("Experimental pc tracing"), cl::Hidden,
74 cl::init(false));
Kostya Serebryanyd4590c72016-02-17 21:34:43 +000075
Kostya Serebryanyda718e52016-09-14 01:39:35 +000076static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard",
77 cl::desc("pc tracing with a guard"),
78 cl::Hidden, cl::init(false));
79
Kostya Serebryany2c2fb882017-06-08 22:58:19 +000080static cl::opt<bool> ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
81 cl::desc("increments 8-bit counter for every edge"),
82 cl::Hidden, cl::init(false));
83
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +000084static cl::opt<bool>
Kostya Serebryany5ac427b2016-08-30 01:12:10 +000085 ClCMPTracing("sanitizer-coverage-trace-compares",
86 cl::desc("Tracing of CMP and similar instructions"),
87 cl::Hidden, cl::init(false));
88
89static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs",
90 cl::desc("Tracing of DIV instructions"),
91 cl::Hidden, cl::init(false));
92
93static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps",
94 cl::desc("Tracing of GEP instructions"),
95 cl::Hidden, cl::init(false));
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +000096
Mike Aizatsky70ea4532016-04-06 23:24:37 +000097static cl::opt<bool>
98 ClPruneBlocks("sanitizer-coverage-prune-blocks",
99 cl::desc("Reduce the number of instrumented blocks"),
100 cl::Hidden, cl::init(true));
Mike Aizatsky5971f182016-02-26 01:17:22 +0000101
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000102namespace {
103
Alexey Samsonov3514f272015-05-07 01:00:31 +0000104SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
105 SanitizerCoverageOptions Res;
106 switch (LegacyCoverageLevel) {
107 case 0:
108 Res.CoverageType = SanitizerCoverageOptions::SCK_None;
109 break;
110 case 1:
111 Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
112 break;
113 case 2:
114 Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
115 break;
116 case 3:
117 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
118 break;
119 case 4:
120 Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
121 Res.IndirectCalls = true;
122 break;
123 }
124 return Res;
125}
126
127SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
128 // Sets CoverageType and IndirectCalls.
129 SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
130 Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
131 Options.IndirectCalls |= CLOpts.IndirectCalls;
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000132 Options.TraceCmp |= ClCMPTracing;
133 Options.TraceDiv |= ClDIVTracing;
134 Options.TraceGep |= ClGEPTracing;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000135 Options.TracePC |= ClTracePC;
Kostya Serebryanyda718e52016-09-14 01:39:35 +0000136 Options.TracePCGuard |= ClTracePCGuard;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000137 Options.Inline8bitCounters |= ClInline8bitCounters;
138 if (!Options.TracePCGuard && !Options.TracePC && !Options.Inline8bitCounters)
Kostya Serebryany53b34c82017-05-31 18:27:33 +0000139 Options.TracePCGuard = true; // TracePCGuard is default.
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000140 Options.NoPrune |= !ClPruneBlocks;
Alexey Samsonov3514f272015-05-07 01:00:31 +0000141 return Options;
142}
143
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000144class SanitizerCoverageModule : public ModulePass {
Mike Aizatsky759aca02016-03-18 23:29:29 +0000145public:
Alexey Samsonov3514f272015-05-07 01:00:31 +0000146 SanitizerCoverageModule(
147 const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
Chandler Carruthe2b70212016-03-18 22:35:58 +0000148 : ModulePass(ID), Options(OverrideFromCL(Options)) {
149 initializeSanitizerCoverageModulePass(*PassRegistry::getPassRegistry());
150 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000151 bool runOnModule(Module &M) override;
152 bool runOnFunction(Function &F);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000153 static char ID; // Pass identification, replacement for typeid
Mehdi Amini117296c2016-10-01 02:56:57 +0000154 StringRef getPassName() const override { return "SanitizerCoverageModule"; }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000155
Chandler Carruth30061152016-03-18 22:43:42 +0000156 void getAnalysisUsage(AnalysisUsage &AU) const override {
157 AU.addRequired<DominatorTreeWrapperPass>();
George Karpenkov018472c2017-05-24 00:29:12 +0000158 AU.addRequired<PostDominatorTreeWrapperPass>();
Chandler Carruth30061152016-03-18 22:43:42 +0000159 }
160
161private:
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000162 void InjectCoverageForIndirectCalls(Function &F,
163 ArrayRef<Instruction *> IndirCalls);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000164 void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000165 void InjectTraceForDiv(Function &F,
166 ArrayRef<BinaryOperator *> DivTraceTargets);
167 void InjectTraceForGep(Function &F,
168 ArrayRef<GetElementPtrInst *> GepTraceTargets);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000169 void InjectTraceForSwitch(Function &F,
170 ArrayRef<Instruction *> SwitchTraceTargets);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000171 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000172 GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
173 Function &F, Type *Ty,
174 const char *Section);
175 void CreateFunctionLocalArrays(size_t NumGuards, Function &F);
Kostya Serebryany53b34c82017-05-31 18:27:33 +0000176 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx);
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000177 void CreateInitCallForSection(Module &M, const char *InitFunctionName,
178 Type *Ty, const std::string &Section);
179
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000180 void SetNoSanitizeMetadata(Instruction *I) {
181 I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
182 MDNode::get(*C, None));
183 }
184
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000185 std::string getSectionName(const std::string &Section) const;
186 std::string getSectionStart(const std::string &Section) const;
187 std::string getSectionEnd(const std::string &Section) const;
Kostya Serebryanyc5d3d492017-04-19 22:42:11 +0000188 Function *SanCovTracePCIndir;
Kostya Serebryanybe87d482017-04-19 21:48:09 +0000189 Function *SanCovTracePC, *SanCovTracePCGuard;
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000190 Function *SanCovTraceCmpFunction[4];
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000191 Function *SanCovTraceDivFunction[2];
192 Function *SanCovTraceGepFunction;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000193 Function *SanCovTraceSwitchFunction;
Kostya Serebryany73762942014-12-16 21:24:15 +0000194 InlineAsm *EmptyAsm;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000195 Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy,
196 *Int8Ty, *Int8PtrTy;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000197 Module *CurModule;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000198 Triple TargetTriple;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000199 LLVMContext *C;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000200 const DataLayout *DL;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000201
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000202 GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000203 GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000204
Alexey Samsonov3514f272015-05-07 01:00:31 +0000205 SanitizerCoverageOptions Options;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000206};
207
Mike Aizatsky759aca02016-03-18 23:29:29 +0000208} // namespace
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000209
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000210void SanitizerCoverageModule::CreateInitCallForSection(
211 Module &M, const char *InitFunctionName, Type *Ty,
212 const std::string &Section) {
213 IRBuilder<> IRB(M.getContext());
214 Function *CtorFunc;
215 GlobalVariable *SecStart =
216 new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
217 getSectionStart(Section));
218 SecStart->setVisibility(GlobalValue::HiddenVisibility);
219 GlobalVariable *SecEnd =
220 new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
221 nullptr, getSectionEnd(Section));
222 SecEnd->setVisibility(GlobalValue::HiddenVisibility);
223
224 std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
225 M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty},
226 {IRB.CreatePointerCast(SecStart, Ty), IRB.CreatePointerCast(SecEnd, Ty)});
227
228 if (TargetTriple.supportsCOMDAT()) {
229 // Use comdat to dedup CtorFunc.
230 CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName));
231 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
232 } else {
233 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
234 }
235}
236
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000237bool SanitizerCoverageModule::runOnModule(Module &M) {
Alexey Samsonov3514f272015-05-07 01:00:31 +0000238 if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
239 return false;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000240 C = &(M.getContext());
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000241 DL = &M.getDataLayout();
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000242 CurModule = &M;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000243 TargetTriple = Triple(M.getTargetTriple());
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000244 FunctionGuardArray = nullptr;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000245 Function8bitCounterArray = nullptr;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000246 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000247 IntptrPtrTy = PointerType::getUnqual(IntptrTy);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000248 Type *VoidTy = Type::getVoidTy(*C);
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000249 IRBuilder<> IRB(*C);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000250 Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty());
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000251 Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000252 Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000253 Int64Ty = IRB.getInt64Ty();
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000254 Int32Ty = IRB.getInt32Ty();
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000255 Int8Ty = IRB.getInt8Ty();
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000256
Mike Aizatsky759aca02016-03-18 23:29:29 +0000257 SanCovTracePCIndir = checkSanitizerInterfaceFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000258 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000259 SanCovTraceCmpFunction[0] =
Ismail Pazarbasi198d6d52015-04-06 21:09:08 +0000260 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000261 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000262 SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
263 M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000264 IRB.getInt16Ty()));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000265 SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
266 M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000267 IRB.getInt32Ty()));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000268 SanCovTraceCmpFunction[3] =
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000269 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000270 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000271
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000272 SanCovTraceDivFunction[0] =
273 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000274 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000275 SanCovTraceDivFunction[1] =
276 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000277 SanCovTraceDiv8, VoidTy, Int64Ty));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000278 SanCovTraceGepFunction =
279 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000280 SanCovTraceGep, VoidTy, IntptrTy));
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000281 SanCovTraceSwitchFunction =
282 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000283 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000284
Kostya Serebryany73762942014-12-16 21:24:15 +0000285 // We insert an empty inline asm after cov callbacks to avoid callback merge.
286 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
287 StringRef(""), StringRef(""),
288 /*hasSideEffects=*/true);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000289
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000290 SanCovTracePC = checkSanitizerInterfaceFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000291 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
Mehdi Aminidb11fdf2017-04-06 20:23:57 +0000292 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000293 SanCovTracePCGuardName, VoidTy, Int32PtrTy));
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000294
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000295 for (auto &F : M)
296 runOnFunction(F);
297
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000298 if (FunctionGuardArray)
299 CreateInitCallForSection(M, SanCovTracePCGuardInitName, Int32PtrTy,
300 SanCovGuardsSectionName);
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000301 if (Function8bitCounterArray)
302 CreateInitCallForSection(M, SanCov8bitCountersInitName, Int8PtrTy,
303 SanCovCountersSectionName);
Justin Bogner41e632b2017-02-01 02:38:39 +0000304
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000305 return true;
306}
307
Mike Aizatsky9987f432016-03-23 23:15:03 +0000308// True if block has successors and it dominates all of them.
309static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) {
310 if (succ_begin(BB) == succ_end(BB))
311 return false;
312
313 for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) {
314 if (!DT->dominates(BB, SUCC))
315 return false;
316 }
317
318 return true;
319}
320
George Karpenkov018472c2017-05-24 00:29:12 +0000321// True if block has predecessors and it postdominates all of them.
322static bool isFullPostDominator(const BasicBlock *BB,
323 const PostDominatorTree *PDT) {
324 if (pred_begin(BB) == pred_end(BB))
325 return false;
326
327 for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) {
328 if (!PDT->dominates(BB, PRED))
329 return false;
330 }
331
332 return true;
333}
334
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000335static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
336 const DominatorTree *DT,
George Karpenkov018472c2017-05-24 00:29:12 +0000337 const PostDominatorTree *PDT,
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000338 const SanitizerCoverageOptions &Options) {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000339 // Don't insert coverage for unreachable blocks: we will never call
340 // __sanitizer_cov() for them, so counting them in
341 // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
342 // percentage. Also, unreachable instructions frequently have no debug
343 // locations.
344 if (isa<UnreachableInst>(BB->getTerminator()))
345 return false;
346
Reid Kleckner392f0622017-03-23 23:30:41 +0000347 // Don't insert coverage into blocks without a valid insertion point
348 // (catchswitch blocks).
349 if (BB->getFirstInsertionPt() == BB->end())
350 return false;
351
Kostya Serebryany424bfed2017-05-05 23:14:40 +0000352 if (Options.NoPrune || &F.getEntryBlock() == BB)
Mike Aizatsky5971f182016-02-26 01:17:22 +0000353 return true;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000354
George Karpenkova1c53272017-05-25 01:41:46 +0000355 // Do not instrument full dominators, or full post-dominators with multiple
356 // predecessors.
357 return !isFullDominator(BB, DT)
358 && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
Mike Aizatsky5971f182016-02-26 01:17:22 +0000359}
360
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000361bool SanitizerCoverageModule::runOnFunction(Function &F) {
Mike Aizatsky759aca02016-03-18 23:29:29 +0000362 if (F.empty())
363 return false;
Kostya Serebryanyfea4fb42014-12-17 21:50:04 +0000364 if (F.getName().find(".module_ctor") != std::string::npos)
Mike Aizatsky759aca02016-03-18 23:29:29 +0000365 return false; // Should not instrument sanitizer init functions.
Kostya Serebryanyda718e52016-09-14 01:39:35 +0000366 if (F.getName().startswith("__sanitizer_"))
367 return false; // Don't instrument __sanitizer_* callbacks.
Reid Klecknerec803542016-11-11 19:18:45 +0000368 // Don't instrument MSVC CRT configuration helpers. They may run before normal
369 // initialization.
370 if (F.getName() == "__local_stdio_printf_options" ||
371 F.getName() == "__local_stdio_scanf_options")
372 return false;
Reid Klecknerdf523372015-09-03 20:18:29 +0000373 // Don't instrument functions using SEH for now. Splitting basic blocks like
374 // we do for coverage breaks WinEHPrepare.
375 // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
376 if (F.hasPersonalityFn() &&
377 isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
378 return false;
Alexey Samsonov3514f272015-05-07 01:00:31 +0000379 if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
Chandler Carruth37df2cf2015-01-19 12:09:11 +0000380 SplitAllCriticalEdges(F);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000381 SmallVector<Instruction *, 8> IndirCalls;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000382 SmallVector<BasicBlock *, 16> BlocksToInstrument;
Mike Aizatsky759aca02016-03-18 23:29:29 +0000383 SmallVector<Instruction *, 8> CmpTraceTargets;
384 SmallVector<Instruction *, 8> SwitchTraceTargets;
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000385 SmallVector<BinaryOperator *, 8> DivTraceTargets;
386 SmallVector<GetElementPtrInst *, 8> GepTraceTargets;
Mike Aizatsky5971f182016-02-26 01:17:22 +0000387
Mike Aizatsky602f7922016-03-21 23:08:16 +0000388 const DominatorTree *DT =
389 &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
George Karpenkov018472c2017-05-24 00:29:12 +0000390 const PostDominatorTree *PDT =
391 &getAnalysis<PostDominatorTreeWrapperPass>(F).getPostDomTree();
Mike Aizatsky602f7922016-03-21 23:08:16 +0000392
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000393 for (auto &BB : F) {
George Karpenkov018472c2017-05-24 00:29:12 +0000394 if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
Mike Aizatsky5971f182016-02-26 01:17:22 +0000395 BlocksToInstrument.push_back(&BB);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000396 for (auto &Inst : BB) {
Alexey Samsonov3514f272015-05-07 01:00:31 +0000397 if (Options.IndirectCalls) {
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000398 CallSite CS(&Inst);
399 if (CS && !CS.getCalledFunction())
400 IndirCalls.push_back(&Inst);
401 }
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000402 if (Options.TraceCmp) {
403 if (isa<ICmpInst>(&Inst))
404 CmpTraceTargets.push_back(&Inst);
405 if (isa<SwitchInst>(&Inst))
406 SwitchTraceTargets.push_back(&Inst);
407 }
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000408 if (Options.TraceDiv)
409 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst))
410 if (BO->getOpcode() == Instruction::SDiv ||
411 BO->getOpcode() == Instruction::UDiv)
412 DivTraceTargets.push_back(BO);
413 if (Options.TraceGep)
414 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst))
415 GepTraceTargets.push_back(GEP);
416 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000417 }
Mike Aizatsky5971f182016-02-26 01:17:22 +0000418
419 InjectCoverage(F, BlocksToInstrument);
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000420 InjectCoverageForIndirectCalls(F, IndirCalls);
421 InjectTraceForCmp(F, CmpTraceTargets);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000422 InjectTraceForSwitch(F, SwitchTraceTargets);
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000423 InjectTraceForDiv(F, DivTraceTargets);
424 InjectTraceForGep(F, GepTraceTargets);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000425 return true;
426}
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000427
428GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
429 size_t NumElements, Function &F, Type *Ty, const char *Section) {
430 ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
431 auto Array = new GlobalVariable(
432 *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
433 Constant::getNullValue(ArrayTy), "__sancov_gen_");
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000434 if (auto Comdat = F.getComdat())
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000435 Array->setComdat(Comdat);
436 Array->setSection(getSectionName(Section));
437 return Array;
438}
439void SanitizerCoverageModule::CreateFunctionLocalArrays(size_t NumGuards,
440 Function &F) {
441 if (Options.TracePCGuard)
442 FunctionGuardArray = CreateFunctionLocalArrayInSection(
443 NumGuards, F, Int32Ty, SanCovGuardsSectionName);
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000444 if (Options.Inline8bitCounters)
445 Function8bitCounterArray = CreateFunctionLocalArrayInSection(
446 NumGuards, F, Int8Ty, SanCovCountersSectionName);
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000447}
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000448
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000449bool SanitizerCoverageModule::InjectCoverage(Function &F,
450 ArrayRef<BasicBlock *> AllBlocks) {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000451 if (AllBlocks.empty()) return false;
Alexey Samsonov3514f272015-05-07 01:00:31 +0000452 switch (Options.CoverageType) {
453 case SanitizerCoverageOptions::SCK_None:
454 return false;
455 case SanitizerCoverageOptions::SCK_Function:
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000456 CreateFunctionLocalArrays(1, F);
Kostya Serebryany53b34c82017-05-31 18:27:33 +0000457 InjectCoverageAtBlock(F, F.getEntryBlock(), 0);
Alexey Samsonov3514f272015-05-07 01:00:31 +0000458 return true;
459 default: {
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000460 CreateFunctionLocalArrays(AllBlocks.size(), F);
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000461 for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
Kostya Serebryany53b34c82017-05-31 18:27:33 +0000462 InjectCoverageAtBlock(F, *AllBlocks[i], i);
Alexey Samsonov3514f272015-05-07 01:00:31 +0000463 return true;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000464 }
Alexey Samsonov3514f272015-05-07 01:00:31 +0000465 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000466}
467
468// On every indirect call we call a run-time function
469// __sanitizer_cov_indir_call* with two parameters:
470// - callee address,
Mike Aizatsky759aca02016-03-18 23:29:29 +0000471// - global cache array that contains CacheSize pointers (zero-initialized).
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000472// The cache is used to speed up recording the caller-callee pairs.
473// The address of the caller is passed implicitly via caller PC.
Mike Aizatsky759aca02016-03-18 23:29:29 +0000474// CacheSize is encoded in the name of the run-time function.
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000475void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
476 Function &F, ArrayRef<Instruction *> IndirCalls) {
Mike Aizatsky759aca02016-03-18 23:29:29 +0000477 if (IndirCalls.empty())
478 return;
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000479 assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000480 for (auto I : IndirCalls) {
481 IRBuilder<> IRB(I);
482 CallSite CS(I);
483 Value *Callee = CS.getCalledValue();
Mike Aizatsky759aca02016-03-18 23:29:29 +0000484 if (isa<InlineAsm>(Callee))
485 continue;
Kostya Serebryanyc5d3d492017-04-19 22:42:11 +0000486 IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000487 }
488}
489
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000490// For every switch statement we insert a call:
491// __sanitizer_cov_trace_switch(CondValue,
492// {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
493
494void SanitizerCoverageModule::InjectTraceForSwitch(
Mike Aizatsky759aca02016-03-18 23:29:29 +0000495 Function &, ArrayRef<Instruction *> SwitchTraceTargets) {
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000496 for (auto I : SwitchTraceTargets) {
497 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
498 IRBuilder<> IRB(I);
499 SmallVector<Constant *, 16> Initializers;
500 Value *Cond = SI->getCondition();
Kostya Serebryany25691182015-08-11 00:24:39 +0000501 if (Cond->getType()->getScalarSizeInBits() >
502 Int64Ty->getScalarSizeInBits())
503 continue;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000504 Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
505 Initializers.push_back(
506 ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
507 if (Cond->getType()->getScalarSizeInBits() <
508 Int64Ty->getScalarSizeInBits())
509 Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000510 for (auto It : SI->cases()) {
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000511 Constant *C = It.getCaseValue();
512 if (C->getType()->getScalarSizeInBits() <
513 Int64Ty->getScalarSizeInBits())
514 C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty);
515 Initializers.push_back(C);
516 }
Kostya Serebryanyf24e52c2016-12-27 21:20:06 +0000517 std::sort(Initializers.begin() + 2, Initializers.end(),
518 [](const Constant *A, const Constant *B) {
519 return cast<ConstantInt>(A)->getLimitedValue() <
520 cast<ConstantInt>(B)->getLimitedValue();
521 });
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000522 ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
523 GlobalVariable *GV = new GlobalVariable(
524 *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
525 ConstantArray::get(ArrayOfInt64Ty, Initializers),
526 "__sancov_gen_cov_switch_values");
527 IRB.CreateCall(SanCovTraceSwitchFunction,
528 {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)});
529 }
530 }
531}
532
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000533void SanitizerCoverageModule::InjectTraceForDiv(
534 Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
535 for (auto BO : DivTraceTargets) {
536 IRBuilder<> IRB(BO);
537 Value *A1 = BO->getOperand(1);
538 if (isa<ConstantInt>(A1)) continue;
539 if (!A1->getType()->isIntegerTy())
540 continue;
541 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
542 int CallbackIdx = TypeSize == 32 ? 0 :
543 TypeSize == 64 ? 1 : -1;
544 if (CallbackIdx < 0) continue;
545 auto Ty = Type::getIntNTy(*C, TypeSize);
546 IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
547 {IRB.CreateIntCast(A1, Ty, true)});
548 }
549}
550
551void SanitizerCoverageModule::InjectTraceForGep(
552 Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
553 for (auto GEP : GepTraceTargets) {
554 IRBuilder<> IRB(GEP);
555 for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I)
Kostya Serebryany45c14472016-09-27 01:55:08 +0000556 if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy())
Kostya Serebryany5ac427b2016-08-30 01:12:10 +0000557 IRB.CreateCall(SanCovTraceGepFunction,
558 {IRB.CreateIntCast(*I, IntptrTy, true)});
559 }
560}
561
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000562void SanitizerCoverageModule::InjectTraceForCmp(
Mike Aizatsky759aca02016-03-18 23:29:29 +0000563 Function &, ArrayRef<Instruction *> CmpTraceTargets) {
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000564 for (auto I : CmpTraceTargets) {
565 if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
566 IRBuilder<> IRB(ICMP);
567 Value *A0 = ICMP->getOperand(0);
568 Value *A1 = ICMP->getOperand(1);
Mike Aizatsky759aca02016-03-18 23:29:29 +0000569 if (!A0->getType()->isIntegerTy())
570 continue;
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000571 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000572 int CallbackIdx = TypeSize == 8 ? 0 :
573 TypeSize == 16 ? 1 :
574 TypeSize == 32 ? 2 :
575 TypeSize == 64 ? 3 : -1;
576 if (CallbackIdx < 0) continue;
Alexey Samsonov0a648a42015-05-06 21:35:25 +0000577 // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000578 auto Ty = Type::getIntNTy(*C, TypeSize);
David Blaikieff6409d2015-05-18 22:13:54 +0000579 IRB.CreateCall(
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000580 SanCovTraceCmpFunction[CallbackIdx],
581 {IRB.CreateIntCast(A0, Ty, true), IRB.CreateIntCast(A1, Ty, true)});
Kostya Serebryanyf4e35cc2015-03-21 01:29:36 +0000582 }
583 }
584}
585
Kostya Serebryany77cc7292015-02-04 01:21:45 +0000586void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
Kostya Serebryany53b34c82017-05-31 18:27:33 +0000587 size_t Idx) {
Justin Bogner7ae63aa2015-08-14 17:03:45 +0000588 BasicBlock::iterator IP = BB.getFirstInsertionPt();
Kostya Serebryanyd421db02015-01-03 00:54:43 +0000589 bool IsEntryBB = &BB == &F.getEntryBlock();
Alexey Samsonov201733b2015-06-12 01:48:47 +0000590 DebugLoc EntryLoc;
591 if (IsEntryBB) {
Pete Cooperadebb932016-03-11 02:14:16 +0000592 if (auto SP = F.getSubprogram())
Alexey Samsonov201733b2015-06-12 01:48:47 +0000593 EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
Reid Klecknera57d0152015-08-14 16:45:42 +0000594 // Keep static allocas and llvm.localescape calls in the entry block. Even
595 // if we aren't splitting the block, it's nice for allocas to be before
596 // calls.
597 IP = PrepareToSplitEntryBlock(BB, IP);
Alexey Samsonov201733b2015-06-12 01:48:47 +0000598 } else {
599 EntryLoc = IP->getDebugLoc();
600 }
601
Duncan P. N. Exon Smithe82c2862015-10-13 17:39:10 +0000602 IRBuilder<> IRB(&*IP);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000603 IRB.SetCurrentDebugLocation(EntryLoc);
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000604 if (Options.TracePC) {
Kostya Serebryanydd5c7f92016-07-14 17:59:01 +0000605 IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC.
606 IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000607 }
608 if (Options.TracePCGuard) {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000609 auto GuardPtr = IRB.CreateIntToPtr(
610 IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy),
611 ConstantInt::get(IntptrTy, Idx * 4)),
612 Int32PtrTy);
Kostya Serebryanyda718e52016-09-14 01:39:35 +0000613 IRB.CreateCall(SanCovTracePCGuard, GuardPtr);
614 IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
Kostya Serebryany77cc7292015-02-04 01:21:45 +0000615 }
Kostya Serebryany2c2fb882017-06-08 22:58:19 +0000616 if (Options.Inline8bitCounters) {
617 auto CounterPtr = IRB.CreateGEP(
618 Function8bitCounterArray,
619 {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
620 auto Load = IRB.CreateLoad(CounterPtr);
621 auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
622 auto Store = IRB.CreateStore(Inc, CounterPtr);
623 SetNoSanitizeMetadata(Load);
624 SetNoSanitizeMetadata(Store);
625 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000626}
627
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000628std::string
629SanitizerCoverageModule::getSectionName(const std::string &Section) const {
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000630 if (TargetTriple.getObjectFormat() == Triple::COFF)
631 return ".SCOV$M";
632 if (TargetTriple.isOSBinFormatMachO())
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000633 return "__DATA,__" + Section;
634 return "__" + Section;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000635}
636
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000637std::string
638SanitizerCoverageModule::getSectionStart(const std::string &Section) const {
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000639 if (TargetTriple.isOSBinFormatMachO())
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000640 return "\1section$start$__DATA$__" + Section;
641 return "__start___" + Section;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000642}
643
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000644std::string
645SanitizerCoverageModule::getSectionEnd(const std::string &Section) const {
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000646 if (TargetTriple.isOSBinFormatMachO())
Kostya Serebryanyaed6ba72017-06-02 23:13:44 +0000647 return "\1section$end$__DATA$__" + Section;
648 return "__stop___" + Section;
Marcos Pividoridb5a5652017-02-03 01:08:06 +0000649}
650
651
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000652char SanitizerCoverageModule::ID = 0;
Mike Aizatsky90562842016-02-27 05:50:40 +0000653INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov",
Mike Aizatsky759aca02016-03-18 23:29:29 +0000654 "SanitizerCoverage: TODO."
655 "ModulePass",
656 false, false)
Mike Aizatsky90562842016-02-27 05:50:40 +0000657INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
George Karpenkov018472c2017-05-24 00:29:12 +0000658INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Mike Aizatsky90562842016-02-27 05:50:40 +0000659INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov",
Mike Aizatsky759aca02016-03-18 23:29:29 +0000660 "SanitizerCoverage: TODO."
661 "ModulePass",
662 false, false)
Alexey Samsonov3514f272015-05-07 01:00:31 +0000663ModulePass *llvm::createSanitizerCoverageModulePass(
664 const SanitizerCoverageOptions &Options) {
665 return new SanitizerCoverageModule(Options);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000666}