| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 1 | //===- FuzzerMutate.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::MutationDispatcher |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_FUZZER_MUTATE_H |
| 13 | #define LLVM_FUZZER_MUTATE_H |
| 14 | |
| 15 | #include "FuzzerDefs.h" |
| 16 | #include "FuzzerDictionary.h" |
| 17 | #include "FuzzerRandom.h" |
| 18 | |
| 19 | namespace fuzzer { |
| 20 | |
| 21 | class MutationDispatcher { |
| 22 | public: |
| 23 | MutationDispatcher(Random &Rand, const FuzzingOptions &Options); |
| 24 | ~MutationDispatcher() {} |
| 25 | /// Indicate that we are about to start a new sequence of mutations. |
| 26 | void StartMutationSequence(); |
| 27 | /// Print the current sequence of mutations. |
| 28 | void PrintMutationSequence(); |
| 29 | /// Indicate that the current sequence of mutations was successfull. |
| 30 | void RecordSuccessfulMutationSequence(); |
| 31 | /// Mutates data by invoking user-provided mutator. |
| 32 | size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize); |
| 33 | /// Mutates data by invoking user-provided crossover. |
| 34 | size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize); |
| 35 | /// Mutates data by shuffling bytes. |
| 36 | size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize); |
| 37 | /// Mutates data by erasing bytes. |
| 38 | size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize); |
| 39 | /// Mutates data by inserting a byte. |
| 40 | size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize); |
| 41 | /// Mutates data by inserting several repeated bytes. |
| 42 | size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize); |
| 43 | /// Mutates data by chanding one byte. |
| 44 | size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize); |
| 45 | /// Mutates data by chanding one bit. |
| 46 | size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize); |
| 47 | /// Mutates data by copying/inserting a part of data into a different place. |
| 48 | size_t Mutate_CopyPart(uint8_t *Data, size_t Size, size_t MaxSize); |
| 49 | |
| 50 | /// Mutates data by adding a word from the manual dictionary. |
| 51 | size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size, |
| 52 | size_t MaxSize); |
| 53 | |
| 54 | /// Mutates data by adding a word from the temporary automatic dictionary. |
| 55 | size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size, |
| 56 | size_t MaxSize); |
| 57 | |
| Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame^] | 58 | /// Mutates data by adding a word from the trace-cmp dictionary. |
| 59 | size_t Mutate_AddWordFromTraceCmpDictionary(uint8_t *Data, size_t Size, |
| 60 | size_t MaxSize); |
| 61 | |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 62 | /// Mutates data by adding a word from the persistent automatic dictionary. |
| 63 | size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size, |
| 64 | size_t MaxSize); |
| 65 | |
| 66 | /// Tries to find an ASCII integer in Data, changes it to another ASCII int. |
| 67 | size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize); |
| 68 | /// Change a 1-, 2-, 4-, or 8-byte integer in interesting ways. |
| 69 | size_t Mutate_ChangeBinaryInteger(uint8_t *Data, size_t Size, size_t MaxSize); |
| 70 | |
| 71 | /// CrossOver Data with some other element of the corpus. |
| 72 | size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize); |
| 73 | |
| 74 | /// Applies one of the configured mutations. |
| 75 | /// Returns the new size of data which could be up to MaxSize. |
| 76 | size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize); |
| 77 | /// Applies one of the default mutations. Provided as a service |
| 78 | /// to mutation authors. |
| 79 | size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize); |
| 80 | |
| 81 | /// Creates a cross-over of two pieces of Data, returns its size. |
| 82 | size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2, |
| 83 | size_t Size2, uint8_t *Out, size_t MaxOutSize); |
| 84 | |
| 85 | void AddWordToManualDictionary(const Word &W); |
| 86 | |
| 87 | void AddWordToAutoDictionary(DictionaryEntry DE); |
| 88 | void ClearAutoDictionary(); |
| 89 | void PrintRecommendedDictionary(); |
| 90 | |
| 91 | void SetCorpus(const InputCorpus *Corpus) { this->Corpus = Corpus; } |
| 92 | |
| 93 | Random &GetRand() { return Rand; } |
| 94 | |
| Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame^] | 95 | Dictionary *GetTraceCmpDictionary() { return &TraceCmpDictionary; } |
| 96 | |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 97 | private: |
| 98 | |
| 99 | struct Mutator { |
| 100 | size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max); |
| 101 | const char *Name; |
| 102 | }; |
| 103 | |
| 104 | size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size, |
| 105 | size_t MaxSize); |
| 106 | size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize, |
| 107 | const std::vector<Mutator> &Mutators); |
| 108 | |
| 109 | size_t InsertPartOf(const uint8_t *From, size_t FromSize, uint8_t *To, |
| 110 | size_t ToSize, size_t MaxToSize); |
| 111 | size_t CopyPartOf(const uint8_t *From, size_t FromSize, uint8_t *To, |
| 112 | size_t ToSize); |
| 113 | |
| 114 | Random &Rand; |
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 115 | const FuzzingOptions &Options; |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 116 | |
| 117 | // Dictionary provided by the user via -dict=DICT_FILE. |
| 118 | Dictionary ManualDictionary; |
| 119 | // Temporary dictionary modified by the fuzzer itself, |
| 120 | // recreated periodically. |
| 121 | Dictionary TempAutoDictionary; |
| 122 | // Persistent dictionary modified by the fuzzer, consists of |
| 123 | // entries that led to successfull discoveries in the past mutations. |
| 124 | Dictionary PersistentAutoDictionary; |
| Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame^] | 125 | |
| 126 | // Dictionary from tracing CMP instructions. |
| 127 | Dictionary TraceCmpDictionary; |
| 128 | |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 129 | std::vector<Mutator> CurrentMutatorSequence; |
| 130 | std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence; |
| 131 | const InputCorpus *Corpus = nullptr; |
| 132 | std::vector<uint8_t> MutateInPlaceHere; |
| 133 | |
| 134 | std::vector<Mutator> Mutators; |
| 135 | std::vector<Mutator> DefaultMutators; |
| 136 | }; |
| 137 | |
| 138 | } // namespace fuzzer |
| 139 | |
| 140 | #endif // LLVM_FUZZER_MUTATE_H |