Refactor tests to use proptest (#6)

* generator: take an &mut GuestMemory

rather than pass the owned GuestMemory in, just give exclusive access
to it. Makes testing easier.

* tests: start transforming tests to check abi-level generated code as well

* finish lowering of test funcs

* tests: rename variables to more sensible names

* proptesting: reliably finds that we dont allow stuff to be right against end of memory!

* memory: fix off-by-one calc in GuestMemory::contains(&self, Region)

ty proptest!

also, refactored the Region::overlaps to be the same code but easier to
read.

* generator: better location information in GuestError

* testing: proptest generates memory areas, tests everything
This commit is contained in:
Pat Hickey
2020-02-04 13:35:39 -08:00
committed by GitHub
parent e6cec049cb
commit 67d2ce6d85
8 changed files with 471 additions and 107 deletions

View File

@@ -11,8 +11,15 @@ pub enum GuestError {
PtrNotAligned(Region, u32),
#[error("Pointer already borrowed: {0:?}")]
PtrBorrowed(Region),
#[error("In {typename}.{field}:")]
InField {
#[error("In func {funcname}:{location}:")]
InFunc {
funcname: &'static str,
location: &'static str,
#[source]
err: Box<GuestError>,
},
#[error("In data {typename}.{field}:")]
InDataField {
typename: String,
field: String,
#[source]

View File

@@ -26,7 +26,7 @@ impl<'a> GuestMemory<'a> {
fn contains(&self, r: Region) -> bool {
r.start < self.len
&& r.len < self.len // make sure next clause doesnt underflow
&& r.start < (self.len - r.len)
&& r.start <= (self.len - r.len)
}
pub fn ptr<T: GuestType>(&'a self, at: u32) -> Result<GuestPtr<'a, T>, GuestError> {

View File

@@ -7,10 +7,10 @@ pub struct Region {
impl Region {
pub fn overlaps(&self, rhs: Region) -> bool {
let self_start = self.start as u64;
let self_end = ((self_start + self.len as u64) as i64 - 1) as u64;
let self_end = self_start + (self.len - 1) as u64;
let rhs_start = rhs.start as u64;
let rhs_end = ((rhs_start + rhs.len as u64) as i64 - 1) as u64;
let rhs_end = rhs_start + (rhs.len - 1) as u64;
// start of rhs inside self:
if rhs_start >= self_start && rhs_start < self_end {