Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 24a148b | 2016-11-30 19:06:14 +0000 | [diff] [blame] | 15 | #include "FuzzerDefs.h" |
| 16 | #include "FuzzerIO.h" |
| 17 | #include "FuzzerUtil.h" |
Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | #include <limits> |
| 20 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 21 | namespace fuzzer { |
| 22 | // A simple POD sized array of bytes. |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 23 | template <size_t kMaxSizeT> class FixedWord { |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 24 | public: |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 25 | static const size_t kMaxSize = kMaxSizeT; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 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 | 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 | |
| 49 | private: |
| 50 | uint8_t Size = 0; |
| 51 | uint8_t Data[kMaxSize]; |
| 52 | }; |
| 53 | |
Kostya Serebryany | 32c5004 | 2017-02-14 23:02:37 +0000 | [diff] [blame^] | 54 | typedef FixedWord<64> Word; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 55 | |
| 56 | class 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 Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 73 | 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 Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 80 | private: |
| 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 | |
| 87 | class 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 | |
| 110 | private: |
| 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. |
| 118 | bool ParseOneDictionaryEntry(const std::string &Str, Unit *U); |
| 119 | // Parses the dictionary file, fills Units, returns true iff all lines |
| 120 | // were parsed succesfully. |
| 121 | bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units); |
| 122 | |
| 123 | } // namespace fuzzer |
| 124 | |
| 125 | #endif // LLVM_FUZZER_DICTIONARY_H |