blob: 84cee87b8971f4ad7b3c92f0d8fc940c3436b046 [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50:50 +00001//===- 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
Zachary Turner24a148b2016-11-30 19:06:14 +000015#include "FuzzerDefs.h"
16#include "FuzzerIO.h"
17#include "FuzzerUtil.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000018#include <algorithm>
19#include <limits>
20
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000021namespace fuzzer {
22// A simple POD sized array of bytes.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000023template <size_t kMaxSizeT> class FixedWord {
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000024public:
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000025 static const size_t kMaxSize = kMaxSizeT;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000026 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 {
Kostya Serebryanyaf2dfce2017-03-31 02:21:28 +000036 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000037 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
38 }
39
40 bool operator<(const FixedWord<kMaxSize> &w) const {
Kostya Serebryanyaf2dfce2017-03-31 02:21:28 +000041 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000042 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
51private:
52 uint8_t Size = 0;
53 uint8_t Data[kMaxSize];
54};
55
Kostya Serebryany32c50042017-02-14 23:02:37 +000056typedef FixedWord<64> Word;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000057
58class DictionaryEntry {
59 public:
60 DictionaryEntry() {}
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 {
67 assert(HasPositionHint());
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
Kostya Serebryany3364f902016-10-25 02:04:43 +000075 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
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000082private:
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
89class 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 }
104 void push_back(DictionaryEntry DE) {
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
112private:
113 DictionaryEntry DE[kMaxDictSize];
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.
120bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
121// Parses the dictionary file, fills Units, returns true iff all lines
122// were parsed succesfully.
123bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
124
125} // namespace fuzzer
126
127#endif // LLVM_FUZZER_DICTIONARY_H