1 use std::time::{Duration, SystemTime, Instant};
2 use std::convert::TryFrom;
4 use crate::stacked_borrows::Tag;
6 use helpers::immty_from_int_checked;
8 /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
9 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
10 time.duration_since(SystemTime::UNIX_EPOCH)
11 .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
14 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
15 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
18 clk_id_op: OpTy<'tcx, Tag>,
19 tp_op: OpTy<'tcx, Tag>,
20 ) -> InterpResult<'tcx, i32> {
21 let this = self.eval_context_mut();
23 this.assert_target_os("linux", "clock_gettime");
24 this.check_no_isolation("clock_gettime")?;
26 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
27 let tp = this.deref_operand(tp_op)?;
29 let duration = if clk_id == this.eval_libc_i32("CLOCK_REALTIME")? {
30 system_time_to_duration(&SystemTime::now())?
31 } else if clk_id == this.eval_libc_i32("CLOCK_MONOTONIC")? {
32 // Absolute time does not matter, only relative time does, so we can just
33 // use our own time anchor here.
34 Instant::now().duration_since(this.machine.time_anchor)
36 let einval = this.eval_libc("EINVAL")?;
37 this.set_last_error(einval)?;
41 let tv_sec = duration.as_secs();
42 let tv_nsec = duration.subsec_nanos();
45 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
46 immty_from_int_checked(tv_nsec, this.libc_ty_layout("c_long")?)?,
49 this.write_packed_immediates(tp, &imms)?;
56 tv_op: OpTy<'tcx, Tag>,
57 tz_op: OpTy<'tcx, Tag>,
58 ) -> InterpResult<'tcx, i32> {
59 let this = self.eval_context_mut();
61 this.assert_target_os("macos", "gettimeofday");
62 this.check_no_isolation("gettimeofday")?;
64 // Using tz is obsolete and should always be null
65 let tz = this.read_scalar(tz_op)?.not_undef()?;
66 if !this.is_null(tz)? {
67 let einval = this.eval_libc("EINVAL")?;
68 this.set_last_error(einval)?;
72 let tv = this.deref_operand(tv_op)?;
74 let duration = system_time_to_duration(&SystemTime::now())?;
75 let tv_sec = duration.as_secs();
76 let tv_usec = duration.subsec_micros();
79 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
80 immty_from_int_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?,
83 this.write_packed_immediates(tv, &imms)?;
88 fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
89 let this = self.eval_context_ref();
91 this.assert_target_os("macos", "mach_absolute_time");
92 this.check_no_isolation("mach_absolute_time")?;
94 // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
95 // We return plain nanoseconds.
96 let duration = Instant::now().duration_since(this.machine.time_anchor);
97 u64::try_from(duration.as_nanos())
98 .map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())