lain 0.5.5

Mutation framework for usage in fuzzers
Documentation
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
use crate::mutator::Mutator;

use crate::rand::seq::SliceRandom;
use crate::rand::Rng;
use crate::traits::*;
use crate::types::*;
use num_traits::Bounded;
use std::fmt::Debug;
use std::mem::MaybeUninit;
use std::{char, cmp};

impl<T> NewFuzzed for Option<T>
where
    T: NewFuzzed,
{
    type RangeType = T::RangeType;

    default fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Option<T> {
        if mutator.gen_chance(0.75) {
            Some(T::new_fuzzed(mutator, constraints))
        } else {
            None
        }
    }
}

impl<T> NewFuzzed for Box<T>
where
    T: NewFuzzed,
{
    type RangeType = T::RangeType;

    default fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Box<T> {
        Box::new(T::new_fuzzed(mutator, constraints))
    }
}

impl<T> NewFuzzed for Vec<T>
where
    T: NewFuzzed + SerializedSize,
{
    type RangeType = usize;

    default fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Vec<T> {
        const MAX_NUM_ELEMENTS: usize = 0x200;

        let mut min: Self::RangeType;
        let mut max: Self::RangeType;
        let weight: Weighted;
        let max_size: Option<usize>;
        let mut used_size: usize = 0;
        let mut output: Vec<T>;

        if T::max_default_object_size() == 0 {
            warn!("Size of element in vec is 0... returning early");
            return vec![];
        }

        trace!(
            "Generating random Vec with constraints: {:#X?}",
            constraints
        );

        // if no min/max were supplied, we'll take a conservative approach of 64 elements
        match constraints {
            Some(ref constraints) => {
                min = constraints.min.unwrap_or(0);
                max = constraints.max.unwrap_or(MAX_NUM_ELEMENTS);

                if min != max {
                    if min != 0 && mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX) {
                        min = 0;
                    }

                    if constraints.max.is_some()
                        && mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX)
                    {
                        if let Some(new_max) = constraints.max.unwrap().checked_mul(2) {
                            max = new_max;
                        }
                    }
                }

                weight = constraints.weighted;

                max_size = constraints.max_size;
                if let Some(max_size) = max_size {
                    max = cmp::min(max, max_size / T::max_default_object_size());
                }
            }
            None => {
                min = 0;
                max = MAX_NUM_ELEMENTS;
                max_size = None;
                weight = Weighted::None;
            }
        }

        if max == 0 {
            return vec![];
        }

        if min > max {
            min = 0;
        }

        // If min == max, that means the user probably wants this to be exactly that many elements.
        let num_elements: usize = if min == max {
            min
        } else {
            mutator.gen_weighted_range(min, max, weight)
        };

        output = Vec::with_capacity(num_elements);

        for _i in 0..num_elements {
            let element = if let Some(ref max_size) = max_size {
                T::new_fuzzed(
                    mutator,
                    Some(
                        &Constraints::new()
                            .max_size(max_size - used_size)
                            .set_base_size_accounted_for(),
                    ),
                )
            } else {
                T::new_fuzzed(mutator, None)
            };

            let element_serialized_size = element.serialized_size();

            if let Some(ref max_size) = max_size {
                if used_size + element_serialized_size > *max_size {
                    break;
                } else {
                    used_size += element_serialized_size;
                }
            }

            output.push(element);
        }

        output
    }
}

