blob: 355c242e1f420de398255aac2cb295dd41640d40 [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
Kostya Serebryany20801e12016-09-21 21:41:48 +000015#include <random>
Kostya Serebryany29bb6642016-09-21 22:42:17 +000016#include <unordered_set>
Kostya Serebryany20801e12016-09-21 21:41:48 +000017
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000018#include "FuzzerDefs.h"
Kostya Serebryany20801e12016-09-21 21:41:48 +000019#include "FuzzerRandom.h"
Kostya Serebryany2c556132016-09-30 01:19:56 +000020#include "FuzzerTracePC.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000021
22namespace fuzzer {
23
24struct InputInfo {
25 Unit U; // The actual input data.
Kostya Serebryany20801e12016-09-21 21:41:48 +000026 uint8_t Sha1[kSHA1NumBytes]; // Checksum.
Kostya Serebryany2c556132016-09-30 01:19:56 +000027 // Number of features that this input has and no smaller input has.
28 size_t NumFeatures = 0;
29 size_t Tmp = 0; // Used by ValidateFeatureSet.
Kostya Serebryany29bb6642016-09-21 22:42:17 +000030 // Stats.
Kostya Serebryany2c556132016-09-30 01:19:56 +000031 size_t NumExecutedMutations = 0;
32 size_t NumSuccessfullMutations = 0;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000033 bool MayDeleteFile = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000034};
35
36class InputCorpus {
37 public:
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000038 static const size_t kFeatureSetSize = 1 << 16;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000039 InputCorpus(const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000040 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
41 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000042 }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000043 ~InputCorpus() {
44 for (auto II : Inputs)
45 delete II;
46 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000047 size_t size() const { return Inputs.size(); }
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000048 size_t SizeInBytes() const {
49 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000050 for (auto II : Inputs)
51 Res += II->U.size();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000052 return Res;
53 }
54 size_t NumActiveUnits() const {
55 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000056 for (auto II : Inputs)
57 Res += !II->U.empty();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000058 return Res;
59 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000060 bool empty() const { return Inputs.empty(); }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000061 const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000062 void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile = false) {
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000063 assert(!U.empty());
Kostya Serebryany624f59f2016-09-22 01:34:58 +000064 uint8_t Hash[kSHA1NumBytes];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000065 if (FeatureDebug)
66 Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
Kostya Serebryany624f59f2016-09-22 01:34:58 +000067 ComputeSHA1(U.data(), U.size(), Hash);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000068 Hashes.insert(Sha1ToString(Hash));
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000069 Inputs.push_back(new InputInfo());
70 InputInfo &II = *Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000071 II.U = U;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000072 II.NumFeatures = NumFeatures;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000073 II.MayDeleteFile = MayDeleteFile;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000074 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000075 UpdateCorpusDistribution();
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000076 ValidateFeatureSet();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000077 }
78
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000079 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +000080 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000081 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000082 InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000083 assert(!II.U.empty());
84 return II;
Kostya Serebryany20801e12016-09-21 21:41:48 +000085 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000086
Kostya Serebryany20801e12016-09-21 21:41:48 +000087 // Returns an index of random unit from the corpus to mutate.
88 // Hypothesis: units added to the corpus last are more likely to be
89 // interesting. This function gives more weight to the more recent units.
90 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000091 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000092 assert(Idx < Inputs.size());
93 return Idx;
94 }
95
Kostya Serebryany29bb6642016-09-21 22:42:17 +000096 void PrintStats() {
97 for (size_t i = 0; i < Inputs.size(); i++) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000098 const auto &II = *Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +000099 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000100 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000101 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000102 }
103 }
104
Kostya Serebryany2c556132016-09-30 01:19:56 +0000105 void PrintFeatureSet() {
Kostya Serebryany2c556132016-09-30 01:19:56 +0000106 for (size_t i = 0; i < kFeatureSetSize; i++) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000107 if(size_t Sz = GetFeature(i))
108 Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
Kostya Serebryany2c556132016-09-30 01:19:56 +0000109 }
110 Printf("\n\t");
111 for (size_t i = 0; i < Inputs.size(); i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000112 if (size_t N = Inputs[i]->NumFeatures)
Kostya Serebryany2c556132016-09-30 01:19:56 +0000113 Printf(" %zd=>%zd ", i, N);
114 Printf("\n");
115 }
116
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000117 void DeleteInput(size_t Idx) {
118 InputInfo &II = *Inputs[Idx];
119 if (!OutputCorpus.empty() && II.MayDeleteFile)
120 DeleteFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
121 Unit().swap(II.U);
122 if (FeatureDebug)
123 Printf("EVICTED %zd\n", Idx);
124 }
125
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000126 bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
127 assert(NewSize);
128 Idx = Idx % kFeatureSetSize;
129 uint32_t OldSize = GetFeature(Idx);
130 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
131 if (OldSize > 0) {
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000132 size_t OldIdx = SmallestElementPerFeature[Idx];
133 InputInfo &II = *Inputs[OldIdx];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000134 assert(II.NumFeatures > 0);
135 II.NumFeatures--;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000136 if (II.NumFeatures == 0)
137 DeleteInput(OldIdx);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000138 }
139 if (FeatureDebug)
140 Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
141 SmallestElementPerFeature[Idx] = Inputs.size();
142 InputSizesPerFeature[Idx] = NewSize;
143 CountingFeatures = true;
144 return true;
145 }
146 return false;
147 }
148
149 size_t NumFeatures() const {
150 size_t Res = 0;
151 for (size_t i = 0; i < kFeatureSetSize; i++)
152 Res += GetFeature(i) != 0;
153 return Res;
154 }
155
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000156 void ResetFeatureSet() {
157 assert(Inputs.empty());
158 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
159 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
160 }
161
Kostya Serebryany20801e12016-09-21 21:41:48 +0000162private:
163
Kostya Serebryany2c556132016-09-30 01:19:56 +0000164 static const bool FeatureDebug = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000165
166 size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
Kostya Serebryany2c556132016-09-30 01:19:56 +0000167
168 void ValidateFeatureSet() {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000169 if (!CountingFeatures) return;
170 if (FeatureDebug)
171 PrintFeatureSet();
172 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
173 if (GetFeature(Idx))
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000174 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
175 for (auto II: Inputs) {
176 if (II->Tmp != II->NumFeatures)
177 Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
178 assert(II->Tmp == II->NumFeatures);
179 II->Tmp = 0;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000180 }
181 }
182
Kostya Serebryany20801e12016-09-21 21:41:48 +0000183 // Updates the probability distribution for the units in the corpus.
184 // Must be called whenever the corpus or unit weights are changed.
185 void UpdateCorpusDistribution() {
186 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000187 Intervals.resize(N + 1);
188 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000189 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000190 if (CountingFeatures)
191 for (size_t i = 0; i < N; i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000192 Weights[i] = Inputs[i]->NumFeatures * (i + 1);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000193 else
194 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000195 CorpusDistribution = std::piecewise_constant_distribution<double>(
196 Intervals.begin(), Intervals.end(), Weights.begin());
197 }
198 std::piecewise_constant_distribution<double> CorpusDistribution;
199
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000200 std::vector<double> Intervals;
201 std::vector<double> Weights;
202
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000203 std::unordered_set<std::string> Hashes;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000204 std::vector<InputInfo*> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000205
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000206 bool CountingFeatures = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000207 uint32_t InputSizesPerFeature[kFeatureSetSize];
208 uint32_t SmallestElementPerFeature[kFeatureSetSize];
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000209
210 std::string OutputCorpus;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000211};
212
213} // namespace fuzzer
214
215#endif // LLVM_FUZZER_CORPUS