ClosureStage

This commit is contained in:
Andrea Fioraldi 2021-11-12 14:50:50 +01:00
parent 3b30ce3c20
commit 20e5500d93
2 changed files with 50 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use alloc::{
vec::Vec,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{
bolts::{ownedref::OwnedRefMut, tuples::Named, AsSlice},

View File

@ -27,6 +27,8 @@ pub use concolic::ConcolicTracingStage;
pub use concolic::SimpleConcolicMutationalStage;
use crate::Error;
use core::{convert::From, marker::PhantomData};
/// A stage is one step in the fuzzing process.
/// Multiple stages will be scheduled one by one for each input.
pub trait Stage<E, EM, S, Z> {
@ -89,3 +91,50 @@ where
.perform_all(fuzzer, executor, state, manager, corpus_idx)
}
}
pub struct ClosureStage<CB, E, EM, S, Z>
where
CB: FnMut(&mut Z, &mut E, &mut S, &mut EM, usize) -> Result<(), Error>,
{
closure: CB,
phantom: PhantomData<(E, EM, S, Z)>,
}
impl<CB, E, EM, S, Z> Stage<E, EM, S, Z> for ClosureStage<CB, E, EM, S, Z>
where
CB: FnMut(&mut Z, &mut E, &mut S, &mut EM, usize) -> Result<(), Error>,
{
fn perform(
&mut self,
fuzzer: &mut Z,
executor: &mut E,
state: &mut S,
manager: &mut EM,
corpus_idx: usize,
) -> Result<(), Error> {
(self.closure)(fuzzer, executor, state, manager, corpus_idx)
}
}
impl<CB, E, EM, S, Z> ClosureStage<CB, E, EM, S, Z>
where
CB: FnMut(&mut Z, &mut E, &mut S, &mut EM, usize) -> Result<(), Error>,
{
#[must_use]
pub fn new(closure: CB) -> Self {
Self {
closure,
phantom: PhantomData,
}
}
}
impl<CB, E, EM, S, Z> From<CB> for ClosureStage<CB, E, EM, S, Z>
where
CB: FnMut(&mut Z, &mut E, &mut S, &mut EM, usize) -> Result<(), Error>,
{
#[must_use]
fn from(closure: CB) -> Self {
Self::new(closure)
}
}