]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
Auto merge of #2245 - saethlin:color-always, r=RalfJung
[rust.git] / src / shims / time.rs
1 use std::time::{Duration, Instant, SystemTime};
2
3 use crate::*;
4 use thread::Time;
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::MiriEvalContext<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
14     fn clock_gettime(
15         &mut self,
16         clk_id_op: &OpTy<'tcx, Tag>,
17         tp_op: &OpTy<'tcx, Tag>,
18     ) -> InterpResult<'tcx, i32> {
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("linux", "clock_gettime");
26         this.check_no_isolation("`clock_gettime`")?;
27
28         let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
29
30         // Linux has two main kinds of clocks. REALTIME clocks return the actual time since the
31         // Unix epoch, including effects which may cause time to move backwards such as NTP.
32         // Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
33         // is just specified to be "faster and less precise", so we implement both the same way.
34         let absolute_clocks =
35             [this.eval_libc_i32("CLOCK_REALTIME")?, this.eval_libc_i32("CLOCK_REALTIME_COARSE")?];
36         // The second kind is MONOTONIC clocks for which 0 is an arbitrary time point, but they are
37         // never allowed to go backwards. We don't need to do any additonal monotonicity
38         // enforcement because std::time::Instant already guarantees that it is monotonic.
39         let relative_clocks =
40             [this.eval_libc_i32("CLOCK_MONOTONIC")?, this.eval_libc_i32("CLOCK_MONOTONIC_COARSE")?];
41
42         let duration = if absolute_clocks.contains(&clk_id) {
43             system_time_to_duration(&SystemTime::now())?
44         } else if relative_clocks.contains(&clk_id) {
45             Instant::now().duration_since(this.machine.time_anchor)
46         } else {
47             let einval = this.eval_libc("EINVAL")?;
48             this.set_last_error(einval)?;
49             return Ok(-1);
50         };
51
52         let tv_sec = duration.as_secs();
53         let tv_nsec = duration.subsec_nanos();
54
55         this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
56
57         Ok(0)
58     }
59
60     fn gettimeofday(
61         &mut self,
62         tv_op: &OpTy<'tcx, Tag>,
63         tz_op: &OpTy<'tcx, Tag>,
64     ) -> InterpResult<'tcx, i32> {
65         let this = self.eval_context_mut();
66
67         this.assert_target_os("macos", "gettimeofday");
68         this.check_no_isolation("`gettimeofday`")?;
69
70         // Using tz is obsolete and should always be null
71         let tz = this.read_pointer(tz_op)?;
72         if !this.ptr_is_null(tz)? {
73             let einval = this.eval_libc("EINVAL")?;
74             this.set_last_error(einval)?;
75             return Ok(-1);
76         }
77
78         let duration = system_time_to_duration(&SystemTime::now())?;
79         let tv_sec = duration.as_secs();
80         let tv_usec = duration.subsec_micros();
81
82         this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
83
84         Ok(0)
85     }
86
87     #[allow(non_snake_case)]
88     fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
89         let this = self.eval_context_mut();
90
91         this.assert_target_os("windows", "GetSystemTimeAsFileTime");
92         this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
93
94         let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
95         let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
96         let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
97         let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
98         let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
99
100         let duration = system_time_to_duration(&SystemTime::now())?
101             + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
102         let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
103             .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
104
105         let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
106         let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
107         this.write_int_fields(
108             &[dwLowDateTime.into(), dwHighDateTime.into()],
109             &this.deref_operand(LPFILETIME_op)?,
110         )?;
111
112         Ok(())
113     }
114
115     #[allow(non_snake_case)]
116     fn QueryPerformanceCounter(
117         &mut self,
118         lpPerformanceCount_op: &OpTy<'tcx, Tag>,
119     ) -> 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()).map_err(|_| {
129             err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
130         })?;
131         this.write_scalar(
132             Scalar::from_i64(qpc),
133             &this.deref_operand(lpPerformanceCount_op)?.into(),
134         )?;
135         Ok(-1) // return non-zero on success
136     }
137
138     #[allow(non_snake_case)]
139     fn QueryPerformanceFrequency(
140         &mut self,
141         lpFrequency_op: &OpTy<'tcx, Tag>,
142     ) -> InterpResult<'tcx, i32> {
143         let this = self.eval_context_mut();
144
145         this.assert_target_os("windows", "QueryPerformanceFrequency");
146         this.check_no_isolation("`QueryPerformanceFrequency`")?;
147
148         // Retrieves the frequency of the hardware performance counter.
149         // The frequency of the performance counter is fixed at system boot and
150         // is consistent across all processors.
151         // Miri emulates a "hardware" performance counter with a resolution of 1ns,
152         // and thus 10^9 counts per second.
153         this.write_scalar(
154             Scalar::from_i64(1_000_000_000),
155             &this.deref_operand(lpFrequency_op)?.into(),
156         )?;
157         Ok(-1) // Return non-zero on success
158     }
159
160     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
161         let this = self.eval_context_ref();
162
163         this.assert_target_os("macos", "mach_absolute_time");
164         this.check_no_isolation("`mach_absolute_time`")?;
165
166         // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
167         // We return plain nanoseconds.
168         let duration = Instant::now().duration_since(this.machine.time_anchor);
169         u64::try_from(duration.as_nanos()).map_err(|_| {
170             err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
171                 .into()
172         })
173     }
174
175     fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
176         let this = self.eval_context_mut();
177
178         this.assert_target_os("macos", "mach_timebase_info");
179         this.check_no_isolation("`mach_timebase_info`")?;
180
181         let info = this.deref_operand(info_op)?;
182
183         // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
184         // no scaling needs to happen.
185         let (numer, denom) = (1, 1);
186         this.write_int_fields(&[numer.into(), denom.into()], &info)?;
187
188         Ok(0) // KERN_SUCCESS
189     }
190
191     fn nanosleep(
192         &mut self,
193         req_op: &OpTy<'tcx, Tag>,
194         _rem: &OpTy<'tcx, Tag>,
195     ) -> InterpResult<'tcx, i32> {
196         // Signal handlers are not supported, so rem will never be written to.
197
198         let this = self.eval_context_mut();
199
200         this.check_no_isolation("`nanosleep`")?;
201
202         let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
203             Some(duration) => duration,
204             None => {
205                 let einval = this.eval_libc("EINVAL")?;
206                 this.set_last_error(einval)?;
207                 return Ok(-1);
208             }
209         };
210         let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
211
212         let active_thread = this.get_active_thread();
213         this.block_thread(active_thread);
214
215         this.register_timeout_callback(
216             active_thread,
217             timeout_time,
218             Box::new(move |ecx| {
219                 ecx.unblock_thread(active_thread);
220                 Ok(())
221             }),
222         );
223
224         Ok(0)
225     }
226 }