blob: 7da9256c562f1b2c1160b1fa0ea3a935944e1a13 [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
Kostya Serebryany556894f2016-09-21 02:05:39 +000014#include "FuzzerCorpus.h"
15#include "FuzzerDefs.h"
16#include "FuzzerExtFunctions.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000017#include "FuzzerMutate.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000018#include "FuzzerOptions.h"
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000019
Aaron Ballmanef116982015-01-29 16:58:29 +000020namespace fuzzer {
21
Kostya Serebryany2f13f222016-01-21 01:52:14 +000022const size_t Dictionary::kMaxDictSize;
23
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000024static void PrintASCII(const Word &W, const char *PrintAfter) {
25 PrintASCII(W.data(), W.size(), PrintAfter);
26}
27
Mike Aizatskyf0b3e852016-06-23 20:44:48 +000028MutationDispatcher::MutationDispatcher(Random &Rand,
29 const FuzzingOptions &Options)
30 : Rand(Rand), Options(Options) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000031 DefaultMutators.insert(
32 DefaultMutators.begin(),
33 {
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000034 {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000035 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +000036 {&MutationDispatcher::Mutate_InsertRepeatedBytes,
37 "InsertRepeatedBytes"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000038 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
39 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
40 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
41 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
Kostya Serebryany0c537b12016-08-17 21:30:30 +000042 {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000043 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000044 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
45 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000046 "ManualDict"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000047 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000048 "TempAutoDict"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000049 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000050 "PersAutoDict"},
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000051 });
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000052 if(Options.UseCmp)
53 DefaultMutators.push_back(
54 {&MutationDispatcher::Mutate_AddWordFromTraceCmpDictionary,
55 "TraceCmpDict"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000056
Dan Liew1873a492016-06-07 23:32:50 +000057 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000058 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
59 else
60 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000061
Dan Liew1873a492016-06-07 23:32:50 +000062 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000063 Mutators.push_back(
64 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000065}
Kostya Serebryany23194962016-02-13 03:46:26 +000066
Kostya Serebryanya3992212016-02-13 03:00:53 +000067static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000068 if (Rand.RandBool()) return Rand(256);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +000069 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000070 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000071}
72
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000073size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
74 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000075 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000076}
77
Mike Aizatsky41d66832016-06-07 20:22:15 +000078size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
79 size_t MaxSize) {
80 if (!Corpus || Corpus->size() < 2 || Size == 0)
81 return 0;
82 size_t Idx = Rand(Corpus->size());
83 const Unit &Other = (*Corpus)[Idx];
84 if (Other.empty())
85 return 0;
86 MutateInPlaceHere.resize(MaxSize);
87 auto &U = MutateInPlaceHere;
Dan Liew1873a492016-06-07 23:32:50 +000088 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000089 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
90 if (!NewSize)
91 return 0;
92 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
93 memcpy(Data, U.data(), NewSize);
94 return NewSize;
95}
96
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000097size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
98 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000099 if (Size > MaxSize) return 0;
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000100 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000101 size_t ShuffleAmount =
102 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000103 size_t ShuffleStart = Rand(Size - ShuffleAmount);
104 assert(ShuffleStart + ShuffleAmount <= Size);
105 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
106 Rand);
107 return Size;
108}
109
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000110size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
111 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000112 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000113 if (Size == 1) return 0;
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000114 size_t N = Rand(Size / 2) + 1;
115 assert(N < Size);
116 size_t Idx = Rand(Size - N + 1);
117 // Erase Data[Idx:Idx+N].
118 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
119 // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
120 return Size - N;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000121}
122
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000123size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
124 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000125 if (Size >= MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000126 size_t Idx = Rand(Size + 1);
127 // Insert new value at Data[Idx].
128 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
129 Data[Idx] = RandCh(Rand);
130 return Size + 1;
131}
132
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000133size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
134 size_t Size,
135 size_t MaxSize) {
136 const size_t kMinBytesToInsert = 3;
137 if (Size + kMinBytesToInsert >= MaxSize) return 0;
138 size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
139 size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
140 assert(Size + N <= MaxSize && N);
141 size_t Idx = Rand(Size + 1);
142 // Insert new values at Data[Idx].
143 memmove(Data + Idx + N, Data + Idx, Size - Idx);
Kostya Serebryanye72774d2016-08-17 21:50:54 +0000144 // Give preference to 0x00 and 0xff.
145 uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
Kostya Serebryanydfbe59b2016-08-15 17:48:28 +0000146 for (size_t i = 0; i < N; i++)
147 Data[Idx + i] = Byte;
148 return Size + N;
149}
150
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000151size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
152 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000153 if (Size > MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000154 size_t Idx = Rand(Size);
155 Data[Idx] = RandCh(Rand);
156 return Size;
157}
158
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000159size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
160 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000161 if (Size > MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000162 size_t Idx = Rand(Size);
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000163 Data[Idx] ^= 1 << Rand(8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000164 return Size;
165}
166
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000167size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
168 size_t Size,
169 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000170 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000171}
172
Kostya Serebryany4b358742016-01-14 02:36:44 +0000173size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
174 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000175 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000176}
177
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000178size_t MutationDispatcher::Mutate_AddWordFromTraceCmpDictionary(
179 uint8_t *Data, size_t Size, size_t MaxSize) {
180 return AddWordFromDictionary(TraceCmpDictionary, Data, Size, MaxSize);
181}
182
Kostya Serebryany4b358742016-01-14 02:36:44 +0000183size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
184 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000185 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000186}
187
Kostya Serebryany292cf032016-02-13 03:37:24 +0000188size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
189 size_t Size, size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000190 if (Size > MaxSize) return 0;
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000191 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000192 DictionaryEntry &DE = D[Rand(D.size())];
193 const Word &W = DE.GetW();
194 bool UsePositionHint = DE.HasPositionHint() &&
195 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000196 if (Rand.RandBool()) { // Insert W.
197 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000198 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000199 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
200 memcpy(Data + Idx, W.data(), W.size());
201 Size += W.size();
202 } else { // Overwrite some bytes with W.
203 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000204 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000205 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000206 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000207 DE.IncUseCount();
208 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000209 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000210}
211
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000212// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
213// Returns ToSize.
214size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
215 uint8_t *To, size_t ToSize) {
216 // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
217 size_t ToBeg = Rand(ToSize);
218 size_t CopySize = Rand(ToSize - ToBeg) + 1;
219 assert(ToBeg + CopySize <= ToSize);
220 CopySize = std::min(CopySize, FromSize);
221 size_t FromBeg = Rand(FromSize - CopySize + 1);
222 assert(FromBeg + CopySize <= FromSize);
223 memmove(To + ToBeg, From + FromBeg, CopySize);
224 return ToSize;
225}
226
227// Inserts part of From[0,ToSize) into To.
228// Returns new size of To on success or 0 on failure.
229size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
230 uint8_t *To, size_t ToSize,
231 size_t MaxToSize) {
232 if (ToSize >= MaxToSize) return 0;
233 size_t AvailableSpace = MaxToSize - ToSize;
234 size_t MaxCopySize = std::min(AvailableSpace, FromSize);
235 size_t CopySize = Rand(MaxCopySize) + 1;
236 size_t FromBeg = Rand(FromSize - CopySize + 1);
237 assert(FromBeg + CopySize <= FromSize);
238 size_t ToInsertPos = Rand(ToSize + 1);
239 assert(ToInsertPos + CopySize <= MaxToSize);
240 size_t TailSize = ToSize - ToInsertPos;
241 if (To == From) {
242 MutateInPlaceHere.resize(MaxToSize);
243 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
244 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
245 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
246 } else {
247 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
248 memmove(To + ToInsertPos, From + FromBeg, CopySize);
249 }
250 return ToSize + CopySize;
251}
252
253size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
254 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000255 if (Size > MaxSize) return 0;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000256 if (Rand.RandBool())
257 return CopyPartOf(Data, Size, Data, Size);
258 else
259 return InsertPartOf(Data, Size, Data, Size, MaxSize);
260}
261
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000262size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
263 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000264 if (Size > MaxSize) return 0;
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000265 size_t B = Rand(Size);
266 while (B < Size && !isdigit(Data[B])) B++;
267 if (B == Size) return 0;
268 size_t E = B;
269 while (E < Size && isdigit(Data[E])) E++;
270 assert(B < E);
271 // now we have digits in [B, E).
272 // strtol and friends don't accept non-zero-teminated data, parse it manually.
273 uint64_t Val = Data[B] - '0';
274 for (size_t i = B + 1; i < E; i++)
275 Val = Val * 10 + Data[i] - '0';
276
277 // Mutate the integer value.
278 switch(Rand(5)) {
279 case 0: Val++; break;
280 case 1: Val--; break;
281 case 2: Val /= 2; break;
282 case 3: Val *= 2; break;
283 case 4: Val = Rand(Val * Val); break;
284 default: assert(0);
285 }
286 // Just replace the bytes with the new ones, don't bother moving bytes.
287 for (size_t i = B; i < E; i++) {
288 size_t Idx = E + B - i - 1;
289 assert(Idx >= B && Idx < E);
290 Data[Idx] = (Val % 10) + '0';
291 Val /= 10;
292 }
293 return Size;
294}
295
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000296template<class T>
297size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
298 if (Size < sizeof(T)) return 0;
299 size_t Off = Rand(Size - sizeof(T) + 1);
300 assert(Off + sizeof(T) <= Size);
301 T Val;
Kostya Serebryany65f102d2016-10-22 03:48:53 +0000302 if (Off < 64 && !Rand(4)) {
303 Val = Size;
304 if (Rand.RandBool())
305 Val = Bswap(Val);
306 } else {
307 memcpy(&Val, Data + Off, sizeof(Val));
308 T Add = Rand(21);
309 Add -= 10;
310 if (Rand.RandBool())
311 Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
312 else
313 Val = Val + Add; // Add assuming current endiannes.
314 if (Add == 0 || Rand.RandBool()) // Maybe negate.
315 Val = -Val;
316 }
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000317 memcpy(Data + Off, &Val, sizeof(Val));
318 return Size;
319}
320
321size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
322 size_t Size,
323 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000324 if (Size > MaxSize) return 0;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000325 switch (Rand(4)) {
326 case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
327 case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
328 case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
329 case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
330 default: assert(0);
331 }
Kostya Serebryanya533e512016-08-19 20:57:09 +0000332 return 0;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000333}
334
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000335size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
336 size_t MaxSize) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000337 if (Size > MaxSize) return 0;
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000338 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
339 size_t Idx = Rand(Corpus->size());
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000340 const Unit &O = (*Corpus)[Idx];
341 if (O.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000342 MutateInPlaceHere.resize(MaxSize);
343 auto &U = MutateInPlaceHere;
Kostya Serebryanya533e512016-08-19 20:57:09 +0000344 size_t NewSize = 0;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000345 switch(Rand(3)) {
346 case 0:
347 NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
348 break;
349 case 1:
350 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
351 if (NewSize)
352 break;
Kostya Serebryany0c537b12016-08-17 21:30:30 +0000353 // LLVM_FALLTHROUGH;
Kostya Serebryanya7398ba2016-08-17 18:10:42 +0000354 case 2:
355 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
356 break;
357 default: assert(0);
358 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000359 assert(NewSize > 0 && "CrossOver returned empty unit");
360 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
361 memcpy(Data, U.data(), NewSize);
362 return NewSize;
363}
364
Kostya Serebryany14c50282015-12-19 01:09:49 +0000365void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000366 CurrentMutatorSequence.clear();
367 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000368}
369
Kostya Serebryany4b358742016-01-14 02:36:44 +0000370// Copy successful dictionary entries to PersistentAutoDictionary.
371void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000372 for (auto DE : CurrentDictionaryEntrySequence) {
373 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000374 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000375 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000376 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
377 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000378 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000379}
380
381void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000382 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000383 for (auto &DE : PersistentAutoDictionary)
384 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000385 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000386 if (V.empty()) return;
387 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000388 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000389 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000390 PrintASCII(DE.GetW(), "\"");
391 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000392 }
393 Printf("###### End of recommended dictionary. ######\n");
394}
395
Kostya Serebryany14c50282015-12-19 01:09:49 +0000396void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000397 Printf("MS: %zd ", CurrentMutatorSequence.size());
398 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000399 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000400 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000401 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000402 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000403 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000404 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000405 }
406 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000407}
408
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000409size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000410 return MutateImpl(Data, Size, MaxSize, Mutators);
411}
412
413size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
414 size_t MaxSize) {
415 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
416}
417
418// Mutates Data in place, returns new size.
419size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
420 size_t MaxSize,
421 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000422 assert(MaxSize > 0);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000423 if (Size == 0) {
424 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000425 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000426 if (Options.OnlyASCII)
427 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000428 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000429 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000430 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000431 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
432 // in which case they will return 0.
433 // Try several times before returning un-mutated data.
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000434 for (int Iter = 0; Iter < 100; Iter++) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000435 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000436 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000437 if (NewSize && NewSize <= MaxSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000438 if (Options.OnlyASCII)
439 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000440 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000441 return NewSize;
442 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000443 }
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000444 return std::min(Size, MaxSize);
Aaron Ballmanef116982015-01-29 16:58:29 +0000445}
446
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000447void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000448 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000449 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000450}
451
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000452void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000453 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000454 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000455 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000456}
457
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000458void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000459 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000460}
461
Aaron Ballmanef116982015-01-29 16:58:29 +0000462} // namespace fuzzer