From 1e6e1e5a9261bf2870e6bb4464b973d63a94d348 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Fri, 30 Oct 2020 15:00:32 +0100 Subject: [PATCH] scheduled mutator --- src/mutators/mod.rs | 13 ++++++---- src/mutators/scheduled.rs | 50 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/mutators/mod.rs b/src/mutators/mod.rs index dd176d3c10..823c16a0d8 100644 --- a/src/mutators/mod.rs +++ b/src/mutators/mod.rs @@ -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; //fn rand_mut(&self) -> &mut Box; - 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(()) + } } diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index c333d4353e..ac1d4c5d1c 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -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 { + 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; + + fn mutations_count(&self) -> usize; } + +pub struct BaseScheduledMutator { + mutations: Vec +} + +impl ScheduledMutator for BaseScheduledMutator { + + fn get_mutation_by_idx(&self, index: usize) -> Result { + 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(()) + } + +} \ No newline at end of file