blob: 5a8581f820061c46e935050dab6342216c4eb389 [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 Serebryany29bb6642016-09-21 22:42:17 +000039 void AddToCorpus(const Unit &U) {
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000040 auto H = Hash(U);
41 if (!Hashes.insert(H).second) return;
42 InputInfo II;
43 II.U = U;
Kostya Serebryany20801e12016-09-21 21:41:48 +000044 memcpy(II.Sha1, H.data(), kSHA1NumBytes);
45 Inputs.push_back(II);
46 UpdateCorpusDistribution();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000047 }
48
49 typedef const std::vector<InputInfo>::const_iterator ConstIter;
Kostya Serebryany20801e12016-09-21 21:41:48 +000050 ConstIter begin() const { return Inputs.begin(); }
51 ConstIter end() const { return Inputs.end(); }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000052
53 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
Kostya Serebryany29bb6642016-09-21 22:42:17 +000054 InputInfo &ChooseUnitToMutate(Random &Rand) {
Kostya Serebryany20801e12016-09-21 21:41:48 +000055 return Inputs[ChooseUnitIdxToMutate(Rand)];
56 };
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000057
Kostya Serebryany20801e12016-09-21 21:41:48 +000058 // Returns an index of random unit from the corpus to mutate.
59 // Hypothesis: units added to the corpus last are more likely to be
60 // interesting. This function gives more weight to the more recent units.
61 size_t ChooseUnitIdxToMutate(Random &Rand) {
Kostya Serebryany29bb6642016-09-21 22:42:17 +000062 size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937()));
Kostya Serebryany20801e12016-09-21 21:41:48 +000063 assert(Idx < Inputs.size());
64 return Idx;
65 }
66
Kostya Serebryany29bb6642016-09-21 22:42:17 +000067 void PrintStats() {
68 for (size_t i = 0; i < Inputs.size(); i++) {
69 const auto &II = Inputs[i];
70 Printf(" [%zd %s]\tsz: %zd\truns: %zd\tsucc: %zd\n", i,
71 Sha1ToString(II.Sha1).c_str(), II.U.size(),
72 II.NumExecutedMutations, II.NumSuccessfullMutations);
73 }
74 }
75
Kostya Serebryany20801e12016-09-21 21:41:48 +000076private:
77
78 // Updates the probability distribution for the units in the corpus.
79 // Must be called whenever the corpus or unit weights are changed.
80 void UpdateCorpusDistribution() {
81 size_t N = Inputs.size();
82 std::vector<double> Intervals(N + 1);
83 std::vector<double> Weights(N);
84 std::iota(Intervals.begin(), Intervals.end(), 0);
85 std::iota(Weights.begin(), Weights.end(), 1);
86 CorpusDistribution = std::piecewise_constant_distribution<double>(
87 Intervals.begin(), Intervals.end(), Weights.begin());
88 }
89 std::piecewise_constant_distribution<double> CorpusDistribution;
90
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000091 std::unordered_set<std::string> Hashes;
Kostya Serebryany20801e12016-09-21 21:41:48 +000092 std::vector<InputInfo> Inputs;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000093};
94
95} // namespace fuzzer
96
97#endif // LLVM_FUZZER_CORPUS