From abf7df0972ad5feb08194f8a51ae5dede21cec47 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Sat, 13 Feb 2016 03:46:26 +0000 Subject: [PATCH] [libFuzzer] simplify CTOR of MutationDispatcher llvm-svn: 260800 --- lib/Fuzzer/FuzzerInternal.h | 6 +++--- lib/Fuzzer/FuzzerMutate.cpp | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/Fuzzer/FuzzerInternal.h b/lib/Fuzzer/FuzzerInternal.h index 3ac9ea63603..ded25da847d 100644 --- a/lib/Fuzzer/FuzzerInternal.h +++ b/lib/Fuzzer/FuzzerInternal.h @@ -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 Mutators; std::vector CurrentMutatorSequence; std::vector CurrentDictionaryEntrySequence; const std::vector *Corpus = nullptr; + + static Mutator Mutators[]; }; class Fuzzer { diff --git a/lib/Fuzzer/FuzzerMutate.cpp b/lib/Fuzzer/FuzzerMutate.cpp index ff099f17636..b3442219b16 100644 --- a/lib/Fuzzer/FuzzerMutate.cpp +++ b/lib/Fuzzer/FuzzerMutate.cpp @@ -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_t *Data, size_t Size, size_t 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(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::ClearAutoDictionary() { 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