From 19c7ae43e58ad6dc8861c41598f71e8632fa830f Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Tue, 24 Nov 2020 12:53:01 +0100 Subject: [PATCH] event methods --- afl/src/events/mod.rs | 85 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index 0faec55cf0..47bdb9cea3 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -10,7 +10,7 @@ pub use crate::events::llmp::LLMP; use alloc::rc::Rc; use core::cell::RefCell; -//use core::any::TypeId; +use core::fmt::Formatter; #[cfg(feature = "std")] use std::io::Write; @@ -21,8 +21,43 @@ use crate::inputs::Input; use crate::utils::Rand; use crate::AflError; +pub enum EventDestination { + Main, + Broker, + Clients, +} + pub trait Event { - fn name(&self) -> &'static str; + fn name() -> &'static str; + + fn destination() -> EventDestination; + + fn log(&self, formatter: &mut Formatter, _state: &S) -> Result<(), AflError> + where + S: State, + C: Corpus, + E: Executor, + I: Input, + R: Rand, + { + match write!(formatter, "[{}]", Self::name()) { + Ok(_) => Ok(()), + Err(_) => Err(AflError::Unknown("write error".to_string())), + } + } + + fn on_recv(&self, _state: &mut S) -> Result<(), AflError> + where + S: State, + C: Corpus, + E: Executor, + I: Input, + R: Rand, + { + Ok(()) + } + + // TODO serialize and deserialize, defaults to serde } pub trait EventManager @@ -34,6 +69,7 @@ where R: Rand, { /// Check if this EventaManager support a given Event type + /// To compare events, use Event::name().as_ptr() fn enabled(&self) -> bool where T: Event; @@ -73,9 +109,13 @@ macro_rules! fire_event { pub struct LoadInitialEvent {} impl Event for LoadInitialEvent { - fn name(&self) -> &'static str { + fn name() -> &'static str { "LOAD" } + + fn destination() -> EventDestination { + EventDestination::Broker + } } impl LoadInitialEvent { pub fn new() -> Self { @@ -94,9 +134,13 @@ impl Event for NewTestcaseEvent where I: Input, { - fn name(&self) -> &'static str { + fn name() -> &'static str { "NEW" } + + fn destination() -> EventDestination { + EventDestination::Clients + } } impl NewTestcaseEvent @@ -114,9 +158,13 @@ where pub struct UpdateStatsEvent {} impl Event for UpdateStatsEvent { - fn name(&self) -> &'static str { + fn name() -> &'static str { "STATS" } + + fn destination() -> EventDestination { + EventDestination::Broker + } } impl UpdateStatsEvent { pub fn new() -> Self { @@ -124,6 +172,22 @@ impl UpdateStatsEvent { } } +pub struct CrashEvent {} +impl Event for CrashEvent { + fn name() -> &'static str { + "CRASH" + } + + fn destination() -> EventDestination { + EventDestination::Broker + } +} +impl CrashEvent { + pub fn new() -> Self { + CrashEvent {} + } +} + #[cfg(feature = "std")] pub struct LoggerEventManager where @@ -148,20 +212,13 @@ where T: Event, { true - /*let _load = TypeId::of::(); - let _new = TypeId::of::(); - match TypeId::of::() { - _load => true, - _new => true, - _ => false, - }*/ } - fn fire(&mut self, event: T) -> Result<(), AflError> + fn fire(&mut self, _event: T) -> Result<(), AflError> where T: Event, { - self.events.push(event.name().to_string()); + self.events.push(T::name().to_string()); Ok(()) }