llvm.org GIT mirror llvm / d1eacb8
[libFuzzer] simplify CTOR of MutationDispatcher git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260800 91177308-0d34-0410-b5e6-96231b3b80d8 Kostya Serebryany 4 years ago
2 changed file(s) with 21 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
173173
174174 class MutationDispatcher {
175175 public:
176 MutationDispatcher(Random &Rand);
176 MutationDispatcher(Random &Rand) : Rand(Rand) {}
177177 ~MutationDispatcher() {}
178178 /// Indicate that we are about to start a new sequence of mutations.
179179 void StartMutationSequence();
235235 const char *Name;
236236 };
237237
238 void Add(Mutator M) { Mutators.push_back(M); }
239238 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
240239 size_t MaxSize);
241240
248247 // Persistent dictionary modified by the fuzzer, consists of
249248 // entries that led to successfull discoveries in the past mutations.
250249 Dictionary PersistentAutoDictionary;
251 std::vector Mutators;
252250 std::vector CurrentMutatorSequence;
253251 std::vector CurrentDictionaryEntrySequence;
254252 const std::vector *Corpus = nullptr;
253
254 static Mutator Mutators[];
255255 };
256256
257257 class Fuzzer {
1616 namespace fuzzer {
1717
1818 const size_t Dictionary::kMaxDictSize;
19
20 MutationDispatcher::Mutator MutationDispatcher::Mutators[] = {
21 {&MutationDispatcher::Mutate_EraseByte, "EraseByte"},
22 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
23 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
24 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
25 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
26 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
27 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
28 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
29 "AddFromManualDict"},
30 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
31 "AddFromTempAutoDict"},
32 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
33 "AddFromPersAutoDict"},
34 };
1935
2036 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed) {
2137 Random R(Seed);
232248 // in which case they will return 0.
233249 // Try several times before returning un-mutated data.
234250 for (int Iter = 0; Iter < 10; Iter++) {
235 size_t MutatorIdx = Rand(Mutators.size());
251 size_t NumMutators = sizeof(Mutators) / sizeof(Mutators[0]);
252 size_t MutatorIdx = Rand(NumMutators);
236253 auto M = Mutators[MutatorIdx];
237254 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
238255 if (NewSize) {
259276 TempAutoDictionary.clear();
260277 }
261278
262 MutationDispatcher::MutationDispatcher(Random &Rand) : Rand(Rand) {
263 Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
264 Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
265 Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
266 Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
267 Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
268 Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
269 Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
270 Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
271 "AddFromManualDict"});
272 Add({&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
273 "AddFromTempAutoDict"});
274 Add({&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
275 "AddFromPersAutoDict"});
276 }
277
278
279279 } // namespace fuzzer