1 use crate::stacked_borrows::Tag;
4 use std::time::{Duration, SystemTime};
6 fn get_time() -> (Duration, i128) {
8 let duration = SystemTime::now()
9 .duration_since(SystemTime::UNIX_EPOCH)
17 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
18 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
21 clk_id_op: OpTy<'tcx, Tag>,
22 tp_op: OpTy<'tcx, Tag>,
23 ) -> InterpResult<'tcx, i32> {
24 let this = self.eval_context_mut();
26 if !this.machine.communicate {
27 throw_unsup_format!("`clock_gettime` not available when isolation is enabled")
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.force_ptr(this.read_scalar(tp_op)?.not_undef()?)?;
39 let (duration, sign) = get_time();
40 let tv_sec = sign * (duration.as_secs() as i128);
41 let tv_nsec = duration.subsec_nanos() as i128;
42 this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?;
49 tv_op: OpTy<'tcx, Tag>,
50 tz_op: OpTy<'tcx, Tag>,
51 ) -> InterpResult<'tcx, i32> {
52 let this = self.eval_context_mut();
54 if !this.machine.communicate {
55 throw_unsup_format!("`gettimeofday` not available when isolation is enabled")
57 // Using tz is obsolete and should always be null
58 let tz = this.read_scalar(tz_op)?.not_undef()?;
59 if !this.is_null(tz)? {
60 let einval = this.eval_libc("EINVAL")?;
61 this.set_last_error(einval)?;
65 let tv = this.force_ptr(this.read_scalar(tv_op)?.not_undef()?)?;
67 let (duration, sign) = get_time();
68 let tv_sec = sign * (duration.as_secs() as i128);
69 let tv_usec = duration.subsec_micros() as i128;
71 this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?;