Skip to content

Commit 396d87f

Browse files
Remove lifetime parameter of inprocess executors (#3524)
For any `H: FnMut(...) -> T`, it is also the case that `&mut H: FnMut(...) -> T`. Thus, changing from `&mut H` to just `H` gives API clients more flexibility. They can still pass a `&mut` reference if they want to (as evidenced by not needing any changes in the example fuzzers), but they can also now pass an owned `H`. A client might want to do this to pass a `Box` they just allocated and don't want to hold onto. This also allows for removing some ubiquitous lifetime parameters, which is a nice simplification. Also, sprinkle a few `#[must_use]`s around while I'm in the area.
1 parent a8f1caf commit 396d87f

File tree

7 files changed

+93
-92
lines changed

7 files changed

+93
-92
lines changed

crates/libafl/src/executors/inprocess/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ pub mod inner;
3636
pub mod stateful;
3737

3838
/// The process executor simply calls a target function, as mutable reference to a closure.
39-
pub type InProcessExecutor<'a, EM, H, I, OT, S, Z> =
40-
GenericInProcessExecutor<EM, H, &'a mut H, (), I, OT, S, Z>;
39+
pub type InProcessExecutor<EM, H, I, OT, S, Z> =
40+
GenericInProcessExecutor<EM, H, H, (), I, OT, S, Z>;
4141

4242
/// The inprocess executor that allows hooks
43-
pub type HookableInProcessExecutor<'a, EM, H, HT, I, OT, S, Z> =
44-
GenericInProcessExecutor<EM, H, &'a mut H, HT, I, OT, S, Z>;
43+
pub type HookableInProcessExecutor<EM, H, HT, I, OT, S, Z> =
44+
GenericInProcessExecutor<EM, H, H, HT, I, OT, S, Z>;
4545
/// The process executor simply calls a target function, as boxed `FnMut` trait object
4646
pub type OwnedInProcessExecutor<EM, I, OT, S, Z> = GenericInProcessExecutor<
4747
EM,
@@ -124,7 +124,7 @@ impl<EM, H, HB, HT, I, OT, S, Z> HasObservers
124124
}
125125
}
126126

