1 //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // Trace PCs. 9 // This module implements __sanitizer_cov_trace_pc_guard[_init], 10 // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "FuzzerTracePC.h" 15 #include "FuzzerBuiltins.h" 16 #include "FuzzerBuiltinsMsvc.h" 17 #include "FuzzerCorpus.h" 18 #include "FuzzerDefs.h" 19 #include "FuzzerDictionary.h" 20 #include "FuzzerExtFunctions.h" 21 #include "FuzzerIO.h" 22 #include "FuzzerUtil.h" 23 #include "FuzzerValueBitMap.h" 24 #include <set> 25 26 // Used by -fsanitize-coverage=stack-depth to track stack depth 27 ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC uintptr_t __sancov_lowest_stack; 28 29 namespace fuzzer { 30 31 TracePC TPC; 32 33 size_t TracePC::GetTotalPCCoverage() { 34 return ObservedPCs.size(); 35 } 36 37 38 void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) { 39 if (Start == Stop) return; 40 if (NumModules && 41 Modules[NumModules - 1].Start() == Start) 42 return; 43 assert(NumModules < 44 sizeof(Modules) / sizeof(Modules[0])); 45 auto &M = Modules[NumModules++]; 46 uint8_t *AlignedStart = RoundUpByPage(Start); 47 uint8_t *AlignedStop = RoundDownByPage(Stop); 48 size_t NumFullPages = AlignedStop > AlignedStart ? 49 (AlignedStop - AlignedStart) / PageSize() : 0; 50 bool NeedFirst = Start < AlignedStart || !NumFullPages; 51 bool NeedLast = Stop > AlignedStop && AlignedStop >= AlignedStart; 52 M.NumRegions = NumFullPages + NeedFirst + NeedLast;; 53 assert(M.NumRegions > 0); 54 M.Regions = new Module::Region[M.NumRegions]; 55 assert(M.Regions); 56 size_t R = 0; 57 if (NeedFirst) 58 M.Regions[R++] = {Start, std::min(Stop, AlignedStart), true, false}; 59 for (uint8_t *P = AlignedStart; P < AlignedStop; P += PageSize()) 60 M.Regions[R++] = {P, P + PageSize(), true, true}; 61 if (NeedLast) 62 M.Regions[R++] = {AlignedStop, Stop, true, false}; 63 assert(R == M.NumRegions); 64 assert(M.Size() == (size_t)(Stop - Start)); 65 assert(M.Stop() == Stop); 66 assert(M.Start() == Start); 67 NumInline8bitCounters += M.Size(); 68 } 69 70 void TracePC::HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop) { 71 const PCTableEntry *B = reinterpret_cast<const PCTableEntry *>(Start); 72 const PCTableEntry *E = reinterpret_cast<const PCTableEntry *>(Stop); 73 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return; 74 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0])); 75 ModulePCTable[NumPCTables++] = {B, E}; 76 NumPCsInPCTables += E - B; 77 } 78 79 void TracePC::PrintModuleInfo() { 80 if (NumModules) { 81 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ", 82 NumModules, NumInline8bitCounters); 83 for (size_t i = 0; i < NumModules; i++) 84 Printf("%zd [%p, %p), ", Modules[i].Size(), Modules[i].Start(), 85 Modules[i].Stop()); 86 Printf("\n"); 87 } 88 if (NumPCTables) { 89 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables, 90 NumPCsInPCTables); 91 for (size_t i = 0; i < NumPCTables; i++) { 92 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start, 93 ModulePCTable[i].Start, ModulePCTable[i].Stop); 94 } 95 Printf("\n"); 96 97 if (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables) { 98 Printf("ERROR: The size of coverage PC tables does not match the\n" 99 "number of instrumented PCs. This might be a compiler bug,\n" 100 "please contact the libFuzzer developers.\n" 101 "Also check https://bugs.llvm.org/show_bug.cgi?id=34636\n" 102 "for possible workarounds (tl;dr: don't use the old GNU ld)\n"); 103 _Exit(1); 104 } 105 } 106 if (size_t NumExtraCounters = ExtraCountersEnd() - ExtraCountersBegin()) 107 Printf("INFO: %zd Extra Counters\n", NumExtraCounters); 108 } 109 110 ATTRIBUTE_NO_SANITIZE_ALL 111 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { 112 const uintptr_t kBits = 12; 113 const uintptr_t kMask = (1 << kBits) - 1; 114 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits); 115 ValueProfileMap.AddValueModPrime(Idx); 116 } 117 118 /// \return the address of the previous instruction. 119 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.h` 120 inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) { 121 #if defined(__arm__) 122 // T32 (Thumb) branch instructions might be 16 or 32 bit long, 123 // so we return (pc-2) in that case in order to be safe. 124 // For A32 mode we return (pc-4) because all instructions are 32 bit long. 125 return (PC - 3) & (~1); 126 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__) 127 // PCs are always 4 byte aligned. 128 return PC - 4; 129 #elif defined(__sparc__) || defined(__mips__) 130 return PC - 8; 131 #else 132 return PC - 1; 133 #endif 134 } 135 136 /// \return the address of the next instruction. 137 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.cpp` 138 ALWAYS_INLINE uintptr_t TracePC::GetNextInstructionPc(uintptr_t PC) { 139 #if defined(__mips__) 140 return PC + 8; 141 #elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \ 142 defined(__aarch64__) 143 return PC + 4; 144 #else 145 return PC + 1; 146 #endif 147 } 148 149 void TracePC::UpdateObservedPCs() { 150 Vector<uintptr_t> CoveredFuncs; 151 auto ObservePC = [&](const PCTableEntry *TE) { 152 if (ObservedPCs.insert(TE).second && DoPrintNewPCs) { 153 PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p", 154 GetNextInstructionPc(TE->PC)); 155 Printf("\n"); 156 } 157 }; 158 159 auto Observe = [&](const PCTableEntry *TE) { 160 if (PcIsFuncEntry(TE)) 161 if (++ObservedFuncs[TE->PC] == 1 && NumPrintNewFuncs) 162 CoveredFuncs.push_back(TE->PC); 163 ObservePC(TE); 164 }; 165 166 if (NumPCsInPCTables) { 167 if (NumInline8bitCounters == NumPCsInPCTables) { 168 for (size_t i = 0; i < NumModules; i++) { 169 auto &M = Modules[i]; 170 assert(M.Size() == 171 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); 172 for (size_t r = 0; r < M.NumRegions; r++) { 173 auto &R = M.Regions[r]; 174 if (!R.Enabled) continue; 175 for (uint8_t *P = R.Start; P < R.Stop; P++) 176 if (*P) 177 Observe(&ModulePCTable[i].Start[M.Idx(P)]); 178 } 179 } 180 } 181 } 182 183 for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N; 184 i++) { 185 Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size()); 186 PrintPC("%p %F %L", "%p", GetNextInstructionPc(CoveredFuncs[i])); 187 Printf("\n"); 188 } 189 } 190 191 uintptr_t TracePC::PCTableEntryIdx(const PCTableEntry *TE) { 192 size_t TotalTEs = 0; 193 for (size_t i = 0; i < NumPCTables; i++) { 194 auto &M = ModulePCTable[i]; 195 if (TE >= M.Start && TE < M.Stop) 196 return TotalTEs + TE - M.Start; 197 TotalTEs += M.Stop - M.Start; 198 } 199 assert(0); 200 return 0; 201 } 202 203 const TracePC::PCTableEntry *TracePC::PCTableEntryByIdx(uintptr_t Idx) { 204 for (size_t i = 0; i < NumPCTables; i++) { 205 auto &M = ModulePCTable[i]; 206 size_t Size = M.Stop - M.Start; 207 if (Idx < Size) return &M.Start[Idx]; 208 Idx -= Size; 209 } 210 return nullptr; 211 } 212 213 static std::string GetModuleName(uintptr_t PC) { 214 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++? 215 void *OffsetRaw = nullptr; 216 if (!EF->__sanitizer_get_module_and_offset_for_pc( 217 reinterpret_cast<void *>(PC), ModulePathRaw, 218 sizeof(ModulePathRaw), &OffsetRaw)) 219 return ""; 220 return ModulePathRaw; 221 } 222 223 template<class CallBack> 224 void TracePC::IterateCoveredFunctions(CallBack CB) { 225 for (size_t i = 0; i < NumPCTables; i++) { 226 auto &M = ModulePCTable[i]; 227 assert(M.Start < M.Stop); 228 auto ModuleName = GetModuleName(M.Start->PC); 229 for (auto NextFE = M.Start; NextFE < M.Stop; ) { 230 auto FE = NextFE; 231 assert(PcIsFuncEntry(FE) && "Not a function entry point"); 232 do { 233 NextFE++; 234 } while (NextFE < M.Stop && !(PcIsFuncEntry(NextFE))); 235 CB(FE, NextFE, ObservedFuncs[FE->PC]); 236 } 237 } 238 } 239 240 void TracePC::SetFocusFunction(const std::string &FuncName) { 241 // This function should be called once. 242 assert(!FocusFunctionCounterPtr); 243 if (FuncName.empty()) 244 return; 245 for (size_t M = 0; M < NumModules; M++) { 246 auto &PCTE = ModulePCTable[M]; 247 size_t N = PCTE.Stop - PCTE.Start; 248 for (size_t I = 0; I < N; I++) { 249 if (!(PcIsFuncEntry(&PCTE.Start[I]))) continue; // not a function entry. 250 auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC)); 251 if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ') 252 Name = Name.substr(3, std::string::npos); 253 if (FuncName != Name) continue; 254 Printf("INFO: Focus function is set to '%s'\n", Name.c_str()); 255 FocusFunctionCounterPtr = Modules[M].Start() + I; 256 return; 257 } 258 } 259 } 260 261 bool TracePC::ObservedFocusFunction() { 262 return FocusFunctionCounterPtr && *FocusFunctionCounterPtr; 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, 275 const PCTableEntry *Last, 276 uintptr_t Counter) { 277 assert(First < Last); 278 auto VisualizePC = GetNextInstructionPc(First->PC); 279 std::string FileStr = DescribePC("%s", VisualizePC); 280 if (!IsInterestingCoverageFile(FileStr)) 281 return; 282 std::string FunctionStr = DescribePC("%F", VisualizePC); 283 if (FunctionStr.find("in ") == 0) 284 FunctionStr = FunctionStr.substr(3); 285 std::string LineStr = DescribePC("%l", VisualizePC); 286 size_t NumEdges = Last - First; 287 Vector<uintptr_t> UncoveredPCs; 288 for (auto TE = First; TE < Last; TE++) 289 if (!ObservedPCs.count(TE)) 290 UncoveredPCs.push_back(TE->PC); 291 Printf("%sCOVERED_FUNC: hits: %zd", Counter ? "" : "UN", Counter); 292 Printf(" edges: %zd/%zd", NumEdges - UncoveredPCs.size(), NumEdges); 293 Printf(" %s %s:%s\n", FunctionStr.c_str(), FileStr.c_str(), 294 LineStr.c_str()); 295 if (Counter) 296 for (auto PC : UncoveredPCs) 297 Printf(" UNCOVERED_PC: %s\n", 298 DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str()); 299 }; 300 301 IterateCoveredFunctions(CoveredFunctionCallback); 302 } 303 304 // Value profile. 305 // We keep track of various values that affect control flow. 306 // These values are inserted into a bit-set-based hash map. 307 // Every new bit in the map is treated as a new coverage. 308 // 309 // For memcmp/strcmp/etc the interesting value is the length of the common 310 // prefix of the parameters. 311 // For cmp instructions the interesting value is a XOR of the parameters. 312 // The interesting value is mixed up with the PC and is then added to the map. 313 314 ATTRIBUTE_NO_SANITIZE_ALL 315 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, 316 size_t n, bool StopAtZero) { 317 if (!n) return; 318 size_t Len = std::min(n, Word::GetMaxSize()); 319 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); 320 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); 321 uint8_t B1[Word::kMaxSize]; 322 uint8_t B2[Word::kMaxSize]; 323 // Copy the data into locals in this non-msan-instrumented function 324 // to avoid msan complaining further. 325 size_t Hash = 0; // Compute some simple hash of both strings. 326 for (size_t i = 0; i < Len; i++) { 327 B1[i] = A1[i]; 328 B2[i] = A2[i]; 329 size_t T = B1[i]; 330 Hash ^= (T << 8) | B2[i]; 331 } 332 size_t I = 0; 333 uint8_t HammingDistance = 0; 334 for (; I < Len; I++) { 335 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) { 336 HammingDistance = Popcountll(B1[I] ^ B2[I]); 337 break; 338 } 339 } 340 size_t PC = reinterpret_cast<size_t>(caller_pc); 341 size_t Idx = (PC & 4095) | (I << 12); 342 Idx += HammingDistance; 343 ValueProfileMap.AddValue(Idx); 344 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len)); 345 } 346 347 template <class T> 348 ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE 349 ATTRIBUTE_NO_SANITIZE_ALL 350 void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) { 351 uint64_t ArgXor = Arg1 ^ Arg2; 352 if (sizeof(T) == 4) 353 TORC4.Insert(ArgXor, Arg1, Arg2); 354 else if (sizeof(T) == 8) 355 TORC8.Insert(ArgXor, Arg1, Arg2); 356 uint64_t HammingDistance = Popcountll(ArgXor); // [0,64] 357 uint64_t AbsoluteDistance = (Arg1 == Arg2 ? 0 : Clzll(Arg1 - Arg2) + 1); 358 ValueProfileMap.AddValue(PC * 128 + HammingDistance); 359 ValueProfileMap.AddValue(PC * 128 + 64 + AbsoluteDistance); 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 IterateCounterRegions([](const Module::Region &R){ 378 if (R.Enabled) 379 memset(R.Start, 0, R.Stop - R.Start); 380 }); 381 } 382 383 ATTRIBUTE_NO_SANITIZE_ALL 384 void TracePC::RecordInitialStack() { 385 int stack; 386 __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack); 387 } 388 389 uintptr_t TracePC::GetMaxStackOffset() const { 390 return InitialStack - __sancov_lowest_stack; // Stack grows down 391 } 392 393 void WarnAboutDeprecatedInstrumentation(const char *flag) { 394 // Use RawPrint because Printf cannot be used on Windows before OutputFile is 395 // initialized. 396 RawPrint(flag); 397 RawPrint( 398 " is no longer supported by libFuzzer.\n" 399 "Please either migrate to a compiler that supports -fsanitize=fuzzer\n" 400 "or use an older version of libFuzzer\n"); 401 exit(1); 402 } 403 404 } // namespace fuzzer 405 406 extern "C" { 407 ATTRIBUTE_INTERFACE 408 ATTRIBUTE_NO_SANITIZE_ALL 409 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) { 410 fuzzer::WarnAboutDeprecatedInstrumentation( 411 "-fsanitize-coverage=trace-pc-guard"); 412 } 413 414 // Best-effort support for -fsanitize-coverage=trace-pc, which is available 415 // in both Clang and GCC. 416 ATTRIBUTE_INTERFACE 417 ATTRIBUTE_NO_SANITIZE_ALL 418 void __sanitizer_cov_trace_pc() { 419 fuzzer::WarnAboutDeprecatedInstrumentation("-fsanitize-coverage=trace-pc"); 420 } 421 422 ATTRIBUTE_INTERFACE 423 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) { 424 fuzzer::WarnAboutDeprecatedInstrumentation( 425 "-fsanitize-coverage=trace-pc-guard"); 426 } 427 428 ATTRIBUTE_INTERFACE 429 void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) { 430 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop); 431 } 432 433 ATTRIBUTE_INTERFACE 434 void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, 435 const uintptr_t *pcs_end) { 436 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end); 437 } 438 439 ATTRIBUTE_INTERFACE 440 ATTRIBUTE_NO_SANITIZE_ALL 441 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { 442 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 443 fuzzer::TPC.HandleCallerCallee(PC, Callee); 444 } 445 446 ATTRIBUTE_INTERFACE 447 ATTRIBUTE_NO_SANITIZE_ALL 448 ATTRIBUTE_TARGET_POPCNT 449 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { 450 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 451 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 452 } 453 454 ATTRIBUTE_INTERFACE 455 ATTRIBUTE_NO_SANITIZE_ALL 456 ATTRIBUTE_TARGET_POPCNT 457 // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic 458 // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however, 459 // should be changed later to make full use of instrumentation. 460 void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) { 461 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 462 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 463 } 464 465 ATTRIBUTE_INTERFACE 466 ATTRIBUTE_NO_SANITIZE_ALL 467 ATTRIBUTE_TARGET_POPCNT 468 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { 469 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 470 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 471 } 472 473 ATTRIBUTE_INTERFACE 474 ATTRIBUTE_NO_SANITIZE_ALL 475 ATTRIBUTE_TARGET_POPCNT 476 void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) { 477 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 478 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 479 } 480 481 ATTRIBUTE_INTERFACE 482 ATTRIBUTE_NO_SANITIZE_ALL 483 ATTRIBUTE_TARGET_POPCNT 484 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) { 485 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 486 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 487 } 488 489 ATTRIBUTE_INTERFACE 490 ATTRIBUTE_NO_SANITIZE_ALL 491 ATTRIBUTE_TARGET_POPCNT 492 void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) { 493 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 494 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 495 } 496 497 ATTRIBUTE_INTERFACE 498 ATTRIBUTE_NO_SANITIZE_ALL 499 ATTRIBUTE_TARGET_POPCNT 500 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) { 501 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 502 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 503 } 504 505 ATTRIBUTE_INTERFACE 506 ATTRIBUTE_NO_SANITIZE_ALL 507 ATTRIBUTE_TARGET_POPCNT 508 void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) { 509 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 510 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); 511 } 512 513 ATTRIBUTE_INTERFACE 514 ATTRIBUTE_NO_SANITIZE_ALL 515 ATTRIBUTE_TARGET_POPCNT 516 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { 517 uint64_t N = Cases[0]; 518 uint64_t ValSizeInBits = Cases[1]; 519 uint64_t *Vals = Cases + 2; 520 // Skip the most common and the most boring case: all switch values are small. 521 // We may want to skip this at compile-time, but it will make the 522 // instrumentation less general. 523 if (Vals[N - 1] < 256) 524 return; 525 // Also skip small inputs values, they won't give good signal. 526 if (Val < 256) 527 return; 528 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 529 size_t i; 530 uint64_t Smaller = 0; 531 uint64_t Larger = ~(uint64_t)0; 532 // Find two switch values such that Smaller < Val < Larger. 533 // Use 0 and 0xfff..f as the defaults. 534 for (i = 0; i < N; i++) { 535 if (Val < Vals[i]) { 536 Larger = Vals[i]; 537 break; 538 } 539 if (Val > Vals[i]) Smaller = Vals[i]; 540 } 541 542 // Apply HandleCmp to {Val,Smaller} and {Val, Larger}, 543 // use i as the PC modifier for HandleCmp. 544 if (ValSizeInBits == 16) { 545 fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint16_t>(Val), 546 (uint16_t)(Smaller)); 547 fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint16_t>(Val), 548 (uint16_t)(Larger)); 549 } else if (ValSizeInBits == 32) { 550 fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint32_t>(Val), 551 (uint32_t)(Smaller)); 552 fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint32_t>(Val), 553 (uint32_t)(Larger)); 554 } else { 555 fuzzer::TPC.HandleCmp(PC + 2*i, Val, Smaller); 556 fuzzer::TPC.HandleCmp(PC + 2*i + 1, Val, Larger); 557 } 558 } 559 560 ATTRIBUTE_INTERFACE 561 ATTRIBUTE_NO_SANITIZE_ALL 562 ATTRIBUTE_TARGET_POPCNT 563 void __sanitizer_cov_trace_div4(uint32_t Val) { 564 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 565 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0); 566 } 567 568 ATTRIBUTE_INTERFACE 569 ATTRIBUTE_NO_SANITIZE_ALL 570 ATTRIBUTE_TARGET_POPCNT 571 void __sanitizer_cov_trace_div8(uint64_t Val) { 572 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 573 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0); 574 } 575 576 ATTRIBUTE_INTERFACE 577 ATTRIBUTE_NO_SANITIZE_ALL 578 ATTRIBUTE_TARGET_POPCNT 579 void __sanitizer_cov_trace_gep(uintptr_t Idx) { 580 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC()); 581 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0); 582 } 583 584 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 585 void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, 586 const void *s2, size_t n, int result) { 587 if (!fuzzer::RunningUserCallback) return; 588 if (result == 0) return; // No reason to mutate. 589 if (n <= 1) return; // Not interesting. 590 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false); 591 } 592 593 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 594 void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, 595 const char *s2, size_t n, int result) { 596 if (!fuzzer::RunningUserCallback) return; 597 if (result == 0) return; // No reason to mutate. 598 size_t Len1 = fuzzer::InternalStrnlen(s1, n); 599 size_t Len2 = fuzzer::InternalStrnlen(s2, n); 600 n = std::min(n, Len1); 601 n = std::min(n, Len2); 602 if (n <= 1) return; // Not interesting. 603 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true); 604 } 605 606 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 607 void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, 608 const char *s2, int result) { 609 if (!fuzzer::RunningUserCallback) return; 610 if (result == 0) return; // No reason to mutate. 611 size_t N = fuzzer::InternalStrnlen2(s1, s2); 612 if (N <= 1) return; // Not interesting. 613 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true); 614 } 615 616 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 617 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1, 618 const char *s2, size_t n, int result) { 619 if (!fuzzer::RunningUserCallback) return; 620 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result); 621 } 622 623 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 624 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1, 625 const char *s2, int result) { 626 if (!fuzzer::RunningUserCallback) return; 627 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result); 628 } 629 630 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 631 void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1, 632 const char *s2, char *result) { 633 if (!fuzzer::RunningUserCallback) return; 634 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 635 } 636 637 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 638 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1, 639 const char *s2, char *result) { 640 if (!fuzzer::RunningUserCallback) return; 641 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); 642 } 643 644 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY 645 void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1, 646 const void *s2, size_t len2, void *result) { 647 if (!fuzzer::RunningUserCallback) return; 648 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2); 649 } 650 } // extern "C" 651