blob: d1a3efebe2d3ea9854c65583608dbdf3de763044 [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 Serebryany624f59f2016-09-22 01:34:58 +000029
30 // A set of features (PCIDs, etc) that were first found with this unit.
31 std::vector<uintptr_t> Features;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000032};
33
34class InputCorpus {
35 public:
36 InputCorpus() {
Kostya Serebryany20801e12016-09-21 21:41:48 +000037 Inputs.reserve(1 << 14); // Avoid too many resizes.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000038 }
Kostya Serebryany20801e12016-09-21 21:41:48 +000039 size_t size() const { return Inputs.size(); }
40 bool empty() const { return Inputs.empty(); }
41 const Unit &operator[] (size_t Idx) const { return Inputs[Idx].U; }
Kostya Serebryany624f59f2016-09-22 01:34:58 +000042 void AddToCorpus(const Unit &U, uintptr_t *Features, size_t NumFeatures) {
43 uint8_t Hash[kSHA1NumBytes];
44 ComputeSHA1(U.data(), U.size(), Hash);
45 if (!Hashes.insert(Sha1ToString(Hash)).second) return;
46 Inputs.push_back(InputInfo());
47 InputInfo &II = Inputs.back();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000048 II.U = U;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000049 II.Features.insert(II.Features.begin(), Features, Features + NumFeatures);
50 memcpy(II.Sha1, Hash, kSHA1NumBytes);
Kostya Serebryany20801e12016-09-21 21:41:48 +000051 UpdateCorpusDistribution();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000052 }
53
54 typedef const std::vector<InputInfo>::const_iterator ConstIter;
Kostya Serebryany20801e12016-09-21 21:41:48 +000055 ConstIter begin() const { return Inputs.begin(); }
56 ConstIter end() const { return Inputs.end(); }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000057
58 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000059 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryany20801e12016-09-21 21:41:48 +000060 return Inputs[ChooseUnitIdxToMutate(Rand)];
61 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000062
Kostya Serebryany20801e12016-09-21 21:41:48 +000063 // Returns an index of random unit from the corpus to mutate.
64 // Hypothesis: units added to the corpus last are more likely to be
65 // interesting. This function gives more weight to the more recent units.
66 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000067 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000068 assert(Idx < Inputs.size());
69 return Idx;
70 }
71
Kostya Serebryany29bb6642016-09-21 22:42:17 +000072 void PrintStats() {
73 for (size_t i = 0; i < Inputs.size(); i++) {
74 const auto &II = Inputs[i];
Kostya Serebryany624f59f2016-09-22 01:34:58 +000075 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\tfea: %zd\n", i,
Kostya Serebryany29bb6642016-09-21 22:42:17 +000076 Sha1ToString(II.Sha1).c_str(), II.U.size(),
Kostya Serebryany624f59f2016-09-22 01:34:58 +000077 II.NumExecutedMutations, II.NumSuccessfullMutations,
78 II.Features.size());
Kostya Serebryany29bb6642016-09-21 22:42:17 +000079 }
80 }
81
Kostya Serebryany20801e12016-09-21 21:41:48 +000082private:
83
84 // Updates the probability distribution for the units in the corpus.
85 // Must be called whenever the corpus or unit weights are changed.
86 void UpdateCorpusDistribution() {
87 size_t N = Inputs.size();
88 std::vector<double> Intervals(N + 1);
89 std::vector<double> Weights(N);
90 std::iota(Intervals.begin(), Intervals.end(), 0);
91 std::iota(Weights.begin(), Weights.end(), 1);
92 CorpusDistribution = std::piecewise_constant_distribution<double>(
93 Intervals.begin(), Intervals.end(), Weights.begin());
94 }
95 std::piecewise_constant_distribution<double> CorpusDistribution;
96
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000097 std::unordered_set<std::string> Hashes;
Kostya Serebryany20801e12016-09-21 21:41:48 +000098 std::vector<InputInfo> Inputs;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099};
100
101} // namespace fuzzer
102
103#endif // LLVM_FUZZER_CORPUS