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