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
, size_t S
) { Set(B
, S
); }
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 cannot take null pointer arguments even if Size is 0.
35 Size
= static_cast<uint8_t>(S
);
38 bool operator==(const FixedWord
<kMaxSize
> &w
) const {
39 return Size
== w
.Size
&& 0 == memcmp(Data
, w
.Data
, Size
);
42 static size_t GetMaxSize() { return kMaxSize
; }
43 const uint8_t *data() const { return Data
; }
44 uint8_t size() const { return Size
; }
48 uint8_t Data
[kMaxSize
];
51 typedef FixedWord
<64> Word
;
53 class DictionaryEntry
{
56 DictionaryEntry(Word W
) : W(W
) {}
57 DictionaryEntry(Word W
, size_t PositionHint
)
58 : W(W
), PositionHint(PositionHint
) {}
59 const Word
&GetW() const { return W
; }
61 bool HasPositionHint() const {
62 return PositionHint
!= std::numeric_limits
<size_t>::max();
64 size_t GetPositionHint() const {
65 assert(HasPositionHint());
68 void IncUseCount() { UseCount
++; }
69 void IncSuccessCount() { SuccessCount
++; }
70 size_t GetUseCount() const { return UseCount
; }
71 size_t GetSuccessCount() const {return SuccessCount
; }
73 void Print(const char *PrintAfter
= "\n") {
74 PrintASCII(W
.data(), W
.size());
75 if (HasPositionHint())
76 Printf("@%zd", GetPositionHint());
77 Printf("%s", PrintAfter
);
82 size_t PositionHint
= std::numeric_limits
<size_t>::max();
84 size_t SuccessCount
= 0;
89 static const size_t kMaxDictSize
= 1 << 14;
91 bool ContainsWord(const Word
&W
) const {
92 return std::any_of(begin(), end(), [&](const DictionaryEntry
&DE
) {
93 return DE
.GetW() == W
;
96 const DictionaryEntry
*begin() const { return &DE
[0]; }
97 const DictionaryEntry
*end() const { return begin() + Size
; }
98 DictionaryEntry
& operator[] (size_t Idx
) {
102 void push_back(DictionaryEntry DE
) {
103 if (Size
< kMaxDictSize
)
104 this->DE
[Size
++] = DE
;
106 void clear() { Size
= 0; }
107 bool empty() const { return Size
== 0; }
108 size_t size() const { return Size
; }
111 DictionaryEntry DE
[kMaxDictSize
];
115 // Parses one dictionary entry.
116 // If successful, writes the entry to Unit and returns true,
117 // otherwise returns false.
118 bool ParseOneDictionaryEntry(const std::string
&Str
, Unit
*U
);
119 // Parses the dictionary file, fills Units, returns true iff all lines
120 // were parsed successfully.
121 bool ParseDictionaryFile(const std::string
&Text
, std::vector
<Unit
> *Units
);
123 } // namespace fuzzer
125 #endif // LLVM_FUZZER_DICTIONARY_H