diff --git a/tests/structs.rs b/tests/structs.rs index c2e4263dad..0d9aa5c5a0 100644 --- a/tests/structs.rs +++ b/tests/structs.rs @@ -25,6 +25,15 @@ impl structs::Structs for WasiCtx { .expect("dereferncing GuestPtr should succeed"); Ok(first as i64 + second as i64) } + + fn sum_of_int_and_ptr(&mut self, an_pair: &types::PairIntAndPtr) -> Result { + let first = *an_pair + .first + .as_ref() + .expect("dereferencing GuestPtr should succeed"); + let second = an_pair.second as i64; + Ok(first as i64 + second) + } } #[derive(Debug)] @@ -200,3 +209,91 @@ proptest! { e.test() } } + +#[derive(Debug)] +struct SumIntAndPtrExercise { + input_first: i32, + input_second: i32, + input_first_loc: MemArea, + input_struct_loc: MemArea, + return_loc: MemArea, +} + +impl SumIntAndPtrExercise { + pub fn strat() -> BoxedStrategy { + ( + prop::num::i32::ANY, + prop::num::i32::ANY, + HostMemory::mem_area_strat(4), + HostMemory::mem_area_strat(8), + HostMemory::mem_area_strat(8), + ) + .prop_map( + |(input_first, input_second, input_first_loc, input_struct_loc, return_loc)| { + SumIntAndPtrExercise { + input_first, + input_second, + input_first_loc, + input_struct_loc, + return_loc, + } + }, + ) + .prop_filter("non-overlapping pointers", |e| { + MemArea::non_overlapping_set(&[ + &e.input_first_loc, + &e.input_struct_loc, + &e.return_loc, + ]) + }) + .boxed() + } + pub fn test(&self) { + let mut ctx = WasiCtx::new(); + let mut host_memory = HostMemory::new(); + let mut guest_memory = host_memory.guest_memory(); + + *guest_memory + .ptr_mut(self.input_first_loc.ptr) + .expect("input_first ptr") + .as_ref_mut() + .expect("input_first ref") = self.input_first; + *guest_memory + .ptr_mut(self.input_struct_loc.ptr) + .expect("input_struct ptr") + .as_ref_mut() + .expect("input_struct ref") = self.input_first_loc.ptr; + *guest_memory + .ptr_mut(self.input_struct_loc.ptr + 4) + .expect("input_struct ptr") + .as_ref_mut() + .expect("input_struct ref") = self.input_second; + + let res = structs::sum_of_int_and_ptr( + &mut ctx, + &mut guest_memory, + self.input_struct_loc.ptr as i32, + self.return_loc.ptr as i32, + ); + + assert_eq!(res, types::Errno::Ok.into(), "sum of int and ptr errno"); + + let doubled: i64 = *guest_memory + .ptr(self.return_loc.ptr) + .expect("return ptr") + .as_ref() + .expect("return ref"); + + assert_eq!( + doubled, + (self.input_first as i64) + (self.input_second as i64), + "sum of pair of ptrs return val" + ); + } +} +proptest! { + #[test] + fn sum_of_int_and_ptr(e in SumIntAndPtrExercise::strat()) { + e.test() + } +} diff --git a/tests/structs.witx b/tests/structs.witx index e9b4140c1e..c6bb8bb6b0 100644 --- a/tests/structs.witx +++ b/tests/structs.witx @@ -11,6 +11,11 @@ (field $first (@witx const_pointer s32)) (field $second (@witx const_pointer s32)))) +(typename $pair_int_and_ptr + (struct + (field $first (@witx const_pointer s32)) + (field $second s32))) + (module $structs (@interface func (export "sum_of_pair") (param $an_pair $pair_ints) @@ -20,4 +25,8 @@ (param $an_pair $pair_int_ptrs) (result $error $errno) (result $doubled s64)) + (@interface func (export "sum_of_int_and_ptr") + (param $an_pair $pair_int_and_ptr) + (result $error $errno) + (result $double s64)) )