1- use std:: time:: { Duration , SystemTime } ;
1+ use std:: time:: { Duration , SystemTime , Instant } ;
22
33use crate :: stacked_borrows:: Tag ;
44use crate :: * ;
55use helpers:: immty_from_int_checked;
66
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-
127/// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
138pub fn system_time_to_duration < ' tcx > ( time : & SystemTime ) -> InterpResult < ' tcx , Duration > {
149 time. duration_since ( SystemTime :: UNIX_EPOCH )
@@ -28,15 +23,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2823 this. check_no_isolation ( "clock_gettime" ) ?;
2924
3025 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 {
3235 let einval = this. eval_libc ( "EINVAL" ) ?;
3336 this. set_last_error ( einval) ?;
3437 return Ok ( -1 ) ;
35- }
36-
37- let tp = this. deref_operand ( tp_op) ?;
38+ } ;
3839
39- let duration = get_time ( ) ?;
4040 let tv_sec = duration. as_secs ( ) ;
4141 let tv_nsec = duration. subsec_nanos ( ) ;
4242
@@ -68,7 +68,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6868
6969 let tv = this. deref_operand ( tv_op) ?;
7070
71- let duration = get_time ( ) ?;
71+ let duration = system_time_to_duration ( & SystemTime :: now ( ) ) ?;
7272 let tv_sec = duration. as_secs ( ) ;
7373 let tv_usec = duration. subsec_micros ( ) ;
7474
0 commit comments