1use std::ffi::{OsStr, OsString};
2use std::fmt::Write;
3use std::str::FromStr;
4use std::time::{Duration, SystemTime};
5
6use chrono::{DateTime, Datelike, Offset, Timelike, Utc};
7use chrono_tz::Tz;
8
9use crate::*;
10
11pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
13 time.duration_since(SystemTime::UNIX_EPOCH)
14 .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported"))
15 .into()
16}
17
18impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
19pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
20 fn clock_gettime(
21 &mut self,
22 clk_id_op: &OpTy<'tcx>,
23 tp_op: &OpTy<'tcx>,
24 ) -> InterpResult<'tcx, Scalar> {
25 let this = self.eval_context_mut();
30
31 this.assert_target_os_is_unix("clock_gettime");
32
33 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
34 let tp = this.deref_pointer_as(tp_op, this.libc_ty_layout("timespec"))?;
35
36 let absolute_clocks;
37 let mut relative_clocks;
38
39 match this.tcx.sess.target.os.as_ref() {
40 "linux" | "freebsd" | "android" => {
41 absolute_clocks = vec![
46 this.eval_libc_i32("CLOCK_REALTIME"),
47 this.eval_libc_i32("CLOCK_REALTIME_COARSE"),
48 ];
49 relative_clocks = vec![
53 this.eval_libc_i32("CLOCK_MONOTONIC"),
54 this.eval_libc_i32("CLOCK_MONOTONIC_COARSE"),
55 ];
56 }
57 "macos" => {
58 absolute_clocks = vec![this.eval_libc_i32("CLOCK_REALTIME")];
59 relative_clocks = vec![this.eval_libc_i32("CLOCK_MONOTONIC")];
60 relative_clocks.push(this.eval_libc_i32("CLOCK_UPTIME_RAW"));
64 }
65 "solaris" | "illumos" => {
66 absolute_clocks = vec![this.eval_libc_i32("CLOCK_REALTIME")];
68 relative_clocks = vec![this.eval_libc_i32("CLOCK_MONOTONIC")];
74 }
75 target => throw_unsup_format!("`clock_gettime` is not supported on target OS {target}"),
76 }
77
78 let duration = if absolute_clocks.contains(&clk_id) {
79 this.check_no_isolation("`clock_gettime` with `REALTIME` clocks")?;
80 system_time_to_duration(&SystemTime::now())?
81 } else if relative_clocks.contains(&clk_id) {
82 this.machine.clock.now().duration_since(this.machine.clock.epoch())
83 } else {
84 return this.set_last_error_and_return_i32(LibcError("EINVAL"));
85 };
86
87 let tv_sec = duration.as_secs();
88 let tv_nsec = duration.subsec_nanos();
89
90 this.write_int_fields(&[tv_sec.into(), tv_nsec.into()], &tp)?;
91
92 interp_ok(Scalar::from_i32(0))
93 }
94
95 fn gettimeofday(
96 &mut self,
97 tv_op: &OpTy<'tcx>,
98 tz_op: &OpTy<'tcx>,
99 ) -> InterpResult<'tcx, Scalar> {
100 let this = self.eval_context_mut();
101
102 this.assert_target_os_is_unix("gettimeofday");
103 this.check_no_isolation("`gettimeofday`")?;
104
105 let tv = this.deref_pointer_as(tv_op, this.libc_ty_layout("timeval"))?;
106
107 let tz = this.read_pointer(tz_op)?;
109 if !this.ptr_is_null(tz)? {
110 return this.set_last_error_and_return_i32(LibcError("EINVAL"));
111 }
112
113 let duration = system_time_to_duration(&SystemTime::now())?;
114 let tv_sec = duration.as_secs();
115 let tv_usec = duration.subsec_micros();
116
117 this.write_int_fields(&[tv_sec.into(), tv_usec.into()], &tv)?;
118
119 interp_ok(Scalar::from_i32(0))
120 }
121
122 fn localtime_r(
126 &mut self,
127 timep: &OpTy<'tcx>,
128 result_op: &OpTy<'tcx>,
129 ) -> InterpResult<'tcx, Pointer> {
130 let this = self.eval_context_mut();
131
132 this.assert_target_os_is_unix("localtime_r");
133 this.check_no_isolation("`localtime_r`")?;
134
135 let time_layout = this.libc_ty_layout("time_t");
136 let timep = this.deref_pointer_as(timep, time_layout)?;
137 let result = this.deref_pointer_as(result_op, this.libc_ty_layout("tm"))?;
138
139 let sec_since_epoch: i64 =
142 this.read_scalar(&timep)?.to_int(time_layout.size)?.try_into().unwrap();
143 let dt_utc: DateTime<Utc> =
144 DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
145
146 let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
148 let tz = match tz.into_string() {
149 Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
150 _ => Tz::UTC,
151 };
152
153 let dt: DateTime<Tz> = dt_utc.with_timezone(&tz);
155
156 let tm_isdst = -1;
160 this.write_int_fields_named(
161 &[
162 ("tm_sec", dt.second().into()),
163 ("tm_min", dt.minute().into()),
164 ("tm_hour", dt.hour().into()),
165 ("tm_mday", dt.day().into()),
166 ("tm_mon", dt.month0().into()),
167 ("tm_year", dt.year().strict_sub(1900).into()),
168 ("tm_wday", dt.weekday().num_days_from_sunday().into()),
169 ("tm_yday", dt.ordinal0().into()),
170 ("tm_isdst", tm_isdst),
171 ],
172 &result,
173 )?;
174
175 if !matches!(&*this.tcx.sess.target.os, "solaris" | "illumos") {
179 let offset_in_seconds = dt.offset().fix().local_minus_utc();
183 let tm_gmtoff = offset_in_seconds;
184 let mut tm_zone = String::new();
185 if offset_in_seconds < 0 {
186 tm_zone.push('-');
187 } else {
188 tm_zone.push('+');
189 }
190 let offset_hour = offset_in_seconds.abs() / 3600;
191 write!(tm_zone, "{:02}", offset_hour).unwrap();
192 let offset_min = (offset_in_seconds.abs() % 3600) / 60;
193 if offset_min != 0 {
194 write!(tm_zone, "{:02}", offset_min).unwrap();
195 }
196
197 tm_zone.push('\0');
199
200 let tm_zone_ptr = this.allocate_bytes_dedup(tm_zone.as_bytes())?;
202
203 this.write_pointer(tm_zone_ptr, &this.project_field_named(&result, "tm_zone")?)?;
205 this.write_int_fields_named(&[("tm_gmtoff", tm_gmtoff.into())], &result)?;
206 }
207 interp_ok(result.ptr())
208 }
209 #[allow(non_snake_case, clippy::arithmetic_side_effects)]
210 fn GetSystemTimeAsFileTime(
211 &mut self,
212 shim_name: &str,
213 LPFILETIME_op: &OpTy<'tcx>,
214 ) -> InterpResult<'tcx> {
215 let this = self.eval_context_mut();
216
217 this.assert_target_os("windows", shim_name);
218 this.check_no_isolation(shim_name)?;
219
220 let filetime = this.deref_pointer_as(LPFILETIME_op, this.windows_ty_layout("FILETIME"))?;
221
222 let NANOS_PER_SEC = this.eval_windows_u64("time", "NANOS_PER_SEC");
223 let INTERVALS_PER_SEC = this.eval_windows_u64("time", "INTERVALS_PER_SEC");
224 let INTERVALS_TO_UNIX_EPOCH = this.eval_windows_u64("time", "INTERVALS_TO_UNIX_EPOCH");
225 let NANOS_PER_INTERVAL = NANOS_PER_SEC / INTERVALS_PER_SEC;
226 let SECONDS_TO_UNIX_EPOCH = INTERVALS_TO_UNIX_EPOCH / INTERVALS_PER_SEC;
227
228 let duration = system_time_to_duration(&SystemTime::now())?
229 + Duration::from_secs(SECONDS_TO_UNIX_EPOCH);
230 let duration_ticks = u64::try_from(duration.as_nanos() / u128::from(NANOS_PER_INTERVAL))
231 .map_err(|_| err_unsup_format!("programs running more than 2^64 Windows ticks after the Windows epoch are not supported"))?;
232
233 let dwLowDateTime = u32::try_from(duration_ticks & 0x00000000FFFFFFFF).unwrap();
234 let dwHighDateTime = u32::try_from((duration_ticks & 0xFFFFFFFF00000000) >> 32).unwrap();
235 this.write_int_fields(&[dwLowDateTime.into(), dwHighDateTime.into()], &filetime)?;
236
237 interp_ok(())
238 }
239
240 #[allow(non_snake_case)]
241 fn QueryPerformanceCounter(
242 &mut self,
243 lpPerformanceCount_op: &OpTy<'tcx>,
244 ) -> InterpResult<'tcx, Scalar> {
245 let this = self.eval_context_mut();
246
247 this.assert_target_os("windows", "QueryPerformanceCounter");
248
249 let duration = this.machine.clock.now().duration_since(this.machine.clock.epoch());
252 let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
253 err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
254 })?;
255 this.write_scalar(
256 Scalar::from_i64(qpc),
257 &this.deref_pointer_as(lpPerformanceCount_op, this.machine.layouts.i64)?,
258 )?;
259 interp_ok(Scalar::from_i32(-1)) }
261
262 #[allow(non_snake_case)]
263 fn QueryPerformanceFrequency(
264 &mut self,
265 lpFrequency_op: &OpTy<'tcx>,
266 ) -> InterpResult<'tcx, Scalar> {
267 let this = self.eval_context_mut();
268
269 this.assert_target_os("windows", "QueryPerformanceFrequency");
270
271 this.write_scalar(
277 Scalar::from_i64(1_000_000_000),
278 &this.deref_pointer_as(lpFrequency_op, this.machine.layouts.u64)?,
279 )?;
280 interp_ok(Scalar::from_i32(-1)) }
282
283 fn mach_absolute_time(&self) -> InterpResult<'tcx, Scalar> {
284 let this = self.eval_context_ref();
285
286 this.assert_target_os("macos", "mach_absolute_time");
287
288 let duration = this.machine.clock.now().duration_since(this.machine.clock.epoch());
291 let res = u64::try_from(duration.as_nanos()).map_err(|_| {
292 err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported")
293 })?;
294 interp_ok(Scalar::from_u64(res))
295 }
296
297 fn mach_timebase_info(&mut self, info_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
298 let this = self.eval_context_mut();
299
300 this.assert_target_os("macos", "mach_timebase_info");
301
302 let info = this.deref_pointer_as(info_op, this.libc_ty_layout("mach_timebase_info"))?;
303
304 let (numer, denom) = (1, 1);
307 this.write_int_fields(&[numer.into(), denom.into()], &info)?;
308
309 interp_ok(Scalar::from_i32(0)) }
311
312 fn nanosleep(
313 &mut self,
314 req_op: &OpTy<'tcx>,
315 _rem: &OpTy<'tcx>, ) -> InterpResult<'tcx, Scalar> {
317 let this = self.eval_context_mut();
318
319 this.assert_target_os_is_unix("nanosleep");
320
321 let req = this.deref_pointer_as(req_op, this.libc_ty_layout("timespec"))?;
322
323 let duration = match this.read_timespec(&req)? {
324 Some(duration) => duration,
325 None => {
326 return this.set_last_error_and_return_i32(LibcError("EINVAL"));
327 }
328 };
329
330 this.block_thread(
331 BlockReason::Sleep,
332 Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
333 callback!(
334 @capture<'tcx> {}
335 |_this, unblock: UnblockKind| {
336 assert_eq!(unblock, UnblockKind::TimedOut);
337 interp_ok(())
338 }
339 ),
340 );
341 interp_ok(Scalar::from_i32(0))
342 }
343
344 #[allow(non_snake_case)]
345 fn Sleep(&mut self, timeout: &OpTy<'tcx>) -> InterpResult<'tcx> {
346 let this = self.eval_context_mut();
347
348 this.assert_target_os("windows", "Sleep");
349
350 let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
351
352 let duration = Duration::from_millis(timeout_ms.into());
353
354 this.block_thread(
355 BlockReason::Sleep,
356 Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration)),
357 callback!(
358 @capture<'tcx> {}
359 |_this, unblock: UnblockKind| {
360 assert_eq!(unblock, UnblockKind::TimedOut);
361 interp_ok(())
362 }
363 ),
364 );
365 interp_ok(())
366 }
367}