1 use std::time::{Duration, SystemTime};
3 use crate::stacked_borrows::Tag;
5 use helpers::immty_from_int_checked;
7 // Returns the time elapsed between now and the unix epoch as a `Duration`.
8 fn get_time<'tcx>() -> InterpResult<'tcx, Duration> {
9 system_time_to_duration(&SystemTime::now())
12 // Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
13 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
15 .duration_since(SystemTime::UNIX_EPOCH)
16 .map_err(|_| err_unsup_format!("Times before the Unix epoch are not supported").into())
19 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
21 // Foreign function used by linux
24 clk_id_op: OpTy<'tcx, Tag>,
25 tp_op: OpTy<'tcx, Tag>,
26 ) -> InterpResult<'tcx, i32> {
27 let this = self.eval_context_mut();
29 this.check_no_isolation("clock_gettime")?;
31 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
32 if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
33 let einval = this.eval_libc("EINVAL")?;
34 this.set_last_error(einval)?;
38 let tp = this.deref_operand(tp_op)?;
40 let duration = get_time()?;
41 let tv_sec = duration.as_secs() as i128;
42 let tv_nsec = duration.subsec_nanos() as i128;
45 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
46 immty_from_int_checked(tv_nsec, this.libc_ty_layout("c_long")?)?,
49 this.write_packed_immediates(&tp, &imms)?;
53 // Foreign function used by generic unix (in particular macOS)
56 tv_op: OpTy<'tcx, Tag>,
57 tz_op: OpTy<'tcx, Tag>,
58 ) -> InterpResult<'tcx, i32> {
59 let this = self.eval_context_mut();
61 this.check_no_isolation("gettimeofday")?;
62 // Using tz is obsolete and should always be null
63 let tz = this.read_scalar(tz_op)?.not_undef()?;
64 if !this.is_null(tz)? {
65 let einval = this.eval_libc("EINVAL")?;
66 this.set_last_error(einval)?;
70 let tv = this.deref_operand(tv_op)?;
72 let duration = get_time()?;
73 let tv_sec = duration.as_secs() as i128;
74 let tv_usec = duration.subsec_micros() as i128;
77 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
78 immty_from_int_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?,
81 this.write_packed_immediates(&tv, &imms)?;