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