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> {
14 time.duration_since(SystemTime::UNIX_EPOCH)
15 .map_err(|_| err_unsup_format!("Times before the Unix epoch are not supported").into())
18 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
19 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
20 // Foreign function used by linux
23 clk_id_op: OpTy<'tcx, Tag>,
24 tp_op: OpTy<'tcx, Tag>,
25 ) -> InterpResult<'tcx, i32> {
26 let this = self.eval_context_mut();
28 this.check_no_isolation("clock_gettime")?;
30 let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
31 if clk_id != this.eval_libc_i32("CLOCK_REALTIME")? {
32 let einval = this.eval_libc("EINVAL")?;
33 this.set_last_error(einval)?;
37 let tp = this.deref_operand(tp_op)?;
39 let duration = get_time()?;
40 let tv_sec = duration.as_secs();
41 let tv_nsec = duration.subsec_nanos();
44 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
45 immty_from_int_checked(tv_nsec, this.libc_ty_layout("c_long")?)?,
48 this.write_packed_immediates(tp, &imms)?;
52 // Foreign function used by generic unix (in particular macOS)
55 tv_op: OpTy<'tcx, Tag>,
56 tz_op: OpTy<'tcx, Tag>,
57 ) -> InterpResult<'tcx, i32> {
58 let this = self.eval_context_mut();
60 this.check_no_isolation("gettimeofday")?;
61 // Using tz is obsolete and should always be null
62 let tz = this.read_scalar(tz_op)?.not_undef()?;
63 if !this.is_null(tz)? {
64 let einval = this.eval_libc("EINVAL")?;
65 this.set_last_error(einval)?;
69 let tv = this.deref_operand(tv_op)?;
71 let duration = get_time()?;
72 let tv_sec = duration.as_secs();
73 let tv_usec = duration.subsec_micros();
76 immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
77 immty_from_int_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?,
80 this.write_packed_immediates(tv, &imms)?;