blob: 2a356df1c8b4ed58f6b483a9fb7ab2169f12d3b2 [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"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000015#include "FuzzerMutate.h"
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
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000021static void PrintASCII(const Word &W, const char *PrintAfter) {
22 PrintASCII(W.data(), W.size(), PrintAfter);
23}
24
Mike Aizatskyf0b3e852016-06-23 20:44:48 +000025MutationDispatcher::MutationDispatcher(Random &Rand,
26 const FuzzingOptions &Options)
27 : Rand(Rand), Options(Options) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000028 DefaultMutators.insert(
29 DefaultMutators.begin(),
30 {
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000031 {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000032 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000033 {&MutationDispatcher::Mutate_InsertRepeatedBytes,
34 "InsertRepeatedBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000035 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
36 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
37 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
38 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
Kostya Serebryany0c537b12016-08-17 21:30:30 +000039 {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000040 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000041 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
42 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
43 "AddFromManualDict"},
44 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
45 "AddFromTempAutoDict"},
46 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
47 "AddFromPersAutoDict"},
48 });
49
Dan Liew1873a492016-06-07 23:32:50 +000050 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000051 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
52 else
53 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000054
Dan Liew1873a492016-06-07 23:32:50 +000055 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000056 Mutators.push_back(
57 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000058}
Kostya Serebryany23194962016-02-13 03:46:26 +000059
Kostya Serebryanya3992212016-02-13 03:00:53 +000060static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000061 if (Rand.RandBool()) return Rand(256);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000062 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000063 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000064}
65
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000066size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
67 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000068 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000069}
70
Mike Aizatsky41d66832016-06-07 20:22:15 +000071size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
72 size_t MaxSize) {
73 if (!Corpus || Corpus->size() < 2 || Size == 0)
74 return 0;
75 size_t Idx = Rand(Corpus->size());
76 const Unit &Other = (*Corpus)[Idx];
77 if (Other.empty())
78 return 0;
79 MutateInPlaceHere.resize(MaxSize);
80 auto &U = MutateInPlaceHere;
Dan Liew1873a492016-06-07 23:32:50 +000081 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000082 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
83 if (!NewSize)
84 return 0;
85 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
86 memcpy(Data, U.data(), NewSize);
87 return NewSize;
88}
89
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000090size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
91 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000092 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000093 size_t ShuffleAmount =
94 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000095 size_t ShuffleStart = Rand(Size - ShuffleAmount);
96 assert(ShuffleStart + ShuffleAmount <= Size);
97 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
98 Rand);
99 return Size;
100}
101
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000102size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
103 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000104 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000105 if (Size == 1) return 0;
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000106 size_t N = Rand(Size / 2) + 1;
107 assert(N < Size);
108 size_t Idx = Rand(Size - N + 1);
109 // Erase Data[Idx:Idx+N].
110 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
111 // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
112 return Size - N;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000113}
114
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000115size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
116 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000117 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000118 size_t Idx = Rand(Size + 1);
119 // Insert new value at Data[Idx].
120 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
121 Data[Idx] = RandCh(Rand);
122 return Size + 1;
123}
124
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000125size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
126 size_t Size,
127 size_t MaxSize) {
128 const size_t kMinBytesToInsert = 3;
129 if (Size + kMinBytesToInsert >= MaxSize) return 0;
130 size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
131 size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
132 assert(Size + N <= MaxSize && N);
133 size_t Idx = Rand(Size + 1);
134 // Insert new values at Data[Idx].
135 memmove(Data + Idx + N, Data + Idx, Size - Idx);
Kostya Serebryanye72774d2016-08-17 21:50:54 +0000136 // Give preference to 0x00 and 0xff.
137 uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000138 for (size_t i = 0; i < N; i++)
139 Data[Idx + i] = Byte;
140 return Size + N;
141}
142
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000143size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
144 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000145 size_t Idx = Rand(Size);
146 Data[Idx] = RandCh(Rand);
147 return Size;
148}
149
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000150size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
151 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000152 size_t Idx = Rand(Size);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000153 Data[Idx] ^= 1 << Rand(8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000154 return Size;
155}
156
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000157size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
158 size_t Size,
159 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000160 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000161}
162
Kostya Serebryany4b358742016-01-14 02:36:44 +0000163size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
164 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000165 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000166}
167
168size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
169 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000170 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000171}
172
Kostya Serebryany292cf032016-02-13 03:37:24 +0000173size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
174 size_t Size, size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000175 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000176 DictionaryEntry &DE = D[Rand(D.size())];
177 const Word &W = DE.GetW();
178 bool UsePositionHint = DE.HasPositionHint() &&
179 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000180 if (Rand.RandBool()) { // Insert W.
181 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000182 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000183 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
184 memcpy(Data + Idx, W.data(), W.size());
185 Size += W.size();
186 } else { // Overwrite some bytes with W.
187 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000188 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000189 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000190 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000191 DE.IncUseCount();
192 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000193 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000194}
195
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000196// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
197// Returns ToSize.
198size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
199 uint8_t *To, size_t ToSize) {
200 // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
201 size_t ToBeg = Rand(ToSize);
202 size_t CopySize = Rand(ToSize - ToBeg) + 1;
203 assert(ToBeg + CopySize <= ToSize);
204 CopySize = std::min(CopySize, FromSize);
205 size_t FromBeg = Rand(FromSize - CopySize + 1);
206 assert(FromBeg + CopySize <= FromSize);
207 memmove(To + ToBeg, From + FromBeg, CopySize);
208 return ToSize;
209}
210
211// Inserts part of From[0,ToSize) into To.
212// Returns new size of To on success or 0 on failure.
213size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
214 uint8_t *To, size_t ToSize,
215 size_t MaxToSize) {
216 if (ToSize >= MaxToSize) return 0;
217 size_t AvailableSpace = MaxToSize - ToSize;
218 size_t MaxCopySize = std::min(AvailableSpace, FromSize);
219 size_t CopySize = Rand(MaxCopySize) + 1;
220 size_t FromBeg = Rand(FromSize - CopySize + 1);
221 assert(FromBeg + CopySize <= FromSize);
222 size_t ToInsertPos = Rand(ToSize + 1);
223 assert(ToInsertPos + CopySize <= MaxToSize);
224 size_t TailSize = ToSize - ToInsertPos;
225 if (To == From) {
226 MutateInPlaceHere.resize(MaxToSize);
227 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
228 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
229 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
230 } else {
231 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
232 memmove(To + ToInsertPos, From + FromBeg, CopySize);
233 }
234 return ToSize + CopySize;
235}
236
237size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
238 size_t MaxSize) {
239 if (Rand.RandBool())
240 return CopyPartOf(Data, Size, Data, Size);
241 else
242 return InsertPartOf(Data, Size, Data, Size, MaxSize);
243}
244
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000245size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
246 size_t MaxSize) {
247 size_t B = Rand(Size);
248 while (B < Size && !isdigit(Data[B])) B++;
249 if (B == Size) return 0;
250 size_t E = B;
251 while (E < Size && isdigit(Data[E])) E++;
252 assert(B < E);
253 // now we have digits in [B, E).
254 // strtol and friends don't accept non-zero-teminated data, parse it manually.
255 uint64_t Val = Data[B] - '0';
256 for (size_t i = B + 1; i < E; i++)
257 Val = Val * 10 + Data[i] - '0';
258
259 // Mutate the integer value.
260 switch(Rand(5)) {
261 case 0: Val++; break;
262 case 1: Val--; break;
263 case 2: Val /= 2; break;
264 case 3: Val *= 2; break;
265 case 4: Val = Rand(Val * Val); break;
266 default: assert(0);
267 }
268 // Just replace the bytes with the new ones, don't bother moving bytes.
269 for (size_t i = B; i < E; i++) {
270 size_t Idx = E + B - i - 1;
271 assert(Idx >= B && Idx < E);
272 Data[Idx] = (Val % 10) + '0';
273 Val /= 10;
274 }
275 return Size;
276}
277
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000278uint8_t Bswap(uint8_t x) { return x; }
279uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
280uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
281uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
282
283template<class T>
284size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
285 if (Size < sizeof(T)) return 0;
286 size_t Off = Rand(Size - sizeof(T) + 1);
287 assert(Off + sizeof(T) <= Size);
288 T Val;
289 memcpy(&Val, Data + Off, sizeof(Val));
290 T Add = Rand(21);
291 Add -= 10;
292 if (Rand.RandBool())
293 Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
294 else
295 Val = Val + Add; // Add assuming current endiannes.
296 if (Add == 0 || Rand.RandBool()) // Maybe negate.
297 Val = -Val;
298 memcpy(Data + Off, &Val, sizeof(Val));
299 return Size;
300}
301
302size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
303 size_t Size,
304 size_t MaxSize) {
305 switch (Rand(4)) {
306 case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
307 case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
308 case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
309 case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
310 default: assert(0);
311 }
Kostya Serebryanya533e512016-08-19 20:57:09 +0000312 return 0;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000313}
314
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000315size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
316 size_t MaxSize) {
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000317 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
318 size_t Idx = Rand(Corpus->size());
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000319 const Unit &O = (*Corpus)[Idx];
320 if (O.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000321 MutateInPlaceHere.resize(MaxSize);
322 auto &U = MutateInPlaceHere;
Kostya Serebryanya533e512016-08-19 20:57:09 +0000323 size_t NewSize = 0;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000324 switch(Rand(3)) {
325 case 0:
326 NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
327 break;
328 case 1:
329 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
330 if (NewSize)
331 break;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000332 // LLVM_FALLTHROUGH;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000333 case 2:
334 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
335 break;
336 default: assert(0);
337 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000338 assert(NewSize > 0 && "CrossOver returned empty unit");
339 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
340 memcpy(Data, U.data(), NewSize);
341 return NewSize;
342}
343
Kostya Serebryany14c50282015-12-19 01:09:49 +0000344void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000345 CurrentMutatorSequence.clear();
346 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000347}
348
Kostya Serebryany4b358742016-01-14 02:36:44 +0000349// Copy successful dictionary entries to PersistentAutoDictionary.
350void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000351 for (auto DE : CurrentDictionaryEntrySequence) {
352 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000353 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000354 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000355 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
356 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000357 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000358}
359
360void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000361 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000362 for (auto &DE : PersistentAutoDictionary)
363 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000364 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000365 if (V.empty()) return;
366 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000367 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000368 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000369 PrintASCII(DE.GetW(), "\"");
370 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000371 }
372 Printf("###### End of recommended dictionary. ######\n");
373}
374
Kostya Serebryany14c50282015-12-19 01:09:49 +0000375void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000376 Printf("MS: %zd ", CurrentMutatorSequence.size());
377 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000378 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000379 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000380 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000381 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000382 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000383 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000384 }
385 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000386}
387
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000388size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000389 return MutateImpl(Data, Size, MaxSize, Mutators);
390}
391
392size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
393 size_t MaxSize) {
394 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
395}
396
397// Mutates Data in place, returns new size.
398size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
399 size_t MaxSize,
400 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000401 assert(MaxSize > 0);
402 assert(Size <= MaxSize);
403 if (Size == 0) {
404 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000405 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000406 if (Options.OnlyASCII)
407 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000408 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000409 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000410 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000411 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
412 // in which case they will return 0.
413 // Try several times before returning un-mutated data.
414 for (int Iter = 0; Iter < 10; Iter++) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000415 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000416 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
417 if (NewSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000418 if (Options.OnlyASCII)
419 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000420 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000421 return NewSize;
422 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000423 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000424 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000425}
426
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000427void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000428 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000429 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000430}
431
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000432void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000433 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000434 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000435 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000436}
437
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000438void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000439 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000440}
441
Aaron Ballmanef116982015-01-29 16:58:29 +0000442} // namespace fuzzer