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