blob: 42b3d2482fa198b3d890fe5eddf7d0a35375830c [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"
Marcos Pividori178fe582016-12-13 17:46:11 +000020#include <numeric>
21#include <random>
22#include <unordered_set>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000023
24namespace fuzzer {
25
26struct InputInfo {
27 Unit U; // The actual input data.
Kostya Serebryany20801e12016-09-21 21:41:48 +000028 uint8_t Sha1[kSHA1NumBytes]; // Checksum.
Kostya Serebryany2c556132016-09-30 01:19:56 +000029 // Number of features that this input has and no smaller input has.
30 size_t NumFeatures = 0;
31 size_t Tmp = 0; // Used by ValidateFeatureSet.
Kostya Serebryany29bb6642016-09-21 22:42:17 +000032 // Stats.
Kostya Serebryany2c556132016-09-30 01:19:56 +000033 size_t NumExecutedMutations = 0;
34 size_t NumSuccessfullMutations = 0;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000035 bool MayDeleteFile = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000036};
37
38class InputCorpus {
39 public:
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000040 static const size_t kFeatureSetSize = 1 << 16;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000041 InputCorpus(const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000042 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
43 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000044 }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000045 ~InputCorpus() {
46 for (auto II : Inputs)
47 delete II;
48 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000049 size_t size() const { return Inputs.size(); }
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000050 size_t SizeInBytes() const {
51 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000052 for (auto II : Inputs)
53 Res += II->U.size();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000054 return Res;
55 }
56 size_t NumActiveUnits() const {
57 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000058 for (auto II : Inputs)
59 Res += !II->U.empty();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000060 return Res;
61 }
Kostya Serebryany2a8440d2016-12-27 23:24:55 +000062 size_t MaxInputSize() const {
63 size_t Res = 0;
64 for (auto II : Inputs)
65 Res = std::max(Res, II->U.size());
66 return Res;
67 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000068 bool empty() const { return Inputs.empty(); }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000069 const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000070 void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile = false) {
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000071 assert(!U.empty());
Kostya Serebryany624f59f2016-09-22 01:34:58 +000072 uint8_t Hash[kSHA1NumBytes];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000073 if (FeatureDebug)
74 Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
Kostya Serebryany624f59f2016-09-22 01:34:58 +000075 ComputeSHA1(U.data(), U.size(), Hash);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000076 Hashes.insert(Sha1ToString(Hash));
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 Serebryany624f59f2016-09-22 01:34:58 +000082 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000083 UpdateCorpusDistribution();
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000084 ValidateFeatureSet();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000085 }
86
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000087 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +000088 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000089 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000090 InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000091 assert(!II.U.empty());
92 return II;
Kostya Serebryany20801e12016-09-21 21:41:48 +000093 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000094
Kostya Serebryany20801e12016-09-21 21:41:48 +000095 // Returns an index of random unit from the corpus to mutate.
96 // Hypothesis: units added to the corpus last are more likely to be
97 // interesting. This function gives more weight to the more recent units.
98 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000099 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +0000100 assert(Idx < Inputs.size());
101 return Idx;
102 }
103
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000104 void PrintStats() {
105 for (size_t i = 0; i < Inputs.size(); i++) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000106 const auto &II = *Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000107 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000108 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000109 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000110 }
111 }
112
Kostya Serebryany2c556132016-09-30 01:19:56 +0000113 void PrintFeatureSet() {
Kostya Serebryany2c556132016-09-30 01:19:56 +0000114 for (size_t i = 0; i < kFeatureSetSize; i++) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000115 if(size_t Sz = GetFeature(i))
116 Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
Kostya Serebryany2c556132016-09-30 01:19:56 +0000117 }
118 Printf("\n\t");
119 for (size_t i = 0; i < Inputs.size(); i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000120 if (size_t N = Inputs[i]->NumFeatures)
Kostya Serebryany2c556132016-09-30 01:19:56 +0000121 Printf(" %zd=>%zd ", i, N);
122 Printf("\n");
123 }
124
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000125 void DeleteInput(size_t Idx) {
126 InputInfo &II = *Inputs[Idx];
127 if (!OutputCorpus.empty() && II.MayDeleteFile)
Marcos Pividori7c1defd2016-12-13 17:46:40 +0000128 RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000129 Unit().swap(II.U);
130 if (FeatureDebug)
131 Printf("EVICTED %zd\n", Idx);
132 }
133
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000134 bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
135 assert(NewSize);
136 Idx = Idx % kFeatureSetSize;
137 uint32_t OldSize = GetFeature(Idx);
138 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
139 if (OldSize > 0) {
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000140 size_t OldIdx = SmallestElementPerFeature[Idx];
141 InputInfo &II = *Inputs[OldIdx];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000142 assert(II.NumFeatures > 0);
143 II.NumFeatures--;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000144 if (II.NumFeatures == 0)
145 DeleteInput(OldIdx);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000146 }
147 if (FeatureDebug)
148 Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
149 SmallestElementPerFeature[Idx] = Inputs.size();
150 InputSizesPerFeature[Idx] = NewSize;
151 CountingFeatures = true;
152 return true;
153 }
154 return false;
155 }
156
157 size_t NumFeatures() const {
158 size_t Res = 0;
159 for (size_t i = 0; i < kFeatureSetSize; i++)
160 Res += GetFeature(i) != 0;
161 return Res;
162 }
163
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000164 void ResetFeatureSet() {
165 assert(Inputs.empty());
166 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
167 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
168 }
169
Kostya Serebryany20801e12016-09-21 21:41:48 +0000170private:
171
Kostya Serebryany2c556132016-09-30 01:19:56 +0000172 static const bool FeatureDebug = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000173
174 size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
Kostya Serebryany2c556132016-09-30 01:19:56 +0000175
176 void ValidateFeatureSet() {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000177 if (!CountingFeatures) return;
178 if (FeatureDebug)
179 PrintFeatureSet();
180 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
181 if (GetFeature(Idx))
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000182 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
183 for (auto II: Inputs) {
184 if (II->Tmp != II->NumFeatures)
185 Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
186 assert(II->Tmp == II->NumFeatures);
187 II->Tmp = 0;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000188 }
189 }
190
Kostya Serebryany20801e12016-09-21 21:41:48 +0000191 // Updates the probability distribution for the units in the corpus.
192 // Must be called whenever the corpus or unit weights are changed.
193 void UpdateCorpusDistribution() {
194 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000195 Intervals.resize(N + 1);
196 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000197 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000198 if (CountingFeatures)
199 for (size_t i = 0; i < N; i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000200 Weights[i] = Inputs[i]->NumFeatures * (i + 1);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000201 else
202 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000203 CorpusDistribution = std::piecewise_constant_distribution<double>(
204 Intervals.begin(), Intervals.end(), Weights.begin());
205 }
206 std::piecewise_constant_distribution<double> CorpusDistribution;
207
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000208 std::vector<double> Intervals;
209 std::vector<double> Weights;
210
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000211 std::unordered_set<std::string> Hashes;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000212 std::vector<InputInfo*> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000213
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000214 bool CountingFeatures = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000215 uint32_t InputSizesPerFeature[kFeatureSetSize];
216 uint32_t SmallestElementPerFeature[kFeatureSetSize];
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000217
218 std::string OutputCorpus;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000219};
220
221} // namespace fuzzer
222
223#endif // LLVM_FUZZER_CORPUS