]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/time.rs
d263aab351b12e07591210cc85ede4bc60c461c0
[rust.git] / src / tools / miri / src / shims / time.rs
1 use std::time::{Duration, SystemTime};
2
3 use crate::concurrency::thread::MachineCallback;
4 use crate::*;
5
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())
10 }
11
12 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
14     fn clock_gettime(
15         &mut self,
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.
22
23         let this = self.eval_context_mut();
24
25         this.assert_target_os_is_unix("clock_gettime");
26
27         let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
28
29         let absolute_clocks;
30         let mut relative_clocks;
31
32         match this.tcx.sess.target.os.as_ref() {
33             "linux" => {
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")?,
41                 ];
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")?,
48                 ];
49             }
50             "macos" => {
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")?);
59                 }
60             }
61             target => throw_unsup_format!("`clock_gettime` is not supported on target OS {target}"),
62         }
63
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())
69         } else {
70             // Unsupported clock.
71             let einval = this.eval_libc("EINVAL")?;
72             this.set_last_error(einval)?;
73             return Ok(Scalar::from_i32(-1));
74         };
75
76         let tv_sec = duration.as_secs();
77         let tv_nsec = duration.subsec_nanos();
78
79         this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
80
81         Ok(Scalar::from_i32(0))
82     }
83
84     fn gettimeofday(
85         &mut self,
86         tv_op: &OpTy<'tcx, Provenance>,
87         tz_op: &OpTy<'tcx, Provenance>,
88     ) -> InterpResult<'tcx, i32> {
89         let this = self.eval_context_mut();
90
91         this.assert_target_os_is_unix("gettimeofday");
92         this.check_no_isolation("`gettimeofday`")?;
93
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)?;
99             return Ok(-1);
100         }
101
102         let duration = system_time_to_duration(&SystemTime::now())?;
103         let tv_sec = duration.as_secs();
104         let tv_usec = duration.subsec_micros();
105
106         this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
107
108         Ok(0)
109     }
110
111     #[allow(non_snake_case, clippy::integer_arithmetic)]
112     fn GetSystemTimeAsFileTime(
113         &mut self,
114         LPFILETIME_op: &OpTy<'tcx, Provenance>,
115     ) -> InterpResult<'tcx> {
116         let this = self.eval_context_mut();
117
118         this.assert_target_os("windows", "GetSystemTimeAsFileTime");
119         this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
120
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;
126
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"))?;
131
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)?,
137         )?;
138
139         Ok(())
140     }
141
142     #[allow(non_snake_case)]
143     fn QueryPerformanceCounter(
144         &mut self,
145         lpPerformanceCount_op: &OpTy<'tcx, Provenance>,
146     ) -> InterpResult<'tcx, Scalar<Provenance>> {
147         let this = self.eval_context_mut();
148
149         this.assert_target_os("windows", "QueryPerformanceCounter");
150
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")
156         })?;
157         this.write_scalar(
158             Scalar::from_i64(qpc),
159             &this.deref_operand(lpPerformanceCount_op)?.into(),
160         )?;
161         Ok(Scalar::from_i32(-1)) // return non-zero on success
162     }
163
164     #[allow(non_snake_case)]
165     fn QueryPerformanceFrequency(
166         &mut self,
167         lpFrequency_op: &OpTy<'tcx, Provenance>,
168     ) -> InterpResult<'tcx, Scalar<Provenance>> {
169         let this = self.eval_context_mut();
170
171         this.assert_target_os("windows", "QueryPerformanceFrequency");
172
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.
178         this.write_scalar(
179             Scalar::from_i64(1_000_000_000),
180             &this.deref_operand(lpFrequency_op)?.into(),
181         )?;
182         Ok(Scalar::from_i32(-1)) // Return non-zero on success
183     }
184
185     fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar<Provenance>> {
186         let this = self.eval_context_ref();
187
188         this.assert_target_os("macos", "mach_absolute_time");
189
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")
195         })?;
196         Ok(Scalar::from_u64(res))
197     }
198
199     fn mach_timebase_info(
200         &mut self,
201         info_op: &OpTy<'tcx, Provenance>,
202     ) -> InterpResult<'tcx, Scalar<Provenance>> {
203         let this = self.eval_context_mut();
204
205         this.assert_target_os("macos", "mach_timebase_info");
206
207         let info = this.deref_operand(info_op)?;
208
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)?;
213
214         Ok(Scalar::from_i32(0)) // KERN_SUCCESS
215     }
216
217     fn nanosleep(
218         &mut self,
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();
223
224         this.assert_target_os_is_unix("nanosleep");
225
226         let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
227             Some(duration) => duration,
228             None => {
229                 let einval = this.eval_libc("EINVAL")?;
230                 this.set_last_error(einval)?;
231                 return Ok(-1);
232             }
233         };
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());
239
240         let active_thread = this.get_active_thread();
241         this.block_thread(active_thread);
242
243         this.register_timeout_callback(
244             active_thread,
245             Time::Monotonic(timeout_time),
246             Box::new(UnblockCallback { thread_to_unblock: active_thread }),
247         );
248
249         Ok(0)
250     }
251
252     #[allow(non_snake_case)]
253     fn Sleep(&mut self, timeout: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx> {
254         let this = self.eval_context_mut();
255
256         this.assert_target_os("windows", "Sleep");
257
258         let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
259
260         let duration = Duration::from_millis(timeout_ms.into());
261         let timeout_time = this.machine.clock.now().checked_add(duration).unwrap();
262
263         let active_thread = this.get_active_thread();
264         this.block_thread(active_thread);
265
266         this.register_timeout_callback(
267             active_thread,
268             Time::Monotonic(timeout_time),
269             Box::new(UnblockCallback { thread_to_unblock: active_thread }),
270         );
271
272         Ok(())
273     }
274 }
275
276 struct UnblockCallback {
277     thread_to_unblock: ThreadId,
278 }
279
280 impl VisitTags for UnblockCallback {
281     fn visit_tags(&self, _visit: &mut dyn FnMut(BorTag)) {}
282 }
283
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);
287         Ok(())
288     }
289 }