1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
use std::any::Any;
use std::cmp::min;
use std::ops::Not;

use libafl::mutators::mutations::{
    BitFlipMutator, ByteAddMutator, ByteDecMutator, ByteFlipMutator, ByteIncMutator,
    ByteInterestingMutator, ByteNegMutator, ByteRandMutator, BytesCopyMutator, BytesDeleteMutator,
    BytesExpandMutator, BytesInsertCopyMutator, BytesInsertMutator, BytesRandInsertMutator,
    BytesRandSetMutator, BytesSetMutator, BytesSwapMutator, DwordAddMutator,
    DwordInterestingMutator, QwordAddMutator, WordAddMutator, WordInterestingMutator,
};
use libafl::prelude::*;
use libafl_bolts::bolts_prelude::Merge;
use libafl_bolts::prelude::{tuple_list, tuple_list_type};
use libafl_bolts::rands::Rand;
use libafl_bolts::Named;

use super::utils::{
    choose_filtered, choose_term_path_filtered, find_term_mut, TermConstraints, TracePath,
};
use crate::algebra::{Term, TermType};
use crate::fuzzer::utils::choose_term_filtered_mut;
use crate::protocol::{ProtocolBehavior, ProtocolTypes};
use crate::trace::{ConfigTrace, Spawner, Trace, TraceContext};

/* List from: https://docs.rs/libafl/latest/src/libafl/mutators/havoc_mutations.rs.html#60
    BitFlipMutator,
    ByteFlipMutator,
    ByteIncMutator,
    ByteDecMutator,
    ByteNegMutator,
    ByteRandMutator,
    ByteAddMutator,
    WordAddMutator,
    DwordAddMutator,
    QwordAddMutator,
    ByteInterestingMutator,
    WordInterestingMutator,
    DwordInterestingMutator,
    BytesDeleteMutator,
    BytesDeleteMutator,
    BytesDeleteMutator,
    BytesDeleteMutator,
    BytesExpandMutator,
    BytesInsertMutator,
    BytesRandInsertMutator,
    BytesSetMutator,
    BytesRandSetMutator,
    BytesCopyMutator,
    BytesInsertCopyMutator,
    BytesSwapMutator,
    CrossoverInsertMutator,
    CrossoverReplaceMutator,
*/

pub type HavocMutationsTypeDY<S> = tuple_list_type!(
    BitFlipMutatorDY<S>,
    ByteFlipMutatorDY<S>,
    ByteIncMutatorDY<S>,
    ByteDecMutatorDY<S>,
    ByteNegMutatorDY<S>,
    ByteRandMutatorDY<S>,
    ByteAddMutatorDY<S>,
    WordAddMutatorDY<S>,
    DwordAddMutatorDY<S>,
    QwordAddMutatorDY<S>,
    ByteInterestingMutatorDY<S>,
    WordInterestingMutatorDY<S>,
    DwordInterestingMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesExpandMutatorDY<S>,
    BytesLargeExpandMutatorDY<S>, // NEW! Different from classical havoc!!
    BytesInsertMutatorDY<S>,
    BytesRandInsertMutatorDY<S>,
    BytesSetMutatorDY<S>,
    BytesRandSetMutatorDY<S>,
    BytesCopyMutatorDY<S>,
    BytesInsertCopyMutatorDY<S>,
    BytesSwapMutatorDY<S>,
    CrossoverInsertMutatorDY<S>,
    CrossoverReplaceMutatorDY<S>,
    SpliceMutatorDY<S>,
);

pub type BitMutations<'harness, PB, S> = tuple_list_type!(
    MakeMessage<'harness, PB>,
    ReadMessage<'harness, PB>,
    BitFlipMutatorDY<S>,
    ByteFlipMutatorDY<S>,
    ByteIncMutatorDY<S>,
    ByteDecMutatorDY<S>,
    ByteNegMutatorDY<S>,
    ByteRandMutatorDY<S>,
    ByteAddMutatorDY<S>,
    WordAddMutatorDY<S>,
    DwordAddMutatorDY<S>,
    QwordAddMutatorDY<S>,
    ByteInterestingMutatorDY<S>,
    WordInterestingMutatorDY<S>,
    DwordInterestingMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesExpandMutatorDY<S>,
    BytesLargeExpandMutatorDY<S>,
    BytesInsertMutatorDY<S>,
    BytesRandInsertMutatorDY<S>,
    BytesSetMutatorDY<S>,
    BytesRandSetMutatorDY<S>,
    BytesCopyMutatorDY<S>,
    BytesInsertCopyMutatorDY<S>,
    BytesSwapMutatorDY<S>,
    CrossoverInsertMutatorDY<S>,
    CrossoverReplaceMutatorDY<S>,
    SpliceMutatorDY<S>,
);

