FuzzerMutate.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===- FuzzerMutate.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::MutationDispatcher
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_MUTATE_H
  11. #define LLVM_FUZZER_MUTATE_H
  12. #include "FuzzerDefs.h"
  13. #include "FuzzerDictionary.h"
  14. #include "FuzzerOptions.h"
  15. #include "FuzzerRandom.h"
  16. namespace fuzzer {
  17. class MutationDispatcher {
  18. public:
  19. MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
  20. ~MutationDispatcher() {}
  21. /// Indicate that we are about to start a new sequence of mutations.
  22. void StartMutationSequence();
  23. /// Print the current sequence of mutations. Only prints the full sequence
  24. /// when Verbose is true.
  25. void PrintMutationSequence(bool Verbose = true);
  26. /// Return the current sequence of mutations.
  27. std::string MutationSequence();
  28. /// Indicate that the current sequence of mutations was successful.
  29. void RecordSuccessfulMutationSequence();
  30. /// Mutates data by invoking user-provided mutator.
  31. size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
  32. /// Mutates data by invoking user-provided crossover.
  33. size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
  34. /// Mutates data by shuffling bytes.
  35. size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
  36. /// Mutates data by erasing bytes.
  37. size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize);
  38. /// Mutates data by inserting a byte.
  39. size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
  40. /// Mutates data by inserting several repeated bytes.
  41. size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize);
  42. /// Mutates data by changing one byte.
  43. size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
  44. /// Mutates data by changing one bit.
  45. size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
  46. /// Mutates data by copying/inserting a part of data into a different place.
  47. size_t Mutate_CopyPart(uint8_t *Data, size_t Size, size_t MaxSize);
  48. /// Mutates data by adding a word from the manual dictionary.
  49. size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
  50. size_t MaxSize);
  51. /// Mutates data by adding a word from the TORC.
  52. size_t Mutate_AddWordFromTORC(uint8_t *Data, size_t Size, size_t MaxSize);
  53. /// Mutates data by adding a word from the persistent automatic dictionary.
  54. size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
  55. size_t MaxSize);
  56. /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
  57. size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
  58. /// Change a 1-, 2-, 4-, or 8-byte integer in interesting ways.
  59. size_t Mutate_ChangeBinaryInteger(uint8_t *Data, size_t Size, size_t MaxSize);
  60. /// CrossOver Data with CrossOverWith.
  61. size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
  62. /// Applies one of the configured mutations.
  63. /// Returns the new size of data which could be up to MaxSize.
  64. size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
  65. /// Applies one of the configured mutations to the bytes of Data
  66. /// that have '1' in Mask.
  67. /// Mask.size() should be >= Size.
  68. size_t MutateWithMask(uint8_t *Data, size_t Size, size_t MaxSize,
  69. const std::vector<uint8_t> &Mask);
  70. /// Applies one of the default mutations. Provided as a service
  71. /// to mutation authors.
  72. size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
  73. /// Creates a cross-over of two pieces of Data, returns its size.
  74. size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
  75. size_t Size2, uint8_t *Out, size_t MaxOutSize);
  76. void AddWordToManualDictionary(const Word &W);
  77. void PrintRecommendedDictionary();
  78. void SetCrossOverWith(const Unit *U) { CrossOverWith = U; }
  79. Random &GetRand() { return Rand; }
  80. private:
  81. struct Mutator {
  82. size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
  83. const char *Name;
  84. };
  85. size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
  86. size_t MaxSize);
  87. size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
  88. std::vector<Mutator> &Mutators);
  89. size_t InsertPartOf(const uint8_t *From, size_t FromSize, uint8_t *To,
  90. size_t ToSize, size_t MaxToSize);
  91. size_t CopyPartOf(const uint8_t *From, size_t FromSize, uint8_t *To,
  92. size_t ToSize);
  93. size_t ApplyDictionaryEntry(uint8_t *Data, size_t Size, size_t MaxSize,
  94. DictionaryEntry &DE);
  95. template <class T>
  96. DictionaryEntry MakeDictionaryEntryFromCMP(T Arg1, T Arg2,
  97. const uint8_t *Data, size_t Size);
  98. DictionaryEntry MakeDictionaryEntryFromCMP(const Word &Arg1, const Word &Arg2,
  99. const uint8_t *Data, size_t Size);
  100. DictionaryEntry MakeDictionaryEntryFromCMP(const void *Arg1, const void *Arg2,
  101. const void *Arg1Mutation,
  102. const void *Arg2Mutation,
  103. size_t ArgSize,
  104. const uint8_t *Data, size_t Size);
  105. Random &Rand;
  106. const FuzzingOptions Options;
  107. // Dictionary provided by the user via -dict=DICT_FILE.
  108. Dictionary ManualDictionary;
  109. // Persistent dictionary modified by the fuzzer, consists of
  110. // entries that led to successful discoveries in the past mutations.
  111. Dictionary PersistentAutoDictionary;
  112. std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
  113. static const size_t kCmpDictionaryEntriesDequeSize = 16;
  114. DictionaryEntry CmpDictionaryEntriesDeque[kCmpDictionaryEntriesDequeSize];
  115. size_t CmpDictionaryEntriesDequeIdx = 0;
  116. const Unit *CrossOverWith = nullptr;
  117. std::vector<uint8_t> MutateInPlaceHere;
  118. std::vector<uint8_t> MutateWithMaskTemp;
  119. // CustomCrossOver needs its own buffer as a custom implementation may call
  120. // LLVMFuzzerMutate, which in turn may resize MutateInPlaceHere.
  121. std::vector<uint8_t> CustomCrossOverInPlaceHere;
  122. std::vector<Mutator> Mutators;
  123. std::vector<Mutator> DefaultMutators;
  124. std::vector<Mutator> CurrentMutatorSequence;
  125. };
  126. } // namespace fuzzer
  127. #endif // LLVM_FUZZER_MUTATE_H