libafl 0.16.0

Slot your own fuzzers together and extend their features using Rust
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
//! An AFL++-style scheduler with a weighted queue.
//!
//! The queue corpus scheduler with weighted queue item selection [from AFL++](https://github.com/AFLplusplus/AFLplusplus/blob/1d4f1e48797c064ee71441ba555b29fc3f467983/src/afl-fuzz-queue.c#L32).
//! This queue corpus scheduler needs calibration stage.

use core::{hash::Hash, marker::PhantomData};

use hashbrown::HashMap;
use libafl_bolts::{
    Named,
    rands::Rand,
    tuples::{Handle, Handled, MatchName},
};
use serde::{Deserialize, Serialize};

use crate::{
    Error, HasMetadata,
    corpus::{Corpus, CorpusId, HasTestcase, Testcase},
    random_corpus_id,
    schedulers::{
        AflScheduler, HasQueueCycles, RemovableScheduler, Scheduler, on_add_metadata_default,
        on_evaluation_metadata_default, on_next_metadata_default,
        powersched::{BaseSchedule, PowerSchedule, SchedulerMetadata},
        testcase_score::{CorpusWeightTestcaseScore, TestcaseScore},
    },
    state::{HasCorpus, HasRand},
};

/// The Metadata for `WeightedScheduler`
#[cfg_attr(
    any(not(feature = "serdeany_autoreg"), miri),
    expect(clippy::unsafe_derive_deserialize)
)] // for SerdeAny
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WeightedScheduleMetadata {
    /// The fuzzer execution spent in the current cycles
    runs_in_current_cycle: usize,
    /// Alias table for weighted queue entry selection
    alias_table: HashMap<CorpusId, CorpusId>,
    /// Probability for which queue entry is selected
    alias_probability: HashMap<CorpusId, f64>,
}

impl Default for WeightedScheduleMetadata {
    fn default() -> Self {
        Self::new()
    }
}

impl WeightedScheduleMetadata {
    /// Constructor for `WeightedScheduleMetadata`
    #[must_use]
    pub fn new() -> Self {
        Self {
            runs_in_current_cycle: 0,
            alias_table: HashMap::default(),
            alias_probability: HashMap::default(),
        }
    }

    /// The getter for `runs_in_current_cycle`
    #[must_use]
    pub fn runs_in_current_cycle(&self) -> usize {
        self.runs_in_current_cycle
    }

    /// The setter for `runs_in_current_cycle`
    pub fn set_runs_current_cycle(&mut self, cycles: usize) {
        self.runs_in_current_cycle = cycles;
    }

    /// The getter for `alias_table`
    #[must_use]
    pub fn alias_table(&self) -> &HashMap<CorpusId, CorpusId> {
        &self.alias_table
    }

    /// The setter for `alias_table`
    pub fn set_alias_table(&mut self, table: HashMap<CorpusId, CorpusId>) {
        self.alias_table = table;
    }

    /// The getter for `alias_probability`
    #[must_use]
    pub fn alias_probability(&self) -> &HashMap<CorpusId, f64> {
        &self.alias_probability
    }

    /// The setter for `alias_probability`
    pub fn set_alias_probability(&mut self, probability: HashMap<CorpusId, f64>) {
        self.alias_probability = probability;
    }
}

libafl_bolts::impl_serdeany!(WeightedScheduleMetadata);

/// A corpus scheduler using power schedules with weighted queue item selection algo.
#[derive(Debug, Clone)]
pub struct WeightedScheduler<C, F, O> {
    table_invalidated: bool,
    strat: Option<PowerSchedule>,
    observer_handle: Handle<C>,
    last_hash: usize,
    queue_cycles: u64,
    phantom: PhantomData<(F, O)>,
    /// Cycle `PowerSchedule` on completion of every queue cycle.
    cycle_schedules: bool,
}

