fuzzcheck 0.13.0

A modular, structure-aware, and feedback-driven fuzzing engine for Rust functions
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
//! Types to combine multiple sensors and pools together
//!
//! If we have two tuples of compatible sensors and pools:
//! * `s1` and `p1`
//! * `s2` and `p2`
//!
//! Then we can combine them into a single sensor and pool as follows:
//! ```
//! use fuzzcheck::sensors_and_pools::{AndSensor, AndPool, DifferentObservations};
//! use fuzzcheck::PoolExt;
//! # use fuzzcheck::sensors_and_pools::{NoopSensor, UniqueValuesPool};
//! # let (s1, s2) = (NoopSensor, NoopSensor);
//! # let (p1, p2) = (UniqueValuesPool::<u8>::new("a", 0), UniqueValuesPool::<bool>::new("b", 0));
//! let s = AndSensor(s1, s2);
//! let p = p1.and(p2, Some(2.0), DifferentObservations);
//! // 1.0 overrides the weight of `p2`, which influences how often the fuzzer will choose a test case
//! // from that pool. By default, all pools have a weight of 1.0. Therefore, in this case, asssuming
//! // `p1` has a weight of 1.0, then test cases will chosen from `p2` as often as `p1`. We can keep
//! // `p2`s original weight using:
//! # let (p1, p2) = (UniqueValuesPool::<u8>::new("a", 0), UniqueValuesPool::<bool>::new("b", 0));
//! let p = p1.and(p2, None, DifferentObservations);
//! // Note that the weight of `p` is the weight of `p1` plus the weight of `p2`.
//! ```
//! At every iteration of the fuzz test, both pools have a chance to provide a test case to mutate.
//! After the test function is run, both sensors will collect data and feed them to their respective pool.
//!
//! It is also possible to use two pools processing the observations of a single sensor. This is done
//! as follows:
//! ```
//! use fuzzcheck::sensors_and_pools::{AndSensor, AndPool, SameObservations};
//! use fuzzcheck::PoolExt;
//! # use fuzzcheck::sensors_and_pools::{NoopSensor, UniqueValuesPool};
//! # let s = NoopSensor;
//! # let (p1, p2) = (UniqueValuesPool::<u8>::new("a", 0), UniqueValuesPool::<u8>::new("b", 0));
//! let p = p1.and(p2, Some(2.0), SameObservations);
//! // if both `p1` and `p2` are compatible with the observations from sensor `s`,
//! // then (s, p) is a valid combination of sensor and pool
//! ```
use std::fmt::Display;
use std::marker::PhantomData;
use std::path::PathBuf;

use crate::traits::{CompatibleWithObservations, CorpusDelta, Pool, SaveToStatsFolder, Sensor, SensorAndPool, Stats};
use crate::{CSVField, PoolStorageIndex, ToCSV};
/// Marker type used by [`AndPool`] to signal that all sub-pools are compatible with the same observations.
pub struct SameObservations;

/// Marker type used by [`AndPool`] to signal that each sub-pool works is compatible with different observations.
pub struct DifferentObservations;

