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