Bug Summary

File:projects/compiler-rt/lib/fuzzer/FuzzerTracePC.cpp
Warning:line 366, column 1
Address of stack memory associated with local variable 'stack' is still referred to by the global variable '__sancov_lowest_stack' upon returning to the caller. This will be a dangling reference

Annotated Source Code

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