1 //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This module implements __sanitizer_cov_trace_pc_guard[_init],
10 // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation.
12 //===----------------------------------------------------------------------===//
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"
22 #include "FuzzerPlatform.h"
23 #include "FuzzerUtil.h"
24 #include "FuzzerValueBitMap.h"
27 // Used by -fsanitize-coverage=stack-depth to track stack depth
28 ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC uintptr_t __sancov_lowest_stack;
34 size_t TracePC::GetTotalPCCoverage() {
35 return ObservedPCs.size();
39 void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
40 if (Start == Stop) return;
42 Modules[NumModules - 1].Start() == Start)
45 sizeof(Modules) / sizeof(Modules[0]));
46 auto &M = Modules[NumModules++];
47 uint8_t *AlignedStart = RoundUpByPage(Start);
48 uint8_t *AlignedStop = RoundDownByPage(Stop);
49 size_t NumFullPages = AlignedStop > AlignedStart ?
50 (AlignedStop - AlignedStart) / PageSize() : 0;
51 bool NeedFirst = Start < AlignedStart || !NumFullPages;
52 bool NeedLast = Stop > AlignedStop && AlignedStop >= AlignedStart;
53 M.NumRegions = NumFullPages + NeedFirst + NeedLast;;
54 assert(M.NumRegions > 0);
55 M.Regions = new Module::Region[M.NumRegions];
59 M.Regions[R++] = {Start, std::min(Stop, AlignedStart), true, false};
60 for (uint8_t *P = AlignedStart; P < AlignedStop; P += PageSize())
61 M.Regions[R++] = {P, P + PageSize(), true, true};
63 M.Regions[R++] = {AlignedStop, Stop, true, false};
64 assert(R == M.NumRegions);
65 assert(M.Size() == (size_t)(Stop - Start));
66 assert(M.Stop() == Stop);
67 assert(M.Start() == Start);
68 NumInline8bitCounters += M.Size();
71 void TracePC::HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop) {
72 const PCTableEntry *B = reinterpret_cast<const PCTableEntry *>(Start);
73 const PCTableEntry *E = reinterpret_cast<const PCTableEntry *>(Stop);
74 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return;
75 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0]));
76 ModulePCTable[NumPCTables++] = {B, E};
77 NumPCsInPCTables += E - B;
80 void TracePC::PrintModuleInfo() {
82 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ",
83 NumModules, NumInline8bitCounters);
84 for (size_t i = 0; i < NumModules; i++)
85 Printf("%zd [%p, %p), ", Modules[i].Size(), Modules[i].Start(),
90 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables,
92 for (size_t i = 0; i < NumPCTables; i++) {
93 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start,
94 ModulePCTable[i].Start, ModulePCTable[i].Stop);
98 if (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables) {
99 Printf("ERROR: The size of coverage PC tables does not match the\n"
100 "number of instrumented PCs. This might be a compiler bug,\n"
101 "please contact the libFuzzer developers.\n"
102 "Also check https://bugs.llvm.org/show_bug.cgi?id=34636\n"
103 "for possible workarounds (tl;dr: don't use the old GNU ld)\n");
107 if (size_t NumExtraCounters = ExtraCountersEnd() - ExtraCountersBegin())
108 Printf("INFO: %zd Extra Counters\n", NumExtraCounters);
111 ATTRIBUTE_NO_SANITIZE_ALL
112 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
113 const uintptr_t kBits = 12;
114 const uintptr_t kMask = (1 << kBits) - 1;
115 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
116 ValueProfileMap.AddValueModPrime(Idx);
119 /// \return the address of the previous instruction.
120 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.h`
121 inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
123 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
124 // so we return (pc-2) in that case in order to be safe.
125 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
126 return (PC - 3) & (~1);
127 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
128 // PCs are always 4 byte aligned.
130 #elif defined(__sparc__) || defined(__mips__)
137 /// \return the address of the next instruction.
138 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.cpp`
139 ALWAYS_INLINE uintptr_t TracePC::GetNextInstructionPc(uintptr_t PC) {
140 #if defined(__mips__)
142 #elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
150 void TracePC::UpdateObservedPCs() {
151 Vector<uintptr_t> CoveredFuncs;
152 auto ObservePC = [&](const PCTableEntry *TE) {
153 if (ObservedPCs.insert(TE).second && DoPrintNewPCs) {
154 PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p",
155 GetNextInstructionPc(TE->PC));
160 auto Observe = [&](const PCTableEntry *TE) {
161 if (PcIsFuncEntry(TE))
162 if (++ObservedFuncs[TE->PC] == 1 && NumPrintNewFuncs)
163 CoveredFuncs.push_back(TE->PC);
167 if (NumPCsInPCTables) {
168 if (NumInline8bitCounters == NumPCsInPCTables) {
169 for (size_t i = 0; i < NumModules; i++) {
170 auto &M = Modules[i];
172 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
173 for (size_t r = 0; r < M.NumRegions; r++) {
174 auto &R = M.Regions[r];
175 if (!R.Enabled) continue;
176 for (uint8_t *P = R.Start; P < R.Stop; P++)
178 Observe(&ModulePCTable[i].Start[M.Idx(P)]);
184 for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N;
186 Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size());
187 PrintPC("%p %F %L", "%p", GetNextInstructionPc(CoveredFuncs[i]));
192 uintptr_t TracePC::PCTableEntryIdx(const PCTableEntry *TE) {
194 for (size_t i = 0; i < NumPCTables; i++) {
195 auto &M = ModulePCTable[i];
196 if (TE >= M.Start && TE < M.Stop)
197 return TotalTEs + TE - M.Start;
198 TotalTEs += M.Stop - M.Start;
204 const TracePC::PCTableEntry *TracePC::PCTableEntryByIdx(uintptr_t Idx) {
205 for (size_t i = 0; i < NumPCTables; i++) {
206 auto &M = ModulePCTable[i];
207 size_t Size = M.Stop - M.Start;
208 if (Idx < Size) return &M.Start[Idx];
214 static std::string GetModuleName(uintptr_t PC) {
215 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
216 void *OffsetRaw = nullptr;
217 if (!EF->__sanitizer_get_module_and_offset_for_pc(
218 reinterpret_cast<void *>(PC), ModulePathRaw,
219 sizeof(ModulePathRaw), &OffsetRaw))
221 return ModulePathRaw;
224 template<class CallBack>
225 void TracePC::IterateCoveredFunctions(CallBack CB) {
226 for (size_t i = 0; i < NumPCTables; i++) {
227 auto &M = ModulePCTable[i];
228 assert(M.Start < M.Stop);
229 auto ModuleName = GetModuleName(M.Start->PC);
230 for (auto NextFE = M.Start; NextFE < M.Stop; ) {
232 assert(PcIsFuncEntry(FE) && "Not a function entry point");
235 } while (NextFE < M.Stop && !(PcIsFuncEntry(NextFE)));
236 CB(FE, NextFE, ObservedFuncs[FE->PC]);
241 void TracePC::SetFocusFunction(const std::string &FuncName) {
242 // This function should be called once.
243 assert(!FocusFunctionCounterPtr);
244 // "auto" is not a valid function name. If this function is called with "auto"
245 // that means the auto focus functionality failed.
246 if (FuncName.empty() || FuncName == "auto")
248 for (size_t M = 0; M < NumModules; M++) {
249 auto &PCTE = ModulePCTable[M];
250 size_t N = PCTE.Stop - PCTE.Start;
251 for (size_t I = 0; I < N; I++) {
252 if (!(PcIsFuncEntry(&PCTE.Start[I]))) continue; // not a function entry.
253 auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC));
254 if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ')
255 Name = Name.substr(3, std::string::npos);
256 if (FuncName != Name) continue;
257 Printf("INFO: Focus function is set to '%s'\n", Name.c_str());
258 FocusFunctionCounterPtr = Modules[M].Start() + I;
263 Printf("ERROR: Failed to set focus function. Make sure the function name is "
264 "valid (%s) and symbolization is enabled.\n", FuncName.c_str());
268 bool TracePC::ObservedFocusFunction() {
269 return FocusFunctionCounterPtr && *FocusFunctionCounterPtr;
272 void TracePC::PrintCoverage() {
273 if (!EF->__sanitizer_symbolize_pc ||
274 !EF->__sanitizer_get_module_and_offset_for_pc) {
275 Printf("INFO: __sanitizer_symbolize_pc or "
276 "__sanitizer_get_module_and_offset_for_pc is not available,"
277 " not printing coverage\n");
280 Printf("COVERAGE:\n");
281 auto CoveredFunctionCallback = [&](const PCTableEntry *First,
282 const PCTableEntry *Last,
284 assert(First < Last);
285 auto VisualizePC = GetNextInstructionPc(First->PC);
286 std::string FileStr = DescribePC("%s", VisualizePC);
287 if (!IsInterestingCoverageFile(FileStr))
289 std::string FunctionStr = DescribePC("%F", VisualizePC);
290 if (FunctionStr.find("in ") == 0)
291 FunctionStr = FunctionStr.substr(3);
292 std::string LineStr = DescribePC("%l", VisualizePC);
293 size_t NumEdges = Last - First;
294 Vector<uintptr_t> UncoveredPCs;
295 for (auto TE = First; TE < Last; TE++)
296 if (!ObservedPCs.count(TE))
297 UncoveredPCs.push_back(TE->PC);
298 Printf("%sCOVERED_FUNC: hits: %zd", Counter ? "" : "UN", Counter);
299 Printf(" edges: %zd/%zd", NumEdges - UncoveredPCs.size(), NumEdges);
300 Printf(" %s %s:%s\n", FunctionStr.c_str(), FileStr.c_str(),
303 for (auto PC : UncoveredPCs)
304 Printf(" UNCOVERED_PC: %s\n",
305 DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str());
308 IterateCoveredFunctions(CoveredFunctionCallback);
312 // We keep track of various values that affect control flow.
313 // These values are inserted into a bit-set-based hash map.
314 // Every new bit in the map is treated as a new coverage.
316 // For memcmp/strcmp/etc the interesting value is the length of the common
317 // prefix of the parameters.
318 // For cmp instructions the interesting value is a XOR of the parameters.
319 // The interesting value is mixed up with the PC and is then added to the map.
321 ATTRIBUTE_NO_SANITIZE_ALL
322 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
323 size_t n, bool StopAtZero) {
325 size_t Len = std::min(n, Word::GetMaxSize());
326 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
327 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
328 uint8_t B1[Word::kMaxSize];
329 uint8_t B2[Word::kMaxSize];
330 // Copy the data into locals in this non-msan-instrumented function
331 // to avoid msan complaining further.
332 size_t Hash = 0; // Compute some simple hash of both strings.
333 for (size_t i = 0; i < Len; i++) {
337 Hash ^= (T << 8) | B2[i];
340 uint8_t HammingDistance = 0;
341 for (; I < Len; I++) {
342 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) {
343 HammingDistance = Popcountll(B1[I] ^ B2[I]);
347 size_t PC = reinterpret_cast<size_t>(caller_pc);
348 size_t Idx = (PC & 4095) | (I << 12);
349 Idx += HammingDistance;
350 ValueProfileMap.AddValue(Idx);
351 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
355 ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
356 ATTRIBUTE_NO_SANITIZE_ALL
357 void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
358 uint64_t ArgXor = Arg1 ^ Arg2;
360 TORC4.Insert(ArgXor, Arg1, Arg2);
361 else if (sizeof(T) == 8)
362 TORC8.Insert(ArgXor, Arg1, Arg2);
363 uint64_t HammingDistance = Popcountll(ArgXor); // [0,64]
364 uint64_t AbsoluteDistance = (Arg1 == Arg2 ? 0 : Clzll(Arg1 - Arg2) + 1);
365 ValueProfileMap.AddValue(PC * 128 + HammingDistance);
366 ValueProfileMap.AddValue(PC * 128 + 64 + AbsoluteDistance);
369 static size_t InternalStrnlen(const char *S, size_t MaxLen) {
371 for (; Len < MaxLen && S[Len]; Len++) {}
375 // Finds min of (strlen(S1), strlen(S2)).
376 // Needed bacause one of these strings may actually be non-zero terminated.
377 static size_t InternalStrnlen2(const char *S1, const char *S2) {
379 for (; S1[Len] && S2[Len]; Len++) {}
383 void TracePC::ClearInlineCounters() {
384 IterateCounterRegions([](const Module::Region &R){
386 memset(R.Start, 0, R.Stop - R.Start);
390 ATTRIBUTE_NO_SANITIZE_ALL
391 void TracePC::RecordInitialStack() {
393 __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack);
396 uintptr_t TracePC::GetMaxStackOffset() const {
397 return InitialStack - __sancov_lowest_stack; // Stack grows down
400 void WarnAboutDeprecatedInstrumentation(const char *flag) {
401 // Use RawPrint because Printf cannot be used on Windows before OutputFile is
405 " is no longer supported by libFuzzer.\n"
406 "Please either migrate to a compiler that supports -fsanitize=fuzzer\n"
407 "or use an older version of libFuzzer\n");
411 } // namespace fuzzer
415 ATTRIBUTE_NO_SANITIZE_ALL
416 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
417 fuzzer::WarnAboutDeprecatedInstrumentation(
418 "-fsanitize-coverage=trace-pc-guard");
421 // Best-effort support for -fsanitize-coverage=trace-pc, which is available
422 // in both Clang and GCC.
424 ATTRIBUTE_NO_SANITIZE_ALL
425 void __sanitizer_cov_trace_pc() {
426 fuzzer::WarnAboutDeprecatedInstrumentation("-fsanitize-coverage=trace-pc");
430 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
431 fuzzer::WarnAboutDeprecatedInstrumentation(
432 "-fsanitize-coverage=trace-pc-guard");
436 void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
437 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
441 void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
442 const uintptr_t *pcs_end) {
443 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
447 ATTRIBUTE_NO_SANITIZE_ALL
448 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
449 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
450 fuzzer::TPC.HandleCallerCallee(PC, Callee);
454 ATTRIBUTE_NO_SANITIZE_ALL
455 ATTRIBUTE_TARGET_POPCNT
456 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
457 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
458 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
462 ATTRIBUTE_NO_SANITIZE_ALL
463 ATTRIBUTE_TARGET_POPCNT
464 // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic
465 // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however,
466 // should be changed later to make full use of instrumentation.
467 void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) {
468 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
469 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
473 ATTRIBUTE_NO_SANITIZE_ALL
474 ATTRIBUTE_TARGET_POPCNT
475 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
476 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
477 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
481 ATTRIBUTE_NO_SANITIZE_ALL
482 ATTRIBUTE_TARGET_POPCNT
483 void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) {
484 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
485 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
489 ATTRIBUTE_NO_SANITIZE_ALL
490 ATTRIBUTE_TARGET_POPCNT
491 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
492 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
493 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
497 ATTRIBUTE_NO_SANITIZE_ALL
498 ATTRIBUTE_TARGET_POPCNT
499 void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) {
500 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
501 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
505 ATTRIBUTE_NO_SANITIZE_ALL
506 ATTRIBUTE_TARGET_POPCNT
507 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
508 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
509 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
513 ATTRIBUTE_NO_SANITIZE_ALL
514 ATTRIBUTE_TARGET_POPCNT
515 void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) {
516 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
517 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
521 ATTRIBUTE_NO_SANITIZE_ALL
522 ATTRIBUTE_TARGET_POPCNT
523 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
524 uint64_t N = Cases[0];
525 uint64_t ValSizeInBits = Cases[1];
526 uint64_t *Vals = Cases + 2;
527 // Skip the most common and the most boring case: all switch values are small.
528 // We may want to skip this at compile-time, but it will make the
529 // instrumentation less general.
530 if (Vals[N - 1] < 256)
532 // Also skip small inputs values, they won't give good signal.
535 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
537 uint64_t Smaller = 0;
538 uint64_t Larger = ~(uint64_t)0;
539 // Find two switch values such that Smaller < Val < Larger.
540 // Use 0 and 0xfff..f as the defaults.
541 for (i = 0; i < N; i++) {
546 if (Val > Vals[i]) Smaller = Vals[i];
549 // Apply HandleCmp to {Val,Smaller} and {Val, Larger},
550 // use i as the PC modifier for HandleCmp.
551 if (ValSizeInBits == 16) {
552 fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint16_t>(Val),
553 (uint16_t)(Smaller));
554 fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint16_t>(Val),
556 } else if (ValSizeInBits == 32) {
557 fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint32_t>(Val),
558 (uint32_t)(Smaller));
559 fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint32_t>(Val),
562 fuzzer::TPC.HandleCmp(PC + 2*i, Val, Smaller);
563 fuzzer::TPC.HandleCmp(PC + 2*i + 1, Val, Larger);
568 ATTRIBUTE_NO_SANITIZE_ALL
569 ATTRIBUTE_TARGET_POPCNT
570 void __sanitizer_cov_trace_div4(uint32_t Val) {
571 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
572 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
576 ATTRIBUTE_NO_SANITIZE_ALL
577 ATTRIBUTE_TARGET_POPCNT
578 void __sanitizer_cov_trace_div8(uint64_t Val) {
579 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
580 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
584 ATTRIBUTE_NO_SANITIZE_ALL
585 ATTRIBUTE_TARGET_POPCNT
586 void __sanitizer_cov_trace_gep(uintptr_t Idx) {
587 uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
588 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
591 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
592 void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
593 const void *s2, size_t n, int result) {
594 if (!fuzzer::RunningUserCallback) return;
595 if (result == 0) return; // No reason to mutate.
596 if (n <= 1) return; // Not interesting.
597 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
600 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
601 void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
602 const char *s2, size_t n, int result) {
603 if (!fuzzer::RunningUserCallback) return;
604 if (result == 0) return; // No reason to mutate.
605 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
606 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
607 n = std::min(n, Len1);
608 n = std::min(n, Len2);
609 if (n <= 1) return; // Not interesting.
610 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
613 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
614 void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
615 const char *s2, int result) {
616 if (!fuzzer::RunningUserCallback) return;
617 if (result == 0) return; // No reason to mutate.
618 size_t N = fuzzer::InternalStrnlen2(s1, s2);
619 if (N <= 1) return; // Not interesting.
620 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
623 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
624 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
625 const char *s2, size_t n, int result) {
626 if (!fuzzer::RunningUserCallback) return;
627 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
630 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
631 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
632 const char *s2, int result) {
633 if (!fuzzer::RunningUserCallback) return;
634 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
637 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
638 void __sanitizer_weak_hook_strstr(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));
644 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
645 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
646 const char *s2, char *result) {
647 if (!fuzzer::RunningUserCallback) return;
648 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
651 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
652 void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
653 const void *s2, size_t len2, void *result) {
654 if (!fuzzer::RunningUserCallback) return;
655 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);