impl<T> NewFuzzed for Vec<T>
where
    T: NewFuzzed + Clone + SerializedSize,
{
    fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Vec<T> {
        const MAX_NUM_ELEMENTS: usize = 0x1000;

        let mut min: Self::RangeType;
        let mut max: Self::RangeType;
        let weight: Weighted;
        let max_size: Option<usize>;
        let mut used_size: usize = 0;
        let mut output: Vec<T>;

        trace!(
            "Generating random Vec with constraints: {:#X?}",
            constraints
        );

        if T::max_default_object_size() == 0 {
            warn!("Size of element in vec is 0... returning early");
            return vec![];
        }

        // if no min/max were supplied, we'll take a conservative approach of 64 elements
        match constraints {
            Some(constraints) => {
                min = constraints.min.unwrap_or(0);
                max = constraints.max.unwrap_or(MAX_NUM_ELEMENTS);

                if min != max {
                    if min != 0 && mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX) {
                        min = 0;
                    }

                    if constraints.max.is_some()
                        && mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX)
                    {
                        // we just hope this doesn't overflow.
                        max = constraints.max.unwrap() * 2;
                    }
                }

                weight = constraints.weighted;

                max_size = constraints.max_size;
                if let Some(max_size) = constraints.max_size {
                    max = cmp::min(max, max_size / T::max_default_object_size());
                }
            }
            None => {
                min = 0;
                max = MAX_NUM_ELEMENTS;
                max_size = None;
                weight = Weighted::None;
            }
        }

        if max == 0 {
            return vec![];
        }

        if min > max {
            min = 0;
        }

        // If min == max, that means the user probably wants this to be exactly that many elements.
        let num_elements: usize = if min == max {
            min
        } else {
            mutator.gen_weighted_range(min, max, weight)
        };

        output = Vec::with_capacity(num_elements);

        let should_reuse_array_item =
            mutator.gen_chance(crate::mutator::CHANCE_TO_REPEAT_ARRAY_VALUE);

        if should_reuse_array_item {
            let element: T = if let Some(ref max_size) = max_size {
                T::new_fuzzed(
                    mutator,
                    Some(
                        &Constraints::new()
                            .max_size(max_size - used_size)
                            .set_base_size_accounted_for(),
                    ),
                )
            } else {
                T::new_fuzzed(mutator, None)
            };

            let element_serialized_size = element.serialized_size();

            for _i in 0..num_elements {
                if let Some(ref max_size) = max_size {
                    if used_size + element_serialized_size > *max_size {
                        break;
                    } else {
                        used_size += element_serialized_size;
                    }
                }

                output.push(element.clone());
            }
        } else {
            for _i in 0..num_elements {
                let element: T = if let Some(ref max_size) = max_size {
                    T::new_fuzzed(
                        mutator,
                        Some(
                            &Constraints::new()
                                .max_size(max_size - used_size)
                                .set_base_size_accounted_for(),
                        ),
                    )
                } else {
                    T::new_fuzzed(mutator, None)
                };

                let element_serialized_size = element.serialized_size();

                if let Some(ref max_size) = max_size {
                    if used_size + element_serialized_size > *max_size {
                        break;
                    } else {
                        used_size += element_serialized_size;
                    }
                }

                output.push(element);
            }
        }

        output
    }
}

// TODO: Uncomment once const generics are more stable
// impl<T, const SIZE: usize> NewFuzzed for [T; SIZE]
// where T: NewFuzzed + Clone {
//     type RangeType = usize;

//     fn new_fuzzed<R: Rng>(mutator: &mut Mutator<R>, constraints: Option<&Constraints<Self::RangeType>>) -> [T; SIZE] {
//         if constraints.is_some() {
//             warn!("Constraints passed to new_fuzzed on fixed-size array do nothing");
//         }

//         let mut output: MaybeUninit<[T; SIZE]> = MaybeUninit::uninit();
//         let arr_ptr = output.as_mut_ptr() as *mut T;

//         let mut idx = 0;
//         let mut element: T = T::new_fuzzed(mutator, None);
//         while idx < SIZE {
//             arr_ptr.add(idx).write(element.clone());

//             idx += 1;
//             if SIZE - idx > 0 {
//                 if mutator.gen_chance(crate::mutator::CHANCE_TO_REPEAT_ARRAY_VALUE) {
//                     let repeat_end_idx = mutator.gen_range(idx, SIZE);
//                     while idx < repeat_end_idx {
//                         arr_ptr.add(idx).write(element.clone());
//                         idx += 1;
//                     }

//                     if SIZE - idx > 0 {
//                         element = T::new_fuzzed(mutator, None);
//                     }
//                 } else {
//                     element = T::new_fuzzed(mutator, None);
//                 }
//             }
//         }

