@@ -311,7 +311,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
311
311
// neither of which have any effect on our current PRNG
312
312
let _flags = this. read_scalar ( args[ 3 ] ) ?. to_i32 ( ) ?;
313
313
314
- gen_random ( this , len as usize , ptr) ?;
314
+ this . gen_random ( len as usize , ptr) ?;
315
315
this. write_scalar ( Scalar :: from_uint ( len, dest. layout . size ) , dest) ?;
316
316
}
317
317
id => {
@@ -328,10 +328,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
328
328
let symbol_name = this. memory ( ) . get ( symbol. alloc_id ) ?. read_c_str ( tcx, symbol) ?;
329
329
let err = format ! ( "bad c unicode symbol: {:?}" , symbol_name) ;
330
330
let symbol_name = :: std:: str:: from_utf8 ( symbol_name) . unwrap_or ( & err) ;
331
- return err ! ( Unimplemented ( format!(
332
- "miri does not support dynamically loading libraries (requested symbol: {})" ,
333
- symbol_name
334
- ) ) ) ;
331
+ if let Some ( dlsym) = Dlsym :: from_str ( symbol_name) {
332
+ let ptr = this. memory_mut ( ) . create_fn_alloc ( FnVal :: Other ( dlsym) ) ;
333
+ this. write_scalar ( Scalar :: from ( ptr) , dest) ?;
334
+ } else {
335
+ return err ! ( Unimplemented ( format!(
336
+ "Unsupported dlsym: {}" , symbol_name
337
+ ) ) ) ;
338
+ }
335
339
}
336
340
337
341
"__rust_maybe_catch_panic" => {
@@ -344,7 +348,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
344
348
// We abort on panic, so not much is going on here, but we still have to call the closure.
345
349
let f = this. read_scalar ( args[ 0 ] ) ?. to_ptr ( ) ?;
346
350
let data = this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?;
347
- let f_instance = this. memory ( ) . get_fn ( f) ?;
351
+ let f_instance = this. memory ( ) . get_fn ( f) ?. as_instance ( ) ? ;
348
352
this. write_null ( dest) ?;
349
353
trace ! ( "__rust_maybe_catch_panic: {:?}" , f_instance) ;
350
354
@@ -663,7 +667,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
663
667
664
668
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
665
669
let dtor = match this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ? {
666
- Scalar :: Ptr ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr) ?) ,
670
+ Scalar :: Ptr ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr) ?. as_instance ( ) ? ) ,
667
671
Scalar :: Raw { data : 0 , size } => {
668
672
// NULL pointer
669
673
assert_eq ! ( size as u64 , this. memory( ) . pointer_size( ) . bytes( ) ) ;
@@ -775,7 +779,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
775
779
"SecRandomCopyBytes" => {
776
780
let len = this. read_scalar ( args[ 1 ] ) ?. to_usize ( this) ?;
777
781
let ptr = this. read_scalar ( args[ 2 ] ) ?. not_undef ( ) ?;
778
- gen_random ( this , len as usize , ptr) ?;
782
+ this . gen_random ( len as usize , ptr) ?;
779
783
this. write_null ( dest) ?;
780
784
}
781
785
@@ -936,7 +940,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
936
940
"SystemFunction036" => {
937
941
let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
938
942
let len = this. read_scalar ( args[ 1 ] ) ?. to_u32 ( ) ?;
939
- gen_random ( this , len as usize , ptr) ?;
943
+ this . gen_random ( len as usize , ptr) ?;
940
944
this. write_scalar ( Scalar :: from_bool ( true ) , dest) ?;
941
945
}
942
946
@@ -972,36 +976,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
972
976
}
973
977
return Ok ( None ) ;
974
978
}
975
- }
976
-
977
- fn gen_random < ' mir , ' tcx > (
978
- this : & mut MiriEvalContext < ' mir , ' tcx > ,
979
- len : usize ,
980
- dest : Scalar < Tag > ,
981
- ) -> InterpResult < ' tcx > {
982
- if len == 0 {
983
- // Nothing to do
984
- return Ok ( ( ) ) ;
985
- }
986
- let ptr = dest. to_ptr ( ) ?;
987
-
988
- let data = match & mut this. memory_mut ( ) . extra . rng {
989
- Some ( rng) => {
990
- let mut rng = rng. borrow_mut ( ) ;
991
- let mut data = vec ! [ 0 ; len] ;
992
- rng. fill_bytes ( & mut data) ;
993
- data
994
- }
995
- None => {
996
- return err ! ( Unimplemented (
997
- "miri does not support gathering system entropy in deterministic mode!
998
- Use '-Zmiri-seed=<seed>' to enable random number generation.
999
- WARNING: Miri does *not* generate cryptographically secure entropy -
1000
- do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
1001
- ) ) ;
979
+
980
+ fn gen_random (
981
+ & mut self ,
982
+ len : usize ,
983
+ dest : Scalar < Tag > ,
984
+ ) -> InterpResult < ' tcx > {
985
+ if len == 0 {
986
+ // Nothing to do
987
+ return Ok ( ( ) ) ;
1002
988
}
1003
- } ;
1004
- let tcx = & { this. tcx . tcx } ;
1005
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
1006
- . write_bytes ( tcx, ptr, & data)
1007
- }
989
+ let this = self . eval_context_mut ( ) ;
990
+ let ptr = dest. to_ptr ( ) ?;
991
+
992
+ let data = match & mut this. memory_mut ( ) . extra . rng {
993
+ Some ( rng) => {
994
+ let mut rng = rng. borrow_mut ( ) ;
995
+ let mut data = vec ! [ 0 ; len] ;
996
+ rng. fill_bytes ( & mut data) ;
997
+ data
998
+ }
999
+ None => {
1000
+ return err ! ( Unimplemented (
1001
+ "miri does not support gathering system entropy in deterministic mode!
1002
+ Use '-Zmiri-seed=<seed>' to enable random number generation.
1003
+ WARNING: Miri does *not* generate cryptographically secure entropy -
1004
+ do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
1005
+ ) ) ;
1006
+ }
1007
+ } ;
1008
+ let tcx = & { this. tcx . tcx } ;
1009
+ this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
1010
+ . write_bytes ( tcx, ptr, & data)
1011
+ }
1012
+ }
0 commit comments