blob: d3eaac086c7bcdd110631e755dcfe8d32bb9d3c8 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- 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 Serebryanyf3424592015-05-22 22:35:31 +000012#include <cstring>
13
Aaron Ballmanef116982015-01-29 16:58:29 +000014#include "FuzzerInternal.h"
15
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000016
Aaron Ballmanef116982015-01-29 16:58:29 +000017namespace fuzzer {
18
Kostya Serebryany2f13f222016-01-21 01:52:14 +000019const size_t Dictionary::kMaxDictSize;
20
Mike Aizatskyf0b3e852016-06-23 20:44:48 +000021MutationDispatcher::MutationDispatcher(Random &Rand,
22 const FuzzingOptions &Options)
23 : Rand(Rand), Options(Options) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000024 DefaultMutators.insert(
25 DefaultMutators.begin(),
26 {
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000027 {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000028 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000029 {&MutationDispatcher::Mutate_InsertRepeatedBytes,
30 "InsertRepeatedBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000031 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
32 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
33 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
34 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000035 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000036 {&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 Liew1873a492016-06-07 23:32:50 +000045 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000046 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
47 else
48 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000049
Dan Liew1873a492016-06-07 23:32:50 +000050 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000051 Mutators.push_back(
52 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000053}
Kostya Serebryany23194962016-02-13 03:46:26 +000054
Kostya Serebryanya3992212016-02-13 03:00:53 +000055static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000056 if (Rand.RandBool()) return Rand(256);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000057 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000058 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000059}
60
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000061size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
62 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000063 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000064}
65
Mike Aizatsky41d66832016-06-07 20:22:15 +000066size_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 Liew1873a492016-06-07 23:32:50 +000076 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000077 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 Serebryanyec2dcb12015-09-03 21:24:19 +000085size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
86 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000087 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000088 size_t ShuffleAmount =
89 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000090 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 Serebryanydfbe59b2016-08-15 17:48:28 +000097size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
98 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +000099 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000100 if (Size == 1) return 0;
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000101 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 Serebryany8ce74242015-08-01 01:42:51 +0000108}
109
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000110size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
111 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000112 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000113 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 Serebryanydfbe59b2016-08-15 17:48:28 +0000120size_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 Serebryanyec2dcb12015-09-03 21:24:19 +0000137size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
138 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000139 size_t Idx = Rand(Size);
140 Data[Idx] = RandCh(Rand);
141 return Size;
142}
143
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000144size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
145 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000146 size_t Idx = Rand(Size);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000147 Data[Idx] ^= 1 << Rand(8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000148 return Size;
149}
150
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000151size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
152 size_t Size,
153 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000154 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000155}
156
Kostya Serebryany4b358742016-01-14 02:36:44 +0000157size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
158 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000159 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000160}
161
162size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
163 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000164 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000165}
166
Kostya Serebryany292cf032016-02-13 03:37:24 +0000167size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
168 size_t Size, size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000169 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000170 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 Serebryany476f0ce2016-01-16 03:53:32 +0000174 if (Rand.RandBool()) { // Insert W.
175 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000176 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000177 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 Serebryany160dcba2016-01-22 23:55:14 +0000182 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000183 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000184 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000185 DE.IncUseCount();
186 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000187 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000188}
189
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000190// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
191// Returns ToSize.
192size_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.
207size_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
231size_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 Serebryany25425ad2015-09-08 17:19:31 +0000239size_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 Serebryany27ab2d72015-12-19 02:49:09 +0000272size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
273 size_t MaxSize) {
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000274 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
275 size_t Idx = Rand(Corpus->size());
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000276 const Unit &O = (*Corpus)[Idx];
277 if (O.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000278 MutateInPlaceHere.resize(MaxSize);
279 auto &U = MutateInPlaceHere;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000280 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 Serebryany27ab2d72015-12-19 02:49:09 +0000295 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 Serebryany14c50282015-12-19 01:09:49 +0000301void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000302 CurrentMutatorSequence.clear();
303 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000304}
305
Kostya Serebryany4b358742016-01-14 02:36:44 +0000306// Copy successful dictionary entries to PersistentAutoDictionary.
307void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000308 for (auto DE : CurrentDictionaryEntrySequence) {
309 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000310 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000311 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000312 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
313 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000314 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000315}
316
317void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000318 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000319 for (auto &DE : PersistentAutoDictionary)
320 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000321 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000322 if (V.empty()) return;
323 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000324 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000325 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000326 PrintASCII(DE.GetW(), "\"");
327 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000328 }
329 Printf("###### End of recommended dictionary. ######\n");
330}
331
Kostya Serebryany14c50282015-12-19 01:09:49 +0000332void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000333 Printf("MS: %zd ", CurrentMutatorSequence.size());
334 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000335 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000336 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000337 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000338 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000339 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000340 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000341 }
342 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000343}
344
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000345size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000346 return MutateImpl(Data, Size, MaxSize, Mutators);
347}
348
349size_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.
355size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
356 size_t MaxSize,
357 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000358 assert(MaxSize > 0);
359 assert(Size <= MaxSize);
360 if (Size == 0) {
361 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000362 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000363 if (Options.OnlyASCII)
364 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000365 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000366 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000367 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000368 // 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 Aizatsky70fd3e42016-06-03 21:34:29 +0000372 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000373 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
374 if (NewSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000375 if (Options.OnlyASCII)
376 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000377 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000378 return NewSize;
379 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000380 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000381 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000382}
383
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000384void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000385 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000386 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000387}
388
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000389void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000390 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000391 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000392 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000393}
394
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000395void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000396 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000397}
398
Aaron Ballmanef116982015-01-29 16:58:29 +0000399} // namespace fuzzer