[libFuzzer] when a single mutation fails try a few more times with other mutations...
[oota-llvm.git] / lib / Fuzzer / FuzzerMutate.cpp
index c425ae0c1cdd970f6316d0aa46dac7d41b89e062..9b2015be8691227f2cbc912473f92854a0702bbc 100644 (file)
 
 namespace fuzzer {
 
+typedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size,
+                                              size_t Max);
+
+struct MutationDispatcher::Impl {
+  std::vector<Unit> Dictionary;
+  std::vector<Mutator> Mutators;
+  Impl() {
+    Mutators.push_back(&MutationDispatcher::Mutate_EraseByte);
+    Mutators.push_back(&MutationDispatcher::Mutate_InsertByte);
+    Mutators.push_back(&MutationDispatcher::Mutate_ChangeByte);
+    Mutators.push_back(&MutationDispatcher::Mutate_ChangeBit);
+    Mutators.push_back(&MutationDispatcher::Mutate_ShuffleBytes);
+  }
+  void AddWordToDictionary(const uint8_t *Word, size_t Size) {
+    if (Dictionary.empty()) {
+      Mutators.push_back(&MutationDispatcher::Mutate_AddWordFromDictionary);
+    }
+    Dictionary.push_back(Unit(Word, Word + Size));
+  }
+};
+
+
 static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
   int Bit = Rand(8);
   char Mask = 1 << Bit;
@@ -35,8 +57,8 @@ static char RandCh(FuzzerRandomBase &Rand) {
   return Special[Rand(sizeof(Special) - 1)];
 }
 
-size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize,
-                           FuzzerRandomBase &Rand) {
+size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
+                                               size_t MaxSize) {
   assert(Size);
   size_t ShuffleAmount = Rand(std::min(Size, 8UL)) + 1;  // [1,8] and <= Size.
   size_t ShuffleStart = Rand(Size - ShuffleAmount);
@@ -46,19 +68,19 @@ size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize,
   return Size;
 }
 
-size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
-                        FuzzerRandomBase &Rand) {
+size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
+                                            size_t MaxSize) {
   assert(Size);
-  if (Size == 1) return Size;
+  if (Size == 1) return 0;
   size_t Idx = Rand(Size);
   // Erase Data[Idx].
   memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
   return Size - 1;
 }
 
-size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize,
-                         FuzzerRandomBase &Rand) {
-  if (Size == MaxSize) return Size;
+size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
+                                             size_t MaxSize) {
+  if (Size == MaxSize) return 0;
   size_t Idx = Rand(Size + 1);
   // Insert new value at Data[Idx].
   memmove(Data + Idx + 1, Data + Idx, Size - Idx);
@@ -66,23 +88,36 @@ size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize,
   return Size + 1;
 }
 
-size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize,
-                         FuzzerRandomBase &Rand) {
+size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
+                                             size_t MaxSize) {
   size_t Idx = Rand(Size);
   Data[Idx] = RandCh(Rand);
   return Size;
 }
 
-size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize,
-                        FuzzerRandomBase &Rand) {
+size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
+                                            size_t MaxSize) {
   size_t Idx = Rand(Size);
   Data[Idx] = FlipRandomBit(Data[Idx], Rand);
   return Size;
 }
 
+size_t MutationDispatcher::Mutate_AddWordFromDictionary(uint8_t *Data,
+                                                        size_t Size,
+                                                        size_t MaxSize) {
+  auto &D = MDImpl->Dictionary;
+  assert(!D.empty());
+  if (D.empty()) return 0;
+  const Unit &Word = D[Rand(D.size())];
+  if (Size + Word.size() > MaxSize) return 0;
+  size_t Idx = Rand(Size + 1);
+  memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx);
+  memcpy(Data + Idx, Word.data(), Word.size());
+  return Size + Word.size();
+}
+
 // Mutates Data in place, returns new size.
-size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
-              FuzzerRandomBase &Rand) {
+size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
   assert(MaxSize > 0);
   assert(Size <= MaxSize);
   if (Size == 0) {
@@ -91,15 +126,26 @@ size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
     return MaxSize;
   }
   assert(Size > 0);
-  switch (Rand(5)) {
-  case 0: Size = Mutate_EraseByte(Data, Size, MaxSize, Rand); break;
-  case 1: Size = Mutate_InsertByte(Data, Size, MaxSize, Rand); break;
-  case 2: Size = Mutate_ChangeByte(Data, Size, MaxSize, Rand); break;
-  case 3: Size = Mutate_ChangeBit(Data, Size, MaxSize, Rand); break;
-  case 4: Size = Mutate_ShuffleBytes(Data, Size, MaxSize, Rand); break;
+  // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
+  // in which case they will return 0.
+  // Try several times before returning un-mutated data.
+  for (int Iter = 0; Iter < 10; Iter++) {
+    size_t MutatorIdx = Rand(MDImpl->Mutators.size());
+    size_t NewSize =
+        (this->*(MDImpl->Mutators[MutatorIdx]))(Data, Size, MaxSize);
+    if (NewSize) return NewSize;
   }
-  assert(Size > 0);
   return Size;
 }
 
+void MutationDispatcher::AddWordToDictionary(const uint8_t *Word, size_t Size) {
+  MDImpl->AddWordToDictionary(Word, Size);
+}
+
+MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
+  MDImpl = new Impl;
+}
+
+MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
+
 }  // namespace fuzzer