A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Error on wasm SIMD for riscv64 without vector support (#14897)
Ilyas Mallahd706822a76f6
diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rsindex cf1ea2b264..c754ff49de 100644--- a/crates/cranelift/src/compiler.rs+++ b/crates/cranelift/src/compiler.rs@@ -870,6 +870,10 @@ impl wasmtime_environ::Compiler for Compiler {self.isa.is_branch_protection_enabled()}+ fn supports_wasm_simd(&self) -> bool {+ self.isa.supports_vector_load_store(ir::types::I8X16)+ }+fn component_compiler(&self) -> &dyn wasmtime_environ::component::ComponentCompiler {self}diff --git a/crates/environ/src/compile/mod.rs b/crates/environ/src/compile/mod.rsindex dbfd044977..a84e38d570 100644--- a/crates/environ/src/compile/mod.rs+++ b/crates/environ/src/compile/mod.rs@@ -403,6 +403,12 @@ pub trait Compiler: Send + Sync {/// Get a flag indicating whether branch protection is enabled.fn is_branch_protection_enabled(&self) -> bool;+ /// Returns whether this compiler can compile the WebAssembly `v128` type+ /// for its target.+ fn supports_wasm_simd(&self) -> bool {+ true+ }+/// Returns a suitable compiler usable for component-related compilations.////// Note that the `ComponentCompiler` trait can also be implemented fordiff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rsindex c4f7bcddb7..254452409b 100644--- a/crates/wasmtime/src/config.rs+++ b/crates/wasmtime/src/config.rs@@ -1127,6 +1127,11 @@ impl Config {/// On x86_64 platforms the base CPU feature requirement for SIMD/// is SSE2 for the Cranelift compiler and AVX for the Winch compiler.///+ /// On riscv64 the Cranelift compiler requires the `has_v` (or+ /// `has_zvl128b`) setting for SIMD, which is not detected when+ /// [`Config::target`] is used. Without it creating an [`Engine`] fails+ /// unless this option and [`Config::wasm_relaxed_simd`] are disabled.+ ////// This is `true` by default.////// [proposal]: https://github.com/webassembly/simd@@ -1155,6 +1160,10 @@ impl Config {/// deterministic behavior across all platforms, as classified by the/// specification, at the cost of performance.///+ /// On riscv64 the Cranelift compiler requires the `has_v` (or+ /// `has_zvl128b`) setting for relaxed SIMD. Without it creating an+ /// [`Engine`] fails unless this option is disabled.+ ////// This is `true` by default.////// [proposal]: https://github.com/webassembly/relaxed-simd@@ -3199,7 +3208,24 @@ impl Config {compiler.wmemcheck(self.compiler_config_mut().wmemcheck);- Ok((self, compiler.build()?))+ let compiler = compiler.build()?;++ // SIMD support can depend on ISA flags, such as `has_v` on riscv64,+ // which aren't known until now, so it's checked here and not in+ // `compiler_panicking_wasm_features`.+ if !compiler.supports_wasm_simd() {+ let simd = WasmFeatures::SIMD | WasmFeatures::RELAXED_SIMD;+ if let Some((name, _)) = (features & simd).iter_names().next() {+ bail!(+ "the wasm_{} feature is not supported on this compiler \+ configuration, enable the target's vector extension or \+ disable both simd and relaxed-simd",+ name.to_lowercase()+ );+ }+ }++ Ok((self, compiler))}/// Internal setting for whether adapter modules for components will havediff --git a/docs/stability-tiers.md b/docs/stability-tiers.mdindex aca5fb810f..e317f2f381 100644--- a/docs/stability-tiers.md+++ b/docs/stability-tiers.md@@ -303,10 +303,10 @@ by default and therefore supported as tier 1. The legend here is:| [`multi-value`] | ✅ | ❌ || [`bulk-memory`] | ✅ | ❌ || [`reference-types`] | ✅ | ❌[^a] |-| [`simd`] | ✅ | ❌ |+| [`simd`] | ✅[^d] | ❌ || [`component-model`] | ✅ | ❌ || [`component-model-async`] | ✅ | ❌ |-| [`relaxed-simd`] | ✅ | ❌ |+| [`relaxed-simd`] | ✅[^d] | ❌ || [`multi-memory`] | ✅ | ❌ || [`threads`] | ✅ | ❌ || [`tail-call`] | ✅ | ❌ |@@ -358,6 +358,9 @@ emitting Pulley bytecode.[^b]: Pulley does not support the [`threads`] proposal because there is no knownsafe way to implement this with Rust's memory model.[^c]: Winch's support for aarch64 is complete for Core Wasm.+[^d]: On riscv64 requires the V extension (the `has_v` Cranelift setting). The+ proposals are enabled by default, so without it they must be disabled+ explicitly or creating an engine fails.## Tier Detailsdiff --git a/tests/all/defaults.rs b/tests/all/defaults.rsindex 4a2d52cd40..1e0e2abd9d 100644--- a/tests/all/defaults.rs+++ b/tests/all/defaults.rs@@ -23,6 +23,8 @@ fn test_tail_call_default() -> Result<()> {true,Config::new().strategy(Strategy::Cranelift)+ .wasm_simd(false)+ .wasm_relaxed_simd(false).target("riscv64")?,),(diff --git a/tests/all/module.rs b/tests/all/module.rsindex 4b99a84759..b97d71f6a9 100644--- a/tests/all/module.rs+++ b/tests/all/module.rs@@ -32,7 +32,14 @@ fn checks_incompatible_target() -> Result<()> {return Ok(());fn assert_invalid_target(target: &str) -> Result<()> {- match Module::new(&Engine::new(Config::new().target(target)?)?, "(module)") {+ // A riscv64 target without `has_v` rejects SIMD, which would fail+ // engine creation before the mismatch is reported.+ let mut config = Config::new();+ config+ .target(target)?+ .wasm_simd(false)+ .wasm_relaxed_simd(false);+ match Module::new(&Engine::new(&config)?, "(module)") {Ok(_) => unreachable!(),Err(e) => assert!(format!("{e:?}").contains("configuration does not match the host"),@@ -121,6 +128,58 @@ fn memory_copy_without_riscv_v() -> Result<()> {Ok(())}+#[test]+#[cfg_attr(miri, ignore)]+fn simd_without_riscv_v() -> Result<()> {+ let wat = br#"+ (module+ (func (export "add") (param v128 v128) (result v128)+ (i32x4.add (local.get 0) (local.get 1)))+ )+ "#;+ let new_config = |has_v: bool| -> Result<Config> {+ let mut config = Config::new();+ config.strategy(Strategy::Cranelift).target("riscv64")?;+ unsafe {+ config.cranelift_flag_set("has_v", if has_v { "true" } else { "false" });+ }+ Ok(config)+ };++ // SIMD is enabled by default, so without vector support creating an+ // engine is an error instead of a panic in the backend later on.+ let err = Engine::new(&new_config(false)?).unwrap_err();+ assert!(+ format!("{err:?}").contains("wasm_simd feature is not supported"),+ "bad error: {err:?}"+ );++ // Disabling only relaxed SIMD leaves SIMD enabled.+ let mut config = new_config(false)?;+ config.wasm_relaxed_simd(false);+ let err = Engine::new(&config).unwrap_err();+ assert!(+ format!("{err:?}").contains("wasm_simd feature is not supported"),+ "bad error: {err:?}"+ );++ // Disabling both works, and modules using SIMD then fail validation.+ let mut config = new_config(false)?;+ config.wasm_simd(false).wasm_relaxed_simd(false);+ let engine = Engine::new(&config)?;+ let err = engine.precompile_module(wat).unwrap_err();+ assert!(+ format!("{err:?}").contains("SIMD support is not enabled"),+ "bad error: {err:?}"+ );++ // With vector support the default configuration compiles SIMD.+ let engine = Engine::new(&new_config(true)?)?;+ engine.precompile_module(wat)?;++ Ok(())+}+#[test]#[cfg_attr(miri, ignore)]fn serialize_deterministic() {diff --git a/tests/disas.rs b/tests/disas.rsindex 27bc9ee2a3..f610e45827 100644--- a/tests/disas.rs+++ b/tests/disas.rs@@ -181,6 +181,16 @@ impl Test {let mut opts = wasmtime_cli_flags::CommonOptions::try_parse_from(&flags)?;opts.codegen.cranelift_debug_verifier = Some(true);+ // Wasm SIMD is enabled by default but riscv64 needs `has_v` for it,+ // which isn't set here. Tests that want SIMD enable it with flags.+ if config.target.starts_with("riscv64")+ && opts.wasm.simd.is_none()+ && opts.wasm.relaxed_simd.is_none()+ {+ opts.wasm.simd = Some(false);+ opts.wasm.relaxed_simd = Some(false);+ }+Ok(Test {path: path.to_path_buf(),config,