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