Default -> Std

This commit is contained in:
Dominik Maier 2020-11-22 19:56:06 +01:00
parent 1a72e8dc4a
commit 1842b11ce0
10 changed files with 97 additions and 175 deletions

View File

@ -8,15 +8,15 @@ use alloc::rc::Rc;
use core::cell::RefCell; use core::cell::RefCell;
use afl::corpus::{Corpus, InMemoryCorpus, Testcase}; 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::inmemory::InMemoryExecutor;
use afl::executors::{Executor, ExitKind}; use afl::executors::{Executor, ExitKind};
use afl::feedbacks::{create_history_map, MaxMapFeedback}; use afl::feedbacks::{create_history_map, MaxMapFeedback};
use afl::inputs::bytes::BytesInput; use afl::inputs::bytes::BytesInput;
use afl::mutators::scheduled::HavocBytesMutator; use afl::mutators::scheduled::HavocBytesMutator;
use afl::observers::DefaultMapObserver; use afl::observers::StdMapObserver;
use afl::stages::mutational::DefaultMutationalStage; use afl::stages::mutational::StdMutationalStage;
use afl::utils::DefaultRand; use afl::utils::StdRand;
const MAP_SIZE: usize = 65536; const MAP_SIZE: usize = 65536;
@ -39,13 +39,13 @@ fn harness<I>(_executor: &dyn Executor<I>, buf: &[u8]) -> ExitKind {
#[no_mangle] #[no_mangle]
pub extern "C" fn afl_libfuzzer_main() { pub extern "C" fn afl_libfuzzer_main() {
let rand = DefaultRand::new(0).into(); let rand = StdRand::new(0).into();
let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand); let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand);
let testcase = Testcase::new(vec![0; 4]).into(); let testcase = Testcase::new(vec![0; 4]).into();
corpus.add(testcase); 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_edges_map },
unsafe { __lafl_max_edges_size as usize }, 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 edges_feedback = MaxMapFeedback::new(edges_observer.clone(), edges_history_map);
let executor = InMemoryExecutor::<BytesInput>::new(harness); let executor = InMemoryExecutor::<BytesInput>::new(harness);
let mut state = DefaultState::new(corpus, executor); let mut state = StdState::new(corpus, executor);
state.add_observer(edges_observer); state.add_observer(edges_observer);
state.add_feedback(Box::new(edges_feedback)); state.add_feedback(Box::new(edges_feedback));
let mut engine = DefaultEngine::new(); let mut engine = StdEngine::new();
let mutator = HavocBytesMutator::new_default(&rand); let mutator = HavocBytesMutator::new_default(&rand);
let stage = DefaultMutationalStage::new(&rand, mutator); let stage = StdMutationalStage::new(&rand, mutator);
engine.add_stage(Box::new(stage)); engine.add_stage(Box::new(stage));
for i in 0..1000 { for i in 0..1000 {

View File

@ -326,15 +326,15 @@ mod tests {
use crate::corpus::Testcase; use crate::corpus::Testcase;
use crate::corpus::{OnDiskCorpus, QueueCorpus}; use crate::corpus::{OnDiskCorpus, QueueCorpus};
use crate::inputs::bytes::BytesInput; use crate::inputs::bytes::BytesInput;
use crate::utils::DefaultRand; use crate::utils::StdRand;
use alloc::rc::Rc; use alloc::rc::Rc;
use std::path::PathBuf; use std::path::PathBuf;
#[test] #[test]
fn test_queuecorpus() { fn test_queuecorpus() {
let mut rand = DefaultRand::new(0); let mut rand = StdRand::new(0);
let mut q = QueueCorpus::new(OnDiskCorpus::<BytesInput, DefaultRand>::new(PathBuf::from( let mut q = QueueCorpus::new(OnDiskCorpus::<BytesInput, StdRand>::new(PathBuf::from(
"fancy/path", "fancy/path",
))); )));
let t: Rc<_> = let t: Rc<_> =

View File

@ -6,30 +6,21 @@ use alloc::vec::Vec;
use core::cell::RefCell; use core::cell::RefCell;
use core::fmt::Debug; use core::fmt::Debug;
use hashbrown::HashMap;
use crate::corpus::{Corpus, Testcase}; use crate::corpus::{Corpus, Testcase};
use crate::events::EventManager;
use crate::executors::Executor; use crate::executors::Executor;
use crate::feedbacks::Feedback; use crate::feedbacks::Feedback;
use crate::inputs::Input; use crate::inputs::Input;
use crate::observers::Observer; use crate::observers::Observer;
use crate::stages::Stage; use crate::stages::Stage;
use crate::utils::{HasRand, Rand};
use crate::AflError; use crate::AflError;
pub trait StateMetadata: Debug { 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 State<C, E, EM, I, R>: HasRand<R = R> pub trait State<C, E, I>
where where
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
/// Get executions /// Get executions
fn executions(&self) -> usize; fn executions(&self) -> usize;
@ -37,21 +28,6 @@ where
/// Set executions /// Set executions
fn set_executions(&mut self, executions: usize); 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<dyn StateMetadata>>;
/// Get all the metadatas into an HashMap (mutable)
fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box<dyn StateMetadata>>;
/// Add a metadata
fn add_metadata(&mut self, meta: Box<dyn StateMetadata>) {
self.metadatas_mut().insert(meta.name(), meta);
}
/// Get the linked observers /// Get the linked observers
fn observers(&self) -> &[Rc<RefCell<dyn Observer>>]; fn observers(&self) -> &[Rc<RefCell<dyn Observer>>];
@ -129,49 +105,24 @@ where
} }
} }
pub struct DefaultState<C, E, EM, I, R> pub struct StdState<C, E, I>
where where
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
rand: R,
executions: usize, executions: usize,
events_manager: EM,
metadatas: HashMap<&'static str, Box<dyn StateMetadata>>,
observers: Vec<Rc<RefCell<dyn Observer>>>, observers: Vec<Rc<RefCell<dyn Observer>>>,
feedbacks: Vec<Box<dyn Feedback<I>>>, feedbacks: Vec<Box<dyn Feedback<I>>>,
corpus: C, corpus: C,
executor: E, executor: E,
} }
impl<C, E, EM, I, R> HasRand for DefaultState<C, E, EM, I, R> impl<C, E, I> State<C, E, I> for StdState<C, E, I>
where where
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, 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<C, E, EM, I, R> State<C, E, EM, I, R> for DefaultState<C, E, EM, I, R>
where
C: Corpus<I, R>,
E: Executor<I>,
EM: EventManager,
I: Input,
R: Rand,
{ {
fn executions(&self) -> usize { fn executions(&self) -> usize {
self.executions self.executions
@ -181,21 +132,6 @@ where
self.executions = executions 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<dyn StateMetadata>> {
&self.metadatas
}
fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box<dyn StateMetadata>> {
&mut self.metadatas
}
fn observers(&self) -> &[Rc<RefCell<dyn Observer>>] { fn observers(&self) -> &[Rc<RefCell<dyn Observer>>] {
&self.observers &self.observers
} }
@ -229,20 +165,15 @@ where
} }
} }
impl<C, E, EM, I, R> DefaultState<C, E, EM, I, R> impl<C, E, I> StdState<C, E, I>
where where
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
pub fn new(corpus: C, executor: E, events_manager: EM, rand: R) -> Self { pub fn new(corpus: C, executor: E) -> Self {
DefaultState { StdState {
rand: rand,
executions: 0, executions: 0,
events_manager: events_manager,
metadatas: HashMap::default(),
observers: vec![], observers: vec![],
feedbacks: vec![], feedbacks: vec![],
corpus: corpus, corpus: corpus,
@ -251,25 +182,24 @@ where
} }
} }
pub trait Engine<S, C, E, EM, I, R> pub trait Engine<S, C, E, I>
where where
S: State<C, E, EM, I, R>, S: State<C, E, I>,
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
fn stages(&self) -> &[Box<dyn Stage<S, C, E, EM, I, R>>]; fn stages(&self) -> &[Box<dyn Stage<S, C, E, I>>];
fn stages_mut(&mut self) -> &mut Vec<Box<dyn Stage<S, C, E, EM, I, R>>>; fn stages_mut(&mut self) -> &mut Vec<Box<dyn Stage<S, C, E, I>>>;
fn add_stage(&mut self, stage: Box<dyn Stage<S, C, E, EM, I, R>>) { fn add_stage(&mut self, stage: Box<dyn Stage<S, C, E, I>>) {
self.stages_mut().push(stage); self.stages_mut().push(stage);
} }
fn fuzz_one(&mut self, state: &mut S) -> Result<usize, AflError> { fn fuzz_one(&mut self, state: &mut S) -> Result<usize, AflError> {
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()); println!("Cur entry: {}\tExecutions: {}", idx, state.executions());
for stage in self.stages_mut() { for stage in self.stages_mut() {
stage.perform(testcase.clone(), state)?; stage.perform(testcase.clone(), state)?;
@ -278,47 +208,41 @@ where
} }
} }
pub struct DefaultEngine<S, C, E, EM, I, R> pub struct StdEngine<S, C, E, I>
where where
S: State<C, E, EM, I, R>, S: State<C, E, I>,
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
stages: Vec<Box<dyn Stage<S, C, E, EM, I, R>>>, stages: Vec<Box<dyn Stage<S, C, E, I>>>,
} }
impl<S, C, E, EM, I, R> Engine<S, C, E, EM, I, R> for DefaultEngine<S, C, E, EM, I, R> impl<S, C, E, I> Engine<S, C, E, I> for StdEngine<S, C, E, I>
where where
S: State<C, E, EM, I, R>, S: State<C, E, I>,
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
fn stages(&self) -> &[Box<dyn Stage<S, C, E, EM, I, R>>] { fn stages(&self) -> &[Box<dyn Stage<S, C, E, I>>] {
&self.stages &self.stages
} }
fn stages_mut(&mut self) -> &mut Vec<Box<dyn Stage<S, C, E, EM, I, R>>> { fn stages_mut(&mut self) -> &mut Vec<Box<dyn Stage<S, C, E, I>>> {
&mut self.stages &mut self.stages
} }
} }
impl<S, C, E, EM, I, R> DefaultEngine<S, C, E, EM, I, R> impl<S, C, E, I> StdEngine<S, C, E, I>
where where
S: State<C, E, EM, I, R>, S: State<C, E, I>,
C: Corpus<I, R>, C: Corpus<I>,
E: Executor<I>, E: Executor<I>,
EM: EventManager,
I: Input, I: Input,
R: Rand,
{ {
pub fn new() -> Self { pub fn new() -> Self {
DefaultEngine { stages: vec![] } StdEngine { stages: vec![] }
} }
} }
@ -328,16 +252,15 @@ mod tests {
use alloc::boxed::Box; use alloc::boxed::Box;
use crate::corpus::{Corpus, InMemoryCorpus, Testcase}; use crate::corpus::{Corpus, InMemoryCorpus, Testcase};
use crate::engines::{DefaultEngine, DefaultState, Engine}; use crate::engines::{StdEngine, StdState, Engine};
use crate::events::LoggerEventManager;
use crate::executors::inmemory::InMemoryExecutor; use crate::executors::inmemory::InMemoryExecutor;
use crate::executors::{Executor, ExitKind}; use crate::executors::{Executor, ExitKind};
use crate::inputs::bytes::BytesInput; use crate::inputs::bytes::BytesInput;
use crate::mutators::scheduled::{ use crate::mutators::scheduled::{
mutation_bitflip, ComposedByMutations, DefaultScheduledMutator, mutation_bitflip, ComposedByMutations, StdScheduledMutator,
}; };
use crate::stages::mutational::DefaultMutationalStage; use crate::stages::mutational::StdMutationalStage;
use crate::utils::DefaultRand; use crate::utils::StdRand;
fn harness<I>(_executor: &dyn Executor<I>, _buf: &[u8]) -> ExitKind { fn harness<I>(_executor: &dyn Executor<I>, _buf: &[u8]) -> ExitKind {
ExitKind::Ok ExitKind::Ok
@ -345,20 +268,19 @@ mod tests {
#[test] #[test]
fn test_engine() { fn test_engine() {
let mut corpus = InMemoryCorpus::<BytesInput, DefaultRand>::new(); let rand = StdRand::new(0).into();
let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand);
let testcase = Testcase::new(vec![0; 4]).into(); let testcase = Testcase::new(vec![0; 4]).into();
corpus.add(testcase); corpus.add(testcase);
let executor = InMemoryExecutor::<BytesInput>::new(harness); let executor = InMemoryExecutor::<BytesInput>::new(harness);
let events = LoggerEventManager::new(); let mut state = StdState::new(corpus, executor);
let rand = DefaultRand::new(0);
let mut state = DefaultState::new(corpus, executor, events, rand); let mut engine = StdEngine::new();
let mut mutator = StdScheduledMutator::new(&rand);
let mut engine = DefaultEngine::new();
let mut mutator = DefaultScheduledMutator::new();
mutator.add_mutation(mutation_bitflip); mutator.add_mutation(mutation_bitflip);
let stage = DefaultMutationalStage::new(mutator); let stage = StdMutationalStage::new(&rand, mutator);
engine.add_stage(Box::new(stage)); engine.add_stage(Box::new(stage));
// //

View File

@ -68,11 +68,11 @@ impl BytesInput {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::utils::{next_pow2, DefaultRand, Rand}; use crate::utils::{next_pow2, StdRand, Rand};
#[test] #[test]
fn test_input() { fn test_input() {
let mut rand = DefaultRand::new(0); let mut rand = StdRand::new(0);
assert_ne!(rand.next(), rand.next()); assert_ne!(rand.next(), rand.next());
assert!(rand.below(100) < 100); assert!(rand.below(100) < 100);
assert_eq!(rand.below(1), 0); assert_eq!(rand.below(1), 0);

View File

@ -1,6 +1,6 @@
pub mod scheduled; pub mod scheduled;
pub use scheduled::ComposedByMutations; pub use scheduled::ComposedByMutations;
pub use scheduled::DefaultScheduledMutator; pub use scheduled::StdScheduledMutator;
pub use scheduled::HavocBytesMutator; pub use scheduled::HavocBytesMutator;
pub use scheduled::ScheduledMutator; pub use scheduled::ScheduledMutator;

View File

@ -79,7 +79,7 @@ where
} }
} }
pub struct DefaultScheduledMutator<S, C, I, R> pub struct StdScheduledMutator<S, C, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input, I: Input,
@ -88,7 +88,7 @@ where
mutations: Vec<MutationFunction<Self, S, I>>, mutations: Vec<MutationFunction<Self, S, I>>,
} }
impl<S, C, I, R> Mutator<S, C, I, R> for DefaultScheduledMutator<S, C, I, R> impl<S, C, I, R> Mutator<S, C, I, R> for StdScheduledMutator<S, C, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input, I: Input,
@ -105,7 +105,7 @@ where
} }
} }
impl<S, C, I, R> ComposedByMutations<S, C, I, R> for DefaultScheduledMutator<S, C, I, R> impl<S, C, I, R> ComposedByMutations<S, C, I, R> for StdScheduledMutator<S, C, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input, I: Input,
@ -127,7 +127,7 @@ where
} }
} }
impl<C, I, R> ScheduledMutator<C, I, R> for DefaultScheduledMutator<C, I, R> impl<C, I, R> ScheduledMutator<C, I, R> for StdScheduledMutator<C, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input, I: Input,
@ -136,20 +136,20 @@ where
// Just use the default methods // Just use the default methods
} }
impl<C, I, R> DefaultScheduledMutator<C, I, R> impl<C, I, R> StdScheduledMutator<C, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input, I: Input,
R: Rand, R: Rand,
{ {
/// Create a new DefaultScheduledMutator instance without mutations and corpus /// Create a new StdScheduledMutator instance without mutations and corpus
pub fn new() -> Self { 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<MutationFunction<Self, S, I>>) -> Self { pub fn new_all(mutations: Vec<MutationFunction<Self, S, I>>) -> Self {
DefaultScheduledMutator { StdScheduledMutator {
mutations: mutations, mutations: mutations,
} }
} }
@ -303,15 +303,15 @@ where
} }
} }
impl<C, I, R> HavocBytesMutator<C, I, DefaultScheduledMutator<C, I, R>, R> impl<C, I, R> HavocBytesMutator<C, I, StdScheduledMutator<C, I, R>, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
I: Input + HasBytesVec, I: Input + HasBytesVec,
R: Rand, R: Rand,
{ {
/// Create a new HavocBytesMutator instance wrapping DefaultScheduledMutator /// Create a new HavocBytesMutator instance wrapping StdScheduledMutator
pub fn new_default() -> Self { pub fn new_default() -> Self {
let mut scheduled = DefaultScheduledMutator::<C, I, R>::new(); let mut scheduled = StdScheduledMutator::<C, I, R>::new();
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
scheduled.add_mutation(mutation_splice); scheduled.add_mutation(mutation_splice);
HavocBytesMutator { HavocBytesMutator {
@ -325,7 +325,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::inputs::BytesInput; 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::utils::{Rand, XKCDRand};
use crate::{ use crate::{
corpus::{Corpus, InMemoryCorpus, Testcase}, corpus::{Corpus, InMemoryCorpus, Testcase},
@ -348,7 +348,7 @@ mod tests {
rand.set_seed(5); rand.set_seed(5);
let mut mutator = let mut mutator =
DefaultScheduledMutator::<InMemoryCorpus<_, _>, BytesInput, XKCDRand>::new(); StdScheduledMutator::<InMemoryCorpus<_, _>, BytesInput, XKCDRand>::new();
mutation_splice(&mut mutator, &mut corpus, &mut rand, &mut input).unwrap(); mutation_splice(&mut mutator, &mut corpus, &mut rand, &mut input).unwrap();

View File

@ -51,7 +51,7 @@ where
} }
} }
pub struct DefaultMapObserver<'a, T> pub struct StdMapObserver<'a, T>
where where
T: Integer + Copy, T: Integer + Copy,
{ {
@ -59,7 +59,7 @@ where
initial: T, initial: T,
} }
impl<'a, T> Observer for DefaultMapObserver<'a, T> impl<'a, T> Observer for StdMapObserver<'a, T>
where where
T: Integer + Copy, T: Integer + Copy,
{ {
@ -68,7 +68,7 @@ where
} }
} }
impl<'a, T> MapObserver<T> for DefaultMapObserver<'a, T> impl<'a, T> MapObserver<T> for StdMapObserver<'a, T>
where where
T: Integer + Copy, T: Integer + Copy,
{ {
@ -93,14 +93,14 @@ where
} }
} }
impl<'a, T> DefaultMapObserver<'a, T> impl<'a, T> StdMapObserver<'a, T>
where where
T: Integer + Copy, T: Integer + Copy,
{ {
/// Creates a new MapObserver /// Creates a new MapObserver
pub fn new(map: &'a mut [T]) -> Self { pub fn new(map: &'a mut [T]) -> Self {
let initial = if map.len() > 0 { map[0] } else { T::zero() }; let initial = if map.len() > 0 { map[0] } else { T::zero() };
DefaultMapObserver { StdMapObserver {
map: map, map: map,
initial: initial, initial: initial,
} }
@ -110,7 +110,7 @@ where
pub fn new_from_ptr(map_ptr: *mut T, len: usize) -> Self { pub fn new_from_ptr(map_ptr: *mut T, len: usize) -> Self {
unsafe { unsafe {
let initial = if len > 0 { *map_ptr } else { T::zero() }; let initial = if len > 0 { *map_ptr } else { T::zero() };
DefaultMapObserver { StdMapObserver {
map: from_raw_parts_mut(map_ptr, len), map: from_raw_parts_mut(map_ptr, len),
initial: initial, initial: initial,
} }
@ -118,7 +118,7 @@ where
} }
} }
impl<'a, T> Into<Rc<RefCell<Self>>> for DefaultMapObserver<'a, T> impl<'a, T> Into<Rc<RefCell<Self>>> for StdMapObserver<'a, T>
where where
T: Integer + Copy, T: Integer + Copy,
{ {

View File

@ -1,5 +1,5 @@
pub mod mutational; pub mod mutational;
pub use mutational::DefaultMutationalStage; pub use mutational::StdMutationalStage;
use crate::corpus::testcase::Testcase; use crate::corpus::testcase::Testcase;
use crate::corpus::Corpus; use crate::corpus::Corpus;

View File

@ -63,7 +63,7 @@ where
} }
/// The default mutational stage /// The default mutational stage
pub struct DefaultMutationalStage<M, S, C, E, EM, I, R> pub struct StdMutationalStage<M, S, C, E, EM, I, R>
where where
M: Mutator<C, I, R>, M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>, S: State<C, E, EM, I, R>,
@ -78,7 +78,7 @@ where
} }
impl<M, S, C, E, EM, I, R> MutationalStage<M, S, C, E, EM, I, R> impl<M, S, C, E, EM, I, R> MutationalStage<M, S, C, E, EM, I, R>
for DefaultMutationalStage<M, S, C, E, EM, I, R> for StdMutationalStage<M, S, C, E, EM, I, R>
where where
M: Mutator<C, I, R>, M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>, S: State<C, E, EM, I, R>,
@ -99,7 +99,7 @@ where
} }
} }
impl<M, S, C, E, EM, I, R> Stage<S, C, E, EM, I, R> for DefaultMutationalStage<M, S, C, E, EM, I, R> impl<M, S, C, E, EM, I, R> Stage<S, C, E, EM, I, R> for StdMutationalStage<M, S, C, E, EM, I, R>
where where
M: Mutator<C, I, R>, M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>, S: State<C, E, EM, I, R>,
@ -118,7 +118,7 @@ where
} }
} }
impl<M, S, C, E, EM, I, R> DefaultMutationalStage<M, S, C, E, EM, I, R> impl<M, S, C, E, EM, I, R> StdMutationalStage<M, S, C, E, EM, I, R>
where where
M: Mutator<C, I, R>, M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>, S: State<C, E, EM, I, R>,
@ -130,7 +130,7 @@ where
{ {
/// Creates a new default mutational stage /// Creates a new default mutational stage
pub fn new(mutator: M) -> Self { pub fn new(mutator: M) -> Self {
DefaultMutationalStage { StdMutationalStage {
mutator: mutator, mutator: mutator,
phantom: PhantomData, phantom: PhantomData,
} }

View File

@ -9,7 +9,7 @@ use xxhash_rust::xxh3::xxh3_64_with_seed;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
pub type DefaultRand = Xoshiro256StarRand; pub type StdRand = Xoshiro256StarRand;
/// Ways to get random around here /// Ways to get random around here
pub trait Rand: Debug { pub trait Rand: Debug {
@ -144,7 +144,7 @@ impl Into<Rc<RefCell<Self>>> for Xoshiro256StarRand {
impl Xoshiro256StarRand { impl Xoshiro256StarRand {
/// Creates a new Xoshiro rand with the given seed /// Creates a new Xoshiro rand with the given seed
pub fn new(seed: u64) -> Self { 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.set_seed(seed); // TODO: Proper random seed?
ret ret
} }
@ -199,7 +199,7 @@ impl XKCDRand {
/* /*
/// A very basic HasRand /// A very basic HasRand
pub struct DefaultHasRand<R> pub struct StdHasRand<R>
where where
R: Rand, R: Rand,
{ {
@ -207,7 +207,7 @@ where
} }
/// A very basic HasRand /// A very basic HasRand
impl<R> HasRand for DefaultHasRand<R> impl<R> HasRand for StdHasRand<R>
where where
R: Rand, R: Rand,
{ {
@ -220,11 +220,11 @@ where
} }
/// A very basic HasRand /// A very basic HasRand
impl<R> DefaultHasRand<R> impl<R> StdHasRand<R>
where where
R: Rand, R: Rand,
{ {
/// Create a new DefaultHasRand, cloning the refcell /// Create a new StdHasRand, cloning the refcell
pub fn new(rand: &Rc<RefCell<R>>) -> Self { pub fn new(rand: &Rc<RefCell<R>>) -> Self {
Self { Self {
rand: Rc::clone(rand), rand: Rc::clone(rand),
@ -246,11 +246,11 @@ pub const fn next_pow2(val: u64) -> u64 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::utils::{next_pow2, DefaultRand, HasRand, Rand}; use crate::utils::{next_pow2, StdRand, HasRand, Rand};
#[test] #[test]
fn test_rand() { fn test_rand() {
let mut rand = DefaultRand::new(0); let mut rand = StdRand::new(0);
assert_ne!(rand.next(), rand.next()); assert_ne!(rand.next(), rand.next());
assert!(rand.below(100) < 100); assert!(rand.below(100) < 100);
assert_eq!(rand.below(1), 0); assert_eq!(rand.below(1), 0);
@ -261,8 +261,8 @@ mod tests {
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[test] #[test]
fn test_rand_preseeded() { fn test_rand_preseeded() {
let mut rand_fixed = DefaultRand::new(0); let mut rand_fixed = StdRand::new(0);
let mut rand = DefaultRand::preseeded(); let mut rand = StdRand::preseeded();
assert_ne!(rand.next(), rand_fixed.next()); assert_ne!(rand.next(), rand_fixed.next());
assert_ne!(rand.next(), rand.next()); assert_ne!(rand.next(), rand.next());
assert!(rand.below(100) < 100); assert!(rand.below(100) < 100);
@ -274,8 +274,8 @@ mod tests {
/* /*
#[test] #[test]
fn test_has_rand() { fn test_has_rand() {
let rand = DefaultRand::new(0).into(); let rand = StdRand::new(0).into();
let has_rand = DefaultHasRand::new(&rand); let has_rand = StdHasRand::new(&rand);
assert!(has_rand.rand_below(100) < 100); assert!(has_rand.rand_below(100) < 100);
assert_eq!(has_rand.rand_below(1), 0); assert_eq!(has_rand.rand_below(1), 0);