better events

This commit is contained in:
Andrea Fioraldi 2020-11-24 10:44:41 +01:00
commit 2e40ecd660
14 changed files with 51 additions and 63 deletions

View File

@ -14,9 +14,7 @@ default = ["std"]
std = [] std = []
[dependencies] [dependencies]
xxhash-rust = { version = "0.8.0-beta.5", features = ["xxh3"] } # xxh3 hashing for rust
hashbrown = "0.9" # A faster hashmap, nostd compatible hashbrown = "0.9" # A faster hashmap, nostd compatible
libc = "0.2" # For (*nix) libc libc = "0.2" # For (*nix) libc
num = "*" num = "*"
xxhash-rust = { version = "0.8.0-beta.5", features = ["xxh3"] } # xxh3 hashing for rust
#shared_memory = { version = "0.11.3", optional = true } # shared mem for windows and unix

View File

@ -9,9 +9,9 @@ use core::marker::PhantomData;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::path::PathBuf; use std::path::PathBuf;
use crate::AflError;
use crate::inputs::Input; use crate::inputs::Input;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError;
pub trait HasTestcaseVec<I> pub trait HasTestcaseVec<I>
where where

View File

@ -1,6 +1,3 @@
use crate::inputs::Input;
use crate::AflError;
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::rc::Rc; use alloc::rc::Rc;
use alloc::string::String; use alloc::string::String;
@ -10,6 +7,9 @@ use core::default::Default;
use core::option::Option; use core::option::Option;
use hashbrown::HashMap; use hashbrown::HashMap;
use crate::AflError;
use crate::inputs::Input;
// TODO PathBuf for no_std and change filename to PathBuf // TODO PathBuf for no_std and change filename to PathBuf
//#[cfg(feature = "std")] //#[cfg(feature = "std")]
//use std::path::PathBuf; //use std::path::PathBuf;

View File

@ -6,11 +6,10 @@ use alloc::vec::Vec;
use core::cell::RefCell; use core::cell::RefCell;
use core::fmt::Debug; use core::fmt::Debug;
use core::marker::PhantomData; use core::marker::PhantomData;
use hashbrown::HashMap; use hashbrown::HashMap;
use crate::corpus::{Corpus, HasCorpus, Testcase}; use crate::corpus::{Corpus, HasCorpus, Testcase};
use crate::events::{EventManager, LoadInitialEvent}; use crate::events::{EventManager, LoadInitialEvent, UpdateStatsEvent};
use crate::executors::Executor; use crate::executors::Executor;
use crate::feedbacks::Feedback; use crate::feedbacks::Feedback;
use crate::generators::Generator; use crate::generators::Generator;
@ -327,7 +326,7 @@ where
let cur = current_milliseconds(); let cur = current_milliseconds();
if cur - last > 60 * 100 { if cur - last > 60 * 100 {
last = cur; last = cur;
fire_event!(events, LoadInitialEvent)?; fire_event!(events, UpdateStatsEvent)?;
} }
} }
} }
@ -385,10 +384,13 @@ where
mod tests { mod tests {
use alloc::boxed::Box; use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::io::stderr; use std::io::stderr;
use crate::corpus::{Corpus, InMemoryCorpus, Testcase}; use crate::corpus::{Corpus, InMemoryCorpus, Testcase};
use crate::engines::{Engine, StdEngine, StdState}; use crate::engines::{Engine, StdEngine, StdState};
#[cfg(feature = "std")]
use crate::events::LoggerEventManager; use crate::events::LoggerEventManager;
use crate::executors::inmemory::InMemoryExecutor; use crate::executors::inmemory::InMemoryExecutor;
use crate::executors::{Executor, ExitKind}; use crate::executors::{Executor, ExitKind};
@ -401,6 +403,7 @@ mod tests {
ExitKind::Ok ExitKind::Ok
} }
#[cfg(feature = "std")]
#[test] #[test]
fn test_engine() { fn test_engine() {
let mut rand = StdRand::new(0); let mut rand = StdRand::new(0);

View File

@ -1,13 +1,17 @@
#[cfg(feature = "std")]
pub mod llmp; pub mod llmp;
#[cfg(feature = "std")]
pub mod llmp_translated; // TODO: Abstract away. pub mod llmp_translated; // TODO: Abstract away.
#[cfg(feature = "std")]
pub mod shmem_translated; pub mod shmem_translated;
#[cfg(feature = "std")]
pub use crate::events::llmp::LLMP; pub use crate::events::llmp::LLMP;
use alloc::rc::Rc; use alloc::rc::Rc;
use core::any::Any;
use core::cell::RefCell; use core::cell::RefCell;
//use core::any::TypeId; //use core::any::TypeId;
// TODO use core version #[cfg(feature = "std")]
use std::io::Write; use std::io::Write;
use crate::corpus::{Corpus, Testcase}; use crate::corpus::{Corpus, Testcase};
@ -17,7 +21,7 @@ use crate::inputs::Input;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError; use crate::AflError;
pub trait Event: Any { pub trait Event {
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
} }
@ -86,7 +90,7 @@ where
testcase: Rc<RefCell<Testcase<I>>>, testcase: Rc<RefCell<Testcase<I>>>,
} }
impl<I> Event<I> for NewTestcaseEvent<I> impl<I> Event for NewTestcaseEvent<I>
where where
I: Input, I: Input,
{ {
@ -102,6 +106,10 @@ where
pub fn new(testcase: Rc<RefCell<Testcase<I>>>) -> Self { pub fn new(testcase: Rc<RefCell<Testcase<I>>>) -> Self {
NewTestcaseEvent { testcase: testcase } NewTestcaseEvent { testcase: testcase }
} }
pub fn testcase(&self) -> &Rc<RefCell<Testcase<I>>> {
&self.testcase
}
} }
pub struct UpdateStatsEvent {} pub struct UpdateStatsEvent {}
@ -116,14 +124,16 @@ impl UpdateStatsEvent {
} }
} }
#[cfg(feature = "std")]
pub struct LoggerEventManager<W> pub struct LoggerEventManager<W>
where where
W: Write, W: Write,
{ {
events: Vec<Box<dyn Event>>, events: Vec<String>,
writer: W, writer: W,
} }
#[cfg(feature = "std")]
impl<S, C, E, I, R, W> EventManager<S, C, E, I, R> for LoggerEventManager<W> impl<S, C, E, I, R, W> EventManager<S, C, E, I, R> for LoggerEventManager<W>
where where
S: State<C, E, I, R>, S: State<C, E, I, R>,
@ -151,7 +161,7 @@ where
where where
T: Event, T: Event,
{ {
self.events.push(Box::new(event)); self.events.push(event.name().to_string());
Ok(()) Ok(())
} }
@ -162,7 +172,7 @@ where
&mut self.writer, &mut self.writer,
"#{}\t[{}] corp: {} exec/s: {}", "#{}\t[{}] corp: {} exec/s: {}",
state.executions(), state.executions(),
event.name(), event,
state.corpus().entries().len(), state.corpus().entries().len(),
state.executions_over_seconds() state.executions_over_seconds()
)?; )?;
@ -172,6 +182,7 @@ where
} }
} }
#[cfg(feature = "std")]
impl<W> LoggerEventManager<W> impl<W> LoggerEventManager<W>
where where
W: Write, W: Write,

