pub mod simple;
pub use simple::*;
pub mod llmp;
pub use llmp::*;
use alloc::{string::String, vec::Vec};
use core::{fmt, marker::PhantomData, time::Duration};
use serde::{Deserialize, Serialize};
use crate::{
executors::ExitKind, inputs::Input, observers::ObserversTuple, stats::UserStats, Error,
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct EventManagerId {
pub id: usize,
}
impl Default for EventManagerId {
fn default() -> Self {
Self { id: 0 }
}
}
#[cfg(feature = "introspection")]
use crate::stats::ClientPerfStats;
#[cfg(feature = "introspection")]
use alloc::boxed::Box;
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum LogSeverity {
Debug,
Info,
Warn,
Error,
}
impl fmt::Display for LogSeverity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LogSeverity::Debug => write!(f, "Debug"),
LogSeverity::Info => write!(f, "Info"),
LogSeverity::Warn => write!(f, "Warn"),
LogSeverity::Error => write!(f, "Error"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum BrokerEventResult {
Handled,
Forward,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub enum Event<I>
where
I: Input,
{
NewTestcase {
input: I,
observers_buf: Vec<u8>,
exit_kind: ExitKind,
corpus_size: usize,
client_config: String,
time: Duration,
executions: usize,
},
UpdateStats {
time: Duration,
executions: usize,
phantom: PhantomData<I>,
},
UpdateUserStats {
name: String,
value: UserStats,
phantom: PhantomData<I>,
},
#[cfg(feature = "introspection")]
UpdatePerfStats {
time: Duration,
executions: usize,
introspection_stats: Box<ClientPerfStats>,
phantom: PhantomData<I>,
},
Objective {
objective_size: usize,
},
Log {
severity_level: LogSeverity,
message: String,
phantom: PhantomData<I>,
},
}
impl<I> Event<I>
where
I: Input,
{
fn name(&self) -> &str {
match self {
Event::NewTestcase {
input: _,
client_config: _,
corpus_size: _,
exit_kind: _,
observers_buf: _,
time: _,
executions: _,
} => "Testcase",
Event::UpdateStats {
time: _,
executions: _,
phantom: _,
}
| Event::UpdateUserStats {
name: _,
value: _,
phantom: _,
} => "Stats",
#[cfg(feature = "introspection")]
Event::UpdatePerfStats {
time: _,
executions: _,
introspection_stats: _,
phantom: _,
} => "PerfStats",
Event::Objective { objective_size: _ } => "Objective",
Event::Log {
severity_level: _,
message: _,
phantom: _,
} => "Log",
}
}
}
pub trait EventFirer<I, S>
where
I: Input,
{
fn fire(&mut self, state: &mut S, event: Event<I>) -> Result<(), Error>;
fn serialize_observers<OT>(&mut self, observers: &OT) -> Result<Vec<u8>, Error>
where
OT: ObserversTuple<I, S> + serde::Serialize,
{
Ok(postcard::to_allocvec(observers)?)
}
fn configuration(&self) -> &str {
"<default>"
}
}
pub trait EventRestarter<S> {
#[inline]
fn on_restart(&mut self, _state: &mut S) -> Result<(), Error> {
Ok(())
}
#[inline]
fn await_restart_safe(&mut self) {}
}
pub trait EventProcessor<E, I, S, Z> {
fn process(&mut self, fuzzer: &mut Z, state: &mut S, executor: &mut E) -> Result<usize, Error>;
fn deserialize_observers<OT>(&mut self, observers_buf: &[u8]) -> Result<OT, Error>
where
OT: ObserversTuple<I, S> + serde::de::DeserializeOwned,
{
Ok(postcard::from_bytes(observers_buf)?)
}
}
pub trait HasEventManagerId {
fn mgr_id(&self) -> EventManagerId;
}
pub trait EventManager<E, I, S, Z>:
EventFirer<I, S> + EventProcessor<E, I, S, Z> + EventRestarter<S> + HasEventManagerId
where
I: Input,
{
}
#[derive(Copy, Clone, Debug)]
pub struct NopEventManager {}
impl<I, S> EventFirer<I, S> for NopEventManager
where
I: Input,
{
fn fire(&mut self, _state: &mut S, _event: Event<I>) -> Result<(), Error> {
Ok(())
}
}
impl<S> EventRestarter<S> for NopEventManager {}
impl<E, I, S, Z> EventProcessor<E, I, S, Z> for NopEventManager {
fn process(
&mut self,
_fuzzer: &mut Z,
_state: &mut S,
_executor: &mut E,
) -> Result<usize, Error> {
Ok(0)
}
}
impl<E, I, S, Z> EventManager<E, I, S, Z> for NopEventManager where I: Input {}
impl HasEventManagerId for NopEventManager {
fn mgr_id(&self) -> EventManagerId {
EventManagerId { id: 0 }
}
}
#[cfg(test)]
mod tests {
use tuple_list::tuple_list_type;
use crate::{
bolts::{
current_time,
tuples::{tuple_list, Named},
},
events::Event,
executors::ExitKind,
inputs::bytes::BytesInput,
observers::StdMapObserver,
};
static mut MAP: [u32; 4] = [0; 4];
#[test]
fn test_event_serde() {
let obv = StdMapObserver::new("test", unsafe { &mut MAP });
let map = tuple_list!(obv);
let observers_buf = postcard::to_allocvec(&map).unwrap();
let i = BytesInput::new(vec![0]);
let e = Event::NewTestcase {
input: i,
observers_buf,
exit_kind: ExitKind::Ok,
corpus_size: 123,
client_config: "conf".into(),
time: current_time(),
executions: 0,
};
let serialized = postcard::to_allocvec(&e).unwrap();
let d = postcard::from_bytes::<Event<BytesInput>>(&serialized).unwrap();
match d {
Event::NewTestcase {
input: _,
observers_buf,
corpus_size: _,
exit_kind: _,
client_config: _,
time: _,
executions: _,
} => {
let o: tuple_list_type!(StdMapObserver::<u32>) =
postcard::from_bytes(&observers_buf).unwrap();
assert_eq!("test", o.0.name());
}
_ => panic!("mistmatch"),
};
}
}