Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 1 | //===- 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 Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 15 | #include <random> |
Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 16 | #include <unordered_set> |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 17 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 18 | #include "FuzzerDefs.h" |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 19 | #include "FuzzerRandom.h" |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 20 | |
| 21 | namespace fuzzer { |
| 22 | |
| 23 | struct InputInfo { |
| 24 | Unit U; // The actual input data. |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 25 | uint8_t Sha1[kSHA1NumBytes]; // Checksum. |
Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 26 | // Stats. |
| 27 | uintptr_t NumExecutedMutations = 0; |
| 28 | uintptr_t NumSuccessfullMutations = 0; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | class InputCorpus { |
| 32 | public: |
| 33 | InputCorpus() { |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 34 | Inputs.reserve(1 << 14); // Avoid too many resizes. |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 35 | } |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 36 | 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 Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 39 | void AddToCorpus(const Unit &U) { |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 40 | auto H = Hash(U); |
| 41 | if (!Hashes.insert(H).second) return; |
| 42 | InputInfo II; |
| 43 | II.U = U; |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 44 | memcpy(II.Sha1, H.data(), kSHA1NumBytes); |
| 45 | Inputs.push_back(II); |
| 46 | UpdateCorpusDistribution(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | typedef const std::vector<InputInfo>::const_iterator ConstIter; |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 50 | ConstIter begin() const { return Inputs.begin(); } |
| 51 | ConstIter end() const { return Inputs.end(); } |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 52 | |
| 53 | bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); } |
Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 54 | InputInfo &ChooseUnitToMutate(Random &Rand) { |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 55 | return Inputs[ChooseUnitIdxToMutate(Rand)]; |
| 56 | }; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 57 | |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 58 | // 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 Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 62 | size_t Idx = static_cast<size_t>(CorpusDistribution(Rand.Get_mt19937())); |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 63 | assert(Idx < Inputs.size()); |
| 64 | return Idx; |
| 65 | } |
| 66 | |
Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame^] | 67 | 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 Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 76 | private: |
| 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 Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 91 | std::unordered_set<std::string> Hashes; |
Kostya Serebryany | 20801e1 | 2016-09-21 21:41:48 +0000 | [diff] [blame] | 92 | std::vector<InputInfo> Inputs; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace fuzzer |
| 96 | |
| 97 | #endif // LLVM_FUZZER_CORPUS |