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