blob: 54172608d180f29f379e5237da2a20aad15fcbad [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===//
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// fuzzer::TracePC
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_TRACE_PC
13#define LLVM_FUZZER_TRACE_PC
14
15#include "FuzzerDefs.h"
16#include "FuzzerDictionary.h"
17#include "FuzzerValueBitMap.h"
18
19#include <set>
20
21namespace fuzzer {
22
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.
30template<class T, size_t kSizeT>
31struct TableOfRecentCompares {
32 static const size_t kSize = kSizeT;
33 struct Pair {
34 T A, B;
35 };
36 ATTRIBUTE_NO_SANITIZE_ALL
37 void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
38 Idx = Idx % kSize;
39 Table[Idx].A = Arg1;
40 Table[Idx].B = Arg2;
41 }
42
43 Pair Get(size_t I) { return Table[I % kSize]; }
44
45 Pair Table[kSize];
46};
47
48template <size_t kSizeT>
49struct MemMemTable {
50 static const size_t kSize = kSizeT;
51 Word MemMemWords[kSize];
52 Word EmptyWord;
53
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);
59 }
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;
64 }
65 EmptyWord.Set(nullptr, 0);
66 return EmptyWord;
67 }
68};
69
70class TracePC {
71 public:
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;
75
76 void HandleInit(uint32_t *Start, uint32_t *Stop);
77 void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
kcc98957a12017-08-25 19:29:47 +000078 void HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop);
george.karpenkov29efa6d2017-08-21 23:25:50 +000079 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
80 template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
81 size_t GetTotalPCCoverage();
82 void SetUseCounters(bool UC) { UseCounters = UC; }
83 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
84 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
kccec9da662017-08-28 22:52:22 +000085 void SetPrintNewFuncs(size_t P) { NumPrintNewFuncs = P; }
george.karpenkov29efa6d2017-08-21 23:25:50 +000086 void UpdateObservedPCs();
87 template <class Callback> void CollectFeatures(Callback CB) const;
88
89 void ResetMaps() {
90 ValueProfileMap.Reset();
91 if (NumModules)
92 memset(Counters(), 0, GetNumPCs());
93 ClearExtraCounters();
94 ClearInlineCounters();
kcc1c0379f2017-08-22 01:28:32 +000095 ClearClangCounters();
george.karpenkov29efa6d2017-08-21 23:25:50 +000096 }
97
98 void ClearInlineCounters();
99
100 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
101 void PrintFeatureSet();
102
103 void PrintModuleInfo();
104
105 void PrintCoverage();
106 void DumpCoverage();
107
108 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
109 size_t n, bool StopAtZero);
110
111 TableOfRecentCompares<uint32_t, 32> TORC4;
112 TableOfRecentCompares<uint64_t, 32> TORC8;
113 TableOfRecentCompares<Word, 32> TORCW;
114 MemMemTable<1024> MMT;
115
116 size_t GetNumPCs() const {
117 return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
118 }
119 uintptr_t GetPC(size_t Idx) {
120 assert(Idx < GetNumPCs());
121 return PCs()[Idx];
122 }
123
kcc1c0379f2017-08-22 01:28:32 +0000124 void RecordInitialStack();
125 uintptr_t GetMaxStackOffset() const;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000126
127 template<class CallBack>
128 void ForEachObservedPC(CallBack CB) {
129 for (auto PC : ObservedPCs)
130 CB(PC);
131 }
132
133private:
134 bool UseCounters = false;
135 bool UseValueProfile = false;
136 bool DoPrintNewPCs = false;
kccec9da662017-08-28 22:52:22 +0000137 size_t NumPrintNewFuncs = 0;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000138
139 struct Module {
140 uint32_t *Start, *Stop;
141 };
142
143 Module Modules[4096];
144 size_t NumModules; // linker-initialized.
145 size_t NumGuards; // linker-initialized.
146
147 struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
148 size_t NumModulesWithInline8bitCounters; // linker-initialized.
149 size_t NumInline8bitCounters;
150
kcc98957a12017-08-25 19:29:47 +0000151 struct PCTableEntry {
152 uintptr_t PC, PCFlags;
153 };
154
155 struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096];
george.karpenkov29efa6d2017-08-21 23:25:50 +0000156 size_t NumPCTables;
157 size_t NumPCsInPCTables;
158
159 uint8_t *Counters() const;
160 uintptr_t *PCs() const;
161
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000162 Set<uintptr_t> ObservedPCs;
163 Set<uintptr_t> ObservedFuncs;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000164
165 ValueBitMap ValueProfileMap;
kcc1c0379f2017-08-22 01:28:32 +0000166 uintptr_t InitialStack;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000167};
168
169template <class Callback>
170// void Callback(size_t FirstFeature, size_t Idx, uint8_t Value);
171ATTRIBUTE_NO_SANITIZE_ALL
172void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
173 size_t FirstFeature, Callback Handle8bitCounter) {
174 typedef uintptr_t LargeType;
175 const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
176 const size_t StepMask = Step - 1;
177 auto P = Begin;
178 // Iterate by 1 byte until either the alignment boundary or the end.
179 for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
180 if (uint8_t V = *P)
181 Handle8bitCounter(FirstFeature, P - Begin, V);
182
183 // Iterate by Step bytes at a time.
184 for (; P < End; P += Step)
185 if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
186 for (size_t I = 0; I < Step; I++, Bundle >>= 8)
187 if (uint8_t V = Bundle & 0xff)
188 Handle8bitCounter(FirstFeature, P - Begin + I, V);
189
190 // Iterate by 1 byte until the end.
191 for (; P < End; P++)
192 if (uint8_t V = *P)
193 Handle8bitCounter(FirstFeature, P - Begin, V);
194}
195
kcc1c0379f2017-08-22 01:28:32 +0000196// Given a non-zero Counters returns a number in [0,7].
197template<class T>
198unsigned CounterToFeature(T Counter) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000199 assert(Counter);
200 unsigned Bit = 0;
201 /**/ if (Counter >= 128) Bit = 7;
202 else if (Counter >= 32) Bit = 6;
203 else if (Counter >= 16) Bit = 5;
204 else if (Counter >= 8) Bit = 4;
205 else if (Counter >= 4) Bit = 3;
206 else if (Counter >= 3) Bit = 2;
207 else if (Counter >= 2) Bit = 1;
kcc1c0379f2017-08-22 01:28:32 +0000208 return Bit;
209}
210
211template <class Callback> // bool Callback(size_t Feature)
212ATTRIBUTE_NO_SANITIZE_ADDRESS
213__attribute__((noinline))
214void TracePC::CollectFeatures(Callback HandleFeature) const {
215 uint8_t *Counters = this->Counters();
216 size_t N = GetNumPCs();
217 auto Handle8bitCounter = [&](size_t FirstFeature,
218 size_t Idx, uint8_t Counter) {
219 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter));
george.karpenkov29efa6d2017-08-21 23:25:50 +0000220 };
221
222 size_t FirstFeature = 0;
223
224 if (!NumInline8bitCounters) {
225 ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
226 FirstFeature += N * 8;
227 }
228
229 if (NumInline8bitCounters) {
230 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
231 ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
232 FirstFeature, Handle8bitCounter);
233 FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
234 }
235 }
236
kcc1c0379f2017-08-22 01:28:32 +0000237 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) {
238 auto P = ClangCountersBegin();
239 for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
240 if (auto Cnt = P[Idx])
241 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt));
242 FirstFeature += NumClangCounters;
243 }
244
george.karpenkov29efa6d2017-08-21 23:25:50 +0000245 ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
246 Handle8bitCounter);
247 FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8;
248
249 if (UseValueProfile) {
250 ValueProfileMap.ForEach([&](size_t Idx) {
251 HandleFeature(FirstFeature + Idx);
252 });
253 FirstFeature += ValueProfileMap.SizeInBits();
254 }
255
256 if (auto MaxStackOffset = GetMaxStackOffset())
257 HandleFeature(FirstFeature + MaxStackOffset);
258}
259
260extern TracePC TPC;
261
262} // namespace fuzzer
263
264#endif // LLVM_FUZZER_TRACE_PC