pub type AllMutations<'harness, PT, PB, S> = tuple_list_type!(
    RepeatMutator<S>,
    SkipMutator<S>,
    ReplaceReuseMutator<S>,
    ReplaceMatchMutator<S, PT>,
    RemoveAndLiftMutator<S>,
    GenerateMutator<'harness, S, PB>,
    SwapMutator<S>,
    MakeMessage<'harness, PB>,
    ReadMessage<'harness, PB>,
    BitFlipMutatorDY<S>,
    ByteFlipMutatorDY<S>,
    ByteIncMutatorDY<S>,
    ByteDecMutatorDY<S>,
    ByteNegMutatorDY<S>,
    ByteRandMutatorDY<S>,
    ByteAddMutatorDY<S>,
    WordAddMutatorDY<S>,
    DwordAddMutatorDY<S>,
    QwordAddMutatorDY<S>,
    ByteInterestingMutatorDY<S>,
    WordInterestingMutatorDY<S>,
    DwordInterestingMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesDeleteMutatorDY<S>,
    BytesExpandMutatorDY<S>,
    BytesLargeExpandMutatorDY<S>,
    BytesInsertMutatorDY<S>,
    BytesRandInsertMutatorDY<S>,
    BytesSetMutatorDY<S>,
    BytesRandSetMutatorDY<S>,
    BytesCopyMutatorDY<S>,
    BytesInsertCopyMutatorDY<S>,
    BytesSwapMutatorDY<S>,
    CrossoverInsertMutatorDY<S>,
    CrossoverReplaceMutatorDY<S>,
    SpliceMutatorDY<S>,
);

#[must_use]
pub fn havoc_mutations_dy<S: HasRand + HasMaxSize + HasCorpus>(
    mutation_config: MutationConfig,
) -> HavocMutationsTypeDY<S> {
    tuple_list!(
        BitFlipMutatorDY::new(mutation_config),
        ByteFlipMutatorDY::new(mutation_config),
        ByteIncMutatorDY::new(mutation_config),
        ByteDecMutatorDY::new(mutation_config),
        ByteNegMutatorDY::new(mutation_config),
        ByteRandMutatorDY::new(mutation_config),
        ByteAddMutatorDY::new(mutation_config),
        WordAddMutatorDY::new(mutation_config),
        DwordAddMutatorDY::new(mutation_config),
        QwordAddMutatorDY::new(mutation_config),
        ByteInterestingMutatorDY::new(mutation_config),
        WordInterestingMutatorDY::new(mutation_config),
        DwordInterestingMutatorDY::new(mutation_config),
        BytesDeleteMutatorDY::new(mutation_config),
        BytesDeleteMutatorDY::new(mutation_config),
        BytesDeleteMutatorDY::new(mutation_config),
        BytesDeleteMutatorDY::new(mutation_config),
        BytesExpandMutatorDY::new(mutation_config),
        BytesLargeExpandMutatorDY::new(mutation_config),
        BytesInsertMutatorDY::new(mutation_config),
        BytesRandInsertMutatorDY::new(mutation_config),
        BytesSetMutatorDY::new(mutation_config),
        BytesRandSetMutatorDY::new(mutation_config),
        BytesCopyMutatorDY::new(mutation_config),
        BytesInsertCopyMutatorDY::new(mutation_config),
        BytesSwapMutatorDY::new(mutation_config),
        CrossoverInsertMutatorDY::new(mutation_config),
        CrossoverReplaceMutatorDY::new(mutation_config),
        SpliceMutatorDY::new(mutation_config),
    )
}

#[must_use]
pub fn bit_mutations_dy<S: HasRand + HasMaxSize + HasCorpus, PB>(
    mutation_config: MutationConfig,
    put_registry: &PutRegistry<PB>,
) -> BitMutations<PB, S>
where
    PB: ProtocolBehavior,
{
    tuple_list!(
        MakeMessage::new(mutation_config, put_registry),
        ReadMessage::new(mutation_config, put_registry)
    )
    .merge(havoc_mutations_dy(mutation_config))
}

pub fn all_mutations<'harness, S, PT: ProtocolTypes, PB>(
    mutation_config: MutationConfig,
    signature: &'static Signature<PT>,
    put_registry: &'harness PutRegistry<PB>,
) -> AllMutations<'harness, PT, PB, S>
where
    S: HasCorpus + HasMetadata + HasMaxSize + HasRand,
    PB: ProtocolBehavior<ProtocolTypes = PT>,
{
    dy_mutations(mutation_config, signature, put_registry)
        .merge(bit_mutations_dy(mutation_config, put_registry))
}

