[DominatorTree] Add support for mixed pre/post CFG views.
[llvm-project.git] / compiler-rt / lib / fuzzer / FuzzerDictionary.h
blob301c5d9afecf8af2410acca1ae3dd18b3adadee0
1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 // fuzzer::Dictionary
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_DICTIONARY_H
12 #define LLVM_FUZZER_DICTIONARY_H
14 #include "FuzzerDefs.h"
15 #include "FuzzerIO.h"
16 #include "FuzzerUtil.h"
17 #include <algorithm>
18 #include <limits>
20 namespace fuzzer {
21 // A simple POD sized array of bytes.
22 template <size_t kMaxSizeT> class FixedWord {
23 public:
24 static const size_t kMaxSize = kMaxSizeT;
25 FixedWord() {}
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);
30 memcpy(Data, B, S);
31 Size = S;
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; }
42 private:
43 uint8_t Size = 0;
44 uint8_t Data[kMaxSize];
47 typedef FixedWord<64> Word;
49 class DictionaryEntry {
50 public:
51 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());
59 return PositionHint;
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);
73 private:
74 Word W;
75 size_t PositionHint = std::numeric_limits<size_t>::max();
76 size_t UseCount = 0;
77 size_t SuccessCount = 0;
80 class Dictionary {
81 public:
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;
87 });
89 const DictionaryEntry *begin() const { return &DE[0]; }
90 const DictionaryEntry *end() const { return begin() + Size; }
91 DictionaryEntry & operator[] (size_t Idx) {
92 assert(Idx < Size);
93 return DE[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; }
103 private:
104 DictionaryEntry DE[kMaxDictSize];
105 size_t Size = 0;
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