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