wiggle: automate borrow checking, explicitly passing borrow checker throughout

This commit is contained in:
Pat Hickey
2020-05-18 11:45:12 -07:00
parent e229fbc79c
commit 52e8300f01
14 changed files with 313 additions and 191 deletions

View File

@@ -1,5 +1,5 @@
use proptest::prelude::*;
use wiggle::{GuestMemory, GuestPtr};
use wiggle::{BorrowChecker, GuestMemory, GuestPtr};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle::from_witx!({
@@ -75,31 +75,35 @@ impl ReduceExcusesExcercise {
}
pub fn test(&self) {
let mut ctx = WasiCtx::new();
let mut host_memory = HostMemory::new();
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let bc = BorrowChecker::new();
// Populate memory with pointers to generated Excuse values
for (&excuse, ptr) in self.excuse_values.iter().zip(self.excuse_ptr_locs.iter()) {
host_memory
.ptr(ptr.ptr)
.ptr(&bc, ptr.ptr)
.write(excuse)
.expect("deref ptr mut to Excuse value");
}
// Populate the array with pointers to generated Excuse values
{
let array: GuestPtr<'_, [GuestPtr<types::Excuse>]> =
host_memory.ptr((self.array_ptr_loc.ptr, self.excuse_ptr_locs.len() as u32));
let array: GuestPtr<'_, [GuestPtr<types::Excuse>]> = host_memory.ptr(
&bc,
(self.array_ptr_loc.ptr, self.excuse_ptr_locs.len() as u32),
);
for (slot, ptr) in array.iter().zip(&self.excuse_ptr_locs) {
let slot = slot.expect("array should be in bounds");
slot.write(host_memory.ptr(ptr.ptr))
slot.write(host_memory.ptr(&bc, ptr.ptr))
.expect("should succeed in writing array");
}
}
let res = arrays::reduce_excuses(
&mut ctx,
&mut host_memory,
&ctx,
&host_memory,
&bc,
self.array_ptr_loc.ptr as i32,
self.excuse_ptr_locs.len() as i32,
self.return_ptr_loc.ptr as i32,
@@ -112,7 +116,7 @@ impl ReduceExcusesExcercise {
.last()
.expect("generated vec of excuses should be non-empty");
let given: types::Excuse = host_memory
.ptr(self.return_ptr_loc.ptr)
.ptr(&bc, self.return_ptr_loc.ptr)
.read()
.expect("deref ptr to returned value");
assert_eq!(expected, given, "reduce excuses return val");
@@ -165,28 +169,30 @@ impl PopulateExcusesExcercise {
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let bc = BorrowChecker::new();
// Populate array with valid pointers to Excuse type in memory
let ptr = host_memory.ptr::<[GuestPtr<'_, types::Excuse>]>((
self.array_ptr_loc.ptr,
self.elements.len() as u32,
));
let ptr = host_memory.ptr::<[GuestPtr<'_, types::Excuse>]>(
&bc,
(self.array_ptr_loc.ptr, self.elements.len() as u32),
);
for (ptr, val) in ptr.iter().zip(&self.elements) {
ptr.expect("should be valid pointer")
.write(host_memory.ptr(val.ptr))
.write(host_memory.ptr(&bc, val.ptr))
.expect("failed to write value");
}
let res = arrays::populate_excuses(
&ctx,
&host_memory,
&bc,
self.array_ptr_loc.ptr as i32,
self.elements.len() as i32,
);
assert_eq!(res, types::Errno::Ok.into(), "populate excuses errno");
let arr: GuestPtr<'_, [GuestPtr<'_, types::Excuse>]> =
host_memory.ptr((self.array_ptr_loc.ptr, self.elements.len() as u32));
host_memory.ptr(&bc, (self.array_ptr_loc.ptr, self.elements.len() as u32));
for el in arr.iter() {
let ptr_to_ptr = el
.expect("valid ptr to ptr")