1*0b57cec5SDimitry Andric //===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // fuzzer::TracePC 9*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 10*0b57cec5SDimitry Andric 11*0b57cec5SDimitry Andric #ifndef LLVM_FUZZER_TRACE_PC 12*0b57cec5SDimitry Andric #define LLVM_FUZZER_TRACE_PC 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "FuzzerDefs.h" 15*0b57cec5SDimitry Andric #include "FuzzerDictionary.h" 16*0b57cec5SDimitry Andric #include "FuzzerValueBitMap.h" 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #include <set> 19*0b57cec5SDimitry Andric #include <unordered_map> 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric namespace fuzzer { 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric // TableOfRecentCompares (TORC) remembers the most recently performed 24*0b57cec5SDimitry Andric // comparisons of type T. 25*0b57cec5SDimitry Andric // We record the arguments of CMP instructions in this table unconditionally 26*0b57cec5SDimitry Andric // because it seems cheaper this way than to compute some expensive 27*0b57cec5SDimitry Andric // conditions inside __sanitizer_cov_trace_cmp*. 28*0b57cec5SDimitry Andric // After the unit has been executed we may decide to use the contents of 29*0b57cec5SDimitry Andric // this table to populate a Dictionary. 30*0b57cec5SDimitry Andric template<class T, size_t kSizeT> 31*0b57cec5SDimitry Andric struct TableOfRecentCompares { 32*0b57cec5SDimitry Andric static const size_t kSize = kSizeT; 33*0b57cec5SDimitry Andric struct Pair { 34*0b57cec5SDimitry Andric T A, B; 35*0b57cec5SDimitry Andric }; 36*0b57cec5SDimitry Andric ATTRIBUTE_NO_SANITIZE_ALL 37*0b57cec5SDimitry Andric void Insert(size_t Idx, const T &Arg1, const T &Arg2) { 38*0b57cec5SDimitry Andric Idx = Idx % kSize; 39*0b57cec5SDimitry Andric Table[Idx].A = Arg1; 40*0b57cec5SDimitry Andric Table[Idx].B = Arg2; 41*0b57cec5SDimitry Andric } 42*0b57cec5SDimitry Andric 43*0b57cec5SDimitry Andric Pair Get(size_t I) { return Table[I % kSize]; } 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric Pair Table[kSize]; 46*0b57cec5SDimitry Andric }; 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric template <size_t kSizeT> 49*0b57cec5SDimitry Andric struct MemMemTable { 50*0b57cec5SDimitry Andric static const size_t kSize = kSizeT; 51*0b57cec5SDimitry Andric Word MemMemWords[kSize]; 52*0b57cec5SDimitry Andric Word EmptyWord; 53*0b57cec5SDimitry Andric 54*0b57cec5SDimitry Andric void Add(const uint8_t *Data, size_t Size) { 55*0b57cec5SDimitry Andric if (Size <= 2) return; 56*0b57cec5SDimitry Andric Size = std::min(Size, Word::GetMaxSize()); 57*0b57cec5SDimitry Andric size_t Idx = SimpleFastHash(Data, Size) % kSize; 58*0b57cec5SDimitry Andric MemMemWords[Idx].Set(Data, Size); 59*0b57cec5SDimitry Andric } 60*0b57cec5SDimitry Andric const Word &Get(size_t Idx) { 61*0b57cec5SDimitry Andric for (size_t i = 0; i < kSize; i++) { 62*0b57cec5SDimitry Andric const Word &W = MemMemWords[(Idx + i) % kSize]; 63*0b57cec5SDimitry Andric if (W.size()) return W; 64*0b57cec5SDimitry Andric } 65*0b57cec5SDimitry Andric EmptyWord.Set(nullptr, 0); 66*0b57cec5SDimitry Andric return EmptyWord; 67*0b57cec5SDimitry Andric } 68*0b57cec5SDimitry Andric }; 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric class TracePC { 71*0b57cec5SDimitry Andric public: 72*0b57cec5SDimitry Andric void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop); 73*0b57cec5SDimitry Andric void HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop); 74*0b57cec5SDimitry Andric void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); 75*0b57cec5SDimitry Andric template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2); 76*0b57cec5SDimitry Andric size_t GetTotalPCCoverage(); 77*0b57cec5SDimitry Andric void SetUseCounters(bool UC) { UseCounters = UC; } 78*0b57cec5SDimitry Andric void SetUseValueProfileMask(uint32_t VPMask) { UseValueProfileMask = VPMask; } 79*0b57cec5SDimitry Andric void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } 80*0b57cec5SDimitry Andric void SetPrintNewFuncs(size_t P) { NumPrintNewFuncs = P; } 81*0b57cec5SDimitry Andric void UpdateObservedPCs(); 82*0b57cec5SDimitry Andric template <class Callback> void CollectFeatures(Callback CB) const; 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric void ResetMaps() { 85*0b57cec5SDimitry Andric ValueProfileMap.Reset(); 86*0b57cec5SDimitry Andric ClearExtraCounters(); 87*0b57cec5SDimitry Andric ClearInlineCounters(); 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric void ClearInlineCounters(); 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); 93*0b57cec5SDimitry Andric void PrintFeatureSet(); 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric void PrintModuleInfo(); 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric void PrintCoverage(); 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric template<class CallBack> 100*0b57cec5SDimitry Andric void IterateCoveredFunctions(CallBack CB); 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, 103*0b57cec5SDimitry Andric size_t n, bool StopAtZero); 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric TableOfRecentCompares<uint32_t, 32> TORC4; 106*0b57cec5SDimitry Andric TableOfRecentCompares<uint64_t, 32> TORC8; 107*0b57cec5SDimitry Andric TableOfRecentCompares<Word, 32> TORCW; 108*0b57cec5SDimitry Andric MemMemTable<1024> MMT; 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric void RecordInitialStack(); 111*0b57cec5SDimitry Andric uintptr_t GetMaxStackOffset() const; 112*0b57cec5SDimitry Andric 113*0b57cec5SDimitry Andric template<class CallBack> 114*0b57cec5SDimitry Andric void ForEachObservedPC(CallBack CB) { 115*0b57cec5SDimitry Andric for (auto PC : ObservedPCs) 116*0b57cec5SDimitry Andric CB(PC); 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric void SetFocusFunction(const std::string &FuncName); 120*0b57cec5SDimitry Andric bool ObservedFocusFunction(); 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric void ProtectLazyCounters(); 123*0b57cec5SDimitry Andric bool UnprotectLazyCounters(void *CounterPtr); 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric struct PCTableEntry { 126*0b57cec5SDimitry Andric uintptr_t PC, PCFlags; 127*0b57cec5SDimitry Andric }; 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric uintptr_t PCTableEntryIdx(const PCTableEntry *TE); 130*0b57cec5SDimitry Andric const PCTableEntry *PCTableEntryByIdx(uintptr_t Idx); 131*0b57cec5SDimitry Andric static uintptr_t GetNextInstructionPc(uintptr_t PC); 132*0b57cec5SDimitry Andric bool PcIsFuncEntry(const PCTableEntry *TE) { return TE->PCFlags & 1; } 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric private: 135*0b57cec5SDimitry Andric bool UseCounters = false; 136*0b57cec5SDimitry Andric uint32_t UseValueProfileMask = false; 137*0b57cec5SDimitry Andric bool DoPrintNewPCs = false; 138*0b57cec5SDimitry Andric size_t NumPrintNewFuncs = 0; 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric // Module represents the array of 8-bit counters split into regions 141*0b57cec5SDimitry Andric // such that every region, except maybe the first and the last one, is one 142*0b57cec5SDimitry Andric // full page. 143*0b57cec5SDimitry Andric struct Module { 144*0b57cec5SDimitry Andric struct Region { 145*0b57cec5SDimitry Andric uint8_t *Start, *Stop; 146*0b57cec5SDimitry Andric bool Enabled; 147*0b57cec5SDimitry Andric bool OneFullPage; 148*0b57cec5SDimitry Andric }; 149*0b57cec5SDimitry Andric Region *Regions; 150*0b57cec5SDimitry Andric size_t NumRegions; 151*0b57cec5SDimitry Andric uint8_t *Start() { return Regions[0].Start; } 152*0b57cec5SDimitry Andric uint8_t *Stop() { return Regions[NumRegions - 1].Stop; } 153*0b57cec5SDimitry Andric size_t Size() { return Stop() - Start(); } 154*0b57cec5SDimitry Andric size_t Idx(uint8_t *P) { 155*0b57cec5SDimitry Andric assert(P >= Start() && P < Stop()); 156*0b57cec5SDimitry Andric return P - Start(); 157*0b57cec5SDimitry Andric } 158*0b57cec5SDimitry Andric }; 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric Module Modules[4096]; 161*0b57cec5SDimitry Andric size_t NumModules; // linker-initialized. 162*0b57cec5SDimitry Andric size_t NumInline8bitCounters; 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric template <class Callback> 165*0b57cec5SDimitry Andric void IterateCounterRegions(Callback CB) { 166*0b57cec5SDimitry Andric for (size_t m = 0; m < NumModules; m++) 167*0b57cec5SDimitry Andric for (size_t r = 0; r < Modules[m].NumRegions; r++) 168*0b57cec5SDimitry Andric CB(Modules[m].Regions[r]); 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096]; 172*0b57cec5SDimitry Andric size_t NumPCTables; 173*0b57cec5SDimitry Andric size_t NumPCsInPCTables; 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric Set<const PCTableEntry*> ObservedPCs; 176*0b57cec5SDimitry Andric std::unordered_map<uintptr_t, uintptr_t> ObservedFuncs; // PC => Counter. 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric uint8_t *FocusFunctionCounterPtr = nullptr; 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric ValueBitMap ValueProfileMap; 181*0b57cec5SDimitry Andric uintptr_t InitialStack; 182*0b57cec5SDimitry Andric }; 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric template <class Callback> 185*0b57cec5SDimitry Andric // void Callback(size_t FirstFeature, size_t Idx, uint8_t Value); 186*0b57cec5SDimitry Andric ATTRIBUTE_NO_SANITIZE_ALL 187*0b57cec5SDimitry Andric size_t ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End, 188*0b57cec5SDimitry Andric size_t FirstFeature, Callback Handle8bitCounter) { 189*0b57cec5SDimitry Andric typedef uintptr_t LargeType; 190*0b57cec5SDimitry Andric const size_t Step = sizeof(LargeType) / sizeof(uint8_t); 191*0b57cec5SDimitry Andric const size_t StepMask = Step - 1; 192*0b57cec5SDimitry Andric auto P = Begin; 193*0b57cec5SDimitry Andric // Iterate by 1 byte until either the alignment boundary or the end. 194*0b57cec5SDimitry Andric for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++) 195*0b57cec5SDimitry Andric if (uint8_t V = *P) 196*0b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin, V); 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric // Iterate by Step bytes at a time. 199*0b57cec5SDimitry Andric for (; P < End; P += Step) 200*0b57cec5SDimitry Andric if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) 201*0b57cec5SDimitry Andric for (size_t I = 0; I < Step; I++, Bundle >>= 8) 202*0b57cec5SDimitry Andric if (uint8_t V = Bundle & 0xff) 203*0b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin + I, V); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric // Iterate by 1 byte until the end. 206*0b57cec5SDimitry Andric for (; P < End; P++) 207*0b57cec5SDimitry Andric if (uint8_t V = *P) 208*0b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin, V); 209*0b57cec5SDimitry Andric return End - Begin; 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric // Given a non-zero Counter returns a number in the range [0,7]. 213*0b57cec5SDimitry Andric template<class T> 214*0b57cec5SDimitry Andric unsigned CounterToFeature(T Counter) { 215*0b57cec5SDimitry Andric // Returns a feature number by placing Counters into buckets as illustrated 216*0b57cec5SDimitry Andric // below. 217*0b57cec5SDimitry Andric // 218*0b57cec5SDimitry Andric // Counter bucket: [1] [2] [3] [4-7] [8-15] [16-31] [32-127] [128+] 219*0b57cec5SDimitry Andric // Feature number: 0 1 2 3 4 5 6 7 220*0b57cec5SDimitry Andric // 221*0b57cec5SDimitry Andric // This is a heuristic taken from AFL (see 222*0b57cec5SDimitry Andric // http://lcamtuf.coredump.cx/afl/technical_details.txt). 223*0b57cec5SDimitry Andric // 224*0b57cec5SDimitry Andric // This implementation may change in the future so clients should 225*0b57cec5SDimitry Andric // not rely on it. 226*0b57cec5SDimitry Andric assert(Counter); 227*0b57cec5SDimitry Andric unsigned Bit = 0; 228*0b57cec5SDimitry Andric /**/ if (Counter >= 128) Bit = 7; 229*0b57cec5SDimitry Andric else if (Counter >= 32) Bit = 6; 230*0b57cec5SDimitry Andric else if (Counter >= 16) Bit = 5; 231*0b57cec5SDimitry Andric else if (Counter >= 8) Bit = 4; 232*0b57cec5SDimitry Andric else if (Counter >= 4) Bit = 3; 233*0b57cec5SDimitry Andric else if (Counter >= 3) Bit = 2; 234*0b57cec5SDimitry Andric else if (Counter >= 2) Bit = 1; 235*0b57cec5SDimitry Andric return Bit; 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric template <class Callback> // void Callback(size_t Feature) 239*0b57cec5SDimitry Andric ATTRIBUTE_NO_SANITIZE_ADDRESS 240*0b57cec5SDimitry Andric ATTRIBUTE_NOINLINE 241*0b57cec5SDimitry Andric void TracePC::CollectFeatures(Callback HandleFeature) const { 242*0b57cec5SDimitry Andric auto Handle8bitCounter = [&](size_t FirstFeature, 243*0b57cec5SDimitry Andric size_t Idx, uint8_t Counter) { 244*0b57cec5SDimitry Andric if (UseCounters) 245*0b57cec5SDimitry Andric HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter)); 246*0b57cec5SDimitry Andric else 247*0b57cec5SDimitry Andric HandleFeature(FirstFeature + Idx); 248*0b57cec5SDimitry Andric }; 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric size_t FirstFeature = 0; 251*0b57cec5SDimitry Andric 252*0b57cec5SDimitry Andric for (size_t i = 0; i < NumModules; i++) { 253*0b57cec5SDimitry Andric for (size_t r = 0; r < Modules[i].NumRegions; r++) { 254*0b57cec5SDimitry Andric if (!Modules[i].Regions[r].Enabled) continue; 255*0b57cec5SDimitry Andric FirstFeature += 8 * ForEachNonZeroByte(Modules[i].Regions[r].Start, 256*0b57cec5SDimitry Andric Modules[i].Regions[r].Stop, 257*0b57cec5SDimitry Andric FirstFeature, Handle8bitCounter); 258*0b57cec5SDimitry Andric } 259*0b57cec5SDimitry Andric } 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric FirstFeature += 262*0b57cec5SDimitry Andric 8 * ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), 263*0b57cec5SDimitry Andric FirstFeature, Handle8bitCounter); 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric if (UseValueProfileMask) { 266*0b57cec5SDimitry Andric ValueProfileMap.ForEach([&](size_t Idx) { 267*0b57cec5SDimitry Andric HandleFeature(FirstFeature + Idx); 268*0b57cec5SDimitry Andric }); 269*0b57cec5SDimitry Andric FirstFeature += ValueProfileMap.SizeInBits(); 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric // Step function, grows similar to 8 * Log_2(A). 273*0b57cec5SDimitry Andric auto StackDepthStepFunction = [](uint32_t A) -> uint32_t { 274*0b57cec5SDimitry Andric if (!A) return A; 275*0b57cec5SDimitry Andric uint32_t Log2 = Log(A); 276*0b57cec5SDimitry Andric if (Log2 < 3) return A; 277*0b57cec5SDimitry Andric Log2 -= 3; 278*0b57cec5SDimitry Andric return (Log2 + 1) * 8 + ((A >> Log2) & 7); 279*0b57cec5SDimitry Andric }; 280*0b57cec5SDimitry Andric assert(StackDepthStepFunction(1024) == 64); 281*0b57cec5SDimitry Andric assert(StackDepthStepFunction(1024 * 4) == 80); 282*0b57cec5SDimitry Andric assert(StackDepthStepFunction(1024 * 1024) == 144); 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric if (auto MaxStackOffset = GetMaxStackOffset()) 285*0b57cec5SDimitry Andric HandleFeature(FirstFeature + StackDepthStepFunction(MaxStackOffset / 8)); 286*0b57cec5SDimitry Andric } 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric extern TracePC TPC; 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric } // namespace fuzzer 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric #endif // LLVM_FUZZER_TRACE_PC 293