impl<C, F, O> WeightedScheduler<C, F, O>
where
    C: Named,
{
    /// Create a new [`WeightedScheduler`] without any power schedule
    #[must_use]
    pub fn new<S>(state: &mut S, observer: &C) -> Self
    where
        S: HasMetadata,
    {
        Self::with_schedule(state, observer, None)
    }

    /// Create a new [`WeightedScheduler`]
    #[must_use]
    pub fn with_schedule<S>(state: &mut S, observer: &C, strat: Option<PowerSchedule>) -> Self
    where
        S: HasMetadata,
    {
        let _ = state.metadata_or_insert_with(|| SchedulerMetadata::new(strat));
        let _ = state.metadata_or_insert_with(WeightedScheduleMetadata::new);

        Self {
            strat,
            observer_handle: observer.handle(),
            last_hash: 0,
            queue_cycles: 0,
            table_invalidated: true,
            cycle_schedules: false,
            phantom: PhantomData,
        }
    }

    /// Cycle the `PowerSchedule` on completion of a queue cycle
    #[must_use]
    pub fn cycling_scheduler(mut self) -> Self {
        self.cycle_schedules = true;
        self
    }

    #[must_use]
    /// Getter for `strat`
    pub fn strat(&self) -> &Option<PowerSchedule> {
        &self.strat
    }

    /// Create a new alias table when the fuzzer finds a new corpus entry
    #[expect(clippy::cast_precision_loss)]
    pub fn create_alias_table<I, S>(&self, state: &mut S) -> Result<(), Error>
    where
        F: TestcaseScore<I, S>,
        S: HasCorpus<I> + HasMetadata,
    {
        let n = state.corpus().count();

        let mut alias_table: HashMap<CorpusId, CorpusId> = HashMap::default();
        let mut alias_probability: HashMap<CorpusId, f64> = HashMap::default();
        let mut weights: HashMap<CorpusId, f64> = HashMap::default();

        let mut p_arr: HashMap<CorpusId, f64> = HashMap::default();
        let mut s_arr: HashMap<usize, CorpusId> = HashMap::default();
        let mut l_arr: HashMap<usize, CorpusId> = HashMap::default();

        let mut sum: f64 = 0.0;

        for i in state.corpus().ids() {
            let mut testcase = state.corpus().get(i)?.borrow_mut();
            let weight = F::compute(state, &mut *testcase)?;
            weights.insert(i, weight);
            sum += weight;
        }

        for (i, w) in &weights {
            p_arr.insert(*i, w * (n as f64) / sum);
        }

        // # of items in queue S
        let mut n_s = 0;

        // # of items in queue L
        let mut n_l = 0;
        // Divide P into two queues, S and L
        for s in state.corpus().ids().rev() {
            if *p_arr.get(&s).unwrap() < 1.0 {
                s_arr.insert(n_s, s);
                n_s += 1;
            } else {
                l_arr.insert(n_l, s);
                n_l += 1;
            }
        }

        while n_s > 0 && n_l > 0 {
            n_s -= 1;
            n_l -= 1;
            let a = *s_arr.get(&n_s).unwrap();
            let g = *l_arr.get(&n_l).unwrap();

            alias_probability.insert(a, *p_arr.get(&a).unwrap());
            alias_table.insert(a, g);
            *p_arr.get_mut(&g).unwrap() += p_arr.get(&a).unwrap() - 1.0;

            if *p_arr.get(&g).unwrap() < 1.0 {
                *s_arr.get_mut(&n_s).unwrap() = g;
                n_s += 1;
            } else {
                *l_arr.get_mut(&n_l).unwrap() = g;
                n_l += 1;
            }
        }

        while n_l > 0 {
            n_l -= 1;
            alias_probability.insert(*l_arr.get(&n_l).unwrap(), 1.0);
        }

        while n_s > 0 {
            n_s -= 1;
            alias_probability.insert(*s_arr.get(&n_s).unwrap(), 1.0);
        }

        let wsmeta = state.metadata_mut::<WeightedScheduleMetadata>()?;

        // Update metadata
        wsmeta.set_alias_probability(alias_probability);
        wsmeta.set_alias_table(alias_table);
        Ok(())
    }

    /// Cycles the strategy of the scheduler; tries to mimic AFL++'s cycling formula
    fn cycle_schedule(&mut self, metadata: &mut SchedulerMetadata) -> Result<(), Error> {
        let mut ps = metadata.strat().ok_or_else(|| {
            Error::illegal_argument(
                "No strategy specified when initializing scheduler; cannot cycle!",
            )
        })?;
        let new_base = match ps.base() {
            BaseSchedule::EXPLORE => BaseSchedule::EXPLOIT,
            BaseSchedule::COE => BaseSchedule::LIN,
            BaseSchedule::LIN => BaseSchedule::QUAD,
            BaseSchedule::FAST => BaseSchedule::COE,
            BaseSchedule::QUAD => BaseSchedule::FAST,
            BaseSchedule::EXPLOIT => BaseSchedule::EXPLORE,
        };
        ps.set_base(new_base);
        metadata.set_strat(Some(ps));
        // We need to recalculate the scores of testcases.
        self.table_invalidated = true;
        Ok(())
    }
}

