xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerDictionary.h (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
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) : W(W), PositionHint(PositionHint) {}
56   const Word &GetW() const { return W; }
57 
58   bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
59   size_t GetPositionHint() const {
60     assert(HasPositionHint());
61     return PositionHint;
62   }
63   void IncUseCount() { UseCount++; }
64   void IncSuccessCount() { SuccessCount++; }
65   size_t GetUseCount() const { return UseCount; }
66   size_t GetSuccessCount() const {return SuccessCount; }
67 
68   void Print(const char *PrintAfter = "\n") {
69     PrintASCII(W.data(), W.size());
70     if (HasPositionHint())
71       Printf("@%zd", GetPositionHint());
72     Printf("%s", PrintAfter);
73   }
74 
75 private:
76   Word W;
77   size_t PositionHint = std::numeric_limits<size_t>::max();
78   size_t UseCount = 0;
79   size_t SuccessCount = 0;
80 };
81 
82 class Dictionary {
83  public:
84   static const size_t kMaxDictSize = 1 << 14;
85 
86   bool ContainsWord(const Word &W) const {
87     return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
88       return DE.GetW() == W;
89     });
90   }
91   const DictionaryEntry *begin() const { return &DE[0]; }
92   const DictionaryEntry *end() const { return begin() + Size; }
93   DictionaryEntry & operator[] (size_t Idx) {
94     assert(Idx < Size);
95     return DE[Idx];
96   }
97   void push_back(DictionaryEntry DE) {
98     if (Size < kMaxDictSize)
99       this->DE[Size++] = DE;
100   }
101   void clear() { Size = 0; }
102   bool empty() const { return Size == 0; }
103   size_t size() const { return Size; }
104 
105 private:
106   DictionaryEntry DE[kMaxDictSize];
107   size_t Size = 0;
108 };
109 
110 // Parses one dictionary entry.
111 // If successful, write the enty to Unit and returns true,
112 // otherwise returns false.
113 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
114 // Parses the dictionary file, fills Units, returns true iff all lines
115 // were parsed successfully.
116 bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units);
117 
118 }  // namespace fuzzer
119 
120 #endif  // LLVM_FUZZER_DICTIONARY_H
121