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 | // |
| 10 | // Coverage instrumentation that works with AddressSanitizer |
| 11 | // and potentially with other Sanitizers. |
| 12 | // |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 13 | // We create a Guard variable with the same linkage |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 14 | // as the function and inject this code into the entry block (SCK_Function) |
| 15 | // or all blocks (SCK_BB): |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 16 | // if (Guard < 0) { |
Kostya Serebryany | 4cadd4a | 2014-11-24 18:49:53 +0000 | [diff] [blame] | 17 | // __sanitizer_cov(&Guard); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 18 | // } |
| 19 | // The accesses to Guard are atomic. The rest of the logic is |
| 20 | // in __sanitizer_cov (it's fine to call it more than once). |
| 21 | // |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 22 | // With SCK_Edge we also split critical edges this effectively |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 23 | // instrumenting all edges. |
| 24 | // |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 25 | // This coverage implementation provides very limited data: |
| 26 | // it only tells if a given function (block) was ever executed. No counters. |
| 27 | // But for many use cases this is what we need and the added slowdown small. |
| 28 | // |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/ArrayRef.h" |
| 32 | #include "llvm/ADT/SmallVector.h" |
David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/EHPersonalities.h" |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/PostDominators.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 35 | #include "llvm/IR/CFG.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 36 | #include "llvm/IR/CallSite.h" |
| 37 | #include "llvm/IR/DataLayout.h" |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 38 | #include "llvm/IR/DebugInfo.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Dominators.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Function.h" |
| 41 | #include "llvm/IR/IRBuilder.h" |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 42 | #include "llvm/IR/InlineAsm.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 43 | #include "llvm/IR/LLVMContext.h" |
| 44 | #include "llvm/IR/MDBuilder.h" |
| 45 | #include "llvm/IR/Module.h" |
| 46 | #include "llvm/IR/Type.h" |
| 47 | #include "llvm/Support/CommandLine.h" |
| 48 | #include "llvm/Support/Debug.h" |
| 49 | #include "llvm/Support/raw_ostream.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Scalar.h" |
| 52 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 53 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 54 | |
| 55 | using namespace llvm; |
| 56 | |
| 57 | #define DEBUG_TYPE "sancov" |
| 58 | |
| 59 | static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init"; |
| 60 | static const char *const kSanCovName = "__sanitizer_cov"; |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 61 | static const char *const kSanCovWithCheckName = "__sanitizer_cov_with_check"; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 62 | static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16"; |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 63 | static const char *const kSanCovTracePCIndir = "__sanitizer_cov_trace_pc_indir"; |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 64 | static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter"; |
| 65 | static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block"; |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 66 | static const char *const kSanCovTracePC = "__sanitizer_cov_trace_pc"; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 67 | static const char *const kSanCovTraceCmp = "__sanitizer_cov_trace_cmp"; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 68 | static const char *const kSanCovTraceSwitch = "__sanitizer_cov_trace_switch"; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 69 | static const char *const kSanCovModuleCtorName = "sancov.module_ctor"; |
Evgeniy Stepanov | 3fdfc7b | 2015-01-27 15:01:22 +0000 | [diff] [blame] | 70 | static const uint64_t kSanCtorAndDtorPriority = 2; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 71 | |
| 72 | static cl::opt<int> ClCoverageLevel("sanitizer-coverage-level", |
| 73 | cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, " |
| 74 | "3: all blocks and critical edges, " |
| 75 | "4: above plus indirect calls"), |
| 76 | cl::Hidden, cl::init(0)); |
| 77 | |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 78 | static cl::opt<unsigned> ClCoverageBlockThreshold( |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 79 | "sanitizer-coverage-block-threshold", |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 80 | cl::desc("Use a callback with a guard check inside it if there are" |
| 81 | " more than this number of blocks."), |
Kostya Serebryany | 8fb05ac | 2015-03-10 01:11:53 +0000 | [diff] [blame] | 82 | cl::Hidden, cl::init(500)); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 83 | |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 84 | static cl::opt<bool> |
| 85 | ClExperimentalTracing("sanitizer-coverage-experimental-tracing", |
| 86 | cl::desc("Experimental basic-block tracing: insert " |
| 87 | "callbacks at every basic block"), |
| 88 | cl::Hidden, cl::init(false)); |
| 89 | |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 90 | static cl::opt<bool> ClExperimentalTracePC("sanitizer-coverage-trace-pc", |
| 91 | cl::desc("Experimental pc tracing"), |
| 92 | cl::Hidden, cl::init(false)); |
| 93 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 94 | static cl::opt<bool> |
| 95 | ClExperimentalCMPTracing("sanitizer-coverage-experimental-trace-compares", |
| 96 | cl::desc("Experimental tracing of CMP and similar " |
| 97 | "instructions"), |
| 98 | cl::Hidden, cl::init(false)); |
| 99 | |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 100 | static cl::opt<bool> ClPruneBlocks( |
| 101 | "sanitizer-coverage-prune-blocks", |
| 102 | cl::desc("Reduce the number of instrumented blocks (experimental)"), |
| 103 | cl::Hidden, cl::init(false)); |
| 104 | |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 105 | // Experimental 8-bit counters used as an additional search heuristic during |
| 106 | // coverage-guided fuzzing. |
| 107 | // The counters are not thread-friendly: |
| 108 | // - contention on these counters may cause significant slowdown; |
| 109 | // - the counter updates are racy and the results may be inaccurate. |
| 110 | // They are also inaccurate due to 8-bit integer overflow. |
| 111 | static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters", |
| 112 | cl::desc("Experimental 8-bit counters"), |
| 113 | cl::Hidden, cl::init(false)); |
| 114 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 115 | namespace { |
| 116 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 117 | SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) { |
| 118 | SanitizerCoverageOptions Res; |
| 119 | switch (LegacyCoverageLevel) { |
| 120 | case 0: |
| 121 | Res.CoverageType = SanitizerCoverageOptions::SCK_None; |
| 122 | break; |
| 123 | case 1: |
| 124 | Res.CoverageType = SanitizerCoverageOptions::SCK_Function; |
| 125 | break; |
| 126 | case 2: |
| 127 | Res.CoverageType = SanitizerCoverageOptions::SCK_BB; |
| 128 | break; |
| 129 | case 3: |
| 130 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 131 | break; |
| 132 | case 4: |
| 133 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 134 | Res.IndirectCalls = true; |
| 135 | break; |
| 136 | } |
| 137 | return Res; |
| 138 | } |
| 139 | |
| 140 | SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) { |
| 141 | // Sets CoverageType and IndirectCalls. |
| 142 | SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel); |
| 143 | Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType); |
| 144 | Options.IndirectCalls |= CLOpts.IndirectCalls; |
| 145 | Options.TraceBB |= ClExperimentalTracing; |
| 146 | Options.TraceCmp |= ClExperimentalCMPTracing; |
| 147 | Options.Use8bitCounters |= ClUse8bitCounters; |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 148 | Options.TracePC |= ClExperimentalTracePC; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 149 | return Options; |
| 150 | } |
| 151 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 152 | class SanitizerCoverageModule : public ModulePass { |
| 153 | public: |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 154 | SanitizerCoverageModule( |
| 155 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()) |
Chandler Carruth | e2b7021 | 2016-03-18 22:35:58 +0000 | [diff] [blame] | 156 | : ModulePass(ID), Options(OverrideFromCL(Options)) { |
| 157 | initializeSanitizerCoverageModulePass(*PassRegistry::getPassRegistry()); |
| 158 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 159 | bool runOnModule(Module &M) override; |
| 160 | bool runOnFunction(Function &F); |
| 161 | static char ID; // Pass identification, replacement for typeid |
| 162 | const char *getPassName() const override { |
| 163 | return "SanitizerCoverageModule"; |
| 164 | } |
| 165 | |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 166 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 167 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 168 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
| 169 | } |
| 170 | |
| 171 | private: |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 172 | void InjectCoverageForIndirectCalls(Function &F, |
| 173 | ArrayRef<Instruction *> IndirCalls); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 174 | void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 175 | void InjectTraceForSwitch(Function &F, |
| 176 | ArrayRef<Instruction *> SwitchTraceTargets); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 177 | bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 178 | void SetNoSanitizeMetadata(Instruction *I); |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 179 | void InjectCoverageAtBlock(Function &F, BasicBlock &BB, bool UseCalls); |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 180 | unsigned NumberOfInstrumentedBlocks() { |
Kostya Serebryany | a3c5347 | 2015-12-02 02:37:13 +0000 | [diff] [blame] | 181 | return SanCovFunction->getNumUses() + |
| 182 | SanCovWithCheckFunction->getNumUses() + SanCovTraceBB->getNumUses() + |
| 183 | SanCovTraceEnter->getNumUses(); |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 184 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 185 | Function *SanCovFunction; |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 186 | Function *SanCovWithCheckFunction; |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 187 | Function *SanCovIndirCallFunction, *SanCovTracePCIndir; |
| 188 | Function *SanCovTraceEnter, *SanCovTraceBB, *SanCovTracePC; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 189 | Function *SanCovTraceCmpFunction; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 190 | Function *SanCovTraceSwitchFunction; |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 191 | InlineAsm *EmptyAsm; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 192 | Type *IntptrTy, *Int64Ty, *Int64PtrTy; |
| 193 | Module *CurModule; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 194 | LLVMContext *C; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 195 | const DataLayout *DL; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 196 | |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 197 | GlobalVariable *GuardArray; |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 198 | GlobalVariable *EightBitCounterArray; |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 199 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 200 | SanitizerCoverageOptions Options; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace |
| 204 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 205 | bool SanitizerCoverageModule::runOnModule(Module &M) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 206 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_None) |
| 207 | return false; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 208 | C = &(M.getContext()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 209 | DL = &M.getDataLayout(); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 210 | CurModule = &M; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 211 | IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits()); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 212 | Type *VoidTy = Type::getVoidTy(*C); |
Kostya Serebryany | 4cadd4a | 2014-11-24 18:49:53 +0000 | [diff] [blame] | 213 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 8859946 | 2015-02-20 00:30:44 +0000 | [diff] [blame] | 214 | Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty()); |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 215 | Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 216 | Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 217 | Int64Ty = IRB.getInt64Ty(); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 218 | |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 219 | SanCovFunction = checkSanitizerInterfaceFunction( |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 220 | M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr)); |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 221 | SanCovWithCheckFunction = checkSanitizerInterfaceFunction( |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 222 | M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr)); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 223 | SanCovTracePCIndir = |
| 224 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 225 | kSanCovTracePCIndir, VoidTy, IntptrTy, nullptr)); |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 226 | SanCovIndirCallFunction = |
| 227 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 228 | kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr)); |
| 229 | SanCovTraceCmpFunction = |
| 230 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 231 | kSanCovTraceCmp, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr)); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 232 | SanCovTraceSwitchFunction = |
| 233 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 234 | kSanCovTraceSwitch, VoidTy, Int64Ty, Int64PtrTy, nullptr)); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 235 | |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 236 | // We insert an empty inline asm after cov callbacks to avoid callback merge. |
| 237 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 238 | StringRef(""), StringRef(""), |
| 239 | /*hasSideEffects=*/true); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 240 | |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 241 | SanCovTracePC = checkSanitizerInterfaceFunction( |
| 242 | M.getOrInsertFunction(kSanCovTracePC, VoidTy, nullptr)); |
Kostya Serebryany | a3c5347 | 2015-12-02 02:37:13 +0000 | [diff] [blame] | 243 | SanCovTraceEnter = checkSanitizerInterfaceFunction( |
| 244 | M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr)); |
| 245 | SanCovTraceBB = checkSanitizerInterfaceFunction( |
| 246 | M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr)); |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 247 | |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 248 | // At this point we create a dummy array of guards because we don't |
| 249 | // know how many elements we will need. |
| 250 | Type *Int32Ty = IRB.getInt32Ty(); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 251 | Type *Int8Ty = IRB.getInt8Ty(); |
| 252 | |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 253 | GuardArray = |
| 254 | new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage, |
| 255 | nullptr, "__sancov_gen_cov_tmp"); |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 256 | if (Options.Use8bitCounters) |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 257 | EightBitCounterArray = |
| 258 | new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage, |
| 259 | nullptr, "__sancov_gen_cov_tmp"); |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 260 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 261 | for (auto &F : M) |
| 262 | runOnFunction(F); |
| 263 | |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 264 | auto N = NumberOfInstrumentedBlocks(); |
| 265 | |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 266 | // Now we know how many elements we need. Create an array of guards |
| 267 | // with one extra element at the beginning for the size. |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 268 | Type *Int32ArrayNTy = ArrayType::get(Int32Ty, N + 1); |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 269 | GlobalVariable *RealGuardArray = new GlobalVariable( |
| 270 | M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage, |
| 271 | Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov"); |
| 272 | |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 273 | |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 274 | // Replace the dummy array with the real one. |
| 275 | GuardArray->replaceAllUsesWith( |
| 276 | IRB.CreatePointerCast(RealGuardArray, Int32PtrTy)); |
| 277 | GuardArray->eraseFromParent(); |
| 278 | |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 279 | GlobalVariable *RealEightBitCounterArray; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 280 | if (Options.Use8bitCounters) { |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 281 | // Make sure the array is 16-aligned. |
| 282 | static const int kCounterAlignment = 16; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 283 | Type *Int8ArrayNTy = ArrayType::get(Int8Ty, alignTo(N, kCounterAlignment)); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 284 | RealEightBitCounterArray = new GlobalVariable( |
| 285 | M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage, |
| 286 | Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter"); |
| 287 | RealEightBitCounterArray->setAlignment(kCounterAlignment); |
| 288 | EightBitCounterArray->replaceAllUsesWith( |
| 289 | IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)); |
| 290 | EightBitCounterArray->eraseFromParent(); |
| 291 | } |
| 292 | |
Kostya Serebryany | 8859946 | 2015-02-20 00:30:44 +0000 | [diff] [blame] | 293 | // Create variable for module (compilation unit) name |
| 294 | Constant *ModNameStrConst = |
| 295 | ConstantDataArray::getString(M.getContext(), M.getName(), true); |
| 296 | GlobalVariable *ModuleName = |
| 297 | new GlobalVariable(M, ModNameStrConst->getType(), true, |
| 298 | GlobalValue::PrivateLinkage, ModNameStrConst); |
| 299 | |
Kostya Serebryany | 3c767db | 2016-02-27 05:45:12 +0000 | [diff] [blame] | 300 | if (!Options.TracePC) { |
| 301 | Function *CtorFunc; |
| 302 | std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( |
| 303 | M, kSanCovModuleCtorName, kSanCovModuleInitName, |
| 304 | {Int32PtrTy, IntptrTy, Int8PtrTy, Int8PtrTy}, |
| 305 | {IRB.CreatePointerCast(RealGuardArray, Int32PtrTy), |
| 306 | ConstantInt::get(IntptrTy, N), |
| 307 | Options.Use8bitCounters |
| 308 | ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy) |
| 309 | : Constant::getNullValue(Int8PtrTy), |
| 310 | IRB.CreatePointerCast(ModuleName, Int8PtrTy)}); |
Ismail Pazarbasi | d02ce13 | 2015-05-10 13:45:05 +0000 | [diff] [blame] | 311 | |
Kostya Serebryany | 3c767db | 2016-02-27 05:45:12 +0000 | [diff] [blame] | 312 | appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority); |
| 313 | } |
Ismail Pazarbasi | d02ce13 | 2015-05-10 13:45:05 +0000 | [diff] [blame] | 314 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 318 | static bool shouldInstrumentBlock(const BasicBlock *BB, |
| 319 | const DominatorTree *DT) { |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 320 | if (!ClPruneBlocks) |
| 321 | return true; |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 322 | if (succ_begin(BB) == succ_end(BB)) |
| 323 | return true; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 324 | |
| 325 | // Check if BB dominates all its successors. |
| 326 | for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) { |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 327 | if (!DT->dominates(BB, SUCC)) |
| 328 | return true; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 331 | return false; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 334 | bool SanitizerCoverageModule::runOnFunction(Function &F) { |
| 335 | if (F.empty()) return false; |
Kostya Serebryany | fea4fb4 | 2014-12-17 21:50:04 +0000 | [diff] [blame] | 336 | if (F.getName().find(".module_ctor") != std::string::npos) |
| 337 | return false; // Should not instrument sanitizer init functions. |
Reid Kleckner | df52337 | 2015-09-03 20:18:29 +0000 | [diff] [blame] | 338 | // Don't instrument functions using SEH for now. Splitting basic blocks like |
| 339 | // we do for coverage breaks WinEHPrepare. |
| 340 | // FIXME: Remove this when SEH no longer uses landingpad pattern matching. |
| 341 | if (F.hasPersonalityFn() && |
| 342 | isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) |
| 343 | return false; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 344 | if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 345 | SplitAllCriticalEdges(F); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 346 | SmallVector<Instruction*, 8> IndirCalls; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 347 | SmallVector<BasicBlock *, 16> BlocksToInstrument; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 348 | SmallVector<Instruction*, 8> CmpTraceTargets; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 349 | SmallVector<Instruction*, 8> SwitchTraceTargets; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 350 | |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 351 | DominatorTree DT; |
| 352 | DT.recalculate(F); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 353 | for (auto &BB : F) { |
Renato Golin | 9a5419e | 2016-02-27 14:19:19 +0000 | [diff] [blame] | 354 | if (shouldInstrumentBlock(&BB, &DT)) |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 355 | BlocksToInstrument.push_back(&BB); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 356 | for (auto &Inst : BB) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 357 | if (Options.IndirectCalls) { |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 358 | CallSite CS(&Inst); |
| 359 | if (CS && !CS.getCalledFunction()) |
| 360 | IndirCalls.push_back(&Inst); |
| 361 | } |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 362 | if (Options.TraceCmp) { |
| 363 | if (isa<ICmpInst>(&Inst)) |
| 364 | CmpTraceTargets.push_back(&Inst); |
| 365 | if (isa<SwitchInst>(&Inst)) |
| 366 | SwitchTraceTargets.push_back(&Inst); |
| 367 | } |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 368 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 369 | } |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 370 | |
| 371 | InjectCoverage(F, BlocksToInstrument); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 372 | InjectCoverageForIndirectCalls(F, IndirCalls); |
| 373 | InjectTraceForCmp(F, CmpTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 374 | InjectTraceForSwitch(F, SwitchTraceTargets); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 378 | bool SanitizerCoverageModule::InjectCoverage(Function &F, |
| 379 | ArrayRef<BasicBlock *> AllBlocks) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 380 | switch (Options.CoverageType) { |
| 381 | case SanitizerCoverageOptions::SCK_None: |
| 382 | return false; |
| 383 | case SanitizerCoverageOptions::SCK_Function: |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 384 | InjectCoverageAtBlock(F, F.getEntryBlock(), false); |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 385 | return true; |
| 386 | default: { |
| 387 | bool UseCalls = ClCoverageBlockThreshold < AllBlocks.size(); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 388 | for (auto BB : AllBlocks) |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 389 | InjectCoverageAtBlock(F, *BB, UseCalls); |
| 390 | return true; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 391 | } |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 392 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // On every indirect call we call a run-time function |
| 396 | // __sanitizer_cov_indir_call* with two parameters: |
| 397 | // - callee address, |
| 398 | // - global cache array that contains kCacheSize pointers (zero-initialized). |
| 399 | // The cache is used to speed up recording the caller-callee pairs. |
| 400 | // The address of the caller is passed implicitly via caller PC. |
| 401 | // kCacheSize is encoded in the name of the run-time function. |
| 402 | void SanitizerCoverageModule::InjectCoverageForIndirectCalls( |
| 403 | Function &F, ArrayRef<Instruction *> IndirCalls) { |
| 404 | if (IndirCalls.empty()) return; |
| 405 | const int kCacheSize = 16; |
| 406 | const int kCacheAlignment = 64; // Align for better performance. |
| 407 | Type *Ty = ArrayType::get(IntptrTy, kCacheSize); |
| 408 | for (auto I : IndirCalls) { |
| 409 | IRBuilder<> IRB(I); |
| 410 | CallSite CS(I); |
| 411 | Value *Callee = CS.getCalledValue(); |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 412 | if (isa<InlineAsm>(Callee)) continue; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 413 | GlobalVariable *CalleeCache = new GlobalVariable( |
| 414 | *F.getParent(), Ty, false, GlobalValue::PrivateLinkage, |
| 415 | Constant::getNullValue(Ty), "__sancov_gen_callee_cache"); |
| 416 | CalleeCache->setAlignment(kCacheAlignment); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 417 | if (Options.TracePC) |
| 418 | IRB.CreateCall(SanCovTracePCIndir, |
| 419 | IRB.CreatePointerCast(Callee, IntptrTy)); |
| 420 | else |
| 421 | IRB.CreateCall(SanCovIndirCallFunction, |
| 422 | {IRB.CreatePointerCast(Callee, IntptrTy), |
| 423 | IRB.CreatePointerCast(CalleeCache, IntptrTy)}); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 427 | // For every switch statement we insert a call: |
| 428 | // __sanitizer_cov_trace_switch(CondValue, |
| 429 | // {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... }) |
| 430 | |
| 431 | void SanitizerCoverageModule::InjectTraceForSwitch( |
| 432 | Function &F, ArrayRef<Instruction *> SwitchTraceTargets) { |
| 433 | for (auto I : SwitchTraceTargets) { |
| 434 | if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { |
| 435 | IRBuilder<> IRB(I); |
| 436 | SmallVector<Constant *, 16> Initializers; |
| 437 | Value *Cond = SI->getCondition(); |
Kostya Serebryany | 2569118 | 2015-08-11 00:24:39 +0000 | [diff] [blame] | 438 | if (Cond->getType()->getScalarSizeInBits() > |
| 439 | Int64Ty->getScalarSizeInBits()) |
| 440 | continue; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 441 | Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases())); |
| 442 | Initializers.push_back( |
| 443 | ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits())); |
| 444 | if (Cond->getType()->getScalarSizeInBits() < |
| 445 | Int64Ty->getScalarSizeInBits()) |
| 446 | Cond = IRB.CreateIntCast(Cond, Int64Ty, false); |
| 447 | for (auto It: SI->cases()) { |
| 448 | Constant *C = It.getCaseValue(); |
| 449 | if (C->getType()->getScalarSizeInBits() < |
| 450 | Int64Ty->getScalarSizeInBits()) |
| 451 | C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty); |
| 452 | Initializers.push_back(C); |
| 453 | } |
| 454 | ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size()); |
| 455 | GlobalVariable *GV = new GlobalVariable( |
| 456 | *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage, |
| 457 | ConstantArray::get(ArrayOfInt64Ty, Initializers), |
| 458 | "__sancov_gen_cov_switch_values"); |
| 459 | IRB.CreateCall(SanCovTraceSwitchFunction, |
| 460 | {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)}); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 466 | void SanitizerCoverageModule::InjectTraceForCmp( |
| 467 | Function &F, ArrayRef<Instruction *> CmpTraceTargets) { |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 468 | for (auto I : CmpTraceTargets) { |
| 469 | if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) { |
| 470 | IRBuilder<> IRB(ICMP); |
| 471 | Value *A0 = ICMP->getOperand(0); |
| 472 | Value *A1 = ICMP->getOperand(1); |
| 473 | if (!A0->getType()->isIntegerTy()) continue; |
| 474 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType()); |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 475 | // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 476 | IRB.CreateCall( |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 477 | SanCovTraceCmpFunction, |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 478 | {ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()), |
| 479 | IRB.CreateIntCast(A0, Int64Ty, true), |
| 480 | IRB.CreateIntCast(A1, Int64Ty, true)}); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 485 | void SanitizerCoverageModule::SetNoSanitizeMetadata(Instruction *I) { |
Kostya Serebryany | 83ce877 | 2015-03-05 01:20:05 +0000 | [diff] [blame] | 486 | I->setMetadata( |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 487 | I->getModule()->getMDKindID("nosanitize"), MDNode::get(*C, None)); |
Kostya Serebryany | 83ce877 | 2015-03-05 01:20:05 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 490 | void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB, |
| 491 | bool UseCalls) { |
Alexey Samsonov | 342b1e8 | 2015-06-30 23:11:45 +0000 | [diff] [blame] | 492 | // Don't insert coverage for unreachable blocks: we will never call |
| 493 | // __sanitizer_cov() for them, so counting them in |
| 494 | // NumberOfInstrumentedBlocks() might complicate calculation of code coverage |
| 495 | // percentage. Also, unreachable instructions frequently have no debug |
| 496 | // locations. |
| 497 | if (isa<UnreachableInst>(BB.getTerminator())) |
| 498 | return; |
Justin Bogner | 7ae63aa | 2015-08-14 17:03:45 +0000 | [diff] [blame] | 499 | BasicBlock::iterator IP = BB.getFirstInsertionPt(); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 500 | |
Kostya Serebryany | d421db0 | 2015-01-03 00:54:43 +0000 | [diff] [blame] | 501 | bool IsEntryBB = &BB == &F.getEntryBlock(); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 502 | DebugLoc EntryLoc; |
| 503 | if (IsEntryBB) { |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 504 | if (auto SP = F.getSubprogram()) |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 505 | EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP); |
Reid Kleckner | a57d015 | 2015-08-14 16:45:42 +0000 | [diff] [blame] | 506 | // Keep static allocas and llvm.localescape calls in the entry block. Even |
| 507 | // if we aren't splitting the block, it's nice for allocas to be before |
| 508 | // calls. |
| 509 | IP = PrepareToSplitEntryBlock(BB, IP); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 510 | } else { |
| 511 | EntryLoc = IP->getDebugLoc(); |
| 512 | } |
| 513 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 514 | IRBuilder<> IRB(&*IP); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 515 | IRB.SetCurrentDebugLocation(EntryLoc); |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 516 | Value *GuardP = IRB.CreateAdd( |
| 517 | IRB.CreatePointerCast(GuardArray, IntptrTy), |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 518 | ConstantInt::get(IntptrTy, (1 + NumberOfInstrumentedBlocks()) * 4)); |
Kostya Serebryany | aa185bf | 2014-12-30 19:29:28 +0000 | [diff] [blame] | 519 | Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
| 520 | GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 521 | if (Options.TracePC) { |
| 522 | IRB.CreateCall(SanCovTracePC); |
| 523 | } else if (Options.TraceBB) { |
Kostya Serebryany | a3c5347 | 2015-12-02 02:37:13 +0000 | [diff] [blame] | 524 | IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP); |
| 525 | } else if (UseCalls) { |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 526 | IRB.CreateCall(SanCovWithCheckFunction, GuardP); |
| 527 | } else { |
| 528 | LoadInst *Load = IRB.CreateLoad(GuardP); |
| 529 | Load->setAtomic(Monotonic); |
| 530 | Load->setAlignment(4); |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 531 | SetNoSanitizeMetadata(Load); |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 532 | Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load); |
| 533 | Instruction *Ins = SplitBlockAndInsertIfThen( |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 534 | Cmp, &*IP, false, MDBuilder(*C).createBranchWeights(1, 100000)); |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 535 | IRB.SetInsertPoint(Ins); |
| 536 | IRB.SetCurrentDebugLocation(EntryLoc); |
| 537 | // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC. |
| 538 | IRB.CreateCall(SanCovFunction, GuardP); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 539 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 540 | } |
Kostya Serebryany | d421db0 | 2015-01-03 00:54:43 +0000 | [diff] [blame] | 541 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 542 | if (Options.Use8bitCounters) { |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 543 | IRB.SetInsertPoint(&*IP); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 544 | Value *P = IRB.CreateAdd( |
| 545 | IRB.CreatePointerCast(EightBitCounterArray, IntptrTy), |
Kostya Serebryany | 48a4023 | 2015-03-10 01:58:27 +0000 | [diff] [blame] | 546 | ConstantInt::get(IntptrTy, NumberOfInstrumentedBlocks() - 1)); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 547 | P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy()); |
Kostya Serebryany | 83ce877 | 2015-03-05 01:20:05 +0000 | [diff] [blame] | 548 | LoadInst *LI = IRB.CreateLoad(P); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 549 | Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1)); |
Kostya Serebryany | 83ce877 | 2015-03-05 01:20:05 +0000 | [diff] [blame] | 550 | StoreInst *SI = IRB.CreateStore(Inc, P); |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 551 | SetNoSanitizeMetadata(LI); |
| 552 | SetNoSanitizeMetadata(SI); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 553 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | char SanitizerCoverageModule::ID = 0; |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 557 | INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov", |
| 558 | "SanitizerCoverage: TODO." |
| 559 | "ModulePass", false, false) |
| 560 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 561 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
| 562 | INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov", |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 563 | "SanitizerCoverage: TODO." |
| 564 | "ModulePass", false, false) |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 565 | ModulePass *llvm::createSanitizerCoverageModulePass( |
| 566 | const SanitizerCoverageOptions &Options) { |
| 567 | return new SanitizerCoverageModule(Options); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 568 | } |