Skip to content

Commit f003388

Browse files
authored
Implement imported/exported modules/instances (#2461)
* Implement imported/exported modules/instances This commit implements the final piece of the module linking proposal which is to flesh out the support for importing/exporting instances and modules. This ended up having a few changes: * Two more `PrimaryMap` instances are now stored in an `Instance`. The value for instances is `InstanceHandle` (pretty easy) and for modules it's `Box<dyn Any>` (less easy). * The custom host state for `InstanceHandle` for `wasmtime` is now `Arc<TypeTables` to be able to fully reconstruct an instance's types just from its instance. * Type matching for imports now has been updated to take instances/modules into account. One of the main downsides of this implementation is that type matching of imports is duplicated between wasmparser and wasmtime, leading to posssible bugs especially in the subtelties of module linking. I'm not sure how best to unify these two pieces of validation, however, and it may be more trouble than it's worth. cc #2094 * Update wat/wast/wasmparser * Review comments * Fix a bug in publish script to vendor the right witx Currently there's two witx binaries in our repository given the two wasi spec submodules, so this updates the publication script to vendor the right one.
1 parent b93381e commit f003388

File tree

37 files changed

+1016
-345
lines changed

37 files changed

+1016
-345
lines changed

Cargo.lock

Lines changed: 27 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ anyhow = "1.0.19"
3838
target-lexicon = { version = "0.11.0", default-features = false }
3939
pretty_env_logger = "0.4.0"
4040
file-per-thread-logger = "0.1.1"
41-
wat = "1.0.27"
41+
wat = "1.0.29"
4242
libc = "0.2.60"
4343
log = "0.4.8"
4444
rayon = "1.2.1"
4545
humantime = "2.0.0"
46-
wasmparser = "0.68"
46+
wasmparser = "0.69"
4747

4848
[dev-dependencies]
4949
env_logger = "0.8.1"

cranelift/codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ peepmatic-traits = { path = "../peepmatic/crates/traits", optional = true, versi
3030
peepmatic-runtime = { path = "../peepmatic/crates/runtime", optional = true, version = "0.68.0" }
3131
regalloc = { version = "0.0.31" }
3232
souper-ir = { version = "1", optional = true }
33-
wast = { version = "27.0.0", optional = true }
33+
wast = { version = "28.0.0", optional = true }
3434
# It is a goal of the cranelift-codegen crate to have minimal external dependencies.
3535
# Please don't add any unless they are essential to the task of creating binary
3636
# machine code. Integration tests that need external dependencies can be

cranelift/peepmatic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ peepmatic-macro = { version = "0.68.0", path = "crates/macro" }
1515
peepmatic-runtime = { version = "0.68.0", path = "crates/runtime", features = ["construct"] }
1616
peepmatic-traits = { version = "0.68.0", path = "crates/traits" }
1717
serde = { version = "1.0.105", features = ["derive"] }
18-
wast = "27.0.0"
18+
wast = "28.0.0"
1919
z3 = { version = "0.7.1", features = ["static-link-z3"] }
2020

2121
[dev-dependencies]

cranelift/peepmatic/crates/fuzzing/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ peepmatic-test-operator = { path = "../test-operator" }
2121
peepmatic-traits = { path = "../traits" }
2222
rand = { version = "0.7.3", features = ["small_rng"] }
2323
serde = "1.0.106"
24-
wast = "27.0.0"
24+
wast = "28.0.0"

cranelift/peepmatic/crates/runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ peepmatic-automata = { version = "0.68.0", path = "../automata", features = ["se
1616
peepmatic-traits = { version = "0.68.0", path = "../traits" }
1717
serde = { version = "1.0.105", features = ["derive"] }
1818
thiserror = "1.0.15"
19-
wast = { version = "27.0.0", optional = true }
19+
wast = { version = "28.0.0", optional = true }
2020

2121
[dev-dependencies]
2222
peepmatic-test-operator = { version = "0.68.0", path = "../test-operator" }

cranelift/peepmatic/crates/souper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ log = "0.4.8"
1616
[dev-dependencies]
1717
peepmatic = { path = "../..", version = "0.68.0" }
1818
peepmatic-test-operator = { version = "0.68.0", path = "../test-operator" }
19-
wast = "27.0.0"
19+
wast = "28.0.0"

cranelift/peepmatic/crates/test-operator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ edition = "2018"
99
[dependencies]
1010
peepmatic-traits = { version = "0.68.0", path = "../traits" }
1111
serde = { version = "1.0.105", features = ["derive"] }
12-
wast = "27.0.0"
12+
wast = "28.0.0"

cranelift/wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ keywords = ["webassembly", "wasm"]
1212
edition = "2018"
1313

1414
[dependencies]
15-
wasmparser = { version = "0.68.0", default-features = false }
15+
wasmparser = { version = "0.69.1", default-features = false }
1616
cranelift-codegen = { path = "../codegen", version = "0.68.0", default-features = false }
1717
cranelift-entity = { path = "../entity", version = "0.68.0" }
1818
cranelift-frontend = { path = "../frontend", version = "0.68.0", default-features = false }

cranelift/wasm/src/translation_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct InstanceTypeIndex(u32);
110110
entity_impl!(InstanceTypeIndex);
111111

112112
/// An index of an entity.
113-
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
113+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
114114
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
115115
pub enum EntityIndex {
116116
/// Function index.

crates/c-api/src/extern.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
1616
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
1717
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
1818
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
19+
20+
// FIXME(#2094)
21+
Extern::Instance(_) => unimplemented!(),
22+
Extern::Module(_) => unimplemented!(),
1923
}
2024
}
2125

crates/debug/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313

1414
[dependencies]
1515
gimli = "0.23.0"
16-
wasmparser = "0.68.0"
16+
wasmparser = "0.69.0"
1717
object = { version = "0.22.0", default-features = false, features = ["read", "write"] }
1818
wasmtime-environ = { path = "../environ", version = "0.21.0" }
1919
target-lexicon = { version = "0.11.0", default-features = false }

crates/environ/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ anyhow = "1.0"
1616
cranelift-codegen = { path = "../../cranelift/codegen", version = "0.68.0", features = ["enable-serde"] }
1717
cranelift-entity = { path = "../../cranelift/entity", version = "0.68.0", features = ["enable-serde"] }
1818
cranelift-wasm = { path = "../../cranelift/wasm", version = "0.68.0", features = ["enable-serde"] }
19-
wasmparser = "0.68.0"
19+
wasmparser = "0.69.0"
2020
indexmap = { version = "1.0.2", features = ["serde-1"] }
2121
thiserror = "1.0.4"
2222
serde = { version = "1.0.94", features = ["derive"] }

crates/environ/src/module.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,31 @@ impl Module {
346346
pub fn is_imported_global(&self, index: GlobalIndex) -> bool {
347347
index.index() < self.num_imported_globals
348348
}
349+
350+
/// Returns an iterator of all the imports in this module, along with their
351+
/// module name, field name, and type that's being imported.
352+
pub fn imports(&self) -> impl Iterator<Item = (&str, Option<&str>, EntityType)> {
353+
self.initializers.iter().filter_map(move |i| match i {
354+
Initializer::Import {
355+
module,
356+
field,
357+
index,
358+
} => Some((module.as_str(), field.as_deref(), self.type_of(*index))),
359+
_ => None,
360+
})
361+
}
362+
363+
/// Returns the type of an item based on its index
364+
pub fn type_of(&self, index: EntityIndex) -> EntityType {
365+
match index {
366+
EntityIndex::Global(i) => EntityType::Global(self.globals[i]),
367+
EntityIndex::Table(i) => EntityType::Table(self.table_plans[i].table),
368+
EntityIndex::Memory(i) => EntityType::Memory(self.memory_plans[i].memory),
369+
EntityIndex::Function(i) => EntityType::Function(self.functions[i]),
370+
EntityIndex::Instance(i) => EntityType::Instance(self.instances[i]),
371+
EntityIndex::Module(i) => EntityType::Module(self.modules[i]),
372+
}
373+
}
349374
}
350375

351376
/// All types which are recorded for the entirety of a translation.
@@ -376,7 +401,7 @@ pub struct ModuleSignature {
376401
#[derive(Debug, Clone, Serialize, Deserialize)]
377402
pub struct InstanceSignature {
378403
/// The name of what's being exported as well as its type signature.
379-
pub exports: Vec<(String, EntityType)>,
404+
pub exports: IndexMap<String, EntityType>,
380405
}
381406

382407
mod passive_data_serde {

crates/environ/src/module_environ.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ and for re-adding support for interface types you can see this issue:
857857
// instance.
858858
Alias::Child { instance, export } => {
859859
let ty = self.result.module.instances[instance];
860-
match &self.types.instance_signatures[ty].exports[export].1 {
860+
match &self.types.instance_signatures[ty].exports[export] {
861861
EntityType::Global(g) => {
862862
self.result.module.globals.push(g.clone());
863863
self.result.module.num_imported_globals += 1;

crates/fuzzing/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ arbitrary = { version = "0.4.1", features = ["derive"] }
1212
env_logger = "0.8.1"
1313
log = "0.4.8"
1414
rayon = "1.2.1"
15-
wasmparser = "0.68.0"
16-
wasmprinter = "0.2.15"
15+
wasmparser = "0.69.0"
16+
wasmprinter = "0.2.16"
1717
wasmtime = { path = "../wasmtime" }
1818
wasmtime-wast = { path = "../wast" }
1919
wasm-smith = "0.1.12"

crates/jit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ rayon = { version = "1.0", optional = true }
2828
region = "2.1.0"
2929
thiserror = "1.0.4"
3030
target-lexicon = { version = "0.11.0", default-features = false }
31-
wasmparser = "0.68.0"
31+
wasmparser = "0.69.0"
3232
more-asserts = "0.2.1"
3333
anyhow = "1.0"
3434
cfg-if = "1.0"

crates/lightbeam/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ more-asserts = "0.2.1"
2424
smallvec = "1.0.0"
2525
thiserror = "1.0.9"
2626
typemap = "0.3"
27-
wasmparser = "0.68.0"
27+
wasmparser = "0.69.0"
2828

2929
[dev-dependencies]
3030
lazy_static = "1.2"

crates/lightbeam/wasmtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ edition = "2018"
1313

1414
[dependencies]
1515
lightbeam = { path = "..", version = "0.21.0" }
16-
wasmparser = "0.68"
16+
wasmparser = "0.69"
1717
cranelift-codegen = { path = "../../../cranelift/codegen", version = "0.68.0" }
1818
wasmtime-environ = { path = "../../environ", version = "0.21.0" }

0 commit comments

Comments
 (0)