1
- use std:: time:: { Duration , SystemTime } ;
1
+ use std:: time:: { Duration , SystemTime , Instant } ;
2
2
3
3
use crate :: stacked_borrows:: Tag ;
4
4
use crate :: * ;
5
5
use helpers:: immty_from_int_checked;
6
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
7
/// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
13
8
pub fn system_time_to_duration < ' tcx > ( time : & SystemTime ) -> InterpResult < ' tcx , Duration > {
14
9
time. duration_since ( SystemTime :: UNIX_EPOCH )
@@ -28,15 +23,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
28
23
this. check_no_isolation ( "clock_gettime" ) ?;
29
24
30
25
let clk_id = this. read_scalar ( clk_id_op) ?. to_i32 ( ) ?;
31
- if clk_id != this. eval_libc_i32 ( "CLOCK_REALTIME" ) ? {
26
+ let tp = this. deref_operand ( tp_op) ?;
27
+
28
+ let duration = if clk_id == this. eval_libc_i32 ( "CLOCK_REALTIME" ) ? {
29
+ system_time_to_duration ( & SystemTime :: now ( ) ) ?
30
+ } else if clk_id == this. eval_libc_i32 ( "CLOCK_MONOTONIC" ) ? {
31
+ // Absolute time does not matter, only relative time does, so we can just
32
+ // use our own time anchor here.
33
+ Instant :: now ( ) . duration_since ( this. machine . time_anchor )
34
+ } else {
32
35
let einval = this. eval_libc ( "EINVAL" ) ?;
33
36
this. set_last_error ( einval) ?;
34
37
return Ok ( -1 ) ;
35
- }
36
-
37
- let tp = this. deref_operand ( tp_op) ?;
38
+ } ;
38
39
39
- let duration = get_time ( ) ?;
40
40
let tv_sec = duration. as_secs ( ) ;
41
41
let tv_nsec = duration. subsec_nanos ( ) ;
42
42
@@ -68,7 +68,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
68
68
69
69
let tv = this. deref_operand ( tv_op) ?;
70
70
71
- let duration = get_time ( ) ?;
71
+ let duration = system_time_to_duration ( & SystemTime :: now ( ) ) ?;
72
72
let tv_sec = duration. as_secs ( ) ;
73
73
let tv_usec = duration. subsec_micros ( ) ;
74
74
0 commit comments