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

source code of compiler-rt/lib/fuzzer/FuzzerTracePC.cpp