10b57cec5SDimitry Andric //===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // fuzzer::TracePC
90b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #ifndef LLVM_FUZZER_TRACE_PC
120b57cec5SDimitry Andric #define LLVM_FUZZER_TRACE_PC
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "FuzzerDefs.h"
150b57cec5SDimitry Andric #include "FuzzerDictionary.h"
160b57cec5SDimitry Andric #include "FuzzerValueBitMap.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric #include <set>
190b57cec5SDimitry Andric #include <unordered_map>
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric namespace fuzzer {
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric // TableOfRecentCompares (TORC) remembers the most recently performed
240b57cec5SDimitry Andric // comparisons of type T.
250b57cec5SDimitry Andric // We record the arguments of CMP instructions in this table unconditionally
260b57cec5SDimitry Andric // because it seems cheaper this way than to compute some expensive
270b57cec5SDimitry Andric // conditions inside __sanitizer_cov_trace_cmp*.
280b57cec5SDimitry Andric // After the unit has been executed we may decide to use the contents of
290b57cec5SDimitry Andric // this table to populate a Dictionary.
300b57cec5SDimitry Andric template<class T, size_t kSizeT>
310b57cec5SDimitry Andric struct TableOfRecentCompares {
320b57cec5SDimitry Andric static const size_t kSize = kSizeT;
330b57cec5SDimitry Andric struct Pair {
340b57cec5SDimitry Andric T A, B;
350b57cec5SDimitry Andric };
360b57cec5SDimitry Andric ATTRIBUTE_NO_SANITIZE_ALL
InsertTableOfRecentCompares370b57cec5SDimitry Andric void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
380b57cec5SDimitry Andric Idx = Idx % kSize;
390b57cec5SDimitry Andric Table[Idx].A = Arg1;
400b57cec5SDimitry Andric Table[Idx].B = Arg2;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
GetTableOfRecentCompares430b57cec5SDimitry Andric Pair Get(size_t I) { return Table[I % kSize]; }
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric Pair Table[kSize];
460b57cec5SDimitry Andric };
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric template <size_t kSizeT>
490b57cec5SDimitry Andric struct MemMemTable {
500b57cec5SDimitry Andric static const size_t kSize = kSizeT;
510b57cec5SDimitry Andric Word MemMemWords[kSize];
520b57cec5SDimitry Andric Word EmptyWord;
530b57cec5SDimitry Andric
AddMemMemTable540b57cec5SDimitry Andric void Add(const uint8_t *Data, size_t Size) {
550b57cec5SDimitry Andric if (Size <= 2) return;
560b57cec5SDimitry Andric Size = std::min(Size, Word::GetMaxSize());
57fe6060f1SDimitry Andric auto Idx = SimpleFastHash(Data, Size) % kSize;
580b57cec5SDimitry Andric MemMemWords[Idx].Set(Data, Size);
590b57cec5SDimitry Andric }
GetMemMemTable600b57cec5SDimitry Andric const Word &Get(size_t Idx) {
610b57cec5SDimitry Andric for (size_t i = 0; i < kSize; i++) {
620b57cec5SDimitry Andric const Word &W = MemMemWords[(Idx + i) % kSize];
630b57cec5SDimitry Andric if (W.size()) return W;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric EmptyWord.Set(nullptr, 0);
660b57cec5SDimitry Andric return EmptyWord;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric };
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric class TracePC {
710b57cec5SDimitry Andric public:
720b57cec5SDimitry Andric void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
730b57cec5SDimitry Andric void HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop);
740b57cec5SDimitry Andric void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
750b57cec5SDimitry Andric template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
760b57cec5SDimitry Andric size_t GetTotalPCCoverage();
SetUseCounters(bool UC)770b57cec5SDimitry Andric void SetUseCounters(bool UC) { UseCounters = UC; }
SetUseValueProfileMask(uint32_t VPMask)780b57cec5SDimitry Andric void SetUseValueProfileMask(uint32_t VPMask) { UseValueProfileMask = VPMask; }
SetPrintNewPCs(bool P)790b57cec5SDimitry Andric void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
SetPrintNewFuncs(size_t P)800b57cec5SDimitry Andric void SetPrintNewFuncs(size_t P) { NumPrintNewFuncs = P; }
810b57cec5SDimitry Andric void UpdateObservedPCs();
82fe6060f1SDimitry Andric template <class Callback> size_t CollectFeatures(Callback CB) const;
830b57cec5SDimitry Andric
ResetMaps()840b57cec5SDimitry Andric void ResetMaps() {
850b57cec5SDimitry Andric ValueProfileMap.Reset();
860b57cec5SDimitry Andric ClearExtraCounters();
870b57cec5SDimitry Andric ClearInlineCounters();
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric void ClearInlineCounters();
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
930b57cec5SDimitry Andric void PrintFeatureSet();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric void PrintModuleInfo();
960b57cec5SDimitry Andric
97e8d8bef9SDimitry Andric void PrintCoverage(bool PrintAllCounters);
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric template<class CallBack>
1000b57cec5SDimitry Andric void IterateCoveredFunctions(CallBack CB);
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
1030b57cec5SDimitry Andric size_t n, bool StopAtZero);
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric TableOfRecentCompares<uint32_t, 32> TORC4;
1060b57cec5SDimitry Andric TableOfRecentCompares<uint64_t, 32> TORC8;
1070b57cec5SDimitry Andric TableOfRecentCompares<Word, 32> TORCW;
1080b57cec5SDimitry Andric MemMemTable<1024> MMT;
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric void RecordInitialStack();
1110b57cec5SDimitry Andric uintptr_t GetMaxStackOffset() const;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric template<class CallBack>
ForEachObservedPC(CallBack CB)1140b57cec5SDimitry Andric void ForEachObservedPC(CallBack CB) {
1150b57cec5SDimitry Andric for (auto PC : ObservedPCs)
1160b57cec5SDimitry Andric CB(PC);
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric void SetFocusFunction(const std::string &FuncName);
1200b57cec5SDimitry Andric bool ObservedFocusFunction();
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric struct PCTableEntry {
1230b57cec5SDimitry Andric uintptr_t PC, PCFlags;
1240b57cec5SDimitry Andric };
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric uintptr_t PCTableEntryIdx(const PCTableEntry *TE);
1270b57cec5SDimitry Andric const PCTableEntry *PCTableEntryByIdx(uintptr_t Idx);
1280b57cec5SDimitry Andric static uintptr_t GetNextInstructionPc(uintptr_t PC);
PcIsFuncEntry(const PCTableEntry * TE)1290b57cec5SDimitry Andric bool PcIsFuncEntry(const PCTableEntry *TE) { return TE->PCFlags & 1; }
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric private:
1320b57cec5SDimitry Andric bool UseCounters = false;
1330b57cec5SDimitry Andric uint32_t UseValueProfileMask = false;
1340b57cec5SDimitry Andric bool DoPrintNewPCs = false;
1350b57cec5SDimitry Andric size_t NumPrintNewFuncs = 0;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric // Module represents the array of 8-bit counters split into regions
1380b57cec5SDimitry Andric // such that every region, except maybe the first and the last one, is one
1390b57cec5SDimitry Andric // full page.
1400b57cec5SDimitry Andric struct Module {
1410b57cec5SDimitry Andric struct Region {
1420b57cec5SDimitry Andric uint8_t *Start, *Stop;
1430b57cec5SDimitry Andric bool Enabled;
1440b57cec5SDimitry Andric bool OneFullPage;
1450b57cec5SDimitry Andric };
1460b57cec5SDimitry Andric Region *Regions;
1470b57cec5SDimitry Andric size_t NumRegions;
StartModule1480b57cec5SDimitry Andric uint8_t *Start() { return Regions[0].Start; }
StopModule1490b57cec5SDimitry Andric uint8_t *Stop() { return Regions[NumRegions - 1].Stop; }
SizeModule1500b57cec5SDimitry Andric size_t Size() { return Stop() - Start(); }
IdxModule1510b57cec5SDimitry Andric size_t Idx(uint8_t *P) {
1520b57cec5SDimitry Andric assert(P >= Start() && P < Stop());
1530b57cec5SDimitry Andric return P - Start();
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric };
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric Module Modules[4096];
1580b57cec5SDimitry Andric size_t NumModules; // linker-initialized.
1590b57cec5SDimitry Andric size_t NumInline8bitCounters;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric template <class Callback>
IterateCounterRegions(Callback CB)1620b57cec5SDimitry Andric void IterateCounterRegions(Callback CB) {
1630b57cec5SDimitry Andric for (size_t m = 0; m < NumModules; m++)
1640b57cec5SDimitry Andric for (size_t r = 0; r < Modules[m].NumRegions; r++)
1650b57cec5SDimitry Andric CB(Modules[m].Regions[r]);
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096];
1690b57cec5SDimitry Andric size_t NumPCTables;
1700b57cec5SDimitry Andric size_t NumPCsInPCTables;
1710b57cec5SDimitry Andric
172*349cc55cSDimitry Andric std::set<const PCTableEntry *> ObservedPCs;
1730b57cec5SDimitry Andric std::unordered_map<uintptr_t, uintptr_t> ObservedFuncs; // PC => Counter.
1740b57cec5SDimitry Andric
1750b57cec5SDimitry Andric uint8_t *FocusFunctionCounterPtr = nullptr;
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric ValueBitMap ValueProfileMap;
1780b57cec5SDimitry Andric uintptr_t InitialStack;
1790b57cec5SDimitry Andric };
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andric template <class Callback>
1820b57cec5SDimitry Andric // void Callback(size_t FirstFeature, size_t Idx, uint8_t Value);
1830b57cec5SDimitry Andric ATTRIBUTE_NO_SANITIZE_ALL
ForEachNonZeroByte(const uint8_t * Begin,const uint8_t * End,size_t FirstFeature,Callback Handle8bitCounter)1840b57cec5SDimitry Andric size_t ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
1850b57cec5SDimitry Andric size_t FirstFeature, Callback Handle8bitCounter) {
1860b57cec5SDimitry Andric typedef uintptr_t LargeType;
1870b57cec5SDimitry Andric const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
1880b57cec5SDimitry Andric const size_t StepMask = Step - 1;
1890b57cec5SDimitry Andric auto P = Begin;
1900b57cec5SDimitry Andric // Iterate by 1 byte until either the alignment boundary or the end.
1910b57cec5SDimitry Andric for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
1920b57cec5SDimitry Andric if (uint8_t V = *P)
1930b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin, V);
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric // Iterate by Step bytes at a time.
196fe6060f1SDimitry Andric for (; P + Step <= End; P += Step)
197e8d8bef9SDimitry Andric if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) {
198e8d8bef9SDimitry Andric Bundle = HostToLE(Bundle);
1990b57cec5SDimitry Andric for (size_t I = 0; I < Step; I++, Bundle >>= 8)
2000b57cec5SDimitry Andric if (uint8_t V = Bundle & 0xff)
2010b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin + I, V);
202e8d8bef9SDimitry Andric }
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric // Iterate by 1 byte until the end.
2050b57cec5SDimitry Andric for (; P < End; P++)
2060b57cec5SDimitry Andric if (uint8_t V = *P)
2070b57cec5SDimitry Andric Handle8bitCounter(FirstFeature, P - Begin, V);
2080b57cec5SDimitry Andric return End - Begin;
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric // Given a non-zero Counter returns a number in the range [0,7].
2120b57cec5SDimitry Andric template<class T>
CounterToFeature(T Counter)2130b57cec5SDimitry Andric unsigned CounterToFeature(T Counter) {
2140b57cec5SDimitry Andric // Returns a feature number by placing Counters into buckets as illustrated
2150b57cec5SDimitry Andric // below.
2160b57cec5SDimitry Andric //
2170b57cec5SDimitry Andric // Counter bucket: [1] [2] [3] [4-7] [8-15] [16-31] [32-127] [128+]
2180b57cec5SDimitry Andric // Feature number: 0 1 2 3 4 5 6 7
2190b57cec5SDimitry Andric //
2200b57cec5SDimitry Andric // This is a heuristic taken from AFL (see
2210b57cec5SDimitry Andric // http://lcamtuf.coredump.cx/afl/technical_details.txt).
2220b57cec5SDimitry Andric //
2230b57cec5SDimitry Andric // This implementation may change in the future so clients should
2240b57cec5SDimitry Andric // not rely on it.
2250b57cec5SDimitry Andric assert(Counter);
2260b57cec5SDimitry Andric unsigned Bit = 0;
2270b57cec5SDimitry Andric /**/ if (Counter >= 128) Bit = 7;
2280b57cec5SDimitry Andric else if (Counter >= 32) Bit = 6;
2290b57cec5SDimitry Andric else if (Counter >= 16) Bit = 5;
2300b57cec5SDimitry Andric else if (Counter >= 8) Bit = 4;
2310b57cec5SDimitry Andric else if (Counter >= 4) Bit = 3;
2320b57cec5SDimitry Andric else if (Counter >= 3) Bit = 2;
2330b57cec5SDimitry Andric else if (Counter >= 2) Bit = 1;
2340b57cec5SDimitry Andric return Bit;
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
237fe6060f1SDimitry Andric template <class Callback> // void Callback(uint32_t Feature)
238fe6060f1SDimitry Andric ATTRIBUTE_NO_SANITIZE_ADDRESS ATTRIBUTE_NOINLINE size_t
CollectFeatures(Callback HandleFeature)239fe6060f1SDimitry Andric TracePC::CollectFeatures(Callback HandleFeature) const {
2400b57cec5SDimitry Andric auto Handle8bitCounter = [&](size_t FirstFeature,
2410b57cec5SDimitry Andric size_t Idx, uint8_t Counter) {
2420b57cec5SDimitry Andric if (UseCounters)
243fe6060f1SDimitry Andric HandleFeature(static_cast<uint32_t>(FirstFeature + Idx * 8 +
244fe6060f1SDimitry Andric CounterToFeature(Counter)));
2450b57cec5SDimitry Andric else
246fe6060f1SDimitry Andric HandleFeature(static_cast<uint32_t>(FirstFeature + Idx));
2470b57cec5SDimitry Andric };
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric size_t FirstFeature = 0;
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric for (size_t i = 0; i < NumModules; i++) {
2520b57cec5SDimitry Andric for (size_t r = 0; r < Modules[i].NumRegions; r++) {
2530b57cec5SDimitry Andric if (!Modules[i].Regions[r].Enabled) continue;
2540b57cec5SDimitry Andric FirstFeature += 8 * ForEachNonZeroByte(Modules[i].Regions[r].Start,
2550b57cec5SDimitry Andric Modules[i].Regions[r].Stop,
2560b57cec5SDimitry Andric FirstFeature, Handle8bitCounter);
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric FirstFeature +=
2610b57cec5SDimitry Andric 8 * ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(),
2620b57cec5SDimitry Andric FirstFeature, Handle8bitCounter);
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric if (UseValueProfileMask) {
2650b57cec5SDimitry Andric ValueProfileMap.ForEach([&](size_t Idx) {
266fe6060f1SDimitry Andric HandleFeature(static_cast<uint32_t>(FirstFeature + Idx));
2670b57cec5SDimitry Andric });
2680b57cec5SDimitry Andric FirstFeature += ValueProfileMap.SizeInBits();
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric // Step function, grows similar to 8 * Log_2(A).
272fe6060f1SDimitry Andric auto StackDepthStepFunction = [](size_t A) -> size_t {
273fe6060f1SDimitry Andric if (!A)
274fe6060f1SDimitry Andric return A;
275fe6060f1SDimitry Andric auto Log2 = Log(A);
276fe6060f1SDimitry Andric if (Log2 < 3)
277fe6060f1SDimitry Andric return A;
2780b57cec5SDimitry Andric Log2 -= 3;
2790b57cec5SDimitry Andric return (Log2 + 1) * 8 + ((A >> Log2) & 7);
2800b57cec5SDimitry Andric };
2810b57cec5SDimitry Andric assert(StackDepthStepFunction(1024) == 64);
2820b57cec5SDimitry Andric assert(StackDepthStepFunction(1024 * 4) == 80);
2830b57cec5SDimitry Andric assert(StackDepthStepFunction(1024 * 1024) == 144);
2840b57cec5SDimitry Andric
285fe6060f1SDimitry Andric if (auto MaxStackOffset = GetMaxStackOffset()) {
286fe6060f1SDimitry Andric HandleFeature(static_cast<uint32_t>(
287fe6060f1SDimitry Andric FirstFeature + StackDepthStepFunction(MaxStackOffset / 8)));
288fe6060f1SDimitry Andric FirstFeature += StackDepthStepFunction(std::numeric_limits<size_t>::max());
289fe6060f1SDimitry Andric }
290fe6060f1SDimitry Andric
291fe6060f1SDimitry Andric return FirstFeature;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric
2940b57cec5SDimitry Andric extern TracePC TPC;
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric } // namespace fuzzer
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric #endif // LLVM_FUZZER_TRACE_PC
299