1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUZZER_DICTIONARY_H
13 #define LLVM_FUZZER_DICTIONARY_H
15 #include "FuzzerDefs.h"
17 #include "FuzzerUtil.h"
22 // A simple POD sized array of bytes.
23 template <size_t kMaxSizeT> class FixedWord {
25 static const size_t kMaxSize = kMaxSizeT;
27 FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
29 void Set(const uint8_t *B, uint8_t S) {
30 assert(S <= kMaxSize);
35 bool operator==(const FixedWord<kMaxSize> &w) const {
36 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
39 static size_t GetMaxSize() { return kMaxSize; }
40 const uint8_t *data() const { return Data; }
41 uint8_t size() const { return Size; }
45 uint8_t Data[kMaxSize];
48 typedef FixedWord<64> Word;
50 class DictionaryEntry {
53 DictionaryEntry(Word W) : W(W) {}
54 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
55 const Word &GetW() const { return W; }
57 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
58 size_t GetPositionHint() const {
59 assert(HasPositionHint());
62 void IncUseCount() { UseCount++; }
63 void IncSuccessCount() { SuccessCount++; }
64 size_t GetUseCount() const { return UseCount; }
65 size_t GetSuccessCount() const {return SuccessCount; }
67 void Print(const char *PrintAfter = "\n") {
68 PrintASCII(W.data(), W.size());
69 if (HasPositionHint())
70 Printf("@%zd", GetPositionHint());
71 Printf("%s", PrintAfter);
76 size_t PositionHint = std::numeric_limits<size_t>::max();
78 size_t SuccessCount = 0;
83 static const size_t kMaxDictSize = 1 << 14;
85 bool ContainsWord(const Word &W) const {
86 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
87 return DE.GetW() == W;
90 const DictionaryEntry *begin() const { return &DE[0]; }
91 const DictionaryEntry *end() const { return begin() + Size; }
92 DictionaryEntry & operator[] (size_t Idx) {
96 void push_back(DictionaryEntry DE) {
97 if (Size < kMaxDictSize)
98 this->DE[Size++] = DE;
100 void clear() { Size = 0; }
101 bool empty() const { return Size == 0; }
102 size_t size() const { return Size; }
105 DictionaryEntry DE[kMaxDictSize];
109 // Parses one dictionary entry.
110 // If successful, write the enty to Unit and returns true,
111 // otherwise returns false.
112 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
113 // Parses the dictionary file, fills Units, returns true iff all lines
114 // were parsed successfully.
115 bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units);
117 } // namespace fuzzer
119 #endif // LLVM_FUZZER_DICTIONARY_H