Add Wasmtime-specific C API functions to return errors (#1467)

* Add Wasmtime-specific C API functions to return errors

This commit adds new `wasmtime_*` symbols to the C API, many of which
mirror the existing counterparts in the `wasm.h` header. These APIs are
enhanced in a number of respects:

* Detailed error information is now available through a
  `wasmtime_error_t`. Currently this only exposes one function which is
  to extract a string version of the error.

* There is a distinction now between traps and errors during
  instantiation and function calling. Traps only happen if wasm traps,
  and errors can happen for things like runtime type errors when
  interacting with the API.

* APIs have improved safety with respect to embedders where the lengths
  of arrays are now taken as explicit parameters rather than assumed
  from other parameters.

* Handle trap updates

* Update C examples

* Fix memory.c compile on MSVC

* Update test assertions

* Refactor C slightly

* Bare-bones .NET update

* Remove bogus nul handling
This commit is contained in:
Alex Crichton
2020-04-06 15:13:06 -05:00
committed by GitHub
parent 78c548dc8f
commit bd374fd6fc
30 changed files with 817 additions and 403 deletions

View File

@@ -174,11 +174,12 @@ fn import_works() -> Result<()> {
}
#[test]
fn trap_smoke() {
fn trap_smoke() -> Result<()> {
let store = Store::default();
let f = Func::wrap(&store, || -> Result<(), Trap> { Err(Trap::new("test")) });
let err = f.call(&[]).unwrap_err();
let err = f.call(&[]).unwrap_err().downcast::<Trap>()?;
assert_eq!(err.message(), "test");
Ok(())
}
#[test]
@@ -391,7 +392,7 @@ fn func_write_nothing() -> anyhow::Result<()> {
let store = Store::default();
let ty = FuncType::new(Box::new([]), Box::new([ValType::I32]));
let f = Func::new(&store, ty, |_, _, _| Ok(()));
let err = f.call(&[]).unwrap_err();
let err = f.call(&[]).unwrap_err().downcast::<Trap>()?;
assert_eq!(
err.message(),
"function attempted to return an incompatible value"

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use std::cell::RefCell;
use std::rc::Rc;
use wasmtime::*;
@@ -57,7 +58,7 @@ fn test_import_calling_export() {
}
#[test]
fn test_returns_incorrect_type() {
fn test_returns_incorrect_type() -> Result<()> {
const WAT: &str = r#"
(module
(import "env" "evil" (func $evil (result i32)))
@@ -68,7 +69,7 @@ fn test_returns_incorrect_type() {
"#;
let store = Store::default();
let module = Module::new(&store, WAT).expect("failed to create module");
let module = Module::new(&store, WAT)?;
let callback_func = Func::new(
&store,
@@ -81,8 +82,7 @@ fn test_returns_incorrect_type() {
);
let imports = vec![callback_func.into()];
let instance =
Instance::new(&module, imports.as_slice()).expect("failed to instantiate module");
let instance = Instance::new(&module, imports.as_slice())?;
let exports = instance.exports();
assert!(!exports.is_empty());
@@ -91,9 +91,13 @@ fn test_returns_incorrect_type() {
.func()
.expect("expected a run func in the module");
let trap = run_func.call(&[]).expect_err("the execution should fail");
let trap = run_func
.call(&[])
.expect_err("the execution should fail")
.downcast::<Trap>()?;
assert_eq!(
trap.message(),
"function attempted to return an incompatible value"
);
Ok(())
}

View File

@@ -21,7 +21,11 @@ fn test_trap_return() -> Result<()> {
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
let e = run_func
.call(&[])
.err()
.expect("error calling function")
.downcast::<Trap>()?;
assert_eq!(e.message(), "test 123");
@@ -44,7 +48,11 @@ fn test_trap_trace() -> Result<()> {
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
let e = run_func
.call(&[])
.err()
.expect("error calling function")
.downcast::<Trap>()?;
let trace = e.trace();
assert_eq!(trace.len(), 2);
@@ -83,7 +91,11 @@ fn test_trap_trace_cb() -> Result<()> {
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
let e = run_func
.call(&[])
.err()
.expect("error calling function")
.downcast::<Trap>()?;
let trace = e.trace();
assert_eq!(trace.len(), 2);
@@ -111,7 +123,11 @@ fn test_trap_stack_overflow() -> Result<()> {
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
let e = run_func
.call(&[])
.err()
.expect("error calling function")
.downcast::<Trap>()?;
let trace = e.trace();
assert!(trace.len() >= 32);
@@ -315,17 +331,17 @@ fn mismatched_arguments() -> Result<()> {
let instance = Instance::new(&module, &[])?;
let func = instance.exports()[0].func().unwrap().clone();
assert_eq!(
func.call(&[]).unwrap_err().message(),
func.call(&[]).unwrap_err().to_string(),
"expected 1 arguments, got 0"
);
assert_eq!(
func.call(&[Val::F32(0)]).unwrap_err().message(),
func.call(&[Val::F32(0)]).unwrap_err().to_string(),
"argument type mismatch",
);
assert_eq!(
func.call(&[Val::I32(0), Val::I32(1)])
.unwrap_err()
.message(),
.to_string(),
"expected 1 arguments, got 2"
);
Ok(())