@@ -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 => {
@@ -714,14 +709,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
714
709
} ,
715
710
"SecRandomCopyBytes" => {
716
711
let len = this. read_scalar ( args[ 1 ] ) ?. to_usize ( this) ?;
717
- let ptr = this. read_scalar ( args[ 2 ] ) ?. to_ptr ( ) ?;
718
-
719
- if len > 0 {
720
- let data = gen_random ( this, len as usize ) ?;
721
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
722
- . write_bytes ( tcx, ptr, & data) ?;
723
- }
724
-
712
+ let ptr = this. read_scalar ( args[ 2 ] ) ?. not_undef ( ) ?;
713
+ gen_random ( this, len as usize , ptr) ?;
725
714
this. write_null ( dest) ?;
726
715
}
727
716
@@ -878,15 +867,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
878
867
}
879
868
// The actual name of 'RtlGenRandom'
880
869
"SystemFunction036" => {
881
- let ptr = this. read_scalar ( args[ 0 ] ) ?. to_ptr ( ) ?;
870
+ let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
882
871
let len = this. read_scalar ( args[ 1 ] ) ?. to_u32 ( ) ?;
883
-
884
- if len > 0 {
885
- let data = gen_random ( this, len as usize ) ?;
886
- this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
887
- . write_bytes ( tcx, ptr, & data) ?;
888
- }
889
-
872
+ gen_random ( this, len as usize , ptr) ?;
890
873
this. write_scalar ( Scalar :: from_bool ( true ) , dest) ?;
891
874
}
892
875
@@ -927,21 +910,30 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
927
910
fn gen_random < ' a , ' mir , ' tcx > (
928
911
this : & mut MiriEvalContext < ' a , ' mir , ' tcx > ,
929
912
len : usize ,
930
- ) -> 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 ( ) ?;
931
920
932
- match & mut this. machine . rng {
921
+ let data = match & mut this. machine . rng {
933
922
Some ( rng) => {
934
923
let mut data = vec ! [ 0 ; len] ;
935
924
rng. fill_bytes ( & mut data) ;
936
- Ok ( data)
925
+ data
937
926
}
938
927
None => {
939
- err ! ( Unimplemented (
928
+ return err ! ( Unimplemented (
940
929
"miri does not support gathering system entropy in deterministic mode!
941
930
Use '-Zmiri-seed=<seed>' to enable random number generation.
942
931
WARNING: Miri does *not* generate cryptographically secure entropy -
943
932
do not use Miri to run any program that needs secure random number generation" . to_owned( ) ,
944
- ) )
933
+ ) ) ;
945
934
}
946
- }
935
+ } ;
936
+ let tcx = & { this. tcx . tcx } ;
937
+ this. memory_mut ( ) . get_mut ( ptr. alloc_id ) ?
938
+ . write_bytes ( tcx, ptr, & data)
947
939
}
0 commit comments