george.karpenkov | 29efa6d | 2017-08-21 23:25: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 | |
| 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 | |
| 56 | typedef FixedWord<64> Word; |
| 57 | |
| 58 | class 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 | |
| 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 | } |
| 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 | |
| 112 | private: |
| 113 | DictionaryEntry DE[kMaxDictSize]; |
| 114 | size_t Size = 0; |
| 115 | }; |
| 116 | |
| 117 | // Parses one dictionary entry. |
sylvestre | a9eb857 | 2018-03-13 14:35:10 +0000 | [diff] [blame^] | 118 | // If successful, write the enty to Unit and returns true, |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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 |
sylvestre | a9eb857 | 2018-03-13 14:35:10 +0000 | [diff] [blame^] | 122 | // were parsed successfully. |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 123 | bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 124 | |
| 125 | } // namespace fuzzer |
| 126 | |
| 127 | #endif // LLVM_FUZZER_DICTIONARY_H |