Skip to content

Commit 89342b2

Browse files
authored
Revert #2935 (#3031)
* revert * null check * no std
1 parent 86d6fa0 commit 89342b2

File tree

3 files changed

+84
-50
lines changed

3 files changed

+84
-50
lines changed

libafl/src/executors/hooks/inprocess.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::sync::atomic::{Ordering, compiler_fence};
66
use core::{
77
ffi::c_void,
88
marker::PhantomData,
9-
ptr::{self, null_mut},
9+
ptr::{null, null_mut},
1010
time::Duration,
1111
};
1212

@@ -43,6 +43,12 @@ use crate::{inputs::Input, observers::ObserversTuple, state::HasCurrentTestcase}
4343
/// The inmem executor's handlers.
4444
#[expect(missing_debug_implementations)]
4545
pub struct InProcessHooks<I, S> {
46+
/// On crash C function pointer
47+
#[cfg(feature = "std")]
48+
pub crash_handler: *const c_void,
49+
/// On timeout C function pointer
50+
#[cfg(feature = "std")]
51+
pub timeout_handler: *const c_void,
4652
/// `TImer` struct
4753
#[cfg(feature = "std")]
4854
pub timer: TimerStruct,
@@ -190,6 +196,19 @@ impl<I, S> ExecutorHook<I, S> for InProcessHooks<I, S> {
190196
fn init(&mut self, _state: &mut S) {}
191197
/// Call before running a target.
192198
fn pre_exec(&mut self, _state: &mut S, _input: &I) {
199+
#[cfg(feature = "std")]
200+
// This is very important!!!!!
201+
// Don't remove these pointer settings.
202+
// Imagine there are two executors, you have to set the correct crash handlers for each of the executor.
203+
unsafe {
204+
let data = &raw mut GLOBAL_STATE;
205+
assert!((*data).crash_handler == null());
206+
// usually timeout handler and crash handler is set together
207+
// so no check for timeout handler is null or not
208+
(*data).crash_handler = self.crash_handler;
209+
(*data).timeout_handler = self.timeout_handler;
210+
}
211+
193212
#[cfg(all(feature = "std", not(all(miri, target_vendor = "apple"))))]
194213
self.timer_mut().set_timer();
195214
}
@@ -200,6 +219,12 @@ impl<I, S> ExecutorHook<I, S> for InProcessHooks<I, S> {
200219
// We're calling this only once per execution, in a single thread.
201220
#[cfg(all(feature = "std", not(all(miri, target_vendor = "apple"))))]
202221
self.timer_mut().unset_timer();
222+
#[cfg(feature = "std")]
223+
unsafe {
224+
let data = &raw mut GLOBAL_STATE;
225+
(*data).crash_handler = null();
226+
(*data).timeout_handler = null();
227+
}
203228
}
204229
}
205230

@@ -234,18 +259,15 @@ impl<I, S> InProcessHooks<I, S> {
234259
setup_signal_handler(data)?;
235260
}
236261

237-
#[cfg(feature = "std")]
238-
unsafe {
239-
let data = &raw mut GLOBAL_STATE;
240-
(*data).crash_handler =
241-
unix_signal_handler::inproc_crash_handler::<E, EM, I, OF, S, Z> as *const c_void;
242-
(*data).timeout_handler =
243-
unix_signal_handler::inproc_timeout_handler::<E, EM, I, OF, S, Z> as *const _;
244-
}
245-
246262
compiler_fence(Ordering::SeqCst);
247263
Ok(Self {
248264
#[cfg(feature = "std")]
265+
crash_handler: unix_signal_handler::inproc_crash_handler::<E, EM, I, OF, S, Z>
266+
as *const c_void,
267+
#[cfg(feature = "std")]
268+
timeout_handler: unix_signal_handler::inproc_timeout_handler::<E, EM, I, OF, S, Z>
269+
as *const _,
270+
#[cfg(feature = "std")]
249271
timer: TimerStruct::new(exec_tmout),
250272
phantom: PhantomData,
251273
})
@@ -278,7 +300,7 @@ impl<I, S> InProcessHooks<I, S> {
278300
>();
279301
setup_exception_handler(data)?;
280302
compiler_fence(Ordering::SeqCst);
281-
(*data).crash_handler =
303+
let crash_handler =
282304
crate::executors::hooks::windows::windows_exception_handler::inproc_crash_handler::<
283305
E,
284306
EM,
@@ -296,9 +318,10 @@ impl<I, S> InProcessHooks<I, S> {
296318
S,
297319
Z,
298320
> as *const c_void;
299-
(*data).timeout_handler = timeout_handler;
300321
let timer = TimerStruct::new(exec_tmout, timeout_handler);
301322
ret = Ok(Self {
323+
crash_handler,
324+
timeout_handler,
302325
timer,
303326
phantom: PhantomData,
304327
});
@@ -336,6 +359,10 @@ impl<I, S> InProcessHooks<I, S> {
336359
#[cfg(not(windows))]
337360
pub fn nop() -> Self {
338361
Self {
362+
#[cfg(feature = "std")]
363+
crash_handler: null(),
364+
#[cfg(feature = "std")]
365+
timeout_handler: null(),
339366
#[cfg(feature = "std")]
340367
timer: TimerStruct::new(Duration::from_millis(5000)),
341368
phantom: PhantomData,
@@ -414,7 +441,7 @@ impl InProcessExecutorHandlerData {
414441
#[cfg(all(feature = "std", any(unix, windows)))]
415442
pub(crate) unsafe fn take_current_input<'a, I>(&mut self) -> &'a I {
416443
let r = unsafe { (self.current_input_ptr as *const I).as_ref().unwrap() };
417-
self.current_input_ptr = ptr::null();
444+
self.current_input_ptr = null();
418445
r
419446
}
420447

@@ -509,19 +536,19 @@ pub static mut GLOBAL_STATE: InProcessExecutorHandlerData = InProcessExecutorHan
509536
// The fuzzer ptr for signal handling
510537
fuzzer_ptr: null_mut(),
511538
// The executor ptr for signal handling
512-
executor_ptr: ptr::null(),
539+
executor_ptr: null(),
513540
// The current input for signal handling
514-
current_input_ptr: ptr::null(),
541+
current_input_ptr: null(),
515542

516543
#[cfg(feature = "std")]
517544
signal_handler_depth: 0,
518545

519546
// The crash handler fn
520547
#[cfg(feature = "std")]
521-
crash_handler: ptr::null(),
548+
crash_handler: null(),
522549
// The timeout handler fn
523550
#[cfg(feature = "std")]
524-
timeout_handler: ptr::null(),
551+
timeout_handler: null(),
525552
#[cfg(all(windows, feature = "std"))]
526553
ptp_timer: None,
527554
#[cfg(all(windows, feature = "std"))]

libafl/src/executors/hooks/inprocess_fork.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use crate::{
2626
/// The inmem fork executor's hooks.
2727
#[derive(Debug)]
2828
pub struct InChildProcessHooks<I, S> {
29+
/// On crash C function pointer
30+
pub crash_handler: *const c_void,
31+
/// On timeout C function pointer
32+
pub timeout_handler: *const c_void,
2933
phantom: PhantomData<(I, S)>,
3034
}
3135

@@ -34,9 +38,22 @@ impl<I, S> ExecutorHook<I, S> for InChildProcessHooks<I, S> {
3438
fn init(&mut self, _state: &mut S) {}
3539

3640
/// Call before running a target.
37-
fn pre_exec(&mut self, _state: &mut S, _input: &I) {}
41+
fn pre_exec(&mut self, _state: &mut S, _input: &I) {
42+
unsafe {
43+
let data = &raw mut FORK_EXECUTOR_GLOBAL_DATA;
44+
(*data).crash_handler = self.crash_handler;
45+
(*data).timeout_handler = self.timeout_handler;
46+
compiler_fence(Ordering::SeqCst);
47+
}
48+
}
3849

39-
fn post_exec(&mut self, _state: &mut S, _input: &I) {}
50+
fn post_exec(&mut self, _state: &mut S, _input: &I) {
51+
unsafe {
52+
let data = &raw mut FORK_EXECUTOR_GLOBAL_DATA;
53+
(*data).crash_handler = null();
54+
(*data).timeout_handler = null();
55+
}
56+
}
4057
}
4158

4259
impl<I, S> InChildProcessHooks<I, S> {
@@ -53,11 +70,11 @@ impl<I, S> InChildProcessHooks<I, S> {
5370
#[cfg(not(miri))]
5471
setup_signal_handler(data)?;
5572
compiler_fence(Ordering::SeqCst);
56-
(*data).crash_handler =
57-
child_signal_handlers::child_crash_handler::<E, I, S> as *const c_void;
58-
(*data).timeout_handler =
59-
child_signal_handlers::child_timeout_handler::<E, I, S> as *const c_void;
6073
Ok(Self {
74+
crash_handler: child_signal_handlers::child_crash_handler::<E, I, S>
75+
as *const c_void,
76+
timeout_handler: child_signal_handlers::child_timeout_handler::<E, I, S>
77+
as *const c_void,
6178
phantom: PhantomData,
6279
})
6380
}
@@ -67,6 +84,8 @@ impl<I, S> InChildProcessHooks<I, S> {
6784
#[must_use]
6885
pub fn nop() -> Self {
6986
Self {
87+
crash_handler: null(),
88+
timeout_handler: null(),
7089
phantom: PhantomData,
7190
}
7291
}

libafl_qemu/src/executor.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use libafl::{
1616
events::{EventFirer, EventRestarter},
1717
executors::{
1818
Executor, ExitKind, HasObservers,
19-
hooks::inprocess::{GLOBAL_STATE, InProcessExecutorHandlerData},
19+
hooks::inprocess::InProcessExecutorHandlerData,
2020
inprocess::{HasInProcessHooks, stateful::StatefulInProcessExecutor},
2121
inprocess_fork::stateful::StatefulInProcessForkExecutor,
2222
},
@@ -251,39 +251,27 @@ where
251251
OF: Feedback<EM, I, OT, S>,
252252
Z: HasObjective<Objective = OF> + HasScheduler<I, S> + ExecutionProcessor<EM, I, OT, S>,
253253
{
254-
let inner = StatefulInProcessExecutor::with_timeout(
254+
let mut inner = StatefulInProcessExecutor::with_timeout(
255255
harness_fn, emulator, observers, fuzzer, state, event_mgr, timeout,
256256
)?;
257257

258-
let data = &raw mut GLOBAL_STATE;
258+
// rewrite the crash handler pointer
259259
#[cfg(feature = "usermode")]
260-
unsafe {
261-
// rewrite the crash handler pointer
262-
(*data).crash_handler =
260+
{
261+
inner.inprocess_hooks_mut().crash_handler =
263262
inproc_qemu_crash_handler::<Self, EM, ET, I, OF, S, Z> as *const c_void;
264263
}
265264

266-
unsafe {
267-
// rewrite the timeout handler pointer
268-
(*data).timeout_handler = inproc_qemu_timeout_handler::<
269-
StatefulInProcessExecutor<
270-
'a,
271-
EM,
272-
Emulator<C, CM, ED, ET, I, S, SM>,
273-
H,
274-
I,
275-
OT,
276-
S,
277-
Z,
278-
>,
279-
EM,
280-
ET,
281-
I,
282-
OF,
283-
S,
284-
Z,
285-
> as *const c_void;
286-
}
265+
// rewrite the timeout handler pointer
266+
inner.inprocess_hooks_mut().timeout_handler = inproc_qemu_timeout_handler::<
267+
StatefulInProcessExecutor<'a, EM, Emulator<C, CM, ED, ET, I, S, SM>, H, I, OT, S, Z>,
268+
EM,
269+
ET,
270+
I,
271+
OF,
272+
S,
273+
Z,
274+
> as *const c_void;
287275

288276
Ok(Self {
289277
inner,

0 commit comments

Comments
 (0)