From 7297c9421d85deab16b6999b26b5f5c22c211a4b Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Tue, 5 Jan 2021 16:44:24 +0100 Subject: [PATCH] remove generics from EventManager --- afl/src/engines/mod.rs | 14 ++--- afl/src/events/mod.rs | 79 ++++++++++------------------- afl/src/executors/inmemory.rs | 41 ++++++--------- afl/src/stages/mod.rs | 8 +-- afl/src/stages/mutational.rs | 10 ++-- fuzzers/libfuzzer_libpng/src/mod.rs | 8 +-- 6 files changed, 63 insertions(+), 97 deletions(-) diff --git a/afl/src/engines/mod.rs b/afl/src/engines/mod.rs index b8fac17e57..99dab97181 100644 --- a/afl/src/engines/mod.rs +++ b/afl/src/engines/mod.rs @@ -71,7 +71,7 @@ where E: Executor + HasObservers, OT: ObserversTuple, ET: ExecutorsTuple, - EM: EventManager, + EM: EventManager, { for entry in fs::read_dir(in_dir)? { let entry = entry?; @@ -114,7 +114,7 @@ where E: Executor + HasObservers, OT: ObserversTuple, ET: ExecutorsTuple, - EM: EventManager, + EM: EventManager, { for in_dir in in_dirs { self.load_from_directory(corpus, generator, engine, manager, in_dir)?; @@ -294,7 +294,7 @@ where E: Executor + HasObservers, OT: ObserversTuple, ET: ExecutorsTuple, - EM: EventManager, + EM: EventManager, { let mut added = 0; for _ in 0..num { @@ -384,7 +384,7 @@ where pub trait Fuzzer where ST: StagesTuple, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -438,7 +438,7 @@ where pub struct StdFuzzer where ST: StagesTuple, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -455,7 +455,7 @@ impl Fuzzer for StdFuzzer where ST: StagesTuple, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -476,7 +476,7 @@ where impl StdFuzzer where ST: StagesTuple, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index b202d44c92..239654fe43 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -222,24 +222,23 @@ where R: Rand; } -pub trait EventManager +pub trait EventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, { /// Fire an Event //fn fire<'a>(&mut self, event: Event) -> Result<(), AflError>; /// Lookup for incoming events and process them. /// Return the number of processes events or an error - fn process( + fn process( &mut self, state: &mut State, corpus: &mut C, - ) -> Result; + ) -> Result where + C: Corpus, + FT: FeedbacksTuple, + R: Rand; fn serialize_observers(&mut self, observers: &OT) -> Result, AflError> where OT: ObserversTuple { Ok(postcard::to_allocvec(observers)?) @@ -402,38 +401,30 @@ where } #[derive(Clone, Debug)] -pub struct LoggerEventManager +pub struct LoggerEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, - ST: Stats, //CE: CustomEvent, { stats: ST, events: Vec>, - // stats (maybe we need a separated struct?) - phantom: PhantomData<(C, E, I, R, FT)>, } -impl EventManager - for LoggerEventManager +impl EventManager + for LoggerEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, ST: Stats, //CE: CustomEvent, { - fn process( + fn process( &mut self, state: &mut State, corpus: &mut C, - ) -> Result { + ) -> Result where + C: Corpus, + FT: FeedbacksTuple, + R: Rand { let count = self.events.len(); self.events .drain(..) @@ -508,20 +499,15 @@ where } } -impl LoggerEventManager +impl LoggerEventManager where - C: Corpus, I: Input, - E: Executor, - FT: FeedbacksTuple, - R: Rand, ST: Stats, //TODO CE: CustomEvent, { pub fn new(stats: ST) -> Self { Self { stats: stats, - phantom: PhantomData, events: vec![], } } @@ -699,30 +685,22 @@ const _LLMP_TAG_EVENT_TO_BROKER: llmp::Tag = 0x2B80438; const LLMP_TAG_EVENT_TO_BOTH: llmp::Tag = 0x2B0741; #[derive(Clone, Debug)] -pub struct LlmpEventManager +pub struct LlmpEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, SH: ShMem, ST: Stats, //CE: CustomEvent, { llmp: llmp::LlmpConnection, stats: ST, - phantom: PhantomData<(C, E, FT, I, R)>, + phantom: PhantomData, } #[cfg(feature = "std")] -impl LlmpEventManager +impl LlmpEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, ST: Stats, { /// Create llmp on a port @@ -745,13 +723,9 @@ where } } -impl LlmpEventManager +impl LlmpEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, SH: ShMem, ST: Stats, { @@ -849,23 +823,22 @@ where } } -impl EventManager - for LlmpEventManager +impl EventManager + for LlmpEventManager where - C: Corpus, - E: Executor, - FT: FeedbacksTuple, I: Input, - R: Rand, SH: ShMem, ST: Stats, //CE: CustomEvent, { - fn process( + fn process( &mut self, state: &mut State, corpus: &mut C, - ) -> Result { + ) -> Result where + C: Corpus, + FT: FeedbacksTuple, + R: Rand { // TODO: Get around local event copy by moving handle_in_client Ok(match &mut self.llmp { llmp::LlmpConnection::IsClient { client } => { diff --git a/afl/src/executors/inmemory.rs b/afl/src/executors/inmemory.rs index 62cd3ad154..3ccfaa6a3d 100644 --- a/afl/src/executors/inmemory.rs +++ b/afl/src/executors/inmemory.rs @@ -1,5 +1,5 @@ use alloc::{boxed::Box, string::ToString, vec::Vec}; -use core::{marker::PhantomData, ffi::c_void, ptr}; +use core::{ffi::c_void, ptr}; use crate::{ corpus::Corpus, @@ -31,13 +31,12 @@ type HarnessFunction = fn(&dyn Executor, &[u8]) -> ExitKind; type OnCrashFunction = dyn FnMut(ExitKind, &I, &State, &C, &mut EM); /// The inmem executor simply calls a target function, then returns afterwards. -pub struct InMemoryExecutor +pub struct InMemoryExecutor where I: Input + HasTargetBytes, OT: ObserversTuple, C: Corpus, - E: Executor, - EM: EventManager, + EM: EventManager, FT: FeedbacksTuple, R: Rand, { @@ -49,17 +48,14 @@ where observers: OT, /// A special function being called right before the process crashes. It may save state to restore fuzzing after respawn. on_crash_fn: Box>, - - phantom: PhantomData } -impl Executor for InMemoryExecutor +impl Executor for InMemoryExecutor where I: Input + HasTargetBytes, OT: ObserversTuple, C: Corpus, - E: Executor, - EM: EventManager, + EM: EventManager, FT: FeedbacksTuple, R: Rand, { @@ -79,13 +75,12 @@ where } } -impl Named for InMemoryExecutor +impl Named for InMemoryExecutor where I: Input + HasTargetBytes, OT: ObserversTuple, C: Corpus, - E: Executor, - EM: EventManager, + EM: EventManager, FT: FeedbacksTuple, R: Rand, { @@ -94,13 +89,12 @@ where } } -impl HasObservers for InMemoryExecutor +impl HasObservers for InMemoryExecutor where I: Input + HasTargetBytes, OT: ObserversTuple, C: Corpus, - E: Executor, - EM: EventManager, + EM: EventManager, FT: FeedbacksTuple, R: Rand, { @@ -115,13 +109,12 @@ where } } -impl InMemoryExecutor +impl InMemoryExecutor where I: Input + HasTargetBytes, OT: ObserversTuple, C: Corpus, - E: Executor, - EM: EventManager, + EM: EventManager, FT: FeedbacksTuple, R: Rand, { @@ -138,6 +131,8 @@ where harness_fn: HarnessFunction, observers: OT, on_crash_fn: Box>, + _state: &State, + _corpus: &C, _event_mgr: &EM, ) -> Self { @@ -154,7 +149,6 @@ where on_crash_fn, observers, name, - phantom: PhantomData } } } @@ -228,9 +222,8 @@ pub mod unix_signals { info: siginfo_t, _void: c_void, ) where - EM: EventManager, + EM: EventManager, C: Corpus, - E: Executor, OT: ObserversTuple, FT: FeedbacksTuple, I: Input, @@ -271,9 +264,8 @@ pub mod unix_signals { _info: siginfo_t, _void: c_void, ) where - EM: EventManager, + EM: EventManager, C: Corpus, - E: Executor, OT: ObserversTuple, FT: FeedbacksTuple, I: Input, @@ -309,9 +301,8 @@ pub mod unix_signals { // TODO clearly state that manager should be static (maybe put the 'static lifetime?) pub unsafe fn setup_crash_handlers(state: &State, corpus: &C, manager: &mut EM) where - EM: EventManager, + EM: EventManager, C: Corpus, - E: Executor, OT: ObserversTuple, FT: FeedbacksTuple, I: Input, diff --git a/afl/src/stages/mod.rs b/afl/src/stages/mod.rs index 2d25b8d3dc..dd2c3260f3 100644 --- a/afl/src/stages/mod.rs +++ b/afl/src/stages/mod.rs @@ -16,7 +16,7 @@ use crate::AflError; /// Multiple stages will be scheduled one by one for each input. pub trait Stage where - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -39,7 +39,7 @@ where pub trait StagesTuple where - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -63,7 +63,7 @@ where impl StagesTuple for () where - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -92,7 +92,7 @@ impl StagesTuple, Tail: StagesTuple + TupleList, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, diff --git a/afl/src/stages/mutational.rs b/afl/src/stages/mutational.rs index 946341c5f4..8d04ef5e27 100644 --- a/afl/src/stages/mutational.rs +++ b/afl/src/stages/mutational.rs @@ -21,7 +21,7 @@ pub trait MutationalStage: Stage where M: Mutator, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -89,7 +89,7 @@ where pub struct StdMutationalStage where M: Mutator, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -106,7 +106,7 @@ impl MutationalStage where M: Mutator, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -132,7 +132,7 @@ impl Stage for StdMutationalStage where M: Mutator, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, @@ -158,7 +158,7 @@ where impl StdMutationalStage where M: Mutator, - EM: EventManager, + EM: EventManager, E: Executor + HasObservers, OT: ObserversTuple, FT: FeedbacksTuple, diff --git a/fuzzers/libfuzzer_libpng/src/mod.rs b/fuzzers/libfuzzer_libpng/src/mod.rs index f3fddbe624..9cda0d0d1f 100644 --- a/fuzzers/libfuzzer_libpng/src/mod.rs +++ b/fuzzers/libfuzzer_libpng/src/mod.rs @@ -12,7 +12,7 @@ use afl::{ llmp::LlmpReceiver, llmp::LlmpSender, shmem::{AflShmem, ShMem}, - LlmpEventManager, SimpleStats, + EventManager, LlmpEventManager, SimpleStats, }, executors::{ inmemory::{serialize_state_corpus, deserialize_state_corpus, InMemoryExecutor}, @@ -142,12 +142,14 @@ fn fuzz(input: Option>, broker_port: u16) -> Result<(), AflError> { tuple_list!(edges_observer), Box::new(move |exit_kind, input, state, corpus, mgr| { match exit_kind { - ExitKind::Timeout => mgr.timeout(input).expect("Error sending Timeout event for input {:?}", input), - ExitKind::Crash => mgr.crash(input).expect("Error sending crash event for input {:?}", input), + ExitKind::Timeout => mgr.timeout(input).expect(&format!("Error sending Timeout event for input {:?}", input)), + ExitKind::Crash => mgr.crash(input).expect(&format!("Error sending crash event for input {:?}", input)), } let state_corpus_serialized = serialize_state_corpus(state, corpus).unwrap(); sender.send_buf(0x1, &state_corpus_serialized).unwrap(); }), + &state, + &corpus, &mgr, );