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