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 92bcddd3b0..9dce6960bf 100644 --- a/src/corpus/mod.rs +++ b/src/corpus/mod.rs @@ -339,15 +339,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 a861a15fb0..92c36404a5 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -15,7 +15,6 @@ use crate::feedbacks::Feedback; use crate::inputs::Input; use crate::observers::Observer; use crate::stages::Stage; -use crate::utils::{HasRand, Rand}; use crate::AflError; // TODO FeedbackMetadata to store histroy_map @@ -27,10 +26,9 @@ pub trait StateMetadata: Debug { pub trait State: HasCorpus where - C: Corpus, + C: Corpus, E: Executor, I: Input, - R: Rand, { /// Get executions fn executions(&self) -> usize; @@ -120,12 +118,11 @@ where } } -pub struct DefaultState +pub struct StdState where - C: Corpus, + C: Corpus, E: Executor, I: Input, - R: Rand, { executions: usize, metadatas: HashMap<&'static str, Box>, @@ -153,12 +150,11 @@ where } } -impl State for DefaultState +impl State for StdState where - C: Corpus, + C: Corpus, E: Executor, I: Input, - R: Rand, { fn executions(&self) -> usize { self.executions @@ -201,15 +197,14 @@ where } } -impl DefaultState +impl StdState where - C: Corpus, + C: Corpus, E: Executor, I: Input, - R: Rand, { pub fn new(corpus: C, executor: E) -> Self { - DefaultState { + StdState { executions: 0, metadatas: HashMap::default(), observers: vec![], @@ -220,14 +215,12 @@ where } } -pub trait Engine +pub trait Engine where S: State, C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { fn stages(&self) -> &[Box>]; @@ -252,26 +245,22 @@ 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>>, } -impl Engine for DefaultEngine +impl Engine for StdEngine where S: State, C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { fn stages(&self) -> &[Box>] { &self.stages @@ -282,17 +271,15 @@ where } } -impl DefaultEngine +impl StdEngine where S: State, C: Corpus, E: Executor, - EM: EventManager, I: Input, - R: Rand, { pub fn new() -> Self { - DefaultEngine { stages: vec![] } + StdEngine { stages: vec![] } } } @@ -302,16 +289,13 @@ 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::{Engine, StdEngine, StdState}; use crate::executors::inmemory::InMemoryExecutor; use crate::executors::{Executor, ExitKind}; use crate::inputs::bytes::BytesInput; - use crate::mutators::scheduled::{ - mutation_bitflip, ComposedByMutations, DefaultScheduledMutator, - }; - use crate::stages::mutational::DefaultMutationalStage; - use crate::utils::DefaultRand; + use crate::mutators::scheduled::{mutation_bitflip, ComposedByMutations, StdScheduledMutator}; + use crate::stages::mutational::StdMutationalStage; + use crate::utils::StdRand; fn harness(_executor: &dyn Executor, _buf: &[u8]) -> ExitKind { ExitKind::Ok @@ -319,20 +303,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 28ee775e27..7df7a2d26a 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 5046307eb0..f1b5b71e05 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 S: HasRand + HasCorpus, C: Corpus, @@ -100,7 +100,7 @@ where } } -impl ComposedByMutations for DefaultScheduledMutator +impl ComposedByMutations for StdScheduledMutator where S: HasRand + HasCorpus, C: Corpus, @@ -140,14 +140,14 @@ where 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, } } @@ -301,15 +301,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 { @@ -323,7 +323,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}, @@ -345,8 +345,7 @@ mod tests { let mut input = testcase.load_input().expect("No input in testcase").clone(); rand.set_seed(5); - let mut mutator = - DefaultScheduledMutator::, BytesInput, XKCDRand>::new(); + let mut mutator = 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 b70318fb0f..1499834870 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 79c1129ebe..a81d6ea2e6 100644 --- a/src/stages/mutational.rs +++ b/src/stages/mutational.rs @@ -64,7 +64,7 @@ where } /// The default mutational stage -pub struct DefaultMutationalStage +pub struct StdMutationalStage where M: Mutator, S: State, @@ -79,7 +79,7 @@ where } impl MutationalStage - for DefaultMutationalStage + for StdMutationalStage where M: Mutator, S: State, @@ -100,7 +100,7 @@ where } } -impl Stage for DefaultMutationalStage +impl Stage for StdMutationalStage where M: Mutator, S: State, @@ -119,7 +119,7 @@ where } } -impl DefaultMutationalStage +impl StdMutationalStage where M: Mutator, S: State, @@ -131,7 +131,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 77261a2f25..b6be491b46 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, HasRand, Rand, StdRand}; #[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);