@@ -17,7 +17,7 @@ use crate::*;
17
17
impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
18
18
19
19
/// Gets an instance for a path.
20
- fn resolve_did < ' mir , ' tcx > ( tcx : TyCtxt < ' tcx > , path : & [ & str ] ) -> InterpResult < ' tcx , DefId > {
20
+ fn try_resolve_did < ' mir , ' tcx > ( tcx : TyCtxt < ' tcx > , path : & [ & str ] ) -> Option < DefId > {
21
21
tcx. crates ( )
22
22
. iter ( )
23
23
. find ( |& & krate| tcx. original_crate_name ( krate) . as_str ( ) == path[ 0 ] )
@@ -40,18 +40,47 @@ fn resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> InterpResult<'tc
40
40
}
41
41
None
42
42
} )
43
- . ok_or_else ( || {
44
- err_unsup_format ! ( "failed to find required Rust item: {:?}" , path) . into ( )
45
- } )
46
43
}
47
44
48
45
pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
49
46
/// Gets an instance for a path.
50
- fn resolve_path ( & self , path : & [ & str ] ) -> InterpResult < ' tcx , ty:: Instance < ' tcx > > {
51
- Ok ( ty:: Instance :: mono (
52
- self . eval_context_ref ( ) . tcx . tcx ,
53
- resolve_did ( self . eval_context_ref ( ) . tcx . tcx , path) ?,
54
- ) )
47
+ fn resolve_path ( & self , path : & [ & str ] ) -> ty:: Instance < ' tcx > {
48
+ let did = try_resolve_did ( self . eval_context_ref ( ) . tcx . tcx , path)
49
+ . unwrap_or_else ( || panic ! ( "failed to find required Rust item: {:?}" , path) ) ;
50
+ ty:: Instance :: mono ( self . eval_context_ref ( ) . tcx . tcx , did)
51
+ }
52
+
53
+ /// Evaluates the scalar at the specified path. Returns Some(val)
54
+ /// if the path could be resolved, and None otherwise
55
+ fn eval_path_scalar (
56
+ & mut self ,
57
+ path : & [ & str ] ,
58
+ ) -> InterpResult < ' tcx , ScalarMaybeUndef < Tag > > {
59
+ let this = self . eval_context_mut ( ) ;
60
+ let instance = this. resolve_path ( path) ;
61
+ let cid = GlobalId { instance, promoted : None } ;
62
+ let const_val = this. const_eval_raw ( cid) ?;
63
+ let const_val = this. read_scalar ( const_val. into ( ) ) ?;
64
+ return Ok ( const_val) ;
65
+ }
66
+
67
+ /// Helper function to get a `libc` constant as a `Scalar`.
68
+ fn eval_libc ( & mut self , name : & str ) -> InterpResult < ' tcx , Scalar < Tag > > {
69
+ self . eval_context_mut ( )
70
+ . eval_path_scalar ( & [ "libc" , name] ) ?
71
+ . not_undef ( )
72
+ }
73
+
74
+ /// Helper function to get a `libc` constant as an `i32`.
75
+ fn eval_libc_i32 ( & mut self , name : & str ) -> InterpResult < ' tcx , i32 > {
76
+ self . eval_libc ( name) ?. to_i32 ( )
77
+ }
78
+
79
+ /// Helper function to get the `TyLayout` of a `libc` type
80
+ fn libc_ty_layout ( & mut self , name : & str ) -> InterpResult < ' tcx , TyLayout < ' tcx > > {
81
+ let this = self . eval_context_mut ( ) ;
82
+ let ty = this. resolve_path ( & [ "libc" , name] ) . monomorphic_ty ( * this. tcx ) ;
83
+ this. layout_of ( ty)
55
84
}
56
85
57
86
/// Write a 0 of the appropriate size to `dest`.
@@ -96,7 +125,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
96
125
if this. machine . communicate {
97
126
// Fill the buffer using the host's rng.
98
127
getrandom:: getrandom ( & mut data)
99
- . map_err ( |err| err_unsup_format ! ( "getrandom failed: {}" , err) ) ?;
128
+ . map_err ( |err| err_unsup_format ! ( "host getrandom failed: {}" , err) ) ?;
100
129
} else {
101
130
let rng = this. memory . extra . rng . get_mut ( ) ;
102
131
rng. fill_bytes ( & mut data) ;
@@ -311,26 +340,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
311
340
}
312
341
}
313
342
314
- /// Helper function to get a `libc` constant as a `Scalar`.
315
- fn eval_libc ( & mut self , name : & str ) -> InterpResult < ' tcx , Scalar < Tag > > {
316
- self . eval_context_mut ( )
317
- . eval_path_scalar ( & [ "libc" , name] ) ?
318
- . ok_or_else ( || err_unsup_format ! ( "Path libc::{} cannot be resolved." , name) ) ?
319
- . not_undef ( )
320
- }
321
-
322
- /// Helper function to get a `libc` constant as an `i32`.
323
- fn eval_libc_i32 ( & mut self , name : & str ) -> InterpResult < ' tcx , i32 > {
324
- self . eval_libc ( name) ?. to_i32 ( )
325
- }
326
-
327
- /// Helper function to get the `TyLayout` of a `libc` type
328
- fn libc_ty_layout ( & mut self , name : & str ) -> InterpResult < ' tcx , TyLayout < ' tcx > > {
329
- let this = self . eval_context_mut ( ) ;
330
- let ty = this. resolve_path ( & [ "libc" , name] ) ?. monomorphic_ty ( * this. tcx ) ;
331
- this. layout_of ( ty)
332
- }
333
-
334
343
// Writes several `ImmTy`s contiguosly into memory. This is useful when you have to pack
335
344
// different values into a struct.
336
345
fn write_packed_immediates (
@@ -529,7 +538,7 @@ pub fn immty_from_int_checked<'tcx>(
529
538
) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
530
539
let int = int. into ( ) ;
531
540
Ok ( ImmTy :: try_from_int ( int, layout) . ok_or_else ( || {
532
- err_unsup_format ! ( "Signed value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
541
+ err_unsup_format ! ( "signed value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
533
542
} ) ?)
534
543
}
535
544
@@ -539,6 +548,6 @@ pub fn immty_from_uint_checked<'tcx>(
539
548
) -> InterpResult < ' tcx , ImmTy < ' tcx , Tag > > {
540
549
let int = int. into ( ) ;
541
550
Ok ( ImmTy :: try_from_uint ( int, layout) . ok_or_else ( || {
542
- err_unsup_format ! ( "Signed value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
551
+ err_unsup_format ! ( "unsigned value {:#x} does not fit in {} bits" , int, layout. size. bits( ) )
543
552
} ) ?)
544
553
}
0 commit comments