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