//         unsafe { output.assume_init() }
//     }
// }

impl<T, I> NewFuzzed for UnsafeEnum<T, I>
where
    T: NewFuzzed,
    I: NewFuzzed<RangeType = I> + Bounded + Debug + Default,
{
    type RangeType = I;

    fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        trace!(
            "Generating random UnsafeEnum with constraints: {:#?}",
            constraints
        );

        if mutator.gen_chance(crate::mutator::CHANCE_TO_PICK_INVALID_ENUM) {
            UnsafeEnum::Invalid(I::new_fuzzed(mutator, constraints))
        } else {
            // TODO/BUG: We should be passing on the constraints, but all
            // objects are generated with RangeType = u8, which causes
            // complications when I is not a u8...
            UnsafeEnum::Valid(T::new_fuzzed(mutator, None))
        }
    }
}

impl NewFuzzed for Utf8String {
    type RangeType = usize;

    fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        let min: Self::RangeType;
        let max: Self::RangeType;
        let weight: Weighted;
        let mut output: Utf8String;

        trace!(
            "Generating random UtfString with constraints: {:#?}",
            constraints
        );

        // if no min/max were supplied, we'll take a conservative approach
        match constraints {
            Some(ref constraints) => {
                min = constraints.min.unwrap_or(0);
                max = constraints.max.unwrap_or(256);
                weight = constraints.weighted;
            }
            None => {
                min = 0;
                max = 256;
                weight = Weighted::None;
            }
        }

        let string_length = mutator.gen_weighted_range(min, max, weight);

        output = Utf8String {
            inner: Vec::with_capacity(string_length),
        };

        let mut idx = 0;
        let mut chr = Utf8Char::new_fuzzed(mutator, None);

        while idx < string_length {
            output.inner.push(chr.clone());

            idx += 1;
            if string_length - idx > 0 {
                if mutator.gen_chance(crate::mutator::CHANCE_TO_REPEAT_ARRAY_VALUE) {
                    let repeat_end_idx = mutator.gen_range(idx, string_length);
                    while idx < repeat_end_idx {
                        output.inner.push(chr.clone());
                        idx += 1;
                    }
                    if string_length - idx > 0 {
                        chr = Utf8Char::new_fuzzed(mutator, None);
                    }
                } else {
                    chr = Utf8Char::new_fuzzed(mutator, None);
                }
            }
        }

        output
    }
}

impl NewFuzzed for AsciiString {
    type RangeType = usize;

    fn new_fuzzed<R: Rng>(
        mutator: &mut Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        let min: Self::RangeType;
        let max: Self::RangeType;
        let weight: Weighted;
        let mut output: AsciiString;

        trace!(
            "Generating random AsciiString with constraints: {:#?}",
            constraints
        );

        // if no min/max were supplied, we'll take a conservative approach
        match constraints {
            Some(ref constraints) => {
                min = constraints.min.unwrap_or(0);
                max = constraints.max.unwrap_or(256);
                weight = constraints.weighted;
            }
            None => {
                min = 0;
                max = 256;
                weight = Weighted::None;
            }
        }

        let string_length = mutator.gen_weighted_range(min, max, weight);

        output = AsciiString {
            inner: Vec::with_capacity(string_length),
        };

        let mut idx = 0;
        let mut chr = AsciiChar::new_fuzzed(mutator, None);

        while idx < string_length {
            output.inner.push(chr.clone());

            idx += 1;
            if string_length - idx > 0 {
                if mutator.gen_chance(crate::mutator::CHANCE_TO_REPEAT_ARRAY_VALUE) {
                    let repeat_end_idx = mutator.gen_range(idx, string_length);
                    while idx < repeat_end_idx {
                        output.inner.push(chr.clone());
                        idx += 1;
                    }
                    if string_length - idx > 0 {
                        chr = AsciiChar::new_fuzzed(mutator, None);
                    }
                } else {
                    chr = AsciiChar::new_fuzzed(mutator, None);
                }
            }
        }

        output
    }
}

