blob: 56f1820f79e7516843a55e2f964965b03f18fd3c [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50: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"
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000016#include "FuzzerDictionary.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000017#include "FuzzerValueBitMap.h"
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000018
Marcos Pividori178fe582016-12-13 17:46:11 +000019#include <set>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000020
21namespace fuzzer {
22
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000023// 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;
Kostya Serebryany3364f902016-10-25 02:04:43 +000033 struct Pair {
34 T A, B;
35 };
Kostya Serebryany7f058972017-01-27 00:09:59 +000036 ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanyaf2dfce2017-03-31 02:21:28 +000037 void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000038 Idx = Idx % kSize;
Kostya Serebryany3364f902016-10-25 02:04:43 +000039 Table[Idx].A = Arg1;
40 Table[Idx].B = Arg2;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000041 }
Kostya Serebryany3364f902016-10-25 02:04:43 +000042
43 Pair Get(size_t I) { return Table[I % kSize]; }
44
45 Pair Table[kSize];
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000046};
47
Kostya Serebryanyf64b8482017-07-14 00:06:27 +000048template <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
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000070class TracePC {
71 public:
Kostya Serebryany68382d02017-02-02 19:56:01 +000072 static const size_t kNumPCs = 1 << 21;
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +000073 // How many bits of PC are used from __sanitizer_cov_trace_pc.
74 static const size_t kTracePcBits = 18;
Kostya Serebryany2c556132016-09-30 01:19:56 +000075
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000076 void HandleInit(uint32_t *Start, uint32_t *Stop);
77 void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000078 void HandlePCsInit(const uint8_t *Start, const uint8_t *Stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000079 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +000080 template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
Kostya Serebryany275e2602016-10-25 23:52:25 +000081 size_t GetTotalPCCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000082 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000083 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000084 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
Kostya Serebryanybe7a3572017-08-04 23:13:58 +000085 void UpdateObservedPCs();
Kostya Serebryanya617e162017-03-31 04:17:45 +000086 template <class Callback> void CollectFeatures(Callback CB) const;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000087
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000088 void ResetMaps() {
Kostya Serebryany16a145f2016-09-23 01:58:51 +000089 ValueProfileMap.Reset();
Kostya Serebryany4f297002017-08-01 00:48:44 +000090 if (NumModules)
91 memset(Counters(), 0, GetNumPCs());
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000092 ClearExtraCounters();
Kostya Serebryanyf14996b2017-07-28 22:00:56 +000093 ClearInlineCounters();
Kostya Serebryany0873be22017-08-11 23:03:22 +000094 ClearClangCounters();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000095 }
96
Kostya Serebryanyf14996b2017-07-28 22:00:56 +000097 void ClearInlineCounters();
98
Kostya Serebryany0800b812016-09-23 23:51:58 +000099 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
100 void PrintFeatureSet();
101
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000102 void PrintModuleInfo();
103
104 void PrintCoverage();
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000105 void DumpCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000106
Kostya Serebryany379359c2016-10-05 01:09:40 +0000107 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000108 size_t n, bool StopAtZero);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000109
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000110 TableOfRecentCompares<uint32_t, 32> TORC4;
111 TableOfRecentCompares<uint64_t, 32> TORC8;
112 TableOfRecentCompares<Word, 32> TORCW;
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000113 MemMemTable<1024> MMT;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000114
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000115 size_t GetNumPCs() const {
116 return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
117 }
Kostya Serebryany06b87572016-10-26 00:42:52 +0000118 uintptr_t GetPC(size_t Idx) {
119 assert(Idx < GetNumPCs());
Kostya Serebryany68382d02017-02-02 19:56:01 +0000120 return PCs()[Idx];
Kostya Serebryany06b87572016-10-26 00:42:52 +0000121 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000122
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000123 void RecordInitialStack();
124 uintptr_t GetMaxStackOffset() const;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000125
Kostya Serebryanya84a6c12017-08-04 23:49:53 +0000126 template<class CallBack>
127 void ForEachObservedPC(CallBack CB) {
Kostya Serebryanye8637962017-08-08 00:17:20 +0000128 for (auto PC : ObservedPCs)
129 CB(PC);
Kostya Serebryanya84a6c12017-08-04 23:49:53 +0000130 }
131
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000132private:
133 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000134 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000135 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000136
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000137 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000138 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000139 };
140
141 Module Modules[4096];
Kostya Serebryany23567912016-11-11 23:06:53 +0000142 size_t NumModules; // linker-initialized.
143 size_t NumGuards; // linker-initialized.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000144
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000145 struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
146 size_t NumModulesWithInline8bitCounters; // linker-initialized.
147 size_t NumInline8bitCounters;
148
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000149 struct { const uintptr_t *Start, *Stop; } ModulePCTable[4096];
150 size_t NumPCTables;
151 size_t NumPCsInPCTables;
152
Kostya Serebryany68382d02017-02-02 19:56:01 +0000153 uint8_t *Counters() const;
154 uintptr_t *PCs() const;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000155
Kostya Serebryanye8637962017-08-08 00:17:20 +0000156 std::set<uintptr_t> ObservedPCs;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000157
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000158 ValueBitMap ValueProfileMap;
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000159 uintptr_t InitialStack;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000160};
161
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000162template <class Callback>
163// void Callback(size_t FirstFeature, size_t Idx, uint8_t Value);
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000164ATTRIBUTE_NO_SANITIZE_ALL
165void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
166 size_t FirstFeature, Callback Handle8bitCounter) {
167 typedef uintptr_t LargeType;
168 const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000169 const size_t StepMask = Step - 1;
170 auto P = Begin;
171 // Iterate by 1 byte until either the alignment boundary or the end.
172 for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
173 if (uint8_t V = *P)
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000174 Handle8bitCounter(FirstFeature, P - Begin, V);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000175
176 // Iterate by Step bytes at a time.
177 for (; P < End; P += Step)
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000178 if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
179 for (size_t I = 0; I < Step; I++, Bundle >>= 8)
180 if (uint8_t V = Bundle & 0xff)
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000181 Handle8bitCounter(FirstFeature, P - Begin + I, V);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000182
183 // Iterate by 1 byte until the end.
184 for (; P < End; P++)
185 if (uint8_t V = *P)
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000186 Handle8bitCounter(FirstFeature, P - Begin, V);
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000187}
188
Kostya Serebryany0873be22017-08-11 23:03:22 +0000189// Given a non-zero Counters returns a number in [0,7].
190template<class T>
191unsigned CounterToFeature(T Counter) {
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000192 assert(Counter);
193 unsigned Bit = 0;
194 /**/ if (Counter >= 128) Bit = 7;
195 else if (Counter >= 32) Bit = 6;
196 else if (Counter >= 16) Bit = 5;
197 else if (Counter >= 8) Bit = 4;
198 else if (Counter >= 4) Bit = 3;
199 else if (Counter >= 3) Bit = 2;
200 else if (Counter >= 2) Bit = 1;
Kostya Serebryany0873be22017-08-11 23:03:22 +0000201 return Bit;
202}
203
204template <class Callback> // bool Callback(size_t Feature)
205ATTRIBUTE_NO_SANITIZE_ADDRESS
206__attribute__((noinline))
207void TracePC::CollectFeatures(Callback HandleFeature) const {
208 uint8_t *Counters = this->Counters();
209 size_t N = GetNumPCs();
210 auto Handle8bitCounter = [&](size_t FirstFeature,
211 size_t Idx, uint8_t Counter) {
212 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter));
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000213 };
214
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000215 size_t FirstFeature = 0;
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000216
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000217 if (!NumInline8bitCounters) {
218 ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
219 FirstFeature += N * 8;
220 }
221
Kostya Serebryany6cdb5a612017-08-01 01:16:26 +0000222 if (NumInline8bitCounters) {
223 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
224 ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
225 FirstFeature, Handle8bitCounter);
226 FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
227 }
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000228 }
229
Kostya Serebryany0873be22017-08-11 23:03:22 +0000230 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) {
231 auto P = ClangCountersBegin();
232 for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
233 if (auto Cnt = P[Idx])
234 HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt));
235 FirstFeature += NumClangCounters;
236 }
237
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000238 ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000239 Handle8bitCounter);
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000240 FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8;
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000241
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000242 if (UseValueProfile) {
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000243 ValueProfileMap.ForEach([&](size_t Idx) {
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000244 HandleFeature(FirstFeature + Idx);
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000245 });
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000246 FirstFeature += ValueProfileMap.SizeInBits();
247 }
248
249 if (auto MaxStackOffset = GetMaxStackOffset())
250 HandleFeature(FirstFeature + MaxStackOffset);
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000251}
252
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000253extern TracePC TPC;
254
255} // namespace fuzzer
256
257#endif // LLVM_FUZZER_TRACE_PC