From 3cb03ada7ce247068dbd3e22046ce0703e8c4789 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 Apr 2020 07:12:19 -0700 Subject: [PATCH 1/2] Make too many imports an instantiation error Previously we'd accidentally only take the head of the list when instantiating, but instead this changes the API to require exactly the right number of imports. --- crates/api/src/instance.rs | 8 ++++++++ crates/api/tests/instance.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 crates/api/tests/instance.rs diff --git a/crates/api/src/instance.rs b/crates/api/src/instance.rs index 391754c42b52..d60c3d17f90c 100644 --- a/crates/api/src/instance.rs +++ b/crates/api/src/instance.rs @@ -124,6 +124,14 @@ impl Instance { } } + if imports.len() != module.imports().len() { + bail!( + "wrong number of imports provided, {} != {}", + imports.len(), + module.imports().len() + ); + } + module.register_frame_info(); let config = store.engine().config(); let instance_handle = instantiate( diff --git a/crates/api/tests/instance.rs b/crates/api/tests/instance.rs new file mode 100644 index 000000000000..722ab8a087dd --- /dev/null +++ b/crates/api/tests/instance.rs @@ -0,0 +1,13 @@ +use wasmtime::*; +use anyhow::Result; + +#[test] +fn wrong_import_numbers() -> Result<()> { + let store = Store::default(); + let module = Module::new(&store, r#"(module (import "" "" (func)))"#)?; + + assert!(Instance::new(&module, &[]).is_err()); + let func = Func::wrap(&store, || {}); + assert!(Instance::new(&module, &[func.clone().into(), func.into()]).is_err()); + Ok(()) +} From 8e4d2054752e24c2ea1075a054e3f7bbd32231f0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 Apr 2020 10:05:18 -0700 Subject: [PATCH 2/2] rustfmt --- crates/api/tests/instance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/api/tests/instance.rs b/crates/api/tests/instance.rs index 722ab8a087dd..c0f3db86a296 100644 --- a/crates/api/tests/instance.rs +++ b/crates/api/tests/instance.rs @@ -1,5 +1,5 @@ -use wasmtime::*; use anyhow::Result; +use wasmtime::*; #[test] fn wrong_import_numbers() -> Result<()> {