// --------------------------------------------------------------------------------------------------
// MakeMessage mutation
// --------------------------------------------------------------------------------------------------

/// MAKE MESSAGE: transforms a sub term into a message which can then be mutated using havoc
pub struct MakeMessage<'a, PB> {
    put_registry: &'a PutRegistry<PB>,
    config: MutationConfig,
}

impl<'a, PB> MakeMessage<'a, PB> {
    #[must_use]
    pub const fn new(config: MutationConfig, put_registry: &'a PutRegistry<PB>) -> Self {
        Self {
            config,
            put_registry,
        }
    }
}

/// `MakeMessage` on the term at path `path` in `tr`.
fn make_message_term<PT: ProtocolTypes, PB: ProtocolBehavior<ProtocolTypes = PT>>(
    tr: &mut Trace<PT>,
    path: &TracePath,
    ctx: &mut TraceContext<PB>,
) -> Result<(), crate::error::Error>
where
    PB: ProtocolBehavior<ProtocolTypes = PT>,
{
    log::debug!("make_message_term: executing until path.0: {}", path.0);
    // Only execute shorter trace: trace[0..step_index])
    // execute the PUT on the first step_index steps and store the resulting trace context
    tr.execute_until_step(ctx, path.0, &mut 0).err().map(|e| {
        // 20% to 50% MakeMessage mutations fail, so this is a bit costly :(
        // TODO: we could memoize the term evaluation in a Option<ConcreteMessage> and use that here
        log::debug!("mutation::MakeMessage trace is not executable until step {},\
            could only happen if this mutation is scheduled with other mutations that create a non-executable trace.\
            Error: {e}", path.0);
        log::trace!("{}", &tr);
        log::debug!("       Skipped MakeMessage");
        Ok::<MutationResult, Error>(MutationResult::Skipped)
    });

    let t = find_term_mut(tr, path).expect("make_message_term - Should never happen.");
    t.make_payload(ctx)?;
    Ok(())
}

impl<'a, S, PT: ProtocolTypes, PB: ProtocolBehavior<ProtocolTypes = PT>> Mutator<Trace<PT>, S>
    for MakeMessage<'a, PB>
where
    S: HasRand,
    PB: ProtocolBehavior<ProtocolTypes = PT>,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());
        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate MakeMessage skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }
        let rand = state.rand_mut();
        let mut constraints_make_message = TermConstraints {
            must_be_symbolic: true, /* we exclude non-symbolic terms, which were already mutated
                                     * with MakeMessage */
            no_payload_in_subterm: false, /* change to true to exclude picking a term with a
                                           * payload in a sub-term */
            // TODO: we may want to set no_payload_subterm to true
            // pros of adding: less mutations on sub-terms that could be subsumed by mutations on a
            // larger term done in the first place cons: might be useful to first
            // shotgun small mutations on a small term to make the trace progress with possibly more
            // actions and then do larger mutations on a larger term from there
            // (might have an impact later). TODO: balance out this trade-off
            not_inside_list: true, /* true means we are not picking terms inside list (like
                                    * fn_append in the middle) */
            // we set it to true since it would otherwise be redundant with picking each of the item
            // as mutated term
            weighted_depth: false, /* true means we select a sub-term by giving higher-priority
                                    * to deeper sub-terms */
            // Set two lasts to false now as this allows to find more case for now.
            // TODO: fix reservori sampling and set this to true (as well as in
            // integration_test/term_zoo.rs)
            ..self.config.term_constraints
        };
        if !self.config.with_dy {
            constraints_make_message.must_be_root = true;
        }

        // choose a random sub term
        if let Some((chosen_term, trace_path)) =
            choose_filtered(trace, &constraints_make_message, |t| !t.is_no_bit(), rand)
        {
            log::debug!("[Mutation-bit] Mutate MakeMessage on term\n{}", chosen_term);
            let spawner = Spawner::new(self.put_registry.clone());
            // log::trace!("Using self.put_registry {:?} to compute ctx",
            // self.put_registry.default().name());
            let mut ctx = TraceContext::new_config(
                spawner,
                ConfigTrace {
                    with_bit_level: self.config.with_bit_level,
                    ..Default::default()
                },
            );
            BIT_EXEC.increment();
            match make_message_term(trace, &trace_path, &mut ctx) {
                // TODO: possibly we would need to make sure the mutated trace can be executed (if
                // not directly dropped by the feedback loop once executed).
                // TODO: maybe add a consitency check: same encoding by reading /encoding
                Ok(()) => {
                    log::debug!("mutation::MakeMessage successful!");
                    BIT_EXEC_SUCCESS.increment();
                    Ok(MutationResult::Mutated)
                }
                Err(e) => {
                    log::warn!(
                        "mutation::MakeMessage failed (with_focus: {}) due to {e}",
                        false
                    );
                    log::debug!("mutation::MakeMessage failed due to {e}");
                    log::debug!("       Skipped {}", self.name());
                    Ok(MutationResult::Skipped)
                }
            }
        } else {
            log::debug!(
                "mutation::MakeMessage failed to choose term in trace:\n {}",
                &trace
            );
            log::debug!("       Skipped {}", self.name());
            Ok(MutationResult::Skipped)
        }
    }
}

