]> git.lizzy.rs Git - rust.git/blob - src/shims/time.rs
add `statx` shim for linux
[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
15         .duration_since(SystemTime::UNIX_EPOCH)
16         .map_err(|_| err_unsup_format!("Times before the Unix epoch are not supported").into())
17 }
18
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
22     fn clock_gettime(
23         &mut self,
24         clk_id_op: OpTy<'tcx, Tag>,
25         tp_op: OpTy<'tcx, Tag>,
26     ) -> InterpResult<'tcx, i32> {
27         let this = self.eval_context_mut();
28
29         this.check_no_isolation("clock_gettime")?;
30
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)?;
35             return Ok(-1);
36         }
37
38         let tp = this.deref_operand(tp_op)?;
39
40         let duration = get_time()?;
41         let tv_sec = duration.as_secs() as i128;
42         let tv_nsec = duration.subsec_nanos() as i128;
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     // Foreign function used by generic unix (in particular macOS)
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.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)?;
67             return Ok(-1);
68         }
69
70         let tv = this.deref_operand(tv_op)?;
71
72         let duration = get_time()?;
73         let tv_sec = duration.as_secs() as i128;
74         let tv_usec = duration.subsec_micros() as i128;
75
76         let imms = [
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")?)?,
79         ];
80
81         this.write_packed_immediates(&tv, &imms)?;
82
83         Ok(0)
84     }
85 }