Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add support for the compact imports proposal (#14335)

chacailline08c1bc4a2137
diff --git a/crates/c-api/include/wasmtime/config.h b/crates/c-api/include/wasmtime/config.h
index 79f6f1d486..591901c46f 100644
--- a/crates/c-api/include/wasmtime/config.h
+++ b/crates/c-api/include/wasmtime/config.h
@@ -332,6 +332,14 @@ WASMTIME_CONFIG_PROP(void, wasm_exceptions, bool)
*/
WASMTIME_CONFIG_PROP(void, wasm_custom_page_sizes, bool)
+/**
+ * \brief Configures whether the WebAssembly compact imports proposal is
+ * enabled.
+ *
+ * This setting is `false` by default.
+ */
+WASMTIME_CONFIG_PROP(void, wasm_compact_imports, bool)
+
#ifdef WASMTIME_FEATURE_COMPILER
/**
diff --git a/crates/c-api/include/wasmtime/config.hh b/crates/c-api/include/wasmtime/config.hh
index 69aca0621b..03d5ee84aa 100644
--- a/crates/c-api/include/wasmtime/config.hh
+++ b/crates/c-api/include/wasmtime/config.hh
@@ -433,6 +433,14 @@ class Config {
wasmtime_config_wasm_custom_page_sizes_set(ptr.get(), enable);
}
+ /// \brief Configures whether the WebAssembly compact imports proposal will be
+ /// enabled
+ ///
+ /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_compact_imports
+ void wasm_compact_imports(bool enable) {
+ wasmtime_config_wasm_compact_imports_set(ptr.get(), enable);
+ }
+
#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
/// \brief Configures whether the WebAssembly component model proposal will be
/// enabled
diff --git a/crates/c-api/src/config.rs b/crates/c-api/src/config.rs
index 1196dc49b5..e494f7786d 100644
--- a/crates/c-api/src/config.rs
+++ b/crates/c-api/src/config.rs
@@ -516,6 +516,11 @@ pub extern "C" fn wasmtime_config_wasm_custom_page_sizes_set(c: &mut wasm_config
c.config.wasm_custom_page_sizes(enable);
}
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_config_wasm_compact_imports_set(c: &mut wasm_config_t, enable: bool) {
+ c.config.wasm_compact_imports(enable);
+}
+
#[repr(C)]
#[derive(Clone)]
#[cfg(feature = "pooling-allocator")]
diff --git a/crates/c-api/tests/config.cc b/crates/c-api/tests/config.cc
index c0343bf73d..371ae1ec2b 100644
--- a/crates/c-api/tests/config.cc
+++ b/crates/c-api/tests/config.cc
@@ -53,6 +53,7 @@ TEST(Config, Smoke) {
config.wasm_component_model(false);
config.wasm_exceptions(false);
config.wasm_custom_page_sizes(false);
+ config.wasm_compact_imports(false);
config.strategy(Strategy::Auto);
config.cranelift_debug_verifier(false);
config.cranelift_opt_level(OptLevel::Speed);
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs
index 69d8637fee..562f948e01 100644
--- a/crates/cli-flags/src/lib.rs
+++ b/crates/cli-flags/src/lib.rs
@@ -517,6 +517,8 @@ wasmtime_option_group! {
pub wide_arithmetic: Option<bool>,
/// Configure support for the branch-hinting proposal.
pub branch_hinting: Option<bool>,
+ /// Configure support for compact imports.
+ pub compact_imports: Option<bool>,
/// Configure support for the extended-const proposal.
pub extended_const: Option<bool>,
/// Configure support for the exceptions proposal.
@@ -1324,6 +1326,10 @@ impl CommonOptions {
if let Some(enable) = self.wasm.branch_hinting {
config.wasm_branch_hinting(enable);
}
+ // Not included in `all_proposals`: off by default until fuzzed.
+ if let Some(enable) = self.wasm.compact_imports {
+ config.wasm_compact_imports(enable);
+ }
if let Some(enable) = self.wasm.extended_const.or(all) {
config.wasm_extended_const(enable);
}
@@ -1512,6 +1518,7 @@ impl CommonOptions {
async_stack_size: Some(engine.get_async_stack_size()),
async_stack_zeroing: Some(engine.get_async_stack_zeroing()),
branch_hinting: Some(engine.get_wasm_branch_hinting()),
+ compact_imports: Some(features.contains(WasmFeatures::COMPACT_IMPORTS)),
bulk_memory: Some(features.contains(WasmFeatures::BULK_MEMORY)),
component_model: Some(features.contains(WasmFeatures::COMPONENT_MODEL)),
component_model_async: Some(features.contains(WasmFeatures::CM_ASYNC)),
diff --git a/crates/fuzzing/src/generators/config.rs b/crates/fuzzing/src/generators/config.rs
index ce44de185f..3ed85022f7 100644
--- a/crates/fuzzing/src/generators/config.rs
+++ b/crates/fuzzing/src/generators/config.rs
@@ -139,6 +139,7 @@ impl Config {
extended_const,
wide_arithmetic,
branch_hinting,
+ compact_imports,
component_model_async,
component_model_more_async_builtins,
component_model_async_stackful,
@@ -191,6 +192,7 @@ impl Config {
config.bulk_memory_enabled = bulk_memory.unwrap_or(false);
config.multi_value_enabled = true;
config.wide_arithmetic_enabled = wide_arithmetic.unwrap_or(false);
+ config.compact_imports_enabled = compact_imports.unwrap_or(false);
config.memory64_enabled = memory64.unwrap_or(false);
config.relaxed_simd_enabled = relaxed_simd.unwrap_or(false);
config.simd_enabled = config.relaxed_simd_enabled || simd.unwrap_or(false);
@@ -343,6 +345,7 @@ impl Config {
cfg.wasm.component_model_canonical_names =
Some(self.module_config.component_model_canonical_names);
cfg.wasm.custom_page_sizes = Some(self.module_config.config.custom_page_sizes_enabled);
+ cfg.wasm.compact_imports = Some(self.module_config.config.compact_imports_enabled);
cfg.wasm.epoch_interruption = Some(self.wasmtime.epoch_interruption);
cfg.wasm.extended_const = Some(self.module_config.config.extended_const_enabled);
cfg.wasm.fuel = self.wasmtime.consume_fuel.then(|| u64::MAX);
diff --git a/crates/fuzzing/src/generators/module.rs b/crates/fuzzing/src/generators/module.rs
index 749329f1ae..0f52f40976 100644
--- a/crates/fuzzing/src/generators/module.rs
+++ b/crates/fuzzing/src/generators/module.rs
@@ -56,6 +56,7 @@ impl<'a> Arbitrary<'a> for ModuleConfig {
let _ = config.tail_call_enabled;
let _ = config.extended_const_enabled;
let _ = config.gc_enabled;
+ let _ = config.compact_imports_enabled;
let _ = config.exceptions_enabled;
config.custom_page_sizes_enabled = u.arbitrary()?;
config.wide_arithmetic_enabled = u.arbitrary()?;
@@ -78,9 +79,6 @@ impl<'a> Arbitrary<'a> for ModuleConfig {
// do that most of the time.
config.disallow_traps = u.ratio(9, 10)?;
- // not supported in wasmtime yet
- config.compact_imports_enabled = false;
-
Ok(ModuleConfig {
component_model_async: false,
component_model_more_async_builtins: false,
diff --git a/crates/fuzzing/src/oracles.rs b/crates/fuzzing/src/oracles.rs
index e9355664a1..535ca309d5 100644
--- a/crates/fuzzing/src/oracles.rs
+++ b/crates/fuzzing/src/oracles.rs
@@ -1340,6 +1340,7 @@ mod tests {
| WasmFeatures::FUNCTION_REFERENCES
| WasmFeatures::GC
| WasmFeatures::GC_TYPES
+ | WasmFeatures::COMPACT_IMPORTS
| WasmFeatures::CUSTOM_PAGE_SIZES
| WasmFeatures::EXTENDED_CONST
| WasmFeatures::EXCEPTIONS;
diff --git a/crates/test-util/src/wasmtime_wast.rs b/crates/test-util/src/wasmtime_wast.rs
index 8729b35bb1..8a2cf70d1b 100644
--- a/crates/test-util/src/wasmtime_wast.rs
+++ b/crates/test-util/src/wasmtime_wast.rs
@@ -40,6 +40,7 @@ pub fn apply_test_config(config: &mut Config, test_config: &wast::TestConfig) {
extended_const,
wide_arithmetic,
branch_hinting,
+ compact_imports,
component_model_async,
component_model_more_async_builtins,
component_model_async_stackful,
@@ -76,6 +77,7 @@ pub fn apply_test_config(config: &mut Config, test_config: &wast::TestConfig) {
let extended_const = extended_const.unwrap_or(false);
let wide_arithmetic = wide_arithmetic.unwrap_or(false);
let branch_hinting = branch_hinting.unwrap_or(false);
+ let compact_imports = compact_imports.unwrap_or(false);
let component_model_async = component_model_async.unwrap_or(false);
let component_model_more_async_builtins = component_model_more_async_builtins.unwrap_or(false);
let component_model_async_stackful = component_model_async_stackful.unwrap_or(false);
@@ -123,6 +125,7 @@ pub fn apply_test_config(config: &mut Config, test_config: &wast::TestConfig) {
.wasm_extended_const(extended_const)
.wasm_wide_arithmetic(wide_arithmetic)
.wasm_branch_hinting(branch_hinting)
+ .wasm_compact_imports(compact_imports)
.wasm_component_model_async(component_model_async)
.wasm_component_model_more_async_builtins(component_model_more_async_builtins)
.wasm_component_model_async_stackful(component_model_async_stackful)
diff --git a/crates/test-util/src/wast.rs b/crates/test-util/src/wast.rs
index e6f0f8060a..56898dc78a 100644
--- a/crates/test-util/src/wast.rs
+++ b/crates/test-util/src/wast.rs
@@ -148,6 +148,16 @@ fn spec_test_config(test: &Path) -> TestConfig {
Some("custom-descriptors") => {
ret.custom_descriptors = Some(true);
}
+ Some("compact-import-section") => {
+ ret.compact_imports = Some(true);
+ ret.reference_types = Some(true);
+ ret.multi_memory = Some(true);
+ ret.exceptions = Some(true);
+ }
+ Some("extended-name-section") => {
+ ret.gc = Some(true);
+ ret.exceptions = Some(true);
+ }
Some(proposal) => panic!("unsupported proposal {proposal:?}"),
// The rough goal here is to enable a minimal set of features for the
@@ -205,6 +215,9 @@ fn spec_test_config(test: &Path) -> TestConfig {
if test_name.contains("return_") || test_name.contains("try_table") {
ret.tail_call = Some(true);
}
+ if test_name == "return_call.wast" || test_name == "return_call_indirect.wast" {
+ ret.function_references = Some(true);
+ }
if test_name.contains("tag")
|| test_name.contains("try_table")
|| test_name.contains("throw")
@@ -336,6 +349,7 @@ macro_rules! foreach_config_option {
extended_const
wide_arithmetic
branch_hinting
+ compact_imports
hogs_memory
nan_canonicalization
component_model_async
@@ -563,6 +577,18 @@ impl WastTest {
return true;
}
+ // Waiting for bytecodealliance/wasm-tools#2664 zero-length compact imports,
+ // as well as the Extended Name Section Proposal.
+ let unsupported = [
+ "spec_testsuite/proposals/compact-import-section/imports-compact.wast",
+ "spec_testsuite/proposals/extended-name-section/custom/name_annot.wast",
+ "spec_testsuite/proposals/extended-name-section/custom/name.wast",
+ "spec_testsuite/type-subtyping.wast",
+ ];
+ if unsupported.iter().any(|part| self.path.ends_with(part)) {
+ return true;
+ }
+
// Disable spec tests per target for proposals that Winch does not implement yet.
if config.compiler == Compiler::Winch {
// Common list for tests that fail in all targets supported by Winch.
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs
index 310faf5067..65239deff7 100644
--- a/crates/wasmtime/src/config.rs
+++ b/crates/wasmtime/src/config.rs
@@ -985,6 +985,27 @@ impl Config {
self
}
+ /// Configures whether the WebAssembly compact imports proposal is enabled.
+ ///
+ /// The [WebAssembly compact import section proposal]
+ /// adds two compact encodings for imports:
+ /// - A module name and a list of `(item name, type)` pairs
+ /// - A module name, a type, and a list of item names
+ ///
+ /// This reduces redundant module and type listings, and can
+ /// reduce WebAssembly file size, especially in files with many
+ /// repeated imports.
+ ///
+ /// When enabled, compact imports are accepted in binary and text-format inputs.
+ ///
+ /// This feature is `false` by default.
+ ///
+ /// [WebAssembly compact import section proposal]: https://github.com/WebAssembly/compact-import-section
+ pub fn wasm_compact_imports(&mut self, enable: bool) -> &mut Self {
+ self.wasm_features(WasmFeatures::COMPACT_IMPORTS, enable);
+ self
+ }
+
/// Configures whether the WebAssembly [threads] proposal will be enabled
/// for compilation.
///
@@ -2432,6 +2453,7 @@ impl Config {
| WasmFeatures::SHARED_EVERYTHING_THREADS
| WasmFeatures::COMPONENT_MODEL
| WasmFeatures::CUSTOM_PAGE_SIZES
+ | WasmFeatures::COMPACT_IMPORTS
| WasmFeatures::STACK_SWITCHING
| WasmFeatures::WIDE_ARITHMETIC
| WasmFeatures::CM_ASYNC
diff --git a/docs/contributing-implementing-wasm-proposals.md b/docs/contributing-implementing-wasm-proposals.md
index 4b1738e74a..f569de1731 100644
--- a/docs/contributing-implementing-wasm-proposals.md
+++ b/docs/contributing-implementing-wasm-proposals.md
@@ -17,9 +17,16 @@ multiple pull requests.
proposal unimplemented for now (implementation comes in subsequent PRs).
[(example)](https://github.com/bytecodealliance/wasmtime/pull/9399)
* [ ] Add `Config::wasm_your_proposal` to the `wasmtime` crate.
+ * [ ] If the proposal adds a `WasmFeatures` flag, include it in
+ `features_known_to_wasmtime` in
+ [`crates/wasmtime/src/config.rs`](https://github.com/bytecodealliance/wasmtime/blob/0675528cca48cc8dd90cefe91eb7f1a279a7415b/crates/wasmtime/src/config.rs).
* [ ] Implement the proposal in `wasmtime`, gated behind this flag.
-* [ ] Add `-Wyour-proposal` to the `wasmtime-cli-flags` crate.
-* [ ] Update `tests/wast.rs` to spec tests should pass for this proposal.
+* [ ] Add `your-proposal` to `WasmOptions` in the `wasmtime-cli-flags`
+ crate, apply it to `wasmtime::Config`, and decide whether it should be
+ included in `-W all-proposals`.
+* [ ] Configure the WAST test harness for the proposal in
+ `crates/test-util/src/wast.rs` and
+ `crates/test-util/src/wasmtime_wast.rs`.
* [ ] Write custom tests in `tests/misc_testsuite/*.wast` for this proposal.
* [ ] Enable the proposal in [the fuzz targets](./contributing-fuzzing.md).
* [ ] Write a custom fuzz target, oracle, and/or test
@@ -39,12 +46,16 @@ multiple pull requests.
> method.
* [ ] Expose the proposal's new functionality in the C API.
- > This may require extensions to the standard C API, and if so, should be
- > defined in
- > [`wasmtime.h`](https://github.com/bytecodealliance/wasmtime/blob/c7cd70fcec3eee66c9d7b5aa6fb4580d5a802218/crates/c-api/include/wasmtime.h)
- > and prefixed with `wasmtime_`.
-* [ ] Update `docs/stability-tiers.md` with an implementation status of the
- proposal.
+ > This may require extensions to the standard C API. Configuration extensions
+ > should be declared in
+ > [`crates/c-api/include/wasmtime/config.h`](https://github.com/bytecodealliance/wasmtime/blob/0675528cca48cc8dd90cefe91eb7f1a279a7415b/crates/c-api/include/wasmtime/config.h)
+ > and prefixed with `wasmtime_`. Implement them in
+ > [`crates/c-api/src/config.rs`](https://github.com/bytecodealliance/wasmtime/blob/0675528cca48cc8dd90cefe91eb7f1a279a7415b/crates/c-api/src/config.rs),
+ > and add corresponding C++ wrappers to
+ > [`crates/c-api/include/wasmtime/config.hh`](https://github.com/bytecodealliance/wasmtime/blob/0675528cca48cc8dd90cefe91eb7f1a279a7415b/crates/c-api/include/wasmtime/config.hh)
+ > when needed.
+* [ ] Update `docs/stability-wasm-proposals.md` with the proposal's
+ implementation status.
[`wasm-tools` repository]: https://github.com/bytecodealliance/wasm-tools
diff --git a/docs/stability-wasm-proposals.md b/docs/stability-wasm-proposals.md
index 375cb9091c..08ec63b040 100644
--- a/docs/stability-wasm-proposals.md
+++ b/docs/stability-wasm-proposals.md
@@ -53,28 +53,31 @@ The emoji legend is:
| [`custom-page-sizes`] | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [`threads`] | ✅ | ✅ | 🚧[^8] | ❌[^4] | ✅ | ✅ |
-[^4]: Fuzzing with threads is an open implementation question that is expected
- to get fleshed out as the [`shared-everything-threads`] proposal advances.
[^8]: There are [known
issues](https://github.com/bytecodealliance/wasmtime/issues/4245) with
shared memories and the implementation/API in Wasmtime, for example they
aren't well integrated with resource-limiting features in `Store`.
Additionally `shared` memories aren't supported in the pooling allocator.
+[^4]: Fuzzing with threads is an open implementation question that is expected
+ to get fleshed out as the [`shared-everything-threads`] proposal advances.
[cm-capi-gaps]: https://github.com/bytecodealliance/wasmtime/issues?q=is%3Aissue%20state%3Aopen%20label%3Awasm-proposal%3Acomponent-model%20label%3Awasmtime%3Ac-api
## Tier 3 WebAssembly Proposals
-| Proposal | Phase 4 | Tests | Finished | Fuzzed | API | C API |
-|-----------------------------|---------|-------|----------|--------|-----|-------|
-| [`branch-hinting`] [^12] | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ |
-| [`stack-switching`] [^11] | ❌ | 🚧 | 🚧 | ❌ | ❌ | ❌ |
+| Proposal | Phase 4 | Tests | Finished | Fuzzed | API | C API |
+|----------------------------------|---------|-------|----------|--------|-----|-------|
+| [`branch-hinting`] [^12] | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ |
+| [`stack-switching`] [^11] | ❌ | 🚧 | 🚧 | ❌ | ❌ | ❌ |
+| [`compact-import-section`] [^13] | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ |
+[^12]: Disabled by default (`Config::wasm_branch_hinting`) pending fuzzing;
+ tracked at [#9463](https://github.com/bytecodealliance/wasmtime/issues/9463).
[^11]: The stack-switching proposal is a work-in-progress being tracked
at [#9465](https://github.com/bytecodealliance/wasmtime/issues/9465).
Currently the implementation is only for x86\_64 Linux.
-[^12]: Disabled by default (`Config::wasm_branch_hinting`) pending fuzzing;
- tracked at [#9463](https://github.com/bytecodealliance/wasmtime/issues/9463).
+[^13]: Disabled by default (`Config::wasm_compact_imports`) pending fuzzing;
+ tracked at [#14108](https://github.com/bytecodealliance/wasmtime/issues/14108).
## Unimplemented proposals
@@ -83,7 +86,6 @@ The emoji legend is:
| [`flexible-vectors`] | [#9464](https://github.com/bytecodealliance/wasmtime/issues/9464) |
| [`memory-control`] | [#9467](https://github.com/bytecodealliance/wasmtime/issues/9467) |
| [`shared-everything-threads`] | [#9466](https://github.com/bytecodealliance/wasmtime/issues/9466) |
-| [`compact-import-section`] | [#14108](https://github.com/bytecodealliance/wasmtime/issues/14108) |
[`mutable-globals`]: https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md
[`sign-extension-ops`]: https://github.com/WebAssembly/spec/blob/master/proposals/sign-extension-ops/Overview.md
diff --git a/tests/misc_testsuite/compact-imports.wast b/tests/misc_testsuite/compact-imports.wast
new file mode 100644
index 0000000000..a387b780d6
--- /dev/null
+++ b/tests/misc_testsuite/compact-imports.wast
@@ -0,0 +1,32 @@
+;;! compact_imports = true
+
+(module
+ (func (export "one") (result i32) i32.const 1)
+ (func (export "two") (result i32) i32.const 2)
+ (func (export "four") (result i32) i32.const 4)
+ (func (export "five") (result i32) i32.const 5)
+)
+(register "provider")
+
+(module
+ (import "provider" "one" (func $one (result i32)))
+ (import "provider"
+ (item "two" (func $two (result i32)))
+ )
+ (import "provider"
+ (item "four")
+ (item "five")
+ (func (result i32))
+ )
+
+ (func (export "sum") (result i32)
+ call $one
+ call $two
+ i32.add
+ call 2
+ i32.add
+ call 3
+ i32.add)
+)
+
+(assert_return (invoke "sum") (i32.const 12))
diff --git a/tests/spec_testsuite b/tests/spec_testsuite
index 0dc0343c98..b464a4cd10 160000
--- a/tests/spec_testsuite
+++ b/tests/spec_testsuite
@@ -1 +1 @@
-Subproject commit 0dc0343c9876267d99a7577ed4fc2289406a7869
+Subproject commit b464a4cd100d98175ae6e3890db89a2e6c8302f7