1 use std::time::{Duration, Instant, SystemTime};
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())
12 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
16 clk_id_op: &OpTy<'tcx, Tag>,
17 tp_op: &OpTy<'tcx, Tag>,
18 ) -> InterpResult<'tcx, i32> {
19 let this = self.eval_context_mut();
21 this.assert_target_os("linux", "clock_gettime");
22 this.check_no_isolation("`clock_gettime`")?;
24 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
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)
33 let einval = this.eval_libc("EINVAL")?;
34 this.set_last_error(einval)?;
38 let tv_sec = duration.as_secs();
39 let tv_nsec = duration.subsec_nanos();
41 this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &this.deref_operand(tp_op)?)?;
48 tv_op: &OpTy<'tcx, Tag>,
49 tz_op: &OpTy<'tcx, Tag>,
50 ) -> InterpResult<'tcx, i32> {
51 let this = self.eval_context_mut();
53 this.assert_target_os("macos", "gettimeofday");
54 this.check_no_isolation("`gettimeofday`")?;
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)?;
64 let duration = system_time_to_duration(&SystemTime::now())?;
65 let tv_sec = duration.as_secs();
66 let tv_usec = duration.subsec_micros();
68 this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &this.deref_operand(tv_op)?)?;
73 #[allow(non_snake_case)]
74 fn GetSystemTimeAsFileTime(&mut self, LPFILETIME_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx> {
75 let this = self.eval_context_mut();
77 this.assert_target_os("windows", "GetSystemTimeAsFileTime");
78 this.check_no_isolation("`GetSystemTimeAsFileTime`")?;
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;
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"))?;
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)?,
101 #[allow(non_snake_case)]
102 fn QueryPerformanceCounter(
104 lpPerformanceCount_op: &OpTy<'tcx, Tag>,
105 ) -> InterpResult<'tcx, i32> {
106 let this = self.eval_context_mut();
108 this.assert_target_os("windows", "QueryPerformanceCounter");
109 this.check_no_isolation("`QueryPerformanceCounter`")?;
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")
118 Scalar::from_i64(qpc),
119 &this.deref_operand(lpPerformanceCount_op)?.into(),
121 Ok(-1) // return non-zero on success
124 #[allow(non_snake_case)]
125 fn QueryPerformanceFrequency(
127 lpFrequency_op: &OpTy<'tcx, Tag>,
128 ) -> InterpResult<'tcx, i32> {
129 let this = self.eval_context_mut();
131 this.assert_target_os("windows", "QueryPerformanceFrequency");
132 this.check_no_isolation("`QueryPerformanceFrequency`")?;
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.
140 Scalar::from_i64(1_000_000_000),
141 &this.deref_operand(lpFrequency_op)?.into(),
143 Ok(-1) // Return non-zero on success
146 fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
147 let this = self.eval_context_ref();
149 this.assert_target_os("macos", "mach_absolute_time");
150 this.check_no_isolation("`mach_absolute_time`")?;
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")
161 fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
162 let this = self.eval_context_mut();
164 this.assert_target_os("macos", "mach_timebase_info");
165 this.check_no_isolation("`mach_timebase_info`")?;
167 let info = this.deref_operand(info_op)?;
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)?;
174 Ok(0) // KERN_SUCCESS
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.
184 let this = self.eval_context_mut();
186 this.check_no_isolation("`nanosleep`")?;
188 let duration = match this.read_timespec(&this.deref_operand(req_op)?)? {
189 Some(duration) => duration,
191 let einval = this.eval_libc("EINVAL")?;
192 this.set_last_error(einval)?;
196 let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
198 let active_thread = this.get_active_thread();
199 this.block_thread(active_thread);
201 this.register_timeout_callback(
204 Box::new(move |ecx| {
205 ecx.unblock_thread(active_thread);