Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ShMem associated type #2861

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ pub trait X<A, B, C> // <- this trait have 3 generics, A, B, and C
fn do_other_stuff(&self, a: A, b: B); // <- this is not ideal because it does not have C.
}
```
- Generic naming should be consistent. Do NOT use multiple name for the same generic, it just makes things more confusing. Do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to add?
"bounds in where clause should be ordered, too"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the appropriate place i mean. not here

Copy link
Member Author

@rmalmain rmalmain Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if others are ok with this i think it's better. wdyt @addisoncrump @domenukk ?

```rust
pub struct X<A> {
phantom: PhanomData<A>,
}

impl<A> X<A> {}
```
But not,
```rust
pub struct X<A> {
phantom: PhanomData<A>,
}

impl<B> X<B> {} // <- Do NOT do that, use A instead of B
```
- Always alphabetically order the type generics. Therefore,
```rust
pub struct X<E, EM, OT, S, Z> {}; // <- Generics are alphabetically ordered
Expand Down
6 changes: 2 additions & 4 deletions libafl/src/events/broker_hooks/centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use core::{fmt::Debug, marker::PhantomData};
use libafl_bolts::{compress::GzipCompressor, llmp::LLMP_FLAG_COMPRESSED};
use libafl_bolts::{
llmp::{Flags, LlmpBrokerInner, LlmpHook, LlmpMsgHookResult, Tag},
shmem::ShMemProvider,
ClientId, Error,
};
use serde::de::DeserializeOwned;
Expand All @@ -21,14 +20,13 @@ pub struct CentralizedLlmpHook<I> {
phantom: PhantomData<I>,
}

impl<I, SP> LlmpHook<SP> for CentralizedLlmpHook<I>
impl<I, SHM, SP> LlmpHook<SHM, SP> for CentralizedLlmpHook<I>
where
I: DeserializeOwned,
SP: ShMemProvider,
{
fn on_new_message(
&mut self,
_broker_inner: &mut LlmpBrokerInner<SP>,
_broker_inner: &mut LlmpBrokerInner<SHM, SP>,
client_id: ClientId,
msg_tag: &mut Tag,
_msg_flags: &mut Flags,
Expand Down
11 changes: 4 additions & 7 deletions libafl/src/events/broker_hooks/centralized_multi_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use libafl_bolts::llmp::LLMP_FLAG_COMPRESSED;
use libafl_bolts::{
llmp::{Flags, LlmpBrokerInner, LlmpHook, LlmpMsgHookResult, Tag, LLMP_FLAG_FROM_MM},
ownedref::OwnedRef,
shmem::ShMemProvider,
ClientId, Error,
};
use serde::Serialize;
Expand Down Expand Up @@ -149,16 +148,15 @@ where
}
}

impl<A, I, SP> LlmpHook<SP> for TcpMultiMachineLlmpSenderHook<A, I>
impl<A, I, SHM, SP> LlmpHook<SHM, SP> for TcpMultiMachineLlmpSenderHook<A, I>
where
I: Input,
A: Clone + Display + ToSocketAddrs + Send + Sync + 'static,
SP: ShMemProvider,
{
/// check for received messages, and forward them alongside the incoming message to inner.
fn on_new_message(
&mut self,
_broker_inner: &mut LlmpBrokerInner<SP>,
_broker_inner: &mut LlmpBrokerInner<SHM, SP>,
_client_id: ClientId,
_msg_tag: &mut Tag,
_msg_flags: &mut Flags,
Expand Down Expand Up @@ -211,16 +209,15 @@ where
}
}

