Skip to main content

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;
8use rustc_target::spec::Os;
9
10use crate::*;
11
12/// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
13pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
14    time.duration_since(SystemTime::UNIX_EPOCH)
15        .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported"))
16        .into()
17}
18
19impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
20pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
21    fn parse_clockid(&self, clk_id: Scalar) -> Option<TimeoutClock> {
22        // This clock support is deliberately minimal because a lot of clock types have fiddly
23        // properties (is it possible for Miri to be suspended independently of the host?). If you
24        // have a use for another clock type, please open an issue.
25        let this = self.eval_context_ref();
26
27        // Portable names that exist everywhere.
28        if clk_id == this.eval_libc("CLOCK_REALTIME") {
29            return Some(TimeoutClock::RealTime);
30        } else if clk_id == this.eval_libc("CLOCK_MONOTONIC") {
31            return Some(TimeoutClock::Monotonic);
32        }
33
34        // Some further platform-specific names we support.
35        match &this.tcx.sess.target.os {
36            Os::Linux | Os::FreeBsd | Os::Android => {
37                // Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
38                // is just specified to be "faster and less precise", so we treat it like normal
39                // clocks.
40                if clk_id == this.eval_libc("CLOCK_REALTIME_COARSE") {
41                    return Some(TimeoutClock::RealTime);
42                } else if clk_id == this.eval_libc("CLOCK_MONOTONIC_COARSE") {
43                    return Some(TimeoutClock::Monotonic);
44                }
45            }
46            Os::MacOs => {
47                // `CLOCK_UPTIME_RAW` supposed to not increment while the system is asleep... but
48                // that's not really something a program running inside Miri can tell, anyway.
49                // We need to support it because std uses it.
50                if clk_id == this.eval_libc("CLOCK_UPTIME_RAW") {
51                    return Some(TimeoutClock::Monotonic);
52                }
53            }
54            _ => {}
55        }
56
57        None
58    }
59
60    fn clock_gettime(
61        &mut self,
62        clk_id_op: &OpTy<'tcx>,
63        tp_op: &OpTy<'tcx>,
64        dest: &MPlaceTy<'tcx>,
65    ) -> InterpResult<'tcx> {
66        let this = self.eval_context_mut();
67
68        this.assert_target_os_is_unix("clock_gettime");
69
70        let clk_id = this.read_scalar(clk_id_op)?;
71        let tp = this.deref_pointer_as(tp_op, this.libc_ty_layout("timespec"))?;
72
73        let duration = match this.parse_clockid(clk_id) {
74            Some(TimeoutClock::RealTime) => {
75                this.check_no_isolation("`clock_gettime` with `REALTIME` clocks")?;
76                system_time_to_duration(&SystemTime::now())?
77            }
78            Some(TimeoutClock::Monotonic) =>
79                this.machine
80                    .monotonic_clock
81                    .now()
82                    .duration_since(this.machine.monotonic_clock.epoch()),
83            None => {
84                return this.set_last_error_and_return(LibcError("EINVAL"), dest);
85            }
86        };
87
88        let tv_sec = duration.as_secs();
89        let tv_nsec = duration.subsec_nanos();
90
91        this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &tp)?;
92        this.write_int(0, dest)?;
93
94        interp_ok(())
95    }
96
97    fn gettimeofday(
98        &mut self,
99        tv_op: &OpTy<'tcx>,
100        tz_op: &OpTy<'tcx>,
101    ) -> InterpResult<'tcx, Scalar> {
102        let this = self.eval_context_mut();
103
104        this.assert_target_os_is_unix("gettimeofday");
105        this.check_no_isolation("`gettimeofday`")?;
106
107        let tv = this.deref_pointer_as(tv_op, this.libc_ty_layout("timeval"))?;
108
109        // Using tz is obsolete and should always be null
110        let tz = this.read_pointer(tz_op)?;
111        if !this.ptr_is_null(tz)? {
112            return this.set_last_error_and_return_i32(LibcError("EINVAL"));
113        }
114
115        let duration = system_time_to_duration(&SystemTime::now())?;
116        let tv_sec = duration.as_secs();
117        let tv_usec = duration.subsec_micros();
118
119        this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &tv)?;
120
121        interp_ok(Scalar::from_i32(0))
122    }
123
124    // The localtime() function shall convert the time in seconds since the Epoch pointed to by
125    // timer into a broken-down time, expressed as a local time.
126    // https://linux.die.net/man/3/localtime_r
127    fn localtime_r(
128        &mut self,
129        timep: &OpTy<'tcx>,
130        result_op: &OpTy<'tcx>,
131    ) -> InterpResult<'tcx, Pointer> {
132        let this = self.eval_context_mut();
133
134        this.assert_target_os_is_unix("localtime_r");
135        this.check_no_isolation("`localtime_r`")?;
136
137        let time_layout = this.libc_ty_layout("time_t");
138        let timep = this.deref_pointer_as(timep, time_layout)?;
139        let result = this.deref_pointer_as(result_op, this.libc_ty_layout("tm"))?;
140
141        // The input "represents the number of seconds elapsed since the Epoch,
142        // 1970-01-01 00:00:00 +0000 (UTC)".
143        let sec_since_epoch: i64 =
144            this.read_scalar(&timep)?.to_int(time_layout.size)?.try_into().unwrap();
145        let dt_utc: DateTime<Utc> =
146            DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
147
148        // Figure out what time zone is in use
149        let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
150        let tz = match tz.into_string() {
151            Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
152            _ => Tz::UTC,
153        };
154
155        // Convert that to local time, then return the broken-down time value.
156        let dt: DateTime<Tz> = dt_utc.with_timezone(&tz);
157
158        // This value is always set to -1, because there is no way to know if dst is in effect with
159        // chrono crate yet.
160        // This may not be consistent with libc::localtime_r's result.
161        let tm_isdst = -1;
162        this.write_int_fields_named(
163            &[
164                ("tm_sec", dt.second().into()),
165                ("tm_min", dt.minute().into()),
166                ("tm_hour", dt.hour().into()),
167                ("tm_mday", dt.day().into()),
168                ("tm_mon", dt.month0().into()),
169                ("tm_year", dt.year().strict_sub(1900).into()),
170                ("tm_wday", dt.weekday().num_days_from_sunday().into()),
171                ("tm_yday", dt.ordinal0().into()),
172                ("tm_isdst", tm_isdst),
173            ],
174            &result,
175        )?;
176
177        // solaris/illumos system tm struct does not have
178        // the additional tm_zone/tm_gmtoff fields.
179        // https://docs.oracle.com/cd/E36784_01/html/E36874/localtime-r-3c.html
180        if !matches!(&this.tcx.sess.target.os, Os::Solaris | Os::Illumos) {
181            // tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
182            // This may not be consistent with libc::localtime_r's result.
183
184            let offset_in_seconds = dt.offset().fix().local_minus_utc();
185            let tm_gmtoff = offset_in_seconds;
186            let mut tm_zone = String::new();
187            if offset_in_seconds < 0 {
188                tm_zone.push('-');
189            } else {
190                tm_zone.push('+');
191            }
192            let offset_hour = offset_in_seconds.abs() / 3600;
193            write!(tm_zone, "{offset_hour:02}").unwrap();
194            let offset_min = (offset_in_seconds.abs() % 3600) / 60;
195            if offset_min != 0 {
196                write!(tm_zone, "{offset_min:02}").unwrap();
197            }
198
199            // Add null terminator for C string compatibility.
200            tm_zone.push('\0');
201
202            // Deduplicate and allocate the string.
203            let tm_zone_ptr = this.allocate_bytes_dedup(tm_zone.as_bytes())?;
204
205            // Write the timezone pointer and offset into the result structure.
206            this.write_pointer(tm_zone_ptr, &this.project_field_named(&result, "tm_zone")?)?;
207            this.write_int_fields_named(&[("tm_gmtoff", tm_gmtoff.into())], &result)?;
208        }
209        interp_ok(result.ptr())
210    }
211    #[allow(non_snake_case, clippy::arithmetic_side_effects)]
212    fn GetSystemTimeAsFileTime(
213        &mut self,
214        shim_name: &str,
215        LPFILETIME_op: &OpTy<'tcx>,
216    ) -> InterpResult<'tcx> {
217        let this = self.eval_context_mut();
218
219        this.assert_target_os(Os::Windows, shim_name);
220        this.check_no_isolation(shim_name)?;
221
222        let filetime = this.deref_pointer_as(LPFILETIME_op, this.windows_ty_layout("FILETIME"))?;
223
224        let duration = this.system_time_since_windows_epoch(&SystemTime::now())?;
225        let duration_ticks = this.windows_ticks_for(duration)?;
226
227        let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
228        let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
229        this.write_int_fields(&[dwLowDateTime.into(), dwHighDateTime.into()], &filetime)?;
230
231        interp_ok(())
232    }
233
234    #[allow(non_snake_case)]
235    fn QueryPerformanceCounter(
236        &mut self,
237        lpPerformanceCount_op: &OpTy<'tcx>,
238    ) -> InterpResult<'tcx, Scalar> {
239        let this = self.eval_context_mut();
240
241        this.assert_target_os(Os::Windows, "QueryPerformanceCounter");
242
243        // QueryPerformanceCounter uses a hardware counter as its basis.
244        // Miri will emulate a counter with a resolution of 1 nanosecond.
245        let duration =
246            this.machine.monotonic_clock.now().duration_since(this.machine.monotonic_clock.epoch());
247        let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
248            err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
249        })?;
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(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(clippy::arithmetic_side_effects)]
280    fn system_time_since_windows_epoch(&self, time: &SystemTime) -> InterpResult<'tcx, Duration> {
281        // The amount of seconds between 1601/1/1 and 1970/1/1.
282        // See https://learn.microsoft.com/en-us/windows/win32/sysinfo/converting-a-time-t-value-to-a-file-time
283        // (just divide by the number of 100 ns intervals per second).
284        const SECONDS_TO_UNIX_EPOCH: u64 = 11_644_473_600;
285
286        interp_ok(system_time_to_duration(time)? + Duration::from_secs(SECONDS_TO_UNIX_EPOCH))
287    }
288
289    #[allow(non_snake_case, clippy::arithmetic_side_effects)]
290    fn windows_ticks_for(&self, duration: Duration) -> InterpResult<'tcx, u64> {
291        // 1 interval = 100 ns.
292        // See https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
293        const NANOS_PER_INTERVAL: u128 = 100;
294
295        let ticks = u64::try_from(duration.as_nanos() / NANOS_PER_INTERVAL)
296            .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
297        interp_ok(ticks)
298    }
299
300    fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar> {
301        let this = self.eval_context_ref();
302
303        this.assert_target_os(Os::MacOs, "mach_absolute_time");
304
305        // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
306        // We return plain nanoseconds.
307        let duration =
308            this.machine.monotonic_clock.now().duration_since(this.machine.monotonic_clock.epoch());
309        let res = u64::try_from(duration.as_nanos()).map_err(|_| {
310            err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
311        })?;
312        interp_ok(Scalar::from_u64(res))
313    }
314
315    fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
316        let this = self.eval_context_mut();
317
318        this.assert_target_os(Os::MacOs, "mach_timebase_info");
319
320        let info = this.deref_pointer_as(info_op, this.libc_ty_layout("mach_timebase_info"))?;
321
322        // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
323        // no scaling needs to happen.
324        let (numerator, denom) = (1, 1);
325        this.write_int_fields(&[numerator.into(), denom.into()], &info)?;
326
327        interp_ok(Scalar::from_i32(0)) // KERN_SUCCESS
328    }
329
330    fn mach_wait_until(&mut self, deadline_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
331        let this = self.eval_context_mut();
332
333        this.assert_target_os(Os::MacOs, "mach_wait_until");
334
335        let deadline = this.read_scalar(deadline_op)?.to_u64()?;
336        // Our mach_absolute_time "ticks" are plain nanoseconds.
337        let duration = Duration::from_nanos(deadline);
338
339        this.block_thread(
340            BlockReason::Sleep,
341            Some((TimeoutClock::Monotonic, TimeoutAnchor::Absolute, duration)),
342            callback!(
343                @capture<'tcx> {}
344                |_this, unblock: UnblockKind| {
345                    assert_eq!(unblock, UnblockKind::TimedOut);
346                    interp_ok(())
347                }
348            ),
349        );
350
351        interp_ok(Scalar::from_i32(0)) // KERN_SUCCESS
352    }
353
354    fn nanosleep(&mut self, duration: &OpTy<'tcx>, rem: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
355        let this = self.eval_context_mut();
356
357        this.assert_target_os_is_unix("nanosleep");
358
359        let duration = this.deref_pointer_as(duration, this.libc_ty_layout("timespec"))?;
360        let _rem = this.read_pointer(rem)?; // Signal handlers are not supported, so rem will never be written to.
361
362        let Some(duration) = this.read_timespec(&duration)? else {
363            return this.set_last_error_and_return_i32(LibcError("EINVAL"));
364        };
365
366        this.block_thread(
367            BlockReason::Sleep,
368            Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
369            callback!(
370                @capture<'tcx> {}
371                |_this, unblock: UnblockKind| {
372                    assert_eq!(unblock, UnblockKind::TimedOut);
373                    interp_ok(())
374                }
375            ),
376        );
377        interp_ok(Scalar::from_i32(0))
378    }
379
380    fn clock_nanosleep(
381        &mut self,
382        clock_id: &OpTy<'tcx>,
383        flags: &OpTy<'tcx>,
384        timespec: &OpTy<'tcx>,
385        rem: &OpTy<'tcx>,
386    ) -> InterpResult<'tcx, Scalar> {
387        let this = self.eval_context_mut();
388        let clockid_t_size = this.libc_ty_layout("clockid_t").size;
389
390        let clock_id = this.read_scalar(clock_id)?.to_int(clockid_t_size)?;
391        let timespec = this.deref_pointer_as(timespec, this.libc_ty_layout("timespec"))?;
392        let flags = this.read_scalar(flags)?.to_i32()?;
393        let _rem = this.read_pointer(rem)?; // Signal handlers are not supported, so rem will never be written to.
394
395        // The standard lib through sleep_until only needs CLOCK_MONOTONIC
396        if clock_id != this.eval_libc("CLOCK_MONOTONIC").to_int(clockid_t_size)? {
397            throw_unsup_format!("clock_nanosleep: only CLOCK_MONOTONIC is supported");
398        }
399
400        let Some(duration) = this.read_timespec(&timespec)? else {
401            return this.set_last_error_and_return_i32(LibcError("EINVAL"));
402        };
403
404        let timeout_anchor = if flags == 0 {
405            // No flags set, the timespec should be interpreted as a duration
406            // to sleep for
407            TimeoutAnchor::Relative
408        } else if flags == this.eval_libc_i32("TIMER_ABSTIME") {
409            // Only flag TIMER_ABSTIME set, the timespec should be interpreted as
410            // an absolute time.
411            TimeoutAnchor::Absolute
412        } else {
413            // The standard lib (through `sleep_until`) only needs TIMER_ABSTIME
414            throw_unsup_format!(
415                "`clock_nanosleep` unsupported flags {flags}, only no flags or \
416                TIMER_ABSTIME is supported"
417            );
418        };
419
420        this.block_thread(
421            BlockReason::Sleep,
422            Some((TimeoutClock::Monotonic, timeout_anchor, duration)),
423            callback!(
424                @capture<'tcx> {}
425                |_this, unblock: UnblockKind| {
426                    assert_eq!(unblock, UnblockKind::TimedOut);
427                    interp_ok(())
428                }
429            ),
430        );
431        interp_ok(Scalar::from_i32(0))
432    }
433
434    #[allow(non_snake_case)]
435    fn Sleep(&mut self, timeout: &OpTy<'tcx>) -> InterpResult<'tcx> {
436        let this = self.eval_context_mut();
437
438        this.assert_target_os(Os::Windows, "Sleep");
439
440        let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
441
442        let duration = Duration::from_millis(timeout_ms.into());
443
444        this.block_thread(
445            BlockReason::Sleep,
446            Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
447            callback!(
448                @capture<'tcx> {}
449                |_this, unblock: UnblockKind| {
450                    assert_eq!(unblock, UnblockKind::TimedOut);
451                    interp_ok(())
452                }
453            ),
454        );
455        interp_ok(())
456    }
457}