blob: 0c9d4b69b6197d1aa83051367d03c64c6ad8539f [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; }
85 void UpdateObservedPCs();
86 template <class Callback> void CollectFeatures(Callback CB) const;
87
88 void ResetMaps() {
89 ValueProfileMap.Reset();
90 if (NumModules)
91 memset(Counters(), 0, GetNumPCs());
92 ClearExtraCounters();
93 ClearInlineCounters();
kcc1c0379f2017-08-22 01:28:32 +000094 ClearClangCounters();
george.karpenkov29efa6d2017-08-21 23:25:50 +000095 }
96
97 void ClearInlineCounters();
98
99 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
100 void PrintFeatureSet();
101
102 void PrintModuleInfo();
103
104 void PrintCoverage();
105 void DumpCoverage();
106
107 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
108 size_t n, bool StopAtZero);
109
110 TableOfRecentCompares<uint32_t, 32> TORC4;
111 TableOfRecentCompares<uint64_t, 32> TORC8;
112 TableOfRecentCompares<Word, 32> TORCW;
113 MemMemTable<1024> MMT;
114
115 size_t GetNumPCs() const {
116 return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
117 }
118 uintptr_t GetPC(size_t Idx) {
119 assert(Idx < GetNumPCs());
120 return PCs()[Idx];
121 }
122
kcc1c0379f2017-08-22 01:28:32 +0000123 void RecordInitialStack();
124 uintptr_t GetMaxStackOffset() const;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000125
126 template<class CallBack>
127 void ForEachObservedPC(CallBack CB) {
128 for (auto PC : ObservedPCs)
129 CB(PC);
130 }
131
132private:
133 bool UseCounters = false;
134 bool UseValueProfile = false;
135 bool DoPrintNewPCs = false;
136
137 struct Module {
138 uint32_t *Start, *Stop;
139 };
140
141 Module Modules[4096];
142 size_t NumModules; // linker-initialized.
143 size_t NumGuards; // linker-initialized.
144
145 struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
146 size_t NumModulesWithInline8bitCounters; // linker-initialized.
147 size_t NumInline8bitCounters;
148
kcc98957a12017-08-25 19:29:47 +0000149 struct PCTableEntry {
150 uintptr_t PC, PCFlags;
151 };
152
153 struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096];
george.karpenkov29efa6d2017-08-21 23:25:50 +0000154 size_t NumPCTables;
155 size_t NumPCsInPCTables;
156
157 uint8_t *Counters() const;
158 uintptr_t *PCs() const;
159
160 std::set<uintptr_t> ObservedPCs;
161
162 ValueBitMap ValueProfileMap;
kcc1c0379f2017-08-22 01:28:32 +0000163 uintptr_t InitialStack;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000164};
165
166template <class Callback>
167// void Callback(size_t FirstFeature, size_t Idx, uint8_t Value);
168ATTRIBUTE_NO_SANITIZE_ALL
169void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
170 size_t FirstFeature, Callback Handle8bitCounter) {
171 typedef uintptr_t LargeType;
172 const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
173 const size_t StepMask = Step - 1;
174 auto P = Begin;
175 // Iterate by 1 byte until either the alignment boundary or the end.
176 for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
177 if (uint8_t V = *P)
178 Handle8bitCounter(FirstFeature, P - Begin, V);
179
180 // Iterate by Step bytes at a time.
181 for (; P < End; P += Step)
182 if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
183 for (size_t I = 0; I < Step; I++, Bundle >>= 8)
184 if (uint8_t V = Bundle & 0xff)
185 Handle8bitCounter(FirstFeature, P - Begin + I, V);
186
187 // Iterate by 1 byte until the end.
188 for (; P < End; P++)
189 if (uint8_t V = *P)
190 Handle8bitCounter(FirstFeature, P - Begin, V);
191}
192
kcc1c0379f2017-08-22 01:28:32 +0000193// Given a non-zero Counters returns a number in [0,7].
194template<class T>
195unsigned CounterToFeature(T Counter) {
george.karpenkov29efa6d2017-08-21 23:25:50 +0000196 assert(Counter);
197 unsigned Bit = 0;
198 /**/ if (Counter >= 128) Bit = 7;
199 else if (Counter >= 32) Bit = 6;
200 else if (Counter >= 16) Bit = 5;
201 else if (Counter >= 8) Bit = 4;
202 else if (Counter >= 4) Bit = 3;
203 else if (Counter >= 3) Bit = 2;
204 else if (Counter >= 2) Bit = 1;
kcc1c0379f2017-08-22 01:28:32 +0000205 return Bit;
206}
207
208template <class Callback> // bool Callback(size_t Feature)
209ATTRIBUTE_NO_SANITIZE_ADDRESS
210__attribute__((noinline))
211void TracePC::CollectFeatures(Callback HandleFeature) const {
212 uint8_t *Counters = this->Counters();
213 size_t N = GetNumPCs();
214 auto Handle8bitCounter = [&](size_t FirstFeature,
215 size_t Idx, uint8_t Counter) {
216 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter));
george.karpenkov29efa6d2017-08-21 23:25:50 +0000217 };
218
219 size_t FirstFeature = 0;
220
221 if (!NumInline8bitCounters) {
222 ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
223 FirstFeature += N * 8;
224 }
225
226 if (NumInline8bitCounters) {
227 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
228 ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
229 FirstFeature, Handle8bitCounter);
230 FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
231 }
232 }
233
kcc1c0379f2017-08-22 01:28:32 +0000234 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) {
235 auto P = ClangCountersBegin();
236 for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
237 if (auto Cnt = P[Idx])
238 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt));
239 FirstFeature += NumClangCounters;
240 }
241
george.karpenkov29efa6d2017-08-21 23:25:50 +0000242 ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
243 Handle8bitCounter);
244 FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8;
245
246 if (UseValueProfile) {
247 ValueProfileMap.ForEach([&](size_t Idx) {
248 HandleFeature(FirstFeature + Idx);
249 });
250 FirstFeature += ValueProfileMap.SizeInBits();
251 }
252
253 if (auto MaxStackOffset = GetMaxStackOffset())
254 HandleFeature(FirstFeature + MaxStackOffset);
255}
256
257extern TracePC TPC;
258
259} // namespace fuzzer
260
261#endif // LLVM_FUZZER_TRACE_PC