View File

@ -3,9 +3,9 @@ use core::cell::RefCell;
use core::ffi::c_void; use core::ffi::c_void;
use core::ptr; use core::ptr;
use crate::AflError;
use crate::executors::{Executor, ExitKind}; use crate::executors::{Executor, ExitKind};
use crate::inputs::Input; use crate::inputs::Input;
use crate::AflError;
type HarnessFunction<I> = fn(&dyn Executor<I>, &[u8]) -> ExitKind; type HarnessFunction<I> = fn(&dyn Executor<I>, &[u8]) -> ExitKind;

View File

@ -1,15 +1,14 @@
extern crate num;
use alloc::rc::Rc; use alloc::rc::Rc;
use alloc::vec::Vec; use alloc::vec::Vec;
use alloc::boxed::Box;
use core::cell::RefCell; use core::cell::RefCell;
use core::marker::PhantomData; use core::marker::PhantomData;
use num::Integer; use num::Integer;
use crate::AflError;
use crate::corpus::{Testcase, TestcaseMetadata}; use crate::corpus::{Testcase, TestcaseMetadata};
use crate::inputs::Input; use crate::inputs::Input;
use crate::observers::MapObserver; use crate::observers::MapObserver;
use crate::AflError;
pub trait Feedback<I> pub trait Feedback<I>
where where

View File

@ -2,10 +2,10 @@ use alloc::vec::Vec;
use core::cmp::min; use core::cmp::min;
use core::marker::PhantomData; use core::marker::PhantomData;
use crate::AflError;
use crate::inputs::bytes::BytesInput; use crate::inputs::bytes::BytesInput;
use crate::inputs::Input; use crate::inputs::Input;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError;
pub trait Generator<I, R> pub trait Generator<I, R>
where where

View File

@ -4,8 +4,8 @@ use alloc::vec::Vec;
use core::cell::RefCell; use core::cell::RefCell;
use core::convert::From; use core::convert::From;
use crate::inputs::{HasBytesVec, HasTargetBytes, Input};
use crate::AflError; use crate::AflError;
use crate::inputs::{HasBytesVec, HasTargetBytes, Input};
/// A bytes input is the basic input /// A bytes input is the basic input
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]

