From 7e65193d53ac6ecc50ac2cf1c170ba57b36f3aa3 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 11 Dec 2020 00:58:15 +0100 Subject: [PATCH] started custom event --- afl/src/events/mod.rs | 50 +++++++++++++------------------------------ 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index c981a06b08..c289403503 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize}; #[cfg(feature = "std")] use std::io::Write; -use crate::corpus::Corpus; +use crate::{corpus::Corpus, serde_anymap::SerdeAny}; use crate::engines::State; use crate::executors::Executor; use crate::inputs::Input; @@ -32,47 +32,25 @@ pub enum BrokerEventResult { pub trait ShowStats {} -/* - -/// A custom event, in case a user wants to extend the features (at compile time) -pub trait CustomEvent +/// A custom event, for own messages, with own handler. +pub trait CustomEvent: SerdeAny + Serialize where - S: State, - C: Corpus, - E: Executor, I: Input, - R: Rand, { /// Returns the name of this event fn name(&self) -> &str; /// This method will be called in the broker - fn handle_in_broker(&self, broker: &dyn EventManager, state: &mut State, corpus: &mut C) -> Result; + fn handle_in_broker(&self) -> Result; /// This method will be called in the clients after handle_in_broker (unless BrokerEventResult::Handled) was returned in handle_in_broker - fn handle_in_client(&self, client: &dyn EventManager, state: &mut State, corpus: &mut C) -> Result<(), AflError>; + fn handle_in_client(&self) -> Result<(), AflError>; } -struct UnusedCustomEvent {} -impl CustomEvent for UnusedCustomEvent -where - S: State, - C: Corpus, - E: Executor, - I: Input, - R: Rand, -{ - fn name(&self) -> &str {"No custom events"} - fn handle_in_broker(&self, broker: &dyn EventManager, state: &mut State, corpus: &mut C) {Ok(BrokerEventResult::Handled)} - fn handle_in_client(&self, client: &dyn EventManager, state: &mut State, corpus: &mut C) {Ok(())} -} -*/ - /// Events sent around in the library #[derive(Serialize, Deserialize)] #[serde(bound = "I: serde::de::DeserializeOwned")] pub enum Event<'a, I> where I: Input, - // CE: CustomEvent, { LoadInitial { sender_id: u64, @@ -108,7 +86,11 @@ where None { phantom: PhantomData, }, - //Custom {sender_id: u64, custom_event: CE}, + Custom { + sender_id: u64, + // TODO: Allow custom events + // custom_event: Box>, + }, } impl<'a, I> Event<'a, I> @@ -150,7 +132,7 @@ where phantom: _, } => "Log", Event::None { phantom: _ } => "None", - //Event::Custom {sender_id, custom_event} => custom_event.name(), + Event::Custom {sender_id, /*custom_event} => custom_event.name()*/} => "todo", } } @@ -172,7 +154,6 @@ where } } - // TODO serialize and deserialize, defaults to serde } pub trait EventManager @@ -252,8 +233,8 @@ where }, Event::None { phantom: _, - } => Ok(BrokerEventResult::Handled) - //Event::Custom {sender_id, custom_event} => custom_event.handle_in_broker(state, corpus), + } => Ok(BrokerEventResult::Handled), + Event::Custom {sender_id, /*custom_event} => custom_event.handle_in_broker(state, corpus)*/} => Ok(BrokerEventResult::Forward), //_ => Ok(BrokerEventResult::Forward), } } @@ -397,7 +378,6 @@ where E: Executor, I: Input, R: Rand, - //CE: CustomEvent, { /// Fire an Event fn fire<'a>( @@ -498,8 +478,8 @@ where }, Event::None { phantom: _, - } => Ok(BrokerEventResult::Handled) - //Event::Custom {sender_id, custom_event} => custom_event.handle_in_broker(state, corpus), + } => Ok(BrokerEventResult::Handled), + Event::Custom {sender_id, /*custom_event} => custom_event.handle_in_broker(state, corpus)*/} => Ok(BrokerEventResult::Forward), //_ => Ok(BrokerEventResult::Forward), } }