]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
Update comments in src/shims/time.rs
[rust.git] / src / shims / time.rs
1 use std::time::{Duration, SystemTime, Instant};
2 use std::convert::TryFrom;
3
4 use crate::stacked_borrows::Tag;
5 use crate::*;
6 use helpers::{immty_from_int_checked, immty_from_uint_checked};
7
8 use rustc_middle::ty::layout::LayoutOf;
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())?
104             .checked_add(Duration::from_secs(SECONDS_TO_UNIX_EPOCH))
105             .unwrap();        
106         let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
107             .map_err(|_| err_unsup_format!("programs running longer than 2^64 ticks are not supported"))?;
108
109         let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
110         let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
111         let DWORD_tylayout = this.layout_of(this.tcx.types.u32)?;
112         let imms = [
113             immty_from_uint_checked(dwLowDateTime, DWORD_tylayout)?,
114             immty_from_uint_checked(dwHighDateTime, DWORD_tylayout)?,
115         ];
116         this.write_packed_immediates(this.deref_operand(LPFILETIME_op)?, &imms)?;
117         Ok(())
118     }
119
120     #[allow(non_snake_case)]
121     fn QueryPerformanceCounter(&mut self, lpPerformanceCount_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
122         let this = self.eval_context_mut();
123
124         this.assert_target_os("windows", "QueryPerformanceCounter");
125         this.check_no_isolation("QueryPerformanceCounter")?;
126
127         // QueryPerformanceCounter uses a hardware counter as its basis.
128         // Miri will emulate a counter with a resolution of 1 nanosecond.
129         let duration = Instant::now().duration_since(this.machine.time_anchor);
130         let qpc = i64::try_from(duration.as_nanos())
131             .map_err(|_| err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported"))?;
132         this.write_scalar(Scalar::from_i64(qpc), this.deref_operand(lpPerformanceCount_op)?.into())?;
133         Ok(-1) // return non-zero on success
134     }
135
136     #[allow(non_snake_case)]
137     fn QueryPerformanceFrequency(&mut self, lpFrequency_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
138         let this = self.eval_context_mut();
139
140         this.assert_target_os("windows", "QueryPerformanceFrequency");
141         this.check_no_isolation("QueryPerformanceFrequency")?;
142
143         // Retrieves the frequency of the hardware performance counter.
144         // The frequency of the performance counter is fixed at system boot and
145         // is consistent across all processors.
146         // Miri emulates a "hardware" performance counter with a resolution of 1ns,
147         // and thus 10^9 counts per second.
148         this.write_scalar(Scalar::from_i64(1_000_000_000), this.deref_operand(lpFrequency_op)?.into())?;
149         Ok(-1) // Return non-zero on success
150     }
151
152     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
153         let this = self.eval_context_ref();
154
155         this.assert_target_os("macos", "mach_absolute_time");
156         this.check_no_isolation("mach_absolute_time")?;
157
158         // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
159         // We return plain nanoseconds.
160         let duration = Instant::now().duration_since(this.machine.time_anchor);
161         u64::try_from(duration.as_nanos())
162             .map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
163     }
164 }