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" 128 " number of instrumented PCs. This might be a bug in the compiler," 129 " please contact the libFuzzer developers.\n"); 130 _Exit(1); 131 } 132 } 133 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) 134 Printf("INFO: %zd Clang Coverage Counters\n", NumClangCounters); 135 } 136 137 ATTRIBUTE_NO_SANITIZE_ALL 138 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { 139 const uintptr_t kBits = 12; 140 const uintptr_t kMask = (1 << kBits) - 1; 141 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits); 142 ValueProfileMap.AddValueModPrime(Idx); 143 } 144 145 void TracePC::UpdateObservedPCs() { 146 auto ObservePC = [&](uintptr_t PC) { 147 if (ObservedPCs.insert(PC).second && DoPrintNewPCs) 148 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1); 149 }; 150 151 auto Observe = [&](const PCTableEntry &TE) { 152 if (TE.PCFlags & 1) 153 if (ObservedFuncs.insert(TE.PC).second && DoPrintNewFuncs) 154 PrintPC("\tNEW_FUNC: %p %F %L\n", "\tNEW_PC: %p\n", TE.PC + 1); 155 ObservePC(TE.PC); 156 }; 157 158 if (NumPCsInPCTables) { 159 if (NumInline8bitCounters == NumPCsInPCTables) { 160 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { 161 uint8_t *Beg = ModuleCounters[i].Start; 162 size_t Size = ModuleCounters[i].Stop - Beg; 163 assert(Size == 164 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); 165 for (size_t j = 0; j < Size; j++) 166 if (Beg[j]) 167 Observe(ModulePCTable[i].Start[j]); 168 } 169 } else if (NumGuards == NumPCsInPCTables) { 170 size_t GuardIdx = 1; 171 for (size_t i = 0; i < NumModules; i++) { 172 uint32_t *Beg = Modules[i].Start; 173 size_t Size = Modules[i].Stop - Beg; 174 assert(Size == 175 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); 176 for (size_t j = 0; j < Size; j++, GuardIdx++) 177 if (Counters()[GuardIdx]) 178 Observe(ModulePCTable[i].Start[j]); 179 } 180 } 181 } 182 if (size_t NumClangCounters = 183 ClangCountersEnd() - ClangCountersBegin()) { 184 auto P = ClangCountersBegin(); 185 for (size_t Idx = 0; Idx < NumClangCounters; Idx++) 186 if (P[Idx]) 187 ObservePC((uintptr_t)Idx); 188 } 189 } 190 191 inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) { 192 // TODO: this implementation is x86 only. 193 // see sanitizer_common GetPreviousInstructionPc for full implementation. 194 return PC - 1; 195 } 196 197 inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) { 198 // TODO: this implementation is x86 only. 199 // see sanitizer_common GetPreviousInstructionPc for full implementation. 200 return PC + 1; 201 } 202 203 static std::string GetModuleName(uintptr_t PC) { 204 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++? 205 void *OffsetRaw = nullptr; 206 if (!EF->__sanitizer_get_module_and_offset_for_pc( 207 reinterpret_cast<void *>(PC), ModulePathRaw, 208 sizeof(ModulePathRaw), &OffsetRaw)) 209 return ""; 210 return ModulePathRaw; 211 } 212 213 void TracePC::PrintCoverage() { 214 if (!EF->__sanitizer_symbolize_pc || 215 !EF->__sanitizer_get_module_and_offset_for_pc) { 216 Printf("INFO: __sanitizer_symbolize_pc or " 217 "__sanitizer_get_module_and_offset_for_pc is not available," 218 " not printing coverage\n"); 219 return; 220 } 221 Printf("COVERAGE:\n"); 222 std::string LastFunctionName = ""; 223 std::string LastFileStr = ""; 224 std::set<size_t> UncoveredLines; 225 std::set<size_t> CoveredLines; 226 227 auto FunctionEndCallback = [&](const std::string &CurrentFunc, 228 const std::string &CurrentFile) { 229 if (LastFunctionName != CurrentFunc) { 230 if (CoveredLines.empty() && !UncoveredLines.empty()) { 231 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str()); 232 } else { 233 for (auto Line : UncoveredLines) { 234 if (!CoveredLines.count(Line)) 235 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(), 236 LastFileStr.c_str(), Line); 237 } 238 } 239 240 UncoveredLines.clear(); 241 CoveredLines.clear(); 242 LastFunctionName = CurrentFunc; 243 LastFileStr = CurrentFile; 244 } 245 }; 246 247 for (size_t i = 0; i < NumPCTables; i++) { 248 auto &M = ModulePCTable[i]; 249 assert(M.Start < M.Stop); 250 auto ModuleName = GetModuleName(M.Start->PC); 251 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) { 252 auto PC = Ptr->PC; 253 auto VisualizePC = GetNextInstructionPc(PC); 254 bool IsObserved = ObservedPCs.count(PC); 255 std::string FileStr = DescribePC("%s", VisualizePC); 256 if (!IsInterestingCoverageFile(FileStr)) continue; 257 std::string FunctionStr = DescribePC("%F", VisualizePC); 258 FunctionEndCallback(FunctionStr, FileStr); 259 std::string LineStr = DescribePC("%l", VisualizePC); 260 size_t Line = std::stoul(LineStr); 261 if (IsObserved && CoveredLines.insert(Line).second) 262 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(), 263 Line); 264 else 265 UncoveredLines.insert(Line); 266 } 267 } 268 FunctionEndCallback("", ""); 269 } 270 271 void TracePC::DumpCoverage() { 272 if (EF->__sanitizer_dump_coverage) { 273 std::vector<uintptr_t> PCsCopy(GetNumPCs()); 274 for (size_t i = 0; i < GetNumPCs(); i++) 275 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0; 276 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size()); 277 } 278 } 279 280 // Value profile. 281 // We keep track of various values that affect control flow. 282 // These values are inserted into a bit-set-based hash map. 283 // Every new bit in the map is treated as a new coverage. 284 // 285 // For memcmp/strcmp/etc the interesting value is the length of the common 286 // prefix of the parameters. 287 // For cmp instructions the interesting value is a XOR of the parameters. 288 // The interesting value is mixed up with the PC and is then added to the map. 289 290 ATTRIBUTE_NO_SANITIZE_ALL 291 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, 292 size_t n, bool StopAtZero) { 293 if (!n) return; 294 size_t Len = std::min(n, Word::GetMaxSize()); 295 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); 296 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); 297 uint8_t B1[Word::kMaxSize]; 298 uint8_t B2[Word::kMaxSize]; 299 // Copy the data into locals in this non-msan-instrumented function 300 // to avoid msan complaining further. 301 size_t Hash = 0; // Compute some simple hash of both strings. 302 for (size_t i = 0; i < Len; i++) { 303 B1[i] = A1[i]; 304 B2[i] = A2[i]; 305 size_t T = B1[i]; 306 Hash ^= (T << 8) | B2[i]; 307 } 308 size_t I = 0; 309 for (; I < Len; I++) 310 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) 311 break; 312 size_t PC = reinterpret_cast<size_t>(caller_pc); 313 size_t Idx = (PC & 4095) | (I << 12); 314 ValueProfileMap.AddValue(Idx); 315 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len)); 316 } 317 318 template <class T> 319 ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE 320 ATTRIBUTE_NO_SANITIZE_ALL 321 void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) { 322 uint64_t ArgXor = Arg1 ^ Arg2; 323 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65] 324 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance; 325 if (sizeof(T) == 4) 326 TORC4.Insert(ArgXor, Arg1, Arg2); 327 else if (sizeof(T) == 8) 328 TORC8.Insert(ArgXor, Arg1, Arg2); 329 ValueProfileMap.AddValue(Idx); 330 } 331 332 static size_t InternalStrnlen(const char *S, size_t MaxLen) { 333 size_t Len = 0; 334 for (; Len < MaxLen && S[Len]; Len++) {} 335 return Len; 336 } 337 338 // Finds min of (strlen(S1), strlen(S2)). 339 // Needed bacause one of these strings may actually be non-zero terminated. 340 static size_t InternalStrnlen2(const char *S1, const char *S2) { 341 size_t Len = 0; 342 for (; S1[Len] && S2[Len]; Len++) {} 343 return Len; 344 } 345 346 void TracePC::ClearInlineCounters() { 347 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { 348 uint8_t *Beg = ModuleCounters[i].Start; 349 size_t Size = ModuleCounters[i].Stop - Beg; 350 memset(Beg, 0, Size); 351 } 352 } 353 354 ATTRIBUTE_NO_SANITIZE_ALL 355 void TracePC::RecordInitialStack() { 356 int stack; 357 __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack); 358 } 359 360 uintptr_t TracePC::GetMaxStackOffset() const { 361 return InitialStack - __sancov_lowest_stack; // Stack grows down 362 } 363 364 } // namespace fuzzer 365 366 extern "C" { 367 ATTRIBUTE_INTERFACE 368 ATTRIBUTE_NO_SANITIZE_ALL 369 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) { 370 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 371 uint32_t Idx = *Guard; 372 __sancov_trace_pc_pcs[Idx] = PC; 373 __sancov_trace_pc_guard_8bit_counters[Idx]++; 374 } 375 376 // Best-effort support for -fsanitize-coverage=trace-pc, which is available 377 // in both Clang and GCC. 378 ATTRIBUTE_INTERFACE 379 ATTRIBUTE_NO_SANITIZE_ALL 380 void __sanitizer_cov_trace_pc() { 381 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 382 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1); 383 __sancov_trace_pc_pcs[Idx] = PC; 384 __sancov_trace_pc_guard_8bit_counters[Idx]++; 385 } 386 387 ATTRIBUTE_INTERFACE 388 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) { 389 fuzzer::TPC.HandleInit(Start, Stop); 390 } 391 392 ATTRIBUTE_INTERFACE 393 void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) { 394 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop); 395 } 396 397 ATTRIBUTE_INTERFACE 398 void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, 399 const uintptr_t *pcs_end) { 400 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end); 401 } 402 403 ATTRIBUTE_INTERFACE 404 ATTRIBUTE_NO_SANITIZE_ALL 405 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { 406 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 407 fuzzer::TPC.HandleCallerCallee(PC, Callee); 408 } 409 410 ATTRIBUTE_INTERFACE 411 ATTRIBUTE_NO_SANITIZE_ALL 412 ATTRIBUTE_TARGET_POPCNT 413 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { 414 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 415 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 416 } 417 418 ATTRIBUTE_INTERFACE 419 ATTRIBUTE_NO_SANITIZE_ALL 420 ATTRIBUTE_TARGET_POPCNT 421 // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic 422 // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however, 423 // should be changed later to make full use of instrumentation. 424 void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) { 425 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 426 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 427 } 428 429 ATTRIBUTE_INTERFACE 430 ATTRIBUTE_NO_SANITIZE_ALL 431 ATTRIBUTE_TARGET_POPCNT 432 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { 433 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 434 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 435 } 436 437 ATTRIBUTE_INTERFACE 438 ATTRIBUTE_NO_SANITIZE_ALL 439 ATTRIBUTE_TARGET_POPCNT 440 void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) { 441 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 442 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 443 } 444 445 ATTRIBUTE_INTERFACE 446 ATTRIBUTE_NO_SANITIZE_ALL 447 ATTRIBUTE_TARGET_POPCNT 448 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) { 449 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 450 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 451 } 452 453 ATTRIBUTE_INTERFACE 454 ATTRIBUTE_NO_SANITIZE_ALL 455 ATTRIBUTE_TARGET_POPCNT 456 void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) { 457 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 458 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 459 } 460 461 ATTRIBUTE_INTERFACE 462 ATTRIBUTE_NO_SANITIZE_ALL 463 ATTRIBUTE_TARGET_POPCNT 464 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) { 465 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 466 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 467 } 468 469 ATTRIBUTE_INTERFACE 470 ATTRIBUTE_NO_SANITIZE_ALL 471 ATTRIBUTE_TARGET_POPCNT 472 void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) { 473 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 474 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 475 } 476 477 ATTRIBUTE_INTERFACE 478 ATTRIBUTE_NO_SANITIZE_ALL 479 ATTRIBUTE_TARGET_POPCNT 480 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { 481 uint64_t N = Cases[0]; 482 uint64_t ValSizeInBits = Cases[1]; 483 uint64_t *Vals = Cases + 2; 484 // Skip the most common and the most boring case. 485 if (Vals[N - 1] < 256 && Val < 256) 486 return; 487 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 488 size_t i; 489 uint64_t Token = 0; 490 for (i = 0; i < N; i++) { 491 Token = Val ^ Vals[i]; 492 if (Val < Vals[i]) 493 break; 494 } 495 496 if (ValSizeInBits == 16) 497 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0)); 498 else if (ValSizeInBits == 32) 499 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0)); 500 else 501 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0)); 502 } 503 504 ATTRIBUTE_INTERFACE 505 ATTRIBUTE_NO_SANITIZE_ALL 506 ATTRIBUTE_TARGET_POPCNT 507 void __sanitizer_cov_trace_div4(uint32_t Val) { 508 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 509 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0); 510 } 511 512 ATTRIBUTE_INTERFACE 513 ATTRIBUTE_NO_SANITIZE_ALL 514 ATTRIBUTE_TARGET_POPCNT 515 void __sanitizer_cov_trace_div8(uint64_t Val) { 516 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 517 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0); 518 } 519 520 ATTRIBUTE_INTERFACE 521 ATTRIBUTE_NO_SANITIZE_ALL 522 ATTRIBUTE_TARGET_POPCNT 523 void __sanitizer_cov_trace_gep(uintptr_t Idx) { 524 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); 525 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0); 526 } 527 528 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 529 void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, 530 const void *s2, size_t n, int result) { 531 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 532 if (result == 0) return; // No reason to mutate. 533 if (n <= 1) return; // Not interesting. 534 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false); 535 } 536 537 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 538 void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, 539 const char *s2, size_t n, int result) { 540 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 541 if (result == 0) return; // No reason to mutate. 542 size_t Len1 = fuzzer::InternalStrnlen(s1, n); 543 size_t Len2 = fuzzer::InternalStrnlen(s2, n); 544 n = std::min(n, Len1); 545 n = std::min(n, Len2); 546 if (n <= 1) return; // Not interesting. 547 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true); 548 } 549 550 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 551 void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, 552 const char *s2, int result) { 553 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 554 if (result == 0) return; // No reason to mutate. 555 size_t N = fuzzer::InternalStrnlen2(s1, s2); 556 if (N <= 1) return; // Not interesting. 557 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true); 558 } 559 560 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 561 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1, 562 const char *s2, size_t n, int result) { 563 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 564 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result); 565 } 566 567 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 568 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1, 569 const char *s2, int result) { 570 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 571 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result); 572 } 573 574 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 575 void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1, 576 const char *s2, char *result) { 577 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 578 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 579 } 580 581 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 582 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1, 583 const char *s2, char *result) { 584 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 585 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 586 } 587 588 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 589 void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1, 590 const void *s2, size_t len2, void *result) { 591 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; 592 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2); 593 } 594 } // extern "C" 595