FuzzerDictionary.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // fuzzer::Dictionary
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_DICTIONARY_H
  11. #define LLVM_FUZZER_DICTIONARY_H
  12. #include "FuzzerDefs.h"
  13. #include "FuzzerIO.h"
  14. #include "FuzzerUtil.h"
  15. #include <algorithm>
  16. #include <limits>
  17. namespace fuzzer {
  18. // A simple POD sized array of bytes.
  19. template <size_t kMaxSizeT> class FixedWord {
  20. public:
  21. static const size_t kMaxSize = kMaxSizeT;
  22. FixedWord() {}
  23. FixedWord(const uint8_t *B, size_t S) { Set(B, S); }
  24. void Set(const uint8_t *B, size_t S) {
  25. static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
  26. "FixedWord::kMaxSizeT cannot fit in a uint8_t.");
  27. assert(S <= kMaxSize);
  28. memcpy(Data, B, S);
  29. Size = static_cast<uint8_t>(S);
  30. }
  31. bool operator==(const FixedWord<kMaxSize> &w) const {
  32. return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
  33. }
  34. static size_t GetMaxSize() { return kMaxSize; }
  35. const uint8_t *data() const { return Data; }
  36. uint8_t size() const { return Size; }
  37. private:
  38. uint8_t Size = 0;
  39. uint8_t Data[kMaxSize];
  40. };
  41. typedef FixedWord<64> Word;
  42. class DictionaryEntry {
  43. public:
  44. DictionaryEntry() {}
  45. DictionaryEntry(Word W) : W(W) {}
  46. DictionaryEntry(Word W, size_t PositionHint)
  47. : W(W), PositionHint(PositionHint) {}
  48. const Word &GetW() const { return W; }
  49. bool HasPositionHint() const {
  50. return PositionHint != std::numeric_limits<size_t>::max();
  51. }
  52. size_t GetPositionHint() const {
  53. assert(HasPositionHint());
  54. return PositionHint;
  55. }
  56. void IncUseCount() { UseCount++; }
  57. void IncSuccessCount() { SuccessCount++; }
  58. size_t GetUseCount() const { return UseCount; }
  59. size_t GetSuccessCount() const {return SuccessCount; }
  60. void Print(const char *PrintAfter = "\n") {
  61. PrintASCII(W.data(), W.size());
  62. if (HasPositionHint())
  63. Printf("@%zd", GetPositionHint());
  64. Printf("%s", PrintAfter);
  65. }
  66. private:
  67. Word W;
  68. size_t PositionHint = std::numeric_limits<size_t>::max();
  69. size_t UseCount = 0;
  70. size_t SuccessCount = 0;
  71. };
  72. class Dictionary {
  73. public:
  74. static const size_t kMaxDictSize = 1 << 14;
  75. bool ContainsWord(const Word &W) const {
  76. return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
  77. return DE.GetW() == W;
  78. });
  79. }
  80. const DictionaryEntry *begin() const { return &DE[0]; }
  81. const DictionaryEntry *end() const { return begin() + Size; }
  82. DictionaryEntry & operator[] (size_t Idx) {
  83. assert(Idx < Size);
  84. return DE[Idx];
  85. }
  86. void push_back(DictionaryEntry DE) {
  87. if (Size < kMaxDictSize)
  88. this->DE[Size++] = DE;
  89. }
  90. void clear() { Size = 0; }
  91. bool empty() const { return Size == 0; }
  92. size_t size() const { return Size; }
  93. private:
  94. DictionaryEntry DE[kMaxDictSize];
  95. size_t Size = 0;
  96. };
  97. // Parses one dictionary entry.
  98. // If successful, writes the entry to Unit and returns true,
  99. // otherwise returns false.
  100. bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
  101. // Parses the dictionary file, fills Units, returns true iff all lines
  102. // were parsed successfully.
  103. bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
  104. } // namespace fuzzer
  105. #endif // LLVM_FUZZER_DICTIONARY_H