blob: 3b76471db6e2d1174f0e1d69c7e462b46c12d5ad [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 Serebryany6f5a8042016-09-21 01:50:50 +000033};
34
35class InputCorpus {
36 public:
37 InputCorpus() {
Kostya Serebryany20801e12016-09-21 21:41:48 +000038 Inputs.reserve(1 << 14); // Avoid too many resizes.
Kostya Serebryany2c556132016-09-30 01:19:56 +000039 memset(FeatureSet, 0, sizeof(FeatureSet));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000040 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000041 size_t size() const { return Inputs.size(); }
42 bool empty() const { return Inputs.empty(); }
43 const Unit &operator[] (size_t Idx) const { return Inputs[Idx].U; }
Kostya Serebryany16a145f2016-09-23 01:58:51 +000044 void AddToCorpus(const Unit &U) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000045 uint8_t Hash[kSHA1NumBytes];
46 ComputeSHA1(U.data(), U.size(), Hash);
47 if (!Hashes.insert(Sha1ToString(Hash)).second) return;
48 Inputs.push_back(InputInfo());
49 InputInfo &II = Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000050 II.U = U;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000051 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany2c556132016-09-30 01:19:56 +000052 UpdateFeatureSet(Inputs.size() - 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +000053 UpdateCorpusDistribution();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000054 }
55
56 typedef const std::vector<InputInfo>::const_iterator ConstIter;
Kostya Serebryany20801e12016-09-21 21:41:48 +000057 ConstIter begin() const { return Inputs.begin(); }
58 ConstIter end() const { return Inputs.end(); }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000059
60 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryanyd2169222016-10-01 01:04:29 +000061 bool HasUnit(const std::string &H) { return Hashes.count(H); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000062 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryany20801e12016-09-21 21:41:48 +000063 return Inputs[ChooseUnitIdxToMutate(Rand)];
64 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000065
Kostya Serebryany20801e12016-09-21 21:41:48 +000066 // Returns an index of random unit from the corpus to mutate.
67 // Hypothesis: units added to the corpus last are more likely to be
68 // interesting. This function gives more weight to the more recent units.
69 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000070 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000071 assert(Idx < Inputs.size());
72 return Idx;
73 }
74
Kostya Serebryany29bb6642016-09-21 22:42:17 +000075 void PrintStats() {
76 for (size_t i = 0; i < Inputs.size(); i++) {
77 const auto &II = Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +000078 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +000079 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +000080 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +000081 }
82 }
83
Kostya Serebryany2c556132016-09-30 01:19:56 +000084 void PrintFeatureSet() {
Kostya Serebryanyd2169222016-10-01 01:04:29 +000085 Printf("Features [id: idx sz] ");
Kostya Serebryany2c556132016-09-30 01:19:56 +000086 for (size_t i = 0; i < kFeatureSetSize; i++) {
87 auto &Fe = FeatureSet[i];
88 if (!Fe.Count) continue;
89 Printf("[%zd: %zd %zd] ", i, Fe.SmallestElementIdx,
90 Fe.SmallestElementSize);
91 }
92 Printf("\n\t");
93 for (size_t i = 0; i < Inputs.size(); i++)
94 if (size_t N = Inputs[i].NumFeatures)
95 Printf(" %zd=>%zd ", i, N);
96 Printf("\n");
97 }
98
Kostya Serebryany20801e12016-09-21 21:41:48 +000099private:
100
Kostya Serebryany2c556132016-09-30 01:19:56 +0000101 static const bool FeatureDebug = false;
102 static const size_t kFeatureSetSize = TracePC::kFeatureSetSize;
103
104 void ValidateFeatureSet() {
105 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++) {
106 Feature &Fe = FeatureSet[Idx];
107 if(Fe.Count && Fe.SmallestElementSize)
108 Inputs[Fe.SmallestElementIdx].Tmp++;
109 }
110 for (auto &II: Inputs) {
111 assert(II.Tmp == II.NumFeatures);
112 II.Tmp = 0;
113 }
114 }
115
116 void UpdateFeatureSet(size_t CurrentElementIdx) {
117 auto &II = Inputs[CurrentElementIdx];
118 size_t Size = II.U.size();
119 if (!Size)
120 return;
121 bool Updated = false;
122 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++) {
123 if (!TPC.HasFeature(Idx))
124 continue;
125 Feature &Fe = FeatureSet[Idx];
126 Fe.Count++;
127 if (!Fe.SmallestElementSize ||
128 Fe.SmallestElementSize > Size) {
129 II.NumFeatures++;
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000130 CountingFeatures = true;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000131 if (Fe.SmallestElementSize > Size) {
132 auto &OlderII = Inputs[Fe.SmallestElementIdx];
133 assert(OlderII.NumFeatures > 0);
134 OlderII.NumFeatures--;
135 if (!OlderII.NumFeatures && FeatureDebug)
136 Printf("EVICTED %zd\n", Fe.SmallestElementIdx);
137 }
138 Fe.SmallestElementIdx = CurrentElementIdx;
139 Fe.SmallestElementSize = Size;
140 Updated = true;
141 }
142 }
143 if (Updated && FeatureDebug) PrintFeatureSet();
144 ValidateFeatureSet();
145 }
146
Kostya Serebryany20801e12016-09-21 21:41:48 +0000147 // Updates the probability distribution for the units in the corpus.
148 // Must be called whenever the corpus or unit weights are changed.
149 void UpdateCorpusDistribution() {
150 size_t N = Inputs.size();
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000151 Intervals.resize(N + 1);
152 Weights.resize(N);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000153 std::iota(Intervals.begin(), Intervals.end(), 0);
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000154 if (CountingFeatures)
155 for (size_t i = 0; i < N; i++)
156 Weights[i] = Inputs[i].NumFeatures * (i + 1);
157 else
158 std::iota(Weights.begin(), Weights.end(), 1);
Kostya Serebryany20801e12016-09-21 21:41:48 +0000159 CorpusDistribution = std::piecewise_constant_distribution<double>(
160 Intervals.begin(), Intervals.end(), Weights.begin());
161 }
162 std::piecewise_constant_distribution<double> CorpusDistribution;
163
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000164 std::vector<double> Intervals;
165 std::vector<double> Weights;
166
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000167 std::unordered_set<std::string> Hashes;
Kostya Serebryany20801e12016-09-21 21:41:48 +0000168 std::vector<InputInfo> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000169
170 struct Feature {
171 size_t Count;
172 size_t SmallestElementIdx;
173 size_t SmallestElementSize;
174 };
Kostya Serebryany5a52a112016-10-04 01:51:44 +0000175 bool CountingFeatures = false;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000176 Feature FeatureSet[kFeatureSetSize];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000177};
178
179} // namespace fuzzer
180
181#endif // LLVM_FUZZER_CORPUS