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 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str
;
37 return Size
== w
.Size
&& 0 == memcmp(Data
, w
.Data
, Size
);
40 bool operator<(const FixedWord
<kMaxSize
> &w
) const {
41 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str
;
44 return memcmp(Data
, w
.Data
, Size
) < 0;
47 static size_t GetMaxSize() { return kMaxSize
; }
48 const uint8_t *data() const { return Data
; }
49 uint8_t size() const { return Size
; }
53 uint8_t Data
[kMaxSize
];
56 typedef FixedWord
<64> Word
;
58 class 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
; }
65 bool HasPositionHint() const { return PositionHint
!= std::numeric_limits
<size_t>::max(); }
66 size_t GetPositionHint() const {
67 assert(HasPositionHint());
70 void IncUseCount() { UseCount
++; }
71 void IncSuccessCount() { SuccessCount
++; }
72 size_t GetUseCount() const { return UseCount
; }
73 size_t GetSuccessCount() const {return SuccessCount
; }
75 void Print(const char *PrintAfter
= "\n") {
76 PrintASCII(W
.data(), W
.size());
77 if (HasPositionHint())
78 Printf("@%zd", GetPositionHint());
79 Printf("%s", PrintAfter
);
84 size_t PositionHint
= std::numeric_limits
<size_t>::max();
86 size_t SuccessCount
= 0;
91 static const size_t kMaxDictSize
= 1 << 14;
93 bool ContainsWord(const Word
&W
) const {
94 return std::any_of(begin(), end(), [&](const DictionaryEntry
&DE
) {
95 return DE
.GetW() == W
;
98 const DictionaryEntry
*begin() const { return &DE
[0]; }
99 const DictionaryEntry
*end() const { return begin() + Size
; }
100 DictionaryEntry
& operator[] (size_t Idx
) {
104 void push_back(DictionaryEntry DE
) {
105 if (Size
< kMaxDictSize
)
106 this->DE
[Size
++] = DE
;
108 void clear() { Size
= 0; }
109 bool empty() const { return Size
== 0; }
110 size_t size() const { return Size
; }
113 DictionaryEntry DE
[kMaxDictSize
];
117 // Parses one dictionary entry.
118 // If successfull, write the enty to Unit and returns true,
119 // otherwise returns false.
120 bool ParseOneDictionaryEntry(const std::string
&Str
, Unit
*U
);
121 // Parses the dictionary file, fills Units, returns true iff all lines
122 // were parsed succesfully.
123 bool ParseDictionaryFile(const std::string
&Text
, Vector
<Unit
> *Units
);
125 } // namespace fuzzer
127 #endif // LLVM_FUZZER_DICTIONARY_H