blob: 8c2f7039280ff1cdebd88462c0a9670765ee9d9b [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 Serebryany6f5a8042016-09-21 01:50:50 +000037};
38
39class InputCorpus {
40 public:
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000041 static const size_t kFeatureSetSize = 1 << 16;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000042 InputCorpus(const std::string &OutputCorpus) : OutputCorpus(OutputCorpus) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000043 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
44 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045 }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000046 ~InputCorpus() {
47 for (auto II : Inputs)
48 delete II;
49 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000050 size_t size() const { return Inputs.size(); }
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000051 size_t SizeInBytes() const {
52 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000053 for (auto II : Inputs)
54 Res += II->U.size();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000055 return Res;
56 }
57 size_t NumActiveUnits() const {
58 size_t Res = 0;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000059 for (auto II : Inputs)
60 Res += !II->U.empty();
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000061 return Res;
62 }
Kostya Serebryany2a8440d2016-12-27 23:24:55 +000063 size_t MaxInputSize() const {
64 size_t Res = 0;
65 for (auto II : Inputs)
66 Res = std::max(Res, II->U.size());
67 return Res;
68 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000069 bool empty() const { return Inputs.empty(); }
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000070 const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000071 void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile = false) {
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000072 assert(!U.empty());
Kostya Serebryany624f59f2016-09-22 01:34:58 +000073 uint8_t Hash[kSHA1NumBytes];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000074 if (FeatureDebug)
75 Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
Kostya Serebryany624f59f2016-09-22 01:34:58 +000076 ComputeSHA1(U.data(), U.size(), Hash);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000077 Hashes.insert(Sha1ToString(Hash));
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000078 Inputs.push_back(new InputInfo());
79 InputInfo &II = *Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000080 II.U = U;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000081 II.NumFeatures = NumFeatures;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000082 II.MayDeleteFile = MayDeleteFile;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000083 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000084 UpdateCorpusDistribution();
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000085 ValidateFeatureSet();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000086 }
87
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000088 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +000089 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000090 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000091 InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000092 assert(!II.U.empty());
93 return II;
Kostya Serebryany20801e12016-09-21 21:41:48 +000094 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000095
Kostya Serebryany20801e12016-09-21 21:41:48 +000096 // Returns an index of random unit from the corpus to mutate.
97 // Hypothesis: units added to the corpus last are more likely to be
98 // interesting. This function gives more weight to the more recent units.
99 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany6ac64c32017-02-07 22:37:34 +0000100 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
Kostya Serebryany20801e12016-09-21 21:41:48 +0000101 assert(Idx < Inputs.size());
102 return Idx;
103 }
104
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000105 void PrintStats() {
106 for (size_t i = 0; i < Inputs.size(); i++) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000107 const auto &II = *Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000108 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000109 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000110 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000111 }
112 }
113
Kostya Serebryany2c556132016-09-30 01:19:56 +0000114 void PrintFeatureSet() {
Kostya Serebryany2c556132016-09-30 01:19:56 +0000115 for (size_t i = 0; i < kFeatureSetSize; i++) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000116 if(size_t Sz = GetFeature(i))
117 Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
Kostya Serebryany2c556132016-09-30 01:19:56 +0000118 }
119 Printf("\n\t");
120 for (size_t i = 0; i < Inputs.size(); i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000121 if (size_t N = Inputs[i]->NumFeatures)
Kostya Serebryany2c556132016-09-30 01:19:56 +0000122 Printf(" %zd=>%zd ", i, N);
123 Printf("\n");
124 }
125
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000126 void DeleteInput(size_t Idx) {
127 InputInfo &II = *Inputs[Idx];
128 if (!OutputCorpus.empty() && II.MayDeleteFile)
Marcos Pividori7c1defd2016-12-13 17:46:40 +0000129 RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000130 Unit().swap(II.U);
131 if (FeatureDebug)
132 Printf("EVICTED %zd\n", Idx);
133 }
134
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000135 bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
136 assert(NewSize);
137 Idx = Idx % kFeatureSetSize;
138 uint32_t OldSize = GetFeature(Idx);
139 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
140 if (OldSize > 0) {
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000141 size_t OldIdx = SmallestElementPerFeature[Idx];
142 InputInfo &II = *Inputs[OldIdx];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000143 assert(II.NumFeatures > 0);
144 II.NumFeatures--;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000145 if (II.NumFeatures == 0)
146 DeleteInput(OldIdx);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000147 }
148 if (FeatureDebug)
149 Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
150 SmallestElementPerFeature[Idx] = Inputs.size();
151 InputSizesPerFeature[Idx] = NewSize;
152 CountingFeatures = true;
153 return true;
154 }
155 return false;
156 }
157
158 size_t NumFeatures() const {
159 size_t Res = 0;
160 for (size_t i = 0; i < kFeatureSetSize; i++)
161 Res += GetFeature(i) != 0;
162 return Res;
163 }
164
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000165 void ResetFeatureSet() {
166 assert(Inputs.empty());
167 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
168 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
169 }
170
Kostya Serebryany20801e12016-09-21 21:41:48 +0000171private:
172
Kostya Serebryany2c556132016-09-30 01:19:56 +0000173 static const bool FeatureDebug = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000174
175 size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
Kostya Serebryany2c556132016-09-30 01:19:56 +0000176
177 void ValidateFeatureSet() {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000178 if (!CountingFeatures) return;
179 if (FeatureDebug)
180 PrintFeatureSet();
181 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
182 if (GetFeature(Idx))
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000183 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
184 for (auto II: Inputs) {
185 if (II->Tmp != II->NumFeatures)
186 Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
187 assert(II->Tmp == II->NumFeatures);
188 II->Tmp = 0;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000189 }
190 }
191
Kostya Serebryany20801e12016-09-21 21:41:48 +0000192 // Updates the probability distribution for the units in the corpus.
193 // Must be called whenever the corpus or unit weights are changed.
194 void UpdateCorpusDistribution() {
195 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000196 Intervals.resize(N + 1);
197 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000198 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000199 if (CountingFeatures)
200 for (size_t i = 0; i < N; i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000201 Weights[i] = Inputs[i]->NumFeatures * (i + 1);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000202 else
203 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000204 CorpusDistribution = std::piecewise_constant_distribution<double>(
205 Intervals.begin(), Intervals.end(), Weights.begin());
206 }
207 std::piecewise_constant_distribution<double> CorpusDistribution;
208
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000209 std::vector<double> Intervals;
210 std::vector<double> Weights;
211
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000212 std::unordered_set<std::string> Hashes;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000213 std::vector<InputInfo*> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000214
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000215 bool CountingFeatures = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000216 uint32_t InputSizesPerFeature[kFeatureSetSize];
217 uint32_t SmallestElementPerFeature[kFeatureSetSize];
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000218
219 std::string OutputCorpus;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000220};
221
222} // namespace fuzzer
223
224#endif // LLVM_FUZZER_CORPUS