1
+ use std:: time:: { Duration , SystemTime } ;
2
+
3
+ use rustc:: ty:: layout:: TyLayout ;
4
+
1
5
use crate :: stacked_borrows:: Tag ;
2
6
use crate :: * ;
3
7
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
6
10
fn get_time ( ) -> ( Duration , i128 ) {
7
11
let mut sign = 1 ;
8
12
let duration = SystemTime :: now ( )
@@ -14,6 +18,24 @@ fn get_time() -> (Duration, i128) {
14
18
( duration, sign)
15
19
}
16
20
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
+
17
39
impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
18
40
pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
19
41
// Foreign function used by linux
@@ -45,7 +67,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
45
67
tv_nsec *= sign;
46
68
}
47
69
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) ?;
49
76
50
77
Ok ( 0 )
51
78
}
@@ -78,7 +105,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
78
105
tv_usec *= sign;
79
106
}
80
107
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) ?;
82
114
83
115
Ok ( 0 )
84
116
}
0 commit comments