blob: c4d1db74014f57d515262adac369b56d669b130d [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50:50 +00001//===- FuzzerCorpus.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::InputCorpus
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_CORPUS
13#define LLVM_FUZZER_CORPUS
14
15#include "FuzzerDefs.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000016#include "FuzzerIO.h"
Kostya Serebryany20801e12016-09-21 21:41:48 +000017#include "FuzzerRandom.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000018#include "FuzzerSHA1.h"
Kostya Serebryany2c556132016-09-30 01:19:56 +000019#include "FuzzerTracePC.h"
Reid Klecknere8ee89f2016-12-30 00:15:40 +000020#include <algorithm>
Marcos Pividori178fe582016-12-13 17:46:11 +000021#include <numeric>
22#include <random>
23#include <unordered_set>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000024
25namespace fuzzer {
26
27struct InputInfo {
28 Unit U; // The actual input data.
Kostya Serebryany20801e12016-09-21 21:41:48 +000029 uint8_t Sha1[kSHA1NumBytes]; // Checksum.
Kostya Serebryany2c556132016-09-30 01:19:56 +000030 // Number of features that this input has and no smaller input has.
31 size_t NumFeatures = 0;
32 size_t Tmp = 0; // Used by ValidateFeatureSet.
Kostya Serebryany29bb6642016-09-21 22:42:17 +000033 // Stats.
Kostya Serebryany2c556132016-09-30 01:19:56 +000034 size_t NumExecutedMutations = 0;
35 size_t NumSuccessfullMutations = 0;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000036 bool MayDeleteFile = false;
Kostya Serebryany1ca73882017-07-13 01:08:53 +000037 std::vector<uint32_t> FeatureSet;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000038};
39
40class InputCorpus {
Kostya Serebryany382730a2017-03-24 00:45:15 +000041 static const size_t kFeatureSetSize = 1 << 21;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000042 public:
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000043 InputCorpus(const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000044 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
45 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000046 }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000047 ~InputCorpus() {
48 for (auto II : Inputs)
49 delete II;
50 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000051 size_t size() const { return Inputs.size(); }
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000052 size_t SizeInBytes() const {
53 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000054 for (auto II : Inputs)
55 Res += II->U.size();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000056 return Res;
57 }
58 size_t NumActiveUnits() const {
59 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000060 for (auto II : Inputs)
61 Res += !II->U.empty();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000062 return Res;
63 }
Kostya Serebryany2a8440d2016-12-27 23:24:55 +000064 size_t MaxInputSize() const {
65 size_t Res = 0;
66 for (auto II : Inputs)
67 Res = std::max(Res, II->U.size());
68 return Res;
69 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000070 bool empty() const { return Inputs.empty(); }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000071 const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
Kostya Serebryany1ca73882017-07-13 01:08:53 +000072 void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile,
73 const std::vector<uint32_t> &FeatureSet) {
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000074 assert(!U.empty());
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000075 if (FeatureDebug)
76 Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000077 Inputs.push_back(new InputInfo());
78 InputInfo &II = *Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000079 II.U = U;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000080 II.NumFeatures = NumFeatures;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000081 II.MayDeleteFile = MayDeleteFile;
Kostya Serebryany1ca73882017-07-13 01:08:53 +000082 II.FeatureSet = FeatureSet;
83 ComputeSHA1(U.data(), U.size(), II.Sha1);
84 Hashes.insert(Sha1ToString(II.Sha1));
Kostya Serebryany20801e12016-09-21 21:41:48 +000085 UpdateCorpusDistribution();
Kostya Serebryany1ca73882017-07-13 01:08:53 +000086 PrintCorpus();
Kostya Serebryany382730a2017-03-24 00:45:15 +000087 // ValidateFeatureSet();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000088 }
89
Kostya Serebryany1ca73882017-07-13 01:08:53 +000090 // Debug-only
91 void PrintUnit(const Unit &U) {
92 if (!FeatureDebug) return;
93 for (uint8_t C : U) {
94 if (C != 'F' && C != 'U' && C != 'Z')
95 C = '.';
96 Printf("%c", C);
97 }
98 }
99
100 // Debug-only
101 void PrintFeatureSet(const std::vector<uint32_t> &FeatureSet) {
102 if (!FeatureDebug) return;
103 Printf("{");
104 for (uint32_t Feature: FeatureSet)
105 Printf("%u,", Feature);
106 Printf("}");
107 }
108
109 // Debug-only
110 void PrintCorpus() {
111 if (!FeatureDebug) return;
112 Printf("======= CORPUS:\n");
113 int i = 0;
114 for (auto II : Inputs) {
115 if (std::find(II->U.begin(), II->U.end(), 'F') != II->U.end()) {
116 Printf("[%2d] ", i);
117 Printf("%s sz=%zd ", Sha1ToString(II->Sha1).c_str(), II->U.size());
118 PrintUnit(II->U);
119 Printf(" ");
120 PrintFeatureSet(II->FeatureSet);
121 Printf("\n");
122 }
123 i++;
124 }
125 }
126
127 // If FeatureSet is that same as in II, replace II->U with {Data,Size}.
128 bool TryToReplace(InputInfo *II, const uint8_t *Data, size_t Size,
129 const std::vector<uint32_t> &FeatureSet) {
130 if (II->U.size() > Size && II->FeatureSet.size() &&
131 II->FeatureSet == FeatureSet) {
132 if (FeatureDebug)
133 Printf("Replace: %zd => %zd\n", II->U.size(), Size);
134 Replace(II, {Data, Data + Size});
135 PrintCorpus();
136 return true;
137 }
138 return false;
139 }
140
141 void Replace(InputInfo *II, const Unit &U) {
142 ComputeSHA1(U.data(), U.size(), II->Sha1);
143 Hashes.insert(Sha1ToString(II->Sha1));
144 II->U = U;
145 }
146
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000147 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +0000148 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000149 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000150 InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
Kostya Serebryany2455f0d2016-10-05 00:25:17 +0000151 assert(!II.U.empty());
152 return II;
Kostya Serebryany20801e12016-09-21 21:41:48 +0000153 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000154
Kostya Serebryany20801e12016-09-21 21:41:48 +0000155 // Returns an index of random unit from the corpus to mutate.
156 // Hypothesis: units added to the corpus last are more likely to be
157 // interesting. This function gives more weight to the more recent units.
158 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany6ac64c32017-02-07 22:37:34 +0000159 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
Kostya Serebryany20801e12016-09-21 21:41:48 +0000160 assert(Idx < Inputs.size());
161 return Idx;
162 }
163
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000164 void PrintStats() {
165 for (size_t i = 0; i < Inputs.size(); i++) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000166 const auto &II = *Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000167 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000168 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000169 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000170 }
171 }
172
Kostya Serebryany2c556132016-09-30 01:19:56 +0000173 void PrintFeatureSet() {
Kostya Serebryany2c556132016-09-30 01:19:56 +0000174 for (size_t i = 0; i < kFeatureSetSize; i++) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000175 if(size_t Sz = GetFeature(i))
176 Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
Kostya Serebryany2c556132016-09-30 01:19:56 +0000177 }
178 Printf("\n\t");
179 for (size_t i = 0; i < Inputs.size(); i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000180 if (size_t N = Inputs[i]->NumFeatures)
Kostya Serebryany2c556132016-09-30 01:19:56 +0000181 Printf(" %zd=>%zd ", i, N);
182 Printf("\n");
183 }
184
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000185 void DeleteInput(size_t Idx) {
186 InputInfo &II = *Inputs[Idx];
187 if (!OutputCorpus.empty() && II.MayDeleteFile)
Marcos Pividori7c1defd2016-12-13 17:46:40 +0000188 RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000189 Unit().swap(II.U);
190 if (FeatureDebug)
191 Printf("EVICTED %zd\n", Idx);
192 }
193
Kostya Serebryanya617e162017-03-31 04:17:45 +0000194 void AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000195 assert(NewSize);
196 Idx = Idx % kFeatureSetSize;
197 uint32_t OldSize = GetFeature(Idx);
198 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
199 if (OldSize > 0) {
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000200 size_t OldIdx = SmallestElementPerFeature[Idx];
201 InputInfo &II = *Inputs[OldIdx];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000202 assert(II.NumFeatures > 0);
203 II.NumFeatures--;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000204 if (II.NumFeatures == 0)
205 DeleteInput(OldIdx);
Kostya Serebryany382730a2017-03-24 00:45:15 +0000206 } else {
207 NumAddedFeatures++;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000208 }
Kostya Serebryanya617e162017-03-31 04:17:45 +0000209 NumUpdatedFeatures++;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000210 if (FeatureDebug)
211 Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
212 SmallestElementPerFeature[Idx] = Inputs.size();
213 InputSizesPerFeature[Idx] = NewSize;
214 CountingFeatures = true;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000215 }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000216 }
217
Kostya Serebryany382730a2017-03-24 00:45:15 +0000218 size_t NumFeatures() const { return NumAddedFeatures; }
Kostya Serebryanya617e162017-03-31 04:17:45 +0000219 size_t NumFeatureUpdates() const { return NumUpdatedFeatures; }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000220
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000221 void ResetFeatureSet() {
222 assert(Inputs.empty());
223 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
224 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
225 }
226
Kostya Serebryany20801e12016-09-21 21:41:48 +0000227private:
228
Kostya Serebryany2c556132016-09-30 01:19:56 +0000229 static const bool FeatureDebug = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000230
231 size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
Kostya Serebryany2c556132016-09-30 01:19:56 +0000232
233 void ValidateFeatureSet() {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000234 if (!CountingFeatures) return;
235 if (FeatureDebug)
236 PrintFeatureSet();
237 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
238 if (GetFeature(Idx))
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000239 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
240 for (auto II: Inputs) {
241 if (II->Tmp != II->NumFeatures)
242 Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
243 assert(II->Tmp == II->NumFeatures);
244 II->Tmp = 0;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000245 }
246 }
247
Kostya Serebryany20801e12016-09-21 21:41:48 +0000248 // Updates the probability distribution for the units in the corpus.
249 // Must be called whenever the corpus or unit weights are changed.
250 void UpdateCorpusDistribution() {
251 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000252 Intervals.resize(N + 1);
253 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000254 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000255 if (CountingFeatures)
256 for (size_t i = 0; i < N; i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000257 Weights[i] = Inputs[i]->NumFeatures * (i + 1);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000258 else
259 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000260 CorpusDistribution = std::piecewise_constant_distribution<double>(
261 Intervals.begin(), Intervals.end(), Weights.begin());
262 }
263 std::piecewise_constant_distribution<double> CorpusDistribution;
264
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000265 std::vector<double> Intervals;
266 std::vector<double> Weights;
267
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000268 std::unordered_set<std::string> Hashes;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000269 std::vector<InputInfo*> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000270
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000271 bool CountingFeatures = false;
Kostya Serebryany382730a2017-03-24 00:45:15 +0000272 size_t NumAddedFeatures = 0;
Kostya Serebryanya617e162017-03-31 04:17:45 +0000273 size_t NumUpdatedFeatures = 0;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000274 uint32_t InputSizesPerFeature[kFeatureSetSize];
275 uint32_t SmallestElementPerFeature[kFeatureSetSize];
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000276
277 std::string OutputCorpus;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000278};
279
280} // namespace fuzzer
281
282#endif // LLVM_FUZZER_CORPUS