LLVM  4.0.0
FuzzerDictionary.h
Go to the documentation of this file.
1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // fuzzer::Dictionary
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_FUZZER_DICTIONARY_H
13 #define LLVM_FUZZER_DICTIONARY_H
14 
15 #include "FuzzerDefs.h"
16 #include "FuzzerIO.h"
17 #include "FuzzerUtil.h"
18 #include <algorithm>
19 #include <limits>
20 
21 namespace fuzzer {
22 // A simple POD sized array of bytes.
23 template <size_t kMaxSize> class FixedWord {
24 public:
25  FixedWord() {}
26  FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
27 
28  void Set(const uint8_t *B, uint8_t S) {
29  assert(S <= kMaxSize);
30  memcpy(Data, B, S);
31  Size = S;
32  }
33 
34  bool operator==(const FixedWord<kMaxSize> &w) const {
35  return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
36  }
37 
38  bool operator<(const FixedWord<kMaxSize> &w) const {
39  if (Size != w.Size)
40  return Size < w.Size;
41  return memcmp(Data, w.Data, Size) < 0;
42  }
43 
44  static size_t GetMaxSize() { return kMaxSize; }
45  const uint8_t *data() const { return Data; }
46  uint8_t size() const { return Size; }
47 
48 private:
49  uint8_t Size = 0;
50  uint8_t Data[kMaxSize];
51 };
52 
53 typedef FixedWord<27> Word; // 28 bytes.
54 
56  public:
58  DictionaryEntry(Word W) : W(W) {}
59  DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
60  const Word &GetW() const { return W; }
61 
62  bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
63  size_t GetPositionHint() const {
65  return PositionHint;
66  }
67  void IncUseCount() { UseCount++; }
68  void IncSuccessCount() { SuccessCount++; }
69  size_t GetUseCount() const { return UseCount; }
70  size_t GetSuccessCount() const {return SuccessCount; }
71 
72  void Print(const char *PrintAfter = "\n") {
73  PrintASCII(W.data(), W.size());
74  if (HasPositionHint())
75  Printf("@%zd", GetPositionHint());
76  Printf("%s", PrintAfter);
77  }
78 
79 private:
80  Word W;
81  size_t PositionHint = std::numeric_limits<size_t>::max();
82  size_t UseCount = 0;
83  size_t SuccessCount = 0;
84 };
85 
86 class Dictionary {
87  public:
88  static const size_t kMaxDictSize = 1 << 14;
89 
90  bool ContainsWord(const Word &W) const {
91  return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
92  return DE.GetW() == W;
93  });
94  }
95  const DictionaryEntry *begin() const { return &DE[0]; }
96  const DictionaryEntry *end() const { return begin() + Size; }
97  DictionaryEntry & operator[] (size_t Idx) {
98  assert(Idx < Size);
99  return DE[Idx];
100  }
102  if (Size < kMaxDictSize)
103  this->DE[Size++] = DE;
104  }
105  void clear() { Size = 0; }
106  bool empty() const { return Size == 0; }
107  size_t size() const { return Size; }
108 
109 private:
111  size_t Size = 0;
112 };
113 
114 // Parses one dictionary entry.
115 // If successfull, write the enty to Unit and returns true,
116 // otherwise returns false.
117 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
118 // Parses the dictionary file, fills Units, returns true iff all lines
119 // were parsed succesfully.
120 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
121 
122 } // namespace fuzzer
123 
124 #endif // LLVM_FUZZER_DICTIONARY_H
const uint8_t * data() const
size_t size() const
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U)
Definition: FuzzerUtil.cpp:81
const DictionaryEntry * end() const
uint8_t size() const
size_t GetSuccessCount() const
FixedWord< 27 > Word
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void Printf(const char *Fmt,...)
Definition: FuzzerIO.cpp:109
static PassOptionList PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), cl::Hidden)
DictionaryEntry(Word W, size_t PositionHint)
bool HasPositionHint() const
bool ParseDictionaryFile(const std::string &Text, std::vector< Unit > *Units)
Definition: FuzzerUtil.cpp:127
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:743
DictionaryEntry & operator[](size_t Idx)
void push_back(DictionaryEntry DE)
void Set(const uint8_t *B, uint8_t S)
static size_t GetMaxSize()
const DictionaryEntry * begin() const
bool ContainsWord(const Word &W) const
FixedWord(const uint8_t *B, uint8_t S)
size_t GetUseCount() const
size_t GetPositionHint() const
static const size_t kMaxDictSize
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:71
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator==(const FixedWord< kMaxSize > &w) const
const Word & GetW() const
void Print(const char *PrintAfter="\n")
static void PrintASCII(const Word &W, const char *PrintAfter)