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, immty_from_uint_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: 'mir> 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 #[allow(non_snake_case)]
89 fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
90 let this = self.eval_context_mut();
92 this.assert_target_os("windows", "GetSystemTimeAsFileTime");
93 this.check_no_isolation("GetSystemTimeAsFileTime")?;
95 let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
96 let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
97 let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
98 let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
99 let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
101 let duration = system_time_to_duration(&SystemTime::now())? + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
102 let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
103 .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
105 let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
106 let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
107 let DWORD_tylayout = this.machine.layouts.u32;
109 immty_from_uint_checked(dwLowDateTime, DWORD_tylayout)?,
110 immty_from_uint_checked(dwHighDateTime, DWORD_tylayout)?,
112 this.write_packed_immediates(this.deref_operand(LPFILETIME_op)?, &imms)?;
116 #[allow(non_snake_case)]
117 fn QueryPerformanceCounter(&mut self, lpPerformanceCount_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
118 let this = self.eval_context_mut();
120 this.assert_target_os("windows", "QueryPerformanceCounter");
121 this.check_no_isolation("QueryPerformanceCounter")?;
123 // QueryPerformanceCounter uses a hardware counter as its basis.
124 // Miri will emulate a counter with a resolution of 1 nanosecond.
125 let duration = Instant::now().duration_since(this.machine.time_anchor);
126 let qpc = i64::try_from(duration.as_nanos())
127 .map_err(|_| err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported"))?;
128 this.write_scalar(Scalar::from_i64(qpc), this.deref_operand(lpPerformanceCount_op)?.into())?;
129 Ok(-1) // return non-zero on success
132 #[allow(non_snake_case)]
133 fn QueryPerformanceFrequency(&mut self, lpFrequency_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
134 let this = self.eval_context_mut();
136 this.assert_target_os("windows", "QueryPerformanceFrequency");
137 this.check_no_isolation("QueryPerformanceFrequency")?;
139 // Retrieves the frequency of the hardware performance counter.
140 // The frequency of the performance counter is fixed at system boot and
141 // is consistent across all processors.
142 // Miri emulates a "hardware" performance counter with a resolution of 1ns,
143 // and thus 10^9 counts per second.
144 this.write_scalar(Scalar::from_i64(1_000_000_000), this.deref_operand(lpFrequency_op)?.into())?;
145 Ok(-1) // Return non-zero on success
148 fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
149 let this = self.eval_context_ref();
151 this.assert_target_os("macos", "mach_absolute_time");
152 this.check_no_isolation("mach_absolute_time")?;
154 // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
155 // We return plain nanoseconds.
156 let duration = Instant::now().duration_since(this.machine.time_anchor);
157 u64::try_from(duration.as_nanos())
158 .map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
161 fn mach_timebase_info(&mut self, info_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
162 let this = self.eval_context_mut();
164 this.assert_target_os("macos", "mach_timebase_info");
165 this.check_no_isolation("mach_timebase_info")?;
167 let info = this.deref_operand(info_op)?;
169 // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
170 // no scaling needs to happen.
171 let (numer, denom) = (1,1);
173 immty_from_int_checked(numer, this.machine.layouts.u32)?,
174 immty_from_int_checked(denom, this.machine.layouts.u32)?
177 this.write_packed_immediates(info, &imms)?;
178 Ok(0) // KERN_SUCCESS