impl<'a, PB> Named for MakeMessage<'a, PB>
where
    PB: ProtocolBehavior,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

// --------------------------------------------------------------------------------------------------
// ReadMessage mutation
// --------------------------------------------------------------------------------------------------

/// READ MESSAGE: picks a sub-term having itself a sub-term with payload, evaluate, read and
/// performs an in-place replacement
pub struct ReadMessage<'a, PB> {
    config: MutationConfig,
    put_registry: &'a PutRegistry<PB>,
}

impl<'a, PB> ReadMessage<'a, PB> {
    #[must_use]
    pub const fn new(config: MutationConfig, put_registry: &'a PutRegistry<PB>) -> Self {
        Self {
            config,
            put_registry,
        }
    }
}

impl<'a, S, PT: ProtocolTypes, PB: ProtocolBehavior<ProtocolTypes = PT>> Mutator<Trace<PT>, S>
    for ReadMessage<'a, PB>
where
    S: HasRand,
    PB: ProtocolBehavior<ProtocolTypes = PT>,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        // Nothing for now
        Ok(MutationResult::Skipped)
    }
}

impl<'a, PB> Named for ReadMessage<'a, PB>
where
    PB: ProtocolBehavior,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

// --------------------------------------------------------------------------------------------------
// Term-level bit-level mutations
// --------------------------------------------------------------------------------------------------

use paste::paste;

use crate::algebra::bitstrings::PayloadMetadata;
use crate::algebra::signature::Signature;
use crate::fuzzer::mutations::{
    dy_mutations, remove_prefix_and_type, GenerateMutator, MutationConfig, RemoveAndLiftMutator,
    RepeatMutator, ReplaceMatchMutator, ReplaceReuseMutator, SkipMutator, SwapMutator,
};
use crate::fuzzer::stats_stage::{BIT_EXEC, BIT_EXEC_SUCCESS, MM_EXEC, MM_EXEC_SUCCESS};
use crate::put_registry::PutRegistry;

macro_rules! expand_mutation {
    ($mutation:ident) => {
paste!{
        /// mutation definition
pub struct [<$mutation  DY>]<S>
    where
        S: HasRand + HasMaxSize,
{
    config: MutationConfig,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> [<$mutation  DY>]<S>
    where
        S: HasRand + HasMaxSize,
{
    #[must_use]
    pub const fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for [<$mutation  DY>]<S>
    where
        S: HasRand + HasMaxSize,
        PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate {} skipped because bit-level mutations are disabled", self.name());
            return Ok(MutationResult::Skipped)
        }
        match choose_term_with_payload_mut(
            trace,
            state,
            &self.config.term_constraints,
            false,
        ) {
            Some(to_mutate) => {
                log::debug!("[Mutation-bit] Mutate {} on term {to_mutate}", self.name(),);
                if let Some(payloads) = &mut to_mutate.payloads {
                    $mutation.mutate(state, &mut payloads.payload, stage_idx)
                              .and_then(|r| {
                               payloads.set_changed();
                               Ok(r)
                          })
                } else {
                    panic!("mutation::{}::this shouldn't happen since we filtered out terms that are symbolic!", self.name());
                }
            }
            None => {
                log::debug!("       Skipped {}", self.name());
                Ok(MutationResult::Skipped)
            }
        }
    }
}

impl<S> Named for [<$mutation  DY>]<S>
    where
        S: HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<[<$mutation  DY>]<S>>())
    }
}
}};
}

