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 "FuzzerExtFunctions.h"
20 #include "FuzzerIO.h"
21 #include "FuzzerPlatform.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   size_t MaxFeatures = CollectFeatures([](uint32_t) {});
110   if (MaxFeatures > std::numeric_limits<uint32_t>::max())
111     Printf("WARNING: The coverage PC tables may produce up to %zu features.\n"
112            "This exceeds the maximum 32-bit value. Some features may be\n"
113            "ignored, and fuzzing may become less precise. If possible,\n"
114            "consider refactoring the fuzzer into several smaller fuzzers\n"
115            "linked against only a portion of the current target.\n",
116            MaxFeatures);
117 }
118 
119 ATTRIBUTE_NO_SANITIZE_ALL
120 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
121   const uintptr_t kBits = 12;
122   const uintptr_t kMask = (1 << kBits) - 1;
123   uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
124   ValueProfileMap.AddValueModPrime(Idx);
125 }
126 
127 /// \return the address of the previous instruction.
128 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.h`
129 inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
130 #if defined(__arm__)
131   // T32 (Thumb) branch instructions might be 16 or 32 bit long,
132   // so we return (pc-2) in that case in order to be safe.
133   // For A32 mode we return (pc-4) because all instructions are 32 bit long.
134   return (PC - 3) & (~1);
135 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
136   // PCs are always 4 byte aligned.
137   return PC - 4;
138 #elif defined(__sparc__) || defined(__mips__)
139   return PC - 8;
140 #else
141   return PC - 1;
142 #endif
143 }
144 
145 /// \return the address of the next instruction.
146 /// Note: the logic is copied from `sanitizer_common/sanitizer_stacktrace.cpp`
147 ALWAYS_INLINE uintptr_t TracePC::GetNextInstructionPc(uintptr_t PC) {
148 #if defined(__mips__)
149   return PC + 8;
150 #elif defined(__powerpc__) || defined(__sparc__) || defined(__arm__) || \
151     defined(__aarch64__)
152   return PC + 4;
153 #else
154   return PC + 1;
155 #endif
156 }
157 
158 void TracePC::UpdateObservedPCs() {
159   Vector<uintptr_t> CoveredFuncs;
160   auto ObservePC = [&](const PCTableEntry *TE) {
161     if (ObservedPCs.insert(TE).second && DoPrintNewPCs) {
162       PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p",
163               GetNextInstructionPc(TE->PC));
164       Printf("\n");
165     }
166   };
167 
168   auto Observe = [&](const PCTableEntry *TE) {
169     if (PcIsFuncEntry(TE))
170       if (++ObservedFuncs[TE->PC] == 1 && NumPrintNewFuncs)
171         CoveredFuncs.push_back(TE->PC);
172     ObservePC(TE);
173   };
174 
175   if (NumPCsInPCTables) {
176     if (NumInline8bitCounters == NumPCsInPCTables) {
177       for (size_t i = 0; i < NumModules; i++) {
178         auto &M = Modules[i];
179         assert(M.Size() ==
180                (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
181         for (size_t r = 0; r < M.NumRegions; r++) {
182           auto &R = M.Regions[r];
183           if (!R.Enabled) continue;
184           for (uint8_t *P = R.Start; P < R.Stop; P++)
185             if (*P)
186               Observe(&ModulePCTable[i].Start[M.Idx(P)]);
187         }
188       }
189     }
190   }
191 
192   for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N;
193        i++) {
194     Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size());
195     PrintPC("%p %F %L", "%p", GetNextInstructionPc(CoveredFuncs[i]));
196     Printf("\n");
197   }
198 }
199 
200 uintptr_t TracePC::PCTableEntryIdx(const PCTableEntry *TE) {
201   size_t TotalTEs = 0;
202   for (size_t i = 0; i < NumPCTables; i++) {
203     auto &M = ModulePCTable[i];
204     if (TE >= M.Start && TE < M.Stop)
205       return TotalTEs + TE - M.Start;
206     TotalTEs += M.Stop - M.Start;
207   }
208   assert(0);
209   return 0;
210 }
211 
212 const TracePC::PCTableEntry *TracePC::PCTableEntryByIdx(uintptr_t Idx) {
213   for (size_t i = 0; i < NumPCTables; i++) {
214     auto &M = ModulePCTable[i];
215     size_t Size = M.Stop - M.Start;
216     if (Idx < Size) return &M.Start[Idx];
217     Idx -= Size;
218   }
219   return nullptr;
220 }
221 
222 static std::string GetModuleName(uintptr_t PC) {
223   char ModulePathRaw[4096] = "";  // What's PATH_MAX in portable C++?
224   void *OffsetRaw = nullptr;
225   if (!EF->__sanitizer_get_module_and_offset_for_pc(
226       reinterpret_cast<void *>(PC), ModulePathRaw,
227       sizeof(ModulePathRaw), &OffsetRaw))
228     return "";
229   return ModulePathRaw;
230 }
231 
232 template<class CallBack>
233 void TracePC::IterateCoveredFunctions(CallBack CB) {
234   for (size_t i = 0; i < NumPCTables; i++) {
235     auto &M = ModulePCTable[i];
236     assert(M.Start < M.Stop);
237     auto ModuleName = GetModuleName(M.Start->PC);
238     for (auto NextFE = M.Start; NextFE < M.Stop; ) {
239       auto FE = NextFE;
240       assert(PcIsFuncEntry(FE) && "Not a function entry point");
241       do {
242         NextFE++;
243       } while (NextFE < M.Stop && !(PcIsFuncEntry(NextFE)));
244       CB(FE, NextFE, ObservedFuncs[FE->PC]);
245     }
246   }
247 }
248 
249 void TracePC::SetFocusFunction(const std::string &FuncName) {
250   // This function should be called once.
251   assert(!FocusFunctionCounterPtr);
252   // "auto" is not a valid function name. If this function is called with "auto"
253   // that means the auto focus functionality failed.
254   if (FuncName.empty() || FuncName == "auto")
255     return;
256   for (size_t M = 0; M < NumModules; M++) {
257     auto &PCTE = ModulePCTable[M];
258     size_t N = PCTE.Stop - PCTE.Start;
259     for (size_t I = 0; I < N; I++) {
260       if (!(PcIsFuncEntry(&PCTE.Start[I]))) continue;  // not a function entry.
261       auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC));
262       if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ')
263         Name = Name.substr(3, std::string::npos);
264       if (FuncName != Name) continue;
265       Printf("INFO: Focus function is set to '%s'\n", Name.c_str());
266       FocusFunctionCounterPtr = Modules[M].Start() + I;
267       return;
268     }
269   }
270 
271   Printf("ERROR: Failed to set focus function. Make sure the function name is "
272          "valid (%s) and symbolization is enabled.\n", FuncName.c_str());
273   exit(1);
274 }
275 
276 bool TracePC::ObservedFocusFunction() {
277   return FocusFunctionCounterPtr && *FocusFunctionCounterPtr;
278 }
279 
280 void TracePC::PrintCoverage(bool PrintAllCounters) {
281   if (!EF->__sanitizer_symbolize_pc ||
282       !EF->__sanitizer_get_module_and_offset_for_pc) {
283     Printf("INFO: __sanitizer_symbolize_pc or "
284            "__sanitizer_get_module_and_offset_for_pc is not available,"
285            " not printing coverage\n");
286     return;
287   }
288   Printf(PrintAllCounters ? "FULL COVERAGE:\n" : "COVERAGE:\n");
289   auto CoveredFunctionCallback = [&](const PCTableEntry *First,
290                                      const PCTableEntry *Last,
291                                      uintptr_t Counter) {
292     assert(First < Last);
293     auto VisualizePC = GetNextInstructionPc(First->PC);
294     std::string FileStr = DescribePC("%s", VisualizePC);
295     if (!IsInterestingCoverageFile(FileStr))
296       return;
297     std::string FunctionStr = DescribePC("%F", VisualizePC);
298     if (FunctionStr.find("in ") == 0)
299       FunctionStr = FunctionStr.substr(3);
300     std::string LineStr = DescribePC("%l", VisualizePC);
301     size_t NumEdges = Last - First;
302     Vector<uintptr_t> UncoveredPCs;
303     Vector<uintptr_t> CoveredPCs;
304     for (auto TE = First; TE < Last; TE++)
305       if (!ObservedPCs.count(TE))
306         UncoveredPCs.push_back(TE->PC);
307       else
308         CoveredPCs.push_back(TE->PC);
309 
310     if (PrintAllCounters) {
311       Printf("U");
312       for (auto PC : UncoveredPCs)
313         Printf(DescribePC(" %l", GetNextInstructionPc(PC)).c_str());
314       Printf("\n");
315 
316       Printf("C");
317       for (auto PC : CoveredPCs)
318         Printf(DescribePC(" %l", GetNextInstructionPc(PC)).c_str());
319       Printf("\n");
320     } else {
321       Printf("%sCOVERED_FUNC: hits: %zd", Counter ? "" : "UN", Counter);
322       Printf(" edges: %zd/%zd", NumEdges - UncoveredPCs.size(), NumEdges);
323       Printf(" %s %s:%s\n", FunctionStr.c_str(), FileStr.c_str(),
324              LineStr.c_str());
325       if (Counter)
326         for (auto PC : UncoveredPCs)
327           Printf("  UNCOVERED_PC: %s\n",
328                  DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str());
329     }
330   };
331 
332   IterateCoveredFunctions(CoveredFunctionCallback);
333 }
334 
335 // Value profile.
336 // We keep track of various values that affect control flow.
337 // These values are inserted into a bit-set-based hash map.
338 // Every new bit in the map is treated as a new coverage.
339 //
340 // For memcmp/strcmp/etc the interesting value is the length of the common
341 // prefix of the parameters.
342 // For cmp instructions the interesting value is a XOR of the parameters.
343 // The interesting value is mixed up with the PC and is then added to the map.
344 
345 ATTRIBUTE_NO_SANITIZE_ALL
346 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
347                                 size_t n, bool StopAtZero) {
348   if (!n) return;
349   size_t Len = std::min(n, Word::GetMaxSize());
350   const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
351   const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
352   uint8_t B1[Word::kMaxSize];
353   uint8_t B2[Word::kMaxSize];
354   // Copy the data into locals in this non-msan-instrumented function
355   // to avoid msan complaining further.
356   size_t Hash = 0;  // Compute some simple hash of both strings.
357   for (size_t i = 0; i < Len; i++) {
358     B1[i] = A1[i];
359     B2[i] = A2[i];
360     size_t T = B1[i];
361     Hash ^= (T << 8) | B2[i];
362   }
363   size_t I = 0;
364   uint8_t HammingDistance = 0;
365   for (; I < Len; I++) {
366     if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) {
367       HammingDistance = static_cast<uint8_t>(Popcountll(B1[I] ^ B2[I]));
368       break;
369     }
370   }
371   size_t PC = reinterpret_cast<size_t>(caller_pc);
372   size_t Idx = (PC & 4095) | (I << 12);
373   Idx += HammingDistance;
374   ValueProfileMap.AddValue(Idx);
375   TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
376 }
377 
378 template <class T>
379 ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
380 ATTRIBUTE_NO_SANITIZE_ALL
381 void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
382   uint64_t ArgXor = Arg1 ^ Arg2;
383   if (sizeof(T) == 4)
384       TORC4.Insert(ArgXor, Arg1, Arg2);
385   else if (sizeof(T) == 8)
386       TORC8.Insert(ArgXor, Arg1, Arg2);
387   uint64_t HammingDistance = Popcountll(ArgXor);  // [0,64]
388   uint64_t AbsoluteDistance = (Arg1 == Arg2 ? 0 : Clzll(Arg1 - Arg2) + 1);
389   ValueProfileMap.AddValue(PC * 128 + HammingDistance);
390   ValueProfileMap.AddValue(PC * 128 + 64 + AbsoluteDistance);
391 }
392 
393 static size_t InternalStrnlen(const char *S, size_t MaxLen) {
394   size_t Len = 0;
395   for (; Len < MaxLen && S[Len]; Len++) {}
396   return Len;
397 }
398 
399 // Finds min of (strlen(S1), strlen(S2)).
400 // Needed bacause one of these strings may actually be non-zero terminated.
401 static size_t InternalStrnlen2(const char *S1, const char *S2) {
402   size_t Len = 0;
403   for (; S1[Len] && S2[Len]; Len++)  {}
404   return Len;
405 }
406 
407 void TracePC::ClearInlineCounters() {
408   IterateCounterRegions([](const Module::Region &R){
409     if (R.Enabled)
410       memset(R.Start, 0, R.Stop - R.Start);
411   });
412 }
413 
414 ATTRIBUTE_NO_SANITIZE_ALL
415 void TracePC::RecordInitialStack() {
416   int stack;
417   __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack);
418 }
419 
420 uintptr_t TracePC::GetMaxStackOffset() const {
421   return InitialStack - __sancov_lowest_stack;  // Stack grows down
422 }
423 
424 void WarnAboutDeprecatedInstrumentation(const char *flag) {
425   // Use RawPrint because Printf cannot be used on Windows before OutputFile is
426   // initialized.
427   RawPrint(flag);
428   RawPrint(
429       " is no longer supported by libFuzzer.\n"
430       "Please either migrate to a compiler that supports -fsanitize=fuzzer\n"
431       "or use an older version of libFuzzer\n");
432   exit(1);
433 }
434 
435 } // namespace fuzzer
436 
437 extern "C" {
438 ATTRIBUTE_INTERFACE
439 ATTRIBUTE_NO_SANITIZE_ALL
440 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
441   fuzzer::WarnAboutDeprecatedInstrumentation(
442       "-fsanitize-coverage=trace-pc-guard");
443 }
444 
445 // Best-effort support for -fsanitize-coverage=trace-pc, which is available
446 // in both Clang and GCC.
447 ATTRIBUTE_INTERFACE
448 ATTRIBUTE_NO_SANITIZE_ALL
449 void __sanitizer_cov_trace_pc() {
450   fuzzer::WarnAboutDeprecatedInstrumentation("-fsanitize-coverage=trace-pc");
451 }
452 
453 ATTRIBUTE_INTERFACE
454 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
455   fuzzer::WarnAboutDeprecatedInstrumentation(
456       "-fsanitize-coverage=trace-pc-guard");
457 }
458 
459 ATTRIBUTE_INTERFACE
460 void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
461   fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
462 }
463 
464 ATTRIBUTE_INTERFACE
465 void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
466                               const uintptr_t *pcs_end) {
467   fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
468 }
469 
470 ATTRIBUTE_INTERFACE
471 ATTRIBUTE_NO_SANITIZE_ALL
472 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
473   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
474   fuzzer::TPC.HandleCallerCallee(PC, Callee);
475 }
476 
477 ATTRIBUTE_INTERFACE
478 ATTRIBUTE_NO_SANITIZE_ALL
479 ATTRIBUTE_TARGET_POPCNT
480 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
481   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
482   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
483 }
484 
485 ATTRIBUTE_INTERFACE
486 ATTRIBUTE_NO_SANITIZE_ALL
487 ATTRIBUTE_TARGET_POPCNT
488 // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic
489 // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however,
490 // should be changed later to make full use of instrumentation.
491 void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) {
492   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
493   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
494 }
495 
496 ATTRIBUTE_INTERFACE
497 ATTRIBUTE_NO_SANITIZE_ALL
498 ATTRIBUTE_TARGET_POPCNT
499 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
500   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
501   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
502 }
503 
504 ATTRIBUTE_INTERFACE
505 ATTRIBUTE_NO_SANITIZE_ALL
506 ATTRIBUTE_TARGET_POPCNT
507 void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) {
508   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
509   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
510 }
511 
512 ATTRIBUTE_INTERFACE
513 ATTRIBUTE_NO_SANITIZE_ALL
514 ATTRIBUTE_TARGET_POPCNT
515 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
516   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
517   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
518 }
519 
520 ATTRIBUTE_INTERFACE
521 ATTRIBUTE_NO_SANITIZE_ALL
522 ATTRIBUTE_TARGET_POPCNT
523 void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) {
524   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
525   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
526 }
527 
528 ATTRIBUTE_INTERFACE
529 ATTRIBUTE_NO_SANITIZE_ALL
530 ATTRIBUTE_TARGET_POPCNT
531 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
532   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
533   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
534 }
535 
536 ATTRIBUTE_INTERFACE
537 ATTRIBUTE_NO_SANITIZE_ALL
538 ATTRIBUTE_TARGET_POPCNT
539 void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) {
540   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
541   fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
542 }
543 
544 ATTRIBUTE_INTERFACE
545 ATTRIBUTE_NO_SANITIZE_ALL
546 ATTRIBUTE_TARGET_POPCNT
547 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
548   uint64_t N = Cases[0];
549   uint64_t ValSizeInBits = Cases[1];
550   uint64_t *Vals = Cases + 2;
551   // Skip the most common and the most boring case: all switch values are small.
552   // We may want to skip this at compile-time, but it will make the
553   // instrumentation less general.
554   if (Vals[N - 1]  < 256)
555     return;
556   // Also skip small inputs values, they won't give good signal.
557   if (Val < 256)
558     return;
559   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
560   size_t i;
561   uint64_t Smaller = 0;
562   uint64_t Larger = ~(uint64_t)0;
563   // Find two switch values such that Smaller < Val < Larger.
564   // Use 0 and 0xfff..f as the defaults.
565   for (i = 0; i < N; i++) {
566     if (Val < Vals[i]) {
567       Larger = Vals[i];
568       break;
569     }
570     if (Val > Vals[i]) Smaller = Vals[i];
571   }
572 
573   // Apply HandleCmp to {Val,Smaller} and {Val, Larger},
574   // use i as the PC modifier for HandleCmp.
575   if (ValSizeInBits == 16) {
576     fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint16_t>(Val),
577                           (uint16_t)(Smaller));
578     fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint16_t>(Val),
579                           (uint16_t)(Larger));
580   } else if (ValSizeInBits == 32) {
581     fuzzer::TPC.HandleCmp(PC + 2 * i, static_cast<uint32_t>(Val),
582                           (uint32_t)(Smaller));
583     fuzzer::TPC.HandleCmp(PC + 2 * i + 1, static_cast<uint32_t>(Val),
584                           (uint32_t)(Larger));
585   } else {
586     fuzzer::TPC.HandleCmp(PC + 2*i, Val, Smaller);
587     fuzzer::TPC.HandleCmp(PC + 2*i + 1, Val, Larger);
588   }
589 }
590 
591 ATTRIBUTE_INTERFACE
592 ATTRIBUTE_NO_SANITIZE_ALL
593 ATTRIBUTE_TARGET_POPCNT
594 void __sanitizer_cov_trace_div4(uint32_t Val) {
595   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
596   fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
597 }
598 
599 ATTRIBUTE_INTERFACE
600 ATTRIBUTE_NO_SANITIZE_ALL
601 ATTRIBUTE_TARGET_POPCNT
602 void __sanitizer_cov_trace_div8(uint64_t Val) {
603   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
604   fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
605 }
606 
607 ATTRIBUTE_INTERFACE
608 ATTRIBUTE_NO_SANITIZE_ALL
609 ATTRIBUTE_TARGET_POPCNT
610 void __sanitizer_cov_trace_gep(uintptr_t Idx) {
611   uintptr_t PC = reinterpret_cast<uintptr_t>(GET_CALLER_PC());
612   fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
613 }
614 
615 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
616 void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
617                                   const void *s2, size_t n, int result) {
618   if (!fuzzer::RunningUserCallback) return;
619   if (result == 0) return;  // No reason to mutate.
620   if (n <= 1) return;  // Not interesting.
621   fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
622 }
623 
624 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
625 void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
626                                    const char *s2, size_t n, int result) {
627   if (!fuzzer::RunningUserCallback) return;
628   if (result == 0) return;  // No reason to mutate.
629   size_t Len1 = fuzzer::InternalStrnlen(s1, n);
630   size_t Len2 = fuzzer::InternalStrnlen(s2, n);
631   n = std::min(n, Len1);
632   n = std::min(n, Len2);
633   if (n <= 1) return;  // Not interesting.
634   fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
635 }
636 
637 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
638 void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
639                                    const char *s2, int result) {
640   if (!fuzzer::RunningUserCallback) return;
641   if (result == 0) return;  // No reason to mutate.
642   size_t N = fuzzer::InternalStrnlen2(s1, s2);
643   if (N <= 1) return;  // Not interesting.
644   fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
645 }
646 
647 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
648 void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
649                                        const char *s2, size_t n, int result) {
650   if (!fuzzer::RunningUserCallback) return;
651   return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
652 }
653 
654 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
655 void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
656                                       const char *s2, int result) {
657   if (!fuzzer::RunningUserCallback) return;
658   return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
659 }
660 
661 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
662 void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
663                                   const char *s2, char *result) {
664   if (!fuzzer::RunningUserCallback) return;
665   fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
666 }
667 
668 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
669 void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
670                                       const char *s2, char *result) {
671   if (!fuzzer::RunningUserCallback) return;
672   fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
673 }
674 
675 ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
676 void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
677                                   const void *s2, size_t len2, void *result) {
678   if (!fuzzer::RunningUserCallback) return;
679   fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
680 }
681 }  // extern "C"
682