Skip to content

Commit 8f4989c

Browse files
committed
Allowing passing a null pointer to getrandom() when length is 0
The Linux kernel will handle a null pointer passed to 'getrandom' without error, as long as the length is also 0. The `getrandom` crate relies on this behavior: https://github.com/rust-random/getrandom/blob/ab44edf3c7af721a00e22648b6c811ccb559ba81/src/linux_android.rs#L26 Since it works fine on the actual kernel (and should continue to, due to the kernel's backwards-compatibility guarantees), Miri should support it as well.
1 parent d9d6df9 commit 8f4989c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/shims/foreign_items.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
293293
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
294294
match this.read_scalar(args[0])?.to_usize(this)? {
295295
id if id == sys_getrandom => {
296-
let ptr = this.read_scalar(args[1])?.not_undef()?;
296+
let ptr = this.read_scalar(args[1])?;
297297
let len = this.read_scalar(args[2])?.to_usize(this)?;
298298

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

303-
this.gen_random(len as usize, ptr)?;
303+
if len != 0 {
304+
// Linux allows passing a null pointer as
305+
// long as the length is also 0. THerefore,
306+
// we only call 'not_undef' if we
307+
// have a non-zero length.
308+
this.gen_random(len as usize, ptr.not_undef()?)?;
309+
}
304310
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
305311
}
306312
id => {

0 commit comments

Comments
 (0)