Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasi-nn: trap instead of panicking on guest memory errors (#14884)

netliomax25-code887712e1d856
diff --git a/crates/wasi-nn/Cargo.toml b/crates/wasi-nn/Cargo.toml
index ecfd388470..594438c14d 100644
--- a/crates/wasi-nn/Cargo.toml
+++ b/crates/wasi-nn/Cargo.toml
@@ -57,7 +57,7 @@ walkdir = { workspace = true }
libtest-mimic = { workspace = true }
test-programs-artifacts = { workspace = true }
wasmtime-wasi = { workspace = true, features = ["p1"] }
-wasmtime = { workspace = true, features = ["cranelift"] }
+wasmtime = { workspace = true, features = ["cranelift", "wat"] }
tracing-subscriber = { workspace = true }
[features]
diff --git a/crates/wasi-nn/src/witx.rs b/crates/wasi-nn/src/witx.rs
index 3a03b41dbf..354d2b04ee 100644
--- a/crates/wasi-nn/src/witx.rs
+++ b/crates/wasi-nn/src/witx.rs
@@ -115,7 +115,7 @@ mod generated {
tracing::debug!("host error: {:?}", e);
match e {
WasiNnError::BackendError(_) => Ok(types::NnErrno::RuntimeError),
- WasiNnError::GuestError(_) => unimplemented!("guest error conversion"),
+ WasiNnError::GuestError(e) => Err(e.into()),
WasiNnError::UsageError(_) => Ok(types::NnErrno::UnsupportedOperation),
WasiNnError::NotEnoughMemory(_) => Ok(types::NnErrno::TooLarge),
}
@@ -307,3 +307,41 @@ pub enum UsageError {
#[error("No graph found with name: {0}")]
NotFound(String),
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::InMemoryRegistry;
+ use wasmtime::{Engine, Linker, Module, Store};
+
+ #[test]
+ fn guest_memory_errors_trap() -> wasmtime::Result<()> {
+ let engine = Engine::default();
+ let mut linker = Linker::<WasiNnCtx>::new(&engine);
+ add_to_linker(&mut linker, |s| s)?;
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (import "wasi_ephemeral_nn" "load_by_name"
+ (func $load_by_name (param i32 i32 i32) (result i32)))
+ (memory (export "memory") 1)
+ (data (i32.const 0) "\ff")
+ (func (export "out-of-bounds") (result i32)
+ (call $load_by_name (i32.const 0xffff0000) (i32.const 16) (i32.const 8)))
+ (func (export "invalid-utf8") (result i32)
+ (call $load_by_name (i32.const 0) (i32.const 1) (i32.const 8))))"#,
+ )?;
+ for name in ["out-of-bounds", "invalid-utf8"] {
+ let ctx = WasiNnCtx::new([], InMemoryRegistry::new().into());
+ let mut store = Store::new(&engine, ctx);
+ let instance = linker.instantiate(&mut store, &module)?;
+ let func = instance.get_typed_func::<(), i32>(&mut store, name)?;
+ let err = func.call(&mut store, ()).unwrap_err();
+ assert!(
+ err.downcast_ref::<GuestError>().is_some(),
+ "{name}: {err:?}"
+ );
+ }
+ Ok(())
+ }
+}