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