Skip to content

Commit

Permalink
Remove CustomBufHandlers (#2829)
Browse files Browse the repository at this point in the history
* rem

* fix

* fixer
  • Loading branch information
tokatoka authored Jan 13, 2025
1 parent aa0391e commit fd06e5c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 219 deletions.
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- `Qemu` cannot be used to initialize `Emulator` directly anymore. Instead, `Qemu` should be initialized through `Emulator` systematically if `Emulator` should be used.
- Related: `EmulatorBuilder` uses a single function to provide a `Qemu` initializer: `EmulatorBuilder::qemu_parameters`. For now, it can be either a `Vec<String>` or a `QemuConfig` instance.
- Related: Qemu's `AsanModule` does not need any special call to `Qemu` init methods anymore. It is now possible to simply initialize `AsanModule` (or `AsanGuestModule`) with a reference to the environment as parameter.

- `CustomBufHandlers` has been deleted. Please use `EventManagerHooksTuple` from now on.
# 0.14.0 -> 0.14.1
- Removed `with_observers` from `Executor` trait.
- `MmapShMemProvider::new_shmem_persistent` has been removed in favour of `MmapShMem::persist`. You probably want to do something like this: `let shmem = MmapShMemProvider::new()?.new_shmem(size)?.persist()?;`
Expand Down
1 change: 0 additions & 1 deletion libafl/src/events/broker_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ where
log::log!((*severity_level).into(), "{message}");
Ok(BrokerEventResult::Handled)
}
Event::CustomBuf { .. } => Ok(BrokerEventResult::Forward),
Event::Stop => Ok(BrokerEventResult::Forward),
//_ => Ok(BrokerEventResult::Forward),
}
Expand Down
26 changes: 4 additions & 22 deletions libafl/src/events/centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// 3. The "main evaluator", the evaluator node that will evaluate all the testcases pass by the centralized event manager to see if the testcases are worth propagating
// 4. The "main broker", the gathers the stats from the fuzzer clients and broadcast the newly found testcases from the main evaluator.

use alloc::{boxed::Box, string::String, vec::Vec};
use alloc::{string::String, vec::Vec};
use core::{fmt::Debug, time::Duration};
use std::{marker::PhantomData, process};

