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
);
33 Size
= static_cast<uint8_t>(S
);
36 bool operator==(const FixedWord
<kMaxSize
> &w
) const {
37 return Size
== w
.Size
&& 0 == memcmp(Data
, w
.Data
, Size
);
40 static size_t GetMaxSize() { return kMaxSize
; }
41 const uint8_t *data() const { return Data
; }
42 uint8_t size() const { return Size
; }
46 uint8_t Data
[kMaxSize
];
49 typedef FixedWord
<64> Word
;
51 class DictionaryEntry
{
54 DictionaryEntry(Word W
) : W(W
) {}
55 DictionaryEntry(Word W
, size_t PositionHint
)
56 : W(W
), PositionHint(PositionHint
) {}
57 const Word
&GetW() const { return W
; }
59 bool HasPositionHint() const {
60 return PositionHint
!= std::numeric_limits
<size_t>::max();
62 size_t GetPositionHint() const {
63 assert(HasPositionHint());
66 void IncUseCount() { UseCount
++; }
67 void IncSuccessCount() { SuccessCount
++; }
68 size_t GetUseCount() const { return UseCount
; }
69 size_t GetSuccessCount() const {return SuccessCount
; }
71 void Print(const char *PrintAfter
= "\n") {
72 PrintASCII(W
.data(), W
.size());
73 if (HasPositionHint())
74 Printf("@%zd", GetPositionHint());
75 Printf("%s", PrintAfter
);
80 size_t PositionHint
= std::numeric_limits
<size_t>::max();
82 size_t SuccessCount
= 0;
87 static const size_t kMaxDictSize
= 1 << 14;
89 bool ContainsWord(const Word
&W
) const {
90 return std::any_of(begin(), end(), [&](const DictionaryEntry
&DE
) {
91 return DE
.GetW() == W
;
94 const DictionaryEntry
*begin() const { return &DE
[0]; }
95 const DictionaryEntry
*end() const { return begin() + Size
; }
96 DictionaryEntry
& operator[] (size_t Idx
) {
100 void push_back(DictionaryEntry DE
) {
101 if (Size
< kMaxDictSize
)
102 this->DE
[Size
++] = DE
;
104 void clear() { Size
= 0; }
105 bool empty() const { return Size
== 0; }
106 size_t size() const { return Size
; }
109 DictionaryEntry DE
[kMaxDictSize
];
113 // Parses one dictionary entry.
114 // If successful, writes the entry to Unit and returns true,
115 // otherwise returns false.
116 bool ParseOneDictionaryEntry(const std::string
&Str
, Unit
*U
);
117 // Parses the dictionary file, fills Units, returns true iff all lines
118 // were parsed successfully.
119 bool ParseDictionaryFile(const std::string
&Text
, std::vector
<Unit
> *Units
);
121 } // namespace fuzzer
123 #endif // LLVM_FUZZER_DICTIONARY_H