Skip to content

Commit bc82f83

Browse files
committed
Auto merge of #884 - Aaron1011:fix/linux-getrandom, r=RalfJung
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.
2 parents 8053288 + 4d3398f commit bc82f83

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/helpers.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8181
ptr: Scalar<Tag>,
8282
len: usize,
8383
) -> InterpResult<'tcx> {
84+
// Some programs pass in a null pointer and a length of 0
85+
// to their platform's random-generation function (e.g. getrandom())
86+
// on Linux. For compatibility with these programs, we don't perform
87+
// any additional checks - it's okay if the pointer is invalid,
88+
// since we wouldn't actually be writing to it.
89+
if len == 0 {
90+
return Ok(());
91+
}
8492
let this = self.eval_context_mut();
8593

86-
let ptr = match this.memory().check_ptr_access(ptr, Size::from_bytes(len as u64), Align::from_bytes(1).unwrap())? {
87-
Some(ptr) => ptr,
88-
None => return Ok(()), // zero-sized access
89-
};
94+
let ptr = this.memory().check_ptr_access(
95+
ptr,
96+
Size::from_bytes(len as u64),
97+
Align::from_bytes(1).unwrap()
98+
)?.expect("we already checked for size 0");
9099

91100
let rng = this.memory_mut().extra.rng.get_mut();
92101
let mut data = vec![0; len];

0 commit comments

Comments
 (0)