127-
impl<'a, EM, H, I, OT, S, Z> InProcessExecutor<'a, EM, H, I, OT, S, Z>
127+
impl<EM, H, I, OT, S, Z> InProcessExecutor<EM, H, I, OT, S, Z>
128128
where
129129
H: FnMut(&I) -> ExitKind + Sized,
130130
OT: ObserversTuple<I, S>,
@@ -133,7 +133,7 @@ where
133133
{
134134
/// Create a new in mem executor with the default timeout (5 sec)
135135
pub fn new<OF>(
136-
harness_fn: &'a mut H,
136+
harness_fn: H,
137137
observers: OT,
138138
fuzzer: &mut Z,
139139
state: &mut S,
@@ -164,7 +164,7 @@ where
164164
///
165165
/// This may return an error on unix, if signal handler setup fails
166166
pub fn with_timeout<OF>(
167-
harness_fn: &'a mut H,
167+
harness_fn: H,
168168
observers: OT,
169169
fuzzer: &mut Z,
170170
state: &mut S,
@@ -262,24 +262,28 @@ where
262262

263263
/// Retrieve the harness function.
264264
#[inline]
265+
#[must_use]
265266
pub fn harness(&self) -> &H {
266267
self.harness_fn.borrow()
267268
}
268269

269270
/// Retrieve the harness function for a mutable reference.
270271
#[inline]
272+
#[must_use]
271273
pub fn harness_mut(&mut self) -> &mut H {
272274
self.harness_fn.borrow_mut()
273275
}
274276

275277
/// The inprocess handlers
276278
#[inline]
279+
#[must_use]
277280
pub fn hooks(&self) -> &(InProcessHooks<I, S>, HT) {
278281
self.inner.hooks()
279282
}
280283

281284
/// The inprocess handlers (mutable)
282285
#[inline]
286+
#[must_use]
283287
pub fn hooks_mut(&mut self) -> &mut (InProcessHooks<I, S>, HT) {
284288
self.inner.hooks_mut()
285289
}

crates/libafl/src/executors/inprocess/stateful.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::{
2727

2828
/// The process executor simply calls a target function, as mutable reference to a closure
2929
/// The internal state of the executor is made available to the harness.
30-
pub type StatefulInProcessExecutor<'a, EM, ES, H, I, OT, S, Z> =
31-
StatefulGenericInProcessExecutor<EM, ES, H, &'a mut H, (), I, OT, S, Z>;
30+
pub type StatefulInProcessExecutor<EM, ES, H, I, OT, S, Z> =
31+
StatefulGenericInProcessExecutor<EM, ES, H, H, (), I, OT, S, Z>;
3232

3333
/// The process executor simply calls a target function, as boxed `FnMut` trait object
3434
/// The internal state of the executor is made available to the harness.
@@ -123,7 +123,7 @@ where
123123
}
124124
}
125125

126-
impl<'a, EM, ES, H, I, OT, S, Z> StatefulInProcessExecutor<'a, EM, ES, H, I, OT, S, Z>
126+
impl<EM, ES, H, I, OT, S, Z> StatefulInProcessExecutor<EM, ES, H, I, OT, S, Z>
127127
where
128128
H: FnMut(&mut ES, &mut S, &I) -> ExitKind + Sized,
129129
OT: ObserversTuple<I, S>,
@@ -132,7 +132,7 @@ where
132132
{
133133
/// Create a new in mem executor with the default timeout (5 sec)
134134
pub fn new<OF>(
135-
harness_fn: &'a mut H,
135+
harness_fn: H,
136136
exposed_executor_state: ES,
137137
observers: OT,
138138
fuzzer: &mut Z,
@@ -165,7 +165,7 @@ where
165165
///
166166
/// This may return an error on unix, if signal handler setup fails
167167
pub fn with_timeout<OF>(
168-
harness_fn: &'a mut H,
168+
harness_fn: H,
169169
exposed_executor_state: ES,
170170
observers: OT,
171171
fuzzer: &mut Z,

crates/libafl/src/executors/inprocess_fork/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ pub mod stateful;
3939
///
4040
/// On Linux, when fuzzing a Rust target, set `panic = "abort"` in your `Cargo.toml` (see [Cargo documentation](https://doc.rust-lang.org/cargo/reference/profiles.html#panic)).
4141
/// Else panics can not be caught by `LibAFL`.
42-
pub type InProcessForkExecutor<'a, EM, H, I, OT, S, SP, Z> =
43-
GenericInProcessForkExecutor<'a, EM, H, (), I, OT, S, SP, Z>;
42+
pub type InProcessForkExecutor<EM, H, I, OT, S, SP, Z> =
43+
GenericInProcessForkExecutor<EM, H, (), I, OT, S, SP, Z>;
4444

45-
impl<'a, H, I, OT, S, SP, EM, Z> InProcessForkExecutor<'a, EM, H, I, OT, S, SP, Z>
45+
impl<H, I, OT, S, SP, EM, Z> InProcessForkExecutor<EM, H, I, OT, S, SP, Z>
4646
where
4747
OT: ObserversTuple<I, S>,
4848
{
4949
/// The constructor for `InProcessForkExecutor`
5050
pub fn new(
51-
harness_fn: &'a mut H,
51+
harness_fn: H,
5252
observers: OT,
5353
fuzzer: &mut Z,
5454
state: &mut S,
@@ -73,13 +73,12 @@ where
7373
///
7474
/// On Linux, when fuzzing a Rust target, set `panic = "abort"` in your `Cargo.toml` (see [Cargo documentation](https://doc.rust-lang.org/cargo/reference/profiles.html#panic)).
7575
/// Else panics can not be caught by `LibAFL`.
76-
pub struct GenericInProcessForkExecutor<'a, EM, H, HT, I, OT, S, SP, Z> {
77-
harness_fn: &'a mut H,
76+
pub struct GenericInProcessForkExecutor<EM, H, HT, I, OT, S, SP, Z> {
77+
harness_fn: H,
7878
inner: GenericInProcessForkExecutorInner<EM, HT, I, OT, S, SP, Z>,
7979
}
8080

81-
impl<H, HT, I, OT, S, SP, EM, Z> Debug
82-
for GenericInProcessForkExecutor<'_, EM, H, HT, I, OT, S, SP, Z>
81+
impl<H, HT, I, OT, S, SP, EM, Z> Debug for GenericInProcessForkExecutor<EM, H, HT, I, OT, S, SP, Z>
8382
where
8483
HT: Debug,
8584
OT: Debug,
@@ -103,7 +102,7 @@ where
103102
}
104103

105104
impl<EM, H, HT, I, OT, S, SP, Z> Executor<EM, I, S, Z>
106-
for GenericInProcessForkExecutor<'_, EM, H, HT, I, OT, S, SP, Z>
105+
for GenericInProcessForkExecutor<EM, H, HT, I, OT, S, SP, Z>
107106
where
108107
H: FnMut(&I) -> ExitKind + Sized,
109108
HT: ExecutorHooksTuple<I, S>,
@@ -141,7 +140,7 @@ where
141140
}
142141
}
143142

144-
impl<'a, H, HT, I, OT, S, SP, EM, Z> GenericInProcessForkExecutor<'a, EM, H, HT, I, OT, S, SP, Z>
143+
impl<H, HT, I, OT, S, SP, EM, Z> GenericInProcessForkExecutor<EM, H, HT, I, OT, S, SP, Z>
145144
where
146145
HT: ExecutorHooksTuple<I, S>,
147146
OT: ObserversTuple<I, S>,
@@ -150,15 +149,14 @@ where
150149
#[expect(clippy::too_many_arguments)]
151150
pub fn with_hooks(
152151
userhooks: HT,
153-
harness_fn: &'a mut H,
152+
harness_fn: H,
154153
observers: OT,
155154
fuzzer: &mut Z,
156155
state: &mut S,
157156
event_mgr: &mut EM,
158157
timeout: Duration,
159158
shmem_provider: SP,
160-
) -> Result<Self, Error>
161-
where {
159+
) -> Result<Self, Error> {
162160
Ok(Self {
163161
harness_fn,
164162
inner: GenericInProcessForkExecutorInner::with_hooks(
@@ -175,19 +173,21 @@ where {
175173

176174
/// Retrieve the harness function.
177175
#[inline]
176+
#[must_use]
178177
pub fn harness(&self) -> &H {
179-
self.harness_fn
178+
&self.harness_fn
180179
}
181180

182181
/// Retrieve the harness function for a mutable reference.
183182
#[inline]
183+
#[must_use]
184184
pub fn harness_mut(&mut self) -> &mut H {
185-
self.harness_fn
185+
&mut self.harness_fn
186186
}
187187
}
188188

189189
impl<H, HT, I, OT, S, SP, EM, Z> HasObservers
190-
for GenericInProcessForkExecutor<'_, EM, H, HT, I, OT, S, SP, Z>
190+
for GenericInProcessForkExecutor<EM, H, HT, I, OT, S, SP, Z>
191191
{
192192
type Observers = OT;
193193
#[inline]

crates/libafl/src/executors/inprocess_fork/stateful.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ use crate::{
2424
};
2525

2626
/// The `StatefulInProcessForkExecutor` with no user hooks
27-
pub type StatefulInProcessForkExecutor<'a, EM, ES, H, I, OT, S, SP, Z> =
28-
StatefulGenericInProcessForkExecutor<'a, EM, ES, H, (), I, OT, S, SP, Z>;
27+
pub type StatefulInProcessForkExecutor<EM, ES, H, I, OT, S, SP, Z> =
28+
StatefulGenericInProcessForkExecutor<EM, ES, H, (), I, OT, S, SP, Z>;
2929

30-
impl<'a, H, I, OT, S, SP, EM, ES, Z> StatefulInProcessForkExecutor<'a, EM, ES, H, I, OT, S, SP, Z>
30+
impl<H, I, OT, S, SP, EM, ES, Z> StatefulInProcessForkExecutor<EM, ES, H, I, OT, S, SP, Z>
3131
where
3232
OT: ObserversTuple<I, S>,
3333
SP: ShMemProvider,
3434
{
3535
#[expect(clippy::too_many_arguments)]
3636
/// The constructor for `InProcessForkExecutor`
3737
pub fn new(
38-
harness_fn: &'a mut H,
38+
harness_fn: H,
3939
exposed_executor_state: ES,
4040
observers: OT,
4141
fuzzer: &mut Z,
@@ -59,17 +59,17 @@ where
5959
}
6060

6161
/// [`StatefulGenericInProcessForkExecutor`] is an executor that forks the current process before each execution. Harness can access some internal state.
62-
pub struct StatefulGenericInProcessForkExecutor<'a, EM, ES, H, HT, I, OT, S, SP, Z> {
62+
pub struct StatefulGenericInProcessForkExecutor<EM, ES, H, HT, I, OT, S, SP, Z> {
6363
/// The harness function, being executed for each fuzzing loop execution
64-
harness_fn: &'a mut H,
64+
harness_fn: H,
6565
/// The state used as argument of the harness
6666
pub exposed_executor_state: ES,
6767
/// Inner state of the executor
6868
pub inner: GenericInProcessForkExecutorInner<EM, HT, I, OT, S, SP, Z>,
6969
}
7070

7171
impl<H, HT, I, OT, S, SP, EM, ES, Z> Debug
72-
for StatefulGenericInProcessForkExecutor<'_, EM, ES, H, HT, I, OT, S, SP, Z>
72+
for StatefulGenericInProcessForkExecutor<EM, ES, H, HT, I, OT, S, SP, Z>
7373
where
7474
HT: Debug,
7575
OT: Debug,
@@ -93,7 +93,7 @@ where
9393
}
9494

9595
impl<EM, H, HT, I, OT, S, SP, Z, ES> Executor<EM, I, S, Z>
96-
for StatefulGenericInProcessForkExecutor<'_, EM, ES, H, HT, I, OT, S, SP, Z>
96+
for StatefulGenericInProcessForkExecutor<EM, ES, H, HT, I, OT, S, SP, Z>
9797
where
9898
H: FnMut(&mut ES, &I) -> ExitKind + Sized,
9999
HT: ExecutorHooksTuple<I, S>,
@@ -134,8 +134,8 @@ where
134134
}
135135
}
136136

137-
impl<'a, H, HT, I, OT, S, SP, EM, ES, Z>
138-
StatefulGenericInProcessForkExecutor<'a, EM, ES, H, HT, I, OT, S, SP, Z>
137+
impl<H, HT, I, OT, S, SP, EM, ES, Z>
138+
StatefulGenericInProcessForkExecutor<EM, ES, H, HT, I, OT, S, SP, Z>
139139
where
140140
HT: ExecutorHooksTuple<I, S>,
141141
OT: ObserversTuple<I, S>,
@@ -144,7 +144,7 @@ where
144144
#[expect(clippy::too_many_arguments)]
145145
pub fn with_hooks(
146146
userhooks: HT,
147-
harness_fn: &'a mut H,
147+
harness_fn: H,
148148
exposed_executor_state: ES,
149149
observers: OT,
150150
fuzzer: &mut Z,
@@ -170,19 +170,21 @@ where
170170

171171
/// Retrieve the harness function.
172172
#[inline]
173+
#[must_use]
173174
pub fn harness(&self) -> &H {
174-
self.harness_fn
175+
&self.harness_fn
175176
}
176177

177178
/// Retrieve the harness function for a mutable reference.
178179
#[inline]
180+
#[must_use]
179181
pub fn harness_mut(&mut self) -> &mut H {
180-
self.harness_fn
182+
&mut self.harness_fn
181183
}
182184
}
183185

184186
impl<H, HT, I, OT, S, SP, EM, ES, Z> HasObservers
185-
for StatefulGenericInProcessForkExecutor<'_, EM, ES, H, HT, I, OT, S, SP, Z>
187+
for StatefulGenericInProcessForkExecutor<EM, ES, H, HT, I, OT, S, SP, Z>
186188
{
187189
type Observers = OT;
188190

0 commit comments

Comments
 (0)