#[macro_export]
macro_rules! expand_mutations {
    () => {};
    ($mutation:ident) => {
          expand_mutation!($mutation);
    };
    ($mutation:ident,) => {
          expand_mutation!($mutation);
    };
    ($mutation:ident, $($MS:ident),*) => {
          expand_mutation!($mutation);
          $crate::expand_mutations!($($MS),*);
    };
}
// Use of `expand_mutations` is below
expand_mutations!(
    BitFlipMutator,
    ByteFlipMutator,
    ByteIncMutator,
    ByteDecMutator,
    ByteNegMutator,
    ByteRandMutator,
    ByteAddMutator,
    WordAddMutator,
    DwordAddMutator,
    QwordAddMutator,
    ByteInterestingMutator,
    WordInterestingMutator,
    DwordInterestingMutator,
    BytesDeleteMutator,
    BytesExpandMutator,
    BytesLargeExpandMutator,
    BytesInsertMutator,
    BytesRandInsertMutator,
    BytesSetMutator,
    BytesRandSetMutator,
    BytesCopyMutator /* The next 2 require custom implementations:
                      *   BytesInsertCopyMutator,
                      *   BytesSwapMutator,
                      * The three next require to pick up another test-case (cross-over), see
                      * dedicated implementations:
                      *   CrossoverInsertMutator
                      *   CrossoverReplaceMutator
                      *   SpliceMutator */
);

// We could write another macro for the two following mutations
// BytesSwapMutatorDY
pub struct BytesSwapMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    config: MutationConfig,
    tmp_buf: BytesSwapMutator,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> BytesSwapMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    #[must_use]
    pub fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            tmp_buf: BytesSwapMutator::new(),
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for BytesSwapMutatorDY<S>
where
    S: HasRand + HasMaxSize,
    PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate BytesSwapMutatorDY skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }
        match choose_term_with_payload_mut(trace, state, &self.config.term_constraints, false) {
            Some(to_mutate) => {
                log::debug!("[Mutation-bit] Mutate {} on term {to_mutate}", self.name(),);
                if let Some(payloads) = &mut to_mutate.payloads {
                    BytesSwapMutator::mutate(
                        &mut self.tmp_buf,
                        state,
                        &mut payloads.payload,
                        stage_idx,
                    )
                    .and_then(|r| {
                        payloads.set_changed();
                        Ok(r)
                    })
                } else {
                    panic!("mutation::{}::this shouldn't happen since we filtered out terms that are symbolic!", self.name());
                }
            }
            None => {
                log::debug!("       Skipped {}", self.name());
                Ok(MutationResult::Skipped)
            }
        }
    }
}

impl<S> Named for BytesSwapMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

// BytesInsertCopyMutatorDY
pub struct BytesInsertCopyMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    config: MutationConfig,
    tmp_buf: BytesInsertCopyMutator,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> BytesInsertCopyMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    #[must_use]
    pub fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            tmp_buf: BytesInsertCopyMutator::new(),
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for BytesInsertCopyMutatorDY<S>
where
    S: HasRand + HasMaxSize,
    PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate BytesInsertCopyMutatorDY skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }
        match choose_term_with_payload_mut(trace, state, &self.config.term_constraints, false) {
            Some(to_mutate) => {
                log::debug!("[Mutation-bit] Mutate {} on term {to_mutate}", self.name(),);
                if let Some(payloads) = &mut to_mutate.payloads {
                    BytesInsertCopyMutator::mutate(
                        &mut self.tmp_buf,
                        state,
                        &mut payloads.payload,
                        stage_idx,
                    )
                    .and_then(|r| {
                        payloads.set_changed();
                        Ok(r)
                    })
                } else {
                    panic!("mutation::{}::this shouldn't happen since we filtered out terms that are symbolic!", self.name());
                }
            }
            None => {
                log::debug!("       Skipped {}", self.name());
                Ok(MutationResult::Skipped)
            }
        }
    }
}

impl<S> Named for BytesInsertCopyMutatorDY<S>
where
    S: HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

// --------------------------------------------------------------------------------------------------
// Trace-level bit-level mutations --> Cross-over need to consider traces with the HasBytesVec
// trait!
// --------------------------------------------------------------------------------------------------

/// Returns the focused payload or randomly choose a non-symbolic payload in a trace (mutable
/// reference).
pub fn choose_term_with_payload_mut<'a, PT, S>(
    trace: &'a mut Trace<PT>,
    state: &mut S,
    term_constraints: &TermConstraints,
    with_focus: bool,
) -> Option<&'a mut Term<PT>>
where
    S: HasRand + HasMaxSize,
    PT: ProtocolTypes,
{
    let res = choose_term_filtered_mut(
        trace,
        |x| x.is_symbolic().not(),
        term_constraints,
        state.rand_mut(),
    );
    log::debug!("choose_term_with_payload_mut -- Chosen term, not focus");
    res
}

