blob: 877a483a539c16402d075104df0177d731cdfa2c [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
15#include "FuzzerDefs.h"
16
17namespace fuzzer {
18
19struct InputInfo {
20 Unit U; // The actual input data.
21};
22
23class InputCorpus {
24 public:
25 InputCorpus() {
26 Corpus.reserve(1 << 14); // Avoid too many resizes.
27 }
28 size_t size() const { return Corpus.size(); }
29 bool empty() const { return Corpus.empty(); }
30 const Unit &operator[] (size_t Idx) const { return Corpus[Idx].U; }
31 void Append(const std::vector<Unit> &V) {
32 for (auto &U : V)
33 push_back(U);
34 }
35 void push_back(const Unit &U) {
36 auto H = Hash(U);
37 if (!Hashes.insert(H).second) return;
38 InputInfo II;
39 II.U = U;
40 Corpus.push_back(II);
41 }
42
43 typedef const std::vector<InputInfo>::const_iterator ConstIter;
44 ConstIter begin() const { return Corpus.begin(); }
45 ConstIter end() const { return Corpus.end(); }
46
47 bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); }
48
49 private:
50 std::unordered_set<std::string> Hashes;
51 std::vector<InputInfo> Corpus;
52};
53
54} // namespace fuzzer
55
56#endif // LLVM_FUZZER_CORPUS