blob: b243aa653c34a8f4cddd34418290b8d997f12444 [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>
Kostya Serebryany556894f2016-09-21 02:05:39 +000013#include <unordered_set>
Kostya Serebryanyf3424592015-05-22 22:35:31 +000014
Kostya Serebryany556894f2016-09-21 02:05:39 +000015#include "FuzzerCorpus.h"
16#include "FuzzerDefs.h"
17#include "FuzzerExtFunctions.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000018#include "FuzzerMutate.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000019#include "FuzzerOptions.h"
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000020
Aaron Ballmanef116982015-01-29 16:58:29 +000021namespace fuzzer {
22
Kostya Serebryany2f13f222016-01-21 01:52:14 +000023const size_t Dictionary::kMaxDictSize;
24
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000025static void PrintASCII(const Word &W, const char *PrintAfter) {
26 PrintASCII(W.data(), W.size(), PrintAfter);
27}
28
Mike Aizatskyf0b3e852016-06-23 20:44:48 +000029MutationDispatcher::MutationDispatcher(Random &Rand,
30 const FuzzingOptions &Options)
31 : Rand(Rand), Options(Options) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000032 DefaultMutators.insert(
33 DefaultMutators.begin(),
34 {
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000035 {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000036 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000037 {&MutationDispatcher::Mutate_InsertRepeatedBytes,
38 "InsertRepeatedBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000039 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
40 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
41 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
42 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
Kostya Serebryany0c537b12016-08-17 21:30:30 +000043 {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000044 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000045 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
46 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
47 "AddFromManualDict"},
48 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
49 "AddFromTempAutoDict"},
50 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
51 "AddFromPersAutoDict"},
52 });
53
Dan Liew1873a492016-06-07 23:32:50 +000054 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000055 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
56 else
57 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000058
Dan Liew1873a492016-06-07 23:32:50 +000059 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000060 Mutators.push_back(
61 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000062}
Kostya Serebryany23194962016-02-13 03:46:26 +000063
Kostya Serebryanya3992212016-02-13 03:00:53 +000064static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000065 if (Rand.RandBool()) return Rand(256);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000066 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000067 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000068}
69
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000070size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
71 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000072 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000073}
74
Mike Aizatsky41d66832016-06-07 20:22:15 +000075size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
76 size_t MaxSize) {
77 if (!Corpus || Corpus->size() < 2 || Size == 0)
78 return 0;
79 size_t Idx = Rand(Corpus->size());
80 const Unit &Other = (*Corpus)[Idx];
81 if (Other.empty())
82 return 0;
83 MutateInPlaceHere.resize(MaxSize);
84 auto &U = MutateInPlaceHere;
Dan Liew1873a492016-06-07 23:32:50 +000085 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000086 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
87 if (!NewSize)
88 return 0;
89 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
90 memcpy(Data, U.data(), NewSize);
91 return NewSize;
92}
93
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000094size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
95 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000096 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000097 size_t ShuffleAmount =
98 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000099 size_t ShuffleStart = Rand(Size - ShuffleAmount);
100 assert(ShuffleStart + ShuffleAmount <= Size);
101 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
102 Rand);
103 return Size;
104}
105
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000106size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
107 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000108 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000109 if (Size == 1) return 0;
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000110 size_t N = Rand(Size / 2) + 1;
111 assert(N < Size);
112 size_t Idx = Rand(Size - N + 1);
113 // Erase Data[Idx:Idx+N].
114 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
115 // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
116 return Size - N;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000117}
118
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000119size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
120 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000121 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000122 size_t Idx = Rand(Size + 1);
123 // Insert new value at Data[Idx].
124 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
125 Data[Idx] = RandCh(Rand);
126 return Size + 1;
127}
128
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000129size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
130 size_t Size,
131 size_t MaxSize) {
132 const size_t kMinBytesToInsert = 3;
133 if (Size + kMinBytesToInsert >= MaxSize) return 0;
134 size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
135 size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
136 assert(Size + N <= MaxSize && N);
137 size_t Idx = Rand(Size + 1);
138 // Insert new values at Data[Idx].
139 memmove(Data + Idx + N, Data + Idx, Size - Idx);
Kostya Serebryanye72774d2016-08-17 21:50:54 +0000140 // Give preference to 0x00 and 0xff.
141 uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000142 for (size_t i = 0; i < N; i++)
143 Data[Idx + i] = Byte;
144 return Size + N;
145}
146
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000147size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
148 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000149 size_t Idx = Rand(Size);
150 Data[Idx] = RandCh(Rand);
151 return Size;
152}
153
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000154size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
155 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000156 size_t Idx = Rand(Size);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000157 Data[Idx] ^= 1 << Rand(8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000158 return Size;
159}
160
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000161size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
162 size_t Size,
163 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000164 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000165}
166
Kostya Serebryany4b358742016-01-14 02:36:44 +0000167size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
168 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000169 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000170}
171
172size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
173 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000174 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000175}
176
Kostya Serebryany292cf032016-02-13 03:37:24 +0000177size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
178 size_t Size, size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000179 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000180 DictionaryEntry &DE = D[Rand(D.size())];
181 const Word &W = DE.GetW();
182 bool UsePositionHint = DE.HasPositionHint() &&
183 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000184 if (Rand.RandBool()) { // Insert W.
185 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000186 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000187 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
188 memcpy(Data + Idx, W.data(), W.size());
189 Size += W.size();
190 } else { // Overwrite some bytes with W.
191 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000192 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000193 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000194 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000195 DE.IncUseCount();
196 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000197 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000198}
199
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000200// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
201// Returns ToSize.
202size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
203 uint8_t *To, size_t ToSize) {
204 // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
205 size_t ToBeg = Rand(ToSize);
206 size_t CopySize = Rand(ToSize - ToBeg) + 1;
207 assert(ToBeg + CopySize <= ToSize);
208 CopySize = std::min(CopySize, FromSize);
209 size_t FromBeg = Rand(FromSize - CopySize + 1);
210 assert(FromBeg + CopySize <= FromSize);
211 memmove(To + ToBeg, From + FromBeg, CopySize);
212 return ToSize;
213}
214
215// Inserts part of From[0,ToSize) into To.
216// Returns new size of To on success or 0 on failure.
217size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
218 uint8_t *To, size_t ToSize,
219 size_t MaxToSize) {
220 if (ToSize >= MaxToSize) return 0;
221 size_t AvailableSpace = MaxToSize - ToSize;
222 size_t MaxCopySize = std::min(AvailableSpace, FromSize);
223 size_t CopySize = Rand(MaxCopySize) + 1;
224 size_t FromBeg = Rand(FromSize - CopySize + 1);
225 assert(FromBeg + CopySize <= FromSize);
226 size_t ToInsertPos = Rand(ToSize + 1);
227 assert(ToInsertPos + CopySize <= MaxToSize);
228 size_t TailSize = ToSize - ToInsertPos;
229 if (To == From) {
230 MutateInPlaceHere.resize(MaxToSize);
231 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
232 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
233 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
234 } else {
235 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
236 memmove(To + ToInsertPos, From + FromBeg, CopySize);
237 }
238 return ToSize + CopySize;
239}
240
241size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
242 size_t MaxSize) {
243 if (Rand.RandBool())
244 return CopyPartOf(Data, Size, Data, Size);
245 else
246 return InsertPartOf(Data, Size, Data, Size, MaxSize);
247}
248
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000249size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
250 size_t MaxSize) {
251 size_t B = Rand(Size);
252 while (B < Size && !isdigit(Data[B])) B++;
253 if (B == Size) return 0;
254 size_t E = B;
255 while (E < Size && isdigit(Data[E])) E++;
256 assert(B < E);
257 // now we have digits in [B, E).
258 // strtol and friends don't accept non-zero-teminated data, parse it manually.
259 uint64_t Val = Data[B] - '0';
260 for (size_t i = B + 1; i < E; i++)
261 Val = Val * 10 + Data[i] - '0';
262
263 // Mutate the integer value.
264 switch(Rand(5)) {
265 case 0: Val++; break;
266 case 1: Val--; break;
267 case 2: Val /= 2; break;
268 case 3: Val *= 2; break;
269 case 4: Val = Rand(Val * Val); break;
270 default: assert(0);
271 }
272 // Just replace the bytes with the new ones, don't bother moving bytes.
273 for (size_t i = B; i < E; i++) {
274 size_t Idx = E + B - i - 1;
275 assert(Idx >= B && Idx < E);
276 Data[Idx] = (Val % 10) + '0';
277 Val /= 10;
278 }
279 return Size;
280}
281
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000282uint8_t Bswap(uint8_t x) { return x; }
283uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
284uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
285uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
286
287template<class T>
288size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
289 if (Size < sizeof(T)) return 0;
290 size_t Off = Rand(Size - sizeof(T) + 1);
291 assert(Off + sizeof(T) <= Size);
292 T Val;
293 memcpy(&Val, Data + Off, sizeof(Val));
294 T Add = Rand(21);
295 Add -= 10;
296 if (Rand.RandBool())
297 Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
298 else
299 Val = Val + Add; // Add assuming current endiannes.
300 if (Add == 0 || Rand.RandBool()) // Maybe negate.
301 Val = -Val;
302 memcpy(Data + Off, &Val, sizeof(Val));
303 return Size;
304}
305
306size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
307 size_t Size,
308 size_t MaxSize) {
309 switch (Rand(4)) {
310 case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
311 case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
312 case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
313 case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
314 default: assert(0);
315 }
Kostya Serebryanya533e512016-08-19 20:57:09 +0000316 return 0;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000317}
318
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000319size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
320 size_t MaxSize) {
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000321 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
322 size_t Idx = Rand(Corpus->size());
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000323 const Unit &O = (*Corpus)[Idx];
324 if (O.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000325 MutateInPlaceHere.resize(MaxSize);
326 auto &U = MutateInPlaceHere;
Kostya Serebryanya533e512016-08-19 20:57:09 +0000327 size_t NewSize = 0;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000328 switch(Rand(3)) {
329 case 0:
330 NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
331 break;
332 case 1:
333 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
334 if (NewSize)
335 break;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000336 // LLVM_FALLTHROUGH;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000337 case 2:
338 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
339 break;
340 default: assert(0);
341 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000342 assert(NewSize > 0 && "CrossOver returned empty unit");
343 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
344 memcpy(Data, U.data(), NewSize);
345 return NewSize;
346}
347
Kostya Serebryany14c50282015-12-19 01:09:49 +0000348void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000349 CurrentMutatorSequence.clear();
350 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000351}
352
Kostya Serebryany4b358742016-01-14 02:36:44 +0000353// Copy successful dictionary entries to PersistentAutoDictionary.
354void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000355 for (auto DE : CurrentDictionaryEntrySequence) {
356 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000357 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000358 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000359 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
360 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000361 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000362}
363
364void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000365 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000366 for (auto &DE : PersistentAutoDictionary)
367 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000368 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000369 if (V.empty()) return;
370 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000371 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000372 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000373 PrintASCII(DE.GetW(), "\"");
374 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000375 }
376 Printf("###### End of recommended dictionary. ######\n");
377}
378
Kostya Serebryany14c50282015-12-19 01:09:49 +0000379void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000380 Printf("MS: %zd ", CurrentMutatorSequence.size());
381 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000382 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000383 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000384 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000385 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000386 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000387 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000388 }
389 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000390}
391
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000392size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000393 return MutateImpl(Data, Size, MaxSize, Mutators);
394}
395
396size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
397 size_t MaxSize) {
398 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
399}
400
401// Mutates Data in place, returns new size.
402size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
403 size_t MaxSize,
404 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000405 assert(MaxSize > 0);
406 assert(Size <= MaxSize);
407 if (Size == 0) {
408 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000409 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000410 if (Options.OnlyASCII)
411 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000412 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000413 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000414 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000415 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
416 // in which case they will return 0.
417 // Try several times before returning un-mutated data.
418 for (int Iter = 0; Iter < 10; Iter++) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000419 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000420 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
421 if (NewSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000422 if (Options.OnlyASCII)
423 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000424 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000425 return NewSize;
426 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000427 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000428 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000429}
430
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000431void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000432 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000433 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000434}
435
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000436void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000437 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000438 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000439 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000440}
441
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000442void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000443 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000444}
445
Aaron Ballmanef116982015-01-29 16:58:29 +0000446} // namespace fuzzer