]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
remove no longer needed imports
[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         let this = self.eval_context_mut();
20
21         this.assert_target_os("linux", "clock_gettime");
22         this.check_no_isolation("`clock_gettime`")?;
23
24         let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
25
26         let duration = if clk_id == this.eval_libc_i32("CLOCK_REALTIME")? {
27             system_time_to_duration(&SystemTime::now())?
28         } else if clk_id == this.eval_libc_i32("CLOCK_MONOTONIC")? {
29             // Absolute time does not matter, only relative time does, so we can just
30             // use our own time anchor here.
31             Instant::now().duration_since(this.machine.time_anchor)
32         } else {
33             let einval = this.eval_libc("EINVAL")?;
34             this.set_last_error(einval)?;
35             return Ok(-1);
36         };
37
38         let tv_sec = duration.as_secs();
39         let tv_nsec = duration.subsec_nanos();
40
41         this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
42
43         Ok(0)
44     }
45
46     fn gettimeofday(
47         &mut self,
48         tv_op: &OpTy<'tcx, Tag>,
49         tz_op: &OpTy<'tcx, Tag>,
50     ) -> InterpResult<'tcx, i32> {
51         let this = self.eval_context_mut();
52
53         this.assert_target_os("macos", "gettimeofday");
54         this.check_no_isolation("`gettimeofday`")?;
55
56         // Using tz is obsolete and should always be null
57         let tz = this.read_pointer(tz_op)?;
58         if !this.ptr_is_null(tz)? {
59             let einval = this.eval_libc("EINVAL")?;
60             this.set_last_error(einval)?;
61             return Ok(-1);
62         }
63
64         let duration = system_time_to_duration(&SystemTime::now())?;
65         let tv_sec = duration.as_secs();
66         let tv_usec = duration.subsec_micros();
67
68         this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
69
70         Ok(0)
71     }
72
73     #[allow(non_snake_case)]
74     fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
75         let this = self.eval_context_mut();
76
77         this.assert_target_os("windows", "GetSystemTimeAsFileTime");
78         this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
79
80         let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC")?;
81         let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC")?;
82         let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH")?;
83         let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
84         let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
85
86         let duration = system_time_to_duration(&SystemTime::now())?
87             + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
88         let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
89             .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
90
91         let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
92         let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
93         this.write_int_fields(
94             &[dwLowDateTime.into(), dwHighDateTime.into()],
95             &this.deref_operand(LPFILETIME_op)?,
96         )?;
97
98         Ok(())
99     }
100
101     #[allow(non_snake_case)]
102     fn QueryPerformanceCounter(
103         &mut self,
104         lpPerformanceCount_op: &OpTy<'tcx, Tag>,
105     ) -> InterpResult<'tcx, i32> {
106         let this = self.eval_context_mut();
107
108         this.assert_target_os("windows", "QueryPerformanceCounter");
109         this.check_no_isolation("`QueryPerformanceCounter`")?;
110
111         // QueryPerformanceCounter uses a hardware counter as its basis.
112         // Miri will emulate a counter with a resolution of 1 nanosecond.
113         let duration = Instant::now().duration_since(this.machine.time_anchor);
114         let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
115             err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
116         })?;
117         this.write_scalar(
118             Scalar::from_i64(qpc),
119             &this.deref_operand(lpPerformanceCount_op)?.into(),
120         )?;
121         Ok(-1) // return non-zero on success
122     }
123
124     #[allow(non_snake_case)]
125     fn QueryPerformanceFrequency(
126         &mut self,
127         lpFrequency_op: &OpTy<'tcx, Tag>,
128     ) -> InterpResult<'tcx, i32> {
129         let this = self.eval_context_mut();
130
131         this.assert_target_os("windows", "QueryPerformanceFrequency");
132         this.check_no_isolation("`QueryPerformanceFrequency`")?;
133
134         // Retrieves the frequency of the hardware performance counter.
135         // The frequency of the performance counter is fixed at system boot and
136         // is consistent across all processors.
137         // Miri emulates a "hardware" performance counter with a resolution of 1ns,
138         // and thus 10^9 counts per second.
139         this.write_scalar(
140             Scalar::from_i64(1_000_000_000),
141             &this.deref_operand(lpFrequency_op)?.into(),
142         )?;
143         Ok(-1) // Return non-zero on success
144     }
145
146     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
147         let this = self.eval_context_ref();
148
149         this.assert_target_os("macos", "mach_absolute_time");
150         this.check_no_isolation("`mach_absolute_time`")?;
151
152         // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
153         // We return plain nanoseconds.
154         let duration = Instant::now().duration_since(this.machine.time_anchor);
155         u64::try_from(duration.as_nanos()).map_err(|_| {
156             err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
157                 .into()
158         })
159     }
160
161     fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
162         let this = self.eval_context_mut();
163
164         this.assert_target_os("macos", "mach_timebase_info");
165         this.check_no_isolation("`mach_timebase_info`")?;
166
167         let info = this.deref_operand(info_op)?;
168
169         // Since our emulated ticks in `mach_absolute_time` *are* nanoseconds,
170         // no scaling needs to happen.
171         let (numer, denom) = (1, 1);
172         this.write_int_fields(&[numer.into(), denom.into()], &info)?;
173
174         Ok(0) // KERN_SUCCESS
175     }
176
177     fn nanosleep(
178         &mut self,
179         req_op: &OpTy<'tcx, Tag>,
180         _rem: &OpTy<'tcx, Tag>,
181     ) -> InterpResult<'tcx, i32> {
182         // Signal handlers are not supported, so rem will never be written to.
183
184         let this = self.eval_context_mut();
185
186         this.check_no_isolation("`nanosleep`")?;
187
188         let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
189             Some(duration) => duration,
190             None => {
191                 let einval = this.eval_libc("EINVAL")?;
192                 this.set_last_error(einval)?;
193                 return Ok(-1);
194             }
195         };
196         let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
197
198         let active_thread = this.get_active_thread();
199         this.block_thread(active_thread);
200
201         this.register_timeout_callback(
202             active_thread,
203             timeout_time,
204             Box::new(move |ecx| {
205                 ecx.unblock_thread(active_thread);
206                 Ok(())
207             }),
208         );
209
210         Ok(0)
211     }
212 }