Skip to content

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

Make Config::wasm_feature public. (#11812)

Graydon Hoarec4688098204c
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs
index 4b313dfa50..9abda5a5f4 100644
--- a/crates/wasmtime/src/config.rs
+++ b/crates/wasmtime/src/config.rs
@@ -5,7 +5,7 @@ use core::fmt;
use core::str::FromStr;
#[cfg(any(feature = "cache", feature = "cranelift", feature = "winch"))]
use std::path::Path;
-use wasmparser::WasmFeatures;
+pub use wasmparser::WasmFeatures;
use wasmtime_environ::{ConfigTunables, TripleExt, Tunables};
#[cfg(feature = "runtime")]
@@ -779,7 +779,18 @@ impl Config {
self
}
- fn wasm_feature(&mut self, flag: WasmFeatures, enable: bool) -> &mut Self {
+ /// Explicitly enables (and un-disables) a given set of [`WasmFeatures`].
+ ///
+ /// Note: this is a low-level method that does not necessarily imply that
+ /// wasmtime _supports_ a feature. It should only be used to _disable_
+ /// features that callers want to be rejected by the parser or _enable_
+ /// features callers are certain that the current configuration of wasmtime
+ /// supports.
+ ///
+ /// Feature validation is deferred until an engine is being built, thus by
+ /// enabling features here a caller may cause [`Engine::new`] to fail later,
+ /// if the feature configuration isn't supported.
+ pub fn wasm_features(&mut self, flag: WasmFeatures, enable: bool) -> &mut Self {
self.enabled_features.set(flag, enable);
self.disabled_features.set(flag, !enable);
self
@@ -797,7 +808,7 @@ impl Config {
///
/// [WebAssembly tail calls proposal]: https://github.com/WebAssembly/tail-call
pub fn wasm_tail_call(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::TAIL_CALL, enable);
+ self.wasm_features(WasmFeatures::TAIL_CALL, enable);
self
}
@@ -822,7 +833,7 @@ impl Config {
///
/// [WebAssembly custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
pub fn wasm_custom_page_sizes(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CUSTOM_PAGE_SIZES, enable);
+ self.wasm_features(WasmFeatures::CUSTOM_PAGE_SIZES, enable);
self
}
@@ -847,7 +858,7 @@ impl Config {
/// [wasi-threads]: https://github.com/webassembly/wasi-threads
#[cfg(feature = "threads")]
pub fn wasm_threads(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::THREADS, enable);
+ self.wasm_features(WasmFeatures::THREADS, enable);
self
}
@@ -862,7 +873,7 @@ impl Config {
/// [shared-everything-threads]:
/// https://github.com/webassembly/shared-everything-threads
pub fn wasm_shared_everything_threads(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::SHARED_EVERYTHING_THREADS, enable);
+ self.wasm_features(WasmFeatures::SHARED_EVERYTHING_THREADS, enable);
self
}
@@ -884,7 +895,7 @@ impl Config {
/// [proposal]: https://github.com/webassembly/reference-types
#[cfg(feature = "gc")]
pub fn wasm_reference_types(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::REFERENCE_TYPES, enable);
+ self.wasm_features(WasmFeatures::REFERENCE_TYPES, enable);
self
}
@@ -903,7 +914,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/function-references
#[cfg(feature = "gc")]
pub fn wasm_function_references(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::FUNCTION_REFERENCES, enable);
+ self.wasm_features(WasmFeatures::FUNCTION_REFERENCES, enable);
self
}
@@ -914,7 +925,7 @@ impl Config {
///
/// [proposal]: https://github.com/WebAssembly/wide-arithmetic
pub fn wasm_wide_arithmetic(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::WIDE_ARITHMETIC, enable);
+ self.wasm_features(WasmFeatures::WIDE_ARITHMETIC, enable);
self
}
@@ -935,7 +946,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/gc
#[cfg(feature = "gc")]
pub fn wasm_gc(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::GC, enable);
+ self.wasm_features(WasmFeatures::GC, enable);
self
}
@@ -956,7 +967,7 @@ impl Config {
/// [proposal]: https://github.com/webassembly/simd
/// [relaxed simd proposal]: https://github.com/WebAssembly/relaxed-simd
pub fn wasm_simd(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::SIMD, enable);
+ self.wasm_features(WasmFeatures::SIMD, enable);
self
}
@@ -983,7 +994,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/relaxed-simd
pub fn wasm_relaxed_simd(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::RELAXED_SIMD, enable);
+ self.wasm_features(WasmFeatures::RELAXED_SIMD, enable);
self
}
@@ -1027,7 +1038,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/bulk-memory-operations
pub fn wasm_bulk_memory(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::BULK_MEMORY, enable);
+ self.wasm_features(WasmFeatures::BULK_MEMORY, enable);
self
}
@@ -1041,7 +1052,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/multi-value
pub fn wasm_multi_value(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::MULTI_VALUE, enable);
+ self.wasm_features(WasmFeatures::MULTI_VALUE, enable);
self
}
@@ -1055,7 +1066,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/multi-memory
pub fn wasm_multi_memory(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::MULTI_MEMORY, enable);
+ self.wasm_features(WasmFeatures::MULTI_MEMORY, enable);
self
}
@@ -1070,7 +1081,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/memory64
pub fn wasm_memory64(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::MEMORY64, enable);
+ self.wasm_features(WasmFeatures::MEMORY64, enable);
self
}
@@ -1081,7 +1092,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/extended-const
pub fn wasm_extended_const(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::EXTENDED_CONST, enable);
+ self.wasm_features(WasmFeatures::EXTENDED_CONST, enable);
self
}
@@ -1099,7 +1110,7 @@ impl Config {
///
/// [proposal]: https://github.com/webassembly/stack-switching
pub fn wasm_stack_switching(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::STACK_SWITCHING, enable);
+ self.wasm_features(WasmFeatures::STACK_SWITCHING, enable);
self
}
@@ -1118,7 +1129,7 @@ impl Config {
/// [proposal]: https://github.com/webassembly/component-model
#[cfg(feature = "component-model")]
pub fn wasm_component_model(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::COMPONENT_MODEL, enable);
+ self.wasm_features(WasmFeatures::COMPONENT_MODEL, enable);
self
}
@@ -1133,7 +1144,7 @@ impl Config {
/// https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md
#[cfg(feature = "component-model-async")]
pub fn wasm_component_model_async(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CM_ASYNC, enable);
+ self.wasm_features(WasmFeatures::CM_ASYNC, enable);
self
}
@@ -1146,7 +1157,7 @@ impl Config {
/// https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md
#[cfg(feature = "component-model-async")]
pub fn wasm_component_model_async_builtins(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CM_ASYNC_BUILTINS, enable);
+ self.wasm_features(WasmFeatures::CM_ASYNC_BUILTINS, enable);
self
}
@@ -1158,7 +1169,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md
#[cfg(feature = "component-model-async")]
pub fn wasm_component_model_async_stackful(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CM_ASYNC_STACKFUL, enable);
+ self.wasm_features(WasmFeatures::CM_ASYNC_STACKFUL, enable);
self
}
@@ -1170,7 +1181,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md
#[cfg(feature = "component-model")]
pub fn wasm_component_model_error_context(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CM_ERROR_CONTEXT, enable);
+ self.wasm_features(WasmFeatures::CM_ERROR_CONTEXT, enable);
self
}
@@ -1185,7 +1196,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/component-model/issues/525
#[cfg(feature = "component-model")]
pub fn wasm_component_model_gc(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::CM_GC, enable);
+ self.wasm_features(WasmFeatures::CM_GC, enable);
self
}
@@ -1194,7 +1205,7 @@ impl Config {
/// [proposal]: https://github.com/WebAssembly/exception-handling
#[cfg(feature = "gc")]
pub fn wasm_exceptions(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::EXCEPTIONS, enable);
+ self.wasm_features(WasmFeatures::EXCEPTIONS, enable);
self
}
@@ -1203,7 +1214,7 @@ impl Config {
usage with the spec testsuite. It may be removed at \
any time and without warning. Do not rely on it!"]
pub fn wasm_legacy_exceptions(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::LEGACY_EXCEPTIONS, enable);
+ self.wasm_features(WasmFeatures::LEGACY_EXCEPTIONS, enable);
self
}
@@ -2082,11 +2093,49 @@ impl Config {
/// backend partially supports simd so it's not listed here. Winch doesn't
/// fully support simd but unimplemented instructions just return errors.
fn compiler_panicking_wasm_features(&self) -> WasmFeatures {
+ // First we compute the set of features that Wasmtime itself knows;
+ // this is a sort of "maximal set" that we invert to create a set
+ // of features we _definitely can't support_ because wasmtime
+ // has never heard of them.
+ let features_known_to_wasmtime = WasmFeatures::empty()
+ | WasmFeatures::MUTABLE_GLOBAL
+ | WasmFeatures::SATURATING_FLOAT_TO_INT
+ | WasmFeatures::SIGN_EXTENSION
+ | WasmFeatures::REFERENCE_TYPES
+ | WasmFeatures::CALL_INDIRECT_OVERLONG
+ | WasmFeatures::MULTI_VALUE
+ | WasmFeatures::BULK_MEMORY
+ | WasmFeatures::BULK_MEMORY_OPT
+ | WasmFeatures::SIMD
+ | WasmFeatures::RELAXED_SIMD
+ | WasmFeatures::THREADS
+ | WasmFeatures::SHARED_EVERYTHING_THREADS
+ | WasmFeatures::TAIL_CALL
+ | WasmFeatures::FLOATS
+ | WasmFeatures::MULTI_MEMORY
+ | WasmFeatures::EXCEPTIONS
+ | WasmFeatures::MEMORY64
+ | WasmFeatures::EXTENDED_CONST
+ | WasmFeatures::COMPONENT_MODEL
+ | WasmFeatures::FUNCTION_REFERENCES
+ | WasmFeatures::GC
+ | WasmFeatures::CUSTOM_PAGE_SIZES
+ | WasmFeatures::GC_TYPES
+ | WasmFeatures::STACK_SWITCHING
+ | WasmFeatures::WIDE_ARITHMETIC
+ | WasmFeatures::CM_ASYNC
+ | WasmFeatures::CM_ASYNC_STACKFUL
+ | WasmFeatures::CM_ASYNC_BUILTINS
+ | WasmFeatures::CM_THREADING
+ | WasmFeatures::CM_ERROR_CONTEXT
+ | WasmFeatures::CM_GC;
+
+ #[allow(unused_mut, reason = "easier to avoid #[cfg]")]
+ let mut unsupported = !features_known_to_wasmtime;
+
#[cfg(any(feature = "cranelift", feature = "winch"))]
match self.compiler_config.strategy {
None | Some(Strategy::Cranelift) => {
- let mut unsupported = WasmFeatures::empty();
-
// Pulley at this time fundamentally doesn't support the
// `threads` proposal, notably shared memory, because Rust can't
// safely implement loads/stores in the face of shared memory.
@@ -2115,10 +2164,9 @@ impl Config {
unsupported |= WasmFeatures::STACK_SWITCHING;
}
}
- unsupported
}
Some(Strategy::Winch) => {
- let mut unsupported = WasmFeatures::GC
+ unsupported |= WasmFeatures::GC
| WasmFeatures::FUNCTION_REFERENCES
| WasmFeatures::RELAXED_SIMD
| WasmFeatures::TAIL_CALL
@@ -2138,12 +2186,10 @@ impl Config {
// them.
_ => {}
}
- unsupported
}
Some(Strategy::Auto) => unreachable!(),
}
- #[cfg(not(any(feature = "cranelift", feature = "winch")))]
- return WasmFeatures::empty();
+ unsupported
}
/// Calculates the set of features that are enabled for this `Config`.
@@ -2700,7 +2746,7 @@ impl Config {
/// This option defaults to whether the crate `gc` feature is enabled or
/// not.
pub fn gc_support(&mut self, enable: bool) -> &mut Self {
- self.wasm_feature(WasmFeatures::GC_TYPES, enable)
+ self.wasm_features(WasmFeatures::GC_TYPES, enable)
}
/// Explicitly indicate or not whether the host is using a hardware float