blob: b6cbf3638c629610ff3a2fb4e6e7e3b59808c991 [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 {
Kostya Serebryany382730a2017-03-24 00:45:15 +000040 static const size_t kFeatureSetSize = 1 << 21;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000041 public:
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 Serebryany382730a2017-03-24 00:45:15 +000071 void AddToCorpus(const Unit &U, size_t NumFeatures,
72 bool MayDeleteFile = false) {
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000073 assert(!U.empty());
Kostya Serebryany624f59f2016-09-22 01:34:58 +000074 uint8_t Hash[kSHA1NumBytes];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000075 if (FeatureDebug)
76 Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
Kostya Serebryany624f59f2016-09-22 01:34:58 +000077 ComputeSHA1(U.data(), U.size(), Hash);
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000078 Hashes.insert(Sha1ToString(Hash));
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000079 Inputs.push_back(new InputInfo());
80 InputInfo &II = *Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000081 II.U = U;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000082 II.NumFeatures = NumFeatures;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000083 II.MayDeleteFile = MayDeleteFile;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000084 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000085 UpdateCorpusDistribution();
Kostya Serebryany382730a2017-03-24 00:45:15 +000086 // ValidateFeatureSet();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000087 }
88
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000089 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +000090 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000091 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +000092 InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
Kostya Serebryany2455f0d2016-10-05 00:25:17 +000093 assert(!II.U.empty());
94 return II;
Kostya Serebryany20801e12016-09-21 21:41:48 +000095 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000096
Kostya Serebryany20801e12016-09-21 21:41:48 +000097 // Returns an index of random unit from the corpus to mutate.
98 // Hypothesis: units added to the corpus last are more likely to be
99 // interesting. This function gives more weight to the more recent units.
100 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany6ac64c32017-02-07 22:37:34 +0000101 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand));
Kostya Serebryany20801e12016-09-21 21:41:48 +0000102 assert(Idx < Inputs.size());
103 return Idx;
104 }
105
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000106 void PrintStats() {
107 for (size_t i = 0; i < Inputs.size(); i++) {
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000108 const auto &II = *Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000109 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000110 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +0000111 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000112 }
113 }
114
Kostya Serebryany2c556132016-09-30 01:19:56 +0000115 void PrintFeatureSet() {
Kostya Serebryany2c556132016-09-30 01:19:56 +0000116 for (size_t i = 0; i < kFeatureSetSize; i++) {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000117 if(size_t Sz = GetFeature(i))
118 Printf("[%zd: id %zd sz%zd] ", i, SmallestElementPerFeature[i], Sz);
Kostya Serebryany2c556132016-09-30 01:19:56 +0000119 }
120 Printf("\n\t");
121 for (size_t i = 0; i < Inputs.size(); i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000122 if (size_t N = Inputs[i]->NumFeatures)
Kostya Serebryany2c556132016-09-30 01:19:56 +0000123 Printf(" %zd=>%zd ", i, N);
124 Printf("\n");
125 }
126
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000127 void DeleteInput(size_t Idx) {
128 InputInfo &II = *Inputs[Idx];
129 if (!OutputCorpus.empty() && II.MayDeleteFile)
Marcos Pividori7c1defd2016-12-13 17:46:40 +0000130 RemoveFile(DirPlusFile(OutputCorpus, Sha1ToString(II.Sha1)));
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000131 Unit().swap(II.U);
132 if (FeatureDebug)
133 Printf("EVICTED %zd\n", Idx);
134 }
135
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000136 bool AddFeature(size_t Idx, uint32_t NewSize, bool Shrink) {
137 assert(NewSize);
138 Idx = Idx % kFeatureSetSize;
139 uint32_t OldSize = GetFeature(Idx);
140 if (OldSize == 0 || (Shrink && OldSize > NewSize)) {
141 if (OldSize > 0) {
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000142 size_t OldIdx = SmallestElementPerFeature[Idx];
143 InputInfo &II = *Inputs[OldIdx];
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000144 assert(II.NumFeatures > 0);
145 II.NumFeatures--;
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000146 if (II.NumFeatures == 0)
147 DeleteInput(OldIdx);
Kostya Serebryany382730a2017-03-24 00:45:15 +0000148 } else {
149 NumAddedFeatures++;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000150 }
151 if (FeatureDebug)
152 Printf("ADD FEATURE %zd sz %d\n", Idx, NewSize);
153 SmallestElementPerFeature[Idx] = Inputs.size();
154 InputSizesPerFeature[Idx] = NewSize;
155 CountingFeatures = true;
156 return true;
157 }
158 return false;
159 }
160
Kostya Serebryany382730a2017-03-24 00:45:15 +0000161 size_t NumFeatures() const { return NumAddedFeatures; }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000162
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000163 void ResetFeatureSet() {
164 assert(Inputs.empty());
165 memset(InputSizesPerFeature, 0, sizeof(InputSizesPerFeature));
166 memset(SmallestElementPerFeature, 0, sizeof(SmallestElementPerFeature));
167 }
168
Kostya Serebryany20801e12016-09-21 21:41:48 +0000169private:
170
Kostya Serebryany2c556132016-09-30 01:19:56 +0000171 static const bool FeatureDebug = false;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000172
173 size_t GetFeature(size_t Idx) const { return InputSizesPerFeature[Idx]; }
Kostya Serebryany2c556132016-09-30 01:19:56 +0000174
175 void ValidateFeatureSet() {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000176 if (!CountingFeatures) return;
177 if (FeatureDebug)
178 PrintFeatureSet();
179 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++)
180 if (GetFeature(Idx))
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000181 Inputs[SmallestElementPerFeature[Idx]]->Tmp++;
182 for (auto II: Inputs) {
183 if (II->Tmp != II->NumFeatures)
184 Printf("ZZZ %zd %zd\n", II->Tmp, II->NumFeatures);
185 assert(II->Tmp == II->NumFeatures);
186 II->Tmp = 0;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000187 }
188 }
189
Kostya Serebryany20801e12016-09-21 21:41:48 +0000190 // Updates the probability distribution for the units in the corpus.
191 // Must be called whenever the corpus or unit weights are changed.
192 void UpdateCorpusDistribution() {
193 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000194 Intervals.resize(N + 1);
195 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000196 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000197 if (CountingFeatures)
198 for (size_t i = 0; i < N; i++)
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000199 Weights[i] = Inputs[i]->NumFeatures * (i + 1);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000200 else
201 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000202 CorpusDistribution = std::piecewise_constant_distribution<double>(
203 Intervals.begin(), Intervals.end(), Weights.begin());
204 }
205 std::piecewise_constant_distribution<double> CorpusDistribution;
206
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000207 std::vector<double> Intervals;
208 std::vector<double> Weights;
209
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000210 std::unordered_set<std::string> Hashes;
Kostya Serebryanycd04ec22016-10-08 21:57:48 +0000211 std::vector<InputInfo*> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000212
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000213 bool CountingFeatures = false;
Kostya Serebryany382730a2017-03-24 00:45:15 +0000214 size_t NumAddedFeatures = 0;
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