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