try to create better events

This commit is contained in:
Andrea Fioraldi 2020-11-24 10:19:52 +01:00
parent ab894b7daf
commit 6994a91255
3 changed files with 71 additions and 22 deletions

View File

@ -3,20 +3,23 @@ pub mod llmp_translated; // TODO: Abstract away.
pub mod shmem_translated; pub mod shmem_translated;
pub use crate::events::llmp::LLMP; pub use crate::events::llmp::LLMP;
use alloc::rc::Rc;
use core::any::Any; use core::any::Any;
use core::cell::RefCell;
//use core::any::TypeId; //use core::any::TypeId;
use core::fmt::Display;
// TODO use core version // TODO use core version
use std::io::Write; use std::io::Write;
use crate::corpus::Corpus; use crate::corpus::{Corpus, Testcase};
use crate::engines::State; use crate::engines::State;
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 crate::AflError;
pub trait Event: Display + Any {} pub trait Event: Any {
fn name(&self) -> &'static str;
}
pub trait EventManager<S, C, E, I, R> pub trait EventManager<S, C, E, I, R>
where where
@ -65,10 +68,9 @@ macro_rules! fire_event {
} }
pub struct LoadInitialEvent {} pub struct LoadInitialEvent {}
impl Event for LoadInitialEvent {} impl Event for LoadInitialEvent {
impl Display for LoadInitialEvent { fn name(&self) -> &'static str {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { "LOAD"
write!(f, "Load")
} }
} }
impl LoadInitialEvent { impl LoadInitialEvent {
@ -77,16 +79,40 @@ impl LoadInitialEvent {
} }
} }
pub struct NewTestcaseEvent {} pub struct NewTestcaseEvent<I>
impl Event for NewTestcaseEvent {} where
impl Display for NewTestcaseEvent { I: Input,
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { {
write!(f, "New") testcase: Rc<RefCell<Testcase<I>>>,
}
impl<I> Event<I> for NewTestcaseEvent<I>
where
I: Input,
{
fn name(&self) -> &'static str {
"NEW"
} }
} }
impl NewTestcaseEvent {
impl<I> NewTestcaseEvent<I>
where
I: Input,
{
pub fn new(testcase: Rc<RefCell<Testcase<I>>>) -> Self {
NewTestcaseEvent { testcase: testcase }
}
}
pub struct UpdateStatsEvent {}
impl Event for UpdateStatsEvent {
fn name(&self) -> &'static str {
"STATS"
}
}
impl UpdateStatsEvent {
pub fn new() -> Self { pub fn new() -> Self {
NewTestcaseEvent {} UpdateStatsEvent {}
} }
} }
@ -136,7 +162,7 @@ where
&mut self.writer, &mut self.writer,
"#{}\t[{}] corp: {} exec/s: {}", "#{}\t[{}] corp: {} exec/s: {}",
state.executions(), state.executions(),
event, event.name(),
state.corpus().entries().len(), state.corpus().entries().len(),
state.executions_over_seconds() state.executions_over_seconds()
)?; )?;

View File

@ -248,6 +248,30 @@ where
} }
} }
pub fn mutation_byteneg<M, C, I, R>(
_mutator: &mut M,
rand: &mut R,
_corpus: &mut C,
input: &mut I,
) -> Result<MutationResult, AflError>
where
M: Mutator<C, I, R>,
C: Corpus<I, R>,
I: Input + HasBytesVec,
R: Rand,
{
if input.bytes().len() == 0 {
Ok(MutationResult::Skipped)
} else {
let idx = rand.below(input.bytes().len() as u64) as usize;
unsafe {
// moar speed, no bound check
*input.bytes_mut().get_unchecked_mut(idx) = !(*input.bytes().get_unchecked(idx));
}
Ok(MutationResult::Mutated)
}
}
/// Returns the first and last diff position between the given vectors, stopping at the min len /// Returns the first and last diff position between the given vectors, stopping at the min len
fn locate_diffs(this: &[u8], other: &[u8]) -> (i64, i64) { fn locate_diffs(this: &[u8], other: &[u8]) -> (i64, i64) {
let mut first_diff: i64 = -1; let mut first_diff: i64 = -1;
@ -374,9 +398,11 @@ where
pub fn new_default() -> Self { pub fn new_default() -> Self {
let mut scheduled = StdScheduledMutator::<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_bitflip); scheduled.add_mutation(mutation_byteflip);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_byteinc);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bytedec);
scheduled.add_mutation(mutation_byteneg);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
@ -395,9 +421,6 @@ where
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_bitflip);
scheduled.add_mutation(mutation_byteflip);
scheduled.add_mutation(mutation_byteinc);
scheduled.add_mutation(mutation_bytedec);
scheduled.add_mutation(mutation_splice); scheduled.add_mutation(mutation_splice);
HavocBytesMutator { HavocBytesMutator {
scheduled: scheduled, scheduled: scheduled,

View File

@ -54,7 +54,7 @@ where
let (interesting, new_testcase) = state.evaluate_input(input)?; let (interesting, new_testcase) = state.evaluate_input(input)?;
if !new_testcase.is_none() { if !new_testcase.is_none() {
fire_event!(events, NewTestcaseEvent)?; fire_event!(events, NewTestcaseEvent<I>, new_testcase.unwrap())?;
} }
self.mutator_mut() self.mutator_mut()