Files
wasmtime/crates/wiggle/tests/handles.rs
Tanya L. Crenshaw b06ed39c1e Fixes #2418: Enhance wiggle to generate its UserErrorConverstion trait with a function that returns Result<abi_err, String> (#2419)
* 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.
2020-11-24 14:06:57 -06:00

75 lines
1.9 KiB
Rust

use proptest::prelude::*;
use wiggle::{GuestMemory, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
const FD_VAL: u32 = 123;
wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/handles.witx"],
ctx: WasiCtx,
});
impl_errno!(types::Errno, types::GuestErrorConversion);
impl<'a> handle_examples::HandleExamples for WasiCtx<'a> {
fn fd_create(&self) -> Result<types::Fd, types::Errno> {
Ok(types::Fd::from(FD_VAL))
}
fn fd_consume(&self, fd: types::Fd) -> Result<(), types::Errno> {
println!("FD_CONSUME {}", fd);
if fd == types::Fd::from(FD_VAL) {
Ok(())
} else {
Err(types::Errno::InvalidArg)
}
}
}
#[derive(Debug)]
struct HandleExercise {
pub return_loc: MemArea,
}
impl HandleExercise {
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let e = handle_examples::fd_create(&ctx, &host_memory, self.return_loc.ptr as i32);
assert_eq!(e, Ok(types::Errno::Ok.into()), "fd_create error");
let h_got: u32 = host_memory
.ptr(self.return_loc.ptr)
.read()
.expect("return ref_mut");
assert_eq!(h_got, 123, "fd_create return val");
let e = handle_examples::fd_consume(&ctx, &host_memory, h_got as i32);
assert_eq!(e, Ok(types::Errno::Ok.into()), "fd_consume error");
let e = handle_examples::fd_consume(&ctx, &host_memory, h_got as i32 + 1);
assert_eq!(
e,
Ok(types::Errno::InvalidArg.into()),
"fd_consume invalid error"
);
}
pub fn strat() -> BoxedStrategy<Self> {
(HostMemory::mem_area_strat(types::Fd::guest_size()))
.prop_map(|return_loc| HandleExercise { return_loc })
.boxed()
}
}
proptest! {
#[test]
fn handle_exercise(e in HandleExercise::strat()) {
e.test()
}
}