/// A pool that combines two pools
///
/// A convenient way to create an `AndPool` is to use [`p1.and(p2, ..)`](crate::PoolExt::and), but you
/// are free to use [`AndPool::new`](AndPool::new) as well.
///
/// If the two pools act on the same observations , then the `ObservationsMarker` generic type
/// parameter should be [`SameObservations`]. However, if they act on different observations,
/// then this type parameter should be [`DifferentObservations`]. This will influence what
/// observations the `AndPool` is [compatible with](crate::CompatibleWithObservations).
///
/// If both `P1` and `P2` are [`CompatibleWithObservations<O>`], then
/// `AndPool<P1, P2, SameObservations>` will be `CompatibleWithObservations<O>` as well.
///
/// If `P1` is [`CompatibleWithObservations<O1>`] and `P2` is [`CompatibleWithObservations<O2>`], then
/// `AndPool<P1, P2, DifferentObservations>` will be `CompatibleWithObservations<(O1, O2)>`.
///
/// When the `AndPool` is [asked to provide a test case](crate::Pool::get_random_index), it will
/// choose between `p1` and `p2` randomly based on their weights, given by `self.p1_weight` and `self.p2_weight`,
/// and based on how recently `p1` or `p2` made some progress. Pools that make progress will be prefered
/// over pools that do not.
pub struct AndPool<P1, P2, ObservationsMarker>
where
    P1: Pool,
    P2: Pool,
{
    pub p1: P1,
    pub p2: P2,

    pub p1_weight: f64,
    pub p2_weight: f64,

    p1_number_times_chosen_since_last_progress: usize,
    p2_number_times_chosen_since_last_progress: usize,

    rng: fastrand::Rng,
    _phantom: PhantomData<ObservationsMarker>,
}
impl<P1, P2, ObservationsMarker> AndPool<P1, P2, ObservationsMarker>
where
    P1: Pool,
    P2: Pool,
{
    #[coverage(off)]
    pub fn new(p1: P1, p2: P2, p1_weight: f64, p2_weight: f64) -> Self {
        Self {
            p1,
            p2,
            p1_weight,
            p2_weight,
            p1_number_times_chosen_since_last_progress: 1,
            p2_number_times_chosen_since_last_progress: 1,
            rng: fastrand::Rng::new(),
            _phantom: PhantomData,
        }
    }
}
impl<P1, P2, ObservationsMarker> AndPool<P1, P2, ObservationsMarker>
where
    P1: Pool,
    P2: Pool,
{
    fn p1_weight(&self) -> f64 {
        self.p1_weight / self.p1_number_times_chosen_since_last_progress as f64
    }
    fn p2_weight(&self) -> f64 {
        self.p2_weight / self.p2_number_times_chosen_since_last_progress as f64
    }
}
impl<P1, P2, ObservationsMarker> Pool for AndPool<P1, P2, ObservationsMarker>
where
    P1: Pool,
    P2: Pool,
{
    type Stats = AndPoolStats<P1::Stats, P2::Stats>;

    #[coverage(off)]
    fn stats(&self) -> Self::Stats {
        AndPoolStats(self.p1.stats(), self.p2.stats())
    }
    #[coverage(off)]
    fn get_random_index(&mut self) -> Option<PoolStorageIndex> {
        let choice = self.rng.f64() * self.weight();
        if choice <= self.p1_weight() {
            if let Some(idx) = self.p1.get_random_index() {
                self.p1_number_times_chosen_since_last_progress += 1;
                Some(idx)
            } else {
                self.p2_number_times_chosen_since_last_progress += 1;
                self.p2.get_random_index()
            }
        } else if let Some(idx) = self.p2.get_random_index() {
            self.p2_number_times_chosen_since_last_progress += 1;
            Some(idx)
        } else {
            self.p1_number_times_chosen_since_last_progress += 1;
            self.p1.get_random_index()
        }
    }

    fn weight(&self) -> f64 {
        self.p1_weight() + self.p2_weight()
    }
}

impl<P1, P2, ObservationsMarker> SaveToStatsFolder for AndPool<P1, P2, ObservationsMarker>
where
    P1: Pool,
    P2: Pool,
{
    #[coverage(off)]
    fn save_to_stats_folder(&self) -> Vec<(PathBuf, Vec<u8>)> {
        let mut x = self.p1.save_to_stats_folder();
        x.extend(self.p2.save_to_stats_folder());
        x
    }
}

/// A sensor that combines two sensors
///
/// The [`observations`](crate::Sensor::Observations) from this sensor are the combination
/// of the observations of both `S1` and `S2`.
/// So `AndSensor<S1, S2>` implements `Sensor<Observations = (S1::Observations, S2::Observations)>`.
///
/// To create a pool that is compatible with an `AndSensor`, use an [`AndPool`] with the [`DifferentObservations`]
/// marker type.
pub struct AndSensor<S1, S2>(pub S1, pub S2)
where
    S1: Sensor,
    S2: Sensor;

