blob: ac48dc32cc20ea1f74e948b9ea1306520adb499a [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 Serebryany0c537b12016-08-17 21:30:30 +000035 {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000036 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000037 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
38 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
39 "AddFromManualDict"},
40 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
41 "AddFromTempAutoDict"},
42 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
43 "AddFromPersAutoDict"},
44 });
45
Dan Liew1873a492016-06-07 23:32:50 +000046 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000047 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
48 else
49 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000050
Dan Liew1873a492016-06-07 23:32:50 +000051 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000052 Mutators.push_back(
53 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000054}
Kostya Serebryany23194962016-02-13 03:46:26 +000055
Kostya Serebryanya3992212016-02-13 03:00:53 +000056static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000057 if (Rand.RandBool()) return Rand(256);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000058 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000059 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000060}
61
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000062size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
63 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000064 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000065}
66
Mike Aizatsky41d66832016-06-07 20:22:15 +000067size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
68 size_t MaxSize) {
69 if (!Corpus || Corpus->size() < 2 || Size == 0)
70 return 0;
71 size_t Idx = Rand(Corpus->size());
72 const Unit &Other = (*Corpus)[Idx];
73 if (Other.empty())
74 return 0;
75 MutateInPlaceHere.resize(MaxSize);
76 auto &U = MutateInPlaceHere;
Dan Liew1873a492016-06-07 23:32:50 +000077 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000078 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
79 if (!NewSize)
80 return 0;
81 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
82 memcpy(Data, U.data(), NewSize);
83 return NewSize;
84}
85
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000086size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
87 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000088 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000089 size_t ShuffleAmount =
90 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000091 size_t ShuffleStart = Rand(Size - ShuffleAmount);
92 assert(ShuffleStart + ShuffleAmount <= Size);
93 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
94 Rand);
95 return Size;
96}
97
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000098size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
99 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000100 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000101 if (Size == 1) return 0;
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000102 size_t N = Rand(Size / 2) + 1;
103 assert(N < Size);
104 size_t Idx = Rand(Size - N + 1);
105 // Erase Data[Idx:Idx+N].
106 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
107 // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
108 return Size - N;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000109}
110
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000111size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
112 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000113 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000114 size_t Idx = Rand(Size + 1);
115 // Insert new value at Data[Idx].
116 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
117 Data[Idx] = RandCh(Rand);
118 return Size + 1;
119}
120
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000121size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
122 size_t Size,
123 size_t MaxSize) {
124 const size_t kMinBytesToInsert = 3;
125 if (Size + kMinBytesToInsert >= MaxSize) return 0;
126 size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
127 size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
128 assert(Size + N <= MaxSize && N);
129 size_t Idx = Rand(Size + 1);
130 // Insert new values at Data[Idx].
131 memmove(Data + Idx + N, Data + Idx, Size - Idx);
Kostya Serebryanye72774d2016-08-17 21:50:54 +0000132 // Give preference to 0x00 and 0xff.
133 uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000134 for (size_t i = 0; i < N; i++)
135 Data[Idx + i] = Byte;
136 return Size + N;
137}
138
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000139size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
140 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000141 size_t Idx = Rand(Size);
142 Data[Idx] = RandCh(Rand);
143 return Size;
144}
145
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000146size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
147 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000148 size_t Idx = Rand(Size);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000149 Data[Idx] ^= 1 << Rand(8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000150 return Size;
151}
152
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000153size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
154 size_t Size,
155 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000156 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000157}
158
Kostya Serebryany4b358742016-01-14 02:36:44 +0000159size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
160 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000161 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000162}
163
164size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
165 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000166 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000167}
168
Kostya Serebryany292cf032016-02-13 03:37:24 +0000169size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
170 size_t Size, size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000171 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000172 DictionaryEntry &DE = D[Rand(D.size())];
173 const Word &W = DE.GetW();
174 bool UsePositionHint = DE.HasPositionHint() &&
175 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000176 if (Rand.RandBool()) { // Insert W.
177 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000178 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000179 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
180 memcpy(Data + Idx, W.data(), W.size());
181 Size += W.size();
182 } else { // Overwrite some bytes with W.
183 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000184 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000185 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000186 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000187 DE.IncUseCount();
188 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000189 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000190}
191
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000192// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
193// Returns ToSize.
194size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
195 uint8_t *To, size_t ToSize) {
196 // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
197 size_t ToBeg = Rand(ToSize);
198 size_t CopySize = Rand(ToSize - ToBeg) + 1;
199 assert(ToBeg + CopySize <= ToSize);
200 CopySize = std::min(CopySize, FromSize);
201 size_t FromBeg = Rand(FromSize - CopySize + 1);
202 assert(FromBeg + CopySize <= FromSize);
203 memmove(To + ToBeg, From + FromBeg, CopySize);
204 return ToSize;
205}
206
207// Inserts part of From[0,ToSize) into To.
208// Returns new size of To on success or 0 on failure.
209size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
210 uint8_t *To, size_t ToSize,
211 size_t MaxToSize) {
212 if (ToSize >= MaxToSize) return 0;
213 size_t AvailableSpace = MaxToSize - ToSize;
214 size_t MaxCopySize = std::min(AvailableSpace, FromSize);
215 size_t CopySize = Rand(MaxCopySize) + 1;
216 size_t FromBeg = Rand(FromSize - CopySize + 1);
217 assert(FromBeg + CopySize <= FromSize);
218 size_t ToInsertPos = Rand(ToSize + 1);
219 assert(ToInsertPos + CopySize <= MaxToSize);
220 size_t TailSize = ToSize - ToInsertPos;
221 if (To == From) {
222 MutateInPlaceHere.resize(MaxToSize);
223 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
224 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
225 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
226 } else {
227 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
228 memmove(To + ToInsertPos, From + FromBeg, CopySize);
229 }
230 return ToSize + CopySize;
231}
232
233size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
234 size_t MaxSize) {
235 if (Rand.RandBool())
236 return CopyPartOf(Data, Size, Data, Size);
237 else
238 return InsertPartOf(Data, Size, Data, Size, MaxSize);
239}
240
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000241size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
242 size_t MaxSize) {
243 size_t B = Rand(Size);
244 while (B < Size && !isdigit(Data[B])) B++;
245 if (B == Size) return 0;
246 size_t E = B;
247 while (E < Size && isdigit(Data[E])) E++;
248 assert(B < E);
249 // now we have digits in [B, E).
250 // strtol and friends don't accept non-zero-teminated data, parse it manually.
251 uint64_t Val = Data[B] - '0';
252 for (size_t i = B + 1; i < E; i++)
253 Val = Val * 10 + Data[i] - '0';
254
255 // Mutate the integer value.
256 switch(Rand(5)) {
257 case 0: Val++; break;
258 case 1: Val--; break;
259 case 2: Val /= 2; break;
260 case 3: Val *= 2; break;
261 case 4: Val = Rand(Val * Val); break;
262 default: assert(0);
263 }
264 // Just replace the bytes with the new ones, don't bother moving bytes.
265 for (size_t i = B; i < E; i++) {
266 size_t Idx = E + B - i - 1;
267 assert(Idx >= B && Idx < E);
268 Data[Idx] = (Val % 10) + '0';
269 Val /= 10;
270 }
271 return Size;
272}
273
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000274uint8_t Bswap(uint8_t x) { return x; }
275uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
276uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
277uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
278
279template<class T>
280size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
281 if (Size < sizeof(T)) return 0;
282 size_t Off = Rand(Size - sizeof(T) + 1);
283 assert(Off + sizeof(T) <= Size);
284 T Val;
285 memcpy(&Val, Data + Off, sizeof(Val));
286 T Add = Rand(21);
287 Add -= 10;
288 if (Rand.RandBool())
289 Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
290 else
291 Val = Val + Add; // Add assuming current endiannes.
292 if (Add == 0 || Rand.RandBool()) // Maybe negate.
293 Val = -Val;
294 memcpy(Data + Off, &Val, sizeof(Val));
295 return Size;
296}
297
298size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
299 size_t Size,
300 size_t MaxSize) {
301 switch (Rand(4)) {
302 case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
303 case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
304 case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
305 case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
306 default: assert(0);
307 }
Kostya Serebryanya533e512016-08-19 20:57:09 +0000308 return 0;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000309}
310
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000311size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
312 size_t MaxSize) {
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000313 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
314 size_t Idx = Rand(Corpus->size());
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000315 const Unit &O = (*Corpus)[Idx];
316 if (O.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000317 MutateInPlaceHere.resize(MaxSize);
318 auto &U = MutateInPlaceHere;
Kostya Serebryanya533e512016-08-19 20:57:09 +0000319 size_t NewSize = 0;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000320 switch(Rand(3)) {
321 case 0:
322 NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
323 break;
324 case 1:
325 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
326 if (NewSize)
327 break;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000328 // LLVM_FALLTHROUGH;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000329 case 2:
330 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
331 break;
332 default: assert(0);
333 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000334 assert(NewSize > 0 && "CrossOver returned empty unit");
335 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
336 memcpy(Data, U.data(), NewSize);
337 return NewSize;
338}
339
Kostya Serebryany14c50282015-12-19 01:09:49 +0000340void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000341 CurrentMutatorSequence.clear();
342 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000343}
344
Kostya Serebryany4b358742016-01-14 02:36:44 +0000345// Copy successful dictionary entries to PersistentAutoDictionary.
346void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000347 for (auto DE : CurrentDictionaryEntrySequence) {
348 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000349 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000350 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000351 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
352 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000353 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000354}
355
356void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000357 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000358 for (auto &DE : PersistentAutoDictionary)
359 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000360 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000361 if (V.empty()) return;
362 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000363 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000364 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000365 PrintASCII(DE.GetW(), "\"");
366 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000367 }
368 Printf("###### End of recommended dictionary. ######\n");
369}
370
Kostya Serebryany14c50282015-12-19 01:09:49 +0000371void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000372 Printf("MS: %zd ", CurrentMutatorSequence.size());
373 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000374 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000375 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000376 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000377 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000378 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000379 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000380 }
381 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000382}
383
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000384size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000385 return MutateImpl(Data, Size, MaxSize, Mutators);
386}
387
388size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
389 size_t MaxSize) {
390 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
391}
392
393// Mutates Data in place, returns new size.
394size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
395 size_t MaxSize,
396 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000397 assert(MaxSize > 0);
398 assert(Size <= MaxSize);
399 if (Size == 0) {
400 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000401 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000402 if (Options.OnlyASCII)
403 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000404 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000405 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000406 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000407 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
408 // in which case they will return 0.
409 // Try several times before returning un-mutated data.
410 for (int Iter = 0; Iter < 10; Iter++) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000411 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000412 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
413 if (NewSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000414 if (Options.OnlyASCII)
415 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000416 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000417 return NewSize;
418 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000419 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000420 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000421}
422
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000423void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000424 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000425 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000426}
427
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000428void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000429 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000430 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000431 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000432}
433
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000434void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000435 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000436}
437
Aaron Ballmanef116982015-01-29 16:58:29 +0000438} // namespace fuzzer