lifetime fixes and generator
This commit is contained in:
parent
397099f0be
commit
4fa50ebfaf
@ -86,8 +86,6 @@ where
|
||||
mod tests {
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::rc::Rc;
|
||||
use core::cell::RefCell;
|
||||
|
||||
use crate::corpus::{Corpus, InMemoryCorpus, Testcase};
|
||||
use crate::engines::{DefaultEngine, Engine};
|
||||
@ -106,13 +104,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_engine() {
|
||||
let rand: Rc<_> = DefaultRand::new(0).into();
|
||||
let rand = DefaultRand::new(0).into();
|
||||
|
||||
let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand);
|
||||
let testcase = Testcase::new(vec![0; 4]).into();
|
||||
corpus.add(testcase);
|
||||
let executor: Rc<RefCell<InMemoryExecutor<BytesInput>>> =
|
||||
InMemoryExecutor::new(harness).into();
|
||||
let executor = InMemoryExecutor::<BytesInput>::new(harness).into();
|
||||
let mut engine = DefaultEngine::new();
|
||||
let mut mutator = DefaultScheduledMutator::new(&rand);
|
||||
mutator.add_mutation(mutation_bitflip);
|
||||
|
87
src/generators/mod.rs
Normal file
87
src/generators/mod.rs
Normal 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])
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ pub mod inputs;
|
||||
pub mod monitors;
|
||||
pub mod mutators;
|
||||
pub mod observers;
|
||||
pub mod generators;
|
||||
pub mod stages;
|
||||
pub mod utils;
|
||||
|
||||
|
@ -66,7 +66,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefaultScheduledMutator<'a, C, I, R>
|
||||
pub struct DefaultScheduledMutator<C, I, R>
|
||||
where
|
||||
C: Corpus<I>,
|
||||
I: Input,
|
||||
@ -76,7 +76,7 @@ where
|
||||
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
|
||||
C: Corpus<I>,
|
||||
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
|
||||
C: Corpus<I>,
|
||||
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
|
||||
C: Corpus<I>,
|
||||
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
|
||||
C: Corpus<I>,
|
||||
I: Input,
|
||||
@ -131,7 +131,7 @@ where
|
||||
// Just use the default methods
|
||||
}
|
||||
|
||||
impl<'a, C, I, R> DefaultScheduledMutator<'a, C, I, R>
|
||||
impl<C, I, R> DefaultScheduledMutator<C, I, R>
|
||||
where
|
||||
C: Corpus<I>,
|
||||
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
|
||||
C: Corpus<I>,
|
||||
I: Input + HasBytesVec,
|
||||
@ -311,7 +311,7 @@ where
|
||||
{
|
||||
/// Create a new HavocBytesMutator instance wrapping DefaultScheduledMutator
|
||||
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);
|
||||
HavocBytesMutator {
|
||||
scheduled: scheduled,
|
||||
|
@ -11,7 +11,7 @@ use crate::stages::Stage;
|
||||
use crate::utils::{HasRand, Rand};
|
||||
use crate::AflError;
|
||||
|
||||
// TODO create HasMutatorsVec trait
|
||||
// TODO multi mutators stage
|
||||
|
||||
pub trait MutationalStage<C, I, M, E>: Stage<C, I> + HasRand
|
||||
where
|
||||
|
Loading…
x
Reference in New Issue
Block a user