1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
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
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_DICTIONARY_H
12 #define LLVM_FUZZER_DICTIONARY_H
14 #include "FuzzerDefs.h"
16 #include "FuzzerUtil.h"
21 // A simple POD sized array of bytes.
22 template <size_t kMaxSizeT
> class FixedWord
{
24 static const size_t kMaxSize
= kMaxSizeT
;
26 FixedWord(const uint8_t *B
, uint8_t S
) { Set(B
, S
); }
28 void Set(const uint8_t *B
, uint8_t S
) {
29 assert(S
<= kMaxSize
);
34 bool operator==(const FixedWord
<kMaxSize
> &w
) const {
35 return Size
== w
.Size
&& 0 == memcmp(Data
, w
.Data
, Size
);
38 static size_t GetMaxSize() { return kMaxSize
; }
39 const uint8_t *data() const { return Data
; }
40 uint8_t size() const { return Size
; }
44 uint8_t Data
[kMaxSize
];
47 typedef FixedWord
<64> Word
;
49 class DictionaryEntry
{
52 DictionaryEntry(Word W
) : W(W
) {}
53 DictionaryEntry(Word W
, size_t PositionHint
) : W(W
), PositionHint(PositionHint
) {}
54 const Word
&GetW() const { return W
; }
56 bool HasPositionHint() const { return PositionHint
!= std::numeric_limits
<size_t>::max(); }
57 size_t GetPositionHint() const {
58 assert(HasPositionHint());
61 void IncUseCount() { UseCount
++; }
62 void IncSuccessCount() { SuccessCount
++; }
63 size_t GetUseCount() const { return UseCount
; }
64 size_t GetSuccessCount() const {return SuccessCount
; }
66 void Print(const char *PrintAfter
= "\n") {
67 PrintASCII(W
.data(), W
.size());
68 if (HasPositionHint())
69 Printf("@%zd", GetPositionHint());
70 Printf("%s", PrintAfter
);
75 size_t PositionHint
= std::numeric_limits
<size_t>::max();
77 size_t SuccessCount
= 0;
82 static const size_t kMaxDictSize
= 1 << 14;
84 bool ContainsWord(const Word
&W
) const {
85 return std::any_of(begin(), end(), [&](const DictionaryEntry
&DE
) {
86 return DE
.GetW() == W
;
89 const DictionaryEntry
*begin() const { return &DE
[0]; }
90 const DictionaryEntry
*end() const { return begin() + Size
; }
91 DictionaryEntry
& operator[] (size_t Idx
) {
95 void push_back(DictionaryEntry DE
) {
96 if (Size
< kMaxDictSize
)
97 this->DE
[Size
++] = DE
;
99 void clear() { Size
= 0; }
100 bool empty() const { return Size
== 0; }
101 size_t size() const { return Size
; }
104 DictionaryEntry DE
[kMaxDictSize
];
108 // Parses one dictionary entry.
109 // If successful, write the enty to Unit and returns true,
110 // otherwise returns false.
111 bool ParseOneDictionaryEntry(const std::string
&Str
, Unit
*U
);
112 // Parses the dictionary file, fills Units, returns true iff all lines
113 // were parsed successfully.
114 bool ParseDictionaryFile(const std::string
&Text
, Vector
<Unit
> *Units
);
116 } // namespace fuzzer
118 #endif // LLVM_FUZZER_DICTIONARY_H