1 use std::time::{Duration, SystemTime};
3 use crate::concurrency::thread::MachineCallback;
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::MiriInterpCx<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
16 clk_id_op: &OpTy<'tcx, Provenance>,
17 tp_op: &OpTy<'tcx, Provenance>,
18 ) -> InterpResult<'tcx, Scalar<Provenance>> {
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_is_unix("clock_gettime");
27 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
30 let mut relative_clocks;
32 match this.tcx.sess.target.os.as_ref() {
34 // Linux has two main kinds of clocks. REALTIME clocks return the actual time since the
35 // Unix epoch, including effects which may cause time to move backwards such as NTP.
36 // Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
37 // is just specified to be "faster and less precise", so we implement both the same way.
38 absolute_clocks = vec![
39 this.eval_libc_i32("CLOCK_REALTIME")?,
40 this.eval_libc_i32("CLOCK_REALTIME_COARSE")?,
42 // The second kind is MONOTONIC clocks for which 0 is an arbitrary time point, but they are
43 // never allowed to go backwards. We don't need to do any additonal monotonicity
44 // enforcement because std::time::Instant already guarantees that it is monotonic.
45 relative_clocks = vec![
46 this.eval_libc_i32("CLOCK_MONOTONIC")?,
47 this.eval_libc_i32("CLOCK_MONOTONIC_COARSE")?,
51 absolute_clocks = vec![this.eval_libc_i32("CLOCK_REALTIME")?];
52 relative_clocks = vec![this.eval_libc_i32("CLOCK_MONOTONIC")?];
53 // Some clocks only seem to exist in the aarch64 version of the target.
54 if this.tcx.sess.target.arch == "aarch64" {
55 // `CLOCK_UPTIME_RAW` supposed to not increment while the system is asleep... but
56 // that's not really something a program running inside Miri can tell, anyway.
57 // We need to support it because std uses it.
58 relative_clocks.push(this.eval_libc_i32("CLOCK_UPTIME_RAW")?);
61 target => throw_unsup_format!("`clock_gettime` is not supported on target OS {target}"),
64 let duration = if absolute_clocks.contains(&clk_id) {
65 this.check_no_isolation("`clock_gettime` with `REALTIME` clocks")?;
66 system_time_to_duration(&SystemTime::now())?
67 } else if relative_clocks.contains(&clk_id) {
68 this.machine.clock.now().duration_since(this.machine.clock.anchor())
71 let einval = this.eval_libc("EINVAL")?;
72 this.set_last_error(einval)?;
73 return Ok(Scalar::from_i32(-1));
76 let tv_sec = duration.as_secs();
77 let tv_nsec = duration.subsec_nanos();
79 this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
81 Ok(Scalar::from_i32(0))
86 tv_op: &OpTy<'tcx, Provenance>,
87 tz_op: &OpTy<'tcx, Provenance>,
88 ) -> InterpResult<'tcx, i32> {
89 let this = self.eval_context_mut();
91 this.assert_target_os_is_unix("gettimeofday");
92 this.check_no_isolation("`gettimeofday`")?;
94 // Using tz is obsolete and should always be null
95 let tz = this.read_pointer(tz_op)?;
96 if !this.ptr_is_null(tz)? {
97 let einval = this.eval_libc("EINVAL")?;
98 this.set_last_error(einval)?;
102 let duration = system_time_to_duration(&SystemTime::now())?;
103 let tv_sec = duration.as_secs();
104 let tv_usec = duration.subsec_micros();
106 this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
111 #[allow(non_snake_case, clippy::integer_arithmetic)]
112 fn GetSystemTimeAsFileTime(
114 LPFILETIME_op: &OpTy<'tcx, Provenance>,
115 ) -> InterpResult<'tcx> {
116 let this = self.eval_context_mut();
118 this.assert_target_os("windows", "GetSystemTimeAsFileTime");
119 this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
121 let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
122 let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
123 let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
124 let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
125 let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
127 let duration = system_time_to_duration(&SystemTime::now())?
128 + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
129 let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
130 .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
132 let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
133 let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
134 this.write_int_fields(
135 &[dwLowDateTime.into(), dwHighDateTime.into()],
136 &this.deref_operand(LPFILETIME_op)?,
142 #[allow(non_snake_case)]
143 fn QueryPerformanceCounter(
145 lpPerformanceCount_op: &OpTy<'tcx, Provenance>,
146 ) -> InterpResult<'tcx, Scalar<Provenance>> {
147 let this = self.eval_context_mut();
149 this.assert_target_os("windows", "QueryPerformanceCounter");
151 // QueryPerformanceCounter uses a hardware counter as its basis.
152 // Miri will emulate a counter with a resolution of 1 nanosecond.
153 let duration = this.machine.clock.now().duration_since(this.machine.clock.anchor());
154 let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
155 err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
158 Scalar::from_i64(qpc),
159 &this.deref_operand(lpPerformanceCount_op)?.into(),
161 Ok(Scalar::from_i32(-1)) // return non-zero on success
164 #[allow(non_snake_case)]
165 fn QueryPerformanceFrequency(
167 lpFrequency_op: &OpTy<'tcx, Provenance>,
168 ) -> InterpResult<'tcx, Scalar<Provenance>> {
169 let this = self.eval_context_mut();
171 this.assert_target_os("windows", "QueryPerformanceFrequency");
173 // Retrieves the frequency of the hardware performance counter.
174 // The frequency of the performance counter is fixed at system boot and
175 // is consistent across all processors.
176 // Miri emulates a "hardware" performance counter with a resolution of 1ns,
177 // and thus 10^9 counts per second.
179 Scalar::from_i64(1_000_000_000),
180 &this.deref_operand(lpFrequency_op)?.into(),
182 Ok(Scalar::from_i32(-1)) // Return non-zero on success
185 fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar<Provenance>> {
186 let this = self.eval_context_ref();
188 this.assert_target_os("macos", "mach_absolute_time");
190 // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
191 // We return plain nanoseconds.
192 let duration = this.machine.clock.now().duration_since(this.machine.clock.anchor());
193 let res = u64::try_from(duration.as_nanos()).map_err(|_| {
194 err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
196 Ok(Scalar::from_u64(res))
199 fn mach_timebase_info(
201 info_op: &OpTy<'tcx, Provenance>,
202 ) -> InterpResult<'tcx, Scalar<Provenance>> {
203 let this = self.eval_context_mut();
205 this.assert_target_os("macos", "mach_timebase_info");
207 let info = this.deref_operand(info_op)?;
209 // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
210 // no scaling needs to happen.
211 let (numer, denom) = (1, 1);
212 this.write_int_fields(&[numer.into(), denom.into()], &info)?;
214 Ok(Scalar::from_i32(0)) // KERN_SUCCESS
219 req_op: &OpTy<'tcx, Provenance>,
220 _rem: &OpTy<'tcx, Provenance>, // Signal handlers are not supported, so rem will never be written to.
221 ) -> InterpResult<'tcx, i32> {
222 let this = self.eval_context_mut();
224 this.assert_target_os_is_unix("nanosleep");
226 let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
227 Some(duration) => duration,
229 let einval = this.eval_libc("EINVAL")?;
230 this.set_last_error(einval)?;
234 // If adding the duration overflows, let's just sleep for an hour. Waking up early is always acceptable.
235 let now = this.machine.clock.now();
236 let timeout_time = now
237 .checked_add(duration)
238 .unwrap_or_else(|| now.checked_add(Duration::from_secs(3600)).unwrap());
240 let active_thread = this.get_active_thread();
241 this.block_thread(active_thread);
243 this.register_timeout_callback(
245 Time::Monotonic(timeout_time),
246 Box::new(UnblockCallback { thread_to_unblock: active_thread }),
252 #[allow(non_snake_case)]
253 fn Sleep(&mut self, timeout: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx> {
254 let this = self.eval_context_mut();
256 this.assert_target_os("windows", "Sleep");
258 let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
260 let duration = Duration::from_millis(timeout_ms.into());
261 let timeout_time = this.machine.clock.now().checked_add(duration).unwrap();
263 let active_thread = this.get_active_thread();
264 this.block_thread(active_thread);
266 this.register_timeout_callback(
268 Time::Monotonic(timeout_time),
269 Box::new(UnblockCallback { thread_to_unblock: active_thread }),
276 struct UnblockCallback {
277 thread_to_unblock: ThreadId,
280 impl VisitTags for UnblockCallback {
281 fn visit_tags(&self, _visit: &mut dyn FnMut(BorTag)) {}
284 impl<'mir, 'tcx: 'mir> MachineCallback<'mir, 'tcx> for UnblockCallback {
285 fn call(&self, ecx: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
286 ecx.unblock_thread(self.thread_to_unblock);