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