@@ -16,6 +16,7 @@ fn get_time() -> (Duration, i128) {
1616
1717impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
1818pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
19+ // Foreign function used by linux
1920 fn clock_gettime (
2021 & mut self ,
2122 clk_id_op : OpTy < ' tcx , Tag > ,
@@ -38,12 +39,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3839
3940 let ( duration, sign) = get_time ( ) ;
4041 let tv_sec = sign * ( duration. as_secs ( ) as i128 ) ;
41- let tv_nsec = duration. subsec_nanos ( ) as i128 ;
42+ let mut tv_nsec = duration. subsec_nanos ( ) as i128 ;
43+ // If the number of seconds is zero, we need to put the sign into the second's fraction.
44+ if tv_sec == 0 && sign < 0 {
45+ tv_nsec *= sign;
46+ }
47+
4248 this. write_c_ints ( & tp, & [ tv_sec, tv_nsec] , & [ "time_t" , "c_long" ] ) ?;
4349
4450 Ok ( 0 )
4551 }
46-
52+ // Foreign function used by generic unix
4753 fn gettimeofday (
4854 & mut self ,
4955 tv_op : OpTy < ' tcx , Tag > ,
@@ -66,7 +72,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6672
6773 let ( duration, sign) = get_time ( ) ;
6874 let tv_sec = sign * ( duration. as_secs ( ) as i128 ) ;
69- let tv_usec = duration. subsec_micros ( ) as i128 ;
75+ let mut tv_usec = duration. subsec_micros ( ) as i128 ;
76+ // If the number of seconds is zero, we need to put the sign into the second's fraction.
77+ if tv_sec == 0 && sign < 0 {
78+ tv_usec *= sign;
79+ }
7080
7181 this. write_c_ints ( & tv, & [ tv_sec, tv_usec] , & [ "time_t" , "suseconds_t" ] ) ?;
7282
0 commit comments