scheduled mutator

This commit is contained in:
Andrea Fioraldi 2020-10-30 15:00:32 +01:00
parent 6366db92bc
commit 1e6e1e5a92
2 changed files with 56 additions and 7 deletions

View File

@ -1,21 +1,24 @@
use crate::inputs::Input;
use std::io::Error;
use crate::AflError;
pub mod scheduled;
pub use scheduled::ScheduledMutator;
pub trait Mutator {
//fn rand(&self) -> &Box<dyn Rand>;
//fn rand_mut(&self) -> &mut Box<dyn Rand>;
fn mutate(&mut self, input: &mut dyn Input) -> Result<(), Error> {
fn mutate(&mut self, input: &mut dyn Input) -> Result<(), AflError> {
self.mutate_at((-1) as i32, input)
}
fn mutate_at(&mut self, stage_idx: i32, input: &mut dyn Input) -> Result<(), Error>;
fn mutate_at(&mut self, stage_idx: i32, input: &mut dyn Input) -> Result<(), AflError>;
fn post_exec(&mut self, is_interesting: bool) -> Result<(), Error> {
fn post_exec(&mut self, is_interesting: bool) -> Result<(), AflError> {
self.post_exec_at((-1) as i32, is_interesting)
}
fn post_exec_at(&mut self, stage_idx: i32, is_interesting: bool) -> Result<(), Error>;
fn post_exec_at(&mut self, _stage_idx: i32, _is_interesting: bool) -> Result<(), AflError> {
Ok(())
}
}

View File

@ -1,8 +1,54 @@
use crate::mutators::Mutator;
use crate::inputs::Input;
use crate::AflError;
type MutationFunction = fn(&mut dyn ScheduledMutator, &mut dyn Input) -> Result<(), AflError>;
pub trait ScheduledMutator: Mutator {
fn iterations(&self) -> u64 {
fn iterations(&self, _input: &dyn Input) -> u64 {
//1 << (1 + self.rand_mut().below(7))
return 0;
return 1;
}
fn schedule(&self, _input: &dyn Input) -> Result<MutationFunction, AflError> {
if self.mutations_count() == 0 {
return Err(AflError::Empty("no mutations".to_string()));
}
self.get_mutation_by_idx(1 /* self.rand_mut().below(self.mutations_count()) */)
}
fn get_mutation_by_idx(&self, index: usize) -> Result<MutationFunction, AflError>;
fn mutations_count(&self) -> usize;
}
pub struct BaseScheduledMutator {
mutations: Vec<MutationFunction>
}
impl ScheduledMutator for BaseScheduledMutator {
fn get_mutation_by_idx(&self, index: usize) -> Result<MutationFunction, AflError> {
if index >= self.mutations.len() {
return Err(AflError::Unknown("oob".to_string()));
}
Ok(self.mutations[index])
}
fn mutations_count(&self) -> usize {
self.mutations.len()
}
}
impl Mutator for BaseScheduledMutator {
fn mutate_at(&mut self, _stage_idx: i32, input: &mut dyn Input) -> Result<(), AflError> {
let num = self.iterations(input);
for _ in 0..num {
self.schedule(input)?(self, input)?;
}
Ok(())
}
}