SanitizerCoverage.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. //
  13. // We create a Guard variable with the same linkage
  14. // as the function and inject this code into the entry block (SCK_Function)
  15. // or all blocks (SCK_BB):
  16. // if (Guard < 0) {
  17. // __sanitizer_cov(&Guard);
  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. //
  22. // With SCK_Edge we also split critical edges this effectively
  23. // instrumenting all edges.
  24. //
  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. #include "llvm/Transforms/Instrumentation.h"
  31. #include "llvm/ADT/ArrayRef.h"
  32. #include "llvm/ADT/SmallVector.h"
  33. #include "llvm/IR/CallSite.h"
  34. #include "llvm/IR/DataLayout.h"
  35. #include "llvm/IR/DebugInfo.h"
  36. #include "llvm/IR/Function.h"
  37. #include "llvm/IR/IRBuilder.h"
  38. #include "llvm/IR/InlineAsm.h"
  39. #include "llvm/IR/LLVMContext.h"
  40. #include "llvm/IR/MDBuilder.h"
  41. #include "llvm/IR/Module.h"
  42. #include "llvm/IR/Type.h"
  43. #include "llvm/Support/CommandLine.h"
  44. #include "llvm/Support/Debug.h"
  45. #include "llvm/Support/raw_ostream.h"
  46. #include "llvm/Transforms/Scalar.h"
  47. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  48. #include "llvm/Transforms/Utils/ModuleUtils.h"
  49. using namespace llvm;
  50. #define DEBUG_TYPE "sancov"
  51. static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
  52. static const char *const kSanCovName = "__sanitizer_cov";
  53. static const char *const kSanCovWithCheckName = "__sanitizer_cov_with_check";
  54. static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
  55. static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
  56. static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
  57. static const char *const kSanCovTraceCmp = "__sanitizer_cov_trace_cmp";
  58. static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
  59. static const uint64_t kSanCtorAndDtorPriority = 2;
  60. static cl::opt<int> ClCoverageLevel("sanitizer-coverage-level",
  61. cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
  62. "3: all blocks and critical edges, "
  63. "4: above plus indirect calls"),
  64. cl::Hidden, cl::init(0));
  65. static cl::opt<unsigned> ClCoverageBlockThreshold(
  66. "sanitizer-coverage-block-threshold",
  67. cl::desc("Use a callback with a guard check inside it if there are"
  68. " more than this number of blocks."),
  69. cl::Hidden, cl::init(500));
  70. static cl::opt<bool>
  71. ClExperimentalTracing("sanitizer-coverage-experimental-tracing",
  72. cl::desc("Experimental basic-block tracing: insert "
  73. "callbacks at every basic block"),
  74. cl::Hidden, cl::init(false));
  75. static cl::opt<bool>
  76. ClExperimentalCMPTracing("sanitizer-coverage-experimental-trace-compares",
  77. cl::desc("Experimental tracing of CMP and similar "
  78. "instructions"),
  79. cl::Hidden, cl::init(false));
  80. // Experimental 8-bit counters used as an additional search heuristic during
  81. // coverage-guided fuzzing.
  82. // The counters are not thread-friendly:
  83. // - contention on these counters may cause significant slowdown;
  84. // - the counter updates are racy and the results may be inaccurate.
  85. // They are also inaccurate due to 8-bit integer overflow.
  86. static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters",
  87. cl::desc("Experimental 8-bit counters"),
  88. cl::Hidden, cl::init(false));
  89. namespace {
  90. SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
  91. SanitizerCoverageOptions Res;
  92. switch (LegacyCoverageLevel) {
  93. case 0:
  94. Res.CoverageType = SanitizerCoverageOptions::SCK_None;
  95. break;
  96. case 1:
  97. Res.CoverageType = SanitizerCoverageOptions::SCK_Function;
  98. break;
  99. case 2:
  100. Res.CoverageType = SanitizerCoverageOptions::SCK_BB;
  101. break;
  102. case 3:
  103. Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
  104. break;
  105. case 4:
  106. Res.CoverageType = SanitizerCoverageOptions::SCK_Edge;
  107. Res.IndirectCalls = true;
  108. break;
  109. }
  110. return Res;
  111. }
  112. SanitizerCoverageOptions OverrideFromCL(SanitizerCoverageOptions Options) {
  113. // Sets CoverageType and IndirectCalls.
  114. SanitizerCoverageOptions CLOpts = getOptions(ClCoverageLevel);
  115. Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
  116. Options.IndirectCalls |= CLOpts.IndirectCalls;
  117. Options.TraceBB |= ClExperimentalTracing;
  118. Options.TraceCmp |= ClExperimentalCMPTracing;
  119. Options.Use8bitCounters |= ClUse8bitCounters;
  120. return Options;
  121. }
  122. class SanitizerCoverageModule : public ModulePass {
  123. public:
  124. SanitizerCoverageModule(
  125. const SanitizerCoverageOptions &Options = SanitizerCoverageOptions())
  126. : ModulePass(ID), Options(OverrideFromCL(Options)) {}
  127. bool runOnModule(Module &M) override;
  128. bool runOnFunction(Function &F);
  129. static char ID; // Pass identification, replacement for typeid
  130. const char *getPassName() const override {
  131. return "SanitizerCoverageModule";
  132. }
  133. private:
  134. void InjectCoverageForIndirectCalls(Function &F,
  135. ArrayRef<Instruction *> IndirCalls);
  136. void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets);
  137. bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks);
  138. void SetNoSanitizeMetadata(Instruction *I);
  139. void InjectCoverageAtBlock(Function &F, BasicBlock &BB, bool UseCalls);
  140. unsigned NumberOfInstrumentedBlocks() {
  141. return SanCovFunction->getNumUses() + SanCovWithCheckFunction->getNumUses();
  142. }
  143. Function *SanCovFunction;
  144. Function *SanCovWithCheckFunction;
  145. Function *SanCovIndirCallFunction;
  146. Function *SanCovTraceEnter, *SanCovTraceBB;
  147. Function *SanCovTraceCmpFunction;
  148. InlineAsm *EmptyAsm;
  149. Type *IntptrTy, *Int64Ty;
  150. LLVMContext *C;
  151. const DataLayout *DL;
  152. GlobalVariable *GuardArray;
  153. GlobalVariable *EightBitCounterArray;
  154. SanitizerCoverageOptions Options;
  155. };
  156. } // namespace
  157. bool SanitizerCoverageModule::runOnModule(Module &M) {
  158. if (Options.CoverageType == SanitizerCoverageOptions::SCK_None)
  159. return false;
  160. C = &(M.getContext());
  161. DL = &M.getDataLayout();
  162. IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
  163. Type *VoidTy = Type::getVoidTy(*C);
  164. IRBuilder<> IRB(*C);
  165. Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty());
  166. Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
  167. Int64Ty = IRB.getInt64Ty();
  168. SanCovFunction = checkSanitizerInterfaceFunction(
  169. M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
  170. SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
  171. M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
  172. SanCovIndirCallFunction =
  173. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  174. kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
  175. SanCovTraceCmpFunction =
  176. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  177. kSanCovTraceCmp, VoidTy, Int64Ty, Int64Ty, Int64Ty, nullptr));
  178. // We insert an empty inline asm after cov callbacks to avoid callback merge.
  179. EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
  180. StringRef(""), StringRef(""),
  181. /*hasSideEffects=*/true);
  182. if (Options.TraceBB) {
  183. SanCovTraceEnter = checkSanitizerInterfaceFunction(
  184. M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr));
  185. SanCovTraceBB = checkSanitizerInterfaceFunction(
  186. M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr));
  187. }
  188. // At this point we create a dummy array of guards because we don't
  189. // know how many elements we will need.
  190. Type *Int32Ty = IRB.getInt32Ty();
  191. Type *Int8Ty = IRB.getInt8Ty();
  192. GuardArray =
  193. new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
  194. nullptr, "__sancov_gen_cov_tmp");
  195. if (Options.Use8bitCounters)
  196. EightBitCounterArray =
  197. new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage,
  198. nullptr, "__sancov_gen_cov_tmp");
  199. for (auto &F : M)
  200. runOnFunction(F);
  201. auto N = NumberOfInstrumentedBlocks();
  202. // Now we know how many elements we need. Create an array of guards
  203. // with one extra element at the beginning for the size.
  204. Type *Int32ArrayNTy = ArrayType::get(Int32Ty, N + 1);
  205. GlobalVariable *RealGuardArray = new GlobalVariable(
  206. M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
  207. Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
  208. // Replace the dummy array with the real one.
  209. GuardArray->replaceAllUsesWith(
  210. IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
  211. GuardArray->eraseFromParent();
  212. GlobalVariable *RealEightBitCounterArray;
  213. if (Options.Use8bitCounters) {
  214. // Make sure the array is 16-aligned.
  215. static const int kCounterAlignment = 16;
  216. Type *Int8ArrayNTy =
  217. ArrayType::get(Int8Ty, RoundUpToAlignment(N, kCounterAlignment));
  218. RealEightBitCounterArray = new GlobalVariable(
  219. M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage,
  220. Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter");
  221. RealEightBitCounterArray->setAlignment(kCounterAlignment);
  222. EightBitCounterArray->replaceAllUsesWith(
  223. IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy));
  224. EightBitCounterArray->eraseFromParent();
  225. }
  226. // Create variable for module (compilation unit) name
  227. Constant *ModNameStrConst =
  228. ConstantDataArray::getString(M.getContext(), M.getName(), true);
  229. GlobalVariable *ModuleName =
  230. new GlobalVariable(M, ModNameStrConst->getType(), true,
  231. GlobalValue::PrivateLinkage, ModNameStrConst);
  232. Function *CtorFunc;
  233. std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
  234. M, kSanCovModuleCtorName, kSanCovModuleInitName,
  235. {Int32PtrTy, IntptrTy, Int8PtrTy, Int8PtrTy},
  236. {IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
  237. ConstantInt::get(IntptrTy, N),
  238. Options.Use8bitCounters
  239. ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)
  240. : Constant::getNullValue(Int8PtrTy),
  241. IRB.CreatePointerCast(ModuleName, Int8PtrTy)});
  242. appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
  243. return true;
  244. }
  245. bool SanitizerCoverageModule::runOnFunction(Function &F) {
  246. if (F.empty()) return false;
  247. if (F.getName().find(".module_ctor") != std::string::npos)
  248. return false; // Should not instrument sanitizer init functions.
  249. if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge)
  250. SplitAllCriticalEdges(F);
  251. SmallVector<Instruction*, 8> IndirCalls;
  252. SmallVector<BasicBlock*, 16> AllBlocks;
  253. SmallVector<Instruction*, 8> CmpTraceTargets;
  254. for (auto &BB : F) {
  255. AllBlocks.push_back(&BB);
  256. for (auto &Inst : BB) {
  257. if (Options.IndirectCalls) {
  258. CallSite CS(&Inst);
  259. if (CS && !CS.getCalledFunction())
  260. IndirCalls.push_back(&Inst);
  261. }
  262. if (Options.TraceCmp && isa<ICmpInst>(&Inst))
  263. CmpTraceTargets.push_back(&Inst);
  264. }
  265. }
  266. InjectCoverage(F, AllBlocks);
  267. InjectCoverageForIndirectCalls(F, IndirCalls);
  268. InjectTraceForCmp(F, CmpTraceTargets);
  269. return true;
  270. }
  271. bool SanitizerCoverageModule::InjectCoverage(Function &F,
  272. ArrayRef<BasicBlock *> AllBlocks) {
  273. switch (Options.CoverageType) {
  274. case SanitizerCoverageOptions::SCK_None:
  275. return false;
  276. case SanitizerCoverageOptions::SCK_Function:
  277. InjectCoverageAtBlock(F, F.getEntryBlock(), false);
  278. return true;
  279. default: {
  280. bool UseCalls = ClCoverageBlockThreshold < AllBlocks.size();
  281. for (auto BB : AllBlocks)
  282. InjectCoverageAtBlock(F, *BB, UseCalls);
  283. return true;
  284. }
  285. }
  286. }
  287. // On every indirect call we call a run-time function
  288. // __sanitizer_cov_indir_call* with two parameters:
  289. // - callee address,
  290. // - global cache array that contains kCacheSize pointers (zero-initialized).
  291. // The cache is used to speed up recording the caller-callee pairs.
  292. // The address of the caller is passed implicitly via caller PC.
  293. // kCacheSize is encoded in the name of the run-time function.
  294. void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
  295. Function &F, ArrayRef<Instruction *> IndirCalls) {
  296. if (IndirCalls.empty()) return;
  297. const int kCacheSize = 16;
  298. const int kCacheAlignment = 64; // Align for better performance.
  299. Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
  300. for (auto I : IndirCalls) {
  301. IRBuilder<> IRB(I);
  302. CallSite CS(I);
  303. Value *Callee = CS.getCalledValue();
  304. if (isa<InlineAsm>(Callee)) continue;
  305. GlobalVariable *CalleeCache = new GlobalVariable(
  306. *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
  307. Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
  308. CalleeCache->setAlignment(kCacheAlignment);
  309. IRB.CreateCall(SanCovIndirCallFunction,
  310. {IRB.CreatePointerCast(Callee, IntptrTy),
  311. IRB.CreatePointerCast(CalleeCache, IntptrTy)});
  312. }
  313. }
  314. void SanitizerCoverageModule::InjectTraceForCmp(
  315. Function &F, ArrayRef<Instruction *> CmpTraceTargets) {
  316. for (auto I : CmpTraceTargets) {
  317. if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
  318. IRBuilder<> IRB(ICMP);
  319. Value *A0 = ICMP->getOperand(0);
  320. Value *A1 = ICMP->getOperand(1);
  321. if (!A0->getType()->isIntegerTy()) continue;
  322. uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
  323. // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
  324. IRB.CreateCall(
  325. SanCovTraceCmpFunction,
  326. {ConstantInt::get(Int64Ty, (TypeSize << 32) | ICMP->getPredicate()),
  327. IRB.CreateIntCast(A0, Int64Ty, true),
  328. IRB.CreateIntCast(A1, Int64Ty, true)});
  329. }
  330. }
  331. }
  332. void SanitizerCoverageModule::SetNoSanitizeMetadata(Instruction *I) {
  333. I->setMetadata(
  334. I->getParent()->getParent()->getParent()->getMDKindID("nosanitize"),
  335. MDNode::get(*C, None));
  336. }
  337. void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
  338. bool UseCalls) {
  339. // Don't insert coverage for unreachable blocks: we will never call
  340. // __sanitizer_cov() for them, so counting them in
  341. // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
  342. // percentage. Also, unreachable instructions frequently have no debug
  343. // locations.
  344. if (isa<UnreachableInst>(BB.getTerminator()))
  345. return;
  346. BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
  347. // Skip static allocas at the top of the entry block so they don't become
  348. // dynamic when we split the block. If we used our optimized stack layout,
  349. // then there will only be one alloca and it will come first.
  350. for (; IP != BE; ++IP) {
  351. AllocaInst *AI = dyn_cast<AllocaInst>(IP);
  352. if (!AI || !AI->isStaticAlloca())
  353. break;
  354. }
  355. bool IsEntryBB = &BB == &F.getEntryBlock();
  356. DebugLoc EntryLoc;
  357. if (IsEntryBB) {
  358. if (auto SP = getDISubprogram(&F))
  359. EntryLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
  360. } else {
  361. EntryLoc = IP->getDebugLoc();
  362. }
  363. IRBuilder<> IRB(IP);
  364. IRB.SetCurrentDebugLocation(EntryLoc);
  365. SmallVector<Value *, 1> Indices;
  366. Value *GuardP = IRB.CreateAdd(
  367. IRB.CreatePointerCast(GuardArray, IntptrTy),
  368. ConstantInt::get(IntptrTy, (1 + NumberOfInstrumentedBlocks()) * 4));
  369. Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
  370. GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
  371. if (UseCalls) {
  372. IRB.CreateCall(SanCovWithCheckFunction, GuardP);
  373. } else {
  374. LoadInst *Load = IRB.CreateLoad(GuardP);
  375. Load->setAtomic(Monotonic);
  376. Load->setAlignment(4);
  377. SetNoSanitizeMetadata(Load);
  378. Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
  379. Instruction *Ins = SplitBlockAndInsertIfThen(
  380. Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
  381. IRB.SetInsertPoint(Ins);
  382. IRB.SetCurrentDebugLocation(EntryLoc);
  383. // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
  384. IRB.CreateCall(SanCovFunction, GuardP);
  385. IRB.CreateCall(EmptyAsm, {}); // Avoids callback merge.
  386. }
  387. if (Options.Use8bitCounters) {
  388. IRB.SetInsertPoint(IP);
  389. Value *P = IRB.CreateAdd(
  390. IRB.CreatePointerCast(EightBitCounterArray, IntptrTy),
  391. ConstantInt::get(IntptrTy, NumberOfInstrumentedBlocks() - 1));
  392. P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy());
  393. LoadInst *LI = IRB.CreateLoad(P);
  394. Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1));
  395. StoreInst *SI = IRB.CreateStore(Inc, P);
  396. SetNoSanitizeMetadata(LI);
  397. SetNoSanitizeMetadata(SI);
  398. }
  399. if (Options.TraceBB) {
  400. // Experimental support for tracing.
  401. // Insert a callback with the same guard variable as used for coverage.
  402. IRB.SetInsertPoint(IP);
  403. IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP);
  404. }
  405. }
  406. char SanitizerCoverageModule::ID = 0;
  407. INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
  408. "SanitizerCoverage: TODO."
  409. "ModulePass", false, false)
  410. ModulePass *llvm::createSanitizerCoverageModulePass(
  411. const SanitizerCoverageOptions &Options) {
  412. return new SanitizerCoverageModule(Options);
  413. }