From 359e94ec97c9c7719edb43d612c8106d599f48a2 Mon Sep 17 00:00:00 2001 From: "Dongjia \"toka\" Zhang" Date: Thu, 2 May 2024 14:43:25 +0200 Subject: [PATCH] ctrl c handler fix for non-fork mode (#2132) * poc * ai suggestion * rename this * aaaa * fmt * simplify * delete blob * ignore * fixup? * some progress on cow-ification * some more * clippy fixes, finalise tests * whoops, missed a spot * no std compat * api change: Named now requires alloc feature * doc fix * missed a spot * additional fixes * libfuzzer fixes * fix tutorial * fix * add * aa * fix tutorial * fix * Rename * fix * aa * fmt * aa * aa * another closure * clp * fix stuff * fix non fork * fmt --------- Co-authored-by: Addison Crump --- libafl/src/events/llmp.rs | 19 ++++++++++--------- libafl/src/events/simple.rs | 18 ++++++++++-------- libafl/src/events/tcp.rs | 18 ++++++++++-------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/libafl/src/events/llmp.rs b/libafl/src/events/llmp.rs index e604de0583..300355b939 100644 --- a/libafl/src/events/llmp.rs +++ b/libafl/src/events/llmp.rs @@ -1554,15 +1554,6 @@ where handle.status() } ForkResult::Child => { - // We setup signal handlers to clean up shmem segments used by state restorer - #[cfg(all(unix, not(miri)))] - if let Err(_e) = unsafe { - setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) - } { - // We can live without a proper ctrl+c signal handler. Print and ignore. - log::error!("Failed to setup signal handlers: {_e}"); - } - // println!("child {}", std::process::id()); self.shmem_provider.post_fork(true)?; break (staterestorer, self.shmem_provider.clone(), core_id); } @@ -1614,6 +1605,16 @@ where ctr = ctr.wrapping_add(1); } } else { + // At this point we are the fuzzer *NOT* the restarter. + // We setup signal handlers to clean up shmem segments used by state restorer + #[cfg(all(unix, not(miri)))] + if let Err(_e) = + unsafe { setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) } + { + // We can live without a proper ctrl+c signal handler. Print and ignore. + log::error!("Failed to setup signal handlers: {_e}"); + } + // We are the newly started fuzzing instance (i.e. on Windows), first, connect to our own restore map. // We get here *only on Windows*, if we were started by a restarting fuzzer. // A staterestorer and a receiver for single communication diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index ffcf1de112..c77878ea4f 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -493,14 +493,6 @@ where handle.status() } ForkResult::Child => { - // We setup signal handlers to clean up shmem segments used by state restorer - #[cfg(all(unix, not(miri)))] - if let Err(_e) = unsafe { - setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) - } { - // We can live without a proper ctrl+c signal handler. Print and ignore. - log::error!("Failed to setup signal handlers: {_e}"); - } shmem_provider.post_fork(true)?; break staterestorer; } @@ -548,6 +540,16 @@ where ctr = ctr.wrapping_add(1); } } else { + // At this point we are the fuzzer *NOT* the restarter. + // We setup signal handlers to clean up shmem segments used by state restorer + #[cfg(all(unix, not(miri)))] + if let Err(_e) = + unsafe { setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) } + { + // We can live without a proper ctrl+c signal handler. Print and ignore. + log::error!("Failed to setup signal handlers: {_e}"); + } + // We are the newly started fuzzing instance (i.e. on Windows), first, connect to our own restore map. // We get here *only on Windows*, if we were started by a restarting fuzzer. // A staterestorer and a receiver for single communication diff --git a/libafl/src/events/tcp.rs b/libafl/src/events/tcp.rs index 0b33cfd12b..7c7e723128 100644 --- a/libafl/src/events/tcp.rs +++ b/libafl/src/events/tcp.rs @@ -1263,14 +1263,6 @@ where handle.status() } ForkResult::Child => { - // We setup signal handlers to clean up shmem segments used by state restorer - #[cfg(all(unix, not(miri)))] - if let Err(_e) = unsafe { - setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) - } { - // We can live without a proper ctrl+c signal handler. Print and ignore. - log::error!("Failed to setup signal handlers: {_e}"); - } self.shmem_provider.post_fork(true)?; break (staterestorer, self.shmem_provider.clone(), core_id); } @@ -1318,6 +1310,16 @@ where ctr = ctr.wrapping_add(1); } } else { + // At this point we are the fuzzer *NOT* the restarter. + // We setup signal handlers to clean up shmem segments used by state restorer + #[cfg(all(unix, not(miri)))] + if let Err(_e) = + unsafe { setup_signal_handler(addr_of_mut!(EVENTMGR_SIGHANDLER_STATE)) } + { + // We can live without a proper ctrl+c signal handler. Print and ignore. + log::error!("Failed to setup signal handlers: {_e}"); + } + // We are the newly started fuzzing instance (i.e. on Windows), first, connect to our own restore map. // We get here *only on Windows*, if we were started by a restarting fuzzer. // A staterestorer and a receiver for single communication