lifetime fixes and generator

This commit is contained in:
Andrea Fioraldi 2020-11-16 11:18:44 +01:00
parent 397099f0be
commit 4fa50ebfaf
5 changed files with 99 additions and 14 deletions

View File

@ -86,8 +86,6 @@ where
mod tests { mod tests {
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::rc::Rc;
use core::cell::RefCell;
use crate::corpus::{Corpus, InMemoryCorpus, Testcase}; use crate::corpus::{Corpus, InMemoryCorpus, Testcase};
use crate::engines::{DefaultEngine, Engine}; use crate::engines::{DefaultEngine, Engine};
@ -106,13 +104,12 @@ mod tests {
#[test] #[test]
fn test_engine() { fn test_engine() {
let rand: Rc<_> = DefaultRand::new(0).into(); let rand = DefaultRand::new(0).into();
let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand); let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand);
let testcase = Testcase::new(vec![0; 4]).into(); let testcase = Testcase::new(vec![0; 4]).into();
corpus.add(testcase); corpus.add(testcase);
let executor: Rc<RefCell<InMemoryExecutor<BytesInput>>> = let executor = InMemoryExecutor::<BytesInput>::new(harness).into();
InMemoryExecutor::new(harness).into();
let mut engine = DefaultEngine::new(); let mut engine = DefaultEngine::new();
let mut mutator = DefaultScheduledMutator::new(&rand); let mut mutator = DefaultScheduledMutator::new(&rand);
mutator.add_mutation(mutation_bitflip); mutator.add_mutation(mutation_bitflip);

87
src/generators/mod.rs Normal file
View File

@ -0,0 +1,87 @@
use crate::inputs::bytes::BytesInput;
use crate::inputs::Input;
use crate::utils::{HasRand, Rand};
use crate::AflError;
use alloc::rc::Rc;
use core::cell::RefCell;
use core::cmp::min;
pub trait Generator<I>: HasRand
where
I: Input,
{
/// Generate a new input
fn generate(&mut self) -> Result<I, AflError>;
/// Generate a new dummy input
fn generate_dummy(&self) -> I;
}
const DUMMY_BYTES_SIZE: usize = 64;
pub struct RandBytesGenerator<R> {
rand: Rc<RefCell<R>>,
max_size: usize,
}
impl<R> HasRand for RandBytesGenerator<R>
where
R: Rand,
{
type R = R;
fn rand(&self) -> &Rc<RefCell<Self::R>> {
&self.rand
}
}
impl<R> Generator<BytesInput> for RandBytesGenerator<R>
where
R: Rand,
{
fn generate(&mut self) -> Result<BytesInput, AflError> {
let size = self.rand_below(self.max_size as u64);
let random_bytes: Vec<u8> = (0..size).map(|_| self.rand_below(256) as u8).collect();
Ok(BytesInput::new(random_bytes))
}
fn generate_dummy(&self) -> BytesInput {
let size = min(self.max_size, DUMMY_BYTES_SIZE);
BytesInput::new(vec![0; size])
}
}
pub struct RandPrintablesGenerator<R> {
rand: Rc<RefCell<R>>,
max_size: usize,
}
impl<R> HasRand for RandPrintablesGenerator<R>
where
R: Rand,
{
type R = R;
fn rand(&self) -> &Rc<RefCell<Self::R>> {
&self.rand
}
}
impl<R> Generator<BytesInput> for RandPrintablesGenerator<R>
where
R: Rand,
{
fn generate(&mut self) -> Result<BytesInput, AflError> {
let size = self.rand_below(self.max_size as u64);
let printables = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \t\n!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".as_bytes();
let random_bytes: Vec<u8> = (0..size)
.map(|_| printables[self.rand_below(printables.len() as u64) as usize])
.collect();
Ok(BytesInput::new(random_bytes))
}
fn generate_dummy(&self) -> BytesInput {
let size = min(self.max_size, DUMMY_BYTES_SIZE);
BytesInput::new(vec!['0' as u8; size])
}
}

View File

@ -11,6 +11,7 @@ pub mod inputs;
pub mod monitors; pub mod monitors;
pub mod mutators; pub mod mutators;
pub mod observers; pub mod observers;
pub mod generators;
pub mod stages; pub mod stages;
pub mod utils; pub mod utils;

View File

@ -66,7 +66,7 @@ where
} }
} }
pub struct DefaultScheduledMutator<'a, C, I, R> pub struct DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -76,7 +76,7 @@ where
mutations: Vec<MutationFunction<C, Self, I>>, mutations: Vec<MutationFunction<C, Self, I>>,
} }
impl<'a, C, I, R> HasRand for DefaultScheduledMutator<'_, C, I, R> impl<C, I, R> HasRand for DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -89,7 +89,7 @@ where
} }
} }
impl<'a, C, I, R> Mutator<C, I> for DefaultScheduledMutator<'_, C, I, R> impl<C, I, R> Mutator<C, I> for DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -100,7 +100,7 @@ where
} }
} }
impl<'a, C, I, R> ComposedByMutations<C, I> for DefaultScheduledMutator<'_, C, I, R> impl<C, I, R> ComposedByMutations<C, I> for DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -122,7 +122,7 @@ where
} }
} }
impl<'a, C, I, R> ScheduledMutator<C, I> for DefaultScheduledMutator<'_, C, I, R> impl<C, I, R> ScheduledMutator<C, I> for DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -131,7 +131,7 @@ where
// Just use the default methods // Just use the default methods
} }
impl<'a, C, I, R> DefaultScheduledMutator<'a, C, I, R> impl<C, I, R> DefaultScheduledMutator<C, I, R>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input, I: Input,
@ -303,7 +303,7 @@ where
} }
} }
impl<'a, C, I, R> HavocBytesMutator<C, I, DefaultScheduledMutator<'a, C, I, R>> impl<C, I, R> HavocBytesMutator<C, I, DefaultScheduledMutator<C, I, R>>
where where
C: Corpus<I>, C: Corpus<I>,
I: Input + HasBytesVec, I: Input + HasBytesVec,
@ -311,7 +311,7 @@ where
{ {
/// Create a new HavocBytesMutator instance wrapping DefaultScheduledMutator /// Create a new HavocBytesMutator instance wrapping DefaultScheduledMutator
pub fn new_default(rand: &Rc<RefCell<R>>) -> Self { pub fn new_default(rand: &Rc<RefCell<R>>) -> Self {
let mut scheduled = DefaultScheduledMutator::<'a, C, I, R>::new(rand); let mut scheduled = DefaultScheduledMutator::<C, I, R>::new(rand);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
HavocBytesMutator { HavocBytesMutator {
scheduled: scheduled, scheduled: scheduled,

View File

@ -11,7 +11,7 @@ use crate::stages::Stage;
use crate::utils::{HasRand, Rand}; use crate::utils::{HasRand, Rand};
use crate::AflError; use crate::AflError;
// TODO create HasMutatorsVec trait // TODO multi mutators stage
pub trait MutationalStage<C, I, M, E>: Stage<C, I> + HasRand pub trait MutationalStage<C, I, M, E>: Stage<C, I> + HasRand
where where