From ae9486814e0cd94d6a204d15354f022eea568090 Mon Sep 17 00:00:00 2001 From: toka Date: Sun, 14 Mar 2021 07:55:14 +0900 Subject: [PATCH] move timeouts to observer --- libafl/src/executors/inprocess.rs | 4 -- libafl/src/observers/mod.rs | 71 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/libafl/src/executors/inprocess.rs b/libafl/src/executors/inprocess.rs index cc9c0460b3..638eab008e 100644 --- a/libafl/src/executors/inprocess.rs +++ b/libafl/src/executors/inprocess.rs @@ -72,13 +72,9 @@ where fn run_target(&mut self, input: &I) -> Result { let bytes = input.target_bytes(); - #[cfg(unix)] - unsafe { libc::alarm(1) }; let ret = (self.harness_fn)(self, bytes.as_slice()); - #[cfg(unix)] - unsafe { libc::alarm(0) }; #[cfg(unix)] unsafe { diff --git a/libafl/src/observers/mod.rs b/libafl/src/observers/mod.rs index 7404ceeeb9..df1d076127 100644 --- a/libafl/src/observers/mod.rs +++ b/libafl/src/observers/mod.rs @@ -7,6 +7,8 @@ use alloc::{ }; use core::time::Duration; use serde::{Deserialize, Serialize}; +use std::os::raw::c_int; +use std::ptr::null_mut; use crate::{ bolts::tuples::{MatchFirstType, MatchNameAndType, MatchType, Named, TupleList}, @@ -94,8 +96,27 @@ pub struct TimeObserver { name: String, start_time: Duration, last_runtime: Option, + exec_tmout: Option, } +#[repr(C)] +struct Timeval { + pub tv_sec: i64, + pub tv_usec: i64, +} + +#[repr(C)] +struct Itimerval { + pub it_interval: Timeval, + pub it_value: Timeval, +} + +extern "C" { + fn setitimer(which: c_int, new_value: *mut Itimerval, old_value: *mut Itimerval) -> c_int; +} + +const ITIMER_REAL: c_int = 0; + impl TimeObserver { /// Creates a new TimeObserver with the given name. pub fn new(name: &'static str) -> Self { @@ -103,6 +124,7 @@ impl TimeObserver { name: name.to_string(), start_time: Duration::from_secs(0), last_runtime: None, + exec_tmout: None, } } @@ -113,6 +135,31 @@ impl TimeObserver { impl Observer for TimeObserver { fn pre_exec(&mut self) -> Result<(), Error> { + + #[cfg(unix)] + match self.exec_tmout { + Some(exec_tmout) => unsafe { + let milli_sec = exec_tmout.as_millis() as i64; + let it_value = Timeval{ + tv_sec: milli_sec / 1000, + tv_usec: milli_sec % 1000, + }; + let it_interval = Timeval{ + tv_sec: 0, + tv_usec: 0, + }; + setitimer( + ITIMER_REAL, + &mut Itimerval{ + it_interval, + it_value, + }, + null_mut(), + ); + }, + None => (), + } + self.last_runtime = None; self.start_time = current_time(); Ok(()) @@ -120,6 +167,30 @@ impl Observer for TimeObserver { fn post_exec(&mut self) -> Result<(), Error> { self.last_runtime = Some(current_time() - self.start_time); + + #[cfg(unix)] + match self.exec_tmout { + Some(_) => unsafe { + let it_value = Timeval{ + tv_sec: 0, + tv_usec: 0, + }; + let it_interval = Timeval{ + tv_sec: 0, + tv_usec: 0, + }; + setitimer( + ITIMER_REAL, + &mut Itimerval{ + it_interval, + it_value, + }, + null_mut(), + ); + }, + None => (), + } + Ok(()) } }