1 use std::convert::TryFrom;
2 use std::time::{Duration, Instant, SystemTime};
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())
13 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
14 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
17 clk_id_op: &OpTy<'tcx, Tag>,
18 tp_op: &OpTy<'tcx, Tag>,
19 ) -> InterpResult<'tcx, i32> {
20 let this = self.eval_context_mut();
22 this.assert_target_os("linux", "clock_gettime");
23 this.check_no_isolation("`clock_gettime`")?;
25 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
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)
34 let einval = this.eval_libc("EINVAL")?;
35 this.set_last_error(einval)?;
39 let tv_sec = duration.as_secs();
40 let tv_nsec = duration.subsec_nanos();
42 this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
49 tv_op: &OpTy<'tcx, Tag>,
50 tz_op: &OpTy<'tcx, Tag>,
51 ) -> InterpResult<'tcx, i32> {
52 let this = self.eval_context_mut();
54 this.assert_target_os("macos", "gettimeofday");
55 this.check_no_isolation("`gettimeofday`")?;
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)?;
65 let duration = system_time_to_duration(&SystemTime::now())?;
66 let tv_sec = duration.as_secs();
67 let tv_usec = duration.subsec_micros();
69 this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
74 #[allow(non_snake_case)]
75 fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
76 let this = self.eval_context_mut();
78 this.assert_target_os("windows", "GetSystemTimeAsFileTime");
79 this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
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;
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"))?;
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)?,
102 #[allow(non_snake_case)]
103 fn QueryPerformanceCounter(
105 lpPerformanceCount_op: &OpTy<'tcx, Tag>,
106 ) -> InterpResult<'tcx, i32> {
107 let this = self.eval_context_mut();
109 this.assert_target_os("windows", "QueryPerformanceCounter");
110 this.check_no_isolation("`QueryPerformanceCounter`")?;
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")
119 Scalar::from_i64(qpc),
120 &this.deref_operand(lpPerformanceCount_op)?.into(),
122 Ok(-1) // return non-zero on success
125 #[allow(non_snake_case)]
126 fn QueryPerformanceFrequency(
128 lpFrequency_op: &OpTy<'tcx, Tag>,
129 ) -> InterpResult<'tcx, i32> {
130 let this = self.eval_context_mut();
132 this.assert_target_os("windows", "QueryPerformanceFrequency");
133 this.check_no_isolation("`QueryPerformanceFrequency`")?;
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.
141 Scalar::from_i64(1_000_000_000),
142 &this.deref_operand(lpFrequency_op)?.into(),
144 Ok(-1) // Return non-zero on success
147 fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
148 let this = self.eval_context_ref();
150 this.assert_target_os("macos", "mach_absolute_time");
151 this.check_no_isolation("`mach_absolute_time`")?;
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")
162 fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
163 let this = self.eval_context_mut();
165 this.assert_target_os("macos", "mach_timebase_info");
166 this.check_no_isolation("`mach_timebase_info`")?;
168 let info = this.deref_operand(info_op)?;
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)?;
175 Ok(0) // KERN_SUCCESS
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.
185 let this = self.eval_context_mut();
187 this.check_no_isolation("`nanosleep`")?;
189 let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
190 Some(duration) => duration,
192 let einval = this.eval_libc("EINVAL")?;
193 this.set_last_error(einval)?;
197 let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
199 let active_thread = this.get_active_thread();
200 this.block_thread(active_thread);
202 this.register_timeout_callback(
205 Box::new(move |ecx| {
206 ecx.unblock_thread(active_thread);