Remove the need to have a Store for an InstancePre (#5683)

* Remove the need to have a `Store` for an `InstancePre`

This commit relaxes a requirement of the `InstancePre` API, notably its
construction via `Linker::instantiate_pre`. Previously this function
required a `Store<T>` to be present to be able to perform type-checking
on the contents of the linker, and now this requirement has been
removed.

Items stored within a linker are either a `HostFunc`, which has type
information inside of it, or an `Extern`, which doesn't have type
information inside of it. Due to the usage of `Extern` this is why a
`Store` was required during the `InstancePre` construction process, it's
used to extract the type of an `Extern`. This commit implements a
solution where the type information of an `Extern` is stored alongside
the `Extern` itself, meaning that the `InstancePre` construction process
no longer requires a `Store<T>`.

One caveat of this implementation is that some items, such as tables and
memories, technically have a "dynamic type" where during type checking
their current size is consulted to match against the minimum size
required of an import. This no longer works when using
`Linker::instantiate_pre` as the current size used is the one when it
was inserted into the linker rather than the one available at
instantiation time. It's hoped, however, that this is a relatively
esoteric use case that doesn't impact many real-world users.

Additionally note that this is an API-breaking change. Not only is the
`Store` argument removed from `Linker::instantiate_pre`, but some other
methods such as `Linker::define` grew a `Store` argument as the type
needs to be extracted when an item is inserted into a linker.

Closes #5675

* Fix the C API

* Fix benchmark compilation

* Add C API docs

* Update crates/wasmtime/src/linker.rs

Co-authored-by: Andrew Brown <andrew.brown@intel.com>

---------

Co-authored-by: Andrew Brown <andrew.brown@intel.com>
This commit is contained in:
Alex Crichton
2023-02-02 11:54:20 -06:00
committed by GitHub
parent f5f517e811
commit 63d80fc509
13 changed files with 242 additions and 224 deletions

View File

@@ -262,7 +262,7 @@ async fn call_linked_func_async() -> Result<(), Error> {
let mut linker = Linker::new(&engine);
linker.define("host", "f", f)?;
linker.define(&mut store, "host", "f", f)?;
let wat = r#"
(module

View File

@@ -56,36 +56,36 @@ fn link_twice_bad() -> Result<()> {
// globals
let ty = GlobalType::new(ValType::I32, Mutability::Const);
let global = Global::new(&mut store, ty, Val::I32(0))?;
linker.define("g", "1", global.clone())?;
assert!(linker.define("g", "1", global.clone()).is_err());
linker.define(&mut store, "g", "1", global.clone())?;
assert!(linker.define(&mut store, "g", "1", global.clone()).is_err());
let ty = GlobalType::new(ValType::I32, Mutability::Var);
let global = Global::new(&mut store, ty, Val::I32(0))?;
linker.define("g", "2", global.clone())?;
assert!(linker.define("g", "2", global.clone()).is_err());
linker.define(&mut store, "g", "2", global.clone())?;
assert!(linker.define(&mut store, "g", "2", global.clone()).is_err());
let ty = GlobalType::new(ValType::I64, Mutability::Const);
let global = Global::new(&mut store, ty, Val::I64(0))?;
linker.define("g", "3", global.clone())?;
assert!(linker.define("g", "3", global.clone()).is_err());
linker.define(&mut store, "g", "3", global.clone())?;
assert!(linker.define(&mut store, "g", "3", global.clone()).is_err());
// memories
let ty = MemoryType::new(1, None);
let memory = Memory::new(&mut store, ty)?;
linker.define("m", "", memory.clone())?;
assert!(linker.define("m", "", memory.clone()).is_err());
linker.define(&mut store, "m", "", memory.clone())?;
assert!(linker.define(&mut store, "m", "", memory.clone()).is_err());
let ty = MemoryType::new(2, None);
let memory = Memory::new(&mut store, ty)?;
assert!(linker.define("m", "", memory.clone()).is_err());
assert!(linker.define(&mut store, "m", "", memory.clone()).is_err());
// tables
let ty = TableType::new(ValType::FuncRef, 1, None);
let table = Table::new(&mut store, ty, Val::FuncRef(None))?;
linker.define("t", "", table.clone())?;
assert!(linker.define("t", "", table.clone()).is_err());
linker.define(&mut store, "t", "", table.clone())?;
assert!(linker.define(&mut store, "t", "", table.clone()).is_err());
let ty = TableType::new(ValType::FuncRef, 2, None);
let table = Table::new(&mut store, ty, Val::FuncRef(None))?;
assert!(linker.define("t", "", table.clone()).is_err());
assert!(linker.define(&mut store, "t", "", table.clone()).is_err());
Ok(())
}
@@ -100,11 +100,8 @@ fn function_interposition() -> Result<()> {
)?;
for _ in 0..4 {
let instance = linker.instantiate(&mut store, &module)?;
linker.define(
"red",
"green",
instance.get_export(&mut store, "green").unwrap().clone(),
)?;
let green = instance.get_export(&mut store, "green").unwrap().clone();
linker.define(&mut store, "red", "green", green)?;
module = Module::new(
store.engine(),
r#"(module
@@ -137,11 +134,8 @@ fn function_interposition_renamed() -> Result<()> {
)?;
for _ in 0..4 {
let instance = linker.instantiate(&mut store, &module)?;
linker.define(
"red",
"green",
instance.get_export(&mut store, "export").unwrap().clone(),
)?;
let export = instance.get_export(&mut store, "export").unwrap().clone();
linker.define(&mut store, "red", "green", export)?;
module = Module::new(
store.engine(),
r#"(module
@@ -334,7 +328,7 @@ fn instance_pre() -> Result<()> {
linker.func_wrap("", "", || {})?;
let module = Module::new(&engine, r#"(module (import "" "" (func)))"#)?;
let instance_pre = linker.instantiate_pre(&mut Store::new(&engine, ()), &module)?;
let instance_pre = linker.instantiate_pre(&module)?;
instance_pre.instantiate(&mut Store::new(&engine, ()))?;
instance_pre.instantiate(&mut Store::new(&engine, ()))?;
@@ -344,7 +338,7 @@ fn instance_pre() -> Result<()> {
GlobalType::new(ValType::I32, Mutability::Const),
1.into(),
)?;
linker.define("", "g", global)?;
linker.define(&mut store, "", "g", global)?;
let module = Module::new(
&engine,
@@ -353,7 +347,7 @@ fn instance_pre() -> Result<()> {
(import "" "g" (global i32))
)"#,
)?;
let instance_pre = linker.instantiate_pre(&mut store, &module)?;
let instance_pre = linker.instantiate_pre(&module)?;
instance_pre.instantiate(&mut store)?;
instance_pre.instantiate(&mut store)?;
Ok(())