Expand All @@ -30,9 +30,9 @@ use crate::events::llmp::COMPRESS_THRESHOLD;
use crate::{
corpus::Corpus,
events::{
AdaptiveSerializer, CustomBufEventResult, Event, EventConfig, EventFirer, EventManager,
EventManagerHooksTuple, EventManagerId, EventProcessor, EventRestarter,
HasCustomBufHandlers, HasEventManagerId, LogSeverity, ProgressReporter,
AdaptiveSerializer, Event, EventConfig, EventFirer, EventManager, EventManagerHooksTuple,
EventManagerId, EventProcessor, EventRestarter, HasEventManagerId, LogSeverity,
ProgressReporter,
},
executors::{Executor, HasObservers},
fuzzer::{EvaluatorObservers, ExecutionProcessor},
Expand Down Expand Up @@ -417,24 +417,6 @@ where
{
}

impl<EM, EMH, S, SP> HasCustomBufHandlers for CentralizedEventManager<EM, EMH, S, SP>
where
EM: HasCustomBufHandlers<State = S>,
EMH: EventManagerHooksTuple<S>,
S: State,
SP: ShMemProvider,
{
/// Adds a custom buffer handler that will run for each incoming `CustomBuf` event.
fn add_custom_buf_handler(
&mut self,
handler: Box<
dyn FnMut(&mut Self::State, &str, &[u8]) -> Result<CustomBufEventResult, Error>,
>,
) {
self.inner.add_custom_buf_handler(handler);
}
}

impl<EM, EMH, S, SP> ProgressReporter for CentralizedEventManager<EM, EMH, S, SP>
where
EM: AdaptiveSerializer + ProgressReporter<State = S> + HasEventManagerId,
Expand Down
33 changes: 3 additions & 30 deletions libafl/src/events/llmp/mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(feature = "std")]
use alloc::string::ToString;
use alloc::{boxed::Box, vec::Vec};
use alloc::vec::Vec;
use core::{marker::PhantomData, time::Duration};
#[cfg(feature = "std")]
use std::net::TcpStream;
Expand Down Expand Up @@ -33,9 +33,8 @@ use crate::{
corpus::Corpus,
events::{
llmp::{LLMP_TAG_EVENT_TO_BOTH, _LLMP_TAG_EVENT_TO_BROKER},
AdaptiveSerializer, CustomBufEventResult, CustomBufHandlerFn, Event, EventConfig,
EventFirer, EventManager, EventManagerHooksTuple, EventManagerId, EventProcessor,
EventRestarter, HasCustomBufHandlers, HasEventManagerId, ProgressReporter,
AdaptiveSerializer, Event, EventConfig, EventFirer, EventManager, EventManagerHooksTuple,
EventManagerId, EventProcessor, EventRestarter, HasEventManagerId, ProgressReporter,
},
executors::{Executor, HasObservers},
fuzzer::{Evaluator, EvaluatorObservers, ExecutionProcessor},
Expand Down Expand Up @@ -64,8 +63,6 @@ where
hooks: EMH,
/// The LLMP client for inter process communication
llmp: LlmpClient<SP>,
/// The custom buf handler
custom_buf_handlers: Vec<Box<CustomBufHandlerFn<S>>>,
#[cfg(feature = "llmp_compression")]
compressor: GzipCompressor,
/// The configuration defines this specific fuzzer.
Expand Down Expand Up @@ -168,7 +165,6 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
should_serialize_cnt: 0,
time_ref,
phantom: PhantomData,
custom_buf_handlers: vec![],
event_buffer: Vec::with_capacity(INITIAL_EVENT_BUFFER_SIZE),
})
}
Expand Down Expand Up @@ -203,7 +199,6 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
should_serialize_cnt: 0,
time_ref,
phantom: PhantomData,
custom_buf_handlers: vec![],
event_buffer: Vec::with_capacity(INITIAL_EVENT_BUFFER_SIZE),
})
}
Expand Down Expand Up @@ -238,7 +233,6 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
should_serialize_cnt: 0,
time_ref,
phantom: PhantomData,
custom_buf_handlers: vec![],
event_buffer: Vec::with_capacity(INITIAL_EVENT_BUFFER_SIZE),
})
}
Expand Down Expand Up @@ -271,7 +265,6 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
should_serialize_cnt: 0,
time_ref,
phantom: PhantomData,
custom_buf_handlers: vec![],
event_buffer: Vec::with_capacity(INITIAL_EVENT_BUFFER_SIZE),
})
}
Expand Down Expand Up @@ -469,13 +462,6 @@ where
}
}
}
Event::CustomBuf { tag, buf } => {
for handler in &mut self.custom_buf_handlers {
if handler(state, &tag, &buf)? == CustomBufEventResult::Handled {
break;
}
}
}
Event::Stop => {
state.request_stop();
}
Expand Down Expand Up @@ -679,19 +665,6 @@ where
{
}

impl<EMH, S, SP> HasCustomBufHandlers for LlmpEventManager<EMH, S, SP>
where
S: State,
SP: ShMemProvider,
{
fn add_custom_buf_handler(
&mut self,
handler: Box<dyn FnMut(&mut S, &str, &[u8]) -> Result<CustomBufEventResult, Error>>,
) {
self.custom_buf_handlers.push(handler);
}
}

impl<EMH, S, SP> ProgressReporter for LlmpEventManager<EMH, S, SP>
where
S: State + HasExecutions + HasMetadata + HasLastReportTime,
Expand Down
18 changes: 1 addition & 17 deletions libafl/src/events/llmp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! LLMP-backed event manager for scalable multi-processed fuzzing
use alloc::{boxed::Box, vec::Vec};
use core::{marker::PhantomData, time::Duration};

#[cfg(feature = "llmp_compression")]
Expand All @@ -17,7 +16,7 @@ use serde::Deserialize;

