@@ -307,7 +307,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
307
307
// neither of which have any effect on our current PRNG
308
308
let _flags = this. read_scalar ( args[ 3 ] ) ?. to_i32 ( ) ?;
309
309
310
- gen_random ( this , len as usize , ptr) ?;
310
+ this . gen_random ( len as usize , ptr) ?;
311
311
this. write_scalar ( Scalar :: from_uint ( len, dest. layout . size ) , dest) ?;
312
312
}
313
313
id => {
@@ -324,10 +324,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
324
324
let symbol_name = this. memory ( ) . get ( symbol. alloc_id ) ?. read_c_str ( tcx, symbol) ?;
325
325
let err = format ! ( "bad c unicode symbol: {:?}" , symbol_name) ;
326
326
let symbol_name = :: std:: str:: from_utf8 ( symbol_name) . unwrap_or ( & err) ;
327
- return err ! ( Unimplemented ( format!(
328
- "miri does not support dynamically loading libraries (requested symbol: {})" ,
329
- symbol_name
330
- ) ) ) ;
327
+ if let Some ( dlsym) = Dlsym :: from_str ( symbol_name) {
328
+ let ptr = this. memory_mut ( ) . create_fn_alloc ( FnVal :: Other ( dlsym) ) ;
329
+ this. write_scalar ( Scalar :: from ( ptr) , dest) ?;
330
+ } else {
331
+ return err ! ( Unimplemented ( format!(
332
+ "Unsupported dlsym: {}" , symbol_name
333
+ ) ) ) ;
334
+ }
331
335
}
332
336
333
337
"__rust_maybe_catch_panic" => {
@@ -340,7 +344,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
340
344
// We abort on panic, so not much is going on here, but we still have to call the closure.
341
345
let f = this. read_scalar ( args[ 0 ] ) ?. to_ptr ( ) ?;
342
346
let data = this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?;
343
- let f_instance = this. memory ( ) . get_fn ( f) ?;
347
+ let f_instance = this. memory ( ) . get_fn ( f) ?. as_instance ( ) ? ;
344
348
this. write_null ( dest) ?;
345
349
trace ! ( "__rust_maybe_catch_panic: {:?}" , f_instance) ;
346
350
@@ -659,7 +663,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
659
663
660
664
// Extract the function type out of the signature (that seems easier than constructing it ourselves).
661
665
let dtor = match this. test_null ( this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?) ? {
662
- Some ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr. to_ptr ( ) ? ) ?) ,
666
+ Some ( dtor_ptr) => Some ( this. memory ( ) . get_fn ( dtor_ptr) ? . as_instance ( ) ?) ,
663
667
None => None ,
664
668
} ;
665
669
@@ -766,7 +770,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
766
770
"SecRandomCopyBytes" => {
767
771
let len = this. read_scalar ( args[ 1 ] ) ?. to_usize ( this) ?;
768
772
let ptr = this. read_scalar ( args[ 2 ] ) ?. not_undef ( ) ?;
769
- gen_random ( this , len as usize , ptr) ?;
773
+ this . gen_random ( len as usize , ptr) ?;
770
774
this. write_null ( dest) ?;
771
775
}
772
776
@@ -934,7 +938,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
934
938
"SystemFunction036" => {
935
939
let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
936
940
let len = this. read_scalar ( args[ 1 ] ) ?. to_u32 ( ) ?;
937
- gen_random ( this , len as usize , ptr) ?;
941
+ this . gen_random ( len as usize , ptr) ?;
938
942
this. write_scalar ( Scalar :: from_bool ( true ) , dest) ?;
939
943
}
940
944
@@ -966,36 +970,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
966
970
}
967
971
return Ok ( None ) ;
968
972
}
969
- }
970
-
971
- fn gen_random < ' mir , ' tcx > (
972
- this : & mut MiriEvalContext < ' mir , ' tcx > ,
973
- len : usize ,
974
- dest : Scalar < Tag > ,
975
- ) -> InterpResult < ' tcx > {
976
- if len == 0 {
977
- // Nothing to do
978
- return Ok ( ( ) ) ;
979
- }
980
- let ptr = dest. to_ptr ( ) ?;
981
-
982
- let data = match & mut this. memory_mut ( ) . extra . rng {
983
- Some ( rng) => {
984
- let mut rng = rng. borrow_mut ( ) ;
985
- let mut data = vec ! [ 0 ; len] ;
986
- rng. fill_bytes ( & mut data) ;
987
- data
988
- }
989
- None => {
990
- return err ! ( Unimplemented (
991
- "miri does not support gathering system entropy in deterministic mode!
992
- Use '-Zmiri-seed=<seed>' to enable random number generation.
993
- WARNING: Miri does *not* generate cryptographically secure entropy -
994
- do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
995
- ) ) ;
973
+
974
+ fn gen_random (
975
+ & mut self ,
976
+ len : usize ,
977
+ dest : Scalar < Tag > ,
978
+ ) -> InterpResult < ' tcx > {
979
+ if len == 0 {
980
+ // Nothing to do
981
+ return Ok ( ( ) ) ;
996
982
}
997
- } ;
998
- let tcx = & { this. tcx . tcx } ;
999
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
1000
- . write_bytes ( tcx, ptr, & data)
1001
- }
983
+ let this = self . eval_context_mut ( ) ;
984
+ let ptr = dest. to_ptr ( ) ?;
985
+
986
+ let data = match & mut this. memory_mut ( ) . extra . rng {
987
+ Some ( rng) => {
988
+ let mut rng = rng. borrow_mut ( ) ;
989
+ let mut data = vec ! [ 0 ; len] ;
990
+ rng. fill_bytes ( & mut data) ;
991
+ data
992
+ }
993
+ None => {
994
+ return err ! ( Unimplemented (
995
+ "miri does not support gathering system entropy in deterministic mode!
996
+ Use '-Zmiri-seed=<seed>' to enable random number generation.
997
+ WARNING: Miri does *not* generate cryptographically secure entropy -
998
+ do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
999
+ ) ) ;
1000
+ }
1001
+ } ;
1002
+ let tcx = & { this. tcx . tcx } ;
1003
+ this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
1004
+ . write_bytes ( tcx, ptr, & data)
1005
+ }
1006
+ }
0 commit comments