Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 1 | //===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 10 | // Coverage instrumentation done on LLVM IR level, works with Sanitizers. |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
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 | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Intrinsics.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LLVMContext.h" |
| 30 | #include "llvm/IR/MDBuilder.h" |
| 31 | #include "llvm/IR/Module.h" |
| 32 | #include "llvm/IR/Type.h" |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
| 38 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 39 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | #define DEBUG_TYPE "sancov" |
| 44 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 45 | static const char *const SanCovTracePCIndirName = |
| 46 | "__sanitizer_cov_trace_pc_indir"; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 47 | static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc"; |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 48 | static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1"; |
| 49 | static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2"; |
| 50 | static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4"; |
| 51 | static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8"; |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 52 | static const char *const SanCovTraceConstCmp1 = |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 53 | "__sanitizer_cov_trace_const_cmp1"; |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 54 | static const char *const SanCovTraceConstCmp2 = |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 55 | "__sanitizer_cov_trace_const_cmp2"; |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 56 | static const char *const SanCovTraceConstCmp4 = |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 57 | "__sanitizer_cov_trace_const_cmp4"; |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 58 | static const char *const SanCovTraceConstCmp8 = |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 59 | "__sanitizer_cov_trace_const_cmp8"; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 60 | static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4"; |
| 61 | static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8"; |
| 62 | static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep"; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 63 | static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch"; |
| 64 | static const char *const SanCovModuleCtorName = "sancov.module_ctor"; |
| 65 | static const uint64_t SanCtorAndDtorPriority = 2; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 66 | |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 67 | static const char *const SanCovTracePCGuardName = |
| 68 | "__sanitizer_cov_trace_pc_guard"; |
| 69 | static const char *const SanCovTracePCGuardInitName = |
| 70 | "__sanitizer_cov_trace_pc_guard_init"; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 71 | static const char *const SanCov8bitCountersInitName = |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 72 | "__sanitizer_cov_8bit_counters_init"; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 73 | static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init"; |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 74 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 75 | static const char *const SanCovGuardsSectionName = "sancov_guards"; |
George Karpenkov | 406c113 | 2017-06-14 23:40:25 +0000 | [diff] [blame] | 76 | static const char *const SanCovCountersSectionName = "sancov_cntrs"; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 77 | static const char *const SanCovPCsSectionName = "sancov_pcs"; |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 78 | |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 79 | static const char *const SanCovLowestStackName = "__sancov_lowest_stack"; |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 80 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 81 | static cl::opt<int> ClCoverageLevel( |
| 82 | "sanitizer-coverage-level", |
| 83 | cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, " |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 84 | "3: all blocks and critical edges"), |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 85 | cl::Hidden, cl::init(0)); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 86 | |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 87 | static cl::opt<bool> ClTracePC("sanitizer-coverage-trace-pc", |
| 88 | cl::desc("Experimental pc tracing"), cl::Hidden, |
| 89 | cl::init(false)); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 90 | |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 91 | static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard", |
| 92 | cl::desc("pc tracing with a guard"), |
| 93 | cl::Hidden, cl::init(false)); |
| 94 | |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 95 | // If true, we create a global variable that contains PCs of all instrumented |
| 96 | // BBs, put this global into a named section, and pass this section's bounds |
| 97 | // to __sanitizer_cov_pcs_init. |
| 98 | // This way the coverage instrumentation does not need to acquire the PCs |
| 99 | // at run-time. Works with trace-pc-guard and inline-8bit-counters. |
Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 100 | static cl::opt<bool> ClCreatePCTable("sanitizer-coverage-pc-table", |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 101 | cl::desc("create a static PC table"), |
| 102 | cl::Hidden, cl::init(false)); |
| 103 | |
| 104 | static cl::opt<bool> |
| 105 | ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters", |
| 106 | cl::desc("increments 8-bit counter for every edge"), |
| 107 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 108 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 109 | static cl::opt<bool> |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 110 | ClCMPTracing("sanitizer-coverage-trace-compares", |
| 111 | cl::desc("Tracing of CMP and similar instructions"), |
| 112 | cl::Hidden, cl::init(false)); |
| 113 | |
| 114 | static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs", |
| 115 | cl::desc("Tracing of DIV instructions"), |
| 116 | cl::Hidden, cl::init(false)); |
| 117 | |
| 118 | static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps", |
| 119 | cl::desc("Tracing of GEP instructions"), |
| 120 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 121 | |
Mike Aizatsky | 70ea453 | 2016-04-06 23:24:37 +0000 | [diff] [blame] | 122 | static cl::opt<bool> |
| 123 | ClPruneBlocks("sanitizer-coverage-prune-blocks", |
| 124 | cl::desc("Reduce the number of instrumented blocks"), |
| 125 | cl::Hidden, cl::init(true)); |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 126 | |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 127 | static cl::opt<bool> ClStackDepth("sanitizer-coverage-stack-depth", |
| 128 | cl::desc("max stack depth tracing"), |
| 129 | cl::Hidden, cl::init(false)); |
| 130 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 131 | namespace { |
| 132 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 133 | SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) { |
| 134 | SanitizerCoverageOptions Res; |
| 135 | switch (LegacyCoverageLevel) { |
| 136 | case 0: |
| 137 | Res.CoverageType = SanitizerCoverageOptions::SCK_None; |
| 138 | break; |
| 139 | case 1: |
| 140 | Res.CoverageType = SanitizerCoverageOptions::SCK_Function; |
| 141 | break; |
| 142 | case 2: |
| 143 | Res.CoverageType = SanitizerCoverageOptions::SCK_BB; |
| 144 | break; |
| 145 | case 3: |
| 146 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 147 | break; |
| 148 | case 4: |
| 149 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 150 | Res.IndirectCalls = true; |
| 151 | break; |
| 152 | } |
| 153 | return Res; |
| 154 | } |
| 155 | |
| 156 | SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) { |
| 157 | // Sets CoverageType and IndirectCalls. |
| 158 | SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel); |
| 159 | Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType); |
| 160 | Options.IndirectCalls |= CLOpts.IndirectCalls; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 161 | Options.TraceCmp |= ClCMPTracing; |
| 162 | Options.TraceDiv |= ClDIVTracing; |
| 163 | Options.TraceGep |= ClGEPTracing; |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 164 | Options.TracePC |= ClTracePC; |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 165 | Options.TracePCGuard |= ClTracePCGuard; |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 166 | Options.Inline8bitCounters |= ClInline8bitCounters; |
Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 167 | Options.PCTable |= ClCreatePCTable; |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 168 | Options.NoPrune |= !ClPruneBlocks; |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 169 | Options.StackDepth |= ClStackDepth; |
| 170 | if (!Options.TracePCGuard && !Options.TracePC && |
| 171 | !Options.Inline8bitCounters && !Options.StackDepth) |
| 172 | Options.TracePCGuard = true; // TracePCGuard is default. |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 173 | return Options; |
| 174 | } |
| 175 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 176 | class SanitizerCoverageModule : public ModulePass { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 177 | public: |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 178 | SanitizerCoverageModule( |
| 179 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()) |
Chandler Carruth | e2b7021 | 2016-03-18 22:35:58 +0000 | [diff] [blame] | 180 | : ModulePass(ID), Options(OverrideFromCL(Options)) { |
| 181 | initializeSanitizerCoverageModulePass(*PassRegistry::getPassRegistry()); |
| 182 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 183 | bool runOnModule(Module &M) override; |
| 184 | bool runOnFunction(Function &F); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 185 | static char ID; // Pass identification, replacement for typeid |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 186 | StringRef getPassName() const override { return "SanitizerCoverageModule"; } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 187 | |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 188 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 189 | AU.addRequired<DominatorTreeWrapperPass>(); |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 190 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private: |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 194 | void InjectCoverageForIndirectCalls(Function &F, |
| 195 | ArrayRef<Instruction *> IndirCalls); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 196 | void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 197 | void InjectTraceForDiv(Function &F, |
| 198 | ArrayRef<BinaryOperator *> DivTraceTargets); |
| 199 | void InjectTraceForGep(Function &F, |
| 200 | ArrayRef<GetElementPtrInst *> GepTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 201 | void InjectTraceForSwitch(Function &F, |
| 202 | ArrayRef<Instruction *> SwitchTraceTargets); |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 203 | bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 204 | GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements, |
| 205 | Function &F, Type *Ty, |
| 206 | const char *Section); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 207 | void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
| 208 | void CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 209 | void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 210 | Function *CreateInitCallsForSections(Module &M, const char *InitFunctionName, |
| 211 | Type *Ty, const char *Section); |
| 212 | std::pair<GlobalVariable *, GlobalVariable *> |
| 213 | CreateSecStartEnd(Module &M, const char *Section, Type *Ty); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 214 | |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 215 | void SetNoSanitizeMetadata(Instruction *I) { |
| 216 | I->setMetadata(I->getModule()->getMDKindID("nosanitize"), |
| 217 | MDNode::get(*C, None)); |
| 218 | } |
| 219 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 220 | std::string getSectionName(const std::string &Section) const; |
| 221 | std::string getSectionStart(const std::string &Section) const; |
| 222 | std::string getSectionEnd(const std::string &Section) const; |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 223 | Function *SanCovTracePCIndir; |
Kostya Serebryany | be87d48 | 2017-04-19 21:48:09 +0000 | [diff] [blame] | 224 | Function *SanCovTracePC, *SanCovTracePCGuard; |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 225 | Function *SanCovTraceCmpFunction[4]; |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 226 | Function *SanCovTraceConstCmpFunction[4]; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 227 | Function *SanCovTraceDivFunction[2]; |
| 228 | Function *SanCovTraceGepFunction; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 229 | Function *SanCovTraceSwitchFunction; |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 230 | GlobalVariable *SanCovLowestStack; |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 231 | InlineAsm *EmptyAsm; |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 232 | Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy, |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 233 | *Int16Ty, *Int8Ty, *Int8PtrTy; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 234 | Module *CurModule; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 235 | Triple TargetTriple; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 236 | LLVMContext *C; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 237 | const DataLayout *DL; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 238 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 239 | GlobalVariable *FunctionGuardArray; // for trace-pc-guard. |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 240 | GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters. |
Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 241 | GlobalVariable *FunctionPCsArray; // for pc-table. |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 242 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 243 | SanitizerCoverageOptions Options; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 246 | } // namespace |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 247 | |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 248 | std::pair<GlobalVariable *, GlobalVariable *> |
| 249 | SanitizerCoverageModule::CreateSecStartEnd(Module &M, const char *Section, |
| 250 | Type *Ty) { |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 251 | GlobalVariable *SecStart = |
| 252 | new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr, |
| 253 | getSectionStart(Section)); |
| 254 | SecStart->setVisibility(GlobalValue::HiddenVisibility); |
| 255 | GlobalVariable *SecEnd = |
| 256 | new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, |
| 257 | nullptr, getSectionEnd(Section)); |
| 258 | SecEnd->setVisibility(GlobalValue::HiddenVisibility); |
| 259 | |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 260 | return std::make_pair(SecStart, SecEnd); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | Function *SanitizerCoverageModule::CreateInitCallsForSections( |
| 265 | Module &M, const char *InitFunctionName, Type *Ty, |
| 266 | const char *Section) { |
| 267 | IRBuilder<> IRB(M.getContext()); |
| 268 | auto SecStartEnd = CreateSecStartEnd(M, Section, Ty); |
| 269 | auto SecStart = SecStartEnd.first; |
| 270 | auto SecEnd = SecStartEnd.second; |
| 271 | Function *CtorFunc; |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 272 | std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( |
| 273 | M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty}, |
| 274 | {IRB.CreatePointerCast(SecStart, Ty), IRB.CreatePointerCast(SecEnd, Ty)}); |
| 275 | |
| 276 | if (TargetTriple.supportsCOMDAT()) { |
| 277 | // Use comdat to dedup CtorFunc. |
| 278 | CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName)); |
| 279 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc); |
| 280 | } else { |
| 281 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority); |
| 282 | } |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 283 | return CtorFunc; |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 286 | bool SanitizerCoverageModule::runOnModule(Module &M) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 287 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_None) |
| 288 | return false; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 289 | C = &(M.getContext()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 290 | DL = &M.getDataLayout(); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 291 | CurModule = &M; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 292 | TargetTriple = Triple(M.getTargetTriple()); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 293 | FunctionGuardArray = nullptr; |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 294 | Function8bitCounterArray = nullptr; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 295 | FunctionPCsArray = nullptr; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 296 | IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits()); |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 297 | IntptrPtrTy = PointerType::getUnqual(IntptrTy); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 298 | Type *VoidTy = Type::getVoidTy(*C); |
Kostya Serebryany | 4cadd4a | 2014-11-24 18:49:53 +0000 | [diff] [blame] | 299 | IRBuilder<> IRB(*C); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 300 | Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty()); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 301 | Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 302 | Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 303 | Int64Ty = IRB.getInt64Ty(); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 304 | Int32Ty = IRB.getInt32Ty(); |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 305 | Int16Ty = IRB.getInt16Ty(); |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 306 | Int8Ty = IRB.getInt8Ty(); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 307 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 308 | SanCovTracePCIndir = checkSanitizerInterfaceFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 309 | M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy)); |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 310 | SanCovTraceCmpFunction[0] = |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 311 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 312 | SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 313 | SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction( |
| 314 | M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 315 | IRB.getInt16Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 316 | SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction( |
| 317 | M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 318 | IRB.getInt32Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 319 | SanCovTraceCmpFunction[3] = |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 320 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 321 | SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty)); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 322 | |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 323 | SanCovTraceConstCmpFunction[0] = |
| 324 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 325 | SanCovTraceConstCmp1, VoidTy, Int8Ty, Int8Ty)); |
| 326 | SanCovTraceConstCmpFunction[1] = |
| 327 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 328 | SanCovTraceConstCmp2, VoidTy, Int16Ty, Int16Ty)); |
| 329 | SanCovTraceConstCmpFunction[2] = |
| 330 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 331 | SanCovTraceConstCmp4, VoidTy, Int32Ty, Int32Ty)); |
| 332 | SanCovTraceConstCmpFunction[3] = |
| 333 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 334 | SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty)); |
| 335 | |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 336 | SanCovTraceDivFunction[0] = |
| 337 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 338 | SanCovTraceDiv4, VoidTy, IRB.getInt32Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 339 | SanCovTraceDivFunction[1] = |
| 340 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 341 | SanCovTraceDiv8, VoidTy, Int64Ty)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 342 | SanCovTraceGepFunction = |
| 343 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 344 | SanCovTraceGep, VoidTy, IntptrTy)); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 345 | SanCovTraceSwitchFunction = |
| 346 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 347 | SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy)); |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 348 | |
| 349 | Constant *SanCovLowestStackConstant = |
| 350 | M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy); |
Matt Morehouse | b1fa825 | 2017-08-22 21:28:29 +0000 | [diff] [blame] | 351 | SanCovLowestStack = cast<GlobalVariable>(SanCovLowestStackConstant); |
| 352 | SanCovLowestStack->setThreadLocalMode( |
| 353 | GlobalValue::ThreadLocalMode::InitialExecTLSModel); |
| 354 | if (Options.StackDepth && !SanCovLowestStack->isDeclaration()) |
| 355 | SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy)); |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 356 | |
Alexander Potapenko | 9385aaa | 2017-07-18 11:47:56 +0000 | [diff] [blame] | 357 | // Make sure smaller parameters are zero-extended to i64 as required by the |
| 358 | // x86_64 ABI. |
| 359 | if (TargetTriple.getArch() == Triple::x86_64) { |
| 360 | for (int i = 0; i < 3; i++) { |
| 361 | SanCovTraceCmpFunction[i]->addParamAttr(0, Attribute::ZExt); |
| 362 | SanCovTraceCmpFunction[i]->addParamAttr(1, Attribute::ZExt); |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 363 | SanCovTraceConstCmpFunction[i]->addParamAttr(0, Attribute::ZExt); |
| 364 | SanCovTraceConstCmpFunction[i]->addParamAttr(1, Attribute::ZExt); |
Alexander Potapenko | 9385aaa | 2017-07-18 11:47:56 +0000 | [diff] [blame] | 365 | } |
| 366 | SanCovTraceDivFunction[0]->addParamAttr(0, Attribute::ZExt); |
| 367 | } |
| 368 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 369 | |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 370 | // We insert an empty inline asm after cov callbacks to avoid callback merge. |
| 371 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 372 | StringRef(""), StringRef(""), |
| 373 | /*hasSideEffects=*/true); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 374 | |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 375 | SanCovTracePC = checkSanitizerInterfaceFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 376 | M.getOrInsertFunction(SanCovTracePCName, VoidTy)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 377 | SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 378 | SanCovTracePCGuardName, VoidTy, Int32PtrTy)); |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 379 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 380 | for (auto &F : M) |
| 381 | runOnFunction(F); |
| 382 | |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 383 | Function *Ctor = nullptr; |
Justin Bogner | 41e632b | 2017-02-01 02:38:39 +0000 | [diff] [blame] | 384 | |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 385 | if (FunctionGuardArray) |
| 386 | Ctor = CreateInitCallsForSections(M, SanCovTracePCGuardInitName, Int32PtrTy, |
| 387 | SanCovGuardsSectionName); |
| 388 | if (Function8bitCounterArray) |
| 389 | Ctor = CreateInitCallsForSections(M, SanCov8bitCountersInitName, Int8PtrTy, |
| 390 | SanCovCountersSectionName); |
Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 391 | if (Ctor && Options.PCTable) { |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 392 | auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrPtrTy); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 393 | Function *InitFunction = declareSanitizerInitFunction( |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 394 | M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy}); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 395 | IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator()); |
| 396 | IRBCtor.CreateCall(InitFunction, |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 397 | {IRB.CreatePointerCast(SecStartEnd.first, IntptrPtrTy), |
| 398 | IRB.CreatePointerCast(SecStartEnd.second, IntptrPtrTy)}); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 399 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
Mike Aizatsky | 9987f43 | 2016-03-23 23:15:03 +0000 | [diff] [blame] | 403 | // True if block has successors and it dominates all of them. |
| 404 | static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) { |
| 405 | if (succ_begin(BB) == succ_end(BB)) |
| 406 | return false; |
| 407 | |
| 408 | for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) { |
| 409 | if (!DT->dominates(BB, SUCC)) |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 416 | // True if block has predecessors and it postdominates all of them. |
| 417 | static bool isFullPostDominator(const BasicBlock *BB, |
| 418 | const PostDominatorTree *PDT) { |
| 419 | if (pred_begin(BB) == pred_end(BB)) |
| 420 | return false; |
| 421 | |
| 422 | for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) { |
| 423 | if (!PDT->dominates(BB, PRED)) |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | return true; |
| 428 | } |
| 429 | |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 430 | static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB, |
| 431 | const DominatorTree *DT, |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 432 | const PostDominatorTree *PDT, |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 433 | const SanitizerCoverageOptions &Options) { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 434 | // Don't insert coverage for unreachable blocks: we will never call |
| 435 | // __sanitizer_cov() for them, so counting them in |
| 436 | // NumberOfInstrumentedBlocks() might complicate calculation of code coverage |
| 437 | // percentage. Also, unreachable instructions frequently have no debug |
| 438 | // locations. |
| 439 | if (isa<UnreachableInst>(BB->getTerminator())) |
| 440 | return false; |
| 441 | |
Reid Kleckner | 392f062 | 2017-03-23 23:30:41 +0000 | [diff] [blame] | 442 | // Don't insert coverage into blocks without a valid insertion point |
| 443 | // (catchswitch blocks). |
| 444 | if (BB->getFirstInsertionPt() == BB->end()) |
| 445 | return false; |
| 446 | |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 447 | if (Options.NoPrune || &F.getEntryBlock() == BB) |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 448 | return true; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 449 | |
Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 450 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_Function && |
| 451 | &F.getEntryBlock() != BB) |
| 452 | return false; |
| 453 | |
George Karpenkov | a1c5327 | 2017-05-25 01:41:46 +0000 | [diff] [blame] | 454 | // Do not instrument full dominators, or full post-dominators with multiple |
| 455 | // predecessors. |
| 456 | return !isFullDominator(BB, DT) |
| 457 | && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor()); |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 460 | bool SanitizerCoverageModule::runOnFunction(Function &F) { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 461 | if (F.empty()) |
| 462 | return false; |
Kostya Serebryany | fea4fb4 | 2014-12-17 21:50:04 +0000 | [diff] [blame] | 463 | if (F.getName().find(".module_ctor") != std::string::npos) |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 464 | return false; // Should not instrument sanitizer init functions. |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 465 | if (F.getName().startswith("__sanitizer_")) |
| 466 | return false; // Don't instrument __sanitizer_* callbacks. |
Kostya Serebryany | bfc83fa | 2017-07-31 20:00:22 +0000 | [diff] [blame] | 467 | // Don't touch available_externally functions, their actual body is elewhere. |
| 468 | if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) |
| 469 | return false; |
Reid Kleckner | ec80354 | 2016-11-11 19:18:45 +0000 | [diff] [blame] | 470 | // Don't instrument MSVC CRT configuration helpers. They may run before normal |
| 471 | // initialization. |
| 472 | if (F.getName() == "__local_stdio_printf_options" || |
| 473 | F.getName() == "__local_stdio_scanf_options") |
| 474 | return false; |
Reid Kleckner | df52337 | 2015-09-03 20:18:29 +0000 | [diff] [blame] | 475 | // Don't instrument functions using SEH for now. Splitting basic blocks like |
| 476 | // we do for coverage breaks WinEHPrepare. |
| 477 | // FIXME: Remove this when SEH no longer uses landingpad pattern matching. |
| 478 | if (F.hasPersonalityFn() && |
| 479 | isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) |
| 480 | return false; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 481 | if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 482 | SplitAllCriticalEdges(F); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 483 | SmallVector<Instruction *, 8> IndirCalls; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 484 | SmallVector<BasicBlock *, 16> BlocksToInstrument; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 485 | SmallVector<Instruction *, 8> CmpTraceTargets; |
| 486 | SmallVector<Instruction *, 8> SwitchTraceTargets; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 487 | SmallVector<BinaryOperator *, 8> DivTraceTargets; |
| 488 | SmallVector<GetElementPtrInst *, 8> GepTraceTargets; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 489 | |
Mike Aizatsky | 602f792 | 2016-03-21 23:08:16 +0000 | [diff] [blame] | 490 | const DominatorTree *DT = |
| 491 | &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 492 | const PostDominatorTree *PDT = |
| 493 | &getAnalysis<PostDominatorTreeWrapperPass>(F).getPostDomTree(); |
Mike Aizatsky | 602f792 | 2016-03-21 23:08:16 +0000 | [diff] [blame] | 494 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 495 | for (auto &BB : F) { |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 496 | if (shouldInstrumentBlock(F, &BB, DT, PDT, Options)) |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 497 | BlocksToInstrument.push_back(&BB); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 498 | for (auto &Inst : BB) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 499 | if (Options.IndirectCalls) { |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 500 | CallSite CS(&Inst); |
| 501 | if (CS && !CS.getCalledFunction()) |
| 502 | IndirCalls.push_back(&Inst); |
| 503 | } |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 504 | if (Options.TraceCmp) { |
| 505 | if (isa<ICmpInst>(&Inst)) |
| 506 | CmpTraceTargets.push_back(&Inst); |
| 507 | if (isa<SwitchInst>(&Inst)) |
| 508 | SwitchTraceTargets.push_back(&Inst); |
| 509 | } |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 510 | if (Options.TraceDiv) |
| 511 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst)) |
| 512 | if (BO->getOpcode() == Instruction::SDiv || |
| 513 | BO->getOpcode() == Instruction::UDiv) |
| 514 | DivTraceTargets.push_back(BO); |
| 515 | if (Options.TraceGep) |
| 516 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst)) |
| 517 | GepTraceTargets.push_back(GEP); |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 518 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 519 | } |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 520 | |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 521 | InjectCoverage(F, BlocksToInstrument); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 522 | InjectCoverageForIndirectCalls(F, IndirCalls); |
| 523 | InjectTraceForCmp(F, CmpTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 524 | InjectTraceForSwitch(F, SwitchTraceTargets); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 525 | InjectTraceForDiv(F, DivTraceTargets); |
| 526 | InjectTraceForGep(F, GepTraceTargets); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 527 | return true; |
| 528 | } |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 529 | |
| 530 | GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection( |
| 531 | size_t NumElements, Function &F, Type *Ty, const char *Section) { |
| 532 | ArrayType *ArrayTy = ArrayType::get(Ty, NumElements); |
| 533 | auto Array = new GlobalVariable( |
| 534 | *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage, |
| 535 | Constant::getNullValue(ArrayTy), "__sancov_gen_"); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 536 | if (auto Comdat = F.getComdat()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 537 | Array->setComdat(Comdat); |
| 538 | Array->setSection(getSectionName(Section)); |
Kostya Serebryany | bb6f079 | 2017-07-31 19:49:45 +0000 | [diff] [blame] | 539 | Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize() |
| 540 | : Ty->getPrimitiveSizeInBits() / 8); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 541 | return Array; |
| 542 | } |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 543 | |
| 544 | void SanitizerCoverageModule::CreatePCArray(Function &F, |
| 545 | ArrayRef<BasicBlock *> AllBlocks) { |
| 546 | size_t N = AllBlocks.size(); |
| 547 | assert(N); |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 548 | SmallVector<Constant *, 32> PCs; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 549 | IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt()); |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 550 | for (size_t i = 0; i < N; i++) { |
| 551 | if (&F.getEntryBlock() == AllBlocks[i]) { |
| 552 | PCs.push_back((Constant *)IRB.CreatePointerCast(&F, IntptrPtrTy)); |
| 553 | PCs.push_back((Constant *)IRB.CreateIntToPtr( |
| 554 | ConstantInt::get(IntptrTy, 1), IntptrPtrTy)); |
| 555 | } else { |
| 556 | PCs.push_back((Constant *)IRB.CreatePointerCast( |
| 557 | BlockAddress::get(AllBlocks[i]), IntptrPtrTy)); |
| 558 | PCs.push_back((Constant *)IRB.CreateIntToPtr( |
| 559 | ConstantInt::get(IntptrTy, 0), IntptrPtrTy)); |
| 560 | } |
| 561 | } |
| 562 | FunctionPCsArray = CreateFunctionLocalArrayInSection(N * 2, F, IntptrPtrTy, |
| 563 | SanCovPCsSectionName); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 564 | FunctionPCsArray->setInitializer( |
Kostya Serebryany | d3e4b7e | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 565 | ConstantArray::get(ArrayType::get(IntptrPtrTy, N * 2), PCs)); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 566 | FunctionPCsArray->setConstant(true); |
Justin Bogner | ad96ff1 | 2017-08-25 01:24:54 +0000 | [diff] [blame] | 567 | |
| 568 | // We don't reference the PCs array in any of our runtime functions, so we |
| 569 | // need to prevent it from being dead stripped. |
| 570 | appendToUsed(*F.getParent(), {FunctionPCsArray}); |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void SanitizerCoverageModule::CreateFunctionLocalArrays( |
| 574 | Function &F, ArrayRef<BasicBlock *> AllBlocks) { |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 575 | if (Options.TracePCGuard) |
| 576 | FunctionGuardArray = CreateFunctionLocalArrayInSection( |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 577 | AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName); |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 578 | if (Options.Inline8bitCounters) |
| 579 | Function8bitCounterArray = CreateFunctionLocalArrayInSection( |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 580 | AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName); |
Kostya Serebryany | 063b652 | 2017-07-28 00:09:29 +0000 | [diff] [blame] | 581 | if (Options.PCTable) |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 582 | CreatePCArray(F, AllBlocks); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 583 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 584 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 585 | bool SanitizerCoverageModule::InjectCoverage(Function &F, |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 586 | ArrayRef<BasicBlock *> AllBlocks) { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 587 | if (AllBlocks.empty()) return false; |
Kostya Serebryany | b75d002 | 2017-07-27 23:36:49 +0000 | [diff] [blame] | 588 | CreateFunctionLocalArrays(F, AllBlocks); |
Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 589 | for (size_t i = 0, N = AllBlocks.size(); i < N; i++) |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 590 | InjectCoverageAtBlock(F, *AllBlocks[i], i); |
Kostya Serebryany | c485ca0 | 2017-07-25 02:07:38 +0000 | [diff] [blame] | 591 | return true; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // On every indirect call we call a run-time function |
| 595 | // __sanitizer_cov_indir_call* with two parameters: |
| 596 | // - callee address, |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 597 | // - global cache array that contains CacheSize pointers (zero-initialized). |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 598 | // The cache is used to speed up recording the caller-callee pairs. |
| 599 | // The address of the caller is passed implicitly via caller PC. |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 600 | // CacheSize is encoded in the name of the run-time function. |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 601 | void SanitizerCoverageModule::InjectCoverageForIndirectCalls( |
| 602 | Function &F, ArrayRef<Instruction *> IndirCalls) { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 603 | if (IndirCalls.empty()) |
| 604 | return; |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 605 | assert(Options.TracePC || Options.TracePCGuard || Options.Inline8bitCounters); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 606 | for (auto I : IndirCalls) { |
| 607 | IRBuilder<> IRB(I); |
| 608 | CallSite CS(I); |
| 609 | Value *Callee = CS.getCalledValue(); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 610 | if (isa<InlineAsm>(Callee)) |
| 611 | continue; |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 612 | IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy)); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 616 | // For every switch statement we insert a call: |
| 617 | // __sanitizer_cov_trace_switch(CondValue, |
| 618 | // {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... }) |
| 619 | |
| 620 | void SanitizerCoverageModule::InjectTraceForSwitch( |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 621 | Function &, ArrayRef<Instruction *> SwitchTraceTargets) { |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 622 | for (auto I : SwitchTraceTargets) { |
| 623 | if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { |
| 624 | IRBuilder<> IRB(I); |
| 625 | SmallVector<Constant *, 16> Initializers; |
| 626 | Value *Cond = SI->getCondition(); |
Kostya Serebryany | 2569118 | 2015-08-11 00:24:39 +0000 | [diff] [blame] | 627 | if (Cond->getType()->getScalarSizeInBits() > |
| 628 | Int64Ty->getScalarSizeInBits()) |
| 629 | continue; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 630 | Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases())); |
| 631 | Initializers.push_back( |
| 632 | ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits())); |
| 633 | if (Cond->getType()->getScalarSizeInBits() < |
| 634 | Int64Ty->getScalarSizeInBits()) |
| 635 | Cond = IRB.CreateIntCast(Cond, Int64Ty, false); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 636 | for (auto It : SI->cases()) { |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 637 | Constant *C = It.getCaseValue(); |
| 638 | if (C->getType()->getScalarSizeInBits() < |
| 639 | Int64Ty->getScalarSizeInBits()) |
| 640 | C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty); |
| 641 | Initializers.push_back(C); |
| 642 | } |
Kostya Serebryany | f24e52c | 2016-12-27 21:20:06 +0000 | [diff] [blame] | 643 | std::sort(Initializers.begin() + 2, Initializers.end(), |
| 644 | [](const Constant *A, const Constant *B) { |
| 645 | return cast<ConstantInt>(A)->getLimitedValue() < |
| 646 | cast<ConstantInt>(B)->getLimitedValue(); |
| 647 | }); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 648 | ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size()); |
| 649 | GlobalVariable *GV = new GlobalVariable( |
| 650 | *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage, |
| 651 | ConstantArray::get(ArrayOfInt64Ty, Initializers), |
| 652 | "__sancov_gen_cov_switch_values"); |
| 653 | IRB.CreateCall(SanCovTraceSwitchFunction, |
| 654 | {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)}); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 659 | void SanitizerCoverageModule::InjectTraceForDiv( |
| 660 | Function &, ArrayRef<BinaryOperator *> DivTraceTargets) { |
| 661 | for (auto BO : DivTraceTargets) { |
| 662 | IRBuilder<> IRB(BO); |
| 663 | Value *A1 = BO->getOperand(1); |
| 664 | if (isa<ConstantInt>(A1)) continue; |
| 665 | if (!A1->getType()->isIntegerTy()) |
| 666 | continue; |
| 667 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType()); |
| 668 | int CallbackIdx = TypeSize == 32 ? 0 : |
| 669 | TypeSize == 64 ? 1 : -1; |
| 670 | if (CallbackIdx < 0) continue; |
| 671 | auto Ty = Type::getIntNTy(*C, TypeSize); |
| 672 | IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx], |
| 673 | {IRB.CreateIntCast(A1, Ty, true)}); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void SanitizerCoverageModule::InjectTraceForGep( |
| 678 | Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) { |
| 679 | for (auto GEP : GepTraceTargets) { |
| 680 | IRBuilder<> IRB(GEP); |
| 681 | for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) |
Kostya Serebryany | 45c1447 | 2016-09-27 01:55:08 +0000 | [diff] [blame] | 682 | if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy()) |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 683 | IRB.CreateCall(SanCovTraceGepFunction, |
| 684 | {IRB.CreateIntCast(*I, IntptrTy, true)}); |
| 685 | } |
| 686 | } |
| 687 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 688 | void SanitizerCoverageModule::InjectTraceForCmp( |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 689 | Function &, ArrayRef<Instruction *> CmpTraceTargets) { |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 690 | for (auto I : CmpTraceTargets) { |
| 691 | if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) { |
| 692 | IRBuilder<> IRB(ICMP); |
| 693 | Value *A0 = ICMP->getOperand(0); |
| 694 | Value *A1 = ICMP->getOperand(1); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 695 | if (!A0->getType()->isIntegerTy()) |
| 696 | continue; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 697 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType()); |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 698 | int CallbackIdx = TypeSize == 8 ? 0 : |
| 699 | TypeSize == 16 ? 1 : |
| 700 | TypeSize == 32 ? 2 : |
| 701 | TypeSize == 64 ? 3 : -1; |
| 702 | if (CallbackIdx < 0) continue; |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 703 | // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1); |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 704 | auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx]; |
| 705 | bool FirstIsConst = isa<ConstantInt>(A0); |
| 706 | bool SecondIsConst = isa<ConstantInt>(A1); |
| 707 | // If both are const, then we don't need such a comparison. |
| 708 | if (FirstIsConst && SecondIsConst) continue; |
| 709 | // If only one is const, then make it the first callback argument. |
| 710 | if (FirstIsConst || SecondIsConst) { |
| 711 | CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx]; |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 712 | if (SecondIsConst) |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 713 | std::swap(A0, A1); |
| 714 | } |
| 715 | |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 716 | auto Ty = Type::getIntNTy(*C, TypeSize); |
Justin Bogner | be757de | 2017-08-28 23:38:12 +0000 | [diff] [blame^] | 717 | IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true), |
Alexander Potapenko | 5241081 | 2017-08-10 15:00:13 +0000 | [diff] [blame] | 718 | IRB.CreateIntCast(A1, Ty, true)}); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 723 | void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB, |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 724 | size_t Idx) { |
Justin Bogner | 7ae63aa | 2015-08-14 17:03:45 +0000 | [diff] [blame] | 725 | BasicBlock::iterator IP = BB.getFirstInsertionPt(); |
Kostya Serebryany | d421db0 | 2015-01-03 00:54:43 +0000 | [diff] [blame] | 726 | bool IsEntryBB = &BB == &F.getEntryBlock(); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 727 | DebugLoc EntryLoc; |
| 728 | if (IsEntryBB) { |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 729 | if (auto SP = F.getSubprogram()) |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 730 | EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP); |
Reid Kleckner | a57d015 | 2015-08-14 16:45:42 +0000 | [diff] [blame] | 731 | // Keep static allocas and llvm.localescape calls in the entry block. Even |
| 732 | // if we aren't splitting the block, it's nice for allocas to be before |
| 733 | // calls. |
| 734 | IP = PrepareToSplitEntryBlock(BB, IP); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 735 | } else { |
| 736 | EntryLoc = IP->getDebugLoc(); |
| 737 | } |
| 738 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 739 | IRBuilder<> IRB(&*IP); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 740 | IRB.SetCurrentDebugLocation(EntryLoc); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 741 | if (Options.TracePC) { |
Kostya Serebryany | dd5c7f9 | 2016-07-14 17:59:01 +0000 | [diff] [blame] | 742 | IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC. |
| 743 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 744 | } |
| 745 | if (Options.TracePCGuard) { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 746 | auto GuardPtr = IRB.CreateIntToPtr( |
| 747 | IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy), |
| 748 | ConstantInt::get(IntptrTy, Idx * 4)), |
| 749 | Int32PtrTy); |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 750 | IRB.CreateCall(SanCovTracePCGuard, GuardPtr); |
| 751 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 752 | } |
Kostya Serebryany | 2c2fb88 | 2017-06-08 22:58:19 +0000 | [diff] [blame] | 753 | if (Options.Inline8bitCounters) { |
| 754 | auto CounterPtr = IRB.CreateGEP( |
| 755 | Function8bitCounterArray, |
| 756 | {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)}); |
| 757 | auto Load = IRB.CreateLoad(CounterPtr); |
| 758 | auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1)); |
| 759 | auto Store = IRB.CreateStore(Inc, CounterPtr); |
| 760 | SetNoSanitizeMetadata(Load); |
| 761 | SetNoSanitizeMetadata(Store); |
| 762 | } |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 763 | if (Options.StackDepth && IsEntryBB) { |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 764 | // Check stack depth. If it's the deepest so far, record it. |
| 765 | Function *GetFrameAddr = |
| 766 | Intrinsic::getDeclaration(F.getParent(), Intrinsic::frameaddress); |
| 767 | auto FrameAddrPtr = |
| 768 | IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)}); |
| 769 | auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy); |
Matt Morehouse | b1fa825 | 2017-08-22 21:28:29 +0000 | [diff] [blame] | 770 | auto LowestStack = IRB.CreateLoad(SanCovLowestStack); |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 771 | auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack); |
| 772 | auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false); |
| 773 | IRBuilder<> ThenIRB(ThenTerm); |
Matt Morehouse | 6ec7595 | 2017-08-25 22:01:21 +0000 | [diff] [blame] | 774 | ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack); |
Matt Morehouse | 5c7fc76 | 2017-08-18 18:43:30 +0000 | [diff] [blame] | 775 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 778 | std::string |
| 779 | SanitizerCoverageModule::getSectionName(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 780 | if (TargetTriple.getObjectFormat() == Triple::COFF) |
| 781 | return ".SCOV$M"; |
| 782 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 783 | return "__DATA,__" + Section; |
| 784 | return "__" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 787 | std::string |
| 788 | SanitizerCoverageModule::getSectionStart(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 789 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 790 | return "\1section$start$__DATA$__" + Section; |
| 791 | return "__start___" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 794 | std::string |
| 795 | SanitizerCoverageModule::getSectionEnd(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 796 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 797 | return "\1section$end$__DATA$__" + Section; |
| 798 | return "__stop___" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 802 | char SanitizerCoverageModule::ID = 0; |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 803 | INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov", |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 804 | "SanitizerCoverage: TODO." |
| 805 | "ModulePass", |
| 806 | false, false) |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 807 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 808 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 809 | INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov", |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 810 | "SanitizerCoverage: TODO." |
| 811 | "ModulePass", |
| 812 | false, false) |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 813 | ModulePass *llvm::createSanitizerCoverageModulePass( |
| 814 | const SanitizerCoverageOptions &Options) { |
| 815 | return new SanitizerCoverageModule(Options); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 816 | } |