View File

@ -4,14 +4,14 @@ pub use scheduled::HavocBytesMutator;
pub use scheduled::ScheduledMutator; pub use scheduled::ScheduledMutator;
pub use scheduled::StdScheduledMutator; pub use scheduled::StdScheduledMutator;
use alloc::rc::Rc;
use core::cell::RefCell;
use crate::AflError;
use crate::corpus::Corpus; use crate::corpus::Corpus;
use crate::corpus::Testcase; use crate::corpus::Testcase;
use crate::inputs::Input; use crate::inputs::Input;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError;
use alloc::rc::Rc;
use core::cell::RefCell;
pub trait Mutator<C, I, R> pub trait Mutator<C, I, R>
where where

View File

@ -1,11 +1,11 @@
use alloc::vec::Vec;
use core::marker::PhantomData;
use crate::AflError;
use crate::inputs::{HasBytesVec, Input}; use crate::inputs::{HasBytesVec, Input};
use crate::mutators::Corpus; use crate::mutators::Corpus;
use crate::mutators::Mutator; use crate::mutators::Mutator;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError;
use alloc::vec::Vec;
use core::marker::PhantomData;
pub enum MutationResult { pub enum MutationResult {
Mutated, Mutated,

View File

@ -1,6 +1,10 @@
pub mod mutational; pub mod mutational;
pub use mutational::StdMutationalStage; pub use mutational::StdMutationalStage;
use alloc::rc::Rc;
use core::cell::RefCell;
use crate::AflError;
use crate::corpus::testcase::Testcase; use crate::corpus::testcase::Testcase;
use crate::corpus::Corpus; use crate::corpus::Corpus;
use crate::engines::State; use crate::engines::State;
@ -8,9 +12,6 @@ use crate::events::EventManager;
use crate::executors::Executor; use crate::executors::Executor;
use crate::inputs::Input; use crate::inputs::Input;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError;
use alloc::rc::Rc;
use core::cell::RefCell;
pub trait Stage<S, EM, E, C, I, R> pub trait Stage<S, EM, E, C, I, R>
where where

View File

@ -6,13 +6,12 @@ use crate::corpus::testcase::Testcase;
use crate::engines::State; use crate::engines::State;
use crate::events::{EventManager, NewTestcaseEvent}; use crate::events::{EventManager, NewTestcaseEvent};
use crate::executors::Executor; use crate::executors::Executor;
use crate::fire_event;
use crate::inputs::Input; use crate::inputs::Input;
use crate::mutators::Mutator; use crate::mutators::Mutator;
use crate::stages::Corpus; use crate::stages::Corpus;
use crate::stages::Stage; use crate::stages::Stage;
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError; use crate::{fire_event, AflError};
// TODO multi mutators stage // TODO multi mutators stage
@ -53,12 +52,13 @@ where
.mutate(rand, state.corpus_mut(), &mut input, i as i32)?; .mutate(rand, state.corpus_mut(), &mut input, i as i32)?;
let (interesting, new_testcase) = state.evaluate_input(input)?; let (interesting, new_testcase) = state.evaluate_input(input)?;
self.mutator_mut()
.post_exec(interesting, new_testcase.clone(), i as i32)?;
if !new_testcase.is_none() { if !new_testcase.is_none() {
fire_event!(events, NewTestcaseEvent<I>, new_testcase.unwrap())?; fire_event!(events, NewTestcaseEvent<I>, new_testcase.unwrap())?;
} }
self.mutator_mut()
.post_exec(interesting, new_testcase, i as i32)?;
} }
Ok(()) Ok(())
} }

View File

@ -71,30 +71,6 @@ where
} }
} }
/// Has a Rand Rc RefCell field (internal mutability), that can be used to get random values
pub trait HasRandRR {
type R: Rand;
/// Get the hold Rand instance
fn rand(&self) -> &Rc<RefCell<Self::R>>;
// Gets the next 64 bit value
fn rand_next(&self) -> u64 {
self.rand().borrow_mut().next()
}
// Gets a value below the given 64 bit val (inclusive)
fn rand_below(&self, upper_bound_excl: u64) -> u64 {
self.rand().borrow_mut().below(upper_bound_excl)
}
// Gets a value between the given lower bound (inclusive) and upper bound (inclusive)
fn rand_between(&self, lower_bound_incl: u64, upper_bound_incl: u64) -> u64 {
self.rand()
.borrow_mut()
.between(lower_bound_incl, upper_bound_incl)
}
}
const HASH_CONST: u64 = 0xa5b35705; const HASH_CONST: u64 = 0xa5b35705;
/// XXH3 Based, hopefully speedy, rnd implementation /// XXH3 Based, hopefully speedy, rnd implementation