impl<C, F, I, O, S> RemovableScheduler<I, S> for WeightedScheduler<C, F, O>
where
    S: HasMetadata,
{
    /// This will *NOT* neutralize the effect of this removed testcase from the global data such as `SchedulerMetadata`
    fn on_remove(
        &mut self,
        state: &mut S,
        _id: CorpusId,
        _prev: &Option<Testcase<I>>,
    ) -> Result<(), Error> {
        if let Ok(wsmeta) = state.metadata_mut::<WeightedScheduleMetadata>() {
            wsmeta.set_runs_current_cycle(wsmeta.runs_in_current_cycle().saturating_sub(1));
        }
        self.table_invalidated = true;
        Ok(())
    }

    /// This will *NOT* neutralize the effect of this removed testcase from the global data such as `SchedulerMetadata`
    fn on_replace(
        &mut self,
        _state: &mut S,
        _id: CorpusId,
        _prev: &Testcase<I>,
    ) -> Result<(), Error> {
        self.table_invalidated = true;
        Ok(())
    }
}

impl<C, F, O> AflScheduler for WeightedScheduler<C, F, O> {
    type ObserverRef = C;

    fn last_hash(&self) -> usize {
        self.last_hash
    }

    fn set_last_hash(&mut self, hash: usize) {
        self.last_hash = hash;
    }

    fn observer_handle(&self) -> &Handle<C> {
        &self.observer_handle
    }
}

impl<C, F, O> HasQueueCycles for WeightedScheduler<C, F, O> {
    fn queue_cycles(&self) -> u64 {
        self.queue_cycles
    }
}

