| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 1 | //===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 9 | // Coverage instrumentation done on LLVM IR level, works with Sanitizers. |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
| David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/EHPersonalities.h" |
| George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/PostDominators.h" |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CFG.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CallSite.h" |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constant.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DebugInfo.h" |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalVariable.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 26 | #include "llvm/IR/IRBuilder.h" |
| Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 27 | #include "llvm/IR/InlineAsm.h" |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IntrinsicInst.h" |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Intrinsics.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/MDBuilder.h" |
| Jonathan Metzman | 0b94e88 | 2018-10-12 18:11:47 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Mangler.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IR/Type.h" |
| 35 | #include "llvm/Support/CommandLine.h" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Instrumentation.h" |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 40 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
| 44 | #define DEBUG_TYPE "sancov" |
| 45 | |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 46 | static const char *const SanCovTracePCIndirName = |
| 47 | "__sanitizer_cov_trace_pc_indir"; |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 48 | static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc"; |
| Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 49 | static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1"; |
| 50 | static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2"; |
| 51 | static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4"; |
| 52 | static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8"; |
| Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 53 | static const char *const SanCovTraceConstCmp1 = |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 54 | "__sanitizer_cov_trace_const_cmp1"; |
| Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 55 | static const char *const SanCovTraceConstCmp2 = |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 56 | "__sanitizer_cov_trace_const_cmp2"; |
| Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 57 | static const char *const SanCovTraceConstCmp4 = |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 58 | "__sanitizer_cov_trace_const_cmp4"; |
| Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 59 | static const char *const SanCovTraceConstCmp8 = |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 60 | "__sanitizer_cov_trace_const_cmp8"; |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 61 | static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4"; |
| 62 | static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8"; |
| 63 | static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep"; |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 64 | static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch"; |
| Fangrui Song | a400ca3 | 2019-05-07 01:39:37 +0000 | [diff] [blame] | 65 | static const char *const SanCovModuleCtorTracePcGuardName = |
| 66 | "sancov.module_ctor_trace_pc_guard"; |
| 67 | static const char *const SanCovModuleCtor8bitCountersName = |
| 68 | "sancov.module_ctor_8bit_counters"; |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 69 | static const uint64_t SanCtorAndDtorPriority = 2; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 70 | |
| Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 71 | static const char *const SanCovTracePCGuardName = |
| 72 | "__sanitizer_cov_trace_pc_guard"; |
| 73 | static const char *const SanCovTracePCGuardInitName = |
| 74 | "__sanitizer_cov_trace_pc_guard_init"; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 75 | static const char *const SanCov8bitCountersInitName = |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 76 | "__sanitizer_cov_8bit_counters_init"; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 77 | static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init"; |
| Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 78 | |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 79 | static const char *const SanCovGuardsSectionName = "sancov_guards"; |
| George Karpenkov | 406c113 | 2017-06-14 23:40:25 +0000 | [diff] [blame] | 80 | static const char *const SanCovCountersSectionName = "sancov_cntrs"; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 81 | static const char *const SanCovPCsSectionName = "sancov_pcs"; |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 82 | |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 83 | static const char *const SanCovLowestStackName = "__sancov_lowest_stack"; |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 84 | |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 85 | static cl::opt<int> ClCoverageLevel( |
| 86 | "sanitizer-coverage-level", |
| 87 | cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, " |
| Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 88 | "3: all blocks and critical edges"), |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 89 | cl::Hidden, cl::init(0)); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 90 | |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 91 | static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc", |
| 92 | cl::desc("Experimental pc tracing"), cl::Hidden, |
| 93 | cl::init(false)); |
| Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 94 | |
| Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 95 | static 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 Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 99 | // 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 Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 104 | static cl::opt<bool> ClCreatePCTable("sanitizer-coverage-pc-table", |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 105 | cl::desc("create a static PC table"), |
| 106 | cl::Hidden, cl::init(false)); |
| 107 | |
| 108 | static 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 Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 112 | |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 113 | static cl::opt<bool> |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 114 | ClCMPTracing("sanitizer-coverage-trace-compares", |
| 115 | cl::desc("Tracing of CMP and similar instructions"), |
| 116 | cl::Hidden, cl::init(false)); |
| 117 | |
| 118 | static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs", |
| 119 | cl::desc("Tracing of DIV instructions"), |
| 120 | cl::Hidden, cl::init(false)); |
| 121 | |
| 122 | static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps", |
| 123 | cl::desc("Tracing of GEP instructions"), |
| 124 | cl::Hidden, cl::init(false)); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 125 | |
| Mike Aizatsky | 70ea453 | 2016-04-06 23:24:37 +0000 | [diff] [blame] | 126 | static 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 Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 130 | |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 131 | static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth", |
| 132 | cl::desc("max stack depth tracing"), |
| 133 | cl::Hidden, cl::init(false)); |
| 134 | |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 135 | namespace { |
| 136 | |
| Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 137 | SanitizerCoverageOptions 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 | |
| 160 | SanitizerCoverageOptions 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 Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 165 | Options.TraceCmp |= ClCMPTracing; |
| 166 | Options.TraceDiv |= ClDIVTracing; |
| 167 | Options.TraceGep |= ClGEPTracing; |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 168 | Options.TracePC |= ClTracePC; |
| Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 169 | Options.TracePCGuard |= ClTracePCGuard; |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 170 | Options.Inline8bitCounters |= ClInline8bitCounters; |
| Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 171 | Options.PCTable |= ClCreatePCTable; |
| Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 172 | Options.NoPrune |= !ClPruneBlocks; |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 173 | Options.StackDepth |= ClStackDepth; |
| 174 | if (!Options.TracePCGuard && !Options.TracePC && |
| 175 | !Options.Inline8bitCounters && !Options.StackDepth) |
| 176 | Options.TracePCGuard = true; // TracePCGuard is default. |
| Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 177 | return Options; |
| 178 | } |
| 179 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 180 | using DomTreeCallback = function_ref<const DominatorTree *(Function &F)>; |
| 181 | using PostDomTreeCallback = |
| 182 | function_ref<const PostDominatorTree *(Function &F)>; |
| Leonard Chan | 5652f35 | 2019-07-11 22:35:40 +0000 | [diff] [blame] | 183 | |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 184 | class ModuleSanitizerCoverage { |
| 185 | public: |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 186 | ModuleSanitizerCoverage( |
| 187 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()) |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 188 | : Options(OverrideFromCL(Options)) {} |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 189 | bool instrumentModule(Module &M, DomTreeCallback DTCallback, |
| 190 | PostDomTreeCallback PDTCallback); |
| Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 191 | |
| 192 | private: |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 193 | void instrumentFunction(Function &F, DomTreeCallback DTCallback, |
| 194 | PostDomTreeCallback PDTCallback); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 195 | void InjectCoverageForIndirectCalls(Function &F, |
| 196 | ArrayRef<Instruction *> IndirCalls); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 197 | void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets); |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 198 | void InjectTraceForDiv(Function &F, |
| 199 | ArrayRef<BinaryOperator *> DivTraceTargets); |
| 200 | void InjectTraceForGep(Function &F, |
| 201 | ArrayRef<GetElementPtrInst *> GepTraceTargets); |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 202 | void InjectTraceForSwitch(Function &F, |
| 203 | ArrayRef<Instruction *> SwitchTraceTargets); |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 204 | bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks, |
| 205 | bool IsLeafFunc = true); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 206 | GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements, |
| 207 | Function &F, Type *Ty, |
| 208 | const char *Section); |
| Justin Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 209 | GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 210 | void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 211 | void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx, |
| 212 | bool IsLeafFunc = true); |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 213 | 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 Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 218 | |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 219 | void SetNoSanitizeMetadata(Instruction *I) { |
| 220 | I->setMetadata(I->getModule()->getMDKindID("nosanitize"), |
| 221 | MDNode::get(*C, None)); |
| 222 | } |
| 223 | |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 224 | std::string getSectionName(const std::string &Section) const; |
| Leonard Chan | bb147aa | 2019-07-15 23:18:31 +0000 | [diff] [blame] | 225 | std::string getSectionStart(const std::string &Section) const; |
| 226 | std::string getSectionEnd(const std::string &Section) const; |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 227 | 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 Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 234 | GlobalVariable *SanCovLowestStack; |
| Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 235 | InlineAsm *EmptyAsm; |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 236 | Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy, |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 237 | *Int16Ty, *Int8Ty, *Int8PtrTy; |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 238 | Module *CurModule; |
| Matt Morehouse | 3bea25e | 2018-09-13 21:45:55 +0000 | [diff] [blame] | 239 | std::string CurModuleUniqueId; |
| Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 240 | Triple TargetTriple; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 241 | LLVMContext *C; |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 242 | const DataLayout *DL; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 243 | |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 244 | GlobalVariable *FunctionGuardArray; // for trace-pc-guard. |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 245 | GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters. |
| Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 246 | GlobalVariable *FunctionPCsArray; // for pc-table. |
| Kostya Serebryany | 4192b96 | 2017-09-09 05:30:13 +0000 | [diff] [blame] | 247 | SmallVector<GlobalValue *, 20> GlobalsToAppendToUsed; |
| Matt Morehouse | 0ea9a90 | 2018-06-15 20:12:58 +0000 | [diff] [blame] | 248 | SmallVector<GlobalValue *, 20> GlobalsToAppendToCompilerUsed; |
| Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 249 | |
| Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 250 | SanitizerCoverageOptions Options; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 253 | class ModuleSanitizerCoverageLegacyPass : public ModulePass { |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 254 | public: |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 255 | 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 Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 273 | static char ID; // Pass identification, replacement for typeid |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 274 | StringRef getPassName() const override { return "ModuleSanitizerCoverage"; } |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 275 | |
| 276 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 277 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 278 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
| 279 | } |
| 280 | |
| 281 | private: |
| 282 | SanitizerCoverageOptions Options; |
| 283 | }; |
| 284 | |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 285 | } // namespace |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 286 | |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 287 | PreservedAnalyses ModuleSanitizerCoveragePass::run(Module &M, |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 288 | ModuleAnalysisManager &MAM) { |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 289 | ModuleSanitizerCoverage ModuleSancov(Options); |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 290 | 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 Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 298 | return PreservedAnalyses::none(); |
| 299 | return PreservedAnalyses::all(); |
| 300 | } |
| 301 | |
| Jonathan Metzman | 5eb8cba | 2018-10-16 23:43:57 +0000 | [diff] [blame] | 302 | std::pair<Value *, Value *> |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 303 | ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section, |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 304 | Type *Ty) { |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 305 | 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 Metzman | 5eb8cba | 2018-10-16 23:43:57 +0000 | [diff] [blame] | 313 | IRBuilder<> IRB(M.getContext()); |
| 314 | Value *SecEndPtr = IRB.CreatePointerCast(SecEnd, Ty); |
| Jonathan Metzman | e159a0d | 2019-01-14 21:02:02 +0000 | [diff] [blame] | 315 | if (!TargetTriple.isOSBinFormatCOFF()) |
| Jonathan Metzman | 5eb8cba | 2018-10-16 23:43:57 +0000 | [diff] [blame] | 316 | return std::make_pair(IRB.CreatePointerCast(SecStart, Ty), SecEndPtr); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 317 | |
| Jonathan Metzman | 5eb8cba | 2018-10-16 23:43:57 +0000 | [diff] [blame] | 318 | // 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 Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 321 | auto GEP = IRB.CreateGEP(Int8Ty, SecStartI8Ptr, |
| Jonathan Metzman | 5eb8cba | 2018-10-16 23:43:57 +0000 | [diff] [blame] | 322 | ConstantInt::get(IntptrTy, sizeof(uint64_t))); |
| 323 | return std::make_pair(IRB.CreatePointerCast(GEP, Ty), SecEndPtr); |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 326 | Function *ModuleSanitizerCoverage::CreateInitCallsForSections( |
| Fangrui Song | a400ca3 | 2019-05-07 01:39:37 +0000 | [diff] [blame] | 327 | Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty, |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 328 | const char *Section) { |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 329 | auto SecStartEnd = CreateSecStartEnd(M, Section, Ty); |
| 330 | auto SecStart = SecStartEnd.first; |
| 331 | auto SecEnd = SecStartEnd.second; |
| 332 | Function *CtorFunc; |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 333 | std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( |
| Fangrui Song | a400ca3 | 2019-05-07 01:39:37 +0000 | [diff] [blame] | 334 | M, CtorName, InitFunctionName, {Ty, Ty}, {SecStart, SecEnd}); |
| 335 | assert(CtorFunc->getName() == CtorName); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 336 | |
| 337 | if (TargetTriple.supportsCOMDAT()) { |
| 338 | // Use comdat to dedup CtorFunc. |
| Fangrui Song | a400ca3 | 2019-05-07 01:39:37 +0000 | [diff] [blame] | 339 | CtorFunc->setComdat(M.getOrInsertComdat(CtorName)); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 340 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc); |
| 341 | } else { |
| 342 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority); |
| 343 | } |
| Jonathan Metzman | 0b94e88 | 2018-10-12 18:11:47 +0000 | [diff] [blame] | 344 | |
| Jonathan Metzman | e159a0d | 2019-01-14 21:02:02 +0000 | [diff] [blame] | 345 | if (TargetTriple.isOSBinFormatCOFF()) { |
| Jonathan Metzman | 0b94e88 | 2018-10-12 18:11:47 +0000 | [diff] [blame] | 346 | // 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 Metzman | e159a0d | 2019-01-14 21:02:02 +0000 | [diff] [blame] | 349 | // 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 Metzman | 0b94e88 | 2018-10-12 18:11:47 +0000 | [diff] [blame] | 352 | CtorFunc->setLinkage(GlobalValue::WeakODRLinkage); |
| Jonathan Metzman | e159a0d | 2019-01-14 21:02:02 +0000 | [diff] [blame] | 353 | appendToUsed(M, CtorFunc); |
| Jonathan Metzman | 0b94e88 | 2018-10-12 18:11:47 +0000 | [diff] [blame] | 354 | } |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 355 | return CtorFunc; |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 358 | bool ModuleSanitizerCoverage::instrumentModule( |
| 359 | Module &M, DomTreeCallback DTCallback, PostDomTreeCallback PDTCallback) { |
| Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 360 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_None) |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 361 | return false; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 362 | C = &(M.getContext()); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 363 | DL = &M.getDataLayout(); |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 364 | CurModule = &M; |
| Matt Morehouse | 3bea25e | 2018-09-13 21:45:55 +0000 | [diff] [blame] | 365 | CurModuleUniqueId = getUniqueModuleId(CurModule); |
| Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 366 | TargetTriple = Triple(M.getTargetTriple()); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 367 | FunctionGuardArray = nullptr; |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 368 | Function8bitCounterArray = nullptr; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 369 | FunctionPCsArray = nullptr; |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 370 | IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits()); |
| Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 371 | IntptrPtrTy = PointerType::getUnqual(IntptrTy); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 372 | Type *VoidTy = Type::getVoidTy(*C); |
| Kostya Serebryany | 4cadd4a | 2014-11-24 18:49:53 +0000 | [diff] [blame] | 373 | IRBuilder<> IRB(*C); |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 374 | Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty()); |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 375 | Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 376 | Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty()); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 377 | Int64Ty = IRB.getInt64Ty(); |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 378 | Int32Ty = IRB.getInt32Ty(); |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 379 | Int16Ty = IRB.getInt16Ty(); |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 380 | Int8Ty = IRB.getInt8Ty(); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 381 | |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 382 | 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 Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 394 | SanCovTraceCmpFunction[0] = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 395 | 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 Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 403 | SanCovTraceCmpFunction[3] = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 404 | M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty); |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 405 | |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 406 | 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 Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 412 | SanCovTraceConstCmpFunction[3] = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 413 | M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty); |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 414 | |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 415 | { |
| 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 Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 422 | SanCovTraceDivFunction[1] = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 423 | M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty); |
| Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 424 | SanCovTraceGepFunction = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 425 | M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy); |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 426 | SanCovTraceSwitchFunction = |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 427 | M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy); |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 428 | |
| 429 | Constant *SanCovLowestStackConstant = |
| 430 | M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy); |
| Julian Lettner | 29ac3a5 | 2019-02-04 22:06:30 +0000 | [diff] [blame] | 431 | SanCovLowestStack = dyn_cast<GlobalVariable>(SanCovLowestStackConstant); |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 432 | if (!SanCovLowestStack) { |
| 433 | C->emitError(StringRef("'") + SanCovLowestStackName + |
| 434 | "' should not be declared by the user"); |
| 435 | return true; |
| 436 | } |
| Matt Morehouse | b1fa825 | 2017-08-22 21:28:29 +0000 | [diff] [blame] | 437 | SanCovLowestStack->setThreadLocalMode( |
| 438 | GlobalValue::ThreadLocalMode::InitialExecTLSModel); |
| 439 | if (Options.StackDepth && !SanCovLowestStack->isDeclaration()) |
| 440 | SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy)); |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 441 | |
| Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 442 | // 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 Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 446 | |
| James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 447 | SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy); |
| 448 | SanCovTracePCGuard = |
| 449 | M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy); |
| Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 450 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 451 | 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 Serebryany | 4192b96 | 2017-09-09 05:30:13 +0000 | [diff] [blame] | 471 | // 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 Morehouse | 0ea9a90 | 2018-06-15 20:12:58 +0000 | [diff] [blame] | 475 | appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed); |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 476 | return true; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| Mike Aizatsky | 9987f43 | 2016-03-23 23:15:03 +0000 | [diff] [blame] | 479 | // True if block has successors and it dominates all of them. |
| 480 | static 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 Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 492 | // True if block has predecessors and it postdominates all of them. |
| 493 | static 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 Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 506 | static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB, |
| 507 | const DominatorTree *DT, |
| George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 508 | const PostDominatorTree *PDT, |
| Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 509 | const SanitizerCoverageOptions &Options) { |
| Reid Kleckner | 701593f | 2019-02-28 22:54:30 +0000 | [diff] [blame] | 510 | // Don't insert coverage for blocks containing nothing but unreachable: we |
| 511 | // will never call __sanitizer_cov() for them, so counting them in |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 512 | // NumberOfInstrumentedBlocks() might complicate calculation of code coverage |
| 513 | // percentage. Also, unreachable instructions frequently have no debug |
| 514 | // locations. |
| Reid Kleckner | 701593f | 2019-02-28 22:54:30 +0000 | [diff] [blame] | 515 | if (isa<UnreachableInst>(BB->getFirstNonPHIOrDbgOrLifetime())) |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 516 | return false; |
| 517 | |
| Reid Kleckner | 392f062 | 2017-03-23 23:30:41 +0000 | [diff] [blame] | 518 | // 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 Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 523 | if (Options.NoPrune || &F.getEntryBlock() == BB) |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 524 | return true; |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 525 | |
| Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 526 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_Function && |
| 527 | &F.getEntryBlock() != BB) |
| 528 | return false; |
| 529 | |
| George Karpenkov | a1c5327 | 2017-05-25 01:41:46 +0000 | [diff] [blame] | 530 | // 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 Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| Kostya Serebryany | a78a44d | 2019-01-31 23:43:00 +0000 | [diff] [blame] | 536 | |
| 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 |
| 541 | static 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. |
| 556 | static 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 Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 567 | void 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 Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 591 | if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) |
| Craig Topper | 03e93f5 | 2019-03-12 18:20:25 +0000 | [diff] [blame] | 592 | SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions().setIgnoreUnreachableDests()); |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 593 | SmallVector<Instruction *, 8> IndirCalls; |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 594 | SmallVector<BasicBlock *, 16> BlocksToInstrument; |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 595 | SmallVector<Instruction *, 8> CmpTraceTargets; |
| 596 | SmallVector<Instruction *, 8> SwitchTraceTargets; |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 597 | SmallVector<BinaryOperator *, 8> DivTraceTargets; |
| 598 | SmallVector<GetElementPtrInst *, 8> GepTraceTargets; |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 599 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 600 | const DominatorTree *DT = DTCallback(F); |
| 601 | const PostDominatorTree *PDT = PDTCallback(F); |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 602 | bool IsLeafFunc = true; |
| Mike Aizatsky | 602f792 | 2016-03-21 23:08:16 +0000 | [diff] [blame] | 603 | |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 604 | for (auto &BB : F) { |
| George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 605 | if (shouldInstrumentBlock(F, &BB, DT, PDT, Options)) |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 606 | BlocksToInstrument.push_back(&BB); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 607 | for (auto &Inst : BB) { |
| Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 608 | if (Options.IndirectCalls) { |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 609 | CallSite CS(&Inst); |
| 610 | if (CS && !CS.getCalledFunction()) |
| 611 | IndirCalls.push_back(&Inst); |
| 612 | } |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 613 | if (Options.TraceCmp) { |
| Kostya Serebryany | a78a44d | 2019-01-31 23:43:00 +0000 | [diff] [blame] | 614 | if (ICmpInst *CMP = dyn_cast<ICmpInst>(&Inst)) |
| 615 | if (IsInterestingCmp(CMP, DT, Options)) |
| 616 | CmpTraceTargets.push_back(&Inst); |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 617 | if (isa<SwitchInst>(&Inst)) |
| 618 | SwitchTraceTargets.push_back(&Inst); |
| 619 | } |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 620 | 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 Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 628 | if (Options.StackDepth) |
| 629 | if (isa<InvokeInst>(Inst) || |
| 630 | (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst))) |
| 631 | IsLeafFunc = false; |
| 632 | } |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 633 | } |
| Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 634 | |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 635 | InjectCoverage(F, BlocksToInstrument, IsLeafFunc); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 636 | InjectCoverageForIndirectCalls(F, IndirCalls); |
| 637 | InjectTraceForCmp(F, CmpTraceTargets); |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 638 | InjectTraceForSwitch(F, SwitchTraceTargets); |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 639 | InjectTraceForDiv(F, DivTraceTargets); |
| 640 | InjectTraceForGep(F, GepTraceTargets); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 641 | } |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 642 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 643 | GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection( |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 644 | 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 Serebryany | bc50455 | 2018-10-12 23:21:48 +0000 | [diff] [blame] | 649 | |
| Matt Morehouse | 19ff35c | 2019-01-15 21:21:01 +0000 | [diff] [blame] | 650 | if (TargetTriple.supportsCOMDAT() && !F.isInterposable()) |
| Reid Kleckner | b41b372 | 2018-11-08 00:57:33 +0000 | [diff] [blame] | 651 | if (auto Comdat = |
| 652 | GetOrCreateFunctionComdat(F, TargetTriple, CurModuleUniqueId)) |
| Kostya Serebryany | bc50455 | 2018-10-12 23:21:48 +0000 | [diff] [blame] | 653 | Array->setComdat(Comdat); |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 654 | Array->setSection(getSectionName(Section)); |
| Kostya Serebryany | bb6f079 | 2017-07-31 19:49:45 +0000 | [diff] [blame] | 655 | Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize() |
| 656 | : Ty->getPrimitiveSizeInBits() / 8); |
| Max Moroz | 4d010ca | 2018-10-12 13:59:31 +0000 | [diff] [blame] | 657 | GlobalsToAppendToUsed.push_back(Array); |
| Matt Morehouse | 3bea25e | 2018-09-13 21:45:55 +0000 | [diff] [blame] | 658 | GlobalsToAppendToCompilerUsed.push_back(Array); |
| 659 | MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F)); |
| 660 | Array->addMetadata(LLVMContext::MD_associated, *MD); |
| 661 | |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 662 | return Array; |
| 663 | } |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 664 | |
| Justin Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 665 | GlobalVariable * |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 666 | ModuleSanitizerCoverage::CreatePCArray(Function &F, |
| 667 | ArrayRef<BasicBlock *> AllBlocks) { |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 668 | size_t N = AllBlocks.size(); |
| 669 | assert(N); |
| Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 670 | SmallVector<Constant *, 32> PCs; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 671 | IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt()); |
| Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 672 | 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 Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 684 | auto *PCArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy, |
| 685 | SanCovPCsSectionName); |
| 686 | PCArray->setInitializer( |
| Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 687 | ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs)); |
| Justin Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 688 | PCArray->setConstant(true); |
| Justin Bogner | ad96ff1 | 2017-08-25 01:24:54 +0000 | [diff] [blame] | 689 | |
| Justin Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 690 | return PCArray; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 693 | void ModuleSanitizerCoverage::CreateFunctionLocalArrays( |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 694 | Function &F, ArrayRef<BasicBlock *> AllBlocks) { |
| Max Moroz | 4d010ca | 2018-10-12 13:59:31 +0000 | [diff] [blame] | 695 | if (Options.TracePCGuard) |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 696 | FunctionGuardArray = CreateFunctionLocalArrayInSection( |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 697 | AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName); |
| Max Moroz | 4d010ca | 2018-10-12 13:59:31 +0000 | [diff] [blame] | 698 | |
| Matt Morehouse | 3bea25e | 2018-09-13 21:45:55 +0000 | [diff] [blame] | 699 | if (Options.Inline8bitCounters) |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 700 | Function8bitCounterArray = CreateFunctionLocalArrayInSection( |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 701 | AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName); |
| Max Moroz | 4d010ca | 2018-10-12 13:59:31 +0000 | [diff] [blame] | 702 | |
| Matt Morehouse | 3bea25e | 2018-09-13 21:45:55 +0000 | [diff] [blame] | 703 | if (Options.PCTable) |
| Justin Bogner | 873a074 | 2017-08-28 23:46:11 +0000 | [diff] [blame] | 704 | FunctionPCsArray = CreatePCArray(F, AllBlocks); |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 705 | } |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 706 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 707 | bool ModuleSanitizerCoverage::InjectCoverage(Function &F, |
| 708 | ArrayRef<BasicBlock *> AllBlocks, |
| 709 | bool IsLeafFunc) { |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 710 | if (AllBlocks.empty()) return false; |
| Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 711 | CreateFunctionLocalArrays(F, AllBlocks); |
| Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 712 | for (size_t i = 0, N = AllBlocks.size(); i < N; i++) |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 713 | InjectCoverageAtBlock(F, *AllBlocks[i], i, IsLeafFunc); |
| Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 714 | return true; |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | // On every indirect call we call a run-time function |
| 718 | // __sanitizer_cov_indir_call* with two parameters: |
| 719 | // - callee address, |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 720 | // - global cache array that contains CacheSize pointers (zero-initialized). |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 721 | // 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 Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 723 | // CacheSize is encoded in the name of the run-time function. |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 724 | void ModuleSanitizerCoverage::InjectCoverageForIndirectCalls( |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 725 | Function &F, ArrayRef<Instruction *> IndirCalls) { |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 726 | if (IndirCalls.empty()) |
| 727 | return; |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 728 | assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 729 | for (auto I : IndirCalls) { |
| 730 | IRBuilder<> IRB(I); |
| 731 | CallSite CS(I); |
| 732 | Value *Callee = CS.getCalledValue(); |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 733 | if (isa<InlineAsm>(Callee)) |
| 734 | continue; |
| Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 735 | IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy)); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 739 | // For every switch statement we insert a call: |
| 740 | // __sanitizer_cov_trace_switch(CondValue, |
| 741 | // {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... }) |
| 742 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 743 | void ModuleSanitizerCoverage::InjectTraceForSwitch( |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 744 | Function &, ArrayRef<Instruction *> SwitchTraceTargets) { |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 745 | 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 Serebryany | 2569118 | 2015-08-11 00:24:39 +0000 | [diff] [blame] | 750 | if (Cond->getType()->getScalarSizeInBits() > |
| 751 | Int64Ty->getScalarSizeInBits()) |
| 752 | continue; |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 753 | 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 Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 759 | for (auto It : SI->cases()) { |
| Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 760 | 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 Grang | 636d94d | 2018-04-13 19:47:57 +0000 | [diff] [blame] | 766 | 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 Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 771 | 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 Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 782 | void ModuleSanitizerCoverage::InjectTraceForDiv( |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 783 | 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 Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 800 | void ModuleSanitizerCoverage::InjectTraceForGep( |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 801 | 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 Serebryany | 45c1447 | 2016-09-27 01:55:08 +0000 | [diff] [blame] | 805 | if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy()) |
| Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 806 | IRB.CreateCall(SanCovTraceGepFunction, |
| 807 | {IRB.CreateIntCast(*I, IntptrTy, true)}); |
| 808 | } |
| 809 | } |
| 810 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 811 | void ModuleSanitizerCoverage::InjectTraceForCmp( |
| Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 812 | Function &, ArrayRef<Instruction *> CmpTraceTargets) { |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 813 | 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 Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 818 | if (!A0->getType()->isIntegerTy()) |
| 819 | continue; |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 820 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType()); |
| Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 821 | int CallbackIdx = TypeSize == 8 ? 0 : |
| 822 | TypeSize == 16 ? 1 : |
| 823 | TypeSize == 32 ? 2 : |
| 824 | TypeSize == 64 ? 3 : -1; |
| 825 | if (CallbackIdx < 0) continue; |
| Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 826 | // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1); |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 827 | 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 Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 835 | if (SecondIsConst) |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 836 | std::swap(A0, A1); |
| 837 | } |
| 838 | |
| Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 839 | auto Ty = Type::getIntNTy(*C, TypeSize); |
| Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame] | 840 | IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true), |
| Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 841 | IRB.CreateIntCast(A1, Ty, true)}); |
| Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 846 | void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB, |
| 847 | size_t Idx, |
| 848 | bool IsLeafFunc) { |
| Justin Bogner | 7ae63aa | 2015-08-14 17:03:45 +0000 | [diff] [blame] | 849 | BasicBlock::iterator IP = BB.getFirstInsertionPt(); |
| Kostya Serebryany | d421db0 | 2015-01-03 00:54:43 +0000 | [diff] [blame] | 850 | bool IsEntryBB = &BB == &F.getEntryBlock(); |
| Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 851 | DebugLoc EntryLoc; |
| 852 | if (IsEntryBB) { |
| Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 853 | if (auto SP = F.getSubprogram()) |
| Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 854 | EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP); |
| Reid Kleckner | a57d015 | 2015-08-14 16:45:42 +0000 | [diff] [blame] | 855 | // 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 Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 859 | } else { |
| 860 | EntryLoc = IP->getDebugLoc(); |
| 861 | } |
| 862 | |
| Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 863 | IRBuilder<> IRB(&*IP); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 864 | IRB.SetCurrentDebugLocation(EntryLoc); |
| Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 865 | if (Options.TracePC) { |
| Kostya Serebryany | dd5c7f9 | 2016-07-14 17:59:01 +0000 | [diff] [blame] | 866 | IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC. |
| 867 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 868 | } |
| 869 | if (Options.TracePCGuard) { |
| Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 870 | auto GuardPtr = IRB.CreateIntToPtr( |
| 871 | IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy), |
| 872 | ConstantInt::get(IntptrTy, Idx * 4)), |
| 873 | Int32PtrTy); |
| Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 874 | IRB.CreateCall(SanCovTracePCGuard, GuardPtr); |
| 875 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
| Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 876 | } |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 877 | if (Options.Inline8bitCounters) { |
| 878 | auto CounterPtr = IRB.CreateGEP( |
| James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 879 | Function8bitCounterArray->getValueType(), Function8bitCounterArray, |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 880 | {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)}); |
| James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 881 | auto Load = IRB.CreateLoad(Int8Ty, CounterPtr); |
| Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 882 | auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1)); |
| 883 | auto Store = IRB.CreateStore(Inc, CounterPtr); |
| 884 | SetNoSanitizeMetadata(Load); |
| 885 | SetNoSanitizeMetadata(Store); |
| 886 | } |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 887 | if (Options.StackDepth && IsEntryBB && !IsLeafFunc) { |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 888 | // Check stack depth. If it's the deepest so far, record it. |
| Christudasan Devadasan | 006cf8c | 2019-07-22 12:42:48 +0000 | [diff] [blame] | 889 | Module *M = F.getParent(); |
| 890 | Function *GetFrameAddr = Intrinsic::getDeclaration( |
| 891 | M, Intrinsic::frameaddress, |
| 892 | IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace())); |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 893 | auto FrameAddrPtr = |
| 894 | IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)}); |
| 895 | auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy); |
| James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 896 | auto LowestStack = IRB.CreateLoad(IntptrTy, SanCovLowestStack); |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 897 | auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack); |
| 898 | auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false); |
| 899 | IRBuilder<> ThenIRB(ThenTerm); |
| Matt Morehouse | 034126e | 2017-08-30 22:49:31 +0000 | [diff] [blame] | 900 | auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack); |
| 901 | SetNoSanitizeMetadata(LowestStack); |
| 902 | SetNoSanitizeMetadata(Store); |
| Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 903 | } |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 906 | std::string |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 907 | ModuleSanitizerCoverage::getSectionName(const std::string &Section) const { |
| Jonathan Metzman | e159a0d | 2019-01-14 21:02:02 +0000 | [diff] [blame] | 908 | if (TargetTriple.isOSBinFormatCOFF()) { |
| Matt Morehouse | 7e042bb | 2018-08-30 15:54:44 +0000 | [diff] [blame] | 909 | if (Section == SanCovCountersSectionName) |
| 910 | return ".SCOV$CM"; |
| 911 | if (Section == SanCovPCsSectionName) |
| 912 | return ".SCOVP$M"; |
| 913 | return ".SCOV$GM"; // For SanCovGuardsSectionName. |
| 914 | } |
| Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 915 | if (TargetTriple.isOSBinFormatMachO()) |
| Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 916 | return "__DATA,__" + Section; |
| 917 | return "__" + Section; |
| Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 920 | std::string |
| 921 | ModuleSanitizerCoverage::getSectionStart(const std::string &Section) const { |
| 922 | if (TargetTriple.isOSBinFormatMachO()) |
| 923 | return "\1section$start$__DATA$__" + Section; |
| 924 | return "__start___" + Section; |
| 925 | } |
| Leonard Chan | bb147aa | 2019-07-15 23:18:31 +0000 | [diff] [blame] | 926 | |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 927 | std::string |
| 928 | ModuleSanitizerCoverage::getSectionEnd(const std::string &Section) const { |
| 929 | if (TargetTriple.isOSBinFormatMachO()) |
| 930 | return "\1section$end$__DATA$__" + Section; |
| 931 | return "__stop___" + Section; |
| 932 | } |
| 933 | |
| 934 | char ModuleSanitizerCoverageLegacyPass::ID = 0; |
| 935 | INITIALIZE_PASS_BEGIN(ModuleSanitizerCoverageLegacyPass, "sancov", |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 936 | "Pass for instrumenting coverage on functions", false, |
| 937 | false) |
| Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 938 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 939 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
| Leonard Chan | eca01b0 | 2019-09-04 20:30:29 +0000 | [diff] [blame] | 940 | INITIALIZE_PASS_END(ModuleSanitizerCoverageLegacyPass, "sancov", |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 941 | "Pass for instrumenting coverage on functions", false, |
| 942 | false) |
| Leonard Chan | 007f674 | 2019-07-25 20:53:15 +0000 | [diff] [blame] | 943 | ModulePass *llvm::createModuleSanitizerCoverageLegacyPassPass( |
| 944 | const SanitizerCoverageOptions &Options) { |
| 945 | return new ModuleSanitizerCoverageLegacyPass(Options); |
| Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 946 | } |