impl<S1, S2> Sensor for AndSensor<S1, S2>
where
    S1: Sensor,
    S2: Sensor,
{
    type Observations = (S1::Observations, S2::Observations);

    #[coverage(off)]
    fn start_recording(&mut self) {
        self.0.start_recording();
        self.1.start_recording();
    }
    #[coverage(off)]
    fn stop_recording(&mut self) {
        self.0.stop_recording();
        self.1.stop_recording();
    }
    #[coverage(off)]
    fn get_observations(&mut self) -> Self::Observations {
        (self.0.get_observations(), self.1.get_observations())
    }
}

impl<S1, S2> SaveToStatsFolder for AndSensor<S1, S2>
where
    S1: Sensor,
    S2: Sensor,
{
    #[coverage(off)]
    fn save_to_stats_folder(&self) -> Vec<(PathBuf, Vec<u8>)> {
        let mut x = self.0.save_to_stats_folder();
        x.extend(self.1.save_to_stats_folder());
        x
    }
}

/// The statistics of an [AndPool]
#[derive(Clone)]
pub struct AndPoolStats<S1: Display, S2: Display>(pub S1, pub S2);
impl<S1: Display, S2: Display> Display for AndPoolStats<S1, S2> {
    #[coverage(off)]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.0, self.1)
    }
}
impl<S1: Display, S2: Display> Stats for AndPoolStats<S1, S2>
where
    S1: Stats,
    S2: Stats,
{
}

impl<O1, O2, P1, P2> CompatibleWithObservations<(O1, O2)> for AndPool<P1, P2, DifferentObservations>
where
    P1: Pool,
    P2: Pool,
    P1: CompatibleWithObservations<O1>,
    P2: CompatibleWithObservations<O2>,
{
    #[coverage(off)]
    fn process(&mut self, input_id: PoolStorageIndex, observations: &(O1, O2), complexity: f64) -> Vec<CorpusDelta> {
        let AndPool {
            p1,
            p2,
            p1_number_times_chosen_since_last_progress,
            p2_number_times_chosen_since_last_progress,
            ..
        } = self;
        let deltas_1 = p1.process(input_id, &observations.0, complexity);
        if !deltas_1.is_empty() {
            *p1_number_times_chosen_since_last_progress = 1;
        }
        let deltas_2 = p2.process(input_id, &observations.1, complexity);
        if !deltas_2.is_empty() {
            *p2_number_times_chosen_since_last_progress = 1;
        }
        let mut deltas = deltas_1;
        deltas.extend(deltas_2);
        deltas
    }
}

impl<P1, P2, O> CompatibleWithObservations<O> for AndPool<P1, P2, SameObservations>
where
    P1: CompatibleWithObservations<O>,
    P2: CompatibleWithObservations<O>,
{
    #[coverage(off)]
    fn process(&mut self, input_id: PoolStorageIndex, observations: &O, complexity: f64) -> Vec<CorpusDelta> {
        let AndPool {
            p1,
            p2,
            p1_number_times_chosen_since_last_progress,
            p2_number_times_chosen_since_last_progress,
            ..
        } = self;
        let deltas_1 = p1.process(input_id, observations, complexity);
        if !deltas_1.is_empty() {
            *p1_number_times_chosen_since_last_progress = 1;
        }
        let deltas_2 = p2.process(input_id, observations, complexity);
        if !deltas_2.is_empty() {
            *p2_number_times_chosen_since_last_progress = 1;
        }
        let mut deltas = deltas_1;
        deltas.extend(deltas_2);
        deltas
    }
}

impl<S1, S2> ToCSV for AndPoolStats<S1, S2>
where
    S1: Display,
    S2: Display,
    S1: ToCSV,
    S2: ToCSV,
{
    #[coverage(off)]
    fn csv_headers(&self) -> Vec<CSVField> {
        let mut h = self.0.csv_headers();
        h.extend(self.1.csv_headers());
        h
    }

    #[coverage(off)]
    fn to_csv_record(&self) -> Vec<CSVField> {
        let mut h = self.0.to_csv_record();
        h.extend(self.1.to_csv_record());
        h
    }
}