impl<C, F, I, O, S> Scheduler<I, S> for WeightedScheduler<C, F, O>
where
    C: AsRef<O> + Named,
    F: TestcaseScore<I, S>,
    O: Hash,
    S: HasCorpus<I> + HasMetadata + HasRand + HasTestcase<I>,
{
    /// Called when a [`Testcase`] is added to the corpus
    fn on_add(&mut self, state: &mut S, id: CorpusId) -> Result<(), Error> {
        on_add_metadata_default(self, state, id)?;
        self.table_invalidated = true;
        Ok(())
    }

    fn on_evaluation<OT>(&mut self, state: &mut S, _input: &I, observers: &OT) -> Result<(), Error>
    where
        OT: MatchName,
    {
        on_evaluation_metadata_default(self, state, observers)
    }

    fn next(&mut self, state: &mut S) -> Result<CorpusId, Error> {
        if self.table_invalidated {
            self.create_alias_table(state)?;
            self.table_invalidated = false;
        }
        let corpus_counts = state.corpus().count();
        if corpus_counts == 0 {
            Err(Error::empty(
                "No entries in corpus. This often implies the target is not properly instrumented.",
            ))
        } else {
            let s = random_corpus_id!(state.corpus(), state.rand_mut());

            // Choose a random value between 0.0 and 1.0
            let probability = state.rand_mut().next_float();

            let wsmeta = state.metadata_mut::<WeightedScheduleMetadata>()?;

            let runs_in_current_cycle = wsmeta.runs_in_current_cycle();

            if runs_in_current_cycle >= corpus_counts {
                wsmeta.set_runs_current_cycle(0);
            } else {
                wsmeta.set_runs_current_cycle(runs_in_current_cycle + 1);
            }

            let idx = if probability < *wsmeta.alias_probability().get(&s).unwrap() {
                s
            } else {
                *wsmeta.alias_table().get(&s).unwrap()
            };

            // Update depth
            if runs_in_current_cycle >= corpus_counts {
                self.queue_cycles += 1;
                let psmeta = state.metadata_mut::<SchedulerMetadata>()?;
                psmeta.set_queue_cycles(self.queue_cycles());
                if self.cycle_schedules {
                    self.cycle_schedule(psmeta)?;
                }
            }

            self.set_current_scheduled(state, Some(idx))?;
            Ok(idx)
        }
    }

    /// Set current fuzzed corpus id and `scheduled_count`
    fn set_current_scheduled(
        &mut self,
        state: &mut S,
        next_id: Option<CorpusId>,
    ) -> Result<(), Error> {
        on_next_metadata_default(state)?;

        *state.corpus_mut().current_mut() = next_id;
        Ok(())
    }
}

/// The standard corpus weight, same as in `AFL++`
pub type StdWeightedScheduler<C, O> = WeightedScheduler<C, CorpusWeightTestcaseScore, O>;

#[cfg(test)]
mod tests {
    use core::time::Duration;

    use libafl_bolts::rands::StdRand;

    use crate::{
        corpus::{Corpus, EnableDisableCorpus, InMemoryCorpus, Testcase},
        inputs::NopInput,
        observers::StdMapObserver,
        schedulers::{Scheduler, StdWeightedScheduler},
        state::{HasCorpus, StdState},
    };

    #[test]
    fn test_weighted_scheduler_testcase_removal() {
        #[cfg(not(feature = "serdeany_autoreg"))]
        unsafe {
            libafl_bolts::serdeany::RegistryBuilder::register::<
                crate::schedulers::powersched::SchedulerMetadata,
            >();
            libafl_bolts::serdeany::RegistryBuilder::register::<super::WeightedScheduleMetadata>();
            libafl_bolts::serdeany::RegistryBuilder::register::<
                crate::corpus::SchedulerTestcaseMetadata,
            >();
        }

        let mut corpus = InMemoryCorpus::new();
        let mut testcase1 = Testcase::new(NopInput {});
        testcase1.set_exec_time(Duration::from_millis(1)); // High weight
        let idx1 = corpus.add(testcase1).unwrap();

        let mut testcase2 = Testcase::new(NopInput {});
        testcase2.set_exec_time(Duration::from_secs(1)); // Low weight
        let idx2 = corpus.add(testcase2).unwrap();

        let observer = StdMapObserver::owned("map", vec![0u8; 16]);
        let mut state = StdState::new(
            StdRand::with_seed(0),
            corpus,
            InMemoryCorpus::new(),
            &mut (),
            &mut (),
        )
        .unwrap();

        let mut scheduler = StdWeightedScheduler::new(&mut state, &observer);
        scheduler.on_add(&mut state, idx1).unwrap();
        scheduler.on_add(&mut state, idx2).unwrap();

        *state.corpus_mut().current_mut() = Some(idx1);

        state.corpus_mut().disable(idx1).unwrap();
        *state.corpus_mut().current_mut() = None;

        assert_eq!(scheduler.next(&mut state).unwrap(), idx2);
    }
}