1 //===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Mutate a test input.
10 //===----------------------------------------------------------------------===//
12 #include "FuzzerCorpus.h"
13 #include "FuzzerDefs.h"
14 #include "FuzzerExtFunctions.h"
16 #include "FuzzerMutate.h"
17 #include "FuzzerOptions.h"
21 const size_t Dictionary::kMaxDictSize;
23 static void PrintASCII(const Word &W, const char *PrintAfter) {
24 PrintASCII(W.data(), W.size(), PrintAfter);
27 MutationDispatcher::MutationDispatcher(Random &Rand,
28 const FuzzingOptions &Options)
29 : Rand(Rand), Options(Options) {
30 DefaultMutators.insert(
31 DefaultMutators.begin(),
33 {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
34 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
35 {&MutationDispatcher::Mutate_InsertRepeatedBytes,
36 "InsertRepeatedBytes"},
37 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
38 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
39 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
40 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
41 {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
42 {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
43 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
44 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
46 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
48 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
52 DefaultMutators.push_back(
53 {&MutationDispatcher::Mutate_AddWordFromTORC, "CMP"});
55 if (EF->LLVMFuzzerCustomMutator)
56 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
58 Mutators = DefaultMutators;
60 if (EF->LLVMFuzzerCustomCrossOver)
62 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
65 static char RandCh(Random &Rand) {
66 if (Rand.RandBool()) return Rand(256);
67 const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
68 return Special[Rand(sizeof(Special) - 1)];
71 size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
73 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
76 size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
78 if (!Corpus || Corpus->size() < 2 || Size == 0)
80 size_t Idx = Rand(Corpus->size());
81 const Unit &Other = (*Corpus)[Idx];
84 CustomCrossOverInPlaceHere.resize(MaxSize);
85 auto &U = CustomCrossOverInPlaceHere;
86 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
87 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
90 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
91 memcpy(Data, U.data(), NewSize);
95 size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
97 if (Size > MaxSize || Size == 0) return 0;
98 size_t ShuffleAmount =
99 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
100 size_t ShuffleStart = Rand(Size - ShuffleAmount);
101 assert(ShuffleStart + ShuffleAmount <= Size);
102 std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);
106 size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
108 if (Size <= 1) return 0;
109 size_t N = Rand(Size / 2) + 1;
111 size_t Idx = Rand(Size - N + 1);
112 // Erase Data[Idx:Idx+N].
113 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
114 // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
118 size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
120 if (Size >= MaxSize) return 0;
121 size_t Idx = Rand(Size + 1);
122 // Insert new value at Data[Idx].
123 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
124 Data[Idx] = RandCh(Rand);
128 size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
131 const size_t kMinBytesToInsert = 3;
132 if (Size + kMinBytesToInsert >= MaxSize) return 0;
133 size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
134 size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
135 assert(Size + N <= MaxSize && N);
136 size_t Idx = Rand(Size + 1);
137 // Insert new values at Data[Idx].
138 memmove(Data + Idx + N, Data + Idx, Size - Idx);
139 // Give preference to 0x00 and 0xff.
140 uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
141 for (size_t i = 0; i < N; i++)
142 Data[Idx + i] = Byte;
146 size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
148 if (Size > MaxSize) return 0;
149 size_t Idx = Rand(Size);
150 Data[Idx] = RandCh(Rand);
154 size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
156 if (Size > MaxSize) return 0;
157 size_t Idx = Rand(Size);
158 Data[Idx] ^= 1 << Rand(8);
162 size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
165 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
168 size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
169 uint8_t *Data, size_t Size, size_t MaxSize) {
170 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
173 size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data, size_t Size,
175 DictionaryEntry &DE) {
176 const Word &W = DE.GetW();
177 bool UsePositionHint = DE.HasPositionHint() &&
178 DE.GetPositionHint() + W.size() < Size &&
180 if (Rand.RandBool()) { // Insert W.
181 if (Size + W.size() > MaxSize) return 0;
182 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
183 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
184 memcpy(Data + Idx, W.data(), W.size());
186 } else { // Overwrite some bytes with W.
187 if (W.size() > Size) return 0;
188 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
189 memcpy(Data + Idx, W.data(), W.size());
194 // Somewhere in the past we have observed a comparison instructions
195 // with arguments Arg1 Arg2. This function tries to guess a dictionary
196 // entry that will satisfy that comparison.
197 // It first tries to find one of the arguments (possibly swapped) in the
198 // input and if it succeeds it creates a DE with a position hint.
199 // Otherwise it creates a DE with one of the arguments w/o a position hint.
200 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
201 const void *Arg1, const void *Arg2,
202 const void *Arg1Mutation, const void *Arg2Mutation,
203 size_t ArgSize, const uint8_t *Data,
205 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
206 bool HandleFirst = Rand.RandBool();
207 const void *ExistingBytes, *DesiredBytes;
209 const uint8_t *End = Data + Size;
210 for (int Arg = 0; Arg < 2; Arg++) {
211 ExistingBytes = HandleFirst ? Arg1 : Arg2;
212 DesiredBytes = HandleFirst ? Arg2Mutation : Arg1Mutation;
213 HandleFirst = !HandleFirst;
214 W.Set(reinterpret_cast<const uint8_t*>(DesiredBytes), ArgSize);
215 const size_t kMaxNumPositions = 8;
216 size_t Positions[kMaxNumPositions];
217 size_t NumPositions = 0;
218 for (const uint8_t *Cur = Data;
219 Cur < End && NumPositions < kMaxNumPositions; Cur++) {
221 (const uint8_t *)SearchMemory(Cur, End - Cur, ExistingBytes, ArgSize);
223 Positions[NumPositions++] = Cur - Data;
225 if (!NumPositions) continue;
226 return DictionaryEntry(W, Positions[Rand(NumPositions)]);
228 DictionaryEntry DE(W);
234 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
235 T Arg1, T Arg2, const uint8_t *Data, size_t Size) {
236 if (Rand.RandBool()) Arg1 = Bswap(Arg1);
237 if (Rand.RandBool()) Arg2 = Bswap(Arg2);
238 T Arg1Mutation = Arg1 + Rand(-1, 1);
239 T Arg2Mutation = Arg2 + Rand(-1, 1);
240 return MakeDictionaryEntryFromCMP(&Arg1, &Arg2, &Arg1Mutation, &Arg2Mutation,
241 sizeof(Arg1), Data, Size);
244 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
245 const Word &Arg1, const Word &Arg2, const uint8_t *Data, size_t Size) {
246 return MakeDictionaryEntryFromCMP(Arg1.data(), Arg2.data(), Arg1.data(),
247 Arg2.data(), Arg1.size(), Data, Size);
250 size_t MutationDispatcher::Mutate_AddWordFromTORC(
251 uint8_t *Data, size_t Size, size_t MaxSize) {
256 auto X = TPC.TORC8.Get(Rand.Rand());
257 DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
260 auto X = TPC.TORC4.Get(Rand.Rand());
261 if ((X.A >> 16) == 0 && (X.B >> 16) == 0 && Rand.RandBool())
262 DE = MakeDictionaryEntryFromCMP((uint16_t)X.A, (uint16_t)X.B, Data, Size);
264 DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
267 auto X = TPC.TORCW.Get(Rand.Rand());
268 DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
273 if (!DE.GetW().size()) return 0;
274 Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
276 DictionaryEntry &DERef =
277 CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ %
278 kCmpDictionaryEntriesDequeSize];
280 CurrentDictionaryEntrySequence.push_back(&DERef);
284 size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
285 uint8_t *Data, size_t Size, size_t MaxSize) {
286 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
289 size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
290 size_t Size, size_t MaxSize) {
291 if (Size > MaxSize) return 0;
292 if (D.empty()) return 0;
293 DictionaryEntry &DE = D[Rand(D.size())];
294 Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
297 CurrentDictionaryEntrySequence.push_back(&DE);
301 // Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
303 size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
304 uint8_t *To, size_t ToSize) {
305 // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
306 size_t ToBeg = Rand(ToSize);
307 size_t CopySize = Rand(ToSize - ToBeg) + 1;
308 assert(ToBeg + CopySize <= ToSize);
309 CopySize = std::min(CopySize, FromSize);
310 size_t FromBeg = Rand(FromSize - CopySize + 1);
311 assert(FromBeg + CopySize <= FromSize);
312 memmove(To + ToBeg, From + FromBeg, CopySize);
316 // Inserts part of From[0,ToSize) into To.
317 // Returns new size of To on success or 0 on failure.
318 size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
319 uint8_t *To, size_t ToSize,
321 if (ToSize >= MaxToSize) return 0;
322 size_t AvailableSpace = MaxToSize - ToSize;
323 size_t MaxCopySize = std::min(AvailableSpace, FromSize);
324 size_t CopySize = Rand(MaxCopySize) + 1;
325 size_t FromBeg = Rand(FromSize - CopySize + 1);
326 assert(FromBeg + CopySize <= FromSize);
327 size_t ToInsertPos = Rand(ToSize + 1);
328 assert(ToInsertPos + CopySize <= MaxToSize);
329 size_t TailSize = ToSize - ToInsertPos;
331 MutateInPlaceHere.resize(MaxToSize);
332 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
333 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
334 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
336 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
337 memmove(To + ToInsertPos, From + FromBeg, CopySize);
339 return ToSize + CopySize;
342 size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
344 if (Size > MaxSize || Size == 0) return 0;
346 return CopyPartOf(Data, Size, Data, Size);
348 return InsertPartOf(Data, Size, Data, Size, MaxSize);
351 size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
353 if (Size > MaxSize) return 0;
354 size_t B = Rand(Size);
355 while (B < Size && !isdigit(Data[B])) B++;
356 if (B == Size) return 0;
358 while (E < Size && isdigit(Data[E])) E++;
360 // now we have digits in [B, E).
361 // strtol and friends don't accept non-zero-teminated data, parse it manually.
362 uint64_t Val = Data[B] - '0';
363 for (size_t i = B + 1; i < E; i++)
364 Val = Val * 10 + Data[i] - '0';
366 // Mutate the integer value.
368 case 0: Val++; break;
369 case 1: Val--; break;
370 case 2: Val /= 2; break;
371 case 3: Val *= 2; break;
372 case 4: Val = Rand(Val * Val); break;
375 // Just replace the bytes with the new ones, don't bother moving bytes.
376 for (size_t i = B; i < E; i++) {
377 size_t Idx = E + B - i - 1;
378 assert(Idx >= B && Idx < E);
379 Data[Idx] = (Val % 10) + '0';
386 size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
387 if (Size < sizeof(T)) return 0;
388 size_t Off = Rand(Size - sizeof(T) + 1);
389 assert(Off + sizeof(T) <= Size);
391 if (Off < 64 && !Rand(4)) {
396 memcpy(&Val, Data + Off, sizeof(Val));
400 Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
402 Val = Val + Add; // Add assuming current endiannes.
403 if (Add == 0 || Rand.RandBool()) // Maybe negate.
406 memcpy(Data + Off, &Val, sizeof(Val));
410 size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
413 if (Size > MaxSize) return 0;
415 case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
416 case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
417 case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
418 case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
424 size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
426 if (Size > MaxSize) return 0;
427 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
428 size_t Idx = Rand(Corpus->size());
429 const Unit &O = (*Corpus)[Idx];
430 if (O.empty()) return 0;
431 MutateInPlaceHere.resize(MaxSize);
432 auto &U = MutateInPlaceHere;
436 NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
439 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
441 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
444 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
448 assert(NewSize > 0 && "CrossOver returned empty unit");
449 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
450 memcpy(Data, U.data(), NewSize);
454 void MutationDispatcher::StartMutationSequence() {
455 CurrentMutatorSequence.clear();
456 CurrentDictionaryEntrySequence.clear();
459 // Copy successful dictionary entries to PersistentAutoDictionary.
460 void MutationDispatcher::RecordSuccessfulMutationSequence() {
461 for (auto DE : CurrentDictionaryEntrySequence) {
462 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
463 DE->IncSuccessCount();
464 assert(DE->GetW().size());
465 // Linear search is fine here as this happens seldom.
466 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
467 PersistentAutoDictionary.push_back({DE->GetW(), 1});
471 void MutationDispatcher::PrintRecommendedDictionary() {
472 std::vector<DictionaryEntry> V;
473 for (auto &DE : PersistentAutoDictionary)
474 if (!ManualDictionary.ContainsWord(DE.GetW()))
476 if (V.empty()) return;
477 Printf("###### Recommended dictionary. ######\n");
479 assert(DE.GetW().size());
481 PrintASCII(DE.GetW(), "\"");
482 Printf(" # Uses: %zd\n", DE.GetUseCount());
484 Printf("###### End of recommended dictionary. ######\n");
487 void MutationDispatcher::PrintMutationSequence() {
488 Printf("MS: %zd ", CurrentMutatorSequence.size());
489 for (auto M : CurrentMutatorSequence)
490 Printf("%s-", M.Name);
491 if (!CurrentDictionaryEntrySequence.empty()) {
493 for (auto DE : CurrentDictionaryEntrySequence) {
495 PrintASCII(DE->GetW(), "\"-");
500 size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
501 return MutateImpl(Data, Size, MaxSize, Mutators);
504 size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
506 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
509 // Mutates Data in place, returns new size.
510 size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
512 const std::vector<Mutator> &Mutators) {
514 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
515 // in which case they will return 0.
516 // Try several times before returning un-mutated data.
517 for (int Iter = 0; Iter < 100; Iter++) {
518 auto M = Mutators[Rand(Mutators.size())];
519 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
520 if (NewSize && NewSize <= MaxSize) {
521 if (Options.OnlyASCII)
522 ToASCII(Data, NewSize);
523 CurrentMutatorSequence.push_back(M);
528 return 1; // Fallback, should not happen frequently.
531 void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
532 ManualDictionary.push_back(
533 {W, std::numeric_limits<size_t>::max()});
536 void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
537 static const size_t kMaxAutoDictSize = 1 << 14;
538 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
539 TempAutoDictionary.push_back(DE);
542 void MutationDispatcher::ClearAutoDictionary() {
543 TempAutoDictionary.clear();
546 } // namespace fuzzer