use crate::{
corpus::Corpus,
events::{CustomBufEventResult, CustomBufHandlerFn, Event, EventFirer},
events::{Event, EventFirer},
executors::{Executor, HasObservers},
fuzzer::{EvaluatorObservers, ExecutionProcessor},
inputs::{Input, InputConverter, NopInput, NopInputConverter, UsesInput},
Expand Down Expand Up @@ -96,8 +95,6 @@ where
throttle: Option<Duration>,
llmp: LlmpClient<SP>,
last_sent: Duration,
/// The custom buf handler
custom_buf_handlers: Vec<Box<CustomBufHandlerFn<S>>>,
#[cfg(feature = "llmp_compression")]
compressor: GzipCompressor,
converter: Option<IC>,
Expand Down Expand Up @@ -165,7 +162,6 @@ impl LlmpEventConverterBuilder {
converter,
converter_back,
phantom: PhantomData,
custom_buf_handlers: vec![],
})
}

Expand Down Expand Up @@ -195,7 +191,6 @@ impl LlmpEventConverterBuilder {
converter,
converter_back,
phantom: PhantomData,
custom_buf_handlers: vec![],
})
}

Expand Down Expand Up @@ -225,7 +220,6 @@ impl LlmpEventConverterBuilder {
converter,
converter_back,
phantom: PhantomData,
custom_buf_handlers: vec![],
})
}
}
Expand Down Expand Up @@ -324,14 +318,6 @@ where
}
Ok(())
}
Event::CustomBuf { tag, buf } => {
for handler in &mut self.custom_buf_handlers {
if handler(state, &tag, &buf)? == CustomBufEventResult::Handled {
break;
}
}
Ok(())
}
Event::Stop => Ok(()),
_ => Err(Error::unknown(format!(
"Received illegal message that message should not have arrived: {:?}.",
Expand Down Expand Up @@ -449,7 +435,6 @@ where
#[cfg(all(unix, feature = "std", feature = "multi_machine"))]
node_id,
},
Event::CustomBuf { buf, tag } => Event::CustomBuf { buf, tag },
_ => {
return Ok(());
}
Expand Down Expand Up @@ -506,7 +491,6 @@ where
#[cfg(all(unix, feature = "std", feature = "multi_machine"))]
node_id,
},
Event::CustomBuf { buf, tag } => Event::CustomBuf { buf, tag },
_ => {
return Ok(());
}
Expand Down
23 changes: 5 additions & 18 deletions libafl/src/events/llmp/restarting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! When the target crashes, a watch process (the parent) will
//! restart/refork it.
use alloc::{boxed::Box, vec::Vec};
use alloc::vec::Vec;
use core::{
marker::PhantomData,
num::NonZeroUsize,
Expand Down Expand Up @@ -34,10 +34,10 @@ use crate::events::EVENTMGR_SIGHANDLER_STATE;
use crate::{
corpus::Corpus,
events::{
launcher::ClientDescription, AdaptiveSerializer, CustomBufEventResult, Event, EventConfig,
EventFirer, EventManager, EventManagerHooksTuple, EventManagerId, EventProcessor,
EventRestarter, HasCustomBufHandlers, HasEventManagerId, LlmpEventManager,
LlmpShouldSaveState, ProgressReporter, StdLlmpEventHook,
launcher::ClientDescription, AdaptiveSerializer, Event, EventConfig, EventFirer,
EventManager, EventManagerHooksTuple, EventManagerId, EventProcessor, EventRestarter,
HasEventManagerId, LlmpEventManager, LlmpShouldSaveState, ProgressReporter,
StdLlmpEventHook,
},
executors::{Executor, HasObservers},
fuzzer::{Evaluator, EvaluatorObservers, ExecutionProcessor},
Expand Down Expand Up @@ -246,19 +246,6 @@ where
}
}

impl<EMH, S, SP> HasCustomBufHandlers for LlmpRestartingEventManager<EMH, S, SP>
where
S: State,
SP: ShMemProvider,
{
fn add_custom_buf_handler(
&mut self,
handler: Box<dyn FnMut(&mut S, &str, &[u8]) -> Result<CustomBufEventResult, Error>>,
) {
self.llmp_mgr.add_custom_buf_handler(handler);
}
}

/// The llmp connection from the actual fuzzer to the process supervising it
const _ENV_FUZZER_SENDER: &str = "_AFL_ENV_FUZZER_SENDER";
const _ENV_FUZZER_RECEIVER: &str = "_AFL_ENV_FUZZER_RECEIVER";
Expand Down
60 changes: 3 additions & 57 deletions libafl/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub use llmp::*;
pub mod tcp;

pub mod broker_hooks;
use alloc::{borrow::Cow, boxed::Box, string::String, vec::Vec};
#[cfg(feature = "introspection")]
use alloc::boxed::Box;
use alloc::{borrow::Cow, string::String, vec::Vec};
use core::{
fmt,
hash::{BuildHasher, Hasher},
Expand Down Expand Up @@ -154,15 +156,6 @@ impl fmt::Display for LogSeverity {
}
}

/// The result of a custom buf handler added using [`HasCustomBufHandlers::add_custom_buf_handler`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CustomBufEventResult {
/// Exit early from event handling
Handled,
/// Call the next handler, if available
Next,
}

