mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Added traps tests
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1907,6 +1907,7 @@ dependencies = [
|
||||
"colored",
|
||||
"distance",
|
||||
"glob",
|
||||
"lazy_static",
|
||||
"rustc_version",
|
||||
"structopt",
|
||||
"test-generator",
|
||||
|
||||
8
lib/api/src/externals/function.rs
vendored
8
lib/api/src/externals/function.rs
vendored
@@ -176,18 +176,14 @@ impl Function {
|
||||
let signature = self.ty();
|
||||
if signature.params().len() != params.len() {
|
||||
return Err(RuntimeError::new(format!(
|
||||
"expected {} arguments, got {}: Parameters of type [{}] did not match signature {}",
|
||||
signature.params().len(),
|
||||
params.len(),
|
||||
"Parameters of type [{}] did not match signature {}",
|
||||
format_types_for_error_message(params),
|
||||
&signature
|
||||
)));
|
||||
}
|
||||
if signature.results().len() != results.len() {
|
||||
return Err(RuntimeError::new(format!(
|
||||
"expected {} results, got {}: Results of type [{}] did not match signature {}",
|
||||
signature.results().len(),
|
||||
results.len(),
|
||||
"Results of type [{}] did not match signature {}",
|
||||
format_types_for_error_message(results),
|
||||
&signature,
|
||||
)));
|
||||
|
||||
@@ -50,7 +50,7 @@ int main()
|
||||
int error_result = wasmer_last_error_message(error_str, error_len);
|
||||
assert(error_len == error_result);
|
||||
printf("Error str: `%s`\n", error_str);
|
||||
assert(0 == strcmp(error_str, "RuntimeError: expected 2 arguments, got 1: Parameters of type [I32] did not match signature [I32, I32] -> [I32]"));
|
||||
assert(0 == strcmp(error_str, "RuntimeError: Parameters of type [I32] did not match signature [I32, I32] -> [I32]"));
|
||||
free(error_str);
|
||||
|
||||
printf("Destroy instance\n");
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
mod macros;
|
||||
mod imports;
|
||||
mod multi_value_imports;
|
||||
mod traps;
|
||||
mod wast;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
473
tests/compilers/traps.rs
Normal file
473
tests/compilers/traps.rs
Normal file
@@ -0,0 +1,473 @@
|
||||
wasmer_compilers! {
|
||||
use anyhow::Result;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use wasmer::*;
|
||||
|
||||
#[test]
|
||||
fn test_trap_return() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module
|
||||
(func $hello (import "" "hello"))
|
||||
(func (export "run") (call $hello))
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let hello_type = FunctionType::new(vec![], vec![]);
|
||||
let hello_func =
|
||||
Function::new_dynamic(&store, &hello_type, |_| Err(RuntimeError::new("test 123")));
|
||||
|
||||
let instance = Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"hello" => hello_func
|
||||
}
|
||||
},
|
||||
)?;
|
||||
let run_func = instance
|
||||
.exports
|
||||
.get_function("run")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = run_func.call(&[]).err().expect("error calling function");
|
||||
|
||||
assert_eq!(e.message(), "test 123");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_trap_trace() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $hello_mod
|
||||
(func (export "run") (call $hello))
|
||||
(func $hello (unreachable))
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let run_func = instance
|
||||
.exports
|
||||
.get_function("run")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = run_func.call(&[]).err().expect("error calling function");
|
||||
|
||||
let trace = e.trace();
|
||||
assert_eq!(trace.len(), 2);
|
||||
assert_eq!(trace[0].module_name(), "hello_mod");
|
||||
assert_eq!(trace[0].func_index(), 1);
|
||||
assert_eq!(trace[0].func_name(), Some("hello"));
|
||||
assert_eq!(trace[1].module_name(), "hello_mod");
|
||||
assert_eq!(trace[1].func_index(), 0);
|
||||
assert_eq!(trace[1].func_name(), None);
|
||||
assert!(
|
||||
e.message().contains("unreachable"),
|
||||
"wrong message: {}",
|
||||
e.message()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trap_trace_cb() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $hello_mod
|
||||
(import "" "throw" (func $throw))
|
||||
(func (export "run") (call $hello))
|
||||
(func $hello (call $throw))
|
||||
)
|
||||
"#;
|
||||
|
||||
let fn_type = FunctionType::new(vec![], vec![]);
|
||||
let fn_func = Function::new_dynamic(&store, &fn_type, |_| Err(RuntimeError::new("cb throw")));
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"throw" => fn_func
|
||||
}
|
||||
},
|
||||
)?;
|
||||
let run_func = instance
|
||||
.exports
|
||||
.get_function("run")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = run_func.call(&[]).err().expect("error calling function");
|
||||
|
||||
let trace = e.trace();
|
||||
println!("Trace {:?}", trace);
|
||||
// TODO: Reenable this (disabled as it was not working with llvm/singlepass)
|
||||
// assert_eq!(trace.len(), 2);
|
||||
// assert_eq!(trace[0].module_name(), "hello_mod");
|
||||
// assert_eq!(trace[0].func_index(), 2);
|
||||
// assert_eq!(trace[1].module_name(), "hello_mod");
|
||||
// assert_eq!(trace[1].func_index(), 1);
|
||||
assert_eq!(e.message(), "cb throw");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trap_stack_overflow() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $rec_mod
|
||||
(func $run (export "run") (call $run))
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let run_func = instance
|
||||
.exports
|
||||
.get_function("run")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = run_func.call(&[]).err().expect("error calling function");
|
||||
|
||||
let trace = e.trace();
|
||||
// assert!(trace.len() >= 32);
|
||||
for i in 0..trace.len() {
|
||||
assert_eq!(trace[i].module_name(), "rec_mod");
|
||||
assert_eq!(trace[i].func_index(), 0);
|
||||
assert_eq!(trace[i].func_name(), Some("run"));
|
||||
}
|
||||
assert!(e.message().contains("call stack exhausted"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn trap_display_pretty() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $m
|
||||
(func $die unreachable)
|
||||
(func call $die)
|
||||
(func $foo call 1)
|
||||
(func (export "bar") call $foo)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let run_func = instance
|
||||
.exports
|
||||
.get_function("bar")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = run_func.call(&[]).err().expect("error calling function");
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
"\
|
||||
RuntimeError: unreachable
|
||||
at die (m[0]:0x23)
|
||||
at <unnamed> (m[1]:0x27)
|
||||
at foo (m[2]:0x2c)
|
||||
at <unnamed> (m[3]:0x31)"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn trap_display_multi_module() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $a
|
||||
(func $die unreachable)
|
||||
(func call $die)
|
||||
(func $foo call 1)
|
||||
(func (export "bar") call $foo)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let bar = instance.exports.get_function("bar")?.clone();
|
||||
|
||||
let wat = r#"
|
||||
(module $b
|
||||
(import "" "" (func $bar))
|
||||
(func $middle call $bar)
|
||||
(func (export "bar2") call $middle)
|
||||
)
|
||||
"#;
|
||||
let module = Module::new(&store, wat)?;
|
||||
let instance = Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"" => bar
|
||||
}
|
||||
},
|
||||
)?;
|
||||
let bar2 = instance
|
||||
.exports
|
||||
.get_function("bar2")
|
||||
.expect("expected function export");
|
||||
|
||||
let e = bar2.call(&[]).err().expect("error calling function");
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
"\
|
||||
RuntimeError: unreachable
|
||||
at die (a[0]:0x23)
|
||||
at <unnamed> (a[1]:0x27)
|
||||
at foo (a[2]:0x2c)
|
||||
at <unnamed> (a[3]:0x31)
|
||||
at middle (b[1]:0x29)
|
||||
at <unnamed> (b[2]:0x2e)"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trap_start_function_import() -> Result<()> {
|
||||
let store = get_store();
|
||||
let binary = r#"
|
||||
(module $a
|
||||
(import "" "" (func $foo))
|
||||
(start $foo)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, &binary)?;
|
||||
let sig = FunctionType::new(vec![], vec![]);
|
||||
let func = Function::new_dynamic(&store, &sig, |_| Err(RuntimeError::new("user trap")));
|
||||
let err = Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"" => func
|
||||
}
|
||||
},
|
||||
)
|
||||
.err()
|
||||
.unwrap();
|
||||
match err {
|
||||
InstantiationError::Link(_) => panic!("It should be a start error"),
|
||||
InstantiationError::Start(err) => {
|
||||
assert_eq!(err.message(), "user trap");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_panic_import() -> Result<()> {
|
||||
let store = get_store();
|
||||
let binary = r#"
|
||||
(module $a
|
||||
(import "" "foo" (func $foo))
|
||||
(import "" "bar" (func $bar))
|
||||
(func (export "foo") call $foo)
|
||||
(func (export "bar") call $bar)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, &binary)?;
|
||||
let sig = FunctionType::new(vec![], vec![]);
|
||||
let func = Function::new_dynamic(&store, &sig, |_| panic!("this is a panic"));
|
||||
let instance = Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"foo" => func,
|
||||
"bar" => Function::new(&store, || panic!("this is another panic"))
|
||||
}
|
||||
},
|
||||
)?;
|
||||
let func = instance.exports.get_function("foo")?.clone();
|
||||
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
drop(func.call(&[]));
|
||||
}))
|
||||
.unwrap_err();
|
||||
assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic"));
|
||||
|
||||
// TODO: Reenable this (disabled as it was not working with llvm/singlepass)
|
||||
// It doesnt' work either with cranelift and `--test-threads=1`.
|
||||
// let func = instance.exports.get_function("bar")?.clone();
|
||||
// let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
// drop(func.call(&[]));
|
||||
// }))
|
||||
// .unwrap_err();
|
||||
// assert_eq!(
|
||||
// err.downcast_ref::<&'static str>(),
|
||||
// Some(&"this is another panic")
|
||||
// );
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_panic_start_function() -> Result<()> {
|
||||
let store = get_store();
|
||||
let binary = r#"
|
||||
(module $a
|
||||
(import "" "" (func $foo))
|
||||
(start $foo)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, &binary)?;
|
||||
let sig = FunctionType::new(vec![], vec![]);
|
||||
let func = Function::new_dynamic(&store, &sig, |_| panic!("this is a panic"));
|
||||
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
drop(Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"" => func
|
||||
}
|
||||
},
|
||||
));
|
||||
}))
|
||||
.unwrap_err();
|
||||
assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic"));
|
||||
|
||||
let func = Function::new(&store, || panic!("this is another panic"));
|
||||
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
drop(Instance::new(
|
||||
&module,
|
||||
&imports! {
|
||||
"" => {
|
||||
"" => func
|
||||
}
|
||||
},
|
||||
));
|
||||
}))
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
err.downcast_ref::<&'static str>(),
|
||||
Some(&"this is another panic")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_arguments() -> Result<()> {
|
||||
let store = get_store();
|
||||
let binary = r#"
|
||||
(module $a
|
||||
(func (export "foo") (param i32))
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, &binary)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let func: &Function = instance.exports.get("foo")?;
|
||||
assert_eq!(
|
||||
func.call(&[]).unwrap_err().message(),
|
||||
"Parameters of type [] did not match signature [I32] -> []"
|
||||
);
|
||||
assert_eq!(
|
||||
func.call(&[Val::F32(0.0)]).unwrap_err().message(),
|
||||
"Parameters of type [F32] did not match signature [I32] -> []",
|
||||
);
|
||||
assert_eq!(
|
||||
func.call(&[Val::I32(0), Val::I32(1)])
|
||||
.unwrap_err()
|
||||
.message(),
|
||||
"Parameters of type [I32, I32] did not match signature [I32] -> []"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn call_signature_mismatch() -> Result<()> {
|
||||
let store = get_store();
|
||||
let binary = r#"
|
||||
(module $a
|
||||
(func $foo
|
||||
i32.const 0
|
||||
call_indirect)
|
||||
(func $bar (param i32))
|
||||
(start $foo)
|
||||
|
||||
(table 1 anyfunc)
|
||||
(elem (i32.const 0) 1)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, &binary)?;
|
||||
let err = Instance::new(&module, &imports! {})
|
||||
.err()
|
||||
.expect("expected error");
|
||||
assert_eq!(
|
||||
format!("{}", err),
|
||||
"\
|
||||
RuntimeError: indirect call type mismatch
|
||||
at foo (a[0]:0x30)\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn start_trap_pretty() -> Result<()> {
|
||||
let store = get_store();
|
||||
let wat = r#"
|
||||
(module $m
|
||||
(func $die unreachable)
|
||||
(func call $die)
|
||||
(func $foo call 1)
|
||||
(func $start call $foo)
|
||||
(start $start)
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(&store, wat)?;
|
||||
let err = Instance::new(&module, &imports! {})
|
||||
.err()
|
||||
.expect("expected error");
|
||||
|
||||
assert_eq!(
|
||||
format!("{}", err),
|
||||
"\
|
||||
RuntimeError: unreachable
|
||||
at die (m[0]:0x1d)
|
||||
at <unnamed> (m[1]:0x21)
|
||||
at foo (m[2]:0x26)
|
||||
at start (m[3]:0x2b)\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn present_after_module_drop() -> Result<()> {
|
||||
let store = get_store();
|
||||
let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?;
|
||||
let instance = Instance::new(&module, &imports! {})?;
|
||||
let func: Function = instance.exports.get_function("foo")?.clone();
|
||||
|
||||
println!("asserting before we drop modules");
|
||||
assert_trap(func.call(&[]).unwrap_err());
|
||||
drop((instance, module));
|
||||
|
||||
println!("asserting after drop");
|
||||
assert_trap(func.call(&[]).unwrap_err());
|
||||
return Ok(());
|
||||
|
||||
fn assert_trap(t: RuntimeError) {
|
||||
println!("{}", t);
|
||||
// assert_eq!(t.trace().len(), 1);
|
||||
// assert_eq!(t.trace()[0].func_index(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user