Handle same-named imports with different signatures
This commit fixes the `wasmtime::Instance` instantiation API when imports have the same name but might be imported under different types. This is handled in the API by listing imports as a list instead of as a name map, but they were interpreted as a name map under the hood causing collisions. This commit now keeps track of the index used to define each import, and the index is passed through in the `Resolver`. Existing implementaitons of `Resolver` all ignore this, but the API now uses it exclusivley to match up `Extern` definitions to imports.
This commit is contained in:
committed by
Dan Gohman
parent
e22d93f750
commit
41780fb1a6
@@ -56,7 +56,7 @@ impl Extern {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_wasmtime_export(&mut self) -> wasmtime_runtime::Export {
|
||||
pub(crate) fn get_wasmtime_export(&self) -> wasmtime_runtime::Export {
|
||||
match self {
|
||||
Extern::Func(f) => f.borrow().wasmtime_export().clone(),
|
||||
Extern::Global(g) => g.borrow().wasmtime_export().clone(),
|
||||
|
||||
@@ -12,23 +12,21 @@ use std::rc::Rc;
|
||||
use wasmtime_jit::{instantiate, Resolver, SetupError};
|
||||
use wasmtime_runtime::{Export, InstanceHandle, InstantiationError};
|
||||
|
||||
struct SimpleResolver {
|
||||
imports: Vec<(String, String, Extern)>,
|
||||
struct SimpleResolver<'a> {
|
||||
imports: &'a [Extern],
|
||||
}
|
||||
|
||||
impl Resolver for SimpleResolver {
|
||||
fn resolve(&mut self, name: &str, field: &str) -> Option<Export> {
|
||||
// TODO speedup lookup
|
||||
impl Resolver for SimpleResolver<'_> {
|
||||
fn resolve(&mut self, idx: u32, _name: &str, _field: &str) -> Option<Export> {
|
||||
self.imports
|
||||
.iter_mut()
|
||||
.find(|(n, f, _)| name == n && field == f)
|
||||
.map(|(_, _, e)| e.get_wasmtime_export())
|
||||
.get(idx as usize)
|
||||
.map(|i| i.get_wasmtime_export())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn instantiate_in_context(
|
||||
data: &[u8],
|
||||
imports: Vec<(String, String, Extern)>,
|
||||
imports: &[Extern],
|
||||
module_name: Option<String>,
|
||||
context: Context,
|
||||
exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>,
|
||||
@@ -73,15 +71,9 @@ impl Instance {
|
||||
pub fn new(store: &Store, module: &Module, externs: &[Extern]) -> Result<Instance, Error> {
|
||||
let context = store.context().clone();
|
||||
let exports = store.global_exports().clone();
|
||||
let imports = module
|
||||
.imports()
|
||||
.iter()
|
||||
.zip(externs.iter())
|
||||
.map(|(i, e)| (i.module().to_string(), i.name().to_string(), e.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let (mut instance_handle, contexts) = instantiate_in_context(
|
||||
module.binary().expect("binary"),
|
||||
imports,
|
||||
externs,
|
||||
module.name().cloned(),
|
||||
context,
|
||||
exports,
|
||||
|
||||
68
crates/api/tests/import-indexes.rs
Normal file
68
crates/api/tests/import-indexes.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::rc::Rc;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
fn same_import_names_still_distinct() -> anyhow::Result<()> {
|
||||
const WAT: &str = r#"
|
||||
(module
|
||||
(import "" "" (func $a (result i32)))
|
||||
(import "" "" (func $b (result f32)))
|
||||
(func (export "foo") (result i32)
|
||||
call $a
|
||||
call $b
|
||||
i32.trunc_f32_u
|
||||
i32.add)
|
||||
)
|
||||
"#;
|
||||
|
||||
struct Ret1;
|
||||
|
||||
impl Callable for Ret1 {
|
||||
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Trap> {
|
||||
assert!(params.is_empty());
|
||||
assert_eq!(results.len(), 1);
|
||||
results[0] = 1i32.into();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct Ret2;
|
||||
|
||||
impl Callable for Ret2 {
|
||||
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Trap> {
|
||||
assert!(params.is_empty());
|
||||
assert_eq!(results.len(), 1);
|
||||
results[0] = 2.0f32.into();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let store = Store::default();
|
||||
let wasm = wat::parse_str(WAT)?;
|
||||
let module = Module::new(&store, &wasm)?;
|
||||
|
||||
let imports = [
|
||||
HostRef::new(Func::new(
|
||||
&store,
|
||||
FuncType::new(Box::new([]), Box::new([ValType::I32])),
|
||||
Rc::new(Ret1),
|
||||
))
|
||||
.into(),
|
||||
HostRef::new(Func::new(
|
||||
&store,
|
||||
FuncType::new(Box::new([]), Box::new([ValType::F32])),
|
||||
Rc::new(Ret2),
|
||||
))
|
||||
.into(),
|
||||
];
|
||||
let instance = Instance::new(&store, &module, &imports)?;
|
||||
|
||||
let func = instance.find_export_by_name("foo").unwrap().func().unwrap();
|
||||
let results = func.borrow().call(&[])?;
|
||||
assert_eq!(results.len(), 1);
|
||||
match results[0] {
|
||||
Val::I32(n) => assert_eq!(n, 3),
|
||||
_ => panic!("unexpected type of return"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user