impl NewFuzzed for Utf8Char {
    type RangeType = u32;

    fn new_fuzzed<R: crate::rand::Rng>(
        mutator: &mut crate::mutator::Mutator<R>,
        _constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        trace!("generating random UTF8 char");

        // This implementation is taken almost verbatim from burntsushi's
        // quickcheck library. See this link for the original implementation:
        // https://github.com/BurntSushi/quickcheck/blob/b3e50a5e7c85e19538cf8612d9fd6da32c588930/src/arbitrary.rs#L573-L637
        //
        // I like his logic for mode generation, so that's kept as well

        let mode_chance = mutator.gen_range(0, 100);
        match mode_chance {
            0..=49 => Utf8Char(mutator.gen_range(0, 0xB0) as u8 as char),
            50..=59 => {
                loop {
                    if let Some(c) = char::from_u32(mutator.gen_range(0, 0x10000)) {
                        return Utf8Char(c);
                    }

                    // keep looping if we got an invalid char. this will
                    // ignore surrogate pairs
                }
            }
            60..=84 => {
                // Characters often used in programming languages
                let c = [
                    ' ', ' ', ' ', '\t', '\n', '~', '`', '!', '@', '#', '$', '%', '^', '&', '*',
                    '(', ')', '_', '-', '=', '+', '[', ']', '{', '}', ':', ';', '\'', '"', '\\',
                    '|', ',', '<', '>', '.', '/', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9',
                ]
                .choose(&mut mutator.rng)
                .unwrap()
                .to_owned();

                Utf8Char(c)
            }
            85..=89 => {
                // Tricky Unicode, part 1
                let c = [
                    '\u{0149}', // a deprecated character
                    '\u{fff0}', // some of "Other, format" category:
                    '\u{fff1}',
                    '\u{fff2}',
                    '\u{fff3}',
                    '\u{fff4}',
                    '\u{fff5}',
                    '\u{fff6}',
                    '\u{fff7}',
                    '\u{fff8}',
                    '\u{fff9}',
                    '\u{fffA}',
                    '\u{fffB}',
                    '\u{fffC}',
                    '\u{fffD}',
                    '\u{fffE}',
                    '\u{fffF}',
                    '\u{0600}',
                    '\u{0601}',
                    '\u{0602}',
                    '\u{0603}',
                    '\u{0604}',
                    '\u{0605}',
                    '\u{061C}',
                    '\u{06DD}',
                    '\u{070F}',
                    '\u{180E}',
                    '\u{110BD}',
                    '\u{1D173}',
                    '\u{e0001}', // tag
                    '\u{e0020}', //  tag space
                    '\u{e000}',
                    '\u{e001}',
                    '\u{ef8ff}', // private use
                    '\u{f0000}',
                    '\u{ffffd}',
                    '\u{ffffe}',
                    '\u{fffff}',
                    '\u{100000}',
                    '\u{10FFFD}',
                    '\u{10FFFE}',
                    '\u{10FFFF}',
                    // "Other, surrogate" characters are so that very special
                    // that they are not even allowed in safe Rust,
                    //so omitted here
                    '\u{3000}', // ideographic space
                    '\u{1680}',
                    // other space characters are already covered by two next
                    // branches
                ]
                .choose(&mut mutator.rng)
                .unwrap()
                .to_owned();

                Utf8Char(c)
            }
            90..=94 => {
                // Tricky unicode, part 2
                Utf8Char(char::from_u32(mutator.gen_range(0x2000, 0x2070)).unwrap())
            }
            95..=99 => {
                // Completely arbitrary characters
                Utf8Char(mutator.gen())
            }
            _ => unreachable!(),
        }
    }
}

impl NewFuzzed for AsciiChar {
    type RangeType = u8;

