1 use std::time::{Duration, Instant, SystemTime};
6 /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
7 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
8 time.duration_since(SystemTime::UNIX_EPOCH)
9 .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
12 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
16 clk_id_op: &OpTy<'tcx, Tag>,
17 tp_op: &OpTy<'tcx, Tag>,
18 ) -> InterpResult<'tcx, i32> {
19 // This clock support is deliberately minimal because a lot of clock types have fiddly
20 // properties (is it possible for Miri to be suspended independently of the host?). If you
21 // have a use for another clock type, please open an issue.
23 let this = self.eval_context_mut();
25 this.assert_target_os("linux", "clock_gettime");
26 this.check_no_isolation("`clock_gettime`")?;
28 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
30 // Linux has two main kinds of clocks. REALTIME clocks return the actual time since the
31 // Unix epoch, including effects which may cause time to move backwards such as NTP.
32 // Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
33 // is just specified to be "faster and less precise", so we implement both the same way.
35 [this.eval_libc_i32("CLOCK_REALTIME")?, this.eval_libc_i32("CLOCK_REALTIME_COARSE")?];
36 // The second kind is MONOTONIC clocks for which 0 is an arbitrary time point, but they are
37 // never allowed to go backwards. We don't need to do any additonal monotonicity
38 // enforcement because std::time::Instant already guarantees that it is monotonic.
40 [this.eval_libc_i32("CLOCK_MONOTONIC")?, this.eval_libc_i32("CLOCK_MONOTONIC_COARSE")?];
42 let duration = if absolute_clocks.contains(&clk_id) {
43 system_time_to_duration(&SystemTime::now())?
44 } else if relative_clocks.contains(&clk_id) {
45 Instant::now().duration_since(this.machine.time_anchor)
47 let einval = this.eval_libc("EINVAL")?;
48 this.set_last_error(einval)?;
52 let tv_sec = duration.as_secs();
53 let tv_nsec = duration.subsec_nanos();
55 this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
62 tv_op: &OpTy<'tcx, Tag>,
63 tz_op: &OpTy<'tcx, Tag>,
64 ) -> InterpResult<'tcx, i32> {
65 let this = self.eval_context_mut();
67 this.assert_target_os_is_unix("gettimeofday");
68 this.check_no_isolation("`gettimeofday`")?;
70 // Using tz is obsolete and should always be null
71 let tz = this.read_pointer(tz_op)?;
72 if !this.ptr_is_null(tz)? {
73 let einval = this.eval_libc("EINVAL")?;
74 this.set_last_error(einval)?;
78 let duration = system_time_to_duration(&SystemTime::now())?;
79 let tv_sec = duration.as_secs();
80 let tv_usec = duration.subsec_micros();
82 this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
87 #[allow(non_snake_case)]
88 fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
89 let this = self.eval_context_mut();
91 this.assert_target_os("windows", "GetSystemTimeAsFileTime");
92 this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
94 let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
95 let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
96 let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
97 let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
98 let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
100 let duration = system_time_to_duration(&SystemTime::now())?
101 + 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 this.write_int_fields(
108 &[dwLowDateTime.into(), dwHighDateTime.into()],
109 &this.deref_operand(LPFILETIME_op)?,
115 #[allow(non_snake_case)]
116 fn QueryPerformanceCounter(
118 lpPerformanceCount_op: &OpTy<'tcx, Tag>,
119 ) -> InterpResult<'tcx, i32> {
120 let this = self.eval_context_mut();
122 this.assert_target_os("windows", "QueryPerformanceCounter");
123 this.check_no_isolation("`QueryPerformanceCounter`")?;
125 // QueryPerformanceCounter uses a hardware counter as its basis.
126 // Miri will emulate a counter with a resolution of 1 nanosecond.
127 let duration = Instant::now().duration_since(this.machine.time_anchor);
128 let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
129 err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
132 Scalar::from_i64(qpc),
133 &this.deref_operand(lpPerformanceCount_op)?.into(),
135 Ok(-1) // return non-zero on success
138 #[allow(non_snake_case)]
139 fn QueryPerformanceFrequency(
141 lpFrequency_op: &OpTy<'tcx, Tag>,
142 ) -> InterpResult<'tcx, i32> {
143 let this = self.eval_context_mut();
145 this.assert_target_os("windows", "QueryPerformanceFrequency");
146 this.check_no_isolation("`QueryPerformanceFrequency`")?;
148 // Retrieves the frequency of the hardware performance counter.
149 // The frequency of the performance counter is fixed at system boot and
150 // is consistent across all processors.
151 // Miri emulates a "hardware" performance counter with a resolution of 1ns,
152 // and thus 10^9 counts per second.
154 Scalar::from_i64(1_000_000_000),
155 &this.deref_operand(lpFrequency_op)?.into(),
157 Ok(-1) // Return non-zero on success
160 fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
161 let this = self.eval_context_ref();
163 this.assert_target_os("macos", "mach_absolute_time");
164 this.check_no_isolation("`mach_absolute_time`")?;
166 // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
167 // We return plain nanoseconds.
168 let duration = Instant::now().duration_since(this.machine.time_anchor);
169 u64::try_from(duration.as_nanos()).map_err(|_| {
170 err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
175 fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
176 let this = self.eval_context_mut();
178 this.assert_target_os("macos", "mach_timebase_info");
179 this.check_no_isolation("`mach_timebase_info`")?;
181 let info = this.deref_operand(info_op)?;
183 // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
184 // no scaling needs to happen.
185 let (numer, denom) = (1, 1);
186 this.write_int_fields(&[numer.into(), denom.into()], &info)?;
188 Ok(0) // KERN_SUCCESS
193 req_op: &OpTy<'tcx, Tag>,
194 _rem: &OpTy<'tcx, Tag>,
195 ) -> InterpResult<'tcx, i32> {
196 // Signal handlers are not supported, so rem will never be written to.
198 let this = self.eval_context_mut();
200 this.check_no_isolation("`nanosleep`")?;
202 let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
203 Some(duration) => duration,
205 let einval = this.eval_libc("EINVAL")?;
206 this.set_last_error(einval)?;
210 let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
212 let active_thread = this.get_active_thread();
213 this.block_thread(active_thread);
215 this.register_timeout_callback(
218 Box::new(move |ecx| {
219 ecx.unblock_thread(active_thread);