pub fn choose_payload_mut<'a, PT, S>(
    trace: &'a mut Trace<PT>,
    state: &mut S,
    config: &MutationConfig,
) -> Option<(&'a mut Vec<u8>, &'a mut PayloadMetadata)>
where
    S: HasRand + HasMaxSize,
    PT: ProtocolTypes,
{
    match choose_term_with_payload_mut(trace, state, &config.term_constraints, false) {
        Some(term) => {
            let payloads = term
                .payloads
                .as_mut()
                .expect("[choose_payload_mut] should never happen");
            let input = payloads.payload.bytes_mut();
            if input.len() < 2 {
                log::debug!(
                    "[choose_payload_mut] Skipped because payload is too small: {} bytes",
                    input.len()
                );
                None
            } else {
                Some((input, &mut payloads.metadata))
            }
        }
        None => None,
    }
}

/// Copied from `libafl::mutators::mutations`
/// Returns the first and last diff position between the given vectors, stopping at the min len
fn locate_diffs(this: &[u8], other: &[u8]) -> (i64, i64) {
    let mut first_diff: i64 = -1;
    let mut last_diff: i64 = -1;
    for (i, (this_el, other_el)) in this.iter().zip(other.iter()).enumerate() {
        if this_el != other_el {
            if first_diff < 0 {
                first_diff = i as i64;
            }
            last_diff = i as i64;
        }
    }

    (first_diff, last_diff)
}

/// Copied from `libafl::mutators::mutations`
/// Mem move in the own vec
#[inline]
pub(crate) unsafe fn buffer_self_copy<T>(data: &mut [T], from: usize, to: usize, len: usize) {
    debug_assert!(!data.is_empty());
    debug_assert!(from + len <= data.len());
    debug_assert!(to + len <= data.len());
    if len != 0 && from != to {
        let ptr = data.as_mut_ptr();
        unsafe {
            core::ptr::copy(ptr.add(from), ptr.add(to), len);
        }
    }
}

/// Mem move between vecs
#[inline]
pub(crate) unsafe fn buffer_copy<T>(dst: &mut [T], src: &[T], from: usize, to: usize, len: usize) {
    debug_assert!(!dst.is_empty());
    debug_assert!(!src.is_empty());
    debug_assert!(from + len <= src.len());
    debug_assert!(to + len <= dst.len());
    let dst_ptr = dst.as_mut_ptr();
    let src_ptr = src.as_ptr();
    if len != 0 {
        unsafe {
            core::ptr::copy(src_ptr.add(from), dst_ptr.add(to), len);
        }
    }
}

// CrossoverInsertMutatorDY
pub struct CrossoverInsertMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    config: MutationConfig,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> CrossoverInsertMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    #[must_use]
    pub fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for CrossoverInsertMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
    S: libafl::inputs::UsesInput<Input = Trace<PT>>,
    PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate CrossoverInsertMutatorDY skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }

        let Some((input, metadata)) = choose_payload_mut(trace, state, &self.config) else {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        };

        // Inlined from libafl::mutators::mutations pub struct CrossoverInsertMutator
        let size = input.len();
        let max_size = state.max_size();
        if size >= max_size {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }

        // We don't want to use the testcase we're already using for splicing
        let idx = random_corpus_id!(state.corpus(), state.rand_mut());

        if let Some(cur) = state.corpus().current() {
            if idx == *cur {
                log::debug!("       Skipped {}", self.name());
                return Ok(MutationResult::Skipped);
            }
        }

        // let other_size = {
        //     let mut other_testcase = state.corpus().get(idx)?.borrow_mut();
        //     other_testcase.load_input(state.corpus())?.bytes().len()
        // };
        let size_vec_payloads = {
            let mut other_testcase = state.corpus().get(idx)?.borrow_mut();
            let other_input_trace = other_testcase.load_input(state.corpus())?;
            other_input_trace.all_payloads().len()
        };
        if size_vec_payloads < 1 {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }
        let payload_idx = state // we need to split the choice of other_input int two steps to
            // avoid borrow checker issues (state need sto be immutably borrowed to access the
            // corpus and mutably borrowed to pick a random payload in the chosen trace)
            .rand_mut()
            .between(0, (size_vec_payloads - 1) as u64) as usize;
        let other_size = {
            let other_testcase = state.corpus().get(idx)?.borrow_mut();
            // Input will already be loaded.
            let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
                .payload
                .bytes();
            other_input.len()
        };
        //

        if other_size < 2 {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }

        let range = rand_range(state, other_size, min(other_size, max_size - size));
        let target = state.rand_mut().below(size as u64) as usize;

        input.resize(size + range.len(), 0);
        unsafe {
            buffer_self_copy(input, target, target + range.len(), size - target);
        }

        // let other_testcase = state.corpus().get(idx)?.borrow_mut();
        // // No need to load the input again, it'll still be cached.
        // let other = other_testcase.input().as_ref().unwrap();
        let other_testcase = state.corpus().get(idx)?.borrow_mut();
        // Input will already be loaded.
        let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
            .payload
            .bytes();
        let _other_size = other_input.len();

        unsafe {
            buffer_copy(input, other_input, range.start, target, range.len());
        }
        metadata.has_changed = true;

        log::debug!("[Mutation-bit] Mutate {} on trace {trace:?} crossing over with corpus id {idx} and payload id {payload_idx}", self.name());
        log::trace!("Trace: {trace}");
        Ok(MutationResult::Mutated)
    }
}

