]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
platform -> target
[rust.git] / src / shims / time.rs
1 use std::time::{Duration, SystemTime, Instant};
2 use std::convert::TryFrom;
3
4 use crate::stacked_borrows::Tag;
5 use crate::*;
6 use helpers::immty_from_int_checked;
7
8 /// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
9 pub fn system_time_to_duration<'tcx>(time: &SystemTime) -> InterpResult<'tcx, Duration> {
10     time.duration_since(SystemTime::UNIX_EPOCH)
11         .map_err(|_| err_unsup_format!("times before the Unix epoch are not supported").into())
12 }
13
14 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
15 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
16     fn clock_gettime(
17         &mut self,
18         clk_id_op: OpTy<'tcx, Tag>,
19         tp_op: OpTy<'tcx, Tag>,
20     ) -> InterpResult<'tcx, i32> {
21         let this = self.eval_context_mut();
22
23         this.assert_target_os("linux", "clock_gettime");
24         this.check_no_isolation("clock_gettime")?;
25
26         let clk_id = this.read_scalar(clk_id_op)?.to_i32()?;
27         let tp = this.deref_operand(tp_op)?;
28
29         let duration = if clk_id == this.eval_libc_i32("CLOCK_REALTIME")? {
30             system_time_to_duration(&SystemTime::now())?
31         } else if clk_id == this.eval_libc_i32("CLOCK_MONOTONIC")? {
32             // Absolute time does not matter, only relative time does, so we can just
33             // use our own time anchor here.
34             Instant::now().duration_since(this.machine.time_anchor)
35         } else {
36             let einval = this.eval_libc("EINVAL")?;
37             this.set_last_error(einval)?;
38             return Ok(-1);
39         };
40
41         let tv_sec = duration.as_secs();
42         let tv_nsec = duration.subsec_nanos();
43
44         let imms = [
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")?)?,
47         ];
48
49         this.write_packed_immediates(tp, &imms)?;
50
51         Ok(0)
52     }
53
54     fn gettimeofday(
55         &mut self,
56         tv_op: OpTy<'tcx, Tag>,
57         tz_op: OpTy<'tcx, Tag>,
58     ) -> InterpResult<'tcx, i32> {
59         let this = self.eval_context_mut();
60
61         this.assert_target_os("macos", "gettimeofday");
62         this.check_no_isolation("gettimeofday")?;
63
64         // Using tz is obsolete and should always be null
65         let tz = this.read_scalar(tz_op)?.not_undef()?;
66         if !this.is_null(tz)? {
67             let einval = this.eval_libc("EINVAL")?;
68             this.set_last_error(einval)?;
69             return Ok(-1);
70         }
71
72         let tv = this.deref_operand(tv_op)?;
73
74         let duration = system_time_to_duration(&SystemTime::now())?;
75         let tv_sec = duration.as_secs();
76         let tv_usec = duration.subsec_micros();
77
78         let imms = [
79             immty_from_int_checked(tv_sec, this.libc_ty_layout("time_t")?)?,
80             immty_from_int_checked(tv_usec, this.libc_ty_layout("suseconds_t")?)?,
81         ];
82
83         this.write_packed_immediates(tv, &imms)?;
84
85         Ok(0)
86     }
87
88     fn mach_absolute_time(&self) -> InterpResult<'tcx, u64> {
89         let this = self.eval_context_ref();
90
91         this.assert_target_os("macos", "mach_absolute_time");
92         this.check_no_isolation("mach_absolute_time")?;
93
94         // This returns a u64, with time units determined dynamically by `mach_timebase_info`.
95         // We return plain nanoseconds.
96         let duration = Instant::now().duration_since(this.machine.time_anchor);
97         u64::try_from(duration.as_nanos())
98             .map_err(|_| err_unsup_format!("programs running longer than 2^64 nanoseconds are not supported").into())
99     }
100 }