From 1842b11ce0c4f692c84a0582ba22183518c41cd8 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Sun, 22 Nov 2020 19:56:06 +0100 Subject: [PATCH] Default -> Std --- fuzzers/libfuzzer/src/lib.rs | 18 ++-- src/corpus/mod.rs | 6 +- src/engines/mod.rs | 164 +++++++++-------------------------- src/inputs/bytes.rs | 4 +- src/mutators/mod.rs | 2 +- src/mutators/scheduled.rs | 28 +++--- src/observers/mod.rs | 14 +-- src/stages/mod.rs | 2 +- src/stages/mutational.rs | 10 +-- src/utils.rs | 24 ++--- 10 files changed, 97 insertions(+), 175 deletions(-) diff --git a/fuzzers/libfuzzer/src/lib.rs b/fuzzers/libfuzzer/src/lib.rs index 85deac5a27..3b5cb40820 100644 --- a/fuzzers/libfuzzer/src/lib.rs +++ b/fuzzers/libfuzzer/src/lib.rs @@ -8,15 +8,15 @@ use alloc::rc::Rc; use core::cell::RefCell; use afl::corpus::{Corpus, InMemoryCorpus, Testcase}; -use afl::engines::{DefaultEngine, DefaultState, Engine, State}; +use afl::engines::{StdEngine, StdState, Engine, State}; use afl::executors::inmemory::InMemoryExecutor; use afl::executors::{Executor, ExitKind}; use afl::feedbacks::{create_history_map, MaxMapFeedback}; use afl::inputs::bytes::BytesInput; use afl::mutators::scheduled::HavocBytesMutator; -use afl::observers::DefaultMapObserver; -use afl::stages::mutational::DefaultMutationalStage; -use afl::utils::DefaultRand; +use afl::observers::StdMapObserver; +use afl::stages::mutational::StdMutationalStage; +use afl::utils::StdRand; const MAP_SIZE: usize = 65536; @@ -39,13 +39,13 @@ fn harness(_executor: &dyn Executor, buf: &[u8]) -> ExitKind { #[no_mangle] pub extern "C" fn afl_libfuzzer_main() { - let rand = DefaultRand::new(0).into(); + let rand = StdRand::new(0).into(); let mut corpus = InMemoryCorpus::::new(&rand); let testcase = Testcase::new(vec![0; 4]).into(); corpus.add(testcase); - let edges_observer = Rc::new(RefCell::new(DefaultMapObserver::new_from_ptr( + let edges_observer = Rc::new(RefCell::new(StdMapObserver::new_from_ptr( unsafe { __lafl_edges_map }, unsafe { __lafl_max_edges_size as usize }, ))); @@ -53,13 +53,13 @@ pub extern "C" fn afl_libfuzzer_main() { let edges_feedback = MaxMapFeedback::new(edges_observer.clone(), edges_history_map); let executor = InMemoryExecutor::::new(harness); - let mut state = DefaultState::new(corpus, executor); + let mut state = StdState::new(corpus, executor); state.add_observer(edges_observer); state.add_feedback(Box::new(edges_feedback)); - let mut engine = DefaultEngine::new(); + let mut engine = StdEngine::new(); let mutator = HavocBytesMutator::new_default(&rand); - let stage = DefaultMutationalStage::new(&rand, mutator); + let stage = StdMutationalStage::new(&rand, mutator); engine.add_stage(Box::new(stage)); for i in 0..1000 { diff --git a/src/corpus/mod.rs b/src/corpus/mod.rs index 8d9cfc2c7b..4a065e3a6d 100644 --- a/src/corpus/mod.rs +++ b/src/corpus/mod.rs @@ -326,15 +326,15 @@ mod tests { use crate::corpus::Testcase; use crate::corpus::{OnDiskCorpus, QueueCorpus}; use crate::inputs::bytes::BytesInput; - use crate::utils::DefaultRand; + use crate::utils::StdRand; use alloc::rc::Rc; use std::path::PathBuf; #[test] fn test_queuecorpus() { - let mut rand = DefaultRand::new(0); - let mut q = QueueCorpus::new(OnDiskCorpus::::new(PathBuf::from( + let mut rand = StdRand::new(0); + let mut q = QueueCorpus::new(OnDiskCorpus::::new(PathBuf::from( "fancy/path", ))); let t: Rc<_> = diff --git a/src/engines/mod.rs b/src/engines/mod.rs index e018bd9688..8b66359704 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -6,30 +6,21 @@ use alloc::vec::Vec; use core::cell::RefCell; use core::fmt::Debug; -use hashbrown::HashMap; - use crate::corpus::{Corpus, Testcase}; -use crate::events::EventManager; use crate::executors::Executor; use crate::feedbacks::Feedback; use crate::inputs::Input; use crate::observers::Observer; use crate::stages::Stage; -use crate::utils::{HasRand, Rand}; use crate::AflError; -pub trait StateMetadata: Debug { - /// The name of this metadata - used to find it in the list of avaliable metadatas - fn name(&self) -> &'static str; -} +pub trait StateMetadata: Debug {} -pub trait State: HasRand +pub trait State where - C: Corpus, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { /// Get executions fn executions(&self) -> usize; @@ -37,21 +28,6 @@ where /// Set executions fn set_executions(&mut self, executions: usize); - fn events_manager(&self) -> &EM; - - fn events_manager_mut(&mut self) -> &mut EM; - - /// Get all the metadatas into an HashMap - fn metadatas(&self) -> &HashMap<&'static str, Box>; - - /// Get all the metadatas into an HashMap (mutable) - fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box>; - - /// Add a metadata - fn add_metadata(&mut self, meta: Box) { - self.metadatas_mut().insert(meta.name(), meta); - } - /// Get the linked observers fn observers(&self) -> &[Rc>]; @@ -129,49 +105,24 @@ where } } -pub struct DefaultState +pub struct StdState where - C: Corpus, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { - rand: R, executions: usize, - events_manager: EM, - metadatas: HashMap<&'static str, Box>, observers: Vec>>, feedbacks: Vec>>, corpus: C, executor: E, } -impl HasRand for DefaultState +impl State for StdState where - C: Corpus, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, -{ - type R = R; - - fn rand(&self) -> &Self::R { - &self.rand - } - fn rand_mut(&mut self) -> &mut Self::R { - &mut self.rand - } -} - -impl State for DefaultState -where - C: Corpus, - E: Executor, - EM: EventManager, - I: Input, - R: Rand, { fn executions(&self) -> usize { self.executions @@ -181,21 +132,6 @@ where self.executions = executions } - fn events_manager(&self) -> &EM { - &self.events_manager - } - fn events_manager_mut(&mut self) -> &mut EM { - &mut self.events_manager - } - - fn metadatas(&self) -> &HashMap<&'static str, Box> { - &self.metadatas - } - - fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box> { - &mut self.metadatas - } - fn observers(&self) -> &[Rc>] { &self.observers } @@ -229,20 +165,15 @@ where } } -impl DefaultState +impl StdState where - C: Corpus, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { - pub fn new(corpus: C, executor: E, events_manager: EM, rand: R) -> Self { - DefaultState { - rand: rand, + pub fn new(corpus: C, executor: E) -> Self { + StdState { executions: 0, - events_manager: events_manager, - metadatas: HashMap::default(), observers: vec![], feedbacks: vec![], corpus: corpus, @@ -251,25 +182,24 @@ where } } -pub trait Engine +pub trait Engine where - S: State, - C: Corpus, + S: State, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { - fn stages(&self) -> &[Box>]; + fn stages(&self) -> &[Box>]; - fn stages_mut(&mut self) -> &mut Vec>>; + fn stages_mut(&mut self) -> &mut Vec>>; - fn add_stage(&mut self, stage: Box>) { + fn add_stage(&mut self, stage: Box>) { self.stages_mut().push(stage); } fn fuzz_one(&mut self, state: &mut S) -> Result { - let (testcase, idx) = state.corpus_mut().next(state.rand_mut())?; + let (testcase, idx) = state.corpus_mut().next()?; + #[cfg(feature = "std")] println!("Cur entry: {}\tExecutions: {}", idx, state.executions()); for stage in self.stages_mut() { stage.perform(testcase.clone(), state)?; @@ -278,47 +208,41 @@ where } } -pub struct DefaultEngine +pub struct StdEngine where - S: State, - C: Corpus, + S: State, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { - stages: Vec>>, + stages: Vec>>, } -impl Engine for DefaultEngine +impl Engine for StdEngine where - S: State, - C: Corpus, + S: State, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { - fn stages(&self) -> &[Box>] { + fn stages(&self) -> &[Box>] { &self.stages } - fn stages_mut(&mut self) -> &mut Vec>> { + fn stages_mut(&mut self) -> &mut Vec>> { &mut self.stages } } -impl DefaultEngine +impl StdEngine where - S: State, - C: Corpus, + S: State, + C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { pub fn new() -> Self { - DefaultEngine { stages: vec![] } + StdEngine { stages: vec![] } } } @@ -328,16 +252,15 @@ mod tests { use alloc::boxed::Box; use crate::corpus::{Corpus, InMemoryCorpus, Testcase}; - use crate::engines::{DefaultEngine, DefaultState, Engine}; - use crate::events::LoggerEventManager; + use crate::engines::{StdEngine, StdState, Engine}; use crate::executors::inmemory::InMemoryExecutor; use crate::executors::{Executor, ExitKind}; use crate::inputs::bytes::BytesInput; use crate::mutators::scheduled::{ - mutation_bitflip, ComposedByMutations, DefaultScheduledMutator, + mutation_bitflip, ComposedByMutations, StdScheduledMutator, }; - use crate::stages::mutational::DefaultMutationalStage; - use crate::utils::DefaultRand; + use crate::stages::mutational::StdMutationalStage; + use crate::utils::StdRand; fn harness(_executor: &dyn Executor, _buf: &[u8]) -> ExitKind { ExitKind::Ok @@ -345,20 +268,19 @@ mod tests { #[test] fn test_engine() { - let mut corpus = InMemoryCorpus::::new(); + let rand = StdRand::new(0).into(); + + let mut corpus = InMemoryCorpus::::new(&rand); let testcase = Testcase::new(vec![0; 4]).into(); corpus.add(testcase); let executor = InMemoryExecutor::::new(harness); - let events = LoggerEventManager::new(); - let rand = DefaultRand::new(0); + let mut state = StdState::new(corpus, executor); - let mut state = DefaultState::new(corpus, executor, events, rand); - - let mut engine = DefaultEngine::new(); - let mut mutator = DefaultScheduledMutator::new(); + let mut engine = StdEngine::new(); + let mut mutator = StdScheduledMutator::new(&rand); mutator.add_mutation(mutation_bitflip); - let stage = DefaultMutationalStage::new(mutator); + let stage = StdMutationalStage::new(&rand, mutator); engine.add_stage(Box::new(stage)); // diff --git a/src/inputs/bytes.rs b/src/inputs/bytes.rs index 54258c4184..e9e010c71c 100644 --- a/src/inputs/bytes.rs +++ b/src/inputs/bytes.rs @@ -68,11 +68,11 @@ impl BytesInput { #[cfg(test)] mod tests { - use crate::utils::{next_pow2, DefaultRand, Rand}; + use crate::utils::{next_pow2, StdRand, Rand}; #[test] fn test_input() { - let mut rand = DefaultRand::new(0); + let mut rand = StdRand::new(0); assert_ne!(rand.next(), rand.next()); assert!(rand.below(100) < 100); assert_eq!(rand.below(1), 0); diff --git a/src/mutators/mod.rs b/src/mutators/mod.rs index 0c0ecdb9ec..fedae267c5 100644 --- a/src/mutators/mod.rs +++ b/src/mutators/mod.rs @@ -1,6 +1,6 @@ pub mod scheduled; pub use scheduled::ComposedByMutations; -pub use scheduled::DefaultScheduledMutator; +pub use scheduled::StdScheduledMutator; pub use scheduled::HavocBytesMutator; pub use scheduled::ScheduledMutator; diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index 1c5460aa17..d50684e3f5 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -79,7 +79,7 @@ where } } -pub struct DefaultScheduledMutator +pub struct StdScheduledMutator where C: Corpus, I: Input, @@ -88,7 +88,7 @@ where mutations: Vec>, } -impl Mutator for DefaultScheduledMutator +impl Mutator for StdScheduledMutator where C: Corpus, I: Input, @@ -105,7 +105,7 @@ where } } -impl ComposedByMutations for DefaultScheduledMutator +impl ComposedByMutations for StdScheduledMutator where C: Corpus, I: Input, @@ -127,7 +127,7 @@ where } } -impl ScheduledMutator for DefaultScheduledMutator +impl ScheduledMutator for StdScheduledMutator where C: Corpus, I: Input, @@ -136,20 +136,20 @@ where // Just use the default methods } -impl DefaultScheduledMutator +impl StdScheduledMutator where C: Corpus, I: Input, R: Rand, { - /// Create a new DefaultScheduledMutator instance without mutations and corpus + /// Create a new StdScheduledMutator instance without mutations and corpus pub fn new() -> Self { - DefaultScheduledMutator { mutations: vec![] } + StdScheduledMutator { mutations: vec![] } } - /// Create a new DefaultScheduledMutator instance specifying mutations and corpus too + /// Create a new StdScheduledMutator instance specifying mutations and corpus too pub fn new_all(mutations: Vec>) -> Self { - DefaultScheduledMutator { + StdScheduledMutator { mutations: mutations, } } @@ -303,15 +303,15 @@ where } } -impl HavocBytesMutator, R> +impl HavocBytesMutator, R> where C: Corpus, I: Input + HasBytesVec, R: Rand, { - /// Create a new HavocBytesMutator instance wrapping DefaultScheduledMutator + /// Create a new HavocBytesMutator instance wrapping StdScheduledMutator pub fn new_default() -> Self { - let mut scheduled = DefaultScheduledMutator::::new(); + let mut scheduled = StdScheduledMutator::::new(); scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_splice); HavocBytesMutator { @@ -325,7 +325,7 @@ where #[cfg(test)] mod tests { use crate::inputs::BytesInput; - use crate::mutators::scheduled::{mutation_splice, DefaultScheduledMutator}; + use crate::mutators::scheduled::{mutation_splice, StdScheduledMutator}; use crate::utils::{Rand, XKCDRand}; use crate::{ corpus::{Corpus, InMemoryCorpus, Testcase}, @@ -348,7 +348,7 @@ mod tests { rand.set_seed(5); let mut mutator = - DefaultScheduledMutator::, BytesInput, XKCDRand>::new(); + StdScheduledMutator::, BytesInput, XKCDRand>::new(); mutation_splice(&mut mutator, &mut corpus, &mut rand, &mut input).unwrap(); diff --git a/src/observers/mod.rs b/src/observers/mod.rs index 5df1e17edc..eae56fe435 100644 --- a/src/observers/mod.rs +++ b/src/observers/mod.rs @@ -51,7 +51,7 @@ where } } -pub struct DefaultMapObserver<'a, T> +pub struct StdMapObserver<'a, T> where T: Integer + Copy, { @@ -59,7 +59,7 @@ where initial: T, } -impl<'a, T> Observer for DefaultMapObserver<'a, T> +impl<'a, T> Observer for StdMapObserver<'a, T> where T: Integer + Copy, { @@ -68,7 +68,7 @@ where } } -impl<'a, T> MapObserver for DefaultMapObserver<'a, T> +impl<'a, T> MapObserver for StdMapObserver<'a, T> where T: Integer + Copy, { @@ -93,14 +93,14 @@ where } } -impl<'a, T> DefaultMapObserver<'a, T> +impl<'a, T> StdMapObserver<'a, T> where T: Integer + Copy, { /// Creates a new MapObserver pub fn new(map: &'a mut [T]) -> Self { let initial = if map.len() > 0 { map[0] } else { T::zero() }; - DefaultMapObserver { + StdMapObserver { map: map, initial: initial, } @@ -110,7 +110,7 @@ where pub fn new_from_ptr(map_ptr: *mut T, len: usize) -> Self { unsafe { let initial = if len > 0 { *map_ptr } else { T::zero() }; - DefaultMapObserver { + StdMapObserver { map: from_raw_parts_mut(map_ptr, len), initial: initial, } @@ -118,7 +118,7 @@ where } } -impl<'a, T> Into>> for DefaultMapObserver<'a, T> +impl<'a, T> Into>> for StdMapObserver<'a, T> where T: Integer + Copy, { diff --git a/src/stages/mod.rs b/src/stages/mod.rs index 8d06aa2fb8..d6acfc3660 100644 --- a/src/stages/mod.rs +++ b/src/stages/mod.rs @@ -1,5 +1,5 @@ pub mod mutational; -pub use mutational::DefaultMutationalStage; +pub use mutational::StdMutationalStage; use crate::corpus::testcase::Testcase; use crate::corpus::Corpus; diff --git a/src/stages/mutational.rs b/src/stages/mutational.rs index f700781396..2da47f5ec9 100644 --- a/src/stages/mutational.rs +++ b/src/stages/mutational.rs @@ -63,7 +63,7 @@ where } /// The default mutational stage -pub struct DefaultMutationalStage +pub struct StdMutationalStage where M: Mutator, S: State, @@ -78,7 +78,7 @@ where } impl MutationalStage - for DefaultMutationalStage + for StdMutationalStage where M: Mutator, S: State, @@ -99,7 +99,7 @@ where } } -impl Stage for DefaultMutationalStage +impl Stage for StdMutationalStage where M: Mutator, S: State, @@ -118,7 +118,7 @@ where } } -impl DefaultMutationalStage +impl StdMutationalStage where M: Mutator, S: State, @@ -130,7 +130,7 @@ where { /// Creates a new default mutational stage pub fn new(mutator: M) -> Self { - DefaultMutationalStage { + StdMutationalStage { mutator: mutator, phantom: PhantomData, } diff --git a/src/utils.rs b/src/utils.rs index e7db765b8f..8d23dcefa7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,7 +9,7 @@ use xxhash_rust::xxh3::xxh3_64_with_seed; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; -pub type DefaultRand = Xoshiro256StarRand; +pub type StdRand = Xoshiro256StarRand; /// Ways to get random around here pub trait Rand: Debug { @@ -144,7 +144,7 @@ impl Into>> for Xoshiro256StarRand { impl Xoshiro256StarRand { /// Creates a new Xoshiro rand with the given seed pub fn new(seed: u64) -> Self { - let mut ret: Xoshiro256StarRand = Default::default(); + let mut ret: Self = Default::default(); ret.set_seed(seed); // TODO: Proper random seed? ret } @@ -199,7 +199,7 @@ impl XKCDRand { /* /// A very basic HasRand -pub struct DefaultHasRand +pub struct StdHasRand where R: Rand, { @@ -207,7 +207,7 @@ where } /// A very basic HasRand -impl HasRand for DefaultHasRand +impl HasRand for StdHasRand where R: Rand, { @@ -220,11 +220,11 @@ where } /// A very basic HasRand -impl DefaultHasRand +impl StdHasRand where R: Rand, { - /// Create a new DefaultHasRand, cloning the refcell + /// Create a new StdHasRand, cloning the refcell pub fn new(rand: &Rc>) -> Self { Self { rand: Rc::clone(rand), @@ -246,11 +246,11 @@ pub const fn next_pow2(val: u64) -> u64 { #[cfg(test)] mod tests { - use crate::utils::{next_pow2, DefaultRand, HasRand, Rand}; + use crate::utils::{next_pow2, StdRand, HasRand, Rand}; #[test] fn test_rand() { - let mut rand = DefaultRand::new(0); + let mut rand = StdRand::new(0); assert_ne!(rand.next(), rand.next()); assert!(rand.below(100) < 100); assert_eq!(rand.below(1), 0); @@ -261,8 +261,8 @@ mod tests { #[cfg(feature = "std")] #[test] fn test_rand_preseeded() { - let mut rand_fixed = DefaultRand::new(0); - let mut rand = DefaultRand::preseeded(); + let mut rand_fixed = StdRand::new(0); + let mut rand = StdRand::preseeded(); assert_ne!(rand.next(), rand_fixed.next()); assert_ne!(rand.next(), rand.next()); assert!(rand.below(100) < 100); @@ -274,8 +274,8 @@ mod tests { /* #[test] fn test_has_rand() { - let rand = DefaultRand::new(0).into(); - let has_rand = DefaultHasRand::new(&rand); + let rand = StdRand::new(0).into(); + let has_rand = StdHasRand::new(&rand); assert!(has_rand.rand_below(100) < 100); assert_eq!(has_rand.rand_below(1), 0);