1 //===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUZZER_TRACE_PC
13 #define LLVM_FUZZER_TRACE_PC
15 #include "FuzzerDefs.h"
16 #include "FuzzerDictionary.h"
17 #include "FuzzerValueBitMap.h"
23 // TableOfRecentCompares (TORC) remembers the most recently performed
24 // comparisons of type T.
25 // We record the arguments of CMP instructions in this table unconditionally
26 // because it seems cheaper this way than to compute some expensive
27 // conditions inside __sanitizer_cov_trace_cmp*.
28 // After the unit has been executed we may decide to use the contents of
29 // this table to populate a Dictionary.
30 template<class T, size_t kSizeT>
31 struct TableOfRecentCompares {
32 static const size_t kSize = kSizeT;
36 ATTRIBUTE_NO_SANITIZE_ALL
37 void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
43 Pair Get(size_t I) { return Table[I % kSize]; }
48 template <size_t kSizeT>
50 static const size_t kSize = kSizeT;
51 Word MemMemWords[kSize];
54 void Add(const uint8_t *Data, size_t Size) {
55 if (Size <= 2) return;
56 Size = std::min(Size, Word::GetMaxSize());
57 size_t Idx = SimpleFastHash(Data, Size) % kSize;
58 MemMemWords[Idx].Set(Data, Size);
60 const Word &Get(size_t Idx) {
61 for (size_t i = 0; i < kSize; i++) {
62 const Word &W = MemMemWords[(Idx + i) % kSize];
63 if (W.size()) return W;
65 EmptyWord.Set(nullptr, 0);
72 static const size_t kNumPCs = 1 << 21;
73 // How many bits of PC are used from __sanitizer_cov_trace_pc.
74 static const size_t kTracePcBits = 18;
76 void HandleInit(uint32_t *Start, uint32_t *Stop);
77 void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
78 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
79 template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
80 size_t GetTotalPCCoverage();
81 void SetUseCounters(bool UC) { UseCounters = UC; }
82 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
83 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
84 template <class Callback> void CollectFeatures(Callback CB) const;
87 ValueProfileMap.Reset();
88 memset(Counters(), 0, GetNumPCs());
92 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
93 void PrintFeatureSet();
95 void PrintModuleInfo();
100 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
101 size_t n, bool StopAtZero);
103 TableOfRecentCompares<uint32_t, 32> TORC4;
104 TableOfRecentCompares<uint64_t, 32> TORC8;
105 TableOfRecentCompares<Word, 32> TORCW;
106 MemMemTable<1024> MMT;
109 void InitializePrintNewPCs();
110 size_t GetNumPCs() const {
111 return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
113 uintptr_t GetPC(size_t Idx) {
114 assert(Idx < GetNumPCs());
119 bool UseCounters = false;
120 bool UseValueProfile = false;
121 bool DoPrintNewPCs = false;
124 uint32_t *Start, *Stop;
127 Module Modules[4096];
128 size_t NumModules; // linker-initialized.
129 size_t NumGuards; // linker-initialized.
131 struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
132 size_t NumModulesWithInline8bitCounters; // linker-initialized.
133 size_t NumInline8bitCounters;
135 uint8_t *Counters() const;
136 uintptr_t *PCs() const;
138 std::set<uintptr_t> *PrintedPCs;
140 ValueBitMap ValueProfileMap;
143 template <class Callback> // void Callback(size_t Idx, uint8_t Value);
144 ATTRIBUTE_NO_SANITIZE_ALL
145 void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
146 size_t FirstFeature, Callback Handle8bitCounter) {
147 typedef uintptr_t LargeType;
148 const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
149 const size_t StepMask = Step - 1;
151 // Iterate by 1 byte until either the alignment boundary or the end.
152 for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
154 Handle8bitCounter(FirstFeature + P - Begin, V);
156 // Iterate by Step bytes at a time.
157 for (; P < End; P += Step)
158 if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
159 for (size_t I = 0; I < Step; I++, Bundle >>= 8)
160 if (uint8_t V = Bundle & 0xff)
161 Handle8bitCounter(FirstFeature + P - Begin + I, V);
163 // Iterate by 1 byte until the end.
166 Handle8bitCounter(FirstFeature + P - Begin, V);
169 template <class Callback> // bool Callback(size_t Feature)
170 ATTRIBUTE_NO_SANITIZE_ALL
171 __attribute__((noinline))
172 void TracePC::CollectFeatures(Callback HandleFeature) const {
173 uint8_t *Counters = this->Counters();
174 size_t N = GetNumPCs();
175 auto Handle8bitCounter = [&](size_t Idx, uint8_t Counter) {
178 /**/ if (Counter >= 128) Bit = 7;
179 else if (Counter >= 32) Bit = 6;
180 else if (Counter >= 16) Bit = 5;
181 else if (Counter >= 8) Bit = 4;
182 else if (Counter >= 4) Bit = 3;
183 else if (Counter >= 3) Bit = 2;
184 else if (Counter >= 2) Bit = 1;
185 HandleFeature(Idx * 8 + Bit);
188 size_t FirstFeature = 0;
189 ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
190 FirstFeature += N * 8;
191 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
192 ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
193 FirstFeature, Handle8bitCounter);
194 FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
197 ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
201 ValueProfileMap.ForEach([&](size_t Idx) {
202 HandleFeature(N * 8 + Idx);
208 } // namespace fuzzer
210 #endif // LLVM_FUZZER_TRACE_PC