Merge pull request #2096 from bytecodealliance/pch/issue-2053

Wiggle: Fix #2053
This commit is contained in:
Pat Hickey
2020-08-05 09:52:08 -07:00
committed by GitHub
4 changed files with 165 additions and 2 deletions

View File

@@ -19,7 +19,14 @@ pub(super) fn define_struct(
let member_decls = s.members.iter().map(|m| {
let name = names.struct_member(&m.name);
let type_ = match &m.tref {
witx::TypeRef::Name(nt) => names.type_(&nt.name),
witx::TypeRef::Name(nt) => {
let tt = names.type_(&nt.name);
if m.tref.needs_lifetime() {
quote!(#tt<'a>)
} else {
quote!(#tt)
}
}
witx::TypeRef::Value(ty) => match &**ty {
witx::Type::Builtin(builtin) => names.builtin_type(*builtin, quote!('a)),
witx::Type::Pointer(pointee) | witx::Type::ConstPointer(pointee) => {

View File

@@ -153,3 +153,30 @@ impl<'a, T> GuestType<'a> for GuestPtr<'a, T> {
ptr.cast::<u32>().write(val.offset())
}
}
// Support pointers-to-arrays where pointers are always 32-bits in wasm land
impl<'a, T> GuestType<'a> for GuestPtr<'a, [T]>
where
T: GuestType<'a>,
{
fn guest_size() -> u32 {
u32::guest_size() * 2
}
fn guest_align() -> usize {
u32::guest_align()
}
fn read(ptr: &GuestPtr<'a, Self>) -> Result<Self, GuestError> {
let offset = ptr.cast::<u32>().read()?;
let len = ptr.cast::<u32>().add(1)?.read()?;
Ok(GuestPtr::new(ptr.mem(), offset).as_array(len))
}
fn write(ptr: &GuestPtr<'_, Self>, val: Self) -> Result<(), GuestError> {
let (offs, len) = val.offset();
let len_ptr = ptr.cast::<u32>().add(1)?;
ptr.cast::<u32>().write(offs)?;
len_ptr.write(len)
}
}

View File

@@ -1,6 +1,6 @@
use proptest::prelude::*;
use wiggle::{GuestMemory, GuestPtr};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
use wiggle_test::{impl_errno, HostMemory, MemArea, MemAreas, WasiCtx};
wiggle::from_witx!({
witx: ["tests/structs.witx"],
@@ -52,6 +52,25 @@ impl<'a> structs::Structs for WasiCtx<'a> {
second: *second,
})
}
fn sum_array<'b>(&self, struct_of_arr: &types::StructOfArray<'b>) -> Result<u16, types::Errno> {
// my kingdom for try blocks
fn aux(struct_of_arr: &types::StructOfArray) -> Result<u16, wiggle::GuestError> {
let mut s = 0;
for elem in struct_of_arr.arr.iter() {
let v = elem?.read()?;
s += v as u16;
}
Ok(s)
}
match aux(struct_of_arr) {
Ok(s) => Ok(s),
Err(guest_err) => {
eprintln!("guest error summing array: {:?}", guest_err);
Err(types::Errno::PicketLine)
}
}
}
}
#[derive(Debug)]
@@ -423,3 +442,103 @@ proptest! {
e.test()
}
}
#[derive(Debug)]
struct SumArrayExercise {
inputs: Vec<u8>,
input_array_loc: MemArea,
input_struct_loc: MemArea,
output_loc: MemArea,
}
impl SumArrayExercise {
pub fn strat() -> BoxedStrategy<Self> {
(0..256u32)
.prop_flat_map(|len| {
let len_usize = len as usize;
(
prop::collection::vec(prop::num::u8::ANY, len_usize..=len_usize),
HostMemory::mem_area_strat(8), // Input struct is 8 bytes - ptr and len
HostMemory::mem_area_strat(4), // Output is 4 bytes - stores a u16, but abi requires 4 byte alignment
)
})
.prop_filter(
"non-overlapping input struct and output pointers",
|(_inputs, input_struct_loc, output_loc)| {
MemArea::non_overlapping_set(&[input_struct_loc.clone(), output_loc.clone()])
},
)
.prop_flat_map(|(inputs, input_struct_loc, output_loc)| {
(
Just(inputs.clone()),
HostMemory::byte_slice_strat(
inputs.len() as u32,
&MemAreas::from([input_struct_loc, output_loc]),
),
Just(input_struct_loc.clone()),
Just(output_loc.clone()),
)
})
.prop_map(
|(inputs, input_array_loc, input_struct_loc, output_loc)| SumArrayExercise {
inputs,
input_array_loc,
input_struct_loc,
output_loc,
},
)
.boxed()
}
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
// Write inputs to memory as an array
for (ix, val) in self.inputs.iter().enumerate() {
let ix = ix as u32;
host_memory
.ptr(self.input_array_loc.ptr + ix)
.write(*val)
.expect("write val to array memory");
}
// Write struct that contains the array
host_memory
.ptr(self.input_struct_loc.ptr)
.write(self.input_array_loc.ptr)
.expect("write ptr to struct memory");
host_memory
.ptr(self.input_struct_loc.ptr + 4)
.write(self.inputs.len() as u32)
.expect("write len to struct memory");
// Call wiggle-generated func
let res = structs::sum_array(
&ctx,
&host_memory,
self.input_struct_loc.ptr as i32,
self.output_loc.ptr as i32,
);
// should be no error - if hostcall did a GuestError it should eprintln it.
assert_eq!(res, types::Errno::Ok.into(), "reduce excuses errno");
// Sum is inputs upcasted to u16
let expected: u16 = self.inputs.iter().map(|v| *v as u16).sum();
// Wiggle stored output value in memory as u16
let given: u16 = host_memory
.ptr(self.output_loc.ptr)
.read()
.expect("deref ptr to returned value");
// Assert the two calculations match
assert_eq!(expected, given, "sum_array return val");
}
}
proptest! {
#[test]
fn sum_of_array(e in SumArrayExercise::strat()) {
e.test()
}
}

View File

@@ -16,6 +16,12 @@
(field $first (@witx const_pointer s32))
(field $second s32)))
(typename $some_bytes (array u8))
(typename $struct_of_array
(struct
(field $arr $some_bytes)))
(module $structs
(@interface func (export "sum_of_pair")
(param $an_pair $pair_ints)
@@ -37,4 +43,8 @@
(param $second (@witx const_pointer s32))
(result $error $errno)
(result $an_pair $pair_int_ptrs))
(@interface func (export "sum_array")
(param $an_arr $struct_of_array)
(result $error $errno)
(result $doubled u16))
)