blob: 6438a6035f1fc4595ff526a47d7e995cd0a26159 [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 Serebryany29bb6642016-09-21 22:42:17 +000061 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryany20801e12016-09-21 21:41:48 +000062 return Inputs[ChooseUnitIdxToMutate(Rand)];
63 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000064
Kostya Serebryany20801e12016-09-21 21:41:48 +000065 // Returns an index of random unit from the corpus to mutate.
66 // Hypothesis: units added to the corpus last are more likely to be
67 // interesting. This function gives more weight to the more recent units.
68 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000069 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000070 assert(Idx < Inputs.size());
71 return Idx;
72 }
73
Kostya Serebryany29bb6642016-09-21 22:42:17 +000074 void PrintStats() {
75 for (size_t i = 0; i < Inputs.size(); i++) {
76 const auto &II = Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +000077 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +000078 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +000079 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +000080 }
81 }
82
Kostya Serebryany2c556132016-09-30 01:19:56 +000083 void PrintFeatureSet() {
84 Printf("Features [id: cnt idx sz] ");
85 for (size_t i = 0; i < kFeatureSetSize; i++) {
86 auto &Fe = FeatureSet[i];
87 if (!Fe.Count) continue;
88 Printf("[%zd: %zd %zd] ", i, Fe.SmallestElementIdx,
89 Fe.SmallestElementSize);
90 }
91 Printf("\n\t");
92 for (size_t i = 0; i < Inputs.size(); i++)
93 if (size_t N = Inputs[i].NumFeatures)
94 Printf(" %zd=>%zd ", i, N);
95 Printf("\n");
96 }
97
Kostya Serebryany20801e12016-09-21 21:41:48 +000098private:
99
Kostya Serebryany2c556132016-09-30 01:19:56 +0000100 static const bool FeatureDebug = false;
101 static const size_t kFeatureSetSize = TracePC::kFeatureSetSize;
102
103 void ValidateFeatureSet() {
104 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++) {
105 Feature &Fe = FeatureSet[Idx];
106 if(Fe.Count && Fe.SmallestElementSize)
107 Inputs[Fe.SmallestElementIdx].Tmp++;
108 }
109 for (auto &II: Inputs) {
110 assert(II.Tmp == II.NumFeatures);
111 II.Tmp = 0;
112 }
113 }
114
115 void UpdateFeatureSet(size_t CurrentElementIdx) {
116 auto &II = Inputs[CurrentElementIdx];
117 size_t Size = II.U.size();
118 if (!Size)
119 return;
120 bool Updated = false;
121 for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++) {
122 if (!TPC.HasFeature(Idx))
123 continue;
124 Feature &Fe = FeatureSet[Idx];
125 Fe.Count++;
126 if (!Fe.SmallestElementSize ||
127 Fe.SmallestElementSize > Size) {
128 II.NumFeatures++;
129 if (Fe.SmallestElementSize > Size) {
130 auto &OlderII = Inputs[Fe.SmallestElementIdx];
131 assert(OlderII.NumFeatures > 0);
132 OlderII.NumFeatures--;
133 if (!OlderII.NumFeatures && FeatureDebug)
134 Printf("EVICTED %zd\n", Fe.SmallestElementIdx);
135 }
136 Fe.SmallestElementIdx = CurrentElementIdx;
137 Fe.SmallestElementSize = Size;
138 Updated = true;
139 }
140 }
141 if (Updated && FeatureDebug) PrintFeatureSet();
142 ValidateFeatureSet();
143 }
144
Kostya Serebryany20801e12016-09-21 21:41:48 +0000145 // Updates the probability distribution for the units in the corpus.
146 // Must be called whenever the corpus or unit weights are changed.
147 void UpdateCorpusDistribution() {
148 size_t N = Inputs.size();
149 std::vector<double> Intervals(N + 1);
150 std::vector<double> Weights(N);
151 std::iota(Intervals.begin(), Intervals.end(), 0);
152 std::iota(Weights.begin(), Weights.end(), 1);
153 CorpusDistribution = std::piecewise_constant_distribution<double>(
154 Intervals.begin(), Intervals.end(), Weights.begin());
155 }
156 std::piecewise_constant_distribution<double> CorpusDistribution;
157
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000158 std::unordered_set<std::string> Hashes;
Kostya Serebryany20801e12016-09-21 21:41:48 +0000159 std::vector<InputInfo> Inputs;
Kostya Serebryany2c556132016-09-30 01:19:56 +0000160
161 struct Feature {
162 size_t Count;
163 size_t SmallestElementIdx;
164 size_t SmallestElementSize;
165 };
166 Feature FeatureSet[kFeatureSetSize];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000167};
168
169} // namespace fuzzer
170
171#endif // LLVM_FUZZER_CORPUS