blob: 821cf9bd0016db7fadb911ff4f3219776a059cf4 [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50:50 +00001//===- FuzzerDictionary.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::Dictionary
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_DICTIONARY_H
13#define LLVM_FUZZER_DICTIONARY_H
14
Kostya Serebryany556894f2016-09-21 02:05:39 +000015#include <algorithm>
16#include <limits>
17
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000018#include "FuzzerDefs.h"
19
20namespace fuzzer {
21// A simple POD sized array of bytes.
22template <size_t kMaxSize> class FixedWord {
23public:
24 FixedWord() {}
25 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
26
27 void Set(const uint8_t *B, uint8_t S) {
28 assert(S <= kMaxSize);
29 memcpy(Data, B, S);
30 Size = S;
31 }
32
33 bool operator==(const FixedWord<kMaxSize> &w) const {
34 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
35 }
36
37 bool operator<(const FixedWord<kMaxSize> &w) const {
38 if (Size != w.Size)
39 return Size < w.Size;
40 return memcmp(Data, w.Data, Size) < 0;
41 }
42
43 static size_t GetMaxSize() { return kMaxSize; }
44 const uint8_t *data() const { return Data; }
45 uint8_t size() const { return Size; }
46
47private:
48 uint8_t Size = 0;
49 uint8_t Data[kMaxSize];
50};
51
52typedef FixedWord<27> Word; // 28 bytes.
53
54class DictionaryEntry {
55 public:
56 DictionaryEntry() {}
57 DictionaryEntry(Word W) : W(W) {}
58 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
59 const Word &GetW() const { return W; }
60
61 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
62 size_t GetPositionHint() const {
63 assert(HasPositionHint());
64 return PositionHint;
65 }
66 void IncUseCount() { UseCount++; }
67 void IncSuccessCount() { SuccessCount++; }
68 size_t GetUseCount() const { return UseCount; }
69 size_t GetSuccessCount() const {return SuccessCount; }
70
71private:
72 Word W;
73 size_t PositionHint = std::numeric_limits<size_t>::max();
74 size_t UseCount = 0;
75 size_t SuccessCount = 0;
76};
77
78class Dictionary {
79 public:
80 static const size_t kMaxDictSize = 1 << 14;
81
82 bool ContainsWord(const Word &W) const {
83 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
84 return DE.GetW() == W;
85 });
86 }
87 const DictionaryEntry *begin() const { return &DE[0]; }
88 const DictionaryEntry *end() const { return begin() + Size; }
89 DictionaryEntry & operator[] (size_t Idx) {
90 assert(Idx < Size);
91 return DE[Idx];
92 }
93 void push_back(DictionaryEntry DE) {
94 if (Size < kMaxDictSize)
95 this->DE[Size++] = DE;
96 }
97 void clear() { Size = 0; }
98 bool empty() const { return Size == 0; }
99 size_t size() const { return Size; }
100
101private:
102 DictionaryEntry DE[kMaxDictSize];
103 size_t Size = 0;
104};
105
106// Parses one dictionary entry.
107// If successfull, write the enty to Unit and returns true,
108// otherwise returns false.
109bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
110// Parses the dictionary file, fills Units, returns true iff all lines
111// were parsed succesfully.
112bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
113
114} // namespace fuzzer
115
116#endif // LLVM_FUZZER_DICTIONARY_H
117