pub mod simple;
pub use simple::*;
pub mod llmp;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use core::{fmt, hash::Hasher, marker::PhantomData, time::Duration};
use ahash::AHasher;
pub use llmp::*;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use uuid::Uuid;
use crate::{
bolts::current_time,
executors::ExitKind,
inputs::Input,
monitors::UserStats,
observers::ObserversTuple,
stages::calibrate::UnstableEntriesMetadata,
state::{HasClientPerfMonitor, HasExecutions, HasMetadata},
Error,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct EventManagerId {
pub id: usize,
}
#[cfg(feature = "introspection")]
use crate::monitors::ClientPerfMonitor;
#[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(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CustomBufEventResult {
Handled,
Next,
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum BrokerEventResult {
Handled,
Forward,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventConfig {
AlwaysUnique,
FromName {
name_hash: u64,
},
#[cfg(feature = "std")]
BuildID {
id: Uuid,
},
}
impl EventConfig {
#[must_use]
pub fn from_name(name: &str) -> Self {
let mut hasher = AHasher::new_with_keys(0, 0);
hasher.write(name.as_bytes());
EventConfig::FromName {
name_hash: hasher.finish(),
}
}
#[cfg(feature = "std")]
#[must_use]
pub fn from_build_id() -> Self {
EventConfig::BuildID {
id: crate::bolts::build_id::get(),
}
}
#[must_use]
pub fn match_with(&self, other: &EventConfig) -> bool {
match self {
EventConfig::AlwaysUnique => false,
EventConfig::FromName { name_hash: a } => match other {
#[cfg(not(feature = "std"))]
EventConfig::AlwaysUnique => false,
EventConfig::FromName { name_hash: b } => a == b,
#[cfg(feature = "std")]
EventConfig::AlwaysUnique | EventConfig::BuildID { id: _ } => false,
},
#[cfg(feature = "std")]
EventConfig::BuildID { id: a } => match other {
EventConfig::AlwaysUnique | EventConfig::FromName { name_hash: _ } => false,
EventConfig::BuildID { id: b } => a == b,
},
}
}
}
impl From<&str> for EventConfig {
#[must_use]
fn from(name: &str) -> Self {
Self::from_name(name)
}
}
impl From<String> for EventConfig {
#[must_use]
fn from(name: String) -> Self {
Self::from_name(&name)
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub enum Event<I>
where
I: Input,
{
NewTestcase {
input: I,
observers_buf: Option<Vec<u8>>,
exit_kind: ExitKind,
corpus_size: usize,
client_config: EventConfig,
time: Duration,
executions: usize,
},
UpdateExecStats {
time: Duration,
executions: usize,
phantom: PhantomData<I>,
},
UpdateUserStats {
name: String,
value: UserStats,
phantom: PhantomData<I>,
},
#[cfg(feature = "introspection")]
UpdatePerfMonitor {
time: Duration,
executions: usize,
introspection_monitor: Box<ClientPerfMonitor>,
phantom: PhantomData<I>,
},
Objective {
objective_size: usize,
},
Log {
severity_level: LogSeverity,
message: String,
phantom: PhantomData<I>,
},
CustomBuf {
buf: Vec<u8>,
tag: String,
},
}
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::UpdateExecStats {
time: _,
executions: _,
phantom: _,
}
| Event::UpdateUserStats {
name: _,
value: _,
phantom: _,
} => "Stats",
#[cfg(feature = "introspection")]
Event::UpdatePerfMonitor {
time: _,
executions: _,
introspection_monitor: _,
phantom: _,
} => "PerfMonitor",
Event::Objective { .. } => "Objective",
Event::Log {
severity_level: _,
message: _,
phantom: _,
} => "Log",
Event::CustomBuf { .. } => "CustomBuf",
}
}
}
pub trait EventFirer<I>
where
I: Input,
{
fn fire<S>(&mut self, state: &mut S, event: Event<I>) -> Result<(), Error>;
fn log<S>(
&mut self,
state: &mut S,
severity_level: LogSeverity,
message: String,
) -> Result<(), Error> {
self.fire(
state,
Event::Log {
severity_level,
message,
phantom: PhantomData,
},
)
}
fn serialize_observers<OT, S>(&mut self, observers: &OT) -> Result<Vec<u8>, Error>
where
OT: ObserversTuple<I, S> + Serialize,
{
Ok(postcard::to_allocvec(observers)?)
}
fn configuration(&self) -> EventConfig {
EventConfig::AlwaysUnique
}
}
pub trait ProgressReporter<I>: EventFirer<I>
where
I: Input,
{
fn maybe_report_progress<S>(
&mut self,
state: &mut S,
last_report_time: Duration,
monitor_timeout: Duration,
) -> Result<Duration, Error>
where
S: HasExecutions + HasClientPerfMonitor + HasMetadata,
{
let executions = *state.executions();
let cur = current_time();
if cur.checked_sub(last_report_time).unwrap_or_default() > monitor_timeout {
#[cfg(not(feature = "introspection"))]
self.fire(
state,
Event::UpdateExecStats {
executions,
time: cur,
phantom: PhantomData,
},
)?;
if let Some(meta) = state.metadata().get::<UnstableEntriesMetadata>() {
let unstable_entries = meta.unstable_entries().len();
let map_len = meta.map_len();
self.fire(
state,
Event::UpdateUserStats {
name: "stability".to_string(),
value: UserStats::Ratio(unstable_entries as u64, map_len as u64),
phantom: PhantomData,
},
)?;
}
#[cfg(feature = "introspection")]
{
state
.introspection_monitor_mut()
.set_current_time(crate::bolts::cpu::read_time_counter());
self.fire(
state,
Event::UpdatePerfMonitor {
executions,
time: cur,
introspection_monitor: Box::new(state.introspection_monitor().clone()),
phantom: PhantomData,
},
)?;
}
Ok(cur)
} else {
if cur.as_millis() % 1000 == 0 {}
Ok(last_report_time)
}
}
}
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>
+ EventProcessor<E, I, S, Z>
+ EventRestarter<S>
+ HasEventManagerId
+ ProgressReporter<I>
where
I: Input,
{
}
type CustomBufHandlerFn<S> =
dyn FnMut(&mut S, &String, &[u8]) -> Result<CustomBufEventResult, Error>;
pub trait HasCustomBufHandlers<S> {
fn add_custom_buf_handler(&mut self, handler: Box<CustomBufHandlerFn<S>>);
}
#[derive(Copy, Clone, Debug)]
pub struct NopEventManager {}
impl<I> EventFirer<I> for NopEventManager
where
I: Input,
{
fn fire<S>(&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<S> HasCustomBufHandlers<S> for NopEventManager {
fn add_custom_buf_handler(
&mut self,
_handler: Box<dyn FnMut(&mut S, &String, &[u8]) -> Result<CustomBufEventResult, Error>>,
) {
}
}
impl<I> ProgressReporter<I> 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, EventConfig},
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: Some(observers_buf),
exit_kind: ExitKind::Ok,
corpus_size: 123,
client_config: EventConfig::AlwaysUnique,
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.as_ref().unwrap()).unwrap();
assert_eq!("test", o.0.name());
}
_ => panic!("mistmatch"),
};
}
}
#[cfg(feature = "python")]
#[allow(missing_docs)]
pub mod pybind {
use pyo3::prelude::*;
use crate::{
events::{
simple::pybind::PythonSimpleEventManager, Event, EventFirer, EventManager,
EventManagerId, EventProcessor, EventRestarter, HasEventManagerId, ProgressReporter,
},
executors::pybind::PythonExecutor,
fuzzer::pybind::PythonStdFuzzer,
inputs::BytesInput,
state::pybind::PythonStdState,
Error,
};
#[derive(Debug, Clone)]
pub enum PythonEventManagerWrapper {
Simple(Py<PythonSimpleEventManager>),
}
#[pyclass(unsendable, name = "EventManager")]
#[derive(Debug, Clone)]
pub struct PythonEventManager {
pub wrapper: PythonEventManagerWrapper,
}
macro_rules! unwrap_me {
($wrapper:expr, $name:ident, $body:block) => {
crate::unwrap_me_body!($wrapper, $name, $body, PythonEventManagerWrapper, {
Simple
})
};
}
macro_rules! unwrap_me_mut {
($wrapper:expr, $name:ident, $body:block) => {
crate::unwrap_me_mut_body!($wrapper, $name, $body, PythonEventManagerWrapper, {
Simple
})
};
}
#[pymethods]
impl PythonEventManager {
#[staticmethod]
#[must_use]
pub fn new_simple(mgr: Py<PythonSimpleEventManager>) -> Self {
Self {
wrapper: PythonEventManagerWrapper::Simple(mgr),
}
}
}
impl EventFirer<BytesInput> for PythonEventManager {
fn fire<S>(&mut self, state: &mut S, event: Event<BytesInput>) -> Result<(), Error> {
unwrap_me_mut!(self.wrapper, e, { e.fire(state, event) })
}
}
impl<S> EventRestarter<S> for PythonEventManager {}
impl EventProcessor<PythonExecutor, BytesInput, PythonStdState, PythonStdFuzzer>
for PythonEventManager
{
fn process(
&mut self,
fuzzer: &mut PythonStdFuzzer,
state: &mut PythonStdState,
executor: &mut PythonExecutor,
) -> Result<usize, Error> {
unwrap_me_mut!(self.wrapper, e, { e.process(fuzzer, state, executor) })
}
}
impl ProgressReporter<BytesInput> for PythonEventManager {}
impl HasEventManagerId for PythonEventManager {
fn mgr_id(&self) -> EventManagerId {
unwrap_me!(self.wrapper, e, { e.mgr_id() })
}
}
impl EventManager<PythonExecutor, BytesInput, PythonStdState, PythonStdFuzzer>
for PythonEventManager
{
}
pub fn register(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PythonEventManager>()?;
Ok(())
}
}