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.
This commit is contained in:
committed by
GitHub
parent
128c3bd749
commit
b06ed39c1e
@@ -23,9 +23,9 @@ impl types::GuestErrorConversion for WasiCtx {
|
||||
}
|
||||
|
||||
impl types::UserErrorConversion for WasiCtx {
|
||||
fn errno_from_error(&self, e: Error) -> Errno {
|
||||
fn errno_from_error(&self, e: Error) -> Result<Errno, String> {
|
||||
debug!("Error: {:?}", e);
|
||||
e.into()
|
||||
Ok(e.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@ pub fn define(args: TokenStream) -> TokenStream {
|
||||
|
||||
let mut ret = TokenStream::new();
|
||||
|
||||
let old = true;
|
||||
|
||||
for module in doc.modules() {
|
||||
for func in module.funcs() {
|
||||
// `proc_exit` is special; it's essentially an unwinding primitive,
|
||||
@@ -24,14 +22,14 @@ pub fn define(args: TokenStream) -> TokenStream {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.extend(generate_wrappers(&func, old));
|
||||
ret.extend(generate_wrappers(&func));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
fn generate_wrappers(func: &witx::InterfaceFunc, old: bool) -> TokenStream {
|
||||
fn generate_wrappers(func: &witx::InterfaceFunc) -> TokenStream {
|
||||
let name = format_ident!("{}", func.name.as_str());
|
||||
let mut arg_declarations = Vec::new();
|
||||
let mut arg_names = Vec::new();
|
||||
@@ -101,33 +99,13 @@ fn generate_wrappers(func: &witx::InterfaceFunc, old: bool) -> TokenStream {
|
||||
}
|
||||
};
|
||||
|
||||
let c_abi_name = if old {
|
||||
format_ident!("old_wasi_common_{}", name)
|
||||
} else {
|
||||
format_ident!("wasi_common_{}", name)
|
||||
};
|
||||
|
||||
quote! {
|
||||
pub unsafe fn #name(
|
||||
wasi_ctx: &mut super::WasiCtx,
|
||||
memory: &mut [u8],
|
||||
#(#arg_declarations,)*
|
||||
) -> #ret {
|
||||
#body
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn #c_abi_name(
|
||||
wasi_ctx: *mut super::WasiCtx,
|
||||
memory: *mut u8,
|
||||
memory_len: usize,
|
||||
#(#arg_declarations,)*
|
||||
) -> #ret {
|
||||
#name(
|
||||
&mut *wasi_ctx,
|
||||
std::slice::from_raw_parts_mut(memory, memory_len),
|
||||
#(#arg_names,)*
|
||||
)
|
||||
) -> Result<#ret, String> {
|
||||
Ok({#body})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ pub fn define_struct(args: TokenStream) -> TokenStream {
|
||||
|
||||
// The first result is returned bare right now...
|
||||
if let Some(ret) = results.next() {
|
||||
handle_early_error = quote! { return e.into() };
|
||||
handle_early_error = quote! { return Ok(e.into()) };
|
||||
match &*ret.tref.type_() {
|
||||
// Eventually we'll want to add support for more returned
|
||||
// types, but for now let's just conform to what `*.witx`
|
||||
@@ -200,7 +200,7 @@ pub fn define_struct(args: TokenStream) -> TokenStream {
|
||||
let my_cx = cx.clone();
|
||||
let #name_ident = wasmtime::Func::wrap(
|
||||
store,
|
||||
move |caller: wasmtime::Caller<'_> #(,#shim_arg_decls)*| -> #ret_ty {
|
||||
move |caller: wasmtime::Caller<'_> #(,#shim_arg_decls)*| -> Result<#ret_ty, wasmtime::Trap> {
|
||||
tracing::trace!(
|
||||
#format_str,
|
||||
#(#format_args),*
|
||||
@@ -214,11 +214,15 @@ pub fn define_struct(args: TokenStream) -> TokenStream {
|
||||
#handle_early_error
|
||||
}
|
||||
};
|
||||
hostcalls::#name_ident(
|
||||
let result = hostcalls::#name_ident(
|
||||
&mut my_cx.borrow_mut(),
|
||||
memory.data_unchecked_mut(),
|
||||
#(#hostcall_args),*
|
||||
) #cvt_ret
|
||||
);
|
||||
match result {
|
||||
Ok(r) => { return Ok(r #cvt_ret); },
|
||||
Err(err) => { return Err(wasmtime::Trap::new(err)); },
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user