/// Indicate if an event worked or not
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum BrokerEventResult {
Expand Down Expand Up @@ -337,13 +330,6 @@ where
/// `PhantomData`
phantom: PhantomData<I>,
},
/// Sends a custom buffer to other clients
CustomBuf {
/// The buffer
buf: Vec<u8>,
/// Tag of this buffer
tag: String,
},
/// Exit gracefully
Stop,
/*/// A custom type
Expand All @@ -367,7 +353,6 @@ where
Event::UpdatePerfMonitor { .. } => "PerfMonitor",
Event::Objective { .. } => "Objective",
Event::Log { .. } => "Log",
Event::CustomBuf { .. } => "CustomBuf",
/*Event::Custom {
sender_id: _, /*custom_event} => custom_event.name()*/
} => "todo",*/
Expand All @@ -387,7 +372,6 @@ where
Event::UpdatePerfMonitor { .. } => Cow::Borrowed("PerfMonitor"),
Event::Objective { .. } => Cow::Borrowed("Objective"),
Event::Log { .. } => Cow::Borrowed("Log"),
Event::CustomBuf { .. } => Cow::Borrowed("CustomBuf"),
Event::Stop => Cow::Borrowed("Stop"),
/*Event::Custom {
sender_id: _, /*custom_event} => custom_event.name()*/
Expand Down Expand Up @@ -598,15 +582,6 @@ where
{
}

/// The handler function for custom buffers exchanged via [`EventManager`]
type CustomBufHandlerFn<S> = dyn FnMut(&mut S, &str, &[u8]) -> Result<CustomBufEventResult, Error>;

/// Supports custom buf handlers to handle `CustomBuf` events.
pub trait HasCustomBufHandlers: UsesState {
/// Adds a custom buffer handler that will run for each incoming `CustomBuf` event.
fn add_custom_buf_handler(&mut self, handler: Box<CustomBufHandlerFn<Self::State>>);
}

/// An eventmgr for tests, and as placeholder if you really don't need an event manager.
#[derive(Copy, Clone, Debug)]
pub struct NopEventManager<S> {
Expand Down Expand Up @@ -678,19 +653,6 @@ impl<E, S, Z> EventManager<E, Z> for NopEventManager<S> where
{
}

impl<S> HasCustomBufHandlers for NopEventManager<S>
where
S: State,
{
fn add_custom_buf_handler(
&mut self,
_handler: Box<
dyn FnMut(&mut Self::State, &str, &[u8]) -> Result<CustomBufEventResult, Error>,
>,
) {
}
}

impl<S> ProgressReporter for NopEventManager<S> where
S: State + HasExecutions + HasLastReportTime + HasMetadata
{
Expand Down Expand Up @@ -815,22 +777,6 @@ where
{
}

impl<EM, M> HasCustomBufHandlers for MonitorTypedEventManager<EM, M>
where
Self: UsesState,
EM: HasCustomBufHandlers<State = Self::State>,
{
#[inline]
fn add_custom_buf_handler(
&mut self,
handler: Box<
dyn FnMut(&mut Self::State, &str, &[u8]) -> Result<CustomBufEventResult, Error>,
>,
) {
self.inner.add_custom_buf_handler(handler);
}
}

impl<EM, M> ProgressReporter for MonitorTypedEventManager<EM, M>
where
Self: UsesState,
Expand Down
Loading

0 comments on commit fd06e5c

Please sign in to comment.