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