    fn new_fuzzed<R: crate::rand::Rng>(
        mutator: &mut crate::mutator::Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        trace!("generating random ASCII char");
        let min: Self::RangeType;
        let max: Self::RangeType;
        let weight: Weighted;

        // if no min/max were supplied, we'll take a conservative approach of 64 elements
        match constraints {
            Some(ref constraints) => {
                min = constraints.min.unwrap_or(0);
                max = constraints.max.unwrap_or(0x80);
                weight = constraints.weighted;
            }
            None => {
                // If no constraints were provided, we'll use logic similar to Utf8Char above and
                // potentially generate special classes of chars

                // even though we could use gen_chance() here, let's not in case we want
                // to add more special classes
                let mode_chance = mutator.gen_range(0, 100);
                match mode_chance {
                    0..=49 => {
                        // Just generate a random char
                        return AsciiChar(mutator.gen_range(0, 0x80) as u8 as char);
                    }
                    50..=99 => {
                        // Characters often used in programming languages
                        let c = [
                            ' ', ' ', ' ', '\t', '\n', '~', '`', '!', '@', '#', '$', '%', '^', '&',
                            '*', '(', ')', '_', '-', '=', '+', '[', ']', '{', '}', ':', ';', '\'',
                            '"', '\\', '|', ',', '<', '>', '.', '/', '?', '0', '1', '2', '3', '4',
                            '5', '6', '7', '8', '9',
                        ]
                        .choose(&mut mutator.rng)
                        .unwrap()
                        .to_owned();

                        return AsciiChar(c);
                    }
                    _ => unreachable!(),
                }
            }
        }

        AsciiChar(
            std::char::from_u32(mutator.gen_weighted_range(min as u32, max as u32, weight))
                .expect("Invalid codepoint generated for AsciiChar"),
        )
    }
}

impl NewFuzzed for char {
    type RangeType = u32;

    #[inline(always)]
    fn new_fuzzed<R: crate::rand::Rng>(
        mutator: &mut crate::mutator::Mutator<R>,
        constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        Utf8Char::new_fuzzed(mutator, constraints).0
    }
}

impl NewFuzzed for bool {
    type RangeType = u8;

    fn new_fuzzed<R: crate::rand::Rng>(
        mutator: &mut crate::mutator::Mutator<R>,
        _constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        trace!("generating random bool");

        mutator.rng.gen()
    }
}

macro_rules! impl_new_fuzzed {
    ( $($name:ident),* ) => {
        $(
            impl NewFuzzed for $name {
                type RangeType = $name;

                fn new_fuzzed<R: Rng>(mutator: &mut Mutator<R>, constraints: Option<&Constraints<Self::RangeType>>) -> Self {
                    let min: Self::RangeType;
                    let max: Self::RangeType;
                    let weight: Weighted;

                    // if no min/max were supplied, we'll take a conservative approach of 64 elements
                    match constraints {
                        Some(ref constraints) => {
                            let mut ignore_min = true;
                            let mut ignore_max = true;

                            min = if let Some(ref min) = constraints.min {
                                if mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX) {
                                    $name::min_value()
                                } else {
                                    ignore_min = false;
                                    *min
                                }
                            } else {
                                $name::min_value()
                            };

                            max = if let Some(ref max) = constraints.max {
                                if mutator.gen_chance(crate::mutator::CHANCE_TO_IGNORE_MIN_MAX) {
                                    $name::max_value()
                                } else {
                                    ignore_max = false;
                                    *max
                                }
                            } else {
                                $name::max_value()
                            };

                            weight = constraints.weighted;

                            // these conditions being met should be rare, so bump the chance to 75%
                            if ignore_min && ignore_max && mutator.gen_chance(0.75) {
                                return $name::select_dangerous_number(&mut mutator.rng);
                            }

                            return mutator.gen_weighted_range(min, max, weight);
                        }
                        None => {
                            if mutator.gen_chance(0.25) {
                                return $name::select_dangerous_number(&mut mutator.rng);
                            }

                            return mutator.rng.gen();
                        }
                    }
                }
            }
        )*
    }
}

// BUG: f32/f64 generate a number between 0/1 when no constraints are supplied,
// otherwise they generate an *integer* between min/max.
impl_new_fuzzed!(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64);

