executor fix

This commit is contained in:
Andrea Fioraldi 2021-02-21 14:34:51 +01:00
parent a3b30a2869
commit 93947c44cf

View File

@ -14,7 +14,7 @@ use crate::{
feedbacks::FeedbacksTuple, feedbacks::FeedbacksTuple,
inputs::{HasTargetBytes, Input}, inputs::{HasTargetBytes, Input},
observers::ObserversTuple, observers::ObserversTuple,
state::State, state::{HasObjectives, HasSolutions},
utils::Rand, utils::Rand,
Error, Error,
}; };
@ -54,35 +54,20 @@ where
OT: ObserversTuple, OT: ObserversTuple,
{ {
#[inline] #[inline]
fn pre_exec<EM, S>( fn pre_exec<EM, S>(&mut self, state: &mut S, event_mgr: &mut EM, input: &I) -> Result<(), Error>
&mut self,
state: &mut S,
event_mgr: &mut EM,
input: &I,
) -> Result<(), Error>
where where
EM: EventManager<I>, EM: EventManager<I>,
{ {
#[cfg(unix)] #[cfg(unix)]
#[cfg(feature = "std")] #[cfg(feature = "std")]
unsafe { unsafe {
set_oncrash_ptrs( set_oncrash_ptrs(state, event_mgr, self.observers(), input);
state,
event_mgr,
self.observers(),
input,
);
} }
Ok(()) Ok(())
} }
#[inline] #[inline]
fn post_exec<EM, S>( fn post_exec<EM, S>(&mut self, _state: &S, _event_mgr: &mut EM, _input: &I) -> Result<(), Error>
&mut self,
_state: &S,
_event_mgr: &mut EM,
_input: &I,
) -> Result<(), Error>
where where
EM: EventManager<I>, EM: EventManager<I>,
{ {
@ -139,25 +124,24 @@ where
/// * `name` - the name of this executor (to address it along the way) /// * `name` - the name of this executor (to address it along the way)
/// * `harness_fn` - the harness, executiong the function /// * `harness_fn` - the harness, executiong the function
/// * `observers` - the observers observing the target during execution /// * `observers` - the observers observing the target during execution
pub fn new<C, EM, FT, OC, OFT, R>( pub fn new<EM, OC, OFT, R, S>(
name: &'static str, name: &'static str,
harness_fn: HarnessFunction<Self>, harness_fn: HarnessFunction<Self>,
observers: OT, observers: OT,
_state: &mut State<C, FT, I, OC, OFT, R>, _state: &mut S,
_event_mgr: &mut EM, _event_mgr: &mut EM,
) -> Self ) -> Self
where where
R: Rand, EM: EventManager<I>,
FT: FeedbacksTuple<I>,
OC: Corpus<I>, OC: Corpus<I>,
OFT: FeedbacksTuple<I>, OFT: FeedbacksTuple<I>,
C: Corpus<I>, S: HasObjectives<OFT, I> + HasSolutions<OC, I>,
EM: EventManager<I>, R: Rand,
{ {
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[cfg(unix)] #[cfg(unix)]
unsafe { unsafe {
setup_crash_handlers::<C, EM, FT, I, OC, OFT, OT, R>(); setup_crash_handlers::<EM, I, OC, OFT, OT, R, S>();
} }
Self { Self {
@ -188,13 +172,13 @@ pub mod unix_signals {
}; };
use crate::{ use crate::{
corpus::Corpus, corpus::{Corpus, Testcase},
events::{Event, EventManager}, events::{Event, EventManager},
executors::ExitKind, executors::ExitKind,
feedbacks::FeedbacksTuple, feedbacks::FeedbacksTuple,
inputs::Input, inputs::Input,
observers::ObserversTuple, observers::ObserversTuple,
state::State, state::{HasObjectives, HasSolutions},
utils::Rand, utils::Rand,
}; };
/// Let's get 8 mb for now. /// Let's get 8 mb for now.
@ -211,17 +195,16 @@ pub mod unix_signals {
/// This is needed for certain non-rust side effects, as well as unix signal handling. /// This is needed for certain non-rust side effects, as well as unix signal handling.
static mut CURRENT_INPUT_PTR: *const c_void = ptr::null(); static mut CURRENT_INPUT_PTR: *const c_void = ptr::null();
unsafe fn inmem_handle_crash<C, EM, FT, I, OC, OFT, OT, R>( unsafe fn inmem_handle_crash<EM, I, OC, OFT, OT, R, S>(
_sig: c_int, _sig: c_int,
info: siginfo_t, info: siginfo_t,
_void: c_void, _void: c_void,
) where ) where
EM: EventManager<I>, EM: EventManager<I>,
C: Corpus<I>,
OT: ObserversTuple, OT: ObserversTuple,
OC: Corpus<I>, OC: Corpus<I>,
OFT: FeedbacksTuple<I>, OFT: FeedbacksTuple<I>,
FT: FeedbacksTuple<I>, S: HasObjectives<OFT, I> + HasSolutions<OC, I>,
I: Input, I: Input,
R: Rand, R: Rand,
{ {
@ -236,7 +219,6 @@ pub mod unix_signals {
Ok(maps) => println!("maps:\n{}", maps), Ok(maps) => println!("maps:\n{}", maps),
Err(e) => println!("Couldn't load mappings: {:?}", e), Err(e) => println!("Couldn't load mappings: {:?}", e),
}; };
#[cfg(feature = "std")] #[cfg(feature = "std")]
{ {
println!("Type QUIT to restart the child"); println!("Type QUIT to restart the child");
@ -258,30 +240,23 @@ pub mod unix_signals {
let input = (CURRENT_INPUT_PTR as *const I).as_ref().unwrap(); let input = (CURRENT_INPUT_PTR as *const I).as_ref().unwrap();
// Make sure we don't crash in the crash handler forever. // Make sure we don't crash in the crash handler forever.
CURRENT_INPUT_PTR = ptr::null(); CURRENT_INPUT_PTR = ptr::null();
let state = (STATE_PTR as *mut State<C, FT, I, OC, OFT, R>) let state = (STATE_PTR as *mut S).as_mut().unwrap();
.as_mut()
.unwrap();
let mgr = (EVENT_MGR_PTR as *mut EM).as_mut().unwrap(); let mgr = (EVENT_MGR_PTR as *mut EM).as_mut().unwrap();
let observers = (OBSERVERS_PTR as *const OT).as_ref().unwrap(); let observers = (OBSERVERS_PTR as *const OT).as_ref().unwrap();
let obj_fitness = state let obj_fitness = state
.objective_feedbacks_mut() .objectives_mut()
.is_interesting_all(&input, observers, ExitKind::Crash) .is_interesting_all(&input, observers, ExitKind::Crash)
.expect("In crash handler objective feedbacks failure.".into()); .expect("In crash handler objectives failure.".into());
if obj_fitness > 0 { if obj_fitness > 0 {
if !state state.solutions_mut().add(Testcase::new(*input));
.add_if_objective(input.clone(), obj_fitness) mgr.fire(
.expect("In crash handler objective corpus add failure.".into()) state,
.is_none() Event::Objective {
{ objective_size: state.solutions().count(),
mgr.fire( },
state, )
Event::Objective { .expect("Could not send crashing input".into());
objective_size: state.objective_corpus().count(),
},
)
.expect(&format!("Could not send timeouting input {:?}", input));
}
} }
mgr.on_restart(state).unwrap(); mgr.on_restart(state).unwrap();
@ -293,17 +268,16 @@ pub mod unix_signals {
std::process::exit(1); std::process::exit(1);
} }
unsafe fn inmem_handle_timeout<C, EM, FT, I, OC, OFT, OT, R>( unsafe fn inmem_handle_timeout<EM, I, OC, OFT, OT, R, S>(
_sig: c_int, _sig: c_int,
_info: siginfo_t, info: siginfo_t,
_void: c_void, _void: c_void,
) where ) where
EM: EventManager<I>, EM: EventManager<I>,
C: Corpus<I>, OT: ObserversTuple,
OC: Corpus<I>, OC: Corpus<I>,
OFT: FeedbacksTuple<I>, OFT: FeedbacksTuple<I>,
OT: ObserversTuple, S: HasObjectives<OFT, I> + HasSolutions<OC, I>,
FT: FeedbacksTuple<I>,
I: Input, I: Input,
R: Rand, R: Rand,
{ {
@ -319,34 +293,31 @@ pub mod unix_signals {
let input = (CURRENT_INPUT_PTR as *const I).as_ref().unwrap(); let input = (CURRENT_INPUT_PTR as *const I).as_ref().unwrap();
// Make sure we don't crash in the crash handler forever. // Make sure we don't crash in the crash handler forever.
CURRENT_INPUT_PTR = ptr::null(); CURRENT_INPUT_PTR = ptr::null();
let state = (STATE_PTR as *mut State<C, FT, I, OC, OFT, R>) let state = (STATE_PTR as *mut S).as_mut().unwrap();
.as_mut()
.unwrap();
let mgr = (EVENT_MGR_PTR as *mut EM).as_mut().unwrap(); let mgr = (EVENT_MGR_PTR as *mut EM).as_mut().unwrap();
let observers = (OBSERVERS_PTR as *const OT).as_ref().unwrap(); let observers = (OBSERVERS_PTR as *const OT).as_ref().unwrap();
let obj_fitness = state let obj_fitness = state
.objective_feedbacks_mut() .objectives_mut()
.is_interesting_all(&input, observers, ExitKind::Timeout) .is_interesting_all(&input, observers, ExitKind::Crash)
.expect("In timeout handler objective feedbacks failure.".into()); .expect("In timeout handler objectives failure.".into());
if obj_fitness > 0 { if obj_fitness > 0 {
if !state state.solutions_mut().add(Testcase::new(*input));
.add_if_objective(input.clone(), obj_fitness) mgr.fire(
.expect("In timeout handler objective corpus add failure.".into()) state,
.is_none() Event::Objective {
{ objective_size: state.solutions().count(),
mgr.fire( },
state, )
Event::Objective { .expect("Could not send timeouting input".into());
objective_size: state.objective_corpus().count(),
},
)
.expect(&format!("Could not send timeouting input {:?}", input));
}
} }
mgr.on_restart(state).unwrap(); mgr.on_restart(state).unwrap();
println!("Waiting for broker...");
mgr.await_restart_safe();
println!("Bye!");
mgr.await_restart_safe(); mgr.await_restart_safe();
std::process::exit(1); std::process::exit(1);
@ -373,14 +344,13 @@ pub mod unix_signals {
OBSERVERS_PTR = ptr::null(); OBSERVERS_PTR = ptr::null();
} }
pub unsafe fn setup_crash_handlers<C, EM, FT, I, OC, OFT, OT, R>() pub unsafe fn setup_crash_handlers<EM, I, OC, OFT, OT, R, S>()
where where
EM: EventManager<I>, EM: EventManager<I>,
C: Corpus<I>, OT: ObserversTuple,
OC: Corpus<I>, OC: Corpus<I>,
OFT: FeedbacksTuple<I>, OFT: FeedbacksTuple<I>,
OT: ObserversTuple, S: HasObjectives<OFT, I> + HasSolutions<OC, I>,
FT: FeedbacksTuple<I>,
I: Input, I: Input,
R: Rand, R: Rand,
{ {
@ -399,8 +369,7 @@ pub mod unix_signals {
let mut sa: sigaction = mem::zeroed(); let mut sa: sigaction = mem::zeroed();
libc::sigemptyset(&mut sa.sa_mask as *mut libc::sigset_t); libc::sigemptyset(&mut sa.sa_mask as *mut libc::sigset_t);
sa.sa_flags = SA_NODEFER | SA_SIGINFO | SA_ONSTACK; sa.sa_flags = SA_NODEFER | SA_SIGINFO | SA_ONSTACK;
sa.sa_sigaction = sa.sa_sigaction = inmem_handle_crash::<EM, I, OC, OFT, OT, R, S> as usize;
inmem_handle_crash::<C, EM, FT, I, OC, OFT, OT, R> as usize;
for (sig, msg) in &[ for (sig, msg) in &[
(SIGSEGV, "segfault"), (SIGSEGV, "segfault"),
(SIGBUS, "sigbus"), (SIGBUS, "sigbus"),
@ -414,8 +383,7 @@ pub mod unix_signals {
} }
} }
sa.sa_sigaction = sa.sa_sigaction = inmem_handle_timeout::<EM, I, OC, OFT, OT, R, S> as usize;
inmem_handle_timeout::<C, EM, FT, I, OC, OFT, OT, R> as usize;
if sigaction(SIGUSR2, &mut sa as *mut sigaction, ptr::null_mut()) < 0 { if sigaction(SIGUSR2, &mut sa as *mut sigaction, ptr::null_mut()) < 0 {
panic!("Could not set up sigusr2 handler for timeouts"); panic!("Could not set up sigusr2 handler for timeouts");
} }