A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add flags needed to gate MMU interruption. (#14929)
Erik Rose753af035600b
diff --git a/crates/cranelift/src/builder.rs b/crates/cranelift/src/builder.rsindex ca3358caea..9defbecbef 100644--- a/crates/cranelift/src/builder.rs+++ b/crates/cranelift/src/builder.rs@@ -12,7 +12,7 @@ use std::fmt;use std::path;use std::sync::Arc;use target_lexicon::Triple;-use wasmtime_environ::error::Result;+use wasmtime_environ::error::{Result, bail};use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables};struct Builder {@@ -108,6 +108,21 @@ impl CompilerBuilder for Builder {}fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {+ let triple = self.inner.triple();+ // Keep this synced with `has_mmu_interruption_runtime` in+ // `crates/wasmtime/build.rs`, which tests against similar conditions+ // but for the host platform of wasmtime itself.+ if tunables.mmu_interruption+ && (!matches!(+ triple.architecture,+ target_lexicon::Architecture::X86_64+ | target_lexicon::Architecture::X86_64h+ | target_lexicon::Architecture::Aarch64(_)+ ) || triple.operating_system != target_lexicon::OperatingSystem::Linux)+ {+ bail!("MMU interruption is supported only on x86_64 and aarch64 Linux, not `{triple}`");+ }+self.tunables = Some(tunables);Ok(())}diff --git a/crates/environ/src/tunables.rs b/crates/environ/src/tunables.rsindex ee5d4893fd..20ee50e19f 100644--- a/crates/environ/src/tunables.rs+++ b/crates/environ/src/tunables.rs@@ -99,6 +99,9 @@ define_tunables! {/// Whether or not we use epoch-based interruption.pub epoch_interruption: bool,+ /// Whether to generate the polling code to support MMU interruption.+ pub mmu_interruption: bool,+/// Whether or not linear memories are allowed to be reallocated after/// initial allocation at runtime.pub memory_may_move: bool,@@ -275,6 +278,7 @@ impl Tunables {consume_fuel: false,operator_cost: OperatorCostStrategy::Default,epoch_interruption: false,+ mmu_interruption: false,memory_may_move: true,guard_before_linear_memory: true,table_lazy_init: true,diff --git a/crates/wasmtime/build.rs b/crates/wasmtime/build.rsindex d3a118ed93..105e3e04ab 100644--- a/crates/wasmtime/build.rs+++ b/crates/wasmtime/build.rs@@ -53,11 +53,26 @@ fn main() {&& cfg!(feature = "runtime");let has_custom_fiber = !has_builtin_stackswitch && cfg!(feature = "custom-fiber");+ // Whether we can run code using MMU interruption on this host. Whether we+ // should compile such code is a separate matter, controlled by the+ // `mmu_interruption` engine tunable. This doesn't require cranelift to be+ // on, because we could be running precompiled cwasms.+ //+ // Keep this synced with `set_tunables()` in+ // `crates/cranelift/src/builder.rs`, which tests similar conditions against+ // the Wasm-targeting platform.+ let has_mmu_interruption_runtime = has_native_signals+ && cfg_is("target_os", "linux")+ && (target_arch == "x86_64" || target_arch == "aarch64")+ && cfg!(feature = "async")+ && cfg!(feature = "std");+custom_cfg("has_native_signals", has_native_signals);custom_cfg("has_virtual_memory", has_virtual_memory);custom_cfg("has_custom_fiber", has_custom_fiber);custom_cfg("has_custom_sync", has_custom_sync);custom_cfg("has_host_compiler_backend", has_host_compiler_backend);+ custom_cfg("has_mmu_interruption_runtime", has_mmu_interruption_runtime);custom_cfg("gc_zeal", cfg("fuzzing"));// If this OS isn't supported and no debug-builtins or if Cranelift doesn't supportdiff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rsindex a9a7f90412..aec4cbade8 100644--- a/crates/wasmtime/src/config.rs+++ b/crates/wasmtime/src/config.rs@@ -683,7 +683,7 @@ impl Config {/// compiled form of the Wasm code so that it maintains a precise/// instruction count, frequently checking this count against the/// remaining fuel. If one does not need this precise count or- /// deterministic interruptions, and only needs a periodic+ /// deterministic interruptions and needs only a periodic/// interrupt of some form, then It would be better to have a more/// lightweight mechanism.///@@ -801,6 +801,91 @@ impl Config {self}+ /// XXX: MMU interruption is not yet fully landed; this config flag, not yet+ /// plumbed through to the CLI, serves as a feature flag that keeps its+ /// landed portions from kicking in.+ ///+ /// Enables MMU-based interruption, a lower-overhead alternative to+ /// [`Config::epoch_interruption`]. It uses memory-protection faults instead+ /// of epoch deadline comparisons to prod a running Wasm guest to yield.+ ///+ /// # How it works+ ///+ /// Cranelift emits, at each loop backedge and function prologue, a load+ /// from a per-store "interrupt page". To trigger an interruption, the+ /// embedder marks that page as inaccessible; the resulting SIGSEGV is+ /// caught by Wasmtime's signal handler, which distinguishes an+ /// interrupt-check load from an actual crash by consulting a record of each+ /// such load in the trap table. The signal handler then causes the active+ /// fiber to yield.+ ///+ /// # Compared to `epoch_interruption`+ ///+ /// - Faster. Whereas epoch interruption needs to compare, branch, and keep+ /// a deadline register coherent, MMU interruption does only a single+ /// (dead) load from a per-store memory page. In the no-interruption case,+ /// the load completes successfully, and overhead (versus doing nothing)+ /// is 2.8% on the SpiderMonkey Sightglass benchmark, compared to 14.4%+ /// for epoch interruption. Factoring in interruptions, the SpiderMonkey+ /// Markdown-parsing workload, when run under `wasmtime serve`, shows a+ /// 12% boost in throughput over epoch interruption and a 19% reduction in+ /// p50 latency.+ /// - Also non-deterministic. The same guidance in+ /// [`Config::epoch_interruption`] about fuel vs. epochs applies here.+ /// - Equally fine-grained. Checks for MMU interruption happen at the same+ /// points in the compiled Wasm as checks for epoch interruption.+ /// - Less flexible. Unlike epochs, there is, as of yet, no configurable+ /// per-store interrupt behavior. When triggered, interruption always+ /// causes the running fiber to yield; the `Store::epoch_deadline_*`+ /// callbacks are not consulted.+ ///+ /// # Triggering an interruption+ ///+ /// Interruptions are triggered from outside the Wasm guest code by an+ /// [`MmuInterrupter`](crate::MmuInterrupter), which hands each running+ /// [`Store`] an interrupt page and protects it when that store is due to+ /// yield. The [`Engine`] owns a single interrupter, shared by all its+ /// stores. You must provide this interrupter using+ /// [`Config::with_mmu_interrupter`] before running code.+ /// [`TimingWheelInterrupter`](crate::TimingWheelInterrupter) is a good+ /// default implementation; like the epoch counter, it does nothing until+ /// you tick it.+ ///+ /// # Requirements+ ///+ /// - **Async.** Since MMU interruption always causes the fiber to yield,+ /// wasmtime must be built with the `async` feature. Configuring an engine+ /// with `mmu_interruption()` puts every [`Store`] derived from it into+ /// async mode automatically.+ /// - **Cranelift.** Winch does not support it.+ /// - **Linux on x86_64 or aarch64 target.** Cross-compilation from other+ /// hosts is fine.+ /// - **Native signals**, because we depend on a signal handler.+ /// - **Signals-based traps**, because signal-handler installation (in+ /// [`Engine::new()`]) is currently gated on this. It should be possible+ /// to remove this dependency, but the use cases are few, and threading+ /// the value of `signals_based_traps` into the signal handler may be+ /// nontrivial.+ ///+ /// # Errors+ ///+ /// Some requirements are validated after config time: some when an+ /// [`Engine`] is instantiated (which calls [`Config::validate`] herein) and+ /// others when code is loaded into the engine. Nothing panics; errors are+ /// reported via return values.+ #[doc(hidden)]+ pub fn mmu_interruption(&mut self, enable: bool) -> &mut Self {+ self.tunables.mmu_interruption = Some(enable);+ self+ }++ /// Whether this build of Wasmtime can run code on this host with+ /// [`Self::mmu_interruption`] enabled.+ #[doc(hidden)]+ pub fn is_mmu_interruption_runtime_available() -> bool {+ cfg!(has_mmu_interruption_runtime)+ }+/// XXX: For internal fuzzing and debugging use only!#[doc(hidden)]pub fn gc_zeal_alloc_counter(&mut self, counter: Option<NonZeroU32>) -> Result<&mut Self> {@@ -2901,6 +2986,14 @@ impl Config {);}+ if tunables.mmu_interruption {+ // Only with signals-based traps can MMU interruption's signal+ // handler be installed (at engine creation time).+ ensure!(+ tunables.signals_based_traps,+ "MMU interruption requires signals-based traps"+ );+ }// Concurrency support is required for some component model features.let requires_concurrency = WasmFeatures::CM_ASYNC| WasmFeatures::CM_MORE_ASYNC_BUILTINS@@ -5183,6 +5276,12 @@ impl Engine {self.tunables().epoch_interruption}+ /// Returns the configured [`Config::mmu_interruption`] value.+ #[doc(hidden)]+ pub fn get_mmu_interruption(&self) -> bool {+ self.tunables().mmu_interruption+ }+/// Returns the configured [`Config::consume_fuel`] value.pub fn get_consume_fuel(&self) -> bool {self.tunables().consume_fueldiff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rsindex 150ffcb0f0..18ea398649 100644--- a/crates/wasmtime/src/engine.rs+++ b/crates/wasmtime/src/engine.rs@@ -408,6 +408,13 @@ impl Engine {if !cfg!(has_native_signals) && self.tunables().signals_based_traps {return Err("signals-based-traps disabled at compile time -- cannot be enabled".into());}++ if self.tunables().mmu_interruption {+ if !cfg!(has_mmu_interruption_runtime) {+ return Err("MMU interruption runtime support was disabled at compile time".into());+ }+ }+if !cfg!(has_virtual_memory) && self.tunables().memory_init_cow {return Err("virtual memory disabled at compile time -- cannot enable CoW".into());}diff --git a/crates/wasmtime/src/engine/serialization.rs b/crates/wasmtime/src/engine/serialization.rsindex 0c8b8b3b00..b56b76cf28 100644--- a/crates/wasmtime/src/engine/serialization.rs+++ b/crates/wasmtime/src/engine/serialization.rs@@ -303,6 +303,7 @@ impl Metadata<'_> {consume_fuel,ref operator_cost,epoch_interruption,+ mmu_interruption,memory_may_move,guard_before_linear_memory,table_lazy_init,@@ -382,6 +383,7 @@ impl Metadata<'_> {other.epoch_interruption,"epoch interruption",)?;+ Self::check_bool(mmu_interruption, other.mmu_interruption, "MMU interruption")?;Self::check_bool(memory_may_move, other.memory_may_move, "memory may move")?;Self::check_bool(guard_before_linear_memory,diff --git a/crates/wasmtime/src/runtime.rs b/crates/wasmtime/src/runtime.rsindex 52580f99ed..ab1537597c 100644--- a/crates/wasmtime/src/runtime.rs+++ b/crates/wasmtime/src/runtime.rs@@ -112,7 +112,6 @@ pub use trap::*;pub use types::*;pub use v128::V128;pub use values::*;-#[cfg(feature = "pooling-allocator")]pub use vm::{PoolConcurrencyLimitError, PoolingAllocatorMetrics};diff --git a/crates/winch/src/builder.rs b/crates/winch/src/builder.rsindex e91d43e459..3efdb900e2 100644--- a/crates/winch/src/builder.rs+++ b/crates/winch/src/builder.rs@@ -71,6 +71,10 @@ impl CompilerBuilder for Builder {bail!("Winch does not currently support guest-level debugging");}+ if tunables.mmu_interruption {+ bail!("Winch does not currently support MMU interruption");+ }+self.tunables = Some(tunables.clone());self.cranelift.set_tunables(tunables)?;Ok(())diff --git a/tests/all/main.rs b/tests/all/main.rsindex f6716f696e..2db32d4ddc 100644--- a/tests/all/main.rs+++ b/tests/all/main.rs@@ -42,6 +42,7 @@ mod linker;mod memory;mod memory_creator;mod missing_async;+mod mmu_interruption;mod module;mod module_serialize;mod name;diff --git a/tests/all/mmu_interruption.rs b/tests/all/mmu_interruption.rsnew file mode 100644index 0000000000..79bcd6978a--- /dev/null+++ b/tests/all/mmu_interruption.rs@@ -0,0 +1,84 @@+#![cfg(not(miri))]++use wasmtime::{Config, Engine, Strategy};++#[test]+fn requires_signals_based_traps() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.signals_based_traps(false);+ let err = Engine::new(&config).expect_err("engine creation should fail");+ assert_eq!(+ err.to_string(),+ "MMU interruption requires signals-based traps",+ );+}++// With Cranelift only the x64 and aarch64 backends are supported.+#[test]+fn requires_supported_isa() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.target("riscv64").unwrap();+ config.signals_based_traps(true);+ let err = Engine::new(&config).expect_err("engine creation should fail");+ assert_eq!(+ err.to_string(),+ "MMU interruption is supported only on x86_64 and aarch64 Linux, not `riscv64-unknown-unknown-elf`",+ );+}++#[test]+fn requires_linux_target() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.signals_based_traps(true);+ config.target("x86_64-apple-darwin").unwrap();+ let err = Engine::new(&config).expect_err("engine creation should fail");+ assert_eq!(+ err.to_string(),+ "MMU interruption is supported only on x86_64 and aarch64 Linux, not `x86_64-apple-darwin`",+ );+}++// The Winch backend does not support this feature.+#[test]+fn rejected_by_winch() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.strategy(Strategy::Winch);+ config.target("x86_64-unknown-linux-gnu").unwrap();+ let err = Engine::new(&config).expect_err("engine creation should fail");+ assert_eq!(+ err.to_string(),+ "Winch does not currently support MMU interruption",+ );+}++// Pulley does not support this feature, since compiled-in interruption checks+// will be absent.+#[test]+fn rejected_by_pulley() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.signals_based_traps(true);+ config.target("pulley64").unwrap();+ let err = Engine::new(&config).expect_err("engine creation should fail");+ assert_eq!(+ err.to_string(),+ "MMU interruption is supported only on x86_64 and aarch64 Linux, not `pulley64-unknown-unknown-elf`",+ );+}++// AOT compilation succeeds for a supported target, regardless of host.+#[test]+fn precompile_succeeds_for_valid_config_on_any_host() {+ let mut config = Config::new();+ config.mmu_interruption(true);+ config.target("x86_64-unknown-linux-gnu").unwrap();+ config.signals_based_traps(true);+ let engine = Engine::new(&config).unwrap();+ engine+ .precompile_module(r#"(module (memory 0) (func))"#.as_bytes())+ .expect("precompilation should succeed regardless of host");+}