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 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 17 | namespace fuzzer { |
| 18 | |
Kostya Serebryany | 2f13f22 | 2016-01-21 01:52:14 +0000 | [diff] [blame] | 19 | const size_t Dictionary::kMaxDictSize; |
| 20 | |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 21 | MutationDispatcher::MutationDispatcher(Random &Rand, |
| 22 | const FuzzingOptions &Options) |
| 23 | : Rand(Rand), Options(Options) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 24 | DefaultMutators.insert( |
| 25 | DefaultMutators.begin(), |
| 26 | { |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 27 | {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 28 | {&MutationDispatcher::Mutate_InsertByte, "InsertByte"}, |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 29 | {&MutationDispatcher::Mutate_InsertRepeatedBytes, |
| 30 | "InsertRepeatedBytes"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 31 | {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"}, |
| 32 | {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"}, |
| 33 | {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"}, |
| 34 | {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"}, |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 35 | {&MutationDispatcher::Mutate_CopyPart, "CopyPart"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 36 | {&MutationDispatcher::Mutate_CrossOver, "CrossOver"}, |
| 37 | {&MutationDispatcher::Mutate_AddWordFromManualDictionary, |
| 38 | "AddFromManualDict"}, |
| 39 | {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary, |
| 40 | "AddFromTempAutoDict"}, |
| 41 | {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary, |
| 42 | "AddFromPersAutoDict"}, |
| 43 | }); |
| 44 | |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 45 | if (EF->LLVMFuzzerCustomMutator) |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 46 | Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"}); |
| 47 | else |
| 48 | Mutators = DefaultMutators; |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 49 | |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 50 | if (EF->LLVMFuzzerCustomCrossOver) |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 51 | Mutators.push_back( |
| 52 | {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"}); |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 53 | } |
Kostya Serebryany | 2319496 | 2016-02-13 03:46:26 +0000 | [diff] [blame] | 54 | |
Kostya Serebryany | a399221 | 2016-02-13 03:00:53 +0000 | [diff] [blame] | 55 | static char RandCh(Random &Rand) { |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 56 | if (Rand.RandBool()) return Rand(256); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 57 | const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00"; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 58 | return Special[Rand(sizeof(Special) - 1)]; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 61 | size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size, |
| 62 | size_t MaxSize) { |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 63 | return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand()); |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 66 | size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size, |
| 67 | size_t MaxSize) { |
| 68 | if (!Corpus || Corpus->size() < 2 || Size == 0) |
| 69 | return 0; |
| 70 | size_t Idx = Rand(Corpus->size()); |
| 71 | const Unit &Other = (*Corpus)[Idx]; |
| 72 | if (Other.empty()) |
| 73 | return 0; |
| 74 | MutateInPlaceHere.resize(MaxSize); |
| 75 | auto &U = MutateInPlaceHere; |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 76 | size_t NewSize = EF->LLVMFuzzerCustomCrossOver( |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 77 | Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand()); |
| 78 | if (!NewSize) |
| 79 | return 0; |
| 80 | assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit"); |
| 81 | memcpy(Data, U.data(), NewSize); |
| 82 | return NewSize; |
| 83 | } |
| 84 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 85 | size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size, |
| 86 | size_t MaxSize) { |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 87 | assert(Size); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 88 | size_t ShuffleAmount = |
| 89 | Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size. |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 90 | size_t ShuffleStart = Rand(Size - ShuffleAmount); |
| 91 | assert(ShuffleStart + ShuffleAmount <= Size); |
| 92 | std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, |
| 93 | Rand); |
| 94 | return Size; |
| 95 | } |
| 96 | |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 97 | size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size, |
| 98 | size_t MaxSize) { |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 99 | assert(Size); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 100 | if (Size == 1) return 0; |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 101 | size_t N = Rand(Size / 2) + 1; |
| 102 | assert(N < Size); |
| 103 | size_t Idx = Rand(Size - N + 1); |
| 104 | // Erase Data[Idx:Idx+N]. |
| 105 | memmove(Data + Idx, Data + Idx + N, Size - Idx - N); |
| 106 | // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx); |
| 107 | return Size - N; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 110 | size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size, |
| 111 | size_t MaxSize) { |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 112 | if (Size == MaxSize) return 0; |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 113 | size_t Idx = Rand(Size + 1); |
| 114 | // Insert new value at Data[Idx]. |
| 115 | memmove(Data + Idx + 1, Data + Idx, Size - Idx); |
| 116 | Data[Idx] = RandCh(Rand); |
| 117 | return Size + 1; |
| 118 | } |
| 119 | |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 120 | size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data, |
| 121 | size_t Size, |
| 122 | size_t MaxSize) { |
| 123 | const size_t kMinBytesToInsert = 3; |
| 124 | if (Size + kMinBytesToInsert >= MaxSize) return 0; |
| 125 | size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128); |
| 126 | size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert; |
| 127 | assert(Size + N <= MaxSize && N); |
| 128 | size_t Idx = Rand(Size + 1); |
| 129 | // Insert new values at Data[Idx]. |
| 130 | memmove(Data + Idx + N, Data + Idx, Size - Idx); |
| 131 | uint8_t Byte = RandCh(Rand); |
| 132 | for (size_t i = 0; i < N; i++) |
| 133 | Data[Idx + i] = Byte; |
| 134 | return Size + N; |
| 135 | } |
| 136 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 137 | size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size, |
| 138 | size_t MaxSize) { |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 139 | size_t Idx = Rand(Size); |
| 140 | Data[Idx] = RandCh(Rand); |
| 141 | return Size; |
| 142 | } |
| 143 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 144 | size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size, |
| 145 | size_t MaxSize) { |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 146 | size_t Idx = Rand(Size); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 147 | Data[Idx] ^= 1 << Rand(8); |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 148 | return Size; |
| 149 | } |
| 150 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 151 | size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data, |
| 152 | size_t Size, |
| 153 | size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 154 | return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 157 | size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary( |
| 158 | uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 159 | return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary( |
| 163 | uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 164 | return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 167 | size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data, |
| 168 | size_t Size, size_t MaxSize) { |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 169 | if (D.empty()) return 0; |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 170 | DictionaryEntry &DE = D[Rand(D.size())]; |
| 171 | const Word &W = DE.GetW(); |
| 172 | bool UsePositionHint = DE.HasPositionHint() && |
| 173 | DE.GetPositionHint() + W.size() < Size && Rand.RandBool(); |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 174 | if (Rand.RandBool()) { // Insert W. |
| 175 | if (Size + W.size() > MaxSize) return 0; |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 176 | size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1); |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 177 | memmove(Data + Idx + W.size(), Data + Idx, Size - Idx); |
| 178 | memcpy(Data + Idx, W.data(), W.size()); |
| 179 | Size += W.size(); |
| 180 | } else { // Overwrite some bytes with W. |
| 181 | if (W.size() > Size) return 0; |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 182 | size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size()); |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 183 | memcpy(Data + Idx, W.data(), W.size()); |
Kostya Serebryany | 80eb76a | 2016-01-06 02:13:04 +0000 | [diff] [blame] | 184 | } |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 185 | DE.IncUseCount(); |
| 186 | CurrentDictionaryEntrySequence.push_back(&DE); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 187 | return Size; |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 190 | // Overwrites part of To[0,ToSize) with a part of From[0,FromSize). |
| 191 | // Returns ToSize. |
| 192 | size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize, |
| 193 | uint8_t *To, size_t ToSize) { |
| 194 | // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize). |
| 195 | size_t ToBeg = Rand(ToSize); |
| 196 | size_t CopySize = Rand(ToSize - ToBeg) + 1; |
| 197 | assert(ToBeg + CopySize <= ToSize); |
| 198 | CopySize = std::min(CopySize, FromSize); |
| 199 | size_t FromBeg = Rand(FromSize - CopySize + 1); |
| 200 | assert(FromBeg + CopySize <= FromSize); |
| 201 | memmove(To + ToBeg, From + FromBeg, CopySize); |
| 202 | return ToSize; |
| 203 | } |
| 204 | |
| 205 | // Inserts part of From[0,ToSize) into To. |
| 206 | // Returns new size of To on success or 0 on failure. |
| 207 | size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize, |
| 208 | uint8_t *To, size_t ToSize, |
| 209 | size_t MaxToSize) { |
| 210 | if (ToSize >= MaxToSize) return 0; |
| 211 | size_t AvailableSpace = MaxToSize - ToSize; |
| 212 | size_t MaxCopySize = std::min(AvailableSpace, FromSize); |
| 213 | size_t CopySize = Rand(MaxCopySize) + 1; |
| 214 | size_t FromBeg = Rand(FromSize - CopySize + 1); |
| 215 | assert(FromBeg + CopySize <= FromSize); |
| 216 | size_t ToInsertPos = Rand(ToSize + 1); |
| 217 | assert(ToInsertPos + CopySize <= MaxToSize); |
| 218 | size_t TailSize = ToSize - ToInsertPos; |
| 219 | if (To == From) { |
| 220 | MutateInPlaceHere.resize(MaxToSize); |
| 221 | memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize); |
| 222 | memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize); |
| 223 | memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize); |
| 224 | } else { |
| 225 | memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize); |
| 226 | memmove(To + ToInsertPos, From + FromBeg, CopySize); |
| 227 | } |
| 228 | return ToSize + CopySize; |
| 229 | } |
| 230 | |
| 231 | size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size, |
| 232 | size_t MaxSize) { |
| 233 | if (Rand.RandBool()) |
| 234 | return CopyPartOf(Data, Size, Data, Size); |
| 235 | else |
| 236 | return InsertPartOf(Data, Size, Data, Size, MaxSize); |
| 237 | } |
| 238 | |
Kostya Serebryany | 25425ad | 2015-09-08 17:19:31 +0000 | [diff] [blame] | 239 | size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, |
| 240 | size_t MaxSize) { |
| 241 | size_t B = Rand(Size); |
| 242 | while (B < Size && !isdigit(Data[B])) B++; |
| 243 | if (B == Size) return 0; |
| 244 | size_t E = B; |
| 245 | while (E < Size && isdigit(Data[E])) E++; |
| 246 | assert(B < E); |
| 247 | // now we have digits in [B, E). |
| 248 | // strtol and friends don't accept non-zero-teminated data, parse it manually. |
| 249 | uint64_t Val = Data[B] - '0'; |
| 250 | for (size_t i = B + 1; i < E; i++) |
| 251 | Val = Val * 10 + Data[i] - '0'; |
| 252 | |
| 253 | // Mutate the integer value. |
| 254 | switch(Rand(5)) { |
| 255 | case 0: Val++; break; |
| 256 | case 1: Val--; break; |
| 257 | case 2: Val /= 2; break; |
| 258 | case 3: Val *= 2; break; |
| 259 | case 4: Val = Rand(Val * Val); break; |
| 260 | default: assert(0); |
| 261 | } |
| 262 | // Just replace the bytes with the new ones, don't bother moving bytes. |
| 263 | for (size_t i = B; i < E; i++) { |
| 264 | size_t Idx = E + B - i - 1; |
| 265 | assert(Idx >= B && Idx < E); |
| 266 | Data[Idx] = (Val % 10) + '0'; |
| 267 | Val /= 10; |
| 268 | } |
| 269 | return Size; |
| 270 | } |
| 271 | |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 272 | size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size, |
| 273 | size_t MaxSize) { |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 274 | if (!Corpus || Corpus->size() < 2 || Size == 0) return 0; |
| 275 | size_t Idx = Rand(Corpus->size()); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 276 | const Unit &O = (*Corpus)[Idx]; |
| 277 | if (O.empty()) return 0; |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 278 | MutateInPlaceHere.resize(MaxSize); |
| 279 | auto &U = MutateInPlaceHere; |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame^] | 280 | size_t NewSize; |
| 281 | switch(Rand(3)) { |
| 282 | case 0: |
| 283 | NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size()); |
| 284 | break; |
| 285 | case 1: |
| 286 | NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize); |
| 287 | if (NewSize) |
| 288 | break; |
| 289 | // Fallthrough |
| 290 | case 2: |
| 291 | NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size()); |
| 292 | break; |
| 293 | default: assert(0); |
| 294 | } |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 295 | assert(NewSize > 0 && "CrossOver returned empty unit"); |
| 296 | assert(NewSize <= MaxSize && "CrossOver returned overisized unit"); |
| 297 | memcpy(Data, U.data(), NewSize); |
| 298 | return NewSize; |
| 299 | } |
| 300 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 301 | void MutationDispatcher::StartMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 302 | CurrentMutatorSequence.clear(); |
| 303 | CurrentDictionaryEntrySequence.clear(); |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 306 | // Copy successful dictionary entries to PersistentAutoDictionary. |
| 307 | void MutationDispatcher::RecordSuccessfulMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 308 | for (auto DE : CurrentDictionaryEntrySequence) { |
| 309 | // PersistentAutoDictionary.AddWithSuccessCountOne(DE); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 310 | DE->IncSuccessCount(); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 311 | // Linear search is fine here as this happens seldom. |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 312 | if (!PersistentAutoDictionary.ContainsWord(DE->GetW())) |
| 313 | PersistentAutoDictionary.push_back({DE->GetW(), 1}); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 314 | } |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void MutationDispatcher::PrintRecommendedDictionary() { |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 318 | std::vector<DictionaryEntry> V; |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 319 | for (auto &DE : PersistentAutoDictionary) |
| 320 | if (!ManualDictionary.ContainsWord(DE.GetW())) |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 321 | V.push_back(DE); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 322 | if (V.empty()) return; |
| 323 | Printf("###### Recommended dictionary. ######\n"); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 324 | for (auto &DE: V) { |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 325 | Printf("\""); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 326 | PrintASCII(DE.GetW(), "\""); |
| 327 | Printf(" # Uses: %zd\n", DE.GetUseCount()); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 328 | } |
| 329 | Printf("###### End of recommended dictionary. ######\n"); |
| 330 | } |
| 331 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 332 | void MutationDispatcher::PrintMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 333 | Printf("MS: %zd ", CurrentMutatorSequence.size()); |
| 334 | for (auto M : CurrentMutatorSequence) |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 335 | Printf("%s-", M.Name); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 336 | if (!CurrentDictionaryEntrySequence.empty()) { |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 337 | Printf(" DE: "); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 338 | for (auto DE : CurrentDictionaryEntrySequence) { |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 339 | Printf("\""); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 340 | PrintASCII(DE->GetW(), "\"-"); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 345 | size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 346 | return MutateImpl(Data, Size, MaxSize, Mutators); |
| 347 | } |
| 348 | |
| 349 | size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size, |
| 350 | size_t MaxSize) { |
| 351 | return MutateImpl(Data, Size, MaxSize, DefaultMutators); |
| 352 | } |
| 353 | |
| 354 | // Mutates Data in place, returns new size. |
| 355 | size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size, |
| 356 | size_t MaxSize, |
| 357 | const std::vector<Mutator> &Mutators) { |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 358 | assert(MaxSize > 0); |
| 359 | assert(Size <= MaxSize); |
| 360 | if (Size == 0) { |
| 361 | for (size_t i = 0; i < MaxSize; i++) |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 362 | Data[i] = RandCh(Rand); |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 363 | if (Options.OnlyASCII) |
| 364 | ToASCII(Data, MaxSize); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 365 | return MaxSize; |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 366 | } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 367 | assert(Size > 0); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 368 | // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize), |
| 369 | // in which case they will return 0. |
| 370 | // Try several times before returning un-mutated data. |
| 371 | for (int Iter = 0; Iter < 10; Iter++) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 372 | auto M = Mutators[Rand(Mutators.size())]; |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 373 | size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize); |
| 374 | if (NewSize) { |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 375 | if (Options.OnlyASCII) |
| 376 | ToASCII(Data, NewSize); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 377 | CurrentMutatorSequence.push_back(M); |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 378 | return NewSize; |
| 379 | } |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 380 | } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 381 | return Size; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 384 | void MutationDispatcher::AddWordToManualDictionary(const Word &W) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 385 | ManualDictionary.push_back( |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 386 | {W, std::numeric_limits<size_t>::max()}); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 389 | void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) { |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 390 | static const size_t kMaxAutoDictSize = 1 << 14; |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 391 | if (TempAutoDictionary.size() >= kMaxAutoDictSize) return; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 392 | TempAutoDictionary.push_back(DE); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 395 | void MutationDispatcher::ClearAutoDictionary() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 396 | TempAutoDictionary.clear(); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 399 | } // namespace fuzzer |