@@ -18,21 +18,30 @@ namespace fuzzer {
18
18
19
19
const size_t Dictionary::kMaxDictSize ;
20
20
21
- MutationDispatcher::Mutator MutationDispatcher::Mutators[] = {
22
- {&MutationDispatcher::Mutate_EraseByte, " EraseByte" },
23
- {&MutationDispatcher::Mutate_InsertByte, " InsertByte" },
24
- {&MutationDispatcher::Mutate_ChangeByte, " ChangeByte" },
25
- {&MutationDispatcher::Mutate_ChangeBit, " ChangeBit" },
26
- {&MutationDispatcher::Mutate_ShuffleBytes, " ShuffleBytes" },
27
- {&MutationDispatcher::Mutate_ChangeASCIIInteger, " ChangeASCIIInt" },
28
- {&MutationDispatcher::Mutate_CrossOver, " CrossOver" },
29
- {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
30
- " AddFromManualDict" },
31
- {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
32
- " AddFromTempAutoDict" },
33
- {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
34
- " AddFromPersAutoDict" },
35
- };
21
+ MutationDispatcher::MutationDispatcher (Random &Rand) : Rand(Rand) {
22
+ DefaultMutators.insert (
23
+ DefaultMutators.begin (),
24
+ {
25
+ {&MutationDispatcher::Mutate_EraseByte, " EraseByte" },
26
+ {&MutationDispatcher::Mutate_InsertByte, " InsertByte" },
27
+ {&MutationDispatcher::Mutate_ChangeByte, " ChangeByte" },
28
+ {&MutationDispatcher::Mutate_ChangeBit, " ChangeBit" },
29
+ {&MutationDispatcher::Mutate_ShuffleBytes, " ShuffleBytes" },
30
+ {&MutationDispatcher::Mutate_ChangeASCIIInteger, " ChangeASCIIInt" },
31
+ {&MutationDispatcher::Mutate_CrossOver, " CrossOver" },
32
+ {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
33
+ " AddFromManualDict" },
34
+ {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
35
+ " AddFromTempAutoDict" },
36
+ {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
37
+ " AddFromPersAutoDict" },
38
+ });
39
+
40
+ if (EF.LLVMFuzzerCustomMutator )
41
+ Mutators.push_back ({&MutationDispatcher::Mutate_Custom, " Custom" });
42
+ else
43
+ Mutators = DefaultMutators;
44
+ }
36
45
37
46
static char FlipRandomBit (char X, Random &Rand) {
38
47
int Bit = Rand (8 );
@@ -52,6 +61,11 @@ static char RandCh(Random &Rand) {
52
61
return Special[Rand (sizeof (Special) - 1 )];
53
62
}
54
63
64
+ size_t MutationDispatcher::Mutate_Custom (uint8_t *Data, size_t Size ,
65
+ size_t MaxSize) {
66
+ return EF.LLVMFuzzerCustomMutator (Data, Size , MaxSize, Rand.Rand ());
67
+ }
68
+
55
69
size_t MutationDispatcher::Mutate_ShuffleBytes (uint8_t *Data, size_t Size ,
56
70
size_t MaxSize) {
57
71
assert (Size );
@@ -230,8 +244,19 @@ void MutationDispatcher::PrintMutationSequence() {
230
244
}
231
245
}
232
246
233
- // Mutates Data in place, returns new size.
234
247
size_t MutationDispatcher::Mutate (uint8_t *Data, size_t Size , size_t MaxSize) {
248
+ return MutateImpl (Data, Size , MaxSize, Mutators);
249
+ }
250
+
251
+ size_t MutationDispatcher::DefaultMutate (uint8_t *Data, size_t Size ,
252
+ size_t MaxSize) {
253
+ return MutateImpl (Data, Size , MaxSize, DefaultMutators);
254
+ }
255
+
256
+ // Mutates Data in place, returns new size.
257
+ size_t MutationDispatcher::MutateImpl (uint8_t *Data, size_t Size ,
258
+ size_t MaxSize,
259
+ const std::vector<Mutator> &Mutators) {
235
260
assert (MaxSize > 0 );
236
261
assert (Size <= MaxSize);
237
262
if (Size == 0 ) {
@@ -244,9 +269,7 @@ size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
244
269
// in which case they will return 0.
245
270
// Try several times before returning un-mutated data.
246
271
for (int Iter = 0 ; Iter < 10 ; Iter++) {
247
- size_t NumMutators = sizeof (Mutators) / sizeof (Mutators[0 ]);
248
- size_t MutatorIdx = Rand (NumMutators);
249
- auto M = Mutators[MutatorIdx];
272
+ auto M = Mutators[Rand (Mutators.size ())];
250
273
size_t NewSize = (this ->*(M.Fn ))(Data, Size , MaxSize);
251
274
if (NewSize) {
252
275
CurrentMutatorSequence.push_back (M);
0 commit comments