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