* Enhance wiggle to generate its UserErrorConverstion trait with a function that returns a Result<abi_err, String>. This enhancement allows hostcall implementations using wiggle to return an actionable error to the instance (the abi_err) or to terminate the instance using the String as fatal error information. * Enhance wiggle to generate its UserErrorConverstion trait with a function that returns a Result<abi_err, String>. This enhancement allows hostcall implementations using wiggle to return an actionable error to the instance (the abi_err) or to terminate the instance using the String as fatal error information. * Enhance the wiggle/wasmtime integration to leverage new work in ab7e9c6. Hostcall implementations generated by wiggle now return an Result<abi_error, Trap>. As a result, hostcalls experiencing fatal errors may trap, thereby terminating the wasmtime instance. This enhancement has been performed for both wasi snapshot1 and wasi snapshot0. * Update wasi-nn crate to reflect enhancement in issue #2418. * Update wiggle test-helpers for wiggle enhancement made in issue #2418. * Address PR feedback; omit verbose return statement. * Address PR feedback; manually format within a proc macro. * Address PR feedback; manually format proc macro. * Restore return statements to wasi.rs. * Restore return statements in funcs.rs. * Address PR feedback; omit TODO and fix formatting. * Ok-wrap error type in assert statement.
80 lines
2.0 KiB
Rust
80 lines
2.0 KiB
Rust
use proptest::prelude::*;
|
|
use std::convert::TryFrom;
|
|
use wiggle::GuestMemory;
|
|
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
|
|
|
|
wiggle::from_witx!({
|
|
witx: ["$CARGO_MANIFEST_DIR/tests/ints.witx"],
|
|
ctx: WasiCtx,
|
|
});
|
|
|
|
impl_errno!(types::Errno, types::GuestErrorConversion);
|
|
|
|
impl<'a> ints::Ints for WasiCtx<'a> {
|
|
fn cookie_cutter(&self, init_cookie: types::Cookie) -> Result<types::Bool, types::Errno> {
|
|
let res = if init_cookie == types::Cookie::START {
|
|
types::Bool::True
|
|
} else {
|
|
types::Bool::False
|
|
};
|
|
Ok(res)
|
|
}
|
|
}
|
|
|
|
fn cookie_strat() -> impl Strategy<Value = types::Cookie> {
|
|
(0..std::u64::MAX)
|
|
.prop_map(|x| types::Cookie::try_from(x).expect("within range of cookie"))
|
|
.boxed()
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct CookieCutterExercise {
|
|
cookie: types::Cookie,
|
|
return_ptr_loc: MemArea,
|
|
}
|
|
|
|
impl CookieCutterExercise {
|
|
pub fn strat() -> BoxedStrategy<Self> {
|
|
(cookie_strat(), HostMemory::mem_area_strat(4))
|
|
.prop_map(|(cookie, return_ptr_loc)| Self {
|
|
cookie,
|
|
return_ptr_loc,
|
|
})
|
|
.boxed()
|
|
}
|
|
|
|
pub fn test(&self) {
|
|
let ctx = WasiCtx::new();
|
|
let host_memory = HostMemory::new();
|
|
|
|
let res = ints::cookie_cutter(
|
|
&ctx,
|
|
&host_memory,
|
|
self.cookie.into(),
|
|
self.return_ptr_loc.ptr as i32,
|
|
);
|
|
assert_eq!(res, Ok(types::Errno::Ok.into()), "cookie cutter errno");
|
|
|
|
let is_cookie_start = host_memory
|
|
.ptr::<types::Bool>(self.return_ptr_loc.ptr)
|
|
.read()
|
|
.expect("deref to Bool value");
|
|
|
|
assert_eq!(
|
|
if is_cookie_start == types::Bool::True {
|
|
true
|
|
} else {
|
|
false
|
|
},
|
|
self.cookie == types::Cookie::START,
|
|
"returned Bool should test if input was Cookie::START",
|
|
);
|
|
}
|
|
}
|
|
proptest! {
|
|
#[test]
|
|
fn cookie_cutter(e in CookieCutterExercise::strat()) {
|
|
e.test()
|
|
}
|
|
}
|