From 745caf5b88d3b7f26ff014ca2c13d6dfb5b47730 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Wed, 9 Dec 2020 16:57:34 +0100 Subject: [PATCH] move some stuffs into events --- afl/src/engines/mod.rs | 7 +-- afl/src/events/mod.rs | 110 ++++++++++++++++++++++------------- afl/src/stages/mutational.rs | 2 +- 3 files changed, 71 insertions(+), 48 deletions(-) diff --git a/afl/src/engines/mod.rs b/afl/src/engines/mod.rs index 387f7456d5..6b265cfc5e 100644 --- a/afl/src/engines/mod.rs +++ b/afl/src/engines/mod.rs @@ -297,12 +297,7 @@ where let cur = current_milliseconds(); if cur - last > 60 * 100 { last = cur; - manager.fire( - Event::UpdateStats { - sender_id: 0, - new_execs: 1, - phantom: PhantomData, - }, + manager.fire(Event::update_stats(state.executions(), state.executions_over_seconds()), state, corpus, )?; // TODO self.new_execs}); diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index d810aa19e9..c24bdbebc2 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -24,14 +24,19 @@ use crate::inputs::Input; use crate::serde_anymap::{Ptr, PtrMut}; use crate::utils::Rand; use crate::AflError; + /// Indicate if an event worked or not -enum BrokerEventResult { +pub enum BrokerEventResult { /// The broker haneled this. No need to pass it on. Handled, /// Pass this message along to the clients. Forward, } +pub trait ShowStats { + +} + /* /// A custom event, in case a user wants to extend the features (at compile time) @@ -87,7 +92,8 @@ where }, UpdateStats { sender_id: u64, - new_execs: usize, + executions: usize, + execs_over_sec: u64, phantom: PhantomData<(C, E, I, R)>, }, Crash { @@ -133,7 +139,8 @@ where } => "New Testcase", Event::UpdateStats { sender_id: _, - new_execs: _, + executions: _, + execs_over_sec: _, phantom: _, } => "Stats", Event::Crash { @@ -157,13 +164,64 @@ where } } + pub fn log(severity_level: u8, message: String) -> Self { + Event::Log { + sender_id: 0, + severity_level: severity_level, + message: message, + phantom: PhantomData, + } + } + + pub fn update_stats(executions: usize, execs_over_sec: u64) -> Self { + Event::UpdateStats { + sender_id: 0, + executions: executions, + execs_over_sec: execs_over_sec, + phantom: PhantomData, + } + } + + // TODO serialize and deserialize, defaults to serde +} + +pub trait EventManager +where + C: Corpus, + E: Executor, + I: Input, + R: Rand, +{ + /// Check if this EventaManager support a given Event type + /// To compare events, use Event::name().as_ptr() + fn enabled(&self) -> bool; + + /// Fire an Event + fn fire<'a>( + &mut self, + event: Event<'a, C, E, I, R>, + state: &mut State, + corpus: &mut C, + ) -> Result<(), AflError>; + + /// Lookup for incoming events and process them. + /// Return the number of processes events or an error + fn process(&mut self, state: &mut State, corpus: &mut C) -> Result; + + fn on_recv(&self, _state: &mut State, _corpus: &mut C) -> Result<(), AflError> { + // TODO: Better way to move out of testcase, or get ref + //Ok(corpus.add(self.testcase.take().unwrap())) + Ok(()) + } + // TODO the broker has a state? do we need to pass state and corpus? fn handle_in_broker( &self, + event: &Event, /*broker: &dyn EventManager,*/ _state: &mut State, _corpus: &mut C, ) -> Result { - match self { + match event { Event::LoadInitial { sender_id: _, phantom: _, @@ -175,7 +233,8 @@ where } => Ok(BrokerEventResult::Forward), Event::UpdateStats { sender_id: _, - new_execs: _, + executions: _, + execs_over_sec: _, phantom: _, } => { // TODO @@ -214,11 +273,12 @@ where } fn handle_in_client( - self, + &self, + event: Event, /*client: &dyn EventManager,*/ _state: &mut State, corpus: &mut C, ) -> Result<(), AflError> { - match self { + match event { Event::NewTestcase { sender_id: _, input: _, @@ -234,38 +294,6 @@ where )), } } - - // TODO serialize and deserialize, defaults to serde -} - -pub trait EventManager -where - C: Corpus, - E: Executor, - I: Input, - R: Rand, -{ - /// Check if this EventaManager support a given Event type - /// To compare events, use Event::name().as_ptr() - fn enabled(&self) -> bool; - - /// Fire an Event - fn fire<'a>( - &mut self, - event: Event<'a, C, E, I, R>, - state: &mut State, - corpus: &mut C, - ) -> Result<(), AflError>; - - /// Lookup for incoming events and process them. - /// Return the number of processes events or an error - fn process(&mut self, state: &mut State, corpus: &mut C) -> Result; - - fn on_recv(&self, _state: &mut State, _corpus: &mut C) -> Result<(), AflError> { - // TODO: Better way to move out of testcase, or get ref - //Ok(corpus.add(self.testcase.take().unwrap())) - Ok(()) - } } /*TODO @@ -311,8 +339,8 @@ where state: &mut State, corpus: &mut C, ) -> Result<(), AflError> { - match event.handle_in_broker(state, corpus)? { - BrokerEventResult::Forward => (), //event.handle_in_client(state, corpus)?, + match self.handle_in_broker(&event, state, corpus)? { + BrokerEventResult::Forward => (), //self.handle_in_client(event, state, corpus)?, // Ignore broker-only events BrokerEventResult::Handled => (), } diff --git a/afl/src/stages/mutational.rs b/afl/src/stages/mutational.rs index 44922f8e04..54c9ab83b8 100644 --- a/afl/src/stages/mutational.rs +++ b/afl/src/stages/mutational.rs @@ -53,6 +53,7 @@ where let fitness = state.evaluate_input(&input_mut, engine.executor_mut())?; + // TODO post exec on the testcase, like post_exec(testcase_maybe, i as i32) self.mutator_mut() .post_exec(fitness, &input_mut, i as i32)?; @@ -63,7 +64,6 @@ where // if needed by particular cases let testcase_maybe = state.testcase_if_interesting(input_mut, fitness)?; if let Some(mut testcase) = testcase_maybe { - // TODO decouple events manager and engine manager.fire( Event::NewTestcase { sender_id: 0,