From 0505868d18ff946da58028de20bf2d5a19bb7f40 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 09:21:17 -0400 Subject: [PATCH 1/3] Do nothing when we try to generate random data of length 0 This preserves compatibility with programs that pass a null pointer and a length of zero to getrandom(), or their platform's equivalent. --- src/helpers.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/helpers.rs b/src/helpers.rs index e9b09d816056..c543240442ec 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -81,6 +81,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx ptr: Scalar, len: usize, ) -> InterpResult<'tcx> { + // Some programs pass in a null pointer and a length of 0 + // to their platform's random-generation function (e.g. getrandom()) + // on Linux. For compatibility with these programs, we don't perform + // any additional checks - it's okay if the pointer is invalid, + // since we wouldn't actually be writing to it. + if len == 0 { + return Ok(()) + } let this = self.eval_context_mut(); let ptr = match this.memory().check_ptr_access(ptr, Size::from_bytes(len as u64), Align::from_bytes(1).unwrap())? { From c2f681f005a036568dcfe2beb171b9ab635fa486 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 10:13:29 -0400 Subject: [PATCH 2/3] Add semicolon Co-Authored-By: Ralf Jung --- src/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.rs b/src/helpers.rs index c543240442ec..569c1dbfb07e 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -87,7 +87,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // any additional checks - it's okay if the pointer is invalid, // since we wouldn't actually be writing to it. if len == 0 { - return Ok(()) + return Ok(()); } let this = self.eval_context_mut(); From 4d3398fc6262a0159af7311524ade980637121ae Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 10:17:39 -0400 Subject: [PATCH 3/3] Replace match with expect() --- src/helpers.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 569c1dbfb07e..c0e1ec2cd75b 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -91,10 +91,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } let this = self.eval_context_mut(); - let ptr = match this.memory().check_ptr_access(ptr, Size::from_bytes(len as u64), Align::from_bytes(1).unwrap())? { - Some(ptr) => ptr, - None => return Ok(()), // zero-sized access - }; + let ptr = this.memory().check_ptr_access( + ptr, + Size::from_bytes(len as u64), + Align::from_bytes(1).unwrap() + )?.expect("we already checked for size 0"); let rng = this.memory_mut().extra.rng.get_mut(); let mut data = vec![0; len];