[llvm] r260800 - [libFuzzer] simplify CTOR of MutationDispatcher

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 12 19:46:26 PST 2016


Author: kcc
Date: Fri Feb 12 21:46:26 2016
New Revision: 260800

URL: http://llvm.org/viewvc/llvm-project?rev=260800&view=rev
Log:
[libFuzzer] simplify CTOR of MutationDispatcher

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

Modified: llvm/trunk/lib/Fuzzer/FuzzerInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerInternal.h?rev=260800&r1=260799&r2=260800&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerInternal.h (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerInternal.h Fri Feb 12 21:46:26 2016
@@ -174,7 +174,7 @@ private:
 
 class MutationDispatcher {
 public:
-  MutationDispatcher(Random &Rand);
+  MutationDispatcher(Random &Rand) : Rand(Rand) {}
   ~MutationDispatcher() {}
   /// Indicate that we are about to start a new sequence of mutations.
   void StartMutationSequence();
@@ -236,7 +236,6 @@ private:
     const char *Name;
   };
 
-  void Add(Mutator M) { Mutators.push_back(M); }
   size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
                                size_t MaxSize);
 
@@ -249,10 +248,11 @@ private:
   // Persistent dictionary modified by the fuzzer, consists of
   // entries that led to successfull discoveries in the past mutations.
   Dictionary PersistentAutoDictionary;
-  std::vector<Mutator> Mutators;
   std::vector<Mutator> CurrentMutatorSequence;
   std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
   const std::vector<Unit> *Corpus = nullptr;
+
+  static Mutator Mutators[];
 };
 
 class Fuzzer {

Modified: llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp?rev=260800&r1=260799&r2=260800&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerMutate.cpp Fri Feb 12 21:46:26 2016
@@ -18,6 +18,22 @@ namespace fuzzer {
 
 const size_t Dictionary::kMaxDictSize;
 
+MutationDispatcher::Mutator MutationDispatcher::Mutators[] = {
+  {&MutationDispatcher::Mutate_EraseByte, "EraseByte"},
+  {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
+  {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
+  {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
+  {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
+  {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
+  {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
+  {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
+    "AddFromManualDict"},
+  {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
+    "AddFromTempAutoDict"},
+  {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
+    "AddFromPersAutoDict"},
+};
+
 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed) {
   Random R(Seed);
   MutationDispatcher MD(R);
@@ -233,7 +249,8 @@ size_t MutationDispatcher::Mutate(uint8_
   // 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(Mutators.size());
+    size_t NumMutators = sizeof(Mutators) / sizeof(Mutators[0]);
+    size_t MutatorIdx = Rand(NumMutators);
     auto M = Mutators[MutatorIdx];
     size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
     if (NewSize) {
@@ -260,21 +277,4 @@ void MutationDispatcher::ClearAutoDictio
   TempAutoDictionary.clear();
 }
 
-MutationDispatcher::MutationDispatcher(Random &Rand) : Rand(Rand) {
-    Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
-    Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
-    Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
-    Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
-    Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
-    Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
-    Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
-    Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
-         "AddFromManualDict"});
-    Add({&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
-         "AddFromTempAutoDict"});
-    Add({&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
-         "AddFromPersAutoDict"});
-}
-
-
 }  // namespace fuzzer




More information about the llvm-commits mailing list