conflicts

This commit is contained in:
Andrea Fioraldi 2020-11-22 20:06:41 +01:00
commit cb55ff1e97
10 changed files with 78 additions and 96 deletions

View File

@ -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<I>(_executor: &dyn Executor<I>, 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::<BytesInput, _>::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::<BytesInput>::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 {

View File

@ -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::<BytesInput, DefaultRand>::new(PathBuf::from(
let mut rand = StdRand::new(0);
let mut q = QueueCorpus::new(OnDiskCorpus::<BytesInput, StdRand>::new(PathBuf::from(
"fancy/path",
)));
let t: Rc<_> =

View File

@ -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<C, E, I, R>: HasCorpus<C, I, R>
where
C: Corpus<I, R>,
C: Corpus<I>,
E: Executor<I>,
I: Input,
R: Rand,
{
/// Get executions
fn executions(&self) -> usize;
@ -120,12 +118,11 @@ where
}
}
pub struct DefaultState<C, E, I, R>
pub struct StdState<C, E, I, R>
where
C: Corpus<I, R>,
C: Corpus<I>,
E: Executor<I>,
I: Input,
R: Rand,
{
executions: usize,
metadatas: HashMap<&'static str, Box<dyn StateMetadata>>,
@ -153,12 +150,11 @@ where
}
}
impl<C, E, I, R> State<C, E, I, R> for DefaultState<C, E, I, R>
impl<C, E, I, R> State<C, E, I, R> for StdState<C, E, I, R>
where
C: Corpus<I, R>,
C: Corpus<I>,
E: Executor<I>,
I: Input,
R: Rand,
{
fn executions(&self) -> usize {
self.executions
@ -201,15 +197,14 @@ where
}
}
impl<C, E, I, R> DefaultState<C, E, I, R>
impl<C, E, I, R> StdState<C, E, I, R>
where
C: Corpus<I, R>,
C: Corpus<I>,
E: Executor<I>,
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<S, C, E, EM, I, R>
pub trait Engine<S, C, E, I>
where
S: State<C, E, I, R>,
C: Corpus<I, R>,
E: Executor<I>,
EM: EventManager,
I: Input,
R: Rand,
{
fn stages(&self) -> &[Box<dyn Stage<S, C, E, I, R>>];
@ -252,26 +245,22 @@ where
}
}
pub struct DefaultEngine<S, C, E, EM, I, R>
pub struct StdEngine<S, C, E, I>
where
S: State<C, E, EM, I, R>,
C: Corpus<I, R>,
S: State<C, E, I>,
C: Corpus<I>,
E: Executor<I>,
EM: EventManager,
I: Input,
R: Rand,
{
stages: Vec<Box<dyn Stage<S, C, E, I, R>>>,
}
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
S: State<C, E, I, R>,
C: Corpus<I, R>,
E: Executor<I>,
EM: EventManager,
I: Input,
R: Rand,
{
fn stages(&self) -> &[Box<dyn Stage<S, C, E, I, R>>] {
&self.stages
@ -282,17 +271,15 @@ where
}
}
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
S: State<C, E, I, R>,
C: Corpus<I, R>,
E: Executor<I>,
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<I>(_executor: &dyn Executor<I>, _buf: &[u8]) -> ExitKind {
ExitKind::Ok
@ -319,20 +303,19 @@ mod tests {
#[test]
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();
corpus.add(testcase);
let executor = InMemoryExecutor::<BytesInput>::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));
//

View File

@ -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);

View File

@ -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;

View File

@ -79,7 +79,7 @@ where
}
}
pub struct DefaultScheduledMutator<S, C, I, R>
pub struct StdScheduledMutator<S, C, I, R>
where
C: Corpus<I, R>,
I: Input,
@ -88,7 +88,7 @@ where
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
S: HasRand<R> + HasCorpus<C, I, R>,
C: Corpus<I, R>,
@ -100,7 +100,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
S: HasRand<R> + HasCorpus<C, I, R>,
C: Corpus<I, R>,
@ -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<MutationFunction<Self, S, I>>) -> Self {
DefaultScheduledMutator {
StdScheduledMutator {
mutations: mutations,
}
}
@ -301,15 +301,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
C: Corpus<I, R>,
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::<C, I, R>::new();
let mut scheduled = StdScheduledMutator::<C, I, R>::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::<InMemoryCorpus<_, _>, BytesInput, XKCDRand>::new();
let mut mutator = StdScheduledMutator::<InMemoryCorpus<_, _>, BytesInput, XKCDRand>::new();
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
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<T> for DefaultMapObserver<'a, T>
impl<'a, T> MapObserver<T> 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<Rc<RefCell<Self>>> for DefaultMapObserver<'a, T>
impl<'a, T> Into<Rc<RefCell<Self>>> for StdMapObserver<'a, T>
where
T: Integer + Copy,
{

View File

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

View File

@ -64,7 +64,7 @@ where
}
/// 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
M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>,
@ -79,7 +79,7 @@ where
}
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
M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>,
@ -100,7 +100,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
M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>,
@ -119,7 +119,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
M: Mutator<C, I, R>,
S: State<C, E, EM, I, R>,
@ -131,7 +131,7 @@ where
{
/// Creates a new default mutational stage
pub fn new(mutator: M) -> Self {
DefaultMutationalStage {
StdMutationalStage {
mutator: mutator,
phantom: PhantomData,
}

View File

@ -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<Rc<RefCell<Self>>> 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<R>
pub struct StdHasRand<R>
where
R: Rand,
{
@ -207,7 +207,7 @@ where
}
/// A very basic HasRand
impl<R> HasRand for DefaultHasRand<R>
impl<R> HasRand for StdHasRand<R>
where
R: Rand,
{
@ -220,11 +220,11 @@ where
}
/// A very basic HasRand
impl<R> DefaultHasRand<R>
impl<R> StdHasRand<R>
where
R: Rand,
{
/// Create a new DefaultHasRand, cloning the refcell
/// Create a new StdHasRand, cloning the refcell
pub fn new(rand: &Rc<RefCell<R>>) -> 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);