miri/shims/
time.rs

1use std::ffi::{OsStr, OsString};
2use std::fmt::Write;
3use std::str::FromStr;
4use std::time::{Duration, SystemTime};
5
6use chrono::{DateTime, Datelike, Offset, Timelike, Utc};
7use chrono_tz::Tz;
8
9use crate::*;
10
11/// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
12pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
13    time.duration_since(SystemTime::UNIX_EPOCH)
14        .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported"))
15        .into()
16}
17
18impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
19pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
20    fn clock_gettime(
21        &mut self,
22        clk_id_op: &OpTy<'tcx>,
23        tp_op: &OpTy<'tcx>,
24    ) -> InterpResult<'tcx, Scalar> {
25        // This clock support is deliberately minimal because a lot of clock types have fiddly
26        // properties (is it possible for Miri to be suspended independently of the host?). If you
27        // have a use for another clock type, please open an issue.
28
29        let this = self.eval_context_mut();
30
31        this.assert_target_os_is_unix("clock_gettime");
32
33        let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
34        let tp = this.deref_pointer_as(tp_op, this.libc_ty_layout("timespec"))?;
35
36        let absolute_clocks;
37        let mut relative_clocks;
38
39        match this.tcx.sess.target.os.as_ref() {
40            "linux" | "freebsd" | "android" => {
41                // Linux, Android, and FreeBSD have two main kinds of clocks. REALTIME clocks return the actual time since the
42                // Unix epoch, including effects which may cause time to move backwards such as NTP.
43                // Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
44                // is just specified to be "faster and less precise", so we implement both the same way.
45                absolute_clocks = vec![
46                    this.eval_libc_i32("CLOCK_REALTIME"),
47                    this.eval_libc_i32("CLOCK_REALTIME_COARSE"),
48                ];
49                // The second kind is MONOTONIC clocks for which 0 is an arbitrary time point, but they are
50                // never allowed to go backwards. We don't need to do any additional monotonicity
51                // enforcement because std::time::Instant already guarantees that it is monotonic.
52                relative_clocks = vec![
53                    this.eval_libc_i32("CLOCK_MONOTONIC"),
54                    this.eval_libc_i32("CLOCK_MONOTONIC_COARSE"),
55                ];
56            }
57            "macos" => {
58                absolute_clocks = vec![this.eval_libc_i32("CLOCK_REALTIME")];
59                relative_clocks = vec![this.eval_libc_i32("CLOCK_MONOTONIC")];
60                // `CLOCK_UPTIME_RAW` supposed to not increment while the system is asleep... but
61                // that's not really something a program running inside Miri can tell, anyway.
62                // We need to support it because std uses it.
63                relative_clocks.push(this.eval_libc_i32("CLOCK_UPTIME_RAW"));
64            }
65            "solaris" | "illumos" => {
66                // The REALTIME clock returns the actual time since the Unix epoch.
67                absolute_clocks = vec![this.eval_libc_i32("CLOCK_REALTIME")];
68                // MONOTONIC, in the other hand, is the high resolution, non-adjustable
69                // clock from an arbitrary time in the past.
70                // Note that the man page mentions HIGHRES but it is just
71                // an alias of MONOTONIC and the libc crate does not expose it anyway.
72                // https://docs.oracle.com/cd/E23824_01/html/821-1465/clock-gettime-3c.html
73                relative_clocks = vec![this.eval_libc_i32("CLOCK_MONOTONIC")];
74            }
75            target => throw_unsup_format!("`clock_gettime` is not supported on target OS {target}"),
76        }
77
78        let duration = if absolute_clocks.contains(&clk_id) {
79            this.check_no_isolation("`clock_gettime` with `REALTIME` clocks")?;
80            system_time_to_duration(&SystemTime::now())?
81        } else if relative_clocks.contains(&clk_id) {
82            this.machine.clock.now().duration_since(this.machine.clock.epoch())
83        } else {
84            return this.set_last_error_and_return_i32(LibcError("EINVAL"));
85        };
86
87        let tv_sec = duration.as_secs();
88        let tv_nsec = duration.subsec_nanos();
89
90        this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &tp)?;
91
92        interp_ok(Scalar::from_i32(0))
93    }
94
95    fn gettimeofday(
96        &mut self,
97        tv_op: &OpTy<'tcx>,
98        tz_op: &OpTy<'tcx>,
99    ) -> InterpResult<'tcx, Scalar> {
100        let this = self.eval_context_mut();
101
102        this.assert_target_os_is_unix("gettimeofday");
103        this.check_no_isolation("`gettimeofday`")?;
104
105        let tv = this.deref_pointer_as(tv_op, this.libc_ty_layout("timeval"))?;
106
107        // Using tz is obsolete and should always be null
108        let tz = this.read_pointer(tz_op)?;
109        if !this.ptr_is_null(tz)? {
110            return this.set_last_error_and_return_i32(LibcError("EINVAL"));
111        }
112
113        let duration = system_time_to_duration(&SystemTime::now())?;
114        let tv_sec = duration.as_secs();
115        let tv_usec = duration.subsec_micros();
116
117        this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &tv)?;
118
119        interp_ok(Scalar::from_i32(0))
120    }
121
122    // The localtime() function shall convert the time in seconds since the Epoch pointed to by
123    // timer into a broken-down time, expressed as a local time.
124    // https://linux.die.net/man/3/localtime_r
125    fn localtime_r(
126        &mut self,
127        timep: &OpTy<'tcx>,
128        result_op: &OpTy<'tcx>,
129    ) -> InterpResult<'tcx, Pointer> {
130        let this = self.eval_context_mut();
131
132        this.assert_target_os_is_unix("localtime_r");
133        this.check_no_isolation("`localtime_r`")?;
134
135        let time_layout = this.libc_ty_layout("time_t");
136        let timep = this.deref_pointer_as(timep, time_layout)?;
137        let result = this.deref_pointer_as(result_op, this.libc_ty_layout("tm"))?;
138
139        // The input "represents the number of seconds elapsed since the Epoch,
140        // 1970-01-01 00:00:00 +0000 (UTC)".
141        let sec_since_epoch: i64 =
142            this.read_scalar(&timep)?.to_int(time_layout.size)?.try_into().unwrap();
143        let dt_utc: DateTime<Utc> =
144            DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
145
146        // Figure out what time zone is in use
147        let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
148        let tz = match tz.into_string() {
149            Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
150            _ => Tz::UTC,
151        };
152
153        // Convert that to local time, then return the broken-down time value.
154        let dt: DateTime<Tz> = dt_utc.with_timezone(&tz);
155
156        // This value is always set to -1, because there is no way to know if dst is in effect with
157        // chrono crate yet.
158        // This may not be consistent with libc::localtime_r's result.
159        let tm_isdst = -1;
160        this.write_int_fields_named(
161            &[
162                ("tm_sec", dt.second().into()),
163                ("tm_min", dt.minute().into()),
164                ("tm_hour", dt.hour().into()),
165                ("tm_mday", dt.day().into()),
166                ("tm_mon", dt.month0().into()),
167                ("tm_year", dt.year().strict_sub(1900).into()),
168                ("tm_wday", dt.weekday().num_days_from_sunday().into()),
169                ("tm_yday", dt.ordinal0().into()),
170                ("tm_isdst", tm_isdst),
171            ],
172            &result,
173        )?;
174
175        // solaris/illumos system tm struct does not have
176        // the additional tm_zone/tm_gmtoff fields.
177        // https://docs.oracle.com/cd/E36784_01/html/E36874/localtime-r-3c.html
178        if !matches!(&*this.tcx.sess.target.os, "solaris" | "illumos") {
179            // tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
180            // This may not be consistent with libc::localtime_r's result.
181
182            let offset_in_seconds = dt.offset().fix().local_minus_utc();
183            let tm_gmtoff = offset_in_seconds;
184            let mut tm_zone = String::new();
185            if offset_in_seconds < 0 {
186                tm_zone.push('-');
187            } else {
188                tm_zone.push('+');
189            }
190            let offset_hour = offset_in_seconds.abs() / 3600;
191            write!(tm_zone, "{:02}", offset_hour).unwrap();
192            let offset_min = (offset_in_seconds.abs() % 3600) / 60;
193            if offset_min != 0 {
194                write!(tm_zone, "{:02}", offset_min).unwrap();
195            }
196
197            // Add null terminator for C string compatibility.
198            tm_zone.push('\0');
199
200            // Deduplicate and allocate the string.
201            let tm_zone_ptr = this.allocate_bytes_dedup(tm_zone.as_bytes())?;
202
203            // Write the timezone pointer and offset into the result structure.
204            this.write_pointer(tm_zone_ptr, &this.project_field_named(&result, "tm_zone")?)?;
205            this.write_int_fields_named(&[("tm_gmtoff", tm_gmtoff.into())], &result)?;
206        }
207        interp_ok(result.ptr())
208    }
209    #[allow(non_snake_case, clippy::arithmetic_side_effects)]
210    fn GetSystemTimeAsFileTime(
211        &mut self,
212        shim_name: &str,
213        LPFILETIME_op: &OpTy<'tcx>,
214    ) -> InterpResult<'tcx> {
215        let this = self.eval_context_mut();
216
217        this.assert_target_os("windows", shim_name);
218        this.check_no_isolation(shim_name)?;
219
220        let filetime = this.deref_pointer_as(LPFILETIME_op, this.windows_ty_layout("FILETIME"))?;
221
222        let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC");
223        let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC");
224        let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH");
225        let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
226        let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
227
228        let duration = system_time_to_duration(&SystemTime::now())?
229            + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
230        let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
231            .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
232
233        let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
234        let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
235        this.write_int_fields(&[dwLowDateTime.into(), dwHighDateTime.into()], &filetime)?;
236
237        interp_ok(())
238    }
239
240    #[allow(non_snake_case)]
241    fn QueryPerformanceCounter(
242        &mut self,
243        lpPerformanceCount_op: &OpTy<'tcx>,
244    ) -> InterpResult<'tcx, Scalar> {
245        let this = self.eval_context_mut();
246
247        this.assert_target_os("windows", "QueryPerformanceCounter");
248
249        // QueryPerformanceCounter uses a hardware counter as its basis.
250        // Miri will emulate a counter with a resolution of 1 nanosecond.
251        let duration = this.machine.clock.now().duration_since(this.machine.clock.epoch());
252        let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
253            err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
254        })?;
255        this.write_scalar(
256            Scalar::from_i64(qpc),
257            &this.deref_pointer_as(lpPerformanceCount_op, this.machine.layouts.i64)?,
258        )?;
259        interp_ok(Scalar::from_i32(-1)) // return non-zero on success
260    }
261
262    #[allow(non_snake_case)]
263    fn QueryPerformanceFrequency(
264        &mut self,
265        lpFrequency_op: &OpTy<'tcx>,
266    ) -> InterpResult<'tcx, Scalar> {
267        let this = self.eval_context_mut();
268
269        this.assert_target_os("windows", "QueryPerformanceFrequency");
270
271        // Retrieves the frequency of the hardware performance counter.
272        // The frequency of the performance counter is fixed at system boot and
273        // is consistent across all processors.
274        // Miri emulates a "hardware" performance counter with a resolution of 1ns,
275        // and thus 10^9 counts per second.
276        this.write_scalar(
277            Scalar::from_i64(1_000_000_000),
278            &this.deref_pointer_as(lpFrequency_op, this.machine.layouts.u64)?,
279        )?;
280        interp_ok(Scalar::from_i32(-1)) // Return non-zero on success
281    }
282
283    fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar> {
284        let this = self.eval_context_ref();
285
286        this.assert_target_os("macos", "mach_absolute_time");
287
288        // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
289        // We return plain nanoseconds.
290        let duration = this.machine.clock.now().duration_since(this.machine.clock.epoch());
291        let res = u64::try_from(duration.as_nanos()).map_err(|_| {
292            err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
293        })?;
294        interp_ok(Scalar::from_u64(res))
295    }
296
297    fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
298        let this = self.eval_context_mut();
299
300        this.assert_target_os("macos", "mach_timebase_info");
301
302        let info = this.deref_pointer_as(info_op, this.libc_ty_layout("mach_timebase_info"))?;
303
304        // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
305        // no scaling needs to happen.
306        let (numer, denom) = (1, 1);
307        this.write_int_fields(&[numer.into(), denom.into()], &info)?;
308
309        interp_ok(Scalar::from_i32(0)) // KERN_SUCCESS
310    }
311
312    fn nanosleep(
313        &mut self,
314        req_op: &OpTy<'tcx>,
315        _rem: &OpTy<'tcx>, // Signal handlers are not supported, so rem will never be written to.
316    ) -> InterpResult<'tcx, Scalar> {
317        let this = self.eval_context_mut();
318
319        this.assert_target_os_is_unix("nanosleep");
320
321        let req = this.deref_pointer_as(req_op, this.libc_ty_layout("timespec"))?;
322
323        let duration = match this.read_timespec(&req)? {
324            Some(duration) => duration,
325            None => {
326                return this.set_last_error_and_return_i32(LibcError("EINVAL"));
327            }
328        };
329
330        this.block_thread(
331            BlockReason::Sleep,
332            Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
333            callback!(
334                @capture<'tcx> {}
335                |_this, unblock: UnblockKind| {
336                    assert_eq!(unblock, UnblockKind::TimedOut);
337                    interp_ok(())
338                }
339            ),
340        );
341        interp_ok(Scalar::from_i32(0))
342    }
343
344    #[allow(non_snake_case)]
345    fn Sleep(&mut self, timeout: &OpTy<'tcx>) -> InterpResult<'tcx> {
346        let this = self.eval_context_mut();
347
348        this.assert_target_os("windows", "Sleep");
349
350        let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
351
352        let duration = Duration::from_millis(timeout_ms.into());
353
354        this.block_thread(
355            BlockReason::Sleep,
356            Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
357            callback!(
358                @capture<'tcx> {}
359                |_this, unblock: UnblockKind| {
360                    assert_eq!(unblock, UnblockKind::TimedOut);
361                    interp_ok(())
362                }
363            ),
364        );
365        interp_ok(())
366    }
367}