/// Combines two [`SensorAndPool`](crate::SensorAndPool) trait objects into one.
///
/// You probably won't need to use this type directly because the
/// [`SensorAndPool`](crate::SensorAndPool) trait is mainly used by fuzzcheck itself
/// and not its users. Instead, it is more likely that you are working with types implementing
/// [`Sensor`](crate::Sensor) and [`Pool`](crate::Pool). If that is the case, then you will
/// want to look at [`AndSensor`] and [`AndPool`] (as well as the convenience method to
/// create an `AndPool`: [`p1.and(p2, ..)`](crate::PoolExt::and)).
pub struct AndSensorAndPool {
    sap1: Box<dyn SensorAndPool>,
    sap2: Box<dyn SensorAndPool>,
    sap1_weight: f64,
    sap2_weight: f64,
    sap1_number_times_chosen_since_last_progress: usize,
    sap2_number_times_chosen_since_last_progress: usize,
    rng: fastrand::Rng,
}
impl AndSensorAndPool {
    #[coverage(off)]
    pub fn new(sap1: Box<dyn SensorAndPool>, sap2: Box<dyn SensorAndPool>, sap1_weight: f64, sap2_weight: f64) -> Self {
        Self {
            sap1,
            sap2,
            sap1_weight,
            sap2_weight,
            sap1_number_times_chosen_since_last_progress: 1,
            sap2_number_times_chosen_since_last_progress: 1,
            rng: fastrand::Rng::new(),
        }
    }
}
impl SaveToStatsFolder for AndSensorAndPool {
    #[coverage(off)]
    fn save_to_stats_folder(&self) -> Vec<(PathBuf, Vec<u8>)> {
        let mut x = self.sap1.save_to_stats_folder();
        x.extend(self.sap2.save_to_stats_folder());
        x
    }
}
impl SensorAndPool for AndSensorAndPool {
    #[coverage(off)]
    fn stats(&self) -> Box<dyn crate::traits::Stats> {
        Box::new(AndPoolStats(self.sap1.stats(), self.sap2.stats()))
    }

    #[coverage(off)]
    fn start_recording(&mut self) {
        self.sap1.start_recording();
        self.sap2.start_recording();
    }

    #[coverage(off)]
    fn stop_recording(&mut self) {
        self.sap1.stop_recording();
        self.sap2.stop_recording();
    }

    #[coverage(off)]
    fn process(&mut self, input_id: PoolStorageIndex, cplx: f64) -> Vec<CorpusDelta> {
        let AndSensorAndPool {
            sap1,
            sap2,
            sap1_number_times_chosen_since_last_progress,
            sap2_number_times_chosen_since_last_progress,
            ..
        } = self;
        let deltas_1 = sap1.process(input_id, cplx);
        if !deltas_1.is_empty() {
            *sap1_number_times_chosen_since_last_progress = 1;
        }
        let deltas_2 = sap2.process(input_id, cplx);
        if !deltas_2.is_empty() {
            *sap2_number_times_chosen_since_last_progress = 1;
        }
        let mut deltas = deltas_1;
        deltas.extend(deltas_2);
        deltas
    }

    #[coverage(off)]
    fn get_random_index(&mut self) -> Option<PoolStorageIndex> {
        let sum_weight = self.sap1_weight + self.sap2_weight;
        if self.rng.f64() <= sum_weight {
            if let Some(idx) = self.sap1.get_random_index() {
                self.sap1_number_times_chosen_since_last_progress += 1;
                Some(idx)
            } else {
                self.sap2_number_times_chosen_since_last_progress += 1;
                self.sap2.get_random_index()
            }
        } else if let Some(idx) = self.sap2.get_random_index() {
            self.sap2_number_times_chosen_since_last_progress += 1;
            Some(idx)
        } else {
            self.sap1_number_times_chosen_since_last_progress += 1;
            self.sap1.get_random_index()
        }
    }
}