1 //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===// 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 // Trace PCs. 10 // This module implements __sanitizer_cov_trace_pc_guard[_init], 11 // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "FuzzerTracePC.h" 16 #include "FuzzerCorpus.h" 17 #include "FuzzerDefs.h" 18 #include "FuzzerDictionary.h" 19 #include "FuzzerExtFunctions.h" 20 #include "FuzzerIO.h" 21 #include "FuzzerUtil.h" 22 #include "FuzzerValueBitMap.h" 23 #include <set> 24 25 // The coverage counters and PCs. 26 // These are declared as global variables named "__sancov_*" to simplify 27 // experiments with inlined instrumentation. 28 alignas(64) ATTRIBUTE_INTERFACE 29 uint8_t __sancov_trace_pc_guard_8bit_counters[fuzzer::TracePC::kNumPCs]; 30 31 ATTRIBUTE_INTERFACE 32 uintptr_t __sancov_trace_pc_pcs[fuzzer::TracePC::kNumPCs]; 33 34 // Used by -fsanitize-coverage=stack-depth to track stack depth 35 ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) 36 thread_local uintptr_t __sancov_lowest_stack; 37 38 namespace fuzzer { 39 40 TracePC TPC; 41 42 uint8_t *TracePC::Counters() const { 43 return __sancov_trace_pc_guard_8bit_counters; 44 } 45 46 uintptr_t *TracePC::PCs() const { 47 return __sancov_trace_pc_pcs; 48 } 49 50 size_t TracePC::GetTotalPCCoverage() { 51 if (ObservedPCs.size()) 52 return ObservedPCs.size(); 53 size_t Res = 0; 54 for (size_t i = 1, N = GetNumPCs(); i < N; i++) 55 if (PCs()[i]) 56 Res++; 57 return Res; 58 } 59 60 template<class CallBack> 61 void TracePC::IterateInline8bitCounters(CallBack CB) const { 62 if (NumInline8bitCounters && NumInline8bitCounters == NumPCsInPCTables) { 63 size_t CounterIdx = 0; 64 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { 65 uint8_t *Beg = ModuleCounters[i].Start; 66 size_t Size = ModuleCounters[i].Stop - Beg; 67 assert(Size == (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); 68 for (size_t j = 0; j < Size; j++, CounterIdx++) 69 CB(i, j, CounterIdx); 70 } 71 } 72 } 73 74 // Initializes unstable counters by copying Inline8bitCounters to unstable 75 // counters. 76 void TracePC::InitializeUnstableCounters() { 77 IterateInline8bitCounters([&](int i, int j, int UnstableIdx) { 78 if (UnstableCounters[UnstableIdx] != kUnstableCounter) 79 UnstableCounters[UnstableIdx] = ModuleCounters[i].Start[j]; 80 }); 81 } 82 83 // Compares the current counters with counters from previous runs 84 // and records differences as unstable edges. 85 void TracePC::UpdateUnstableCounters() { 86 IterateInline8bitCounters([&](int i, int j, int UnstableIdx) { 87 if (ModuleCounters[i].Start[j] != UnstableCounters[UnstableIdx]) 88 UnstableCounters[UnstableIdx] = kUnstableCounter; 89 }); 90 } 91 92 void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) { 93 if (Start == Stop) return; 94 if (NumModulesWithInline8bitCounters && 95 ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return; 96 assert(NumModulesWithInline8bitCounters < 97 sizeof(ModuleCounters) / sizeof(ModuleCounters[0])); 98 ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop}; 99 NumInline8bitCounters += Stop - Start; 100 } 101 102 void TracePC::HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop) { 103 const PCTableEntry *B = reinterpret_cast<const PCTableEntry *>(Start); 104 const PCTableEntry *E = reinterpret_cast<const PCTableEntry *>(Stop); 105 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return; 106 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0])); 107 ModulePCTable[NumPCTables++] = {B, E}; 108 NumPCsInPCTables += E - B; 109 } 110 111 void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) { 112 if (Start == Stop || *Start) return; 113 assert(NumModules < sizeof(Modules) / sizeof(Modules[0])); 114 for (uint32_t *P = Start; P < Stop; P++) { 115 NumGuards++; 116 if (NumGuards == kNumPCs) { 117 RawPrint( 118 "WARNING: The binary has too many instrumented PCs.\n" 119 " You may want to reduce the size of the binary\n" 120 " for more efficient fuzzing and precise coverage data\n"); 121 } 122 *P = NumGuards % kNumPCs; 123 } 124 Modules[NumModules].Start = Start; 125 Modules[NumModules].Stop = Stop; 126 NumModules++; 127 } 128 129 void TracePC::PrintModuleInfo() { 130 if (NumGuards) { 131 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards); 132 for (size_t i = 0; i < NumModules; i++) 133 Printf("%zd [%p, %p), ", Modules[i].Stop - Modules[i].Start, 134 Modules[i].Start, Modules[i].Stop); 135 Printf("\n"); 136 } 137 if (NumModulesWithInline8bitCounters) { 138 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ", 139 NumModulesWithInline8bitCounters, NumInline8bitCounters); 140 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) 141 Printf("%zd [%p, %p), ", ModuleCounters[i].Stop - ModuleCounters[i].Start, 142 ModuleCounters[i].Start, ModuleCounters[i].Stop); 143 Printf("\n"); 144 } 145 if (NumPCTables) { 146 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables, 147 NumPCsInPCTables); 148 for (size_t i = 0; i < NumPCTables; i++) { 149 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start, 150 ModulePCTable[i].Start, ModulePCTable[i].Stop); 151 } 152 Printf("\n"); 153 154 if ((NumGuards && NumGuards != NumPCsInPCTables) || 155 (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables)) { 156 Printf("ERROR: The size of coverage PC tables does not match the\n" 157 "number of instrumented PCs. This might be a compiler bug,\n" 158 "please contact the libFuzzer developers.\n" 159 "Also check https://bugs.llvm.org/show_bug.cgi?id=34636\n" 160 "for possible workarounds (tl;dr: don't use the old GNU ld)\n"); 161 _Exit(1); 162 } 163 } 164 if (size_t NumExtraCounters = ExtraCountersEnd() - ExtraCountersBegin()) 165 Printf("INFO: %zd Extra Counters\n", NumExtraCounters); 166 } 167 168 ATTRIBUTE_NO_SANITIZE_ALL 169 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { 170 const uintptr_t kBits = 12; 171 const uintptr_t kMask = (1 << kBits) - 1; 172 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits); 173 ValueProfileMap.AddValueModPrime(Idx); 174 } 175 176 void TracePC::UpdateObservedPCs() { 177 Vector<uintptr_t> CoveredFuncs; 178 auto ObservePC = [&](uintptr_t PC) { 179 if (ObservedPCs.insert(PC).second && DoPrintNewPCs) { 180 PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p", PC + 1); 181 Printf("\n"); 182 } 183 }; 184 185 auto Observe = [&](const PCTableEntry &TE) { 186 if (TE.PCFlags & 1) 187 if (ObservedFuncs.insert(TE.PC).second && NumPrintNewFuncs) 188 CoveredFuncs.push_back(TE.PC); 189 ObservePC(TE.PC); 190 }; 191 192 if (NumPCsInPCTables) { 193 if (NumInline8bitCounters == NumPCsInPCTables) { 194 IterateInline8bitCounters([&](int i, int j, int CounterIdx) { 195 if (ModuleCounters[i].Start[j]) 196 Observe(ModulePCTable[i].Start[j]); 197 }); 198 } else if (NumGuards == NumPCsInPCTables) { 199 size_t GuardIdx = 1; 200 for (size_t i = 0; i < NumModules; i++) { 201 uint32_t *Beg = Modules[i].Start; 202 size_t Size = Modules[i].Stop - Beg; 203 assert(Size == 204 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); 205 for (size_t j = 0; j < Size; j++, GuardIdx++) 206 if (Counters()[GuardIdx]) 207 Observe(ModulePCTable[i].Start[j]); 208 } 209 } 210 } 211 212 for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N; i++) { 213 Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size()); 214 PrintPC("%p %F %L", "%p", CoveredFuncs[i] + 1); 215 Printf("\n"); 216 } 217 } 218 219 inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) { 220 // TODO: this implementation is x86 only. 221 // see sanitizer_common GetPreviousInstructionPc for full implementation. 222 return PC - 1; 223 } 224 225 inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) { 226 // TODO: this implementation is x86 only. 227 // see sanitizer_common GetPreviousInstructionPc for full implementation. 228 return PC + 1; 229 } 230 231 static std::string GetModuleName(uintptr_t PC) { 232 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++? 233 void *OffsetRaw = nullptr; 234 if (!EF->__sanitizer_get_module_and_offset_for_pc( 235 reinterpret_cast<void *>(PC), ModulePathRaw, 236 sizeof(ModulePathRaw), &OffsetRaw)) 237 return ""; 238 return ModulePathRaw; 239 } 240 241 template<class CallBack> 242 void TracePC::IterateCoveredFunctions(CallBack CB) { 243 for (size_t i = 0; i < NumPCTables; i++) { 244 auto &M = ModulePCTable[i]; 245 assert(M.Start < M.Stop); 246 auto ModuleName = GetModuleName(M.Start->PC); 247 for (auto NextFE = M.Start; NextFE < M.Stop; ) { 248 auto FE = NextFE; 249 assert((FE->PCFlags & 1) && "Not a function entry point"); 250 do { 251 NextFE++; 252 } while (NextFE < M.Stop && !(NextFE->PCFlags & 1)); 253 if (ObservedFuncs.count(FE->PC)) 254 CB(FE, NextFE); 255 } 256 } 257 } 258 259 void TracePC::SetFocusFunction(const std::string &FuncName) { 260 // This function should be called once. 261 assert(FocusFunction.first > NumModulesWithInline8bitCounters); 262 if (FuncName.empty()) 263 return; 264 for (size_t M = 0; M < NumModulesWithInline8bitCounters; M++) { 265 auto &PCTE = ModulePCTable[M]; 266 size_t N = PCTE.Stop - PCTE.Start; 267 for (size_t I = 0; I < N; I++) { 268 if (!(PCTE.Start[I].PCFlags & 1)) continue; // not a function entry. 269 auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC)); 270 if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ') 271 Name = Name.substr(3, std::string::npos); 272 if (FuncName != Name) continue; 273 Printf("INFO: Focus function is set to '%s'\n", Name.c_str()); 274 FocusFunction = {M, I}; 275 return; 276 } 277 } 278 } 279 280 bool TracePC::ObservedFocusFunction() { 281 size_t I = FocusFunction.first; 282 size_t J = FocusFunction.second; 283 if (I >= NumModulesWithInline8bitCounters) 284 return false; 285 auto &MC = ModuleCounters[I]; 286 size_t Size = MC.Stop - MC.Start; 287 if (J >= Size) 288 return false; 289 return MC.Start[J] != 0; 290 } 291 292 void TracePC::PrintCoverage() { 293 if (!EF->__sanitizer_symbolize_pc || 294 !EF->__sanitizer_get_module_and_offset_for_pc) { 295 Printf("INFO: __sanitizer_symbolize_pc or " 296 "__sanitizer_get_module_and_offset_for_pc is not available," 297 " not printing coverage\n"); 298 return; 299 } 300 Printf("COVERAGE:\n"); 301 auto CoveredFunctionCallback = [&](const PCTableEntry *First, const PCTableEntry *Last) { 302 assert(First < Last); 303 auto VisualizePC = GetNextInstructionPc(First->PC); 304 std::string FileStr = DescribePC("%s", VisualizePC); 305 if (!IsInterestingCoverageFile(FileStr)) return; 306 std::string FunctionStr = DescribePC("%F", VisualizePC); 307 std::string LineStr = DescribePC("%l", VisualizePC); 308 size_t Line = std::stoul(LineStr); 309 Vector<uintptr_t> UncoveredPCs; 310 for (auto TE = First; TE < Last; TE++) 311 if (!ObservedPCs.count(TE->PC)) 312 UncoveredPCs.push_back(TE->PC); 313 Printf("COVERED_FUNC: "); 314 UncoveredPCs.empty() 315 ? Printf("all") 316 : Printf("%zd/%zd", (Last - First) - UncoveredPCs.size(), Last - First); 317 Printf(" PCs covered %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(), 318 Line); 319 for (auto PC: UncoveredPCs) { 320 Printf(" UNCOVERED_PC: %s\n", 321 DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str()); 322 } 323 }; 324 325 IterateCoveredFunctions(CoveredFunctionCallback); 326 } 327 328 void TracePC::DumpCoverage() { 329 if (EF->__sanitizer_dump_coverage) { 330 Vector<uintptr_t> PCsCopy(GetNumPCs()); 331 for (size_t i = 0; i < GetNumPCs(); i++) 332 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0; 333 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size()); 334 } 335 } 336 337 void TracePC::PrintUnstableStats() { 338 size_t count = 0; 339 for (size_t i = 0; i < NumInline8bitCounters; i++) 340 if (UnstableCounters[i] == kUnstableCounter) 341 count++; 342 Printf("stat::stability_rate: %.2f\n", 343 100 - static_cast<float>(count * 100) / NumInline8bitCounters); 344 } 345 346 // Value profile. 347 // We keep track of various values that affect control flow. 348 // These values are inserted into a bit-set-based hash map. 349 // Every new bit in the map is treated as a new coverage. 350 // 351 // For memcmp/strcmp/etc the interesting value is the length of the common 352 // prefix of the parameters. 353 // For cmp instructions the interesting value is a XOR of the parameters. 354 // The interesting value is mixed up with the PC and is then added to the map. 355 356 ATTRIBUTE_NO_SANITIZE_ALL 357 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, 358 size_t n, bool StopAtZero) { 359 if (!n) return; 360 size_t Len = std::min(n, Word::GetMaxSize()); 361 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); 362 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); 363 uint8_t B1[Word::kMaxSize]; 364 uint8_t B2[Word::kMaxSize]; 365 // Copy the data into locals in this non-msan-instrumented function 366 // to avoid msan complaining further. 367 size_t Hash = 0; // Compute some simple hash of both strings. 368 for (size_t i = 0; i < Len; i++) { 369 B1[i] = A1[i]; 370 B2[i] = A2[i]; 371 size_t T = B1[i]; 372 Hash ^= (T << 8) | B2[i]; 373 } 374 size_t I = 0; 375 for (; I < Len; I++) 376 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) 377 break; 378 size_t PC = reinterpret_cast<size_t>(caller_pc); 379 size_t Idx = (PC & 4095) | (I << 12); 380 ValueProfileMap.AddValue(Idx); 381 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len)); 382 } 383 384 template <class T> 385 ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE 386 ATTRIBUTE_NO_SANITIZE_ALL 387 void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) { 388 uint64_t ArgXor = Arg1 ^ Arg2; 389 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65] 390 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance; 391 if (sizeof(T) == 4) 392 TORC4.Insert(ArgXor, Arg1, Arg2); 393 else if (sizeof(T) == 8) 394 TORC8.Insert(ArgXor, Arg1, Arg2); 395 // TODO: remove these flags and instead use all metrics at once. 396 if (UseValueProfileMask & 1) 397 ValueProfileMap.AddValue(Idx); 398 if (UseValueProfileMask & 2) 399 ValueProfileMap.AddValue( 400 PC * 64 + (Arg1 == Arg2 ? 0 : __builtin_clzll(Arg1 - Arg2) + 1)); 401 if (UseValueProfileMask & 4) // alternative way to use the hamming distance 402 ValueProfileMap.AddValue(PC * 64 + ArgDistance); 403 } 404 405 static size_t InternalStrnlen(const char *S, size_t MaxLen) { 406 size_t Len = 0; 407 for (; Len < MaxLen && S[Len]; Len++) {} 408 return Len; 409 } 410 411 // Finds min of (strlen(S1), strlen(S2)). 412 // Needed bacause one of these strings may actually be non-zero terminated. 413 static size_t InternalStrnlen2(const char *S1, const char *S2) { 414 size_t Len = 0; 415 for (; S1[Len] && S2[Len]; Len++) {} 416 return Len; 417 } 418 419 void TracePC::ClearInlineCounters() { 420 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { 421 uint8_t *Beg = ModuleCounters[i].Start; 422 size_t Size = ModuleCounters[i].Stop - Beg; 423 memset(Beg, 0, Size); 424 } 425 } 426 427 ATTRIBUTE_NO_SANITIZE_ALL 428 void TracePC::RecordInitialStack() { 429 int stack; 430 __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack); 431 } 432 433 uintptr_t TracePC::GetMaxStackOffset() const { 434 return InitialStack - __sancov_lowest_stack; // Stack grows down 435 } 436 437 } // namespace fuzzer 438 439 extern "C" { 440 ATTRIBUTE_INTERFACE 441 ATTRIBUTE_NO_SANITIZE_ALL 442 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) { 443 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 444 uint32_t Idx = *Guard; 445 __sancov_trace_pc_pcs[Idx] = PC; 446 __sancov_trace_pc_guard_8bit_counters[Idx]++; 447 } 448 449 // Best-effort support for -fsanitize-coverage=trace-pc, which is available 450 // in both Clang and GCC. 451 ATTRIBUTE_INTERFACE 452 ATTRIBUTE_NO_SANITIZE_ALL 453 void __sanitizer_cov_trace_pc() { 454 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 455 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1); 456 __sancov_trace_pc_pcs[Idx] = PC; 457 __sancov_trace_pc_guard_8bit_counters[Idx]++; 458 } 459 460 ATTRIBUTE_INTERFACE 461 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) { 462 fuzzer::TPC.HandleInit(Start, Stop); 463 } 464 465 ATTRIBUTE_INTERFACE 466 void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) { 467 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop); 468 } 469 470 ATTRIBUTE_INTERFACE 471 void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, 472 const uintptr_t *pcs_end) { 473 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end); 474 } 475 476 ATTRIBUTE_INTERFACE 477 ATTRIBUTE_NO_SANITIZE_ALL 478 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { 479 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 480 fuzzer::TPC.HandleCallerCallee(PC, Callee); 481 } 482 483 ATTRIBUTE_INTERFACE 484 ATTRIBUTE_NO_SANITIZE_ALL 485 ATTRIBUTE_TARGET_POPCNT 486 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { 487 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 488 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 489 } 490 491 ATTRIBUTE_INTERFACE 492 ATTRIBUTE_NO_SANITIZE_ALL 493 ATTRIBUTE_TARGET_POPCNT 494 // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic 495 // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however, 496 // should be changed later to make full use of instrumentation. 497 void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) { 498 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 499 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 500 } 501 502 ATTRIBUTE_INTERFACE 503 ATTRIBUTE_NO_SANITIZE_ALL 504 ATTRIBUTE_TARGET_POPCNT 505 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { 506 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 507 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 508 } 509 510 ATTRIBUTE_INTERFACE 511 ATTRIBUTE_NO_SANITIZE_ALL 512 ATTRIBUTE_TARGET_POPCNT 513 void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) { 514 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 515 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 516 } 517 518 ATTRIBUTE_INTERFACE 519 ATTRIBUTE_NO_SANITIZE_ALL 520 ATTRIBUTE_TARGET_POPCNT 521 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) { 522 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 523 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 524 } 525 526 ATTRIBUTE_INTERFACE 527 ATTRIBUTE_NO_SANITIZE_ALL 528 ATTRIBUTE_TARGET_POPCNT 529 void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) { 530 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 531 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 532 } 533 534 ATTRIBUTE_INTERFACE 535 ATTRIBUTE_NO_SANITIZE_ALL 536 ATTRIBUTE_TARGET_POPCNT 537 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) { 538 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 539 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 540 } 541 542 ATTRIBUTE_INTERFACE 543 ATTRIBUTE_NO_SANITIZE_ALL 544 ATTRIBUTE_TARGET_POPCNT 545 void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) { 546 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 547 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 548 } 549 550 ATTRIBUTE_INTERFACE 551 ATTRIBUTE_NO_SANITIZE_ALL 552 ATTRIBUTE_TARGET_POPCNT 553 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { 554 uint64_t N = Cases[0]; 555 uint64_t ValSizeInBits = Cases[1]; 556 uint64_t *Vals = Cases + 2; 557 // Skip the most common and the most boring case. 558 if (Vals[N - 1] < 256 && Val < 256) 559 return; 560 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 561 size_t i; 562 uint64_t Token = 0; 563 for (i = 0; i < N; i++) { 564 Token = Val ^ Vals[i]; 565 if (Val < Vals[i]) 566 break; 567 } 568 569 if (ValSizeInBits == 16) 570 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0)); 571 else if (ValSizeInBits == 32) 572 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0)); 573 else 574 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0)); 575 } 576 577 ATTRIBUTE_INTERFACE 578 ATTRIBUTE_NO_SANITIZE_ALL 579 ATTRIBUTE_TARGET_POPCNT 580 void __sanitizer_cov_trace_div4(uint32_t Val) { 581 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 582 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0); 583 } 584 585 ATTRIBUTE_INTERFACE 586 ATTRIBUTE_NO_SANITIZE_ALL 587 ATTRIBUTE_TARGET_POPCNT 588 void __sanitizer_cov_trace_div8(uint64_t Val) { 589 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 590 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0); 591 } 592 593 ATTRIBUTE_INTERFACE 594 ATTRIBUTE_NO_SANITIZE_ALL 595 ATTRIBUTE_TARGET_POPCNT 596 void __sanitizer_cov_trace_gep(uintptr_t Idx) { 597 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 598 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0); 599 } 600 601 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 602 void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, 603 const void *s2, size_t n, int result) { 604 if (!fuzzer::RunningUserCallback) return; 605 if (result == 0) return; // No reason to mutate. 606 if (n <= 1) return; // Not interesting. 607 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false); 608 } 609 610 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 611 void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, 612 const char *s2, size_t n, int result) { 613 if (!fuzzer::RunningUserCallback) return; 614 if (result == 0) return; // No reason to mutate. 615 size_t Len1 = fuzzer::InternalStrnlen(s1, n); 616 size_t Len2 = fuzzer::InternalStrnlen(s2, n); 617 n = std::min(n, Len1); 618 n = std::min(n, Len2); 619 if (n <= 1) return; // Not interesting. 620 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true); 621 } 622 623 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 624 void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, 625 const char *s2, int result) { 626 if (!fuzzer::RunningUserCallback) return; 627 if (result == 0) return; // No reason to mutate. 628 size_t N = fuzzer::InternalStrnlen2(s1, s2); 629 if (N <= 1) return; // Not interesting. 630 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true); 631 } 632 633 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 634 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1, 635 const char *s2, size_t n, int result) { 636 if (!fuzzer::RunningUserCallback) return; 637 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result); 638 } 639 640 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 641 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1, 642 const char *s2, int result) { 643 if (!fuzzer::RunningUserCallback) return; 644 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result); 645 } 646 647 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 648 void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1, 649 const char *s2, char *result) { 650 if (!fuzzer::RunningUserCallback) return; 651 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 652 } 653 654 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 655 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1, 656 const char *s2, char *result) { 657 if (!fuzzer::RunningUserCallback) return; 658 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 659 } 660 661 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 662 void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1, 663 const void *s2, size_t len2, void *result) { 664 if (!fuzzer::RunningUserCallback) return; 665 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2); 666 } 667 } // extern "C" 668