]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
avoid using unchecked casts or arithmetic
[rust.git] / src / shims / time.rs
1 use std::time::{Duration, SystemTime};
2
3 use crate::stacked_borrows::Tag;
4 use crate::*;
5 use helpers::immty_from_int_checked;
6
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())
10 }
11
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())
16 }
17
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
21     fn clock_gettime(
22         &mut self,
23         clk_id_op: OpTy<'tcx, Tag>,
24         tp_op: OpTy<'tcx, Tag>,
25     ) -> InterpResult<'tcx, i32> {
26         let this = self.eval_context_mut();
27
28         this.check_no_isolation("clock_gettime")?;
29
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)?;
34             return Ok(-1);
35         }
36
37         let tp = this.deref_operand(tp_op)?;
38
39         let duration = get_time()?;
40         let tv_sec = duration.as_secs();
41         let tv_nsec = duration.subsec_nanos();
42
43         let imms = [
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")?)?,
46         ];
47
48         this.write_packed_immediates(tp, &imms)?;
49
50         Ok(0)
51     }
52     // Foreign function used by generic unix (in particular macOS)
53     fn gettimeofday(
54         &mut self,
55         tv_op: OpTy<'tcx, Tag>,
56         tz_op: OpTy<'tcx, Tag>,
57     ) -> InterpResult<'tcx, i32> {
58         let this = self.eval_context_mut();
59
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)?;
66             return Ok(-1);
67         }
68
69         let tv = this.deref_operand(tv_op)?;
70
71         let duration = get_time()?;
72         let tv_sec = duration.as_secs();
73         let tv_usec = duration.subsec_micros();
74
75         let imms = [
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")?)?,
78         ];
79
80         this.write_packed_immediates(tv, &imms)?;
81
82         Ok(0)
83     }
84 }