use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
#[cfg(feature = "std")]
use core::sync::atomic::{compiler_fence, Ordering};
use core::{fmt::Debug, marker::PhantomData};
#[cfg(feature = "std")]
use serde::{de::DeserializeOwned, Serialize};
use super::{CustomBufEventResult, CustomBufHandlerFn, HasCustomBufHandlers, ProgressReporter};
#[cfg(all(feature = "std", any(windows, not(feature = "fork"))))]
use crate::bolts::os::startable_self;
#[cfg(all(feature = "std", feature = "fork", unix))]
use crate::bolts::os::{fork, ForkResult};
#[cfg(feature = "std")]
use crate::{
bolts::{shmem::ShMemProvider, staterestore::StateRestorer},
corpus::Corpus,
executors::Executor,
state::{HasCorpus, HasSolutions},
};
use crate::{
events::{
BrokerEventResult, Event, EventFirer, EventManager, EventManagerId, EventProcessor,
EventRestarter, HasEventManagerId,
},
inputs::Input,
monitors::Monitor,
Error,
};
const _ENV_FUZZER_SENDER: &str = "_AFL_ENV_FUZZER_SENDER";
const _ENV_FUZZER_RECEIVER: &str = "_AFL_ENV_FUZZER_RECEIVER";
const _ENV_FUZZER_BROKER_CLIENT_INITIAL: &str = "_AFL_ENV_FUZZER_BROKER_CLIENT";
pub struct SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
monitor: MT,
events: Vec<Event<I>>,
custom_buf_handlers: Vec<Box<CustomBufHandlerFn<S>>>,
phantom: PhantomData<S>,
}
impl<I, MT, S> Debug for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SimpleEventManager")
.field("monitor", &self.monitor)
.field("events", &self.events)
.finish_non_exhaustive()
}
}
impl<I, MT, S> EventFirer<I> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
fn fire<S2>(&mut self, _state: &mut S2, event: Event<I>) -> Result<(), Error> {
match Self::handle_in_broker(&mut self.monitor, &event)? {
BrokerEventResult::Forward => self.events.push(event),
BrokerEventResult::Handled => (),
};
Ok(())
}
}
impl<I, MT, S> EventRestarter<S> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
}
impl<E, I, MT, S, Z> EventProcessor<E, I, S, Z> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
fn process(
&mut self,
_fuzzer: &mut Z,
state: &mut S,
_executor: &mut E,
) -> Result<usize, Error> {
let count = self.events.len();
while !self.events.is_empty() {
let event = self.events.pop().unwrap();
self.handle_in_client(state, event)?;
}
Ok(count)
}
}
impl<E, I, MT, S, Z> EventManager<E, I, S, Z> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
}
impl<I, MT, S> HasCustomBufHandlers<S> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
fn add_custom_buf_handler(
&mut self,
handler: Box<dyn FnMut(&mut S, &String, &[u8]) -> Result<CustomBufEventResult, Error>>,
) {
self.custom_buf_handlers.push(handler);
}
}
impl<I, MT, S> ProgressReporter<I> for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
}
impl<I, MT, S> HasEventManagerId for SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug,
{
fn mgr_id(&self) -> EventManagerId {
EventManagerId { id: 0 }
}
}
impl<I, MT, S> SimpleEventManager<I, MT, S>
where
I: Input,
MT: Monitor + Debug, {
pub fn new(monitor: MT) -> Self {
Self {
monitor,
events: vec![],
custom_buf_handlers: vec![],
phantom: PhantomData,
}
}
#[allow(clippy::unnecessary_wraps)]
fn handle_in_broker(monitor: &mut MT, event: &Event<I>) -> Result<BrokerEventResult, Error> {
match event {
Event::NewTestcase {
input: _,
client_config: _,
exit_kind: _,
corpus_size,
observers_buf: _,
time,
executions,
} => {
monitor
.client_stats_mut_for(0)
.update_corpus_size(*corpus_size as u64);
monitor
.client_stats_mut_for(0)
.update_executions(*executions as u64, *time);
monitor.display(event.name().to_string(), 0);
Ok(BrokerEventResult::Handled)
}
Event::UpdateExecStats {
time,
executions,
phantom: _,
} => {
let client = monitor.client_stats_mut_for(0);
client.update_executions(*executions as u64, *time);
monitor.display(event.name().to_string(), 0);
Ok(BrokerEventResult::Handled)
}
Event::UpdateUserStats {
name,
value,
phantom: _,
} => {
monitor
.client_stats_mut_for(0)
.update_user_stats(name.clone(), value.clone());
monitor.display(event.name().to_string(), 0);
Ok(BrokerEventResult::Handled)
}
#[cfg(feature = "introspection")]
Event::UpdatePerfMonitor {
time,
executions,
introspection_monitor,
phantom: _,
} => {
let client = &mut monitor.client_stats_mut()[0];
client.update_executions(*executions as u64, *time);
client.update_introspection_monitor((**introspection_monitor).clone());
monitor.display(event.name().to_string(), 0);
Ok(BrokerEventResult::Handled)
}
Event::Objective { objective_size } => {
monitor
.client_stats_mut_for(0)
.update_objective_size(*objective_size as u64);
monitor.display(event.name().to_string(), 0);
Ok(BrokerEventResult::Handled)
}
Event::Log {
severity_level,
message,
phantom: _,
} => {
let (_, _) = (message, severity_level);
#[cfg(feature = "std")]
println!("[LOG {severity_level}]: {message}");
Ok(BrokerEventResult::Handled)
}
Event::CustomBuf { .. } => Ok(BrokerEventResult::Forward),
}
}
#[allow(clippy::needless_pass_by_value, clippy::unused_self)]
fn handle_in_client(&mut self, state: &mut S, event: Event<I>) -> Result<(), Error> {
if let Event::CustomBuf { tag, buf } = &event {
for handler in &mut self.custom_buf_handlers {
handler(state, tag, buf)?;
}
Ok(())
} else {
Err(Error::unknown(format!(
"Received illegal message that message should not have arrived: {:?}.",
event
)))
}
}
}
#[cfg(feature = "std")]
#[allow(clippy::default_trait_access)]
#[derive(Debug)]
pub struct SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug, {
simple_event_mgr: SimpleEventManager<I, MT, S>,
staterestorer: StateRestorer<SP>,
}
#[cfg(feature = "std")]
impl<I, MT, S, SP> EventFirer<I> for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug, {
fn fire<S2>(&mut self, _state: &mut S2, event: Event<I>) -> Result<(), Error> {
self.simple_event_mgr.fire(_state, event)
}
}
#[cfg(feature = "std")]
impl<I, MT, S, SP> EventRestarter<S> for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
S: Serialize,
SP: ShMemProvider,
MT: Monitor + Debug, {
fn on_restart(&mut self, state: &mut S) -> Result<(), Error> {
self.staterestorer.reset();
self.staterestorer.save(state)
}
}
#[cfg(feature = "std")]
impl<E, I, S, SP, MT, Z> EventProcessor<E, I, S, Z> for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
S: Serialize,
SP: ShMemProvider,
MT: Monitor + Debug, {
fn process(&mut self, fuzzer: &mut Z, state: &mut S, executor: &mut E) -> Result<usize, Error> {
self.simple_event_mgr.process(fuzzer, state, executor)
}
}
#[cfg(feature = "std")]
impl<E, I, S, SP, MT, Z> EventManager<E, I, S, Z> for SimpleRestartingEventManager<I, MT, S, SP>
where
E: Executor<Self, I, S, Z>,
I: Input,
S: Serialize,
SP: ShMemProvider,
MT: Monitor + Debug, {
}
#[cfg(feature = "std")]
impl<I, MT, S, SP> HasCustomBufHandlers<S> for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug, {
fn add_custom_buf_handler(
&mut self,
handler: Box<dyn FnMut(&mut S, &String, &[u8]) -> Result<CustomBufEventResult, Error>>,
) {
self.simple_event_mgr.add_custom_buf_handler(handler);
}
}
#[cfg(feature = "std")]
impl<I, MT, S, SP> ProgressReporter<I> for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug, {
}
#[cfg(feature = "std")]
impl<I, MT, S, SP> HasEventManagerId for SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug,
{
fn mgr_id(&self) -> EventManagerId {
self.simple_event_mgr.mgr_id()
}
}
#[cfg(feature = "std")]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
impl<I, MT, S, SP> SimpleRestartingEventManager<I, MT, S, SP>
where
I: Input,
SP: ShMemProvider,
MT: Monitor + Debug, {
fn new_launched(monitor: MT, staterestorer: StateRestorer<SP>) -> Self {
Self {
staterestorer,
simple_event_mgr: SimpleEventManager::new(monitor),
}
}
#[allow(clippy::similar_names)]
pub fn launch(mut monitor: MT, shmem_provider: &mut SP) -> Result<(Option<S>, Self), Error>
where
S: DeserializeOwned + Serialize + HasCorpus<I> + HasSolutions<I>,
MT: Debug,
{
let mut staterestorer = if std::env::var(_ENV_FUZZER_SENDER).is_err() {
let staterestorer: StateRestorer<SP> =
StateRestorer::new(shmem_provider.new_shmem(256 * 1024 * 1024)?);
staterestorer.write_to_env(_ENV_FUZZER_SENDER)?;
let mut ctr: u64 = 0;
loop {
println!("Spawning next client (id {ctr})");
#[cfg(all(unix, feature = "fork"))]
let child_status = {
shmem_provider.pre_fork()?;
match unsafe { fork() }? {
ForkResult::Parent(handle) => {
shmem_provider.post_fork(false)?;
handle.status()
}
ForkResult::Child => {
shmem_provider.post_fork(true)?;
break staterestorer;
}
}
};
#[cfg(any(windows, not(feature = "fork")))]
let child_status = startable_self()?.status()?;
#[cfg(all(unix, not(feature = "fork")))]
let child_status = child_status.code().unwrap_or_default();
compiler_fence(Ordering::SeqCst);
#[allow(clippy::manual_assert)]
if !staterestorer.has_content() {
#[cfg(unix)]
if child_status == 137 {
panic!("Fuzzer-respawner: The fuzzed target crashed with an out of memory error! Fix your harness, or switch to another executor (for example, a forkserver).");
}
panic!("Fuzzer-respawner: Storing state in crashed fuzzer instance did not work, no point to spawn the next client! This can happen if the child calls `exit()`, in that case make sure it uses `abort()`, if it got killed unrecoverable (OOM), or if there is a bug in the fuzzer itself. (Child exited with: {child_status})");
}
ctr = ctr.wrapping_add(1);
}
} else {
StateRestorer::from_env(shmem_provider, _ENV_FUZZER_SENDER)?
};
let (state, mgr) = match staterestorer.restore::<S>()? {
None => {
println!("First run. Let's set it all up");
(
None,
SimpleRestartingEventManager::new_launched(monitor, staterestorer),
)
}
Some(state) => {
println!("Subsequent run. Loaded previous state.");
staterestorer.reset();
let client_stats = monitor.client_stats_mut_for(0);
client_stats.update_corpus_size(state.corpus().count().try_into()?);
client_stats.update_objective_size(state.solutions().count().try_into()?);
(
Some(state),
SimpleRestartingEventManager::new_launched(monitor, staterestorer),
)
}
};
Ok((state, mgr))
}
}
#[cfg(feature = "python")]
#[allow(missing_docs)]
pub mod pybind {
use pyo3::prelude::*;
use crate::{
events::{pybind::PythonEventManager, SimpleEventManager},
inputs::BytesInput,
monitors::pybind::PythonMonitor,
state::pybind::PythonStdState,
};
#[pyclass(unsendable, name = "SimpleEventManager")]
#[derive(Debug)]
pub struct PythonSimpleEventManager {
pub inner: SimpleEventManager<BytesInput, PythonMonitor, PythonStdState>,
}
#[pymethods]
impl PythonSimpleEventManager {
#[new]
fn new(py_monitor: PythonMonitor) -> Self {
Self {
inner: SimpleEventManager::new(py_monitor),
}
}
fn as_manager(slf: Py<Self>) -> PythonEventManager {
PythonEventManager::new_simple(slf)
}
}
pub fn register(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PythonSimpleEventManager>()?;
Ok(())
}
}