impl<T> NewFuzzed for [T; 0]
where
    T: NewFuzzed + Clone,
{
    type RangeType = usize;

    fn new_fuzzed<R: Rng>(
        _mutator: &mut Mutator<R>,
        _constraints: Option<&Constraints<Self::RangeType>>,
    ) -> [T; 0] {
        // no-op
        []
    }
}

macro_rules! impl_new_fuzzed_array {
    ( $($size:expr),* ) => {
        $(
            impl<T> NewFuzzed for [T; $size]
            where T: NewFuzzed + Clone + SerializedSize {
                type RangeType = usize;

                fn new_fuzzed<R: Rng>(mutator: &mut Mutator<R>, constraints: Option<&Constraints<Self::RangeType>>) -> [T; $size] {
                    let mut per_item_max_size: Option<usize> = constraints.and_then(|c| c.max_size.as_ref().and_then(|size| Some(*size / $size)));

                    let mut output: MaybeUninit<[T; $size]> = MaybeUninit::uninit();
                    let arr_ptr = output.as_mut_ptr() as *mut T;

                    let constraints = per_item_max_size.as_ref().map(|size| {
                        let mut constraints = Constraints::new();
                        constraints.max_size(*size);
                        constraints.set_base_size_accounted_for();

                        constraints
                    });

                    let mut idx = 0;
                    let mut element: T = T::new_fuzzed(mutator, constraints.as_ref());

                    while idx < $size {
                        unsafe {
                            arr_ptr.add(idx).write(element.clone());
                        }

                        idx += 1;
                        if $size - idx > 0 {
                            if mutator.gen_chance(crate::mutator::CHANCE_TO_REPEAT_ARRAY_VALUE) {
                                let repeat_end_idx = mutator.gen_range(idx, $size);
                                while idx < repeat_end_idx {
                                    unsafe {
                                        arr_ptr.add(idx).write(element.clone());
                                    }
                                    idx += 1;
                                }
                            } else {
                                let constraints = per_item_max_size.as_ref().map(|size| {
                                    let mut constraints = Constraints::new();
                                    constraints.max_size(*size);
                                    constraints.set_base_size_accounted_for();

                                    constraints
                                });

                                element = T::new_fuzzed(mutator, constraints.as_ref());
                            }
                        }
                    }

                    unsafe { output.assume_init() }
                }
            }

            impl<T> NewFuzzed for [T; $size]
            where T: NewFuzzed + SerializedSize {
                default type RangeType = usize;

                default fn new_fuzzed<R: Rng>(mutator: &mut Mutator<R>, constraints: Option<&Constraints<Self::RangeType>>) -> [T; $size] {
                    let mut per_item_max_size: Option<usize> = constraints.and_then(|c| c.max_size.as_ref().and_then(|size| Some(*size / $size)));

                    let mut output: MaybeUninit<[T; $size]> = MaybeUninit::uninit();
                    let arr_ptr = output.as_mut_ptr() as *mut T;

                    for i in 0..$size {
                        let constraints = per_item_max_size.as_ref().map(|size| {
                            let mut constraints = Constraints::new();
                            constraints.max_size(*size);
                            constraints.set_base_size_accounted_for();

                            constraints
                        });
                        let element = T::new_fuzzed(mutator, constraints.as_ref());

                        unsafe {
                            arr_ptr.offset(i).write(element);
                        }
                    }

                    unsafe { output.assume_init() }
                }
            }
        )*
    }
}

impl_new_fuzzed_array!(
    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
);

impl NewFuzzed for *mut std::ffi::c_void {
    type RangeType = usize;

    fn new_fuzzed<R: Rng>(
        _mutator: &mut Mutator<R>,
        _constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        std::ptr::null_mut()
    }
}

impl NewFuzzed for *const std::ffi::c_void {
    type RangeType = usize;

    fn new_fuzzed<R: Rng>(
        _mutator: &mut Mutator<R>,
        _constraints: Option<&Constraints<Self::RangeType>>,
    ) -> Self {
        std::ptr::null()
    }
}