impl<S> Named for CrossoverInsertMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

pub struct CrossoverReplaceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    config: MutationConfig,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> CrossoverReplaceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    #[must_use]
    pub fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for CrossoverReplaceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
    S: libafl::inputs::UsesInput<Input = Trace<PT>>,
    PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate CrossoverReplaceMutatorDY skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }

        let Some((input, metadata)) = choose_payload_mut(trace, state, &self.config) else {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        };

        // Inlined from libafl::mutators::mutations pub struct CrossoverReplaceMutator
        let size = input.len();
        if size == 0 {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }

        // We don't want to use the testcase we're already using for splicing
        let idx = random_corpus_id!(state.corpus(), state.rand_mut());
        if let Some(cur) = state.corpus().current() {
            if idx == *cur {
                log::debug!("       Skipped {}", self.name());
                return Ok(MutationResult::Skipped);
            }
        }

        // let other_size = {
        //     let mut other_testcase = state.corpus().get(idx)?.borrow_mut();
        //     other_testcase.load_input(state.corpus())?.bytes().len()
        // };
        let size_vec_payloads = {
            let mut other_testcase = state.corpus().get(idx)?.borrow_mut();
            let other_input_trace = other_testcase.load_input(state.corpus())?;
            other_input_trace.all_payloads().len()
        };
        if size_vec_payloads < 1 {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }
        let payload_idx = state // we need to split the choice of other_input int two steps to
            // avoid borrow checker issues (state need sto be immutably borrowed to access the
            // corpus and mutably borrowed to pick a random payload in the chosen trace)
            .rand_mut()
            .between(0, (size_vec_payloads - 1) as u64) as usize;
        let other_size = {
            let other_testcase = state.corpus().get(idx)?.borrow_mut();
            // Input will already be loaded.
            let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
                .payload
                .bytes();
            other_input.len()
        };
        //

        if other_size < 2 {
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }

        let target = state.rand_mut().below(size as u64) as usize;
        let range = rand_range(state, other_size, min(other_size, size - target));

        // let other_testcase = state.corpus().get(idx)?.borrow_mut();
        // // No need to load the input again, it'll still be cached.
        // let other = other_testcase.input().as_ref().unwrap();
        let other_testcase = state.corpus().get(idx)?.borrow_mut();
        // Input will already be loaded.
        let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
            .payload
            .bytes();

        unsafe {
            buffer_copy(input, other_input, range.start, target, range.len());
        }
        metadata.has_changed = true;

        log::debug!("[Mutation-bit] Mutate {} on trace {trace:?} crossing over with corpus id {idx} and payload id {payload_idx}", self.name());
        log::trace!("Trace: {trace}");
        Ok(MutationResult::Mutated)
    }
}

impl<S> Named for CrossoverReplaceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

pub struct SpliceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    config: MutationConfig,
    phantom_s: std::marker::PhantomData<S>,
}

