1+ use std:: time:: { Duration , SystemTime } ;
2+
3+ use rustc:: ty:: layout:: TyLayout ;
4+
15use crate :: stacked_borrows:: Tag ;
26use crate :: * ;
37
4- use std :: time:: { Duration , SystemTime } ;
5-
8+ // Returns the time elapsed between now and the unix epoch as a ` Duration` and the sign of the time
9+ // interval
610fn get_time ( ) -> ( Duration , i128 ) {
711 let mut sign = 1 ;
812 let duration = SystemTime :: now ( )
@@ -14,6 +18,24 @@ fn get_time() -> (Duration, i128) {
1418 ( duration, sign)
1519}
1620
21+ fn int_to_immty_checked < ' tcx > (
22+ int : i128 ,
23+ layout : TyLayout < ' tcx > ,
24+ ) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
25+ // If `int` does not fit in `size` bits, we error instead of letting
26+ // `ImmTy::from_int` panic.
27+ let size = layout. size ;
28+ let truncated = truncate ( int as u128 , size) ;
29+ if sign_extend ( truncated, size) as i128 != int {
30+ throw_unsup_format ! (
31+ "Signed value {:#x} does not fit in {} bits" ,
32+ int,
33+ size. bits( )
34+ )
35+ }
36+ Ok ( ImmTy :: from_int ( int, layout) )
37+ }
38+
1739impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
1840pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
1941 // Foreign function used by linux
@@ -45,7 +67,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4567 tv_nsec *= sign;
4668 }
4769
48- this. write_c_ints ( & tp, & [ tv_sec, tv_nsec] , & [ "time_t" , "c_long" ] ) ?;
70+ let imms = [
71+ int_to_immty_checked ( tv_sec, this. libc_ty_layout ( "time_t" ) ?) ?,
72+ int_to_immty_checked ( tv_nsec, this. libc_ty_layout ( "c_long" ) ?) ?,
73+ ] ;
74+
75+ this. write_immediates ( & tp, & imms) ?;
4976
5077 Ok ( 0 )
5178 }
@@ -78,7 +105,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
78105 tv_usec *= sign;
79106 }
80107
81- this. write_c_ints ( & tv, & [ tv_sec, tv_usec] , & [ "time_t" , "suseconds_t" ] ) ?;
108+ let imms = [
109+ int_to_immty_checked ( tv_sec, this. libc_ty_layout ( "time_t" ) ?) ?,
110+ int_to_immty_checked ( tv_usec, this. libc_ty_layout ( "suseconds_t" ) ?) ?,
111+ ] ;
112+
113+ this. write_immediates ( & tv, & imms) ?;
82114
83115 Ok ( 0 )
84116 }
0 commit comments