Skip to content

Commit 009d0c2

Browse files
committed
gen_random: handle size 0 and writing to mem
1 parent 0ed0b2b commit 009d0c2

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/fn_call.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,14 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
298298
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
299299
match this.read_scalar(args[0])?.to_usize(this)? {
300300
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()?;
302302
let len = this.read_scalar(args[2])?.to_usize(this)?;
303303

304304
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
305305
// neither of which have any effect on our current PRNG
306306
let _flags = this.read_scalar(args[3])?.to_i32()?;
307307

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)?;
314309
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
315310
}
316311
id => {
@@ -714,14 +709,8 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
714709
},
715710
"SecRandomCopyBytes" => {
716711
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)?;
725714
this.write_null(dest)?;
726715
}
727716

@@ -878,15 +867,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
878867
}
879868
// The actual name of 'RtlGenRandom'
880869
"SystemFunction036" => {
881-
let ptr = this.read_scalar(args[0])?.to_ptr()?;
870+
let ptr = this.read_scalar(args[0])?.not_undef()?;
882871
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)?;
890873
this.write_scalar(Scalar::from_bool(true), dest)?;
891874
}
892875

@@ -927,21 +910,30 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
927910
fn gen_random<'a, 'mir, 'tcx>(
928911
this: &mut MiriEvalContext<'a, 'mir, 'tcx>,
929912
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()?;
931920

932-
match &mut this.machine.rng {
921+
let data = match &mut this.machine.rng {
933922
Some(rng) => {
934923
let mut data = vec![0; len];
935924
rng.fill_bytes(&mut data);
936-
Ok(data)
925+
data
937926
}
938927
None => {
939-
err!(Unimplemented(
928+
return err!(Unimplemented(
940929
"miri does not support gathering system entropy in deterministic mode!
941930
Use '-Zmiri-seed=<seed>' to enable random number generation.
942931
WARNING: Miri does *not* generate cryptographically secure entropy -
943932
do not use Miri to run any program that needs secure random number generation".to_owned(),
944-
))
933+
));
945934
}
946-
}
935+
};
936+
let tcx = &{this.tcx.tcx};
937+
this.memory_mut().get_mut(ptr.alloc_id)?
938+
.write_bytes(tcx, ptr, &data)
947939
}

0 commit comments

Comments
 (0)