Skip to content

Commit

Permalink
Remove ShMemProvider bound from struct definitions (#2861)
Browse files Browse the repository at this point in the history
* No more ShMemProvider bound constraint in struct definition whenever possible

* Introduce StdShMem

* Update CONTRIBUTING.md
  • Loading branch information
rmalmain authored Jan 20, 2025
1 parent 348bfdc commit 8089b18
Show file tree
Hide file tree
Showing 48 changed files with 753 additions and 663 deletions.
47 changes: 45 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub trait X
}
```

- __Ideally__ the types used in the the arguments of methods in traits should have the same as the types defined on the traits.
- __Ideally__ the types used in the arguments of methods in traits should have the same as the types defined on the traits.
```rust
pub trait X<A, B, C> // <- this trait have 3 generics, A, B, and C
{
Expand All @@ -84,11 +84,54 @@ 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:
```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
```
But not,
```rust
pub struct X<S, OT, Z, EM, E> {}; // <- Generics are not ordered
```
```
- Similarly, generic bounds in `where` clauses should be alphabetically sorted. Prefer:
```rust
pub trait FooA {}
pub trait FooB {}

pub struct X<A, B>;

impl<A, B> X<A, B>
where
A: FooA,
B: FooB,
{}
```
Over:
```rust
pub trait FooA {}
pub trait FooB {}

pub struct X<A, B>;

impl<A, B> X<A, B>
where
B: FooB, // <-|
// | Generic bounds are not alphabetically ordered.
A: FooA, // <-|
{}
```
8 changes: 4 additions & 4 deletions fuzzers/binary_only/frida_executable_libpng/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ unsafe fn fuzz(
let shmem_provider = StdShMemProvider::new()?;

let mut run_client = |state: Option<_>,
mgr: LlmpRestartingEventManager<_, _, _, _>,
mgr: LlmpRestartingEventManager<_, _, _, _, _>,
client_description: ClientDescription| {
// The restarting state will spawn the same process again as child, then restarted it each time it crashes.

// println!("{:?}", mgr.mgr_id());

if options.asan && options.asan_cores.contains(client_description.core_id()) {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down Expand Up @@ -231,7 +231,7 @@ unsafe fn fuzz(
})(state, mgr, client_description)
} else if options.cmplog && options.cmplog_cores.contains(client_description.core_id()) {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down Expand Up @@ -367,7 +367,7 @@ unsafe fn fuzz(
})(state, mgr, client_description)
} else {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down
4 changes: 2 additions & 2 deletions fuzzers/binary_only/frida_libpng/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {
};

let mut run_client = |state: Option<_>,
mgr: LlmpRestartingEventManager<_, _, _, _>,
mgr: LlmpRestartingEventManager<_, _, _, _, _>,
client_description: ClientDescription| {
// The restarting state will spawn the same process again as child, then restarted it each time it crashes.

Expand All @@ -101,7 +101,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {

// if options.asan && options.asan_cores.contains(client_description.core_id()) {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down
8 changes: 4 additions & 4 deletions fuzzers/binary_only/frida_windows_gdiplus/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {
let shmem_provider = StdShMemProvider::new()?;

let mut run_client = |state: Option<_>,
mgr: LlmpRestartingEventManager<_, _, _, _>,
mgr: LlmpRestartingEventManager<_, _, _, _, _>,
client_description: ClientDescription| {
// The restarting state will spawn the same process again as child, then restarted it each time it crashes.

Expand All @@ -98,7 +98,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {

if options.asan && options.asan_cores.contains(client_description.core_id()) {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down Expand Up @@ -214,7 +214,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {
})(state, mgr, client_description)
} else if options.cmplog && options.cmplog_cores.contains(client_description.core_id()) {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

Expand Down Expand Up @@ -344,7 +344,7 @@ unsafe fn fuzz(options: &FuzzerOptions) -> Result<(), Error> {
})(state, mgr, client_description)
} else {
(|state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
_client_description| {
let gum = Gum::obtain();

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 @@ -124,7 +124,7 @@ pub fn fuzz() {
env::remove_var("LD_LIBRARY_PATH");

let mut run_client = |state: Option<_>,
mut mgr: LlmpRestartingEventManager<_, _, _, _>,
mut mgr: LlmpRestartingEventManager<_, _, _, _, _>,
client_description: ClientDescription| {
let mut cov_path = options.coverage_path.clone();

Expand Down
6 changes: 3 additions & 3 deletions fuzzers/binary_only/qemu_launcher/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libafl::events::SimpleEventManager;
use libafl::events::{LlmpRestartingEventManager, MonitorTypedEventManager};
use libafl::{
corpus::{Corpus, InMemoryOnDiskCorpus, OnDiskCorpus},
events::{ClientDescription, EventRestarter, NopEventManager},
events::{ClientDescription, EventRestarter},
executors::{Executor, ShadowExecutor},
feedback_or, feedback_or_fast,
feedbacks::{CrashFeedback, MaxMapFeedback, TimeFeedback, TimeoutFeedback},
Expand All @@ -30,7 +30,7 @@ use libafl::{
Error, HasMetadata, NopFuzzer,
};
#[cfg(not(feature = "simplemgr"))]
use libafl_bolts::shmem::StdShMemProvider;
use libafl_bolts::shmem::{StdShMem, StdShMemProvider};
use libafl_bolts::{
ownedref::OwnedMutSlice,
rands::StdRand,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub type ClientState =
pub type ClientMgr<M> = SimpleEventManager<BytesInput, M, ClientState>;
#[cfg(not(feature = "simplemgr"))]
pub type ClientMgr<M> = MonitorTypedEventManager<
LlmpRestartingEventManager<(), BytesInput, ClientState, StdShMemProvider>,
LlmpRestartingEventManager<(), BytesInput, ClientState, StdShMem, StdShMemProvider>,
M,
>;

Expand Down
12 changes: 6 additions & 6 deletions fuzzers/forkserver/libafl-fuzz/src/feedback/filepath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ where
Ok(false)
}

#[cfg(feature = "track_hit_feedbacks")]
#[inline]
fn last_result(&self) -> Result<bool, Error> {
Ok(false)
}

fn append_metadata(
&mut self,
state: &mut S,
Expand All @@ -78,10 +84,4 @@ where
(self.func)(state, testcase, &self.out_dir)?;
Ok(())
}

#[cfg(feature = "track_hit_feedbacks")]
#[inline]
fn last_result(&self) -> Result<bool, Error> {
Ok(false)
}
}
5 changes: 3 additions & 2 deletions fuzzers/forkserver/libafl-fuzz/src/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use libafl::{
Error, Fuzzer, HasFeedback, HasMetadata, SerdeAny,
};
#[cfg(not(feature = "fuzzbench"))]
use libafl_bolts::shmem::StdShMemProvider;
use libafl_bolts::shmem::{StdShMem, StdShMemProvider};
use libafl_bolts::{
core_affinity::CoreId,
current_nanos, current_time,
Expand Down Expand Up @@ -77,10 +77,11 @@ pub type LibaflFuzzState =

#[cfg(not(feature = "fuzzbench"))]
type LibaflFuzzManager = CentralizedEventManager<
LlmpRestartingEventManager<(), BytesInput, LibaflFuzzState, StdShMemProvider>,
LlmpRestartingEventManager<(), BytesInput, LibaflFuzzState, StdShMem, StdShMemProvider>,
(),
BytesInput,
LibaflFuzzState,
StdShMem,
StdShMemProvider,
>;
#[cfg(feature = "fuzzbench")]
Expand Down
4 changes: 2 additions & 2 deletions fuzzers/full_system/nyx_launcher/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libafl::{
use libafl_bolts::{
current_nanos,
rands::StdRand,
shmem::StdShMemProvider,
shmem::{StdShMem, StdShMemProvider},
tuples::{tuple_list, Merge},
};
use libafl_nyx::{
Expand All @@ -44,7 +44,7 @@ pub type ClientState =
StdState<InMemoryOnDiskCorpus<BytesInput>, BytesInput, StdRand, OnDiskCorpus<BytesInput>>;

pub type ClientMgr<M> = MonitorTypedEventManager<
LlmpRestartingEventManager<(), BytesInput, ClientState, StdShMemProvider>,
LlmpRestartingEventManager<(), BytesInput, ClientState, StdShMem, StdShMemProvider>,
M,
>;

Expand Down
2 changes: 1 addition & 1 deletion fuzzers/inprocess/libfuzzer_libpng_centralized/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub extern "C" fn libafl_main() {

let mut secondary_run_client =
|state: Option<_>,
mut mgr: CentralizedEventManager<_, _, _, _, _>,
mut mgr: CentralizedEventManager<_, _, _, _, _, _>,
_client_description: ClientDescription| {
// Create an observation channel using the coverage map
let edges_observer =
Expand Down
4 changes: 2 additions & 2 deletions fuzzers/inprocess/libfuzzer_libpng_cmin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ fn fuzz(corpus_dirs: &[PathBuf], objective_dir: PathBuf, broker_port: u16) -> Re

let orig_size = state.corpus().count();
let msg = "Started distillation...".to_string();
<LlmpRestartingEventManager<_, _, _, _> as EventFirer<BytesInput, _>>::log(
<LlmpRestartingEventManager<_, _, _, _, _> as EventFirer<BytesInput, _>>::log(
&mut restarting_mgr,
&mut state,
LogSeverity::Info,
msg,
)?;
minimizer.minimize(&mut fuzzer, &mut executor, &mut restarting_mgr, &mut state)?;
let msg = format!("Distilled out {} cases", orig_size - state.corpus().count());
<LlmpRestartingEventManager<_, _, _, _> as EventFirer<BytesInput, _>>::log(
<LlmpRestartingEventManager<_, _, _, _, _> as EventFirer<BytesInput, _>>::log(
&mut restarting_mgr,
&mut state,
LogSeverity::Info,
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/inprocess/libfuzzer_libpng_norestart/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub extern "C" fn libafl_main() {
);

let mut run_client = |state: Option<_>,
mut restarting_mgr: LlmpRestartingEventManager<_, _, _, _>,
mut restarting_mgr: LlmpRestartingEventManager<_, _, _, _, _>,
client_description: ClientDescription| {
// Create an observation channel using the coverage map
let edges_observer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub extern "C" fn libafl_main() {

let mut secondary_run_client =
|state: Option<_>,
mut mgr: CentralizedEventManager<_, _, _, _>,
mut mgr: CentralizedEventManager<_, _, _, _, _, _>,
_client_description: ClientDescription| {
// Create an observation channel using the coverage map
let edges_observer =
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
Loading

0 comments on commit 8089b18

Please sign in to comment.