Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 1 | //===- FuzzerMutate.cpp - Mutate a test input -----------------------------===// |
| 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 | // Mutate a test input. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 12 | #include <cstring> |
| 13 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 14 | #include "FuzzerInternal.h" |
| 15 | |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 18 | namespace fuzzer { |
| 19 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 20 | struct Mutator { |
| 21 | size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max); |
| 22 | const char *Name; |
| 23 | }; |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 24 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 25 | struct DictionaryEntry { |
| 26 | Unit Word; |
| 27 | size_t PositionHint; |
| 28 | }; |
| 29 | |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 30 | struct MutationDispatcher::Impl { |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 31 | std::vector<DictionaryEntry> ManualDictionary; |
| 32 | std::vector<DictionaryEntry> AutoDictionary; |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 33 | std::vector<Mutator> Mutators; |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 34 | std::vector<Mutator> CurrentMutatorSequence; |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 35 | const std::vector<Unit> *Corpus = nullptr; |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 36 | FuzzerRandomBase &Rand; |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 37 | |
| 38 | void Add(Mutator M) { Mutators.push_back(M); } |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 39 | Impl(FuzzerRandomBase &Rand) : Rand(Rand) { |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 40 | Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"}); |
| 41 | Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"}); |
| 42 | Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"}); |
| 43 | Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"}); |
| 44 | Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"}); |
| 45 | Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"}); |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 46 | Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"}); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 47 | Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary, |
| 48 | "AddFromManualDict"}); |
| 49 | Add({&MutationDispatcher::Mutate_AddWordFromAutoDictionary, |
| 50 | "AddFromAutoDict"}); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 51 | } |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 52 | void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; } |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 53 | size_t AddWordFromDictionary(const std::vector<DictionaryEntry> &D, |
| 54 | uint8_t *Data, size_t Size, size_t MaxSize); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 57 | static char FlipRandomBit(char X, FuzzerRandomBase &Rand) { |
| 58 | int Bit = Rand(8); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 59 | char Mask = 1 << Bit; |
| 60 | char R; |
| 61 | if (X & (1 << Bit)) |
| 62 | R = X & ~Mask; |
| 63 | else |
| 64 | R = X | Mask; |
| 65 | assert(R != X); |
| 66 | return R; |
| 67 | } |
| 68 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 69 | static char RandCh(FuzzerRandomBase &Rand) { |
| 70 | if (Rand.RandBool()) return Rand(256); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 71 | const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~."; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 72 | return Special[Rand(sizeof(Special) - 1)]; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 75 | size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size, |
| 76 | size_t MaxSize) { |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 77 | assert(Size); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 78 | size_t ShuffleAmount = |
| 79 | Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size. |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 80 | size_t ShuffleStart = Rand(Size - ShuffleAmount); |
| 81 | assert(ShuffleStart + ShuffleAmount <= Size); |
| 82 | std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, |
| 83 | Rand); |
| 84 | return Size; |
| 85 | } |
| 86 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 87 | size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size, |
| 88 | size_t MaxSize) { |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 89 | assert(Size); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 90 | if (Size == 1) return 0; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 91 | size_t Idx = Rand(Size); |
| 92 | // Erase Data[Idx]. |
| 93 | memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1); |
| 94 | return Size - 1; |
| 95 | } |
| 96 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 97 | size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size, |
| 98 | size_t MaxSize) { |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 99 | if (Size == MaxSize) return 0; |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 100 | size_t Idx = Rand(Size + 1); |
| 101 | // Insert new value at Data[Idx]. |
| 102 | memmove(Data + Idx + 1, Data + Idx, Size - Idx); |
| 103 | Data[Idx] = RandCh(Rand); |
| 104 | return Size + 1; |
| 105 | } |
| 106 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 107 | size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size, |
| 108 | size_t MaxSize) { |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 109 | size_t Idx = Rand(Size); |
| 110 | Data[Idx] = RandCh(Rand); |
| 111 | return Size; |
| 112 | } |
| 113 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 114 | size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size, |
| 115 | size_t MaxSize) { |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 116 | size_t Idx = Rand(Size); |
| 117 | Data[Idx] = FlipRandomBit(Data[Idx], Rand); |
| 118 | return Size; |
| 119 | } |
| 120 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 121 | size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data, |
| 122 | size_t Size, |
| 123 | size_t MaxSize) { |
| 124 | return MDImpl->AddWordFromDictionary(MDImpl->ManualDictionary, Data, Size, |
| 125 | MaxSize); |
| 126 | } |
| 127 | |
| 128 | size_t MutationDispatcher::Mutate_AddWordFromAutoDictionary(uint8_t *Data, |
| 129 | size_t Size, |
| 130 | size_t MaxSize) { |
| 131 | return MDImpl->AddWordFromDictionary(MDImpl->AutoDictionary, Data, Size, |
| 132 | MaxSize); |
| 133 | } |
| 134 | |
| 135 | size_t MutationDispatcher::Impl::AddWordFromDictionary( |
| 136 | const std::vector<DictionaryEntry> &D, uint8_t *Data, size_t Size, |
| 137 | size_t MaxSize) { |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 138 | if (D.empty()) return 0; |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 139 | const DictionaryEntry &DE = D[Rand(D.size())]; |
| 140 | const Unit &Word = DE.Word; |
| 141 | size_t PositionHint = DE.PositionHint; |
| 142 | bool UsePositionHint = PositionHint != std::numeric_limits<size_t>::max() && |
| 143 | PositionHint + Word.size() < Size && Rand.RandBool(); |
Kostya Serebryany | 80eb76a | 2016-01-06 02:13:04 +0000 | [diff] [blame] | 144 | if (Rand.RandBool()) { // Insert Word. |
| 145 | if (Size + Word.size() > MaxSize) return 0; |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 146 | size_t Idx = UsePositionHint ? PositionHint : Rand(Size + 1); |
Kostya Serebryany | 80eb76a | 2016-01-06 02:13:04 +0000 | [diff] [blame] | 147 | memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx); |
| 148 | memcpy(Data + Idx, Word.data(), Word.size()); |
| 149 | return Size + Word.size(); |
| 150 | } else { // Overwrite some bytes with Word. |
| 151 | if (Word.size() > Size) return 0; |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 152 | size_t Idx = UsePositionHint ? PositionHint : Rand(Size - Word.size()); |
Kostya Serebryany | 80eb76a | 2016-01-06 02:13:04 +0000 | [diff] [blame] | 153 | memcpy(Data + Idx, Word.data(), Word.size()); |
| 154 | return Size; |
| 155 | } |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Kostya Serebryany | 25425ad | 2015-09-08 17:19:31 +0000 | [diff] [blame] | 158 | size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, |
| 159 | size_t MaxSize) { |
| 160 | size_t B = Rand(Size); |
| 161 | while (B < Size && !isdigit(Data[B])) B++; |
| 162 | if (B == Size) return 0; |
| 163 | size_t E = B; |
| 164 | while (E < Size && isdigit(Data[E])) E++; |
| 165 | assert(B < E); |
| 166 | // now we have digits in [B, E). |
| 167 | // strtol and friends don't accept non-zero-teminated data, parse it manually. |
| 168 | uint64_t Val = Data[B] - '0'; |
| 169 | for (size_t i = B + 1; i < E; i++) |
| 170 | Val = Val * 10 + Data[i] - '0'; |
| 171 | |
| 172 | // Mutate the integer value. |
| 173 | switch(Rand(5)) { |
| 174 | case 0: Val++; break; |
| 175 | case 1: Val--; break; |
| 176 | case 2: Val /= 2; break; |
| 177 | case 3: Val *= 2; break; |
| 178 | case 4: Val = Rand(Val * Val); break; |
| 179 | default: assert(0); |
| 180 | } |
| 181 | // Just replace the bytes with the new ones, don't bother moving bytes. |
| 182 | for (size_t i = B; i < E; i++) { |
| 183 | size_t Idx = E + B - i - 1; |
| 184 | assert(Idx >= B && Idx < E); |
| 185 | Data[Idx] = (Val % 10) + '0'; |
| 186 | Val /= 10; |
| 187 | } |
| 188 | return Size; |
| 189 | } |
| 190 | |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 191 | size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size, |
| 192 | size_t MaxSize) { |
| 193 | auto Corpus = MDImpl->Corpus; |
| 194 | if (!Corpus || Corpus->size() < 2 || Size == 0) return 0; |
| 195 | size_t Idx = Rand(Corpus->size()); |
| 196 | const Unit &Other = (*Corpus)[Idx]; |
| 197 | if (Other.empty()) return 0; |
| 198 | Unit U(MaxSize); |
| 199 | size_t NewSize = |
| 200 | CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size()); |
| 201 | assert(NewSize > 0 && "CrossOver returned empty unit"); |
| 202 | assert(NewSize <= MaxSize && "CrossOver returned overisized unit"); |
| 203 | memcpy(Data, U.data(), NewSize); |
| 204 | return NewSize; |
| 205 | } |
| 206 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 207 | void MutationDispatcher::StartMutationSequence() { |
| 208 | MDImpl->CurrentMutatorSequence.clear(); |
| 209 | } |
| 210 | |
| 211 | void MutationDispatcher::PrintMutationSequence() { |
| 212 | Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size()); |
| 213 | for (auto M : MDImpl->CurrentMutatorSequence) |
| 214 | Printf("%s-", M.Name); |
| 215 | } |
| 216 | |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 217 | // Mutates Data in place, returns new size. |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 218 | size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 219 | assert(MaxSize > 0); |
| 220 | assert(Size <= MaxSize); |
| 221 | if (Size == 0) { |
| 222 | for (size_t i = 0; i < MaxSize; i++) |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 223 | Data[i] = RandCh(Rand); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 224 | return MaxSize; |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 225 | } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 226 | assert(Size > 0); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 227 | // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize), |
| 228 | // in which case they will return 0. |
| 229 | // Try several times before returning un-mutated data. |
| 230 | for (int Iter = 0; Iter < 10; Iter++) { |
| 231 | size_t MutatorIdx = Rand(MDImpl->Mutators.size()); |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 232 | auto M = MDImpl->Mutators[MutatorIdx]; |
| 233 | size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize); |
| 234 | if (NewSize) { |
| 235 | MDImpl->CurrentMutatorSequence.push_back(M); |
| 236 | return NewSize; |
| 237 | } |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 238 | } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 239 | return Size; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 242 | void MutationDispatcher::SetCorpus(const std::vector<Unit> *Corpus) { |
| 243 | MDImpl->SetCorpus(Corpus); |
| 244 | } |
| 245 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 246 | void MutationDispatcher::AddWordToManualDictionary(const Unit &Word) { |
| 247 | MDImpl->ManualDictionary.push_back( |
| 248 | {Word, std::numeric_limits<size_t>::max()}); |
| 249 | } |
| 250 | |
| 251 | void MutationDispatcher::AddWordToAutoDictionary(const Unit &Word, |
| 252 | size_t PositionHint) { |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame^] | 253 | static const size_t kMaxAutoDictSize = 1 << 14; |
| 254 | if (MDImpl->AutoDictionary.size() >= kMaxAutoDictSize) return; |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 255 | MDImpl->AutoDictionary.push_back({Word, PositionHint}); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame^] | 258 | void MutationDispatcher::ClearAutoDictionary() { |
| 259 | MDImpl->AutoDictionary.clear(); |
| 260 | } |
| 261 | |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 262 | MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) { |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 263 | MDImpl = new Impl(Rand); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | MutationDispatcher::~MutationDispatcher() { delete MDImpl; } |
| 267 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 268 | } // namespace fuzzer |