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, GuestType};
use wiggle::{BorrowChecker, GuestMemory, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle::from_witx!({
@@ -107,21 +107,22 @@ impl GetTagExercise {
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let bc = BorrowChecker::new();
let discriminant: u8 = reason_tag(&self.input).into();
host_memory
.ptr(self.input_loc.ptr)
.ptr(&bc, self.input_loc.ptr)
.write(discriminant)
.expect("input discriminant ptr");
match self.input {
types::Reason::DogAte(f) => {
host_memory
.ptr(self.input_loc.ptr + 4)
.ptr(&bc, self.input_loc.ptr + 4)
.write(f)
.expect("input contents ref_mut");
}
types::Reason::Traffic(v) => host_memory
.ptr(self.input_loc.ptr + 4)
.ptr(&bc, self.input_loc.ptr + 4)
.write(v)
.expect("input contents ref_mut"),
types::Reason::Sleeping => {} // Do nothing
@@ -129,6 +130,7 @@ impl GetTagExercise {
let e = union_example::get_tag(
&ctx,
&host_memory,
&bc,
self.input_loc.ptr as i32,
self.return_loc.ptr as i32,
);
@@ -136,7 +138,7 @@ impl GetTagExercise {
assert_eq!(e, types::Errno::Ok.into(), "get_tag errno");
let return_val: types::Excuse = host_memory
.ptr(self.return_loc.ptr)
.ptr(&bc, self.return_loc.ptr)
.read()
.expect("return ref");
@@ -184,27 +186,28 @@ impl ReasonMultExercise {
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let bc = BorrowChecker::new();
let discriminant: u8 = reason_tag(&self.input).into();
host_memory
.ptr(self.input_loc.ptr)
.ptr(&bc, self.input_loc.ptr)
.write(discriminant)
.expect("input discriminant ref_mut");
host_memory
.ptr(self.input_loc.ptr + 4)
.ptr(&bc, self.input_loc.ptr + 4)
.write(self.input_pointee_loc.ptr)
.expect("input pointer ref_mut");
match self.input {
types::Reason::DogAte(f) => {
host_memory
.ptr(self.input_pointee_loc.ptr)
.ptr(&bc, self.input_pointee_loc.ptr)
.write(f)
.expect("input contents ref_mut");
}
types::Reason::Traffic(v) => {
host_memory
.ptr(self.input_pointee_loc.ptr)
.ptr(&bc, self.input_pointee_loc.ptr)
.write(v)
.expect("input contents ref_mut");
}
@@ -213,6 +216,7 @@ impl ReasonMultExercise {
let e = union_example::reason_mult(
&ctx,
&host_memory,
&bc,
self.input_loc.ptr as i32,
self.multiply_by as i32,
);
@@ -222,7 +226,7 @@ impl ReasonMultExercise {
match self.input {
types::Reason::DogAte(f) => {
let f_result: f32 = host_memory
.ptr(self.input_pointee_loc.ptr)
.ptr(&bc, self.input_pointee_loc.ptr)
.read()
.expect("input contents ref_mut");
assert_eq!(
@@ -233,7 +237,7 @@ impl ReasonMultExercise {
}
types::Reason::Traffic(v) => {
let v_result: i32 = host_memory
.ptr(self.input_pointee_loc.ptr)
.ptr(&bc, self.input_pointee_loc.ptr)
.read()
.expect("input contents ref_mut");
assert_eq!(