move timeouts to observer

This commit is contained in:
toka 2021-03-14 07:55:14 +09:00
parent cad5e339d9
commit ae9486814e
2 changed files with 71 additions and 4 deletions

View File

@ -72,13 +72,9 @@ where
fn run_target(&mut self, input: &I) -> Result<ExitKind, Error> { fn run_target(&mut self, input: &I) -> Result<ExitKind, Error> {
let bytes = input.target_bytes(); let bytes = input.target_bytes();
#[cfg(unix)]
unsafe { libc::alarm(1) };
let ret = (self.harness_fn)(self, bytes.as_slice()); let ret = (self.harness_fn)(self, bytes.as_slice());
#[cfg(unix)]
unsafe { libc::alarm(0) };
#[cfg(unix)] #[cfg(unix)]
unsafe { unsafe {

View File

@ -7,6 +7,8 @@ use alloc::{
}; };
use core::time::Duration; use core::time::Duration;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::os::raw::c_int;
use std::ptr::null_mut;
use crate::{ use crate::{
bolts::tuples::{MatchFirstType, MatchNameAndType, MatchType, Named, TupleList}, bolts::tuples::{MatchFirstType, MatchNameAndType, MatchType, Named, TupleList},
@ -94,8 +96,27 @@ pub struct TimeObserver {
name: String, name: String,
start_time: Duration, start_time: Duration,
last_runtime: Option<Duration>, last_runtime: Option<Duration>,
exec_tmout: Option<Duration>,
} }
#[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 { impl TimeObserver {
/// Creates a new TimeObserver with the given name. /// Creates a new TimeObserver with the given name.
pub fn new(name: &'static str) -> Self { pub fn new(name: &'static str) -> Self {
@ -103,6 +124,7 @@ impl TimeObserver {
name: name.to_string(), name: name.to_string(),
start_time: Duration::from_secs(0), start_time: Duration::from_secs(0),
last_runtime: None, last_runtime: None,
exec_tmout: None,
} }
} }
@ -113,6 +135,31 @@ impl TimeObserver {
impl Observer for TimeObserver { impl Observer for TimeObserver {
fn pre_exec(&mut self) -> Result<(), Error> { 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.last_runtime = None;
self.start_time = current_time(); self.start_time = current_time();
Ok(()) Ok(())
@ -120,6 +167,30 @@ impl Observer for TimeObserver {
fn post_exec(&mut self) -> Result<(), Error> { fn post_exec(&mut self) -> Result<(), Error> {
self.last_runtime = Some(current_time() - self.start_time); 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(()) Ok(())
} }
} }