Check type when resuming function

This commit is contained in:
Jef 2018-12-11 12:56:36 +01:00
parent 899cc32e45
commit c8bae0f4b1
3 changed files with 74 additions and 9 deletions

View File

@ -293,16 +293,23 @@ impl<'args> FuncInvocation<'args> {
return_val: Option<RuntimeValue>,
externals: &'externals mut E,
) -> Result<Option<RuntimeValue>, ResumableError> {
match self.kind {
FuncInvocationKind::Internal(ref mut interpreter) => {
if !interpreter.state().is_resumable() {
return Err(ResumableError::AlreadyStarted);
use crate::TrapKind;
if return_val.map(|v| v.value_type()) != self.resumable_value_type() {
return Err(ResumableError::Trap(Trap::new(
TrapKind::UnexpectedSignature,
)));
}
match &mut self.kind {
FuncInvocationKind::Internal(interpreter) => {
if interpreter.state().is_resumable() {
Ok(interpreter.resume_execution(return_val, externals)?)
} else {
Err(ResumableError::AlreadyStarted)
}
Ok(interpreter.resume_execution(return_val, externals)?)
}
FuncInvocationKind::Host { .. } => {
return Err(ResumableError::NotResumable);
}
FuncInvocationKind::Host { .. } => Err(ResumableError::NotResumable),
}
}
}

View File

@ -136,7 +136,8 @@ extern crate libm;
/// Traps can't be handled by WebAssembly code, but are reported to the embedder.
#[derive(Debug)]
pub struct Trap {
kind: TrapKind,
// Needs to be `pub(crate)` to allow `if let` matching in tests
pub(crate) kind: TrapKind,
}
impl Trap {

View File

@ -302,6 +302,63 @@ fn resume_call_host_func() {
);
}
#[test]
fn resume_call_host_func_type_mismatch() {
fn resume_with_val(val: Option<RuntimeValue>) {
let module = parse_wat(
r#"
(module
(import "env" "trap_sub" (func $trap_sub (param i32 i32) (result i32)))
(func (export "test") (result i32)
(call $trap_sub
(i32.const 5)
(i32.const 7)
)
)
)
"#,
);
let mut env = TestHost::new();
let instance =
ModuleInstance::new(&module, &ImportsBuilder::new().with_resolver("env", &env))
.expect("Failed to instantiate module")
.assert_no_start();
let export = instance.export_by_name("test").unwrap();
let func_instance = export.as_func().unwrap();
let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[]).unwrap();
let result = invocation.start_execution(&mut env);
match result {
Err(ResumableError::Trap(_)) => {}
_ => panic!(),
}
assert!(invocation.is_resumable());
match invocation
.resume_execution(val, &mut env)
.unwrap_err()
{
ResumableError::Trap(Trap {
kind: TrapKind::UnexpectedSignature,
}) => {
// success
}
other => panic!(
"Expected `ResumableError::Trap(Trap {{ kind: \
TrapKind::UnexpectedSignature, }})`, got `{:?}`",
other
),
};
}
resume_with_val(None);
resume_with_val(Some((-1i64).into()));
}
#[test]
fn host_err() {
let module = parse_wat(