impl<S> SpliceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    #[must_use]
    pub fn new(config: MutationConfig) -> Self {
        Self {
            config: MutationConfig {
                term_constraints: TermConstraints {
                    not_readable: true,
                    ..config.term_constraints
                },
                ..config
            },
            phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, PT> Mutator<Trace<PT>, S> for SpliceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
    //        <S as libafl::inputs::UsesInput>::Input = BytesInput,
    S: libafl::inputs::UsesInput<Input = Trace<PT>>,
    PT: ProtocolTypes,
{
    fn mutate(
        &mut self,
        state: &mut S,
        trace: &mut Trace<PT>,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        log::debug!("[Bit] Start mutate with {}", self.name());

        if !self.config.with_bit_level {
            log::debug!("[Mutation-bit] Mutate SpliceMutatorDY skipped because bit-level mutations are disabled");
            return Ok(MutationResult::Skipped);
        }

        let Some((input, metadata)) = choose_payload_mut(trace, state, &self.config) else {
            log::trace!("choose_payload_mut failed");
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        };

        // Inlined from libafl::mutators::mutations pub struct SpliceMutator
        // We don't want to use the testcase we're already using for splicing
        let idx = random_corpus_id!(state.corpus(), state.rand_mut());
        if let Some(cur) = state.corpus().current() {
            if idx == *cur {
                log::trace!("Same other testcase");
                log::debug!("       Skipped {}", self.name());
                return Ok(MutationResult::Skipped);
            }
        }

        // Picking up other payload
        let size_vec_payloads = {
            let mut other_testcase = state.corpus().get(idx)?.borrow_mut();
            let other_input_trace = other_testcase.load_input(state.corpus())?;
            other_input_trace.all_payloads().len()
        };
        if size_vec_payloads < 1 {
            log::trace!("other payload is too short: {size_vec_payloads}");
            log::debug!("       Skipped {}", self.name());
            return Ok(MutationResult::Skipped);
        }
        let payload_idx = state // we need to split the choice of other_input int two steps to
            // avoid borrow checker issues (state need sto be immutably borrowed to access the
            // corpus and mutably borrowed to pick a random payload in the chosen trace)
            .rand_mut()
            .between(0, (size_vec_payloads - 1) as u64) as usize;
        let _other_size = {
            let other_testcase = state.corpus().get(idx)?.borrow_mut();
            // Input will already be loaded.
            let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
                .payload
                .bytes();
            other_input.len()
        };
        //

        let (first_diff, last_diff) = {
            let other_testcase = state.corpus().get(idx)?.borrow_mut();
            // Input will already be loaded.
            let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
                .payload
                .bytes();

            let mut counter: u32 = 0;
            loop {
                let (f, l) = locate_diffs(input, other_input);

                if f != l && f >= 0 && l >= 2 {
                    break (f as u64, l as u64);
                }
                if counter == 3 {
                    log::trace!("counter is 3");
                    log::debug!("       Skipped {}", self.name());
                    return Ok(MutationResult::Skipped);
                }
                counter += 1;
            }
        };

        let split_at = state.rand_mut().between(first_diff, last_diff) as usize;

        let other_testcase = state.corpus().get(idx)?.borrow_mut();
        // Input will already be loaded.
        let other_input = other_testcase.input().as_ref().unwrap().all_payloads()[payload_idx]
            .payload
            .bytes();

        input.splice(split_at.., other_input[split_at..].iter().copied());
        metadata.has_changed = true;

        log::debug!("[Mutation-bit] Mutate {} on trace {trace:?} crossing over with corpus id {idx} and payload id {payload_idx}", self.name());
        log::trace!("Trace: {trace}");
        Ok(MutationResult::Mutated)
    }
}

impl<S> Named for SpliceMutatorDY<S>
where
    S: HasCorpus + HasRand + HasMaxSize,
{
    fn name(&self) -> &str {
        remove_prefix_and_type(std::any::type_name::<Self>())
    }
}

/***************************************************************************************************
                      New BytesLargeExpandMutator mutation
***************************************************************************************************/

/// Large number of bytes expand mutation for inputs with a bytes vector (expand range: from 2^5 to
/// 2^12)
#[derive(Default, Debug)]
pub struct BytesLargeExpandMutator;

impl<I, S> Mutator<I, S> for BytesLargeExpandMutator
where
    S: HasRand + HasMaxSize,
    I: HasBytesVec,
{
    fn mutate(
        &mut self,
        state: &mut S,
        input: &mut I,
        _stage_idx: i32,
    ) -> Result<MutationResult, Error> {
        let min_length_log = 5;
        let max_length_log = 12;

        let max_size = state.max_size();
        let size = input.bytes().len();
        if size == 0 || size >= max_size {
            return Ok(MutationResult::Skipped);
        }
        if size < 1 << min_length_log {
            return Ok(MutationResult::Skipped);
        }
        let len_log = state
            .rand_mut()
            .between(min_length_log as u64, max_length_log as u64) as usize;
        let len = min(1 << len_log, max_size - size);
        let start = state.rand_mut().between(0, size as u64) as usize;
        let range = start..(start + len);
        log::trace!("[BytesLargeExpandMutator] len: {len}, range: {range:?}, size: {size}");
        input.bytes_mut().resize(size + range.len(), 0);
        unsafe {
            buffer_self_copy(
                input.bytes_mut(),
                range.start,
                range.start + range.len(),
                size - range.start,
            );
        }
        log::trace!("After mutation, length is: {}", input.bytes().len());
        Ok(MutationResult::Mutated)
    }
}

impl Named for BytesLargeExpandMutator {
    fn name(&self) -> &str {
        "BytesLargexpandMutator"
    }
}

impl BytesLargeExpandMutator {
    /// Creates a new [`BytesLargeExpandMutator`].
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}