]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
Auto merge of #1294 - JOE1994:windows_instant, r=RalfJung
[rust.git] / src / shims / time.rs
1 use std::time::{Duration, SystemTime, Instant};
2 use std::convert::TryFrom;
3
4 use rustc_target::abi::LayoutOf;
5
6 use crate::stacked_borrows::Tag;
7 use crate::*;
8 use helpers::{immty_from_int_checked, immty_from_uint_checked};
9
10 /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
11 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
12     time.duration_since(SystemTime::UNIX_EPOCH)
13         .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
14 }
15
16 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
17 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
18     fn clock_gettime(
19         &mut self,
20         clk_id_op: OpTy<'tcx, Tag>,
21         tp_op: OpTy<'tcx, Tag>,
22     ) -> InterpResult<'tcx, i32> {
23         let this = self.eval_context_mut();
24
25         this.assert_target_os("linux", "clock_gettime");
26         this.check_no_isolation("clock_gettime")?;
27
28         let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
29         let tp = this.deref_operand(tp_op)?;
30
31         let duration = if clk_id == this.eval_libc_i32("CLOCK_REALTIME")? {
32             system_time_to_duration(&SystemTime::now())?
33         } else if clk_id == this.eval_libc_i32("CLOCK_MONOTONIC")? {
34             // Absolute time does not matter, only relative time does, so we can just
35             // use our own time anchor here.
36             Instant::now().duration_since(this.machine.time_anchor)
37         } else {
38             let einval = this.eval_libc("EINVAL")?;
39             this.set_last_error(einval)?;
40             return Ok(-1);
41         };
42
43         let tv_sec = duration.as_secs();
44         let tv_nsec = duration.subsec_nanos();
45
46         let imms = [
47             immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
48             immty_from_int_checked(tv_nsec, this.libc_ty_layout("c_long")?)?,
49         ];
50
51         this.write_packed_immediates(tp, &imms)?;
52
53         Ok(0)
54     }
55
56     fn gettimeofday(
57         &mut self,
58         tv_op: OpTy<'tcx, Tag>,
59         tz_op: OpTy<'tcx, Tag>,
60     ) -> InterpResult<'tcx, i32> {
61         let this = self.eval_context_mut();
62
63         this.assert_target_os("macos", "gettimeofday");
64         this.check_no_isolation("gettimeofday")?;
65
66         // Using tz is obsolete and should always be null
67         let tz = this.read_scalar(tz_op)?.not_undef()?;
68         if !this.is_null(tz)? {
69             let einval = this.eval_libc("EINVAL")?;
70             this.set_last_error(einval)?;
71             return Ok(-1);
72         }
73
74         let tv = this.deref_operand(tv_op)?;
75
76         let duration = system_time_to_duration(&SystemTime::now())?;
77         let tv_sec = duration.as_secs();
78         let tv_usec = duration.subsec_micros();
79
80         let imms = [
81             immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
82             immty_from_int_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?,
83         ];
84
85         this.write_packed_immediates(tv, &imms)?;
86
87         Ok(0)
88     }
89
90     #[allow(non_snake_case)]
91     fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
92         let this = self.eval_context_mut();
93
94         this.assert_target_os("windows", "GetSystemTimeAsFileTime");
95         this.check_no_isolation("GetSystemTimeAsFileTime")?;
96
97         let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
98         let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
99         let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
100         let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
101         let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
102
103         let duration = system_time_to_duration(&SystemTime::now())? + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
104         let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
105             .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
106
107         let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
108         let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
109         let DWORD_tylayout = this.layout_of(this.tcx.types.u32)?;
110         let imms = [
111             immty_from_uint_checked(dwLowDateTime, DWORD_tylayout)?,
112             immty_from_uint_checked(dwHighDateTime, DWORD_tylayout)?,
113         ];
114         this.write_packed_immediates(this.deref_operand(LPFILETIME_op)?, &imms)?;
115         Ok(())
116     }
117
118     #[allow(non_snake_case)]
119     fn QueryPerformanceCounter(&mut self, lpPerformanceCount_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
120         let this = self.eval_context_mut();
121
122         this.assert_target_os("windows", "QueryPerformanceCounter");
123         this.check_no_isolation("QueryPerformanceCounter")?;
124
125         // QueryPerformanceCounter uses a hardware counter as its basis.
126         // Miri will emulate a counter with a resolution of 1 nanosecond.
127         let duration = Instant::now().duration_since(this.machine.time_anchor);
128         let qpc = i64::try_from(duration.as_nanos())
129             .map_err(|_| err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported"))?;
130         this.write_scalar(Scalar::from_i64(qpc), this.deref_operand(lpPerformanceCount_op)?.into())?;
131         Ok(-1) // return non-zero on success
132     }
133
134     #[allow(non_snake_case)]
135     fn QueryPerformanceFrequency(&mut self, lpFrequency_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
136         let this = self.eval_context_mut();
137
138         this.assert_target_os("windows", "QueryPerformanceFrequency");
139         this.check_no_isolation("QueryPerformanceFrequency")?;
140
141         // Retrieves the frequency of the hardware performance counter.
142         // The frequency of the performance counter is fixed at system boot and
143         // is consistent across all processors.
144         // Miri emulates a "hardware" performance counter with a resolution of 1ns,
145         // and thus 10^9 counts per second.
146         this.write_scalar(Scalar::from_i64(1_000_000_000), this.deref_operand(lpFrequency_op)?.into())?;
147         Ok(-1) // Return non-zero on success
148     }
149
150     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
151         let this = self.eval_context_ref();
152
153         this.assert_target_os("macos", "mach_absolute_time");
154         this.check_no_isolation("mach_absolute_time")?;
155
156         // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
157         // We return plain nanoseconds.
158         let duration = Instant::now().duration_since(this.machine.time_anchor);
159         u64::try_from(duration.as_nanos())
160             .map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
161     }
162 }