1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // fuzzer::Dictionary
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_FUZZER_DICTIONARY_H
12 #define LLVM_FUZZER_DICTIONARY_H
13 
14 #include "FuzzerDefs.h"
15 #include "FuzzerIO.h"
16 #include "FuzzerUtil.h"
17 #include <algorithm>
18 #include <limits>
19 
20 namespace fuzzer {
21 // A simple POD sized array of bytes.
22 template <size_t kMaxSizeT> class FixedWord {
23 public:
24   static const size_t kMaxSize = kMaxSizeT;
25   FixedWord() {}
26   FixedWord(const uint8_t *B, size_t S) { Set(B, S); }
27 
28   void Set(const uint8_t *B, size_t S) {
29     static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
30                   "FixedWord::kMaxSizeT cannot fit in a uint8_t.");
31     assert(S <= kMaxSize);
32     memcpy(Data, B, S);
33     Size = static_cast<uint8_t>(S);
34   }
35 
36   bool operator==(const FixedWord<kMaxSize> &w) const {
37     return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
38   }
39 
40   static size_t GetMaxSize() { return kMaxSize; }
41   const uint8_t *data() const { return Data; }
42   uint8_t size() const { return Size; }
43 
44 private:
45   uint8_t Size = 0;
46   uint8_t Data[kMaxSize];
47 };
48 
49 typedef FixedWord<64> Word;
50 
51 class DictionaryEntry {
52  public:
53   DictionaryEntry() {}
54   DictionaryEntry(Word W) : W(W) {}
55   DictionaryEntry(Word W, size_t PositionHint)
56       : W(W), PositionHint(PositionHint) {}
57   const Word &GetW() const { return W; }
58 
59   bool HasPositionHint() const {
60     return PositionHint != std::numeric_limits<size_t>::max();
61   }
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 
71   void Print(const char *PrintAfter = "\n") {
72     PrintASCII(W.data(), W.size());
73     if (HasPositionHint())
74       Printf("@%zd", GetPositionHint());
75     Printf("%s", PrintAfter);
76   }
77 
78 private:
79   Word W;
80   size_t PositionHint = std::numeric_limits<size_t>::max();
81   size_t UseCount = 0;
82   size_t SuccessCount = 0;
83 };
84 
85 class Dictionary {
86  public:
87   static const size_t kMaxDictSize = 1 << 14;
88 
89   bool ContainsWord(const Word &W) const {
90     return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
91       return DE.GetW() == W;
92     });
93   }
94   const DictionaryEntry *begin() const { return &DE[0]; }
95   const DictionaryEntry *end() const { return begin() + Size; }
96   DictionaryEntry & operator[] (size_t Idx) {
97     assert(Idx < Size);
98     return DE[Idx];
99   }
100   void push_back(DictionaryEntry DE) {
101     if (Size < kMaxDictSize)
102       this->DE[Size++] = DE;
103   }
104   void clear() { Size = 0; }
105   bool empty() const { return Size == 0; }
106   size_t size() const { return Size; }
107 
108 private:
109   DictionaryEntry DE[kMaxDictSize];
110   size_t Size = 0;
111 };
112 
113 // Parses one dictionary entry.
114 // If successful, writes the entry to Unit and returns true,
115 // otherwise returns false.
116 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
117 // Parses the dictionary file, fills Units, returns true iff all lines
118 // were parsed successfully.
119 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
120 
121 }  // namespace fuzzer
122 
123 #endif  // LLVM_FUZZER_DICTIONARY_H
124