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" |
| 20 | #include "llvm/IR/DataLayout.h" |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DebugInfo.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/IRBuilder.h" |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 25 | #include "llvm/IR/InlineAsm.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/MDBuilder.h" |
| 28 | #include "llvm/IR/Module.h" |
| 29 | #include "llvm/IR/Type.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Scalar.h" |
| 35 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 36 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
| 40 | #define DEBUG_TYPE "sancov" |
| 41 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 42 | static const char *const SanCovTracePCIndirName = |
| 43 | "__sanitizer_cov_trace_pc_indir"; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 44 | static const char *const SanCovTracePCName = "__sanitizer_cov_trace_pc"; |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 45 | static const char *const SanCovTraceCmp1 = "__sanitizer_cov_trace_cmp1"; |
| 46 | static const char *const SanCovTraceCmp2 = "__sanitizer_cov_trace_cmp2"; |
| 47 | static const char *const SanCovTraceCmp4 = "__sanitizer_cov_trace_cmp4"; |
| 48 | static const char *const SanCovTraceCmp8 = "__sanitizer_cov_trace_cmp8"; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 49 | static const char *const SanCovTraceDiv4 = "__sanitizer_cov_trace_div4"; |
| 50 | static const char *const SanCovTraceDiv8 = "__sanitizer_cov_trace_div8"; |
| 51 | static const char *const SanCovTraceGep = "__sanitizer_cov_trace_gep"; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 52 | static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch"; |
| 53 | static const char *const SanCovModuleCtorName = "sancov.module_ctor"; |
| 54 | static const uint64_t SanCtorAndDtorPriority = 2; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 55 | |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 56 | static const char *const SanCovTracePCGuardName = |
| 57 | "__sanitizer_cov_trace_pc_guard"; |
| 58 | static const char *const SanCovTracePCGuardInitName = |
| 59 | "__sanitizer_cov_trace_pc_guard_init"; |
| 60 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 61 | static const char *const SanCovGuardsSectionName = "sancov_guards"; |
| 62 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 63 | static cl::opt<int> ClCoverageLevel( |
| 64 | "sanitizer-coverage-level", |
| 65 | cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, " |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 66 | "3: all blocks and critical edges"), |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 67 | cl::Hidden, cl::init(0)); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 68 | |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 69 | static cl::opt<bool> ClExperimentalTracePC("sanitizer-coverage-trace-pc", |
| 70 | cl::desc("Experimental pc tracing"), |
| 71 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 72 | |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 73 | static cl::opt<bool> ClTracePCGuard("sanitizer-coverage-trace-pc-guard", |
| 74 | cl::desc("pc tracing with a guard"), |
| 75 | cl::Hidden, cl::init(false)); |
| 76 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 77 | static cl::opt<bool> |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 78 | ClCMPTracing("sanitizer-coverage-trace-compares", |
| 79 | cl::desc("Tracing of CMP and similar instructions"), |
| 80 | cl::Hidden, cl::init(false)); |
| 81 | |
| 82 | static cl::opt<bool> ClDIVTracing("sanitizer-coverage-trace-divs", |
| 83 | cl::desc("Tracing of DIV instructions"), |
| 84 | cl::Hidden, cl::init(false)); |
| 85 | |
| 86 | static cl::opt<bool> ClGEPTracing("sanitizer-coverage-trace-geps", |
| 87 | cl::desc("Tracing of GEP instructions"), |
| 88 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 89 | |
Mike Aizatsky | 70ea453 | 2016-04-06 23:24:37 +0000 | [diff] [blame] | 90 | static cl::opt<bool> |
| 91 | ClPruneBlocks("sanitizer-coverage-prune-blocks", |
| 92 | cl::desc("Reduce the number of instrumented blocks"), |
| 93 | cl::Hidden, cl::init(true)); |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 94 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 95 | namespace { |
| 96 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 97 | SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) { |
| 98 | SanitizerCoverageOptions Res; |
| 99 | switch (LegacyCoverageLevel) { |
| 100 | case 0: |
| 101 | Res.CoverageType = SanitizerCoverageOptions::SCK_None; |
| 102 | break; |
| 103 | case 1: |
| 104 | Res.CoverageType = SanitizerCoverageOptions::SCK_Function; |
| 105 | break; |
| 106 | case 2: |
| 107 | Res.CoverageType = SanitizerCoverageOptions::SCK_BB; |
| 108 | break; |
| 109 | case 3: |
| 110 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 111 | break; |
| 112 | case 4: |
| 113 | Res.CoverageType = SanitizerCoverageOptions::SCK_Edge; |
| 114 | Res.IndirectCalls = true; |
| 115 | break; |
| 116 | } |
| 117 | return Res; |
| 118 | } |
| 119 | |
| 120 | SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) { |
| 121 | // Sets CoverageType and IndirectCalls. |
| 122 | SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel); |
| 123 | Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType); |
| 124 | Options.IndirectCalls |= CLOpts.IndirectCalls; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 125 | Options.TraceCmp |= ClCMPTracing; |
| 126 | Options.TraceDiv |= ClDIVTracing; |
| 127 | Options.TraceGep |= ClGEPTracing; |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 128 | Options.TracePC |= ClExperimentalTracePC; |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 129 | Options.TracePCGuard |= ClTracePCGuard; |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 130 | if (!Options.TracePCGuard && !Options.TracePC) |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 131 | Options.TracePCGuard = true; // TracePCGuard is default. |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 132 | Options.NoPrune |= !ClPruneBlocks; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 133 | return Options; |
| 134 | } |
| 135 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 136 | class SanitizerCoverageModule : public ModulePass { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 137 | public: |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 138 | SanitizerCoverageModule( |
| 139 | const SanitizerCoverageOptions &Options = SanitizerCoverageOptions()) |
Chandler Carruth | e2b7021 | 2016-03-18 22:35:58 +0000 | [diff] [blame] | 140 | : ModulePass(ID), Options(OverrideFromCL(Options)) { |
| 141 | initializeSanitizerCoverageModulePass(*PassRegistry::getPassRegistry()); |
| 142 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 143 | bool runOnModule(Module &M) override; |
| 144 | bool runOnFunction(Function &F); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 145 | static char ID; // Pass identification, replacement for typeid |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 146 | StringRef getPassName() const override { return "SanitizerCoverageModule"; } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 147 | |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 148 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 149 | AU.addRequired<DominatorTreeWrapperPass>(); |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 150 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
Chandler Carruth | 3006115 | 2016-03-18 22:43:42 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private: |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 154 | void InjectCoverageForIndirectCalls(Function &F, |
| 155 | ArrayRef<Instruction *> IndirCalls); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 156 | void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 157 | void InjectTraceForDiv(Function &F, |
| 158 | ArrayRef<BinaryOperator *> DivTraceTargets); |
| 159 | void InjectTraceForGep(Function &F, |
| 160 | ArrayRef<GetElementPtrInst *> GepTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 161 | void InjectTraceForSwitch(Function &F, |
| 162 | ArrayRef<Instruction *> SwitchTraceTargets); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 163 | bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 164 | GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements, |
| 165 | Function &F, Type *Ty, |
| 166 | const char *Section); |
| 167 | void CreateFunctionLocalArrays(size_t NumGuards, Function &F); |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 168 | void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 169 | void CreateInitCallForSection(Module &M, const char *InitFunctionName, |
| 170 | Type *Ty, const std::string &Section); |
| 171 | |
| 172 | std::string getSectionName(const std::string &Section) const; |
| 173 | std::string getSectionStart(const std::string &Section) const; |
| 174 | std::string getSectionEnd(const std::string &Section) const; |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 175 | Function *SanCovTracePCIndir; |
Kostya Serebryany | be87d48 | 2017-04-19 21:48:09 +0000 | [diff] [blame] | 176 | Function *SanCovTracePC, *SanCovTracePCGuard; |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 177 | Function *SanCovTraceCmpFunction[4]; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 178 | Function *SanCovTraceDivFunction[2]; |
| 179 | Function *SanCovTraceGepFunction; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 180 | Function *SanCovTraceSwitchFunction; |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 181 | InlineAsm *EmptyAsm; |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 182 | Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 183 | Module *CurModule; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 184 | Triple TargetTriple; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 185 | LLVMContext *C; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 186 | const DataLayout *DL; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 187 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 188 | GlobalVariable *FunctionGuardArray; // for trace-pc-guard. |
Kostya Serebryany | 9fdeb37 | 2014-12-23 22:32:17 +0000 | [diff] [blame] | 189 | |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 190 | SanitizerCoverageOptions Options; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 193 | } // namespace |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 194 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 195 | void SanitizerCoverageModule::CreateInitCallForSection( |
| 196 | Module &M, const char *InitFunctionName, Type *Ty, |
| 197 | const std::string &Section) { |
| 198 | IRBuilder<> IRB(M.getContext()); |
| 199 | Function *CtorFunc; |
| 200 | GlobalVariable *SecStart = |
| 201 | new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr, |
| 202 | getSectionStart(Section)); |
| 203 | SecStart->setVisibility(GlobalValue::HiddenVisibility); |
| 204 | GlobalVariable *SecEnd = |
| 205 | new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, |
| 206 | nullptr, getSectionEnd(Section)); |
| 207 | SecEnd->setVisibility(GlobalValue::HiddenVisibility); |
| 208 | |
| 209 | std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( |
| 210 | M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty}, |
| 211 | {IRB.CreatePointerCast(SecStart, Ty), IRB.CreatePointerCast(SecEnd, Ty)}); |
| 212 | |
| 213 | if (TargetTriple.supportsCOMDAT()) { |
| 214 | // Use comdat to dedup CtorFunc. |
| 215 | CtorFunc->setComdat(M.getOrInsertComdat(SanCovModuleCtorName)); |
| 216 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc); |
| 217 | } else { |
| 218 | appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority); |
| 219 | } |
| 220 | } |
| 221 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 222 | bool SanitizerCoverageModule::runOnModule(Module &M) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 223 | if (Options.CoverageType == SanitizerCoverageOptions::SCK_None) |
| 224 | return false; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 225 | C = &(M.getContext()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 226 | DL = &M.getDataLayout(); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 227 | CurModule = &M; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 228 | TargetTriple = Triple(M.getTargetTriple()); |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 229 | FunctionGuardArray = nullptr; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 230 | IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits()); |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 231 | IntptrPtrTy = PointerType::getUnqual(IntptrTy); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 232 | Type *VoidTy = Type::getVoidTy(*C); |
Kostya Serebryany | 4cadd4a | 2014-11-24 18:49:53 +0000 | [diff] [blame] | 233 | IRBuilder<> IRB(*C); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 234 | Int64PtrTy = PointerType::getUnqual(IRB.getInt64Ty()); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 235 | Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 236 | Int64Ty = IRB.getInt64Ty(); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 237 | Int32Ty = IRB.getInt32Ty(); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 238 | |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 239 | SanCovTracePCIndir = checkSanitizerInterfaceFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 240 | M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy)); |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 241 | SanCovTraceCmpFunction[0] = |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 242 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 243 | SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 244 | SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction( |
| 245 | M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 246 | IRB.getInt16Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 247 | SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction( |
| 248 | M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 249 | IRB.getInt32Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 250 | SanCovTraceCmpFunction[3] = |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 251 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 252 | SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty)); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 253 | |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 254 | SanCovTraceDivFunction[0] = |
| 255 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 256 | SanCovTraceDiv4, VoidTy, IRB.getInt32Ty())); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 257 | SanCovTraceDivFunction[1] = |
| 258 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 259 | SanCovTraceDiv8, VoidTy, Int64Ty)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 260 | SanCovTraceGepFunction = |
| 261 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 262 | SanCovTraceGep, VoidTy, IntptrTy)); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 263 | SanCovTraceSwitchFunction = |
| 264 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 265 | SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy)); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 266 | |
Kostya Serebryany | 7376294 | 2014-12-16 21:24:15 +0000 | [diff] [blame] | 267 | // We insert an empty inline asm after cov callbacks to avoid callback merge. |
| 268 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 269 | StringRef(""), StringRef(""), |
| 270 | /*hasSideEffects=*/true); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 271 | |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 272 | SanCovTracePC = checkSanitizerInterfaceFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 273 | M.getOrInsertFunction(SanCovTracePCName, VoidTy)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 274 | SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 275 | SanCovTracePCGuardName, VoidTy, Int32PtrTy)); |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 276 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 277 | for (auto &F : M) |
| 278 | runOnFunction(F); |
| 279 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 280 | if (FunctionGuardArray) |
| 281 | CreateInitCallForSection(M, SanCovTracePCGuardInitName, Int32PtrTy, |
| 282 | SanCovGuardsSectionName); |
Justin Bogner | 41e632b | 2017-02-01 02:38:39 +0000 | [diff] [blame] | 283 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Mike Aizatsky | 9987f43 | 2016-03-23 23:15:03 +0000 | [diff] [blame] | 287 | // True if block has successors and it dominates all of them. |
| 288 | static bool isFullDominator(const BasicBlock *BB, const DominatorTree *DT) { |
| 289 | if (succ_begin(BB) == succ_end(BB)) |
| 290 | return false; |
| 291 | |
| 292 | for (const BasicBlock *SUCC : make_range(succ_begin(BB), succ_end(BB))) { |
| 293 | if (!DT->dominates(BB, SUCC)) |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 300 | // True if block has predecessors and it postdominates all of them. |
| 301 | static bool isFullPostDominator(const BasicBlock *BB, |
| 302 | const PostDominatorTree *PDT) { |
| 303 | if (pred_begin(BB) == pred_end(BB)) |
| 304 | return false; |
| 305 | |
| 306 | for (const BasicBlock *PRED : make_range(pred_begin(BB), pred_end(BB))) { |
| 307 | if (!PDT->dominates(BB, PRED)) |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | return true; |
| 312 | } |
| 313 | |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 314 | static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB, |
| 315 | const DominatorTree *DT, |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 316 | const PostDominatorTree *PDT, |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 317 | const SanitizerCoverageOptions &Options) { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 318 | // Don't insert coverage for unreachable blocks: we will never call |
| 319 | // __sanitizer_cov() for them, so counting them in |
| 320 | // NumberOfInstrumentedBlocks() might complicate calculation of code coverage |
| 321 | // percentage. Also, unreachable instructions frequently have no debug |
| 322 | // locations. |
| 323 | if (isa<UnreachableInst>(BB->getTerminator())) |
| 324 | return false; |
| 325 | |
Reid Kleckner | 392f062 | 2017-03-23 23:30:41 +0000 | [diff] [blame] | 326 | // Don't insert coverage into blocks without a valid insertion point |
| 327 | // (catchswitch blocks). |
| 328 | if (BB->getFirstInsertionPt() == BB->end()) |
| 329 | return false; |
| 330 | |
Kostya Serebryany | 424bfed | 2017-05-05 23:14:40 +0000 | [diff] [blame] | 331 | if (Options.NoPrune || &F.getEntryBlock() == BB) |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 332 | return true; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 333 | |
George Karpenkov | a1c5327 | 2017-05-25 01:41:46 +0000 | [diff] [blame] | 334 | // Do not instrument full dominators, or full post-dominators with multiple |
| 335 | // predecessors. |
| 336 | return !isFullDominator(BB, DT) |
| 337 | && !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor()); |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 340 | bool SanitizerCoverageModule::runOnFunction(Function &F) { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 341 | if (F.empty()) |
| 342 | return false; |
Kostya Serebryany | fea4fb4 | 2014-12-17 21:50:04 +0000 | [diff] [blame] | 343 | if (F.getName().find(".module_ctor") != std::string::npos) |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 344 | return false; // Should not instrument sanitizer init functions. |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 345 | if (F.getName().startswith("__sanitizer_")) |
| 346 | return false; // Don't instrument __sanitizer_* callbacks. |
Reid Kleckner | ec80354 | 2016-11-11 19:18:45 +0000 | [diff] [blame] | 347 | // Don't instrument MSVC CRT configuration helpers. They may run before normal |
| 348 | // initialization. |
| 349 | if (F.getName() == "__local_stdio_printf_options" || |
| 350 | F.getName() == "__local_stdio_scanf_options") |
| 351 | return false; |
Reid Kleckner | df52337 | 2015-09-03 20:18:29 +0000 | [diff] [blame] | 352 | // Don't instrument functions using SEH for now. Splitting basic blocks like |
| 353 | // we do for coverage breaks WinEHPrepare. |
| 354 | // FIXME: Remove this when SEH no longer uses landingpad pattern matching. |
| 355 | if (F.hasPersonalityFn() && |
| 356 | isAsynchronousEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) |
| 357 | return false; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 358 | if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 359 | SplitAllCriticalEdges(F); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 360 | SmallVector<Instruction *, 8> IndirCalls; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 361 | SmallVector<BasicBlock *, 16> BlocksToInstrument; |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 362 | SmallVector<Instruction *, 8> CmpTraceTargets; |
| 363 | SmallVector<Instruction *, 8> SwitchTraceTargets; |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 364 | SmallVector<BinaryOperator *, 8> DivTraceTargets; |
| 365 | SmallVector<GetElementPtrInst *, 8> GepTraceTargets; |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 366 | |
Mike Aizatsky | 602f792 | 2016-03-21 23:08:16 +0000 | [diff] [blame] | 367 | const DominatorTree *DT = |
| 368 | &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 369 | const PostDominatorTree *PDT = |
| 370 | &getAnalysis<PostDominatorTreeWrapperPass>(F).getPostDomTree(); |
Mike Aizatsky | 602f792 | 2016-03-21 23:08:16 +0000 | [diff] [blame] | 371 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 372 | for (auto &BB : F) { |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 373 | if (shouldInstrumentBlock(F, &BB, DT, PDT, Options)) |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 374 | BlocksToInstrument.push_back(&BB); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 375 | for (auto &Inst : BB) { |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 376 | if (Options.IndirectCalls) { |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 377 | CallSite CS(&Inst); |
| 378 | if (CS && !CS.getCalledFunction()) |
| 379 | IndirCalls.push_back(&Inst); |
| 380 | } |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 381 | if (Options.TraceCmp) { |
| 382 | if (isa<ICmpInst>(&Inst)) |
| 383 | CmpTraceTargets.push_back(&Inst); |
| 384 | if (isa<SwitchInst>(&Inst)) |
| 385 | SwitchTraceTargets.push_back(&Inst); |
| 386 | } |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 387 | if (Options.TraceDiv) |
| 388 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&Inst)) |
| 389 | if (BO->getOpcode() == Instruction::SDiv || |
| 390 | BO->getOpcode() == Instruction::UDiv) |
| 391 | DivTraceTargets.push_back(BO); |
| 392 | if (Options.TraceGep) |
| 393 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Inst)) |
| 394 | GepTraceTargets.push_back(GEP); |
| 395 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 396 | } |
Mike Aizatsky | 5971f18 | 2016-02-26 01:17:22 +0000 | [diff] [blame] | 397 | |
| 398 | InjectCoverage(F, BlocksToInstrument); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 399 | InjectCoverageForIndirectCalls(F, IndirCalls); |
| 400 | InjectTraceForCmp(F, CmpTraceTargets); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 401 | InjectTraceForSwitch(F, SwitchTraceTargets); |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 402 | InjectTraceForDiv(F, DivTraceTargets); |
| 403 | InjectTraceForGep(F, GepTraceTargets); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 404 | return true; |
| 405 | } |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 406 | |
| 407 | GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection( |
| 408 | size_t NumElements, Function &F, Type *Ty, const char *Section) { |
| 409 | ArrayType *ArrayTy = ArrayType::get(Ty, NumElements); |
| 410 | auto Array = new GlobalVariable( |
| 411 | *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage, |
| 412 | Constant::getNullValue(ArrayTy), "__sancov_gen_"); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 413 | if (auto Comdat = F.getComdat()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 414 | Array->setComdat(Comdat); |
| 415 | Array->setSection(getSectionName(Section)); |
| 416 | return Array; |
| 417 | } |
| 418 | void SanitizerCoverageModule::CreateFunctionLocalArrays(size_t NumGuards, |
| 419 | Function &F) { |
| 420 | if (Options.TracePCGuard) |
| 421 | FunctionGuardArray = CreateFunctionLocalArrayInSection( |
| 422 | NumGuards, F, Int32Ty, SanCovGuardsSectionName); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 423 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 424 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 425 | bool SanitizerCoverageModule::InjectCoverage(Function &F, |
| 426 | ArrayRef<BasicBlock *> AllBlocks) { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 427 | if (AllBlocks.empty()) return false; |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 428 | switch (Options.CoverageType) { |
| 429 | case SanitizerCoverageOptions::SCK_None: |
| 430 | return false; |
| 431 | case SanitizerCoverageOptions::SCK_Function: |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 432 | CreateFunctionLocalArrays(1, F); |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 433 | InjectCoverageAtBlock(F, F.getEntryBlock(), 0); |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 434 | return true; |
| 435 | default: { |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 436 | CreateFunctionLocalArrays(AllBlocks.size(), F); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 437 | for (size_t i = 0, N = AllBlocks.size(); i < N; i++) |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 438 | InjectCoverageAtBlock(F, *AllBlocks[i], i); |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 439 | return true; |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 440 | } |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 441 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // On every indirect call we call a run-time function |
| 445 | // __sanitizer_cov_indir_call* with two parameters: |
| 446 | // - callee address, |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 447 | // - global cache array that contains CacheSize pointers (zero-initialized). |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 448 | // The cache is used to speed up recording the caller-callee pairs. |
| 449 | // The address of the caller is passed implicitly via caller PC. |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 450 | // CacheSize is encoded in the name of the run-time function. |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 451 | void SanitizerCoverageModule::InjectCoverageForIndirectCalls( |
| 452 | Function &F, ArrayRef<Instruction *> IndirCalls) { |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 453 | if (IndirCalls.empty()) |
| 454 | return; |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 455 | assert(Options.TracePC || Options.TracePCGuard); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 456 | for (auto I : IndirCalls) { |
| 457 | IRBuilder<> IRB(I); |
| 458 | CallSite CS(I); |
| 459 | Value *Callee = CS.getCalledValue(); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 460 | if (isa<InlineAsm>(Callee)) |
| 461 | continue; |
Kostya Serebryany | c5d3d49 | 2017-04-19 22:42:11 +0000 | [diff] [blame] | 462 | IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy)); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 466 | // For every switch statement we insert a call: |
| 467 | // __sanitizer_cov_trace_switch(CondValue, |
| 468 | // {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... }) |
| 469 | |
| 470 | void SanitizerCoverageModule::InjectTraceForSwitch( |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 471 | Function &, ArrayRef<Instruction *> SwitchTraceTargets) { |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 472 | for (auto I : SwitchTraceTargets) { |
| 473 | if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { |
| 474 | IRBuilder<> IRB(I); |
| 475 | SmallVector<Constant *, 16> Initializers; |
| 476 | Value *Cond = SI->getCondition(); |
Kostya Serebryany | 2569118 | 2015-08-11 00:24:39 +0000 | [diff] [blame] | 477 | if (Cond->getType()->getScalarSizeInBits() > |
| 478 | Int64Ty->getScalarSizeInBits()) |
| 479 | continue; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 480 | Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases())); |
| 481 | Initializers.push_back( |
| 482 | ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits())); |
| 483 | if (Cond->getType()->getScalarSizeInBits() < |
| 484 | Int64Ty->getScalarSizeInBits()) |
| 485 | Cond = IRB.CreateIntCast(Cond, Int64Ty, false); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 486 | for (auto It : SI->cases()) { |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 487 | Constant *C = It.getCaseValue(); |
| 488 | if (C->getType()->getScalarSizeInBits() < |
| 489 | Int64Ty->getScalarSizeInBits()) |
| 490 | C = ConstantExpr::getCast(CastInst::ZExt, It.getCaseValue(), Int64Ty); |
| 491 | Initializers.push_back(C); |
| 492 | } |
Kostya Serebryany | f24e52c | 2016-12-27 21:20:06 +0000 | [diff] [blame] | 493 | std::sort(Initializers.begin() + 2, Initializers.end(), |
| 494 | [](const Constant *A, const Constant *B) { |
| 495 | return cast<ConstantInt>(A)->getLimitedValue() < |
| 496 | cast<ConstantInt>(B)->getLimitedValue(); |
| 497 | }); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 498 | ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size()); |
| 499 | GlobalVariable *GV = new GlobalVariable( |
| 500 | *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage, |
| 501 | ConstantArray::get(ArrayOfInt64Ty, Initializers), |
| 502 | "__sancov_gen_cov_switch_values"); |
| 503 | IRB.CreateCall(SanCovTraceSwitchFunction, |
| 504 | {Cond, IRB.CreatePointerCast(GV, Int64PtrTy)}); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 509 | void SanitizerCoverageModule::InjectTraceForDiv( |
| 510 | Function &, ArrayRef<BinaryOperator *> DivTraceTargets) { |
| 511 | for (auto BO : DivTraceTargets) { |
| 512 | IRBuilder<> IRB(BO); |
| 513 | Value *A1 = BO->getOperand(1); |
| 514 | if (isa<ConstantInt>(A1)) continue; |
| 515 | if (!A1->getType()->isIntegerTy()) |
| 516 | continue; |
| 517 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType()); |
| 518 | int CallbackIdx = TypeSize == 32 ? 0 : |
| 519 | TypeSize == 64 ? 1 : -1; |
| 520 | if (CallbackIdx < 0) continue; |
| 521 | auto Ty = Type::getIntNTy(*C, TypeSize); |
| 522 | IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx], |
| 523 | {IRB.CreateIntCast(A1, Ty, true)}); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void SanitizerCoverageModule::InjectTraceForGep( |
| 528 | Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) { |
| 529 | for (auto GEP : GepTraceTargets) { |
| 530 | IRBuilder<> IRB(GEP); |
| 531 | for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) |
Kostya Serebryany | 45c1447 | 2016-09-27 01:55:08 +0000 | [diff] [blame] | 532 | if (!isa<ConstantInt>(*I) && (*I)->getType()->isIntegerTy()) |
Kostya Serebryany | 5ac427b | 2016-08-30 01:12:10 +0000 | [diff] [blame] | 533 | IRB.CreateCall(SanCovTraceGepFunction, |
| 534 | {IRB.CreateIntCast(*I, IntptrTy, true)}); |
| 535 | } |
| 536 | } |
| 537 | |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 538 | void SanitizerCoverageModule::InjectTraceForCmp( |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 539 | Function &, ArrayRef<Instruction *> CmpTraceTargets) { |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 540 | for (auto I : CmpTraceTargets) { |
| 541 | if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) { |
| 542 | IRBuilder<> IRB(ICMP); |
| 543 | Value *A0 = ICMP->getOperand(0); |
| 544 | Value *A1 = ICMP->getOperand(1); |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 545 | if (!A0->getType()->isIntegerTy()) |
| 546 | continue; |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 547 | uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType()); |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 548 | int CallbackIdx = TypeSize == 8 ? 0 : |
| 549 | TypeSize == 16 ? 1 : |
| 550 | TypeSize == 32 ? 2 : |
| 551 | TypeSize == 64 ? 3 : -1; |
| 552 | if (CallbackIdx < 0) continue; |
Alexey Samsonov | 0a648a4 | 2015-05-06 21:35:25 +0000 | [diff] [blame] | 553 | // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1); |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 554 | auto Ty = Type::getIntNTy(*C, TypeSize); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 555 | IRB.CreateCall( |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 556 | SanCovTraceCmpFunction[CallbackIdx], |
| 557 | {IRB.CreateIntCast(A0, Ty, true), IRB.CreateIntCast(A1, Ty, true)}); |
Kostya Serebryany | f4e35cc | 2015-03-21 01:29:36 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 562 | void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB, |
Kostya Serebryany | 53b34c8 | 2017-05-31 18:27:33 +0000 | [diff] [blame] | 563 | size_t Idx) { |
Justin Bogner | 7ae63aa | 2015-08-14 17:03:45 +0000 | [diff] [blame] | 564 | BasicBlock::iterator IP = BB.getFirstInsertionPt(); |
Kostya Serebryany | d421db0 | 2015-01-03 00:54:43 +0000 | [diff] [blame] | 565 | bool IsEntryBB = &BB == &F.getEntryBlock(); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 566 | DebugLoc EntryLoc; |
| 567 | if (IsEntryBB) { |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 568 | if (auto SP = F.getSubprogram()) |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 569 | EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP); |
Reid Kleckner | a57d015 | 2015-08-14 16:45:42 +0000 | [diff] [blame] | 570 | // Keep static allocas and llvm.localescape calls in the entry block. Even |
| 571 | // if we aren't splitting the block, it's nice for allocas to be before |
| 572 | // calls. |
| 573 | IP = PrepareToSplitEntryBlock(BB, IP); |
Alexey Samsonov | 201733b | 2015-06-12 01:48:47 +0000 | [diff] [blame] | 574 | } else { |
| 575 | EntryLoc = IP->getDebugLoc(); |
| 576 | } |
| 577 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 578 | IRBuilder<> IRB(&*IP); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 579 | IRB.SetCurrentDebugLocation(EntryLoc); |
Kostya Serebryany | d4590c7 | 2016-02-17 21:34:43 +0000 | [diff] [blame] | 580 | if (Options.TracePC) { |
Kostya Serebryany | dd5c7f9 | 2016-07-14 17:59:01 +0000 | [diff] [blame] | 581 | IRB.CreateCall(SanCovTracePC); // gets the PC using GET_CALLER_PC. |
| 582 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
Renato Golin | cdf840fd | 2017-06-05 07:35:52 +0000 | [diff] [blame] | 583 | } else { |
| 584 | assert(Options.TracePCGuard); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 585 | auto GuardPtr = IRB.CreateIntToPtr( |
| 586 | IRB.CreateAdd(IRB.CreatePointerCast(FunctionGuardArray, IntptrTy), |
| 587 | ConstantInt::get(IntptrTy, Idx * 4)), |
| 588 | Int32PtrTy); |
Kostya Serebryany | da718e5 | 2016-09-14 01:39:35 +0000 | [diff] [blame] | 589 | IRB.CreateCall(SanCovTracePCGuard, GuardPtr); |
| 590 | IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge. |
Kostya Serebryany | 77cc729 | 2015-02-04 01:21:45 +0000 | [diff] [blame] | 591 | } |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 594 | std::string |
| 595 | SanitizerCoverageModule::getSectionName(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 596 | if (TargetTriple.getObjectFormat() == Triple::COFF) |
| 597 | return ".SCOV$M"; |
| 598 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 599 | return "__DATA,__" + Section; |
| 600 | return "__" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 603 | std::string |
| 604 | SanitizerCoverageModule::getSectionStart(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 605 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 606 | return "\1section$start$__DATA$__" + Section; |
| 607 | return "__start___" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 610 | std::string |
| 611 | SanitizerCoverageModule::getSectionEnd(const std::string &Section) const { |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 612 | if (TargetTriple.isOSBinFormatMachO()) |
Kostya Serebryany | aed6ba7 | 2017-06-02 23:13:44 +0000 | [diff] [blame] | 613 | return "\1section$end$__DATA$__" + Section; |
| 614 | return "__stop___" + Section; |
Marcos Pividori | db5a565 | 2017-02-03 01:08:06 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 618 | char SanitizerCoverageModule::ID = 0; |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 619 | INITIALIZE_PASS_BEGIN(SanitizerCoverageModule, "sancov", |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 620 | "SanitizerCoverage: TODO." |
| 621 | "ModulePass", |
| 622 | false, false) |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 623 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
George Karpenkov | 018472c | 2017-05-24 00:29:12 +0000 | [diff] [blame] | 624 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
Mike Aizatsky | 9056284 | 2016-02-27 05:50:40 +0000 | [diff] [blame] | 625 | INITIALIZE_PASS_END(SanitizerCoverageModule, "sancov", |
Mike Aizatsky | 759aca0 | 2016-03-18 23:29:29 +0000 | [diff] [blame] | 626 | "SanitizerCoverage: TODO." |
| 627 | "ModulePass", |
| 628 | false, false) |
Alexey Samsonov | 3514f27 | 2015-05-07 01:00:31 +0000 | [diff] [blame] | 629 | ModulePass *llvm::createSanitizerCoverageModulePass( |
| 630 | const SanitizerCoverageOptions &Options) { |
| 631 | return new SanitizerCoverageModule(Options); |
Kostya Serebryany | 29a18dc | 2014-11-11 22:14:37 +0000 | [diff] [blame] | 632 | } |