From 4608b94bd89d444cbb02ed41fca2ca002cc0c5ca Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 19 Mar 2020 23:00:02 +0100 Subject: [PATCH] implement CLOCK_MONOTONIC on Linux --- src/machine.rs | 5 +++++ src/shims/time.rs | 24 ++++++++++++------------ tests/run-pass/clock.rs | 14 -------------- tests/run-pass/time.rs | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 26 deletions(-) delete mode 100644 tests/run-pass/clock.rs create mode 100644 tests/run-pass/time.rs diff --git a/src/machine.rs b/src/machine.rs index 3a8e7fc90241..3cf00781338c 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -5,6 +5,7 @@ use std::cell::RefCell; use std::num::NonZeroU64; use std::rc::Rc; +use std::time::Instant; use rand::rngs::StdRng; @@ -164,6 +165,9 @@ pub struct Evaluator<'tcx> { /// the call to `miri_start_panic` (the panic payload) when unwinding. /// This is pointer-sized, and matches the `Payload` type in `src/libpanic_unwind/miri.rs`. pub(crate) panic_payload: Option>, + + /// The "time anchor" for this machine's monotone clock (for `Instant` simulation). + pub(crate) time_anchor: Instant, } impl<'tcx> Evaluator<'tcx> { @@ -182,6 +186,7 @@ pub(crate) fn new(communicate: bool, validate: bool) -> Self { file_handler: Default::default(), dir_handler: Default::default(), panic_payload: None, + time_anchor: Instant::now(), } } } diff --git a/src/shims/time.rs b/src/shims/time.rs index 627478eaaab7..c8807dd6ea84 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -1,14 +1,9 @@ -use std::time::{Duration, SystemTime}; +use std::time::{Duration, SystemTime, Instant}; use crate::stacked_borrows::Tag; use crate::*; use helpers::immty_from_int_checked; -// Returns the time elapsed between now and the unix epoch as a `Duration`. -fn get_time<'tcx>() -> InterpResult<'tcx, Duration> { - system_time_to_duration(&SystemTime::now()) -} - /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`. pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> { time.duration_since(SystemTime::UNIX_EPOCH) @@ -28,15 +23,20 @@ fn clock_gettime( this.check_no_isolation("clock_gettime")?; let clk_id = this.read_scalar(clk_id_op)?.to_i32()?; - if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? { + let tp = this.deref_operand(tp_op)?; + + let duration = if clk_id == this.eval_libc_i32("CLOCK_REALTIME")? { + system_time_to_duration(&SystemTime::now())? + } else if clk_id == this.eval_libc_i32("CLOCK_MONOTONIC")? { + // Absolute time does not matter, only relative time does, so we can just + // use our own time anchor here. + Instant::now().duration_since(this.machine.time_anchor) + } else { let einval = this.eval_libc("EINVAL")?; this.set_last_error(einval)?; return Ok(-1); - } + }; - let tp = this.deref_operand(tp_op)?; - - let duration = get_time()?; let tv_sec = duration.as_secs(); let tv_nsec = duration.subsec_nanos(); @@ -68,7 +68,7 @@ fn gettimeofday( let tv = this.deref_operand(tv_op)?; - let duration = get_time()?; + let duration = system_time_to_duration(&SystemTime::now())?; let tv_sec = duration.as_secs(); let tv_usec = duration.subsec_micros(); diff --git a/tests/run-pass/clock.rs b/tests/run-pass/clock.rs deleted file mode 100644 index b4c3fa08fdc6..000000000000 --- a/tests/run-pass/clock.rs +++ /dev/null @@ -1,14 +0,0 @@ -// ignore-windows: TODO clock shims are not implemented on Windows -// compile-flags: -Zmiri-disable-isolation - -use std::time::SystemTime; - -fn main() { - let now1 = SystemTime::now(); - - // Do some work to make time pass. - for _ in 0..10 { drop(vec![42]); } - - let now2 = SystemTime::now(); - assert!(now2 > now1); -} diff --git a/tests/run-pass/time.rs b/tests/run-pass/time.rs new file mode 100644 index 000000000000..bbe8b4011dfa --- /dev/null +++ b/tests/run-pass/time.rs @@ -0,0 +1,18 @@ +// ignore-windows: TODO clock shims are not implemented on Windows +// compile-flags: -Zmiri-disable-isolation + +use std::time::{SystemTime, Instant}; + +fn main() { + let now1 = SystemTime::now(); + // Do some work to make time pass. + for _ in 0..10 { drop(vec![42]); } + let now2 = SystemTime::now(); + assert!(now2 > now1); + + let now1 = Instant::now(); + // Do some work to make time pass. + for _ in 0..10 { drop(vec![42]); } + let now2 = Instant::now(); + assert!(now2 > now1); +}