blob: f5024b1aeeca78b8611d9cb877d67ab69e300934 [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 {
36 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
37 }
38
39 bool operator<(const FixedWord<kMaxSize> &w) const {
40 if (Size != w.Size)
41 return Size < w.Size;
42 return memcmp(Data, w.Data, Size) < 0;
43 }
44
45 static size_t GetMaxSize() { return kMaxSize; }
46 const uint8_t *data() const { return Data; }
47 uint8_t size() const { return Size; }
48
49private:
50 uint8_t Size = 0;
51 uint8_t Data[kMaxSize];
52};
53
Kostya Serebryany32c50042017-02-14 23:02:37 +000054typedef FixedWord<64> Word;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000055
56class DictionaryEntry {
57 public:
58 DictionaryEntry() {}
59 DictionaryEntry(Word W) : W(W) {}
60 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
61 const Word &GetW() const { return W; }
62
63 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
64 size_t GetPositionHint() const {
65 assert(HasPositionHint());
66 return PositionHint;
67 }
68 void IncUseCount() { UseCount++; }
69 void IncSuccessCount() { SuccessCount++; }
70 size_t GetUseCount() const { return UseCount; }
71 size_t GetSuccessCount() const {return SuccessCount; }
72
Kostya Serebryany3364f902016-10-25 02:04:43 +000073 void Print(const char *PrintAfter = "\n") {
74 PrintASCII(W.data(), W.size());
75 if (HasPositionHint())
76 Printf("@%zd", GetPositionHint());
77 Printf("%s", PrintAfter);
78 }
79
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000080private:
81 Word W;
82 size_t PositionHint = std::numeric_limits<size_t>::max();
83 size_t UseCount = 0;
84 size_t SuccessCount = 0;
85};
86
87class Dictionary {
88 public:
89 static const size_t kMaxDictSize = 1 << 14;
90
91 bool ContainsWord(const Word &W) const {
92 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
93 return DE.GetW() == W;
94 });
95 }
96 const DictionaryEntry *begin() const { return &DE[0]; }
97 const DictionaryEntry *end() const { return begin() + Size; }
98 DictionaryEntry & operator[] (size_t Idx) {
99 assert(Idx < Size);
100 return DE[Idx];
101 }
102 void push_back(DictionaryEntry DE) {
103 if (Size < kMaxDictSize)
104 this->DE[Size++] = DE;
105 }
106 void clear() { Size = 0; }
107 bool empty() const { return Size == 0; }
108 size_t size() const { return Size; }
109
110private:
111 DictionaryEntry DE[kMaxDictSize];
112 size_t Size = 0;
113};
114
115// Parses one dictionary entry.
116// If successfull, write the enty to Unit and returns true,
117// otherwise returns false.
118bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
119// Parses the dictionary file, fills Units, returns true iff all lines
120// were parsed succesfully.
121bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
122
123} // namespace fuzzer
124
125#endif // LLVM_FUZZER_DICTIONARY_H