blob: b9a066552733d316134f39525548bc1e384d94ce [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 Serebryany6f5a8042016-09-21 01:50:50 +000020
21namespace fuzzer {
22
23struct InputInfo {
24 Unit U; // The actual input data.
Kostya Serebryany20801e12016-09-21 21:41:48 +000025 uint8_t Sha1[kSHA1NumBytes]; // Checksum.
Kostya Serebryany29bb6642016-09-21 22:42:17 +000026 // Stats.
27 uintptr_t NumExecutedMutations = 0;
28 uintptr_t NumSuccessfullMutations = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000029};
30
31class InputCorpus {
32 public:
33 InputCorpus() {
Kostya Serebryany20801e12016-09-21 21:41:48 +000034 Inputs.reserve(1 << 14); // Avoid too many resizes.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000035 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000036 size_t size() const { return Inputs.size(); }
37 bool empty() const { return Inputs.empty(); }
38 const Unit &operator[] (size_t Idx) const { return Inputs[Idx].U; }
Kostya Serebryany16a145f2016-09-23 01:58:51 +000039 void AddToCorpus(const Unit &U) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000040 uint8_t Hash[kSHA1NumBytes];
41 ComputeSHA1(U.data(), U.size(), Hash);
42 if (!Hashes.insert(Sha1ToString(Hash)).second) return;
43 Inputs.push_back(InputInfo());
44 InputInfo &II = Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045 II.U = U;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000046 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000047 UpdateCorpusDistribution();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000048 }
49
50 typedef const std::vector<InputInfo>::const_iterator ConstIter;
Kostya Serebryany20801e12016-09-21 21:41:48 +000051 ConstIter begin() const { return Inputs.begin(); }
52 ConstIter end() const { return Inputs.end(); }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000053
54 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000055 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryany20801e12016-09-21 21:41:48 +000056 return Inputs[ChooseUnitIdxToMutate(Rand)];
57 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000058
Kostya Serebryany20801e12016-09-21 21:41:48 +000059 // Returns an index of random unit from the corpus to mutate.
60 // Hypothesis: units added to the corpus last are more likely to be
61 // interesting. This function gives more weight to the more recent units.
62 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000063 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000064 assert(Idx < Inputs.size());
65 return Idx;
66 }
67
Kostya Serebryany29bb6642016-09-21 22:42:17 +000068 void PrintStats() {
69 for (size_t i = 0; i < Inputs.size(); i++) {
70 const auto &II = Inputs[i];
Kostya Serebryany16a145f2016-09-23 01:58:51 +000071 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +000072 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany16a145f2016-09-23 01:58:51 +000073 II.NumExecutedMutations, II.NumSuccessfullMutations);
Kostya Serebryany29bb6642016-09-21 22:42:17 +000074 }
75 }
76
Kostya Serebryany20801e12016-09-21 21:41:48 +000077private:
78
79 // Updates the probability distribution for the units in the corpus.
80 // Must be called whenever the corpus or unit weights are changed.
81 void UpdateCorpusDistribution() {
82 size_t N = Inputs.size();
83 std::vector<double> Intervals(N + 1);
84 std::vector<double> Weights(N);
85 std::iota(Intervals.begin(), Intervals.end(), 0);
86 std::iota(Weights.begin(), Weights.end(), 1);
87 CorpusDistribution = std::piecewise_constant_distribution<double>(
88 Intervals.begin(), Intervals.end(), Weights.begin());
89 }
90 std::piecewise_constant_distribution<double> CorpusDistribution;
91
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000092 std::unordered_set<std::string> Hashes;
Kostya Serebryany20801e12016-09-21 21:41:48 +000093 std::vector<InputInfo> Inputs;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000094};
95
96} // namespace fuzzer
97
98#endif // LLVM_FUZZER_CORPUS