[llvm] r244188 - [libFuzzer] add one more mutation strategy: byte shuffling

Kostya Serebryany kcc at google.com
Wed Aug 5 18:29:14 PDT 2015


Author: kcc
Date: Wed Aug  5 20:29:13 2015
New Revision: 244188

URL: http://llvm.org/viewvc/llvm-project?rev=244188&view=rev
Log:
[libFuzzer] add one more mutation strategy: byte shuffling

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerInterface.h
    llvm/trunk/lib/Fuzzer/FuzzerInternal.h
    llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp
    llvm/trunk/lib/Fuzzer/test/FuzzerUnittest.cpp

Modified: llvm/trunk/lib/Fuzzer/FuzzerInterface.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInterface.h?rev=244188&r1=244187&r2=244188&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInterface.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInterface.h Wed Aug  5 20:29:13 2015
@@ -50,7 +50,7 @@ class FuzzerRandomBase {
   // Return a random number.
   virtual size_t Rand() = 0;
   // Return a random number in range [0,n).
-  size_t operator()(size_t n) { return Rand() % n; }
+  size_t operator()(size_t n) { return n ? Rand() % n : 0; }
   bool RandBool() { return Rand() % 2; }
 };
 

Modified: llvm/trunk/lib/Fuzzer/FuzzerInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInternal.h?rev=244188&r1=244187&r2=244188&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInternal.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInternal.h Wed Aug  5 20:29:13 2015
@@ -33,6 +33,8 @@ void CopyFileToErr(const std::string &Pa
 std::string DirPlusFile(const std::string &DirPath,
                         const std::string &FileName);
 
+size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize,
+                           FuzzerRandomBase &Rand);
 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
                         FuzzerRandomBase &Rand);
 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize,

Modified: llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp?rev=244188&r1=244187&r2=244188&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp Wed Aug  5 20:29:13 2015
@@ -13,6 +13,8 @@
 
 #include "FuzzerInternal.h"
 
+#include <algorithm>
+
 namespace fuzzer {
 
 static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
@@ -33,6 +35,17 @@ static char RandCh(FuzzerRandomBase &Ran
   return Special[Rand(sizeof(Special) - 1)];
 }
 
+size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize,
+                           FuzzerRandomBase &Rand) {
+  assert(Size);
+  size_t ShuffleAmount = Rand(std::min(Size, 8UL)) + 1;  // [1,8] and <= Size.
+  size_t ShuffleStart = Rand(Size - ShuffleAmount);
+  assert(ShuffleStart + ShuffleAmount <= Size);
+  std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
+                      Rand);
+  return Size;
+}
+
 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
                         FuzzerRandomBase &Rand) {
   assert(Size);
@@ -78,11 +91,12 @@ size_t Mutate(uint8_t *Data, size_t Size
     return MaxSize;
   }
   assert(Size > 0);
-  switch (Rand(4)) {
+  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;
   }
   assert(Size > 0);
   return Size;

Modified: llvm/trunk/lib/Fuzzer/test/FuzzerUnittest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/test/FuzzerUnittest.cpp?rev=244188&r1=244187&r2=244188&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/test/FuzzerUnittest.cpp (original)
+++ llvm/trunk/lib/Fuzzer/test/FuzzerUnittest.cpp Wed Aug  5 20:29:13 2015
@@ -192,3 +192,26 @@ void TestChangeBit(Mutator M, int NumIte
 
 TEST(FuzzerMutate, ChangeBit1) { TestChangeBit(Mutate_ChangeBit, 1 << 16); }
 TEST(FuzzerMutate, ChangeBit2) { TestChangeBit(Mutate, 1 << 18); }
+
+void TestShuffleBytes(Mutator M, int NumIter) {
+  FuzzerRandomLibc Rand(0);
+  int FoundMask = 0;
+  uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66};
+  uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66};
+  uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66};
+  uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33};
+  uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66};
+  for (int i = 0; i < NumIter; i++) {
+    uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+    size_t NewSize = M(T, 7, 7, Rand);
+    if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
+    if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
+    if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
+    if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
+    if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4;
+  }
+  EXPECT_EQ(FoundMask, 31);
+}
+
+TEST(FuzzerMutate, ShuffleBytes1) { TestShuffleBytes(Mutate_ShuffleBytes, 1 << 15); }
+TEST(FuzzerMutate, ShuffleBytes2) { TestShuffleBytes(Mutate, 1 << 16); }




More information about the llvm-commits mailing list