george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Trace PCs. |
| 10 | // This module implements __sanitizer_cov_trace_pc_guard[_init], |
| 11 | // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "FuzzerTracePC.h" |
| 16 | #include "FuzzerCorpus.h" |
| 17 | #include "FuzzerDefs.h" |
| 18 | #include "FuzzerDictionary.h" |
| 19 | #include "FuzzerExtFunctions.h" |
| 20 | #include "FuzzerIO.h" |
| 21 | #include "FuzzerUtil.h" |
| 22 | #include "FuzzerValueBitMap.h" |
| 23 | #include <set> |
| 24 | |
| 25 | // The coverage counters and PCs. |
| 26 | // These are declared as global variables named "__sancov_*" to simplify |
| 27 | // experiments with inlined instrumentation. |
| 28 | alignas(64) ATTRIBUTE_INTERFACE |
| 29 | uint8_t __sancov_trace_pc_guard_8bit_counters[fuzzer::TracePC::kNumPCs]; |
| 30 | |
| 31 | ATTRIBUTE_INTERFACE |
| 32 | uintptr_t __sancov_trace_pc_pcs[fuzzer::TracePC::kNumPCs]; |
| 33 | |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 34 | // Used by -fsanitize-coverage=stack-depth to track stack depth |
morehouse | 398297f | 2017-08-22 21:28:29 +0000 | [diff] [blame] | 35 | ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) |
| 36 | thread_local uintptr_t __sancov_lowest_stack; |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 37 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 38 | namespace fuzzer { |
| 39 | |
| 40 | TracePC TPC; |
| 41 | |
| 42 | int ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr; |
| 43 | |
| 44 | uint8_t *TracePC::Counters() const { |
| 45 | return __sancov_trace_pc_guard_8bit_counters; |
| 46 | } |
| 47 | |
| 48 | uintptr_t *TracePC::PCs() const { |
| 49 | return __sancov_trace_pc_pcs; |
| 50 | } |
| 51 | |
| 52 | size_t TracePC::GetTotalPCCoverage() { |
| 53 | if (ObservedPCs.size()) |
| 54 | return ObservedPCs.size(); |
| 55 | size_t Res = 0; |
| 56 | for (size_t i = 1, N = GetNumPCs(); i < N; i++) |
| 57 | if (PCs()[i]) |
| 58 | Res++; |
| 59 | return Res; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) { |
| 64 | if (Start == Stop) return; |
| 65 | if (NumModulesWithInline8bitCounters && |
| 66 | ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return; |
| 67 | assert(NumModulesWithInline8bitCounters < |
| 68 | sizeof(ModuleCounters) / sizeof(ModuleCounters[0])); |
| 69 | ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop}; |
| 70 | NumInline8bitCounters += Stop - Start; |
| 71 | } |
| 72 | |
kcc | 98957a1 | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 73 | void TracePC::HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop) { |
| 74 | const PCTableEntry *B = reinterpret_cast<const PCTableEntry *>(Start); |
| 75 | const PCTableEntry *E = reinterpret_cast<const PCTableEntry *>(Stop); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 76 | if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return; |
| 77 | assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0])); |
| 78 | ModulePCTable[NumPCTables++] = {B, E}; |
| 79 | NumPCsInPCTables += E - B; |
| 80 | } |
| 81 | |
| 82 | void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) { |
| 83 | if (Start == Stop || *Start) return; |
| 84 | assert(NumModules < sizeof(Modules) / sizeof(Modules[0])); |
| 85 | for (uint32_t *P = Start; P < Stop; P++) { |
| 86 | NumGuards++; |
| 87 | if (NumGuards == kNumPCs) { |
| 88 | RawPrint( |
| 89 | "WARNING: The binary has too many instrumented PCs.\n" |
| 90 | " You may want to reduce the size of the binary\n" |
| 91 | " for more efficient fuzzing and precise coverage data\n"); |
| 92 | } |
| 93 | *P = NumGuards % kNumPCs; |
| 94 | } |
| 95 | Modules[NumModules].Start = Start; |
| 96 | Modules[NumModules].Stop = Stop; |
| 97 | NumModules++; |
| 98 | } |
| 99 | |
| 100 | void TracePC::PrintModuleInfo() { |
| 101 | if (NumGuards) { |
| 102 | Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards); |
| 103 | for (size_t i = 0; i < NumModules; i++) |
| 104 | Printf("%zd [%p, %p), ", Modules[i].Stop - Modules[i].Start, |
| 105 | Modules[i].Start, Modules[i].Stop); |
| 106 | Printf("\n"); |
| 107 | } |
| 108 | if (NumModulesWithInline8bitCounters) { |
| 109 | Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ", |
| 110 | NumModulesWithInline8bitCounters, NumInline8bitCounters); |
| 111 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) |
| 112 | Printf("%zd [%p, %p), ", ModuleCounters[i].Stop - ModuleCounters[i].Start, |
| 113 | ModuleCounters[i].Start, ModuleCounters[i].Stop); |
| 114 | Printf("\n"); |
| 115 | } |
| 116 | if (NumPCTables) { |
| 117 | Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables, |
| 118 | NumPCsInPCTables); |
| 119 | for (size_t i = 0; i < NumPCTables; i++) { |
| 120 | Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start, |
| 121 | ModulePCTable[i].Start, ModulePCTable[i].Stop); |
| 122 | } |
| 123 | Printf("\n"); |
| 124 | |
| 125 | if ((NumGuards && NumGuards != NumPCsInPCTables) || |
| 126 | (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables)) { |
kcc | e220ebb | 2017-10-14 00:07:11 +0000 | [diff] [blame] | 127 | Printf("ERROR: The size of coverage PC tables does not match the\n" |
| 128 | "number of instrumented PCs. This might be a compiler bug,\n" |
| 129 | "please contact the libFuzzer developers.\n" |
| 130 | "Also check https://bugs.llvm.org/show_bug.cgi?id=34636\n" |
| 131 | "for possible workarounds (tl;dr: don't use the old GNU ld)\n"); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 132 | _Exit(1); |
| 133 | } |
| 134 | } |
delcypher | fcd19a8 | 2018-04-20 06:46:19 +0000 | [diff] [blame] | 135 | if (size_t NumExtraCounters = ExtraCountersEnd() - ExtraCountersBegin()) |
| 136 | Printf("INFO: %zd Extra Counters\n", NumExtraCounters); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ATTRIBUTE_NO_SANITIZE_ALL |
| 140 | void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { |
| 141 | const uintptr_t kBits = 12; |
| 142 | const uintptr_t kMask = (1 << kBits) - 1; |
| 143 | uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits); |
| 144 | ValueProfileMap.AddValueModPrime(Idx); |
| 145 | } |
| 146 | |
| 147 | void TracePC::UpdateObservedPCs() { |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 148 | Vector<uintptr_t> CoveredFuncs; |
kcc | 00da648 | 2017-08-25 20:09:25 +0000 | [diff] [blame] | 149 | auto ObservePC = [&](uintptr_t PC) { |
kcc | 09b3e5f | 2018-07-06 19:47:00 +0000 | [diff] [blame] | 150 | if (ObservedPCs.insert(PC).second && DoPrintNewPCs) { |
| 151 | PrintPC("\tNEW_PC: %p %F %L", "\tNEW_PC: %p", PC + 1); |
| 152 | Printf("\n"); |
| 153 | } |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 154 | }; |
kcc | 00da648 | 2017-08-25 20:09:25 +0000 | [diff] [blame] | 155 | |
| 156 | auto Observe = [&](const PCTableEntry &TE) { |
| 157 | if (TE.PCFlags & 1) |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 158 | if (ObservedFuncs.insert(TE.PC).second && NumPrintNewFuncs) |
| 159 | CoveredFuncs.push_back(TE.PC); |
kcc | 00da648 | 2017-08-25 20:09:25 +0000 | [diff] [blame] | 160 | ObservePC(TE.PC); |
| 161 | }; |
| 162 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 163 | if (NumPCsInPCTables) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 164 | if (NumInline8bitCounters == NumPCsInPCTables) { |
| 165 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { |
| 166 | uint8_t *Beg = ModuleCounters[i].Start; |
| 167 | size_t Size = ModuleCounters[i].Stop - Beg; |
| 168 | assert(Size == |
| 169 | (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); |
| 170 | for (size_t j = 0; j < Size; j++) |
| 171 | if (Beg[j]) |
kcc | 00da648 | 2017-08-25 20:09:25 +0000 | [diff] [blame] | 172 | Observe(ModulePCTable[i].Start[j]); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 173 | } |
| 174 | } else if (NumGuards == NumPCsInPCTables) { |
| 175 | size_t GuardIdx = 1; |
| 176 | for (size_t i = 0; i < NumModules; i++) { |
| 177 | uint32_t *Beg = Modules[i].Start; |
| 178 | size_t Size = Modules[i].Stop - Beg; |
| 179 | assert(Size == |
| 180 | (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); |
| 181 | for (size_t j = 0; j < Size; j++, GuardIdx++) |
| 182 | if (Counters()[GuardIdx]) |
kcc | 00da648 | 2017-08-25 20:09:25 +0000 | [diff] [blame] | 183 | Observe(ModulePCTable[i].Start[j]); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 187 | |
| 188 | for (size_t i = 0, N = Min(CoveredFuncs.size(), NumPrintNewFuncs); i < N; i++) { |
kcc | 873dc11 | 2018-06-07 21:15:24 +0000 | [diff] [blame] | 189 | Printf("\tNEW_FUNC[%zd/%zd]: ", i + 1, CoveredFuncs.size()); |
kcc | 09b3e5f | 2018-07-06 19:47:00 +0000 | [diff] [blame] | 190 | PrintPC("%p %F %L", "%p", CoveredFuncs[i] + 1); |
| 191 | Printf("\n"); |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 192 | } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) { |
| 196 | // TODO: this implementation is x86 only. |
| 197 | // see sanitizer_common GetPreviousInstructionPc for full implementation. |
| 198 | return PC - 1; |
| 199 | } |
| 200 | |
| 201 | inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) { |
| 202 | // TODO: this implementation is x86 only. |
| 203 | // see sanitizer_common GetPreviousInstructionPc for full implementation. |
| 204 | return PC + 1; |
| 205 | } |
| 206 | |
| 207 | static std::string GetModuleName(uintptr_t PC) { |
| 208 | char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++? |
| 209 | void *OffsetRaw = nullptr; |
| 210 | if (!EF->__sanitizer_get_module_and_offset_for_pc( |
| 211 | reinterpret_cast<void *>(PC), ModulePathRaw, |
| 212 | sizeof(ModulePathRaw), &OffsetRaw)) |
| 213 | return ""; |
| 214 | return ModulePathRaw; |
| 215 | } |
| 216 | |
kcc | 85cad3d | 2018-05-11 01:17:52 +0000 | [diff] [blame] | 217 | template<class CallBack> |
| 218 | void TracePC::IterateCoveredFunctions(CallBack CB) { |
| 219 | for (size_t i = 0; i < NumPCTables; i++) { |
| 220 | auto &M = ModulePCTable[i]; |
| 221 | assert(M.Start < M.Stop); |
| 222 | auto ModuleName = GetModuleName(M.Start->PC); |
| 223 | for (auto NextFE = M.Start; NextFE < M.Stop; ) { |
| 224 | auto FE = NextFE; |
| 225 | assert((FE->PCFlags & 1) && "Not a function entry point"); |
| 226 | do { |
| 227 | NextFE++; |
| 228 | } while (NextFE < M.Stop && !(NextFE->PCFlags & 1)); |
| 229 | if (ObservedFuncs.count(FE->PC)) |
| 230 | CB(FE, NextFE); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
kcc | 3acbe07 | 2018-05-16 23:26:37 +0000 | [diff] [blame] | 235 | void TracePC::SetFocusFunction(const std::string &FuncName) { |
| 236 | // This function should be called once. |
| 237 | assert(FocusFunction.first > NumModulesWithInline8bitCounters); |
| 238 | if (FuncName.empty()) |
| 239 | return; |
| 240 | for (size_t M = 0; M < NumModulesWithInline8bitCounters; M++) { |
| 241 | auto &PCTE = ModulePCTable[M]; |
| 242 | size_t N = PCTE.Stop - PCTE.Start; |
| 243 | for (size_t I = 0; I < N; I++) { |
| 244 | if (!(PCTE.Start[I].PCFlags & 1)) continue; // not a function entry. |
| 245 | auto Name = DescribePC("%F", GetNextInstructionPc(PCTE.Start[I].PC)); |
| 246 | if (Name[0] == 'i' && Name[1] == 'n' && Name[2] == ' ') |
| 247 | Name = Name.substr(3, std::string::npos); |
| 248 | if (FuncName != Name) continue; |
| 249 | Printf("INFO: Focus function is set to '%s'\n", Name.c_str()); |
| 250 | FocusFunction = {M, I}; |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | bool TracePC::ObservedFocusFunction() { |
| 257 | size_t I = FocusFunction.first; |
| 258 | size_t J = FocusFunction.second; |
| 259 | if (I >= NumModulesWithInline8bitCounters) |
| 260 | return false; |
| 261 | auto &MC = ModuleCounters[I]; |
| 262 | size_t Size = MC.Stop - MC.Start; |
| 263 | if (J >= Size) |
| 264 | return false; |
| 265 | return MC.Start[J] != 0; |
| 266 | } |
| 267 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 268 | void TracePC::PrintCoverage() { |
| 269 | if (!EF->__sanitizer_symbolize_pc || |
| 270 | !EF->__sanitizer_get_module_and_offset_for_pc) { |
| 271 | Printf("INFO: __sanitizer_symbolize_pc or " |
| 272 | "__sanitizer_get_module_and_offset_for_pc is not available," |
| 273 | " not printing coverage\n"); |
| 274 | return; |
| 275 | } |
| 276 | Printf("COVERAGE:\n"); |
kcc | 85cad3d | 2018-05-11 01:17:52 +0000 | [diff] [blame] | 277 | auto CoveredFunctionCallback = [&](const PCTableEntry *First, const PCTableEntry *Last) { |
| 278 | assert(First < Last); |
| 279 | auto VisualizePC = GetNextInstructionPc(First->PC); |
| 280 | std::string FileStr = DescribePC("%s", VisualizePC); |
| 281 | if (!IsInterestingCoverageFile(FileStr)) return; |
| 282 | std::string FunctionStr = DescribePC("%F", VisualizePC); |
| 283 | std::string LineStr = DescribePC("%l", VisualizePC); |
| 284 | size_t Line = std::stoul(LineStr); |
morehouse | f64b940 | 2018-06-25 15:59:24 +0000 | [diff] [blame] | 285 | Vector<uintptr_t> UncoveredPCs; |
kcc | 85cad3d | 2018-05-11 01:17:52 +0000 | [diff] [blame] | 286 | for (auto TE = First; TE < Last; TE++) |
| 287 | if (!ObservedPCs.count(TE->PC)) |
| 288 | UncoveredPCs.push_back(TE->PC); |
| 289 | Printf("COVERED_FUNC: "); |
| 290 | UncoveredPCs.empty() |
| 291 | ? Printf("all") |
| 292 | : Printf("%zd/%zd", (Last - First) - UncoveredPCs.size(), Last - First); |
| 293 | Printf(" PCs covered %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(), |
| 294 | Line); |
| 295 | for (auto PC: UncoveredPCs) { |
| 296 | Printf(" UNCOVERED_PC: %s\n", |
| 297 | DescribePC("%s:%l", GetNextInstructionPc(PC)).c_str()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 298 | } |
| 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 | |
kcc | a7dd2a9 | 2018-05-21 19:47:00 +0000 | [diff] [blame] | 304 | void TracePC::DumpCoverage() { |
| 305 | if (EF->__sanitizer_dump_coverage) { |
| 306 | Vector<uintptr_t> PCsCopy(GetNumPCs()); |
| 307 | for (size_t i = 0; i < GetNumPCs(); i++) |
| 308 | PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0; |
| 309 | EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size()); |
| 310 | } |
| 311 | } |
| 312 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 313 | // Value profile. |
| 314 | // We keep track of various values that affect control flow. |
| 315 | // These values are inserted into a bit-set-based hash map. |
| 316 | // Every new bit in the map is treated as a new coverage. |
| 317 | // |
| 318 | // For memcmp/strcmp/etc the interesting value is the length of the common |
| 319 | // prefix of the parameters. |
| 320 | // For cmp instructions the interesting value is a XOR of the parameters. |
| 321 | // The interesting value is mixed up with the PC and is then added to the map. |
| 322 | |
| 323 | ATTRIBUTE_NO_SANITIZE_ALL |
| 324 | void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 325 | size_t n, bool StopAtZero) { |
| 326 | if (!n) return; |
| 327 | size_t Len = std::min(n, Word::GetMaxSize()); |
| 328 | const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); |
| 329 | const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); |
| 330 | uint8_t B1[Word::kMaxSize]; |
| 331 | uint8_t B2[Word::kMaxSize]; |
| 332 | // Copy the data into locals in this non-msan-instrumented function |
| 333 | // to avoid msan complaining further. |
| 334 | size_t Hash = 0; // Compute some simple hash of both strings. |
| 335 | for (size_t i = 0; i < Len; i++) { |
| 336 | B1[i] = A1[i]; |
| 337 | B2[i] = A2[i]; |
| 338 | size_t T = B1[i]; |
| 339 | Hash ^= (T << 8) | B2[i]; |
| 340 | } |
| 341 | size_t I = 0; |
| 342 | for (; I < Len; I++) |
| 343 | if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0)) |
| 344 | break; |
| 345 | size_t PC = reinterpret_cast<size_t>(caller_pc); |
| 346 | size_t Idx = (PC & 4095) | (I << 12); |
| 347 | ValueProfileMap.AddValue(Idx); |
| 348 | TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len)); |
| 349 | } |
| 350 | |
| 351 | template <class T> |
| 352 | ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE |
| 353 | ATTRIBUTE_NO_SANITIZE_ALL |
| 354 | void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) { |
| 355 | uint64_t ArgXor = Arg1 ^ Arg2; |
| 356 | uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65] |
| 357 | uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance; |
| 358 | if (sizeof(T) == 4) |
dor1s | e6729cb | 2018-07-16 15:15:34 +0000 | [diff] [blame^] | 359 | TORC4.Insert(ArgXor, Arg1, Arg2); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 360 | else if (sizeof(T) == 8) |
dor1s | e6729cb | 2018-07-16 15:15:34 +0000 | [diff] [blame^] | 361 | TORC8.Insert(ArgXor, Arg1, Arg2); |
kcc | 3850d06 | 2018-07-03 22:33:09 +0000 | [diff] [blame] | 362 | // TODO: remove these flags and instead use all metrics at once. |
| 363 | if (UseValueProfileMask & 1) |
| 364 | ValueProfileMap.AddValue(Idx); |
| 365 | if (UseValueProfileMask & 2) |
| 366 | ValueProfileMap.AddValue( |
| 367 | PC * 64 + (Arg1 == Arg2 ? 0 : __builtin_clzll(Arg1 - Arg2) + 1)); |
| 368 | if (UseValueProfileMask & 4) // alternative way to use the hamming distance |
| 369 | ValueProfileMap.AddValue(PC * 64 + ArgDistance); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static size_t InternalStrnlen(const char *S, size_t MaxLen) { |
| 373 | size_t Len = 0; |
| 374 | for (; Len < MaxLen && S[Len]; Len++) {} |
| 375 | return Len; |
| 376 | } |
| 377 | |
| 378 | // Finds min of (strlen(S1), strlen(S2)). |
| 379 | // Needed bacause one of these strings may actually be non-zero terminated. |
| 380 | static size_t InternalStrnlen2(const char *S1, const char *S2) { |
| 381 | size_t Len = 0; |
| 382 | for (; S1[Len] && S2[Len]; Len++) {} |
| 383 | return Len; |
| 384 | } |
| 385 | |
| 386 | void TracePC::ClearInlineCounters() { |
| 387 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { |
| 388 | uint8_t *Beg = ModuleCounters[i].Start; |
| 389 | size_t Size = ModuleCounters[i].Stop - Beg; |
| 390 | memset(Beg, 0, Size); |
| 391 | } |
| 392 | } |
| 393 | |
kcc | 0f3c031 | 2017-08-22 01:50:00 +0000 | [diff] [blame] | 394 | ATTRIBUTE_NO_SANITIZE_ALL |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 395 | void TracePC::RecordInitialStack() { |
kcc | 0f3c031 | 2017-08-22 01:50:00 +0000 | [diff] [blame] | 396 | int stack; |
| 397 | __sancov_lowest_stack = InitialStack = reinterpret_cast<uintptr_t>(&stack); |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | uintptr_t TracePC::GetMaxStackOffset() const { |
| 401 | return InitialStack - __sancov_lowest_stack; // Stack grows down |
| 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) { |
| 410 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 411 | uint32_t Idx = *Guard; |
| 412 | __sancov_trace_pc_pcs[Idx] = PC; |
| 413 | __sancov_trace_pc_guard_8bit_counters[Idx]++; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Best-effort support for -fsanitize-coverage=trace-pc, which is available |
| 417 | // in both Clang and GCC. |
| 418 | ATTRIBUTE_INTERFACE |
| 419 | ATTRIBUTE_NO_SANITIZE_ALL |
| 420 | void __sanitizer_cov_trace_pc() { |
| 421 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 422 | uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1); |
| 423 | __sancov_trace_pc_pcs[Idx] = PC; |
| 424 | __sancov_trace_pc_guard_8bit_counters[Idx]++; |
| 425 | } |
| 426 | |
| 427 | ATTRIBUTE_INTERFACE |
| 428 | void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) { |
| 429 | fuzzer::TPC.HandleInit(Start, Stop); |
| 430 | } |
| 431 | |
| 432 | ATTRIBUTE_INTERFACE |
| 433 | void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) { |
| 434 | fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop); |
| 435 | } |
| 436 | |
| 437 | ATTRIBUTE_INTERFACE |
kcc | 98957a1 | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 438 | void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, |
| 439 | const uintptr_t *pcs_end) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 440 | fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end); |
| 441 | } |
| 442 | |
| 443 | ATTRIBUTE_INTERFACE |
| 444 | ATTRIBUTE_NO_SANITIZE_ALL |
| 445 | void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { |
| 446 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 447 | fuzzer::TPC.HandleCallerCallee(PC, Callee); |
| 448 | } |
| 449 | |
| 450 | ATTRIBUTE_INTERFACE |
| 451 | ATTRIBUTE_NO_SANITIZE_ALL |
| 452 | ATTRIBUTE_TARGET_POPCNT |
| 453 | void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { |
| 454 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 455 | fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); |
| 456 | } |
| 457 | |
| 458 | ATTRIBUTE_INTERFACE |
| 459 | ATTRIBUTE_NO_SANITIZE_ALL |
| 460 | ATTRIBUTE_TARGET_POPCNT |
| 461 | // Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic |
| 462 | // the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however, |
| 463 | // should be changed later to make full use of instrumentation. |
| 464 | void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) { |
| 465 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 466 | fuzzer::TPC.HandleCmp(PC, Arg1, Arg2); |
| 467 | } |
| 468 | |
| 469 | ATTRIBUTE_INTERFACE |
| 470 | ATTRIBUTE_NO_SANITIZE_ALL |
| 471 | ATTRIBUTE_TARGET_POPCNT |
| 472 | void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { |
| 473 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_const_cmp4(uint32_t Arg1, uint32_t Arg2) { |
| 481 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_cmp2(uint16_t Arg1, uint16_t Arg2) { |
| 489 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_const_cmp2(uint16_t Arg1, uint16_t Arg2) { |
| 497 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_cmp1(uint8_t Arg1, uint8_t Arg2) { |
| 505 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_const_cmp1(uint8_t Arg1, uint8_t Arg2) { |
| 513 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 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_switch(uint64_t Val, uint64_t *Cases) { |
| 521 | uint64_t N = Cases[0]; |
| 522 | uint64_t ValSizeInBits = Cases[1]; |
| 523 | uint64_t *Vals = Cases + 2; |
| 524 | // Skip the most common and the most boring case. |
| 525 | if (Vals[N - 1] < 256 && Val < 256) |
| 526 | return; |
| 527 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 528 | size_t i; |
| 529 | uint64_t Token = 0; |
| 530 | for (i = 0; i < N; i++) { |
| 531 | Token = Val ^ Vals[i]; |
| 532 | if (Val < Vals[i]) |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | if (ValSizeInBits == 16) |
| 537 | fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0)); |
| 538 | else if (ValSizeInBits == 32) |
| 539 | fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0)); |
| 540 | else |
| 541 | fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0)); |
| 542 | } |
| 543 | |
| 544 | ATTRIBUTE_INTERFACE |
| 545 | ATTRIBUTE_NO_SANITIZE_ALL |
| 546 | ATTRIBUTE_TARGET_POPCNT |
| 547 | void __sanitizer_cov_trace_div4(uint32_t Val) { |
| 548 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 549 | fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0); |
| 550 | } |
| 551 | |
| 552 | ATTRIBUTE_INTERFACE |
| 553 | ATTRIBUTE_NO_SANITIZE_ALL |
| 554 | ATTRIBUTE_TARGET_POPCNT |
| 555 | void __sanitizer_cov_trace_div8(uint64_t Val) { |
| 556 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 557 | fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0); |
| 558 | } |
| 559 | |
| 560 | ATTRIBUTE_INTERFACE |
| 561 | ATTRIBUTE_NO_SANITIZE_ALL |
| 562 | ATTRIBUTE_TARGET_POPCNT |
| 563 | void __sanitizer_cov_trace_gep(uintptr_t Idx) { |
| 564 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 565 | fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0); |
| 566 | } |
| 567 | |
| 568 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 569 | void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, |
| 570 | const void *s2, size_t n, int result) { |
| 571 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 572 | if (result == 0) return; // No reason to mutate. |
| 573 | if (n <= 1) return; // Not interesting. |
| 574 | fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false); |
| 575 | } |
| 576 | |
| 577 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 578 | void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, |
| 579 | const char *s2, size_t n, int result) { |
| 580 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 581 | if (result == 0) return; // No reason to mutate. |
| 582 | size_t Len1 = fuzzer::InternalStrnlen(s1, n); |
| 583 | size_t Len2 = fuzzer::InternalStrnlen(s2, n); |
| 584 | n = std::min(n, Len1); |
| 585 | n = std::min(n, Len2); |
| 586 | if (n <= 1) return; // Not interesting. |
| 587 | fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true); |
| 588 | } |
| 589 | |
| 590 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 591 | void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, |
dor1s | e6729cb | 2018-07-16 15:15:34 +0000 | [diff] [blame^] | 592 | const char *s2, int result) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 593 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 594 | if (result == 0) return; // No reason to mutate. |
| 595 | size_t N = fuzzer::InternalStrnlen2(s1, s2); |
| 596 | if (N <= 1) return; // Not interesting. |
| 597 | fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true); |
| 598 | } |
| 599 | |
| 600 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 601 | void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1, |
| 602 | const char *s2, size_t n, int result) { |
| 603 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 604 | return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result); |
| 605 | } |
| 606 | |
| 607 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 608 | void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1, |
| 609 | const char *s2, int result) { |
| 610 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 611 | return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result); |
| 612 | } |
| 613 | |
| 614 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 615 | void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1, |
| 616 | const char *s2, char *result) { |
| 617 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 618 | fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); |
| 619 | } |
| 620 | |
| 621 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 622 | void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1, |
| 623 | const char *s2, char *result) { |
| 624 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 625 | fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); |
| 626 | } |
| 627 | |
| 628 | ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY |
| 629 | void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1, |
| 630 | const void *s2, size_t len2, void *result) { |
| 631 | if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return; |
| 632 | fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2); |
| 633 | } |
| 634 | } // extern "C" |