@@ -286,7 +286,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
286
286
// neither of which have any effect on our current PRNG
287
287
let _flags = this. read_scalar ( args[ 3 ] ) ?. to_i32 ( ) ?;
288
288
289
- gen_random ( this , len as usize , ptr) ?;
289
+ this . gen_random ( len as usize , ptr) ?;
290
290
this. write_scalar ( Scalar :: from_uint ( len, dest. layout . size ) , dest) ?;
291
291
}
292
292
id => {
@@ -303,10 +303,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
303
303
let symbol_name = this. memory ( ) . get ( symbol. alloc_id ) ?. read_c_str ( tcx, symbol) ?;
304
304
let err = format ! ( "bad c unicode symbol: {:?}" , symbol_name) ;
305
305
let symbol_name = :: std:: str:: from_utf8 ( symbol_name) . unwrap_or ( & err) ;
306
- return err ! ( Unimplemented ( format!(
307
- "miri does not support dynamically loading libraries (requested symbol: {})" ,
308
- symbol_name
309
- ) ) ) ;
306
+ if let Some ( dlsym) = Dlsym :: from_str ( symbol_name) {
307
+ let ptr = this. memory_mut ( ) . create_fn_alloc ( FnVal :: Other ( dlsym) ) ;
308
+ this. write_scalar ( Scalar :: from ( ptr) , dest) ?;
309
+ } else {
310
+ return err ! ( Unimplemented ( format!(
311
+ "Unsupported dlsym: {}" , symbol_name
312
+ ) ) ) ;
313
+ }
310
314
}
311
315
312
316
"__rust_maybe_catch_panic" => {
@@ -319,7 +323,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
319
323
// We abort on panic, so not much is going on here, but we still have to call the closure.
320
324
let f = this. read_scalar ( args[ 0 ] ) ?. to_ptr ( ) ?;
321
325
let data = this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?;
322
- let f_instance = this. memory ( ) . get_fn ( f) ?;
326
+ let f_instance = this. memory ( ) . get_fn ( f) ?. as_instance ( ) ? ;
323
327
this. write_null ( dest) ?;
324
328
trace ! ( "__rust_maybe_catch_panic: {:?}" , f_instance) ;
325
329
@@ -638,7 +642,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
638
642
639
643
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
640
644
let dtor = match this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ? {
641
- Scalar :: Ptr ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr) ?) ,
645
+ Scalar :: Ptr ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr) ?. as_instance ( ) ? ) ,
642
646
Scalar :: Raw { data : 0 , size } => {
643
647
// NULL pointer
644
648
assert_eq ! ( size as u64 , this. memory( ) . pointer_size( ) . bytes( ) ) ;
@@ -750,7 +754,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
750
754
"SecRandomCopyBytes" => {
751
755
let len = this. read_scalar ( args[ 1 ] ) ?. to_usize ( this) ?;
752
756
let ptr = this. read_scalar ( args[ 2 ] ) ?. not_undef ( ) ?;
753
- gen_random ( this , len as usize , ptr) ?;
757
+ this . gen_random ( len as usize , ptr) ?;
754
758
this. write_null ( dest) ?;
755
759
}
756
760
@@ -911,7 +915,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
911
915
"SystemFunction036" => {
912
916
let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
913
917
let len = this. read_scalar ( args[ 1 ] ) ?. to_u32 ( ) ?;
914
- gen_random ( this , len as usize , ptr) ?;
918
+ this . gen_random ( len as usize , ptr) ?;
915
919
this. write_scalar ( Scalar :: from_bool ( true ) , dest) ?;
916
920
}
917
921
@@ -947,36 +951,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
947
951
}
948
952
return Ok ( None ) ;
949
953
}
950
- }
951
-
952
- fn gen_random < ' mir , ' tcx > (
953
- this : & mut MiriEvalContext < ' mir , ' tcx > ,
954
- len : usize ,
955
- dest : Scalar < Tag > ,
956
- ) -> InterpResult < ' tcx > {
957
- if len == 0 {
958
- // Nothing to do
959
- return Ok ( ( ) ) ;
960
- }
961
- let ptr = dest. to_ptr ( ) ?;
962
-
963
- let data = match & mut this. memory_mut ( ) . extra . rng {
964
- Some ( rng) => {
965
- let mut rng = rng. borrow_mut ( ) ;
966
- let mut data = vec ! [ 0 ; len] ;
967
- rng. fill_bytes ( & mut data) ;
968
- data
969
- }
970
- None => {
971
- return err ! ( Unimplemented (
972
- "miri does not support gathering system entropy in deterministic mode!
973
- Use '-Zmiri-seed=<seed>' to enable random number generation.
974
- WARNING: Miri does *not* generate cryptographically secure entropy -
975
- do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
976
- ) ) ;
954
+
955
+ fn gen_random (
956
+ & mut self ,
957
+ len : usize ,
958
+ dest : Scalar < Tag > ,
959
+ ) -> InterpResult < ' tcx > {
960
+ if len == 0 {
961
+ // Nothing to do
962
+ return Ok ( ( ) ) ;
977
963
}
978
- } ;
979
- let tcx = & { this. tcx . tcx } ;
980
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
981
- . write_bytes ( tcx, ptr, & data)
982
- }
964
+ let this = self . eval_context_mut ( ) ;
965
+ let ptr = dest. to_ptr ( ) ?;
966
+
967
+ let data = match & mut this. memory_mut ( ) . extra . rng {
968
+ Some ( rng) => {
969
+ let mut rng = rng. borrow_mut ( ) ;
970
+ let mut data = vec ! [ 0 ; len] ;
971
+ rng. fill_bytes ( & mut data) ;
972
+ data
973
+ }
974
+ None => {
975
+ return err ! ( Unimplemented (
976
+ "miri does not support gathering system entropy in deterministic mode!
977
+ Use '-Zmiri-seed=<seed>' to enable random number generation.
978
+ WARNING: Miri does *not* generate cryptographically secure entropy -
979
+ do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
980
+ ) ) ;
981
+ }
982
+ } ;
983
+ let tcx = & { this. tcx . tcx } ;
984
+ this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
985
+ . write_bytes ( tcx, ptr, & data)
986
+ }
987
+ }
0 commit comments