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 (CoverageLevel=1) 15 // or all blocks (CoverageLevel>=2): 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 CoverageLevel>=3 we also split critical edges this effectively 23 // instrumenting all edges. 24 // 25 // CoverageLevel>=4 add indirect call profiling implented as a function call. 26 // 27 // This coverage implementation provides very limited data: 28 // it only tells if a given function (block) was ever executed. No counters. 29 // But for many use cases this is what we need and the added slowdown small. 30 // 31 //===----------------------------------------------------------------------===// 32 33 #include "llvm/Transforms/Instrumentation.h" 34 #include "llvm/ADT/ArrayRef.h" 35 #include "llvm/ADT/SmallVector.h" 36 #include "llvm/IR/CallSite.h" 37 #include "llvm/IR/DataLayout.h" 38 #include "llvm/IR/Function.h" 39 #include "llvm/IR/IRBuilder.h" 40 #include "llvm/IR/InlineAsm.h" 41 #include "llvm/IR/LLVMContext.h" 42 #include "llvm/IR/MDBuilder.h" 43 #include "llvm/IR/Module.h" 44 #include "llvm/IR/Type.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/Debug.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include "llvm/Transforms/Scalar.h" 49 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 50 #include "llvm/Transforms/Utils/ModuleUtils.h" 51 52 using namespace llvm; 53 54 #define DEBUG_TYPE "sancov" 55 56 static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init"; 57 static const char *const kSanCovName = "__sanitizer_cov"; 58 static const char *const kSanCovWithCheckName = "__sanitizer_cov_with_check"; 59 static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16"; 60 static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter"; 61 static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block"; 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(1000)); 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 // Experimental 8-bit counters used as an additional search heuristic during 84 // coverage-guided fuzzing. 85 // The counters are not thread-friendly: 86 // - contention on these counters may cause significant slowdown; 87 // - the counter updates are racy and the results may be inaccurate. 88 // They are also inaccurate due to 8-bit integer overflow. 89 static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters", 90 cl::desc("Experimental 8-bit counters"), 91 cl::Hidden, cl::init(false)); 92 93 namespace { 94 95 class SanitizerCoverageModule : public ModulePass { 96 public: 97 SanitizerCoverageModule(int CoverageLevel = 0) 98 : ModulePass(ID), 99 CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {} 100 bool runOnModule(Module &M) override; 101 bool runOnFunction(Function &F); 102 static char ID; // Pass identification, replacement for typeid 103 const char *getPassName() const override { 104 return "SanitizerCoverageModule"; 105 } 106 107 private: 108 void InjectCoverageForIndirectCalls(Function &F, 109 ArrayRef<Instruction *> IndirCalls); 110 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks, 111 ArrayRef<Instruction *> IndirCalls); 112 void SetNoSanitizeMetada(Instruction *I); 113 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, bool UseCalls); 114 Function *SanCovFunction; 115 Function *SanCovWithCheckFunction; 116 Function *SanCovIndirCallFunction; 117 Function *SanCovModuleInit; 118 Function *SanCovTraceEnter, *SanCovTraceBB; 119 InlineAsm *EmptyAsm; 120 Type *IntptrTy; 121 LLVMContext *C; 122 123 GlobalVariable *GuardArray; 124 GlobalVariable *EightBitCounterArray; 125 126 int CoverageLevel; 127 }; 128 129 } // namespace 130 131 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) { 132 if (Function *F = dyn_cast<Function>(FuncOrBitcast)) 133 return F; 134 std::string Err; 135 raw_string_ostream Stream(Err); 136 Stream << "SanitizerCoverage interface function redefined: " 137 << *FuncOrBitcast; 138 report_fatal_error(Err); 139 } 140 141 bool SanitizerCoverageModule::runOnModule(Module &M) { 142 if (!CoverageLevel) return false; 143 C = &(M.getContext()); 144 auto &DL = M.getDataLayout(); 145 IntptrTy = Type::getIntNTy(*C, DL.getPointerSizeInBits()); 146 Type *VoidTy = Type::getVoidTy(*C); 147 IRBuilder<> IRB(*C); 148 Type *Int8PtrTy = PointerType::getUnqual(IRB.getInt8Ty()); 149 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); 150 151 Function *CtorFunc = 152 Function::Create(FunctionType::get(VoidTy, false), 153 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M); 154 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc)); 155 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority); 156 157 SanCovFunction = checkInterfaceFunction( 158 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr)); 159 SanCovWithCheckFunction = checkInterfaceFunction( 160 M.getOrInsertFunction(kSanCovWithCheckName, VoidTy, Int32PtrTy, nullptr)); 161 SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction( 162 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr)); 163 SanCovModuleInit = checkInterfaceFunction(M.getOrInsertFunction( 164 kSanCovModuleInitName, Type::getVoidTy(*C), Int32PtrTy, IntptrTy, 165 Int8PtrTy, Int8PtrTy, nullptr)); 166 SanCovModuleInit->setLinkage(Function::ExternalLinkage); 167 // We insert an empty inline asm after cov callbacks to avoid callback merge. 168 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), 169 StringRef(""), StringRef(""), 170 /*hasSideEffects=*/true); 171 172 if (ClExperimentalTracing) { 173 SanCovTraceEnter = checkInterfaceFunction( 174 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, Int32PtrTy, nullptr)); 175 SanCovTraceBB = checkInterfaceFunction( 176 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, Int32PtrTy, nullptr)); 177 } 178 179 // At this point we create a dummy array of guards because we don't 180 // know how many elements we will need. 181 Type *Int32Ty = IRB.getInt32Ty(); 182 Type *Int8Ty = IRB.getInt8Ty(); 183 184 GuardArray = 185 new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage, 186 nullptr, "__sancov_gen_cov_tmp"); 187 if (ClUse8bitCounters) 188 EightBitCounterArray = 189 new GlobalVariable(M, Int8Ty, false, GlobalVariable::ExternalLinkage, 190 nullptr, "__sancov_gen_cov_tmp"); 191 192 for (auto &F : M) 193 runOnFunction(F); 194 195 // Now we know how many elements we need. Create an array of guards 196 // with one extra element at the beginning for the size. 197 Type *Int32ArrayNTy = 198 ArrayType::get(Int32Ty, SanCovFunction->getNumUses() + 1); 199 GlobalVariable *RealGuardArray = new GlobalVariable( 200 M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage, 201 Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov"); 202 203 204 // Replace the dummy array with the real one. 205 GuardArray->replaceAllUsesWith( 206 IRB.CreatePointerCast(RealGuardArray, Int32PtrTy)); 207 GuardArray->eraseFromParent(); 208 209 GlobalVariable *RealEightBitCounterArray; 210 if (ClUse8bitCounters) { 211 // Make sure the array is 16-aligned. 212 static const int kCounterAlignment = 16; 213 Type *Int8ArrayNTy = 214 ArrayType::get(Int8Ty, RoundUpToAlignment(SanCovFunction->getNumUses(), 215 kCounterAlignment)); 216 RealEightBitCounterArray = new GlobalVariable( 217 M, Int8ArrayNTy, false, GlobalValue::PrivateLinkage, 218 Constant::getNullValue(Int8ArrayNTy), "__sancov_gen_cov_counter"); 219 RealEightBitCounterArray->setAlignment(kCounterAlignment); 220 EightBitCounterArray->replaceAllUsesWith( 221 IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy)); 222 EightBitCounterArray->eraseFromParent(); 223 } 224 225 // Create variable for module (compilation unit) name 226 Constant *ModNameStrConst = 227 ConstantDataArray::getString(M.getContext(), M.getName(), true); 228 GlobalVariable *ModuleName = 229 new GlobalVariable(M, ModNameStrConst->getType(), true, 230 GlobalValue::PrivateLinkage, ModNameStrConst); 231 232 // Call __sanitizer_cov_module_init 233 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator()); 234 IRB.CreateCall4( 235 SanCovModuleInit, IRB.CreatePointerCast(RealGuardArray, Int32PtrTy), 236 ConstantInt::get(IntptrTy, SanCovFunction->getNumUses()), 237 ClUse8bitCounters 238 ? IRB.CreatePointerCast(RealEightBitCounterArray, Int8PtrTy) 239 : Constant::getNullValue(Int8PtrTy), 240 IRB.CreatePointerCast(ModuleName, Int8PtrTy)); 241 return true; 242 } 243 244 bool SanitizerCoverageModule::runOnFunction(Function &F) { 245 if (F.empty()) return false; 246 if (F.getName().find(".module_ctor") != std::string::npos) 247 return false; // Should not instrument sanitizer init functions. 248 if (CoverageLevel >= 3) 249 SplitAllCriticalEdges(F); 250 SmallVector<Instruction*, 8> IndirCalls; 251 SmallVector<BasicBlock*, 16> AllBlocks; 252 for (auto &BB : F) { 253 AllBlocks.push_back(&BB); 254 if (CoverageLevel >= 4) 255 for (auto &Inst : BB) { 256 CallSite CS(&Inst); 257 if (CS && !CS.getCalledFunction()) 258 IndirCalls.push_back(&Inst); 259 } 260 } 261 InjectCoverage(F, AllBlocks, IndirCalls); 262 return true; 263 } 264 265 bool 266 SanitizerCoverageModule::InjectCoverage(Function &F, 267 ArrayRef<BasicBlock *> AllBlocks, 268 ArrayRef<Instruction *> IndirCalls) { 269 if (!CoverageLevel) return false; 270 271 if (CoverageLevel == 1) { 272 InjectCoverageAtBlock(F, F.getEntryBlock(), false); 273 } else { 274 for (auto BB : AllBlocks) 275 InjectCoverageAtBlock(F, *BB, 276 ClCoverageBlockThreshold < AllBlocks.size()); 277 } 278 InjectCoverageForIndirectCalls(F, IndirCalls); 279 return true; 280 } 281 282 // On every indirect call we call a run-time function 283 // __sanitizer_cov_indir_call* with two parameters: 284 // - callee address, 285 // - global cache array that contains kCacheSize pointers (zero-initialized). 286 // The cache is used to speed up recording the caller-callee pairs. 287 // The address of the caller is passed implicitly via caller PC. 288 // kCacheSize is encoded in the name of the run-time function. 289 void SanitizerCoverageModule::InjectCoverageForIndirectCalls( 290 Function &F, ArrayRef<Instruction *> IndirCalls) { 291 if (IndirCalls.empty()) return; 292 const int kCacheSize = 16; 293 const int kCacheAlignment = 64; // Align for better performance. 294 Type *Ty = ArrayType::get(IntptrTy, kCacheSize); 295 for (auto I : IndirCalls) { 296 IRBuilder<> IRB(I); 297 CallSite CS(I); 298 Value *Callee = CS.getCalledValue(); 299 if (dyn_cast<InlineAsm>(Callee)) continue; 300 GlobalVariable *CalleeCache = new GlobalVariable( 301 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage, 302 Constant::getNullValue(Ty), "__sancov_gen_callee_cache"); 303 CalleeCache->setAlignment(kCacheAlignment); 304 IRB.CreateCall2(SanCovIndirCallFunction, 305 IRB.CreatePointerCast(Callee, IntptrTy), 306 IRB.CreatePointerCast(CalleeCache, IntptrTy)); 307 } 308 } 309 310 void SanitizerCoverageModule::SetNoSanitizeMetada(Instruction *I) { 311 I->setMetadata( 312 I->getParent()->getParent()->getParent()->getMDKindID("nosanitize"), 313 MDNode::get(*C, None)); 314 } 315 316 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB, 317 bool UseCalls) { 318 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end(); 319 // Skip static allocas at the top of the entry block so they don't become 320 // dynamic when we split the block. If we used our optimized stack layout, 321 // then there will only be one alloca and it will come first. 322 for (; IP != BE; ++IP) { 323 AllocaInst *AI = dyn_cast<AllocaInst>(IP); 324 if (!AI || !AI->isStaticAlloca()) 325 break; 326 } 327 328 bool IsEntryBB = &BB == &F.getEntryBlock(); 329 DebugLoc EntryLoc = 330 IsEntryBB ? IP->getDebugLoc().getFnDebugLoc(*C) : IP->getDebugLoc(); 331 IRBuilder<> IRB(IP); 332 IRB.SetCurrentDebugLocation(EntryLoc); 333 SmallVector<Value *, 1> Indices; 334 Value *GuardP = IRB.CreateAdd( 335 IRB.CreatePointerCast(GuardArray, IntptrTy), 336 ConstantInt::get(IntptrTy, (1 + SanCovFunction->getNumUses()) * 4)); 337 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); 338 GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy); 339 if (UseCalls) { 340 IRB.CreateCall(SanCovWithCheckFunction, GuardP); 341 } else { 342 LoadInst *Load = IRB.CreateLoad(GuardP); 343 Load->setAtomic(Monotonic); 344 Load->setAlignment(4); 345 SetNoSanitizeMetada(Load); 346 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load); 347 Instruction *Ins = SplitBlockAndInsertIfThen( 348 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000)); 349 IRB.SetInsertPoint(Ins); 350 IRB.SetCurrentDebugLocation(EntryLoc); 351 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC. 352 IRB.CreateCall(SanCovFunction, GuardP); 353 IRB.CreateCall(EmptyAsm); // Avoids callback merge. 354 } 355 356 if(ClUse8bitCounters) { 357 IRB.SetInsertPoint(IP); 358 Value *P = IRB.CreateAdd( 359 IRB.CreatePointerCast(EightBitCounterArray, IntptrTy), 360 ConstantInt::get(IntptrTy, SanCovFunction->getNumUses() - 1)); 361 P = IRB.CreateIntToPtr(P, IRB.getInt8PtrTy()); 362 LoadInst *LI = IRB.CreateLoad(P); 363 Value *Inc = IRB.CreateAdd(LI, ConstantInt::get(IRB.getInt8Ty(), 1)); 364 StoreInst *SI = IRB.CreateStore(Inc, P); 365 SetNoSanitizeMetada(LI); 366 SetNoSanitizeMetada(SI); 367 } 368 369 if (ClExperimentalTracing) { 370 // Experimental support for tracing. 371 // Insert a callback with the same guard variable as used for coverage. 372 IRB.SetInsertPoint(IP); 373 IRB.CreateCall(IsEntryBB ? SanCovTraceEnter : SanCovTraceBB, GuardP); 374 } 375 } 376 377 char SanitizerCoverageModule::ID = 0; 378 INITIALIZE_PASS(SanitizerCoverageModule, "sancov", 379 "SanitizerCoverage: TODO." 380 "ModulePass", false, false) 381 ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) { 382 return new SanitizerCoverageModule(CoverageLevel); 383 } 384