impl<A, I, SP> LlmpHook<SP> for TcpMultiMachineLlmpReceiverHook<A, I>
impl<A, I, SHM, SP> LlmpHook<SHM, SP> for TcpMultiMachineLlmpReceiverHook<A, I>
where
I: Input,
A: Clone + Display + ToSocketAddrs + Send + Sync + 'static,
SP: ShMemProvider,
{
/// check for received messages, and forward them alongside the incoming message to inner.
fn on_new_message(
&mut self,
_broker_inner: &mut LlmpBrokerInner<SP>,
_broker_inner: &mut LlmpBrokerInner<SHM, SP>,
_client_id: ClientId,
_msg_tag: &mut Tag,
_msg_flags: &mut Flags,
Expand Down
6 changes: 2 additions & 4 deletions libafl/src/events/broker_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use core::marker::PhantomData;
use libafl_bolts::{compress::GzipCompressor, llmp::LLMP_FLAG_COMPRESSED};
use libafl_bolts::{
llmp::{Flags, LlmpBrokerInner, LlmpHook, LlmpMsgHookResult, Tag},
shmem::ShMemProvider,
ClientId,
};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -40,15 +39,14 @@ pub struct StdLlmpEventHook<I, MT> {
phantom: PhantomData<I>,
}

impl<I, MT, SP> LlmpHook<SP> for StdLlmpEventHook<I, MT>
impl<I, MT, SHM, SP> LlmpHook<SHM, SP> for StdLlmpEventHook<I, MT>
where
I: DeserializeOwned,
SP: ShMemProvider,
MT: Monitor,
{
fn on_new_message(
&mut self,
_broker_inner: &mut LlmpBrokerInner<SP>,
_broker_inner: &mut LlmpBrokerInner<SHM, SP>,
client_id: ClientId,
msg_tag: &mut Tag,
#[cfg(feature = "llmp_compression")] msg_flags: &mut Flags,
Expand Down
102 changes: 57 additions & 45 deletions libafl/src/events/centralized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use libafl_bolts::{
};
use libafl_bolts::{
llmp::{LlmpClient, LlmpClientDescription, Tag},
shmem::{NopShMemProvider, ShMemProvider},
shmem::{NopShMem, NopShMemProvider, ShMem, ShMemProvider},
tuples::{Handle, MatchNameRef},
ClientId,
};
Expand Down Expand Up @@ -46,13 +46,10 @@ pub(crate) const _LLMP_TAG_TO_MAIN: Tag = Tag(0x3453453);

/// A wrapper manager to implement a main-secondary architecture with another broker
#[derive(Debug)]
pub struct CentralizedEventManager<EM, EMH, I, S, SP>
where
SP: ShMemProvider,
{
pub struct CentralizedEventManager<EM, EMH, I, S, SHM, SP> {
inner: EM,
/// The centralized LLMP client for inter process communication
client: LlmpClient<SP>,
client: LlmpClient<SHM, SP>,
#[cfg(feature = "llmp_compression")]
compressor: GzipCompressor,
time_ref: Option<Handle<TimeObserver>>,
Expand All @@ -61,7 +58,16 @@ where
phantom: PhantomData<(I, S)>,
}

impl CentralizedEventManager<NopEventManager, (), NopInput, NopState<NopInput>, NopShMemProvider> {
impl
CentralizedEventManager<
NopEventManager,
(),
NopInput,
NopState<NopInput>,
NopShMem,
NopShMemProvider,
>
{
/// Creates a builder for [`CentralizedEventManager`]
#[must_use]
pub fn builder() -> CentralizedEventManagerBuilder {
Expand Down Expand Up @@ -95,16 +101,13 @@ impl CentralizedEventManagerBuilder {
}

/// Creates a new [`CentralizedEventManager`].
pub fn build_from_client<EM, EMH, I, S, SP>(
pub fn build_from_client<EM, EMH, I, S, SHM, SP>(
self,
inner: EM,
hooks: EMH,
client: LlmpClient<SP>,
client: LlmpClient<SHM, SP>,
time_obs: Option<Handle<TimeObserver>>,
) -> Result<CentralizedEventManager<EM, EMH, I, S, SP>, Error>
where
SP: ShMemProvider,
{
) -> Result<CentralizedEventManager<EM, EMH, I, S, SHM, SP>, Error> {
Ok(CentralizedEventManager {
inner,
hooks,
Expand All @@ -121,59 +124,61 @@ impl CentralizedEventManagerBuilder {
///
/// If the port is not yet bound, it will act as a broker; otherwise, it
/// will act as a client.
pub fn build_on_port<EM, EMH, I, S, SP>(
pub fn build_on_port<EM, EMH, I, S, SHM, SP>(
self,
inner: EM,
hooks: EMH,
shmem_provider: SP,
port: u16,
time_obs: Option<Handle<TimeObserver>>,
) -> Result<CentralizedEventManager<EM, EMH, I, S, SP>, Error>
) -> Result<CentralizedEventManager<EM, EMH, I, S, SHM, SP>, Error>
where
SP: ShMemProvider,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
let client = LlmpClient::create_attach_to_tcp(shmem_provider, port)?;
Self::build_from_client(self, inner, hooks, client, time_obs)
}

/// If a client respawns, it may reuse the existing connection, previously
/// stored by [`LlmpClient::to_env()`].
pub fn build_existing_client_from_env<EM, EMH, I, S, SP>(
pub fn build_existing_client_from_env<EM, EMH, I, S, SHM, SP>(
self,
inner: EM,
hooks: EMH,
shmem_provider: SP,
env_name: &str,
time_obs: Option<Handle<TimeObserver>>,
) -> Result<CentralizedEventManager<EM, EMH, I, S, SP>, Error>
) -> Result<CentralizedEventManager<EM, EMH, I, S, SHM, SP>, Error>
where
SP: ShMemProvider,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
let client = LlmpClient::on_existing_from_env(shmem_provider, env_name)?;
Self::build_from_client(self, inner, hooks, client, time_obs)
}

/// Create an existing client from description
pub fn existing_client_from_description<EM, EMH, I, S, SP>(
pub fn existing_client_from_description<EM, EMH, I, S, SHM, SP>(
self,
inner: EM,
hooks: EMH,
shmem_provider: SP,
description: &LlmpClientDescription,
time_obs: Option<Handle<TimeObserver>>,
) -> Result<CentralizedEventManager<EM, EMH, I, S, SP>, Error>
) -> Result<CentralizedEventManager<EM, EMH, I, S, SHM, SP>, Error>
where
SP: ShMemProvider,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
let client = LlmpClient::existing_client_from_description(shmem_provider, description)?;
Self::build_from_client(self, inner, hooks, client, time_obs)
}
}

impl<EM, EMH, I, S, SP> AdaptiveSerializer for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> AdaptiveSerializer for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: AdaptiveSerializer,
SP: ShMemProvider,
{
fn serialization_time(&self) -> Duration {
self.inner.serialization_time()
Expand Down Expand Up @@ -206,13 +211,14 @@ where
}
}

impl<EM, EMH, I, S, SP> EventFirer<I, S> for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> EventFirer<I, S> for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: HasEventManagerId + EventFirer<I, S>,
EMH: EventManagerHooksTuple<I, S>,
SP: ShMemProvider,
S: Stoppable,
I: Input,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
fn should_send(&self) -> bool {
self.inner.should_send()
Expand Down Expand Up @@ -262,10 +268,11 @@ where
}
}

impl<EM, EMH, I, S, SP> EventRestarter<S> for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> EventRestarter<S> for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
SP: ShMemProvider,
EM: EventRestarter<S>,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
#[inline]
fn on_restart(&mut self, state: &mut S) -> Result<(), Error> {
Expand All @@ -275,10 +282,10 @@ where
}
}

impl<EM, EMH, I, OT, S, SP> CanSerializeObserver<OT> for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, OT, S, SHM, SP> CanSerializeObserver<OT>
for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: AdaptiveSerializer,
SP: ShMemProvider,
OT: Serialize + MatchNameRef,
{
fn serialize_observers(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> {
Expand All @@ -291,10 +298,11 @@ where
}
}

impl<EM, EMH, I, S, SP> ManagerExit for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> ManagerExit for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: ManagerExit,
SP: ShMemProvider,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
fn send_exiting(&mut self) -> Result<(), Error> {
self.client.sender_mut().send_exiting()?;
Expand All @@ -308,15 +316,17 @@ where
}
}

impl<E, EM, EMH, I, S, SP, Z> EventProcessor<E, S, Z> for CentralizedEventManager<EM, EMH, I, S, SP>
impl<E, EM, EMH, I, S, SHM, SP, Z> EventProcessor<E, S, Z>
for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
E: HasObservers,
E::Observers: DeserializeOwned,
EM: EventProcessor<E, S, Z> + HasEventManagerId + EventFirer<I, S>,
EMH: EventManagerHooksTuple<I, S>,
S: Stoppable,
I: Input,
SP: ShMemProvider,
S: Stoppable,
SHM: ShMem,
SP: ShMemProvider<SHM>,
Z: ExecutionProcessor<Self, I, E::Observers, S> + EvaluatorObservers<E, Self, I, S>,
{
fn process(&mut self, fuzzer: &mut Z, state: &mut S, executor: &mut E) -> Result<usize, Error> {
Expand All @@ -336,13 +346,14 @@ where
}
}

impl<EM, EMH, I, S, SP> ProgressReporter<S> for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> ProgressReporter<S> for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: EventFirer<I, S> + HasEventManagerId,
EMH: EventManagerHooksTuple<I, S>,
S: HasExecutions + HasMetadata + HasLastReportTime + Stoppable + MaybeHasClientPerfMonitor,
I: Input,
SP: ShMemProvider,
S: HasExecutions + HasMetadata + HasLastReportTime + Stoppable + MaybeHasClientPerfMonitor,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
fn maybe_report_progress(
&mut self,
Expand All @@ -357,19 +368,19 @@ where
}
}

impl<EM, EMH, I, S, SP> HasEventManagerId for CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> HasEventManagerId for CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: HasEventManagerId,
SP: ShMemProvider,
{
fn mgr_id(&self) -> EventManagerId {
self.inner.mgr_id()
}
}

impl<EM, EMH, I, S, SP> CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
SP: ShMemProvider,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
/// Describe the client event manager's LLMP parts in a restorable fashion
pub fn describe(&self) -> Result<LlmpClientDescription, Error> {
Expand All @@ -388,13 +399,14 @@ where
}
}

impl<EM, EMH, I, S, SP> CentralizedEventManager<EM, EMH, I, S, SP>
impl<EM, EMH, I, S, SHM, SP> CentralizedEventManager<EM, EMH, I, S, SHM, SP>
where
EM: HasEventManagerId + EventFirer<I, S>,
EMH: EventManagerHooksTuple<I, S>,
S: Stoppable,
I: Input,
SP: ShMemProvider,
S: Stoppable,
SHM: ShMem,
SP: ShMemProvider<SHM>,
{
#[cfg(feature = "llmp_compression")]
fn forward_to_main(&mut self, event: &Event<I>) -> Result<(), Error> {
Expand Down
Loading
Loading