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