@@ -298,19 +298,14 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
298
298
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
299
299
match this. read_scalar ( args[ 0 ] ) ?. to_usize ( this) ? {
300
300
id if id == sys_getrandom => {
301
- let ptr = this. read_scalar ( args[ 1 ] ) ?. to_ptr ( ) ?;
301
+ let ptr = this. read_scalar ( args[ 1 ] ) ?. not_undef ( ) ?;
302
302
let len = this. read_scalar ( args[ 2 ] ) ?. to_usize ( this) ?;
303
303
304
304
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
305
305
// neither of which have any effect on our current PRNG
306
306
let _flags = this. read_scalar ( args[ 3 ] ) ?. to_i32 ( ) ?;
307
307
308
- if len > 0 {
309
- let data = gen_random ( this, len as usize ) ?;
310
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
311
- . write_bytes ( tcx, ptr, & data) ?;
312
- }
313
-
308
+ gen_random ( this, len as usize , ptr) ?;
314
309
this. write_scalar ( Scalar :: from_uint ( len, dest. layout . size ) , dest) ?;
315
310
}
316
311
id => {
@@ -712,6 +707,12 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
712
707
"_NSGetArgv" => {
713
708
this. write_scalar ( Scalar :: Ptr ( this. machine . argv . unwrap ( ) ) , dest) ?;
714
709
} ,
710
+ "SecRandomCopyBytes" => {
711
+ let len = this. read_scalar ( args[ 1 ] ) ?. to_usize ( this) ?;
712
+ let ptr = this. read_scalar ( args[ 2 ] ) ?. not_undef ( ) ?;
713
+ gen_random ( this, len as usize , ptr) ?;
714
+ this. write_null ( dest) ?;
715
+ }
715
716
716
717
// Windows API stubs.
717
718
// HANDLE = isize
@@ -866,15 +867,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
866
867
}
867
868
// The actual name of 'RtlGenRandom'
868
869
"SystemFunction036" => {
869
- let ptr = this. read_scalar ( args[ 0 ] ) ?. to_ptr ( ) ?;
870
+ let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
870
871
let len = this. read_scalar ( args[ 1 ] ) ?. to_u32 ( ) ?;
871
-
872
- if len > 0 {
873
- let data = gen_random ( this, len as usize ) ?;
874
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
875
- . write_bytes ( tcx, ptr, & data) ?;
876
- }
877
-
872
+ gen_random ( this, len as usize , ptr) ?;
878
873
this. write_scalar ( Scalar :: from_bool ( true ) , dest) ?;
879
874
}
880
875
@@ -915,21 +910,30 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
915
910
fn gen_random < ' a , ' mir , ' tcx > (
916
911
this : & mut MiriEvalContext < ' a , ' mir , ' tcx > ,
917
912
len : usize ,
918
- ) -> Result < Vec < u8 > , EvalError < ' tcx > > {
913
+ dest : Scalar < Tag > ,
914
+ ) -> EvalResult < ' tcx > {
915
+ if len == 0 {
916
+ // Nothing to do
917
+ return Ok ( ( ) ) ;
918
+ }
919
+ let ptr = dest. to_ptr ( ) ?;
919
920
920
- match & mut this. machine . rng {
921
+ let data = match & mut this. machine . rng {
921
922
Some ( rng) => {
922
923
let mut data = vec ! [ 0 ; len] ;
923
924
rng. fill_bytes ( & mut data) ;
924
- Ok ( data)
925
+ data
925
926
}
926
927
None => {
927
- err ! ( Unimplemented (
928
+ return err ! ( Unimplemented (
928
929
"miri does not support gathering system entropy in deterministic mode!
929
930
Use '-Zmiri-seed=<seed>' to enable random number generation.
930
931
WARNING: Miri does *not* generate cryptographically secure entropy -
931
932
do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
932
- ) )
933
+ ) ) ;
933
934
}
934
- }
935
+ } ;
936
+ let tcx = & { this. tcx . tcx } ;
937
+ this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
938
+ . write_bytes ( tcx, ptr, & data)
935
939
}
0 commit comments