Skip to content

Commit

Permalink
Remove shmem associated type (#2870)
Browse files Browse the repository at this point in the history
* reduce shm trait bound

* Rename to SendExiting

* alpha beta gamam

* alphabet

* work

* std only

---------

Co-authored-by: Romain Malmain <[email protected]>
  • Loading branch information
domenukk and rmalmain authored Jan 20, 2025
1 parent 72adb48 commit 7e18887
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 473 deletions.
2 changes: 1 addition & 1 deletion fuzzers/binary_only/qemu_cmin/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{env, fmt::Write, io, path::PathBuf, process, ptr::NonNull};
use clap::{builder::Str, Parser};
use libafl::{
corpus::{Corpus, InMemoryOnDiskCorpus, NopCorpus},
events::{EventRestarter, ManagerExit, SimpleRestartingEventManager},
events::{EventRestarter, SendExiting, SimpleRestartingEventManager},
executors::ExitKind,
feedbacks::MaxMapFeedback,
fuzzer::StdFuzzer,
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/binary_only/qemu_coverage/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap::{builder::Str, Parser};
use libafl::{
corpus::{Corpus, InMemoryCorpus},
events::{
launcher::Launcher, ClientDescription, EventConfig, LlmpRestartingEventManager, ManagerExit,
launcher::Launcher, ClientDescription, EventConfig, LlmpRestartingEventManager, SendExiting,
},
executors::ExitKind,
fuzzer::StdFuzzer,
Expand Down
36 changes: 17 additions & 19 deletions libafl/src/events/centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ use libafl_bolts::{
};
use libafl_bolts::{
llmp::{LlmpClient, LlmpClientDescription, Tag},
shmem::{NopShMem, NopShMemProvider, ShMem, ShMemProvider},
shmem::{ShMem, ShMemProvider},
tuples::{Handle, MatchNameRef},
ClientId,
};
use serde::{de::DeserializeOwned, Serialize};

use super::{CanSerializeObserver, ManagerExit, NopEventManager};
use super::AwaitRestartSafe;
#[cfg(feature = "llmp_compression")]
use crate::events::llmp::COMPRESS_THRESHOLD;
use crate::{
common::HasMetadata,
events::{
serialize_observers_adaptive, std_maybe_report_progress, std_report_progress,
AdaptiveSerializer, Event, EventConfig, EventFirer, EventManagerHooksTuple, EventManagerId,
EventProcessor, EventRestarter, HasEventManagerId, LogSeverity, ProgressReporter,
AdaptiveSerializer, CanSerializeObserver, Event, EventConfig, EventFirer,
EventManagerHooksTuple, EventManagerId, EventProcessor, EventRestarter, HasEventManagerId,
LogSeverity, ProgressReporter, SendExiting,
},
executors::HasObservers,
fuzzer::{EvaluatorObservers, ExecutionProcessor},
inputs::{Input, NopInput},
inputs::Input,
observers::TimeObserver,
state::{HasExecutions, HasLastReportTime, MaybeHasClientPerfMonitor, NopState, Stoppable},
state::{HasExecutions, HasLastReportTime, MaybeHasClientPerfMonitor, Stoppable},
Error,
};

Expand All @@ -58,16 +59,7 @@ pub struct CentralizedEventManager<EM, EMH, I, S, SHM, SP> {
phantom: PhantomData<(I, S)>,
}

impl
CentralizedEventManager<
NopEventManager,
(),
NopInput,
NopState<NopInput>,
NopShMem,
NopShMemProvider,
>
{
impl CentralizedEventManager<(), (), (), (), (), ()> {
/// Creates a builder for [`CentralizedEventManager`]
#[must_use]
pub fn builder() -> CentralizedEventManagerBuilder {
Expand Down Expand Up @@ -291,7 +283,7 @@ impl<EM, EMH, I, OT, S, SHM, SP> CanSerializeObserver<OT>
for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: AdaptiveSerializer,
OT: Serialize + MatchNameRef,
OT: MatchNameRef + Serialize,
{
fn serialize_observers(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> {
serialize_observers_adaptive::<EM, OT>(
Expand All @@ -303,17 +295,23 @@ where
}
}

impl<EM, EMH, I, S, SHM, SP> ManagerExit for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
impl<EM, EMH, I, S, SHM, SP> SendExiting for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: ManagerExit,
EM: SendExiting,
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn send_exiting(&mut self) -> Result<(), Error> {
self.client.sender_mut().send_exiting()?;
self.inner.send_exiting()
}
}

impl<EM, EMH, I, S, SHM, SP> AwaitRestartSafe for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
SHM: ShMem,
EM: AwaitRestartSafe,
{
#[inline]
fn await_restart_safe(&mut self) {
self.client.await_safe_to_unmap_blocking();
Expand Down
47 changes: 23 additions & 24 deletions libafl/src/events/llmp/mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use libafl_bolts::{
use libafl_bolts::{
current_time,
llmp::{LlmpClient, LlmpClientDescription, LLMP_FLAG_FROM_MM},
shmem::{NopShMem, NopShMemProvider, ShMem, ShMemProvider},
shmem::{NopShMem, ShMem, ShMemProvider},
tuples::Handle,
ClientId,
};
Expand All @@ -38,18 +38,18 @@ use crate::events::{serialize_observers_adaptive, CanSerializeObserver};
use crate::{
events::{
llmp::{LLMP_TAG_EVENT_TO_BOTH, _LLMP_TAG_EVENT_TO_BROKER},
std_maybe_report_progress, std_on_restart, std_report_progress, AdaptiveSerializer, Event,
EventConfig, EventFirer, EventManagerHooksTuple, EventManagerId, EventProcessor,
EventRestarter, HasEventManagerId, ManagerExit, ProgressReporter,
std_maybe_report_progress, std_on_restart, std_report_progress, AdaptiveSerializer,
AwaitRestartSafe, Event, EventConfig, EventFirer, EventManagerHooksTuple, EventManagerId,
EventProcessor, EventRestarter, HasEventManagerId, ProgressReporter, SendExiting,
},
executors::HasObservers,
fuzzer::{EvaluatorObservers, ExecutionProcessor},
inputs::{Input, NopInput},
inputs::Input,
observers::TimeObserver,
stages::HasCurrentStageId,
state::{
HasCurrentTestcase, HasExecutions, HasImported, HasLastReportTime, HasSolutions,
MaybeHasClientPerfMonitor, NopState, Stoppable,
MaybeHasClientPerfMonitor, Stoppable,
},
Error, HasMetadata,
};
Expand All @@ -62,7 +62,6 @@ const INITIAL_EVENT_BUFFER_SIZE: usize = 1024 * 4;
pub struct LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
/// We only send 1 testcase for every `throttle` second
pub(crate) throttle: Option<Duration>,
Expand All @@ -82,11 +81,11 @@ where
serializations_cnt: usize,
should_serialize_cnt: usize,
pub(crate) time_ref: Option<Handle<TimeObserver>>,
phantom: PhantomData<(I, S)>,
event_buffer: Vec<u8>,
phantom: PhantomData<(I, S)>,
}

impl LlmpEventManager<(), NopState<NopInput>, NopInput, NopShMem, NopShMemProvider> {
impl LlmpEventManager<(), (), (), NopShMem, ()> {
/// Creates a builder for [`LlmpEventManager`]
#[must_use]
pub fn builder() -> LlmpEventManagerBuilder<()> {
Expand Down Expand Up @@ -143,7 +142,6 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
) -> Result<LlmpEventManager<EMH, I, S, SHM, SP>, Error>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
Ok(LlmpEventManager {
throttle: self.throttle,
Expand All @@ -158,8 +156,8 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
serializations_cnt: 0,
should_serialize_cnt: 0,
time_ref,
phantom: PhantomData,
event_buffer: Vec::with_capacity(INITIAL_EVENT_BUFFER_SIZE),
phantom: PhantomData,
})
}

Expand Down Expand Up @@ -217,11 +215,10 @@ impl<EMH> LlmpEventManagerBuilder<EMH> {
}

#[cfg(feature = "std")]
impl<EMH, I, OT, S, SHM, SP> CanSerializeObserver<OT> for LlmpEventManager<EMH, I, S, SHM, SP>
impl<EMH, I, S, OT, SHM, SP> CanSerializeObserver<OT> for LlmpEventManager<EMH, I, S, SHM, SP>
where
OT: Serialize + MatchNameRef,
OT: MatchNameRef + Serialize,
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn serialize_observers(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> {
serialize_observers_adaptive::<Self, OT>(self, observers, 2, 80)
Expand All @@ -231,7 +228,6 @@ where
impl<EMH, I, S, SHM, SP> AdaptiveSerializer for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn serialization_time(&self) -> Duration {
self.serialization_time
Expand Down Expand Up @@ -267,7 +263,7 @@ where
impl<EMH, I, S, SHM, SP> Debug for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
SP: Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut debug_struct = f.debug_struct("LlmpEventManager");
Expand All @@ -277,15 +273,13 @@ where
let debug = debug.field("compressor", &self.compressor);
debug
.field("configuration", &self.configuration)
.field("phantom", &self.phantom)
.finish_non_exhaustive()
}
}

impl<EMH, I, S, SHM, SP> Drop for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
/// LLMP clients will have to wait until their pages are mapped by somebody.
fn drop(&mut self) {
Expand All @@ -296,7 +290,6 @@ where
impl<EMH, I, S, SHM, SP> LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
/// Calling this function will tell the llmp broker that this client is exiting
/// This should be called from the restarter not from the actual fuzzer client
Expand Down Expand Up @@ -330,7 +323,12 @@ where
log::debug!("Asking he broker to be disconnected");
Ok(())
}
}

impl<EMH, I, S, SHM, SP> LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
{
/// Describe the client event manager's LLMP parts in a restorable fashion
pub fn describe(&self) -> Result<LlmpClientDescription, Error> {
self.llmp.describe()
Expand All @@ -347,7 +345,6 @@ where
impl<EMH, I, S, SHM, SP> LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
// Handle arriving events in the client
fn handle_in_client<E, Z>(
Expand Down Expand Up @@ -516,23 +513,26 @@ impl<EMH, I, S, SHM, SP> EventRestarter<S> for LlmpEventManager<EMH, I, S, SHM,
where
S: HasCurrentStageId,
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn on_restart(&mut self, state: &mut S) -> Result<(), Error> {
std_on_restart(self, state)
}
}

impl<EMH, I, S, SHM, SP> ManagerExit for LlmpEventManager<EMH, I, S, SHM, SP>
impl<EMH, I, S, SHM, SP> SendExiting for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn send_exiting(&mut self) -> Result<(), Error> {
self.llmp.sender_mut().send_exiting()
}
}

impl<EMH, I, S, SHM, SP> AwaitRestartSafe for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
{
/// The LLMP client needs to wait until a broker has mapped all pages before shutting down.
/// Otherwise, the OS may already have removed the shared maps.
fn await_restart_safe(&mut self) {
Expand Down Expand Up @@ -621,7 +621,6 @@ where
impl<EMH, I, S, SHM, SP> HasEventManagerId for LlmpEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
/// Gets the id assigned to this staterestorer.
fn mgr_id(&self) -> EventManagerId {
Expand Down
22 changes: 12 additions & 10 deletions libafl/src/events/llmp/restarting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ use crate::{
common::HasMetadata,
events::{
launcher::ClientDescription, serialize_observers_adaptive, std_maybe_report_progress,
std_report_progress, AdaptiveSerializer, CanSerializeObserver, Event, EventConfig,
EventFirer, EventManagerHooksTuple, EventManagerId, EventProcessor, EventRestarter,
HasEventManagerId, LlmpEventManager, LlmpShouldSaveState, ManagerExit, ProgressReporter,
StdLlmpEventHook,
std_report_progress, AdaptiveSerializer, AwaitRestartSafe, CanSerializeObserver, Event,
EventConfig, EventFirer, EventManagerHooksTuple, EventManagerId, EventProcessor,
EventRestarter, HasEventManagerId, LlmpEventManager, LlmpShouldSaveState, ProgressReporter,
SendExiting, StdLlmpEventHook,
},
executors::HasObservers,
fuzzer::{EvaluatorObservers, ExecutionProcessor},
Expand All @@ -58,7 +58,6 @@ use crate::{
pub struct LlmpRestartingEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
/// The embedded LLMP event manager
llmp_mgr: LlmpEventManager<EMH, I, S, SHM, SP>,
Expand All @@ -71,7 +70,6 @@ where
impl<EMH, I, S, SHM, SP> AdaptiveSerializer for LlmpRestartingEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn serialization_time(&self) -> Duration {
self.llmp_mgr.serialization_time()
Expand Down Expand Up @@ -151,9 +149,8 @@ where
impl<EMH, I, OT, S, SHM, SP> CanSerializeObserver<OT>
for LlmpRestartingEventManager<EMH, I, S, SHM, SP>
where
OT: Serialize + MatchNameRef,
OT: MatchNameRef + Serialize,
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
{
fn serialize_observers(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> {
serialize_observers_adaptive::<Self, OT>(self, observers, 2, 80)
Expand Down Expand Up @@ -187,7 +184,7 @@ where
}
}

impl<EMH, I, S, SHM, SP> ManagerExit for LlmpRestartingEventManager<EMH, I, S, SHM, SP>
impl<EMH, I, S, SHM, SP> SendExiting for LlmpRestartingEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
SP: ShMemProvider<ShMem = SHM>,
Expand All @@ -198,7 +195,12 @@ where
// This way, the broker can clean up the pages, and eventually exit.
self.llmp_mgr.send_exiting()
}
}

impl<EMH, I, S, SHM, SP> AwaitRestartSafe for LlmpRestartingEventManager<EMH, I, S, SHM, SP>
where
SHM: ShMem,
{
/// The llmp client needs to wait until a broker mapped all pages, before shutting down.
/// Otherwise, the OS may already have removed the shared maps,
#[inline]
Expand Down Expand Up @@ -331,9 +333,9 @@ pub fn setup_restarting_mgr_std<I, MT, S>(
Error,
>
where
I: DeserializeOwned,
MT: Monitor + Clone,
S: Serialize + DeserializeOwned,
I: DeserializeOwned,
{
RestartingMgr::builder()
.shmem_provider(StdShMemProvider::new()?)
Expand Down
Loading

0 comments on commit 7e18887

Please sign in to comment.