LLVM  5.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 kMaxSizeT> class FixedWord {
24 public:
25  static const size_t kMaxSize = kMaxSizeT;
26  FixedWord() {}
27  FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
28 
29  void Set(const uint8_t *B, uint8_t S) {
30  assert(S <= kMaxSize);
31  memcpy(Data, B, S);
32  Size = S;
33  }
34 
35  bool operator==(const FixedWord<kMaxSize> &w) const {
36  ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
37  return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
38  }
39 
40  bool operator<(const FixedWord<kMaxSize> &w) const {
41  ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
42  if (Size != w.Size)
43  return Size < w.Size;
44  return memcmp(Data, w.Data, Size) < 0;
45  }
46 
47  static size_t GetMaxSize() { return kMaxSize; }
48  const uint8_t *data() const { return Data; }
49  uint8_t size() const { return Size; }
50 
51 private:
52  uint8_t Size = 0;
53  uint8_t Data[kMaxSize];
54 };
55 
57 
59  public:
61  DictionaryEntry(Word W) : W(W) {}
62  DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
63  const Word &GetW() const { return W; }
64 
65  bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
66  size_t GetPositionHint() const {
68  return PositionHint;
69  }
70  void IncUseCount() { UseCount++; }
71  void IncSuccessCount() { SuccessCount++; }
72  size_t GetUseCount() const { return UseCount; }
73  size_t GetSuccessCount() const {return SuccessCount; }
74 
75  void Print(const char *PrintAfter = "\n") {
76  PrintASCII(W.data(), W.size());
77  if (HasPositionHint())
78  Printf("@%zd", GetPositionHint());
79  Printf("%s", PrintAfter);
80  }
81 
82 private:
83  Word W;
84  size_t PositionHint = std::numeric_limits<size_t>::max();
85  size_t UseCount = 0;
86  size_t SuccessCount = 0;
87 };
88 
89 class Dictionary {
90  public:
91  static const size_t kMaxDictSize = 1 << 14;
92 
93  bool ContainsWord(const Word &W) const {
94  return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
95  return DE.GetW() == W;
96  });
97  }
98  const DictionaryEntry *begin() const { return &DE[0]; }
99  const DictionaryEntry *end() const { return begin() + Size; }
100  DictionaryEntry & operator[] (size_t Idx) {
101  assert(Idx < Size);
102  return DE[Idx];
103  }
105  if (Size < kMaxDictSize)
106  this->DE[Size++] = DE;
107  }
108  void clear() { Size = 0; }
109  bool empty() const { return Size == 0; }
110  size_t size() const { return Size; }
111 
112 private:
114  size_t Size = 0;
115 };
116 
117 // Parses one dictionary entry.
118 // If successfull, write the enty to Unit and returns true,
119 // otherwise returns false.
120 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
121 // Parses the dictionary file, fills Units, returns true iff all lines
122 // were parsed succesfully.
123 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
124 
125 } // namespace fuzzer
126 
127 #endif // LLVM_FUZZER_DICTIONARY_H
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
size_t size() const
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U)
Definition: FuzzerUtil.cpp:81
const DictionaryEntry * end() const
FixedWord< 64 > Word
size_t GetSuccessCount() const
uint8_t size() const
const uint8_t * data() const
static PassOptionList PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), cl::Hidden)
bool operator==(const FixedWord< kMaxSize > &w) const
DictionaryEntry(Word W, size_t PositionHint)
bool HasPositionHint() const
PrintASCII(Dict[i].data(), Dict[i].size(),"\"")
bool ParseDictionaryFile(const std::string &Text, std::vector< Unit > *Units)
Definition: FuzzerUtil.cpp:127
void Set(const uint8_t *B, uint8_t S)
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:825
DictionaryEntry & operator[](size_t Idx)
void push_back(DictionaryEntry DE)
const DictionaryEntry * begin() const
#define B
Definition: LargeTest.cpp:24
bool ContainsWord(const Word &W) const
size_t GetUseCount() const
static const size_t kMaxSize
size_t GetPositionHint() const
static const size_t kMaxDictSize
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
FixedWord(const uint8_t *B, uint8_t S)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])
const Word & GetW() const
void Print(const char *PrintAfter="\n")
static size_t GetMaxSize()