FuzzerDictionary.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, uint8_t S) { Set(B, S); }
  24. void Set(const uint8_t *B, uint8_t S) {
  25. assert(S <= kMaxSize);
  26. memcpy(Data, B, S);
  27. Size = S;
  28. }
  29. bool operator==(const FixedWord<kMaxSize> &w) const {
  30. return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
  31. }
  32. static size_t GetMaxSize() { return kMaxSize; }
  33. const uint8_t *data() const { return Data; }
  34. uint8_t size() const { return Size; }
  35. private:
  36. uint8_t Size = 0;
  37. uint8_t Data[kMaxSize];
  38. };
  39. typedef FixedWord<64> Word;
  40. class DictionaryEntry {
  41. public:
  42. DictionaryEntry() {}
  43. DictionaryEntry(Word W) : W(W) {}
  44. DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
  45. const Word &GetW() const { return W; }
  46. bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
  47. size_t GetPositionHint() const {
  48. assert(HasPositionHint());
  49. return PositionHint;
  50. }
  51. void IncUseCount() { UseCount++; }
  52. void IncSuccessCount() { SuccessCount++; }
  53. size_t GetUseCount() const { return UseCount; }
  54. size_t GetSuccessCount() const {return SuccessCount; }
  55. void Print(const char *PrintAfter = "\n") {
  56. PrintASCII(W.data(), W.size());
  57. if (HasPositionHint())
  58. Printf("@%zd", GetPositionHint());
  59. Printf("%s", PrintAfter);
  60. }
  61. private:
  62. Word W;
  63. size_t PositionHint = std::numeric_limits<size_t>::max();
  64. size_t UseCount = 0;
  65. size_t SuccessCount = 0;
  66. };
  67. class Dictionary {
  68. public:
  69. static const size_t kMaxDictSize = 1 << 14;
  70. bool ContainsWord(const Word &W) const {
  71. return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
  72. return DE.GetW() == W;
  73. });
  74. }
  75. const DictionaryEntry *begin() const { return &DE[0]; }
  76. const DictionaryEntry *end() const { return begin() + Size; }
  77. DictionaryEntry & operator[] (size_t Idx) {
  78. assert(Idx < Size);
  79. return DE[Idx];
  80. }
  81. void push_back(DictionaryEntry DE) {
  82. if (Size < kMaxDictSize)
  83. this->DE[Size++] = DE;
  84. }
  85. void clear() { Size = 0; }
  86. bool empty() const { return Size == 0; }
  87. size_t size() const { return Size; }
  88. private:
  89. DictionaryEntry DE[kMaxDictSize];
  90. size_t Size = 0;
  91. };
  92. // Parses one dictionary entry.
  93. // If successful, write the enty to Unit and returns true,
  94. // otherwise returns false.
  95. bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
  96. // Parses the dictionary file, fills Units, returns true iff all lines
  97. // were parsed successfully.
  98. bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units);
  99. } // namespace fuzzer
  100. #endif // LLVM_FUZZER_DICTIONARY_H