From b0fba57f4a321180feedbc4ae15cb65347ae02c0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 16:29:20 -0800 Subject: [PATCH] Plug-ins: the host checks the WebAssembly features a plug-in uses Plugin::load now validates each module with wasmparser and a fixed list of WebAssembly features before wasmi reads it: mutable globals, sign extension, saturating conversions, multi-value, bulk memory, reference types, tail calls, extended constant expressions and floats. A module that uses any other feature is refused, so a wasmi upgrade, a change to its defaults or one of its crate features turned on elsewhere in the build does not change what a plug-in may use. wasmi's Config is set to the same list for every setter it has in this build. The list is the set wasmi accepted before, without multi-memory. A second memory was already refused by the one-memory limit; its error now says "multiple memories". No module that loaded before is refused. wasmparser 0.228.0 is the version wasmi 2.0.0 already uses. A new test loads a plug-in that uses each listed feature and checks that modules using SIMD, relaxed SIMD, memory64, a second memory, custom page sizes, wide arithmetic, threads, exceptions, legacy exceptions, function references or garbage collection are refused with an error that names the feature. docs/plugins.md lists the whole set; it named only four of the allowed features before. --- Cargo.lock | 1 + Cargo.toml | 3 +++ crates/plugins/Cargo.toml | 1 + crates/plugins/src/runtime.rs | 42 +++++++++++++++++++++++++++++- crates/plugins/tests/abi.rs | 48 +++++++++++++++++++++++++++++++++++ docs/plugins.md | 11 +++++--- 6 files changed, 101 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59e5fec9bb..10f643bb87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7065,6 +7065,7 @@ dependencies = [ "vectorcraft-doc", "vectorcraft-geom 0.24.1", "wasmi", + "wasmparser 0.228.0", "wat", "web-time", ] diff --git a/Cargo.toml b/Cargo.toml index 0279fa081a..c77f759f06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,6 +97,9 @@ socket2 = "0.6" # too. Without `wat` (text parsing is test-only) and memory64; `validate` keeps full module # validation on; the portable dispatcher can't overflow the host stack on long-running modules. wasmi = { version = "2.0.0", default-features = false, features = ["std", "validate", "stable", "portable-dispatch"] } +# The plug-in host's check of the WebAssembly features a module uses (Apache-2.0 WITH +# LLVM-exception). It is the version wasmi 2.0.0 uses. +wasmparser = { version = "0.228.0", default-features = false, features = ["std", "validate", "features"] } # Tests: WebAssembly text -> binary for the plug-in fixtures (Apache-2.0 WITH LLVM-exception). wat = "1.228" diff --git a/crates/plugins/Cargo.toml b/crates/plugins/Cargo.toml index 28b972d679..8cd7ceb62f 100644 --- a/crates/plugins/Cargo.toml +++ b/crates/plugins/Cargo.toml @@ -22,6 +22,7 @@ craft-vectorcraft-format = { workspace = true } sha2 = { version = "0.10", default-features = false } thiserror = { workspace = true } wasmi = { workspace = true } +wasmparser = { workspace = true } # The monotonic clock of run deadlines and the first-view budget: `std::time::Instant` on the # desktop, the page's `performance.now()` in the web build. It is MIT OR Apache-2.0 and already in # the build through eframe. diff --git a/crates/plugins/src/runtime.rs b/crates/plugins/src/runtime.rs index 0cb6197e97..1a25e9e6b0 100644 --- a/crates/plugins/src/runtime.rs +++ b/crates/plugins/src/runtime.rs @@ -4,6 +4,7 @@ use wasmi::{ Config, CustomFuelCosts, EnforcedLimits, Engine, Instance, Memory, Module, Store, StoreLimitsBuilder, TypedFunc, TypedResumableCall, Val, WasmParams, WasmResults, }; +use wasmparser::{Validator, WasmFeatures}; use crate::host::{self, Bridge, Deadline, Host, Report, Run}; use crate::manifest::{MAX_MANIFEST_BYTES, Manifest}; @@ -27,6 +28,44 @@ const FUEL_COSTS: CustomFuelCosts = CustomFuelCosts { bytes_copied_per_fuel: 64, /// Bytes a bulk memory instruction copies or fills for each unit of fuel. pub const BYTES_COPIED_PER_FUEL: u32 = FUEL_COSTS.bytes_copied_per_fuel; +/// The WebAssembly proposals a plug-in may use, as `docs/plugins.md` lists them. `GC_TYPES` lets +/// reference types use `funcref` and `externref`; the garbage collection proposal stays off. Every +/// proposal not listed here is off, whatever wasmi's defaults and crate features turn on. +const WASM_FEATURES: WasmFeatures = WasmFeatures::MUTABLE_GLOBAL + .union(WasmFeatures::SIGN_EXTENSION) + .union(WasmFeatures::SATURATING_FLOAT_TO_INT) + .union(WasmFeatures::MULTI_VALUE) + .union(WasmFeatures::BULK_MEMORY) + .union(WasmFeatures::REFERENCE_TYPES) + .union(WasmFeatures::GC_TYPES) + .union(WasmFeatures::TAIL_CALL) + .union(WasmFeatures::EXTENDED_CONST) + .union(WasmFeatures::FLOATS); + +/// Validates `bytes` with only [`WASM_FEATURES`] on, before wasmi reads the module. A module that +/// uses any other proposal is refused with the validator's error, which names the proposal. +fn check_features(bytes: &[u8]) -> Result<()> { + Validator::new_with_features(WASM_FEATURES).validate_all(bytes).map(|_| ()).map_err(|e| Error::Module(e.to_string())) +} + +/// Sets each WebAssembly proposal that wasmi's `Config` has a setter for in this build on or off, +/// to match [`WASM_FEATURES`]. +fn set_features(config: &mut Config) -> &mut Config { + config + .wasm_mutable_global(true) + .wasm_sign_extension(true) + .wasm_saturating_float_to_int(true) + .wasm_multi_value(true) + .wasm_bulk_memory(true) + .wasm_reference_types(true) + .wasm_tail_call(true) + .wasm_extended_const(true) + .floats(true) + .wasm_multi_memory(false) + .wasm_custom_page_sizes(false) + .wasm_wide_arithmetic(false) +} + /// Instructions a heavy filter's run may use besides [`Limits::fuel_per_byte`] for each byte of /// input ([`crate::Budget::Heavy`]): 20 times a light one's. Its output's size can't add to it: /// the host learns that only once the run has ended. @@ -171,8 +210,9 @@ impl Plugin { if !bytes.starts_with(b"\0asm") { return Err(Error::Module("not a WebAssembly binary (missing \\0asm header)".into())); } + check_features(bytes)?; let mut config = Config::default(); - config + set_features(&mut config) .consume_fuel(true) .fuel_cost(FUEL_COSTS) .enforced_limits(EnforcedLimits::strict()) diff --git a/crates/plugins/tests/abi.rs b/crates/plugins/tests/abi.rs index 26dec0a7d2..ae99846c69 100644 --- a/crates/plugins/tests/abi.rs +++ b/crates/plugins/tests/abi.rs @@ -149,6 +149,54 @@ fn malformed_and_oversized_modules_are_refused() { assert!(matches!(e, Error::Abi(_)), "{e}"); } +/// The WebAssembly proposals `docs/plugins.md` lets a plug-in use, each with module fields that +/// use it. +const ACCEPTED_PROPOSALS: &[(&str, &str)] = &[ + ("mutable globals", r#"(global (export "counter") (mut i32) (i32.const 0))"#), + ("sign extension", "(func (param i32) (result i32) (i32.extend8_s (local.get 0)))"), + ("saturating conversions", "(func (param f32) (result i32) (i32.trunc_sat_f32_s (local.get 0)))"), + ("multi-value", "(func (result i32 i32) (i32.const 1) (i32.const 2))"), + ("bulk memory", "(func (memory.fill (i32.const 0) (i32.const 0) (i32.const 0)))"), + ("reference types", "(func (result externref) (ref.null extern))"), + ("tail calls", "(func $again (param i32) (return_call $again (local.get 0)))"), + ("extended constant expressions", "(global i32 (i32.add (i32.const 1) (i32.const 2)))"), +]; + +/// Modules that use a WebAssembly proposal plug-ins may not use, each with the words its error +/// names it by. +const REFUSED_PROPOSALS: &[(&str, &str)] = &[ + ("(module (func (result v128) (v128.const i32x4 0 0 0 0)))", "SIMD"), + ("(module (func (result v128) (i32x4.relaxed_trunc_f32x4_s (v128.const i32x4 0 0 0 0))))", "SIMD"), + ("(module (memory i64 1))", "memory64"), + ("(module (memory 1) (memory 1))", "multiple memories"), + ("(module (memory 1 (pagesize 1)))", "custom page sizes"), + ( + "(module (func (param i64 i64 i64 i64) (result i64 i64) (i64.add128 (local.get 0) (local.get 1) (local.get 2) (local.get 3))))", + "wide arithmetic", + ), + ("(module (memory 1 1 shared))", "threads"), + ("(module (tag))", "exceptions"), + ("(module (func try catch_all end))", "legacy exceptions"), + ("(module (type $t (func)) (func (param (ref $t))))", "function references"), + ("(module (type (struct)))", "gc feature"), +]; + +#[test] +fn a_plugin_may_use_the_listed_webassembly_proposals_and_no_others() { + for (name, fields) in ACCEPTED_PROPOSALS { + let bytes = module(MANIFEST, tpl::ECHO, fields); + let out = Plugin::load(&bytes, Limits::default()).and_then(|p| p.run(INPUT.as_bytes(), b"{}")); + assert!(out.is_ok(), "{name}: {out:?}"); + } + for (src, name) in REFUSED_PROPOSALS { + match Plugin::load(&build(src), Limits::default()) { + Err(Error::Module(m)) => assert!(m.to_lowercase().contains(&name.to_lowercase()), "{name}: the error doesn't name it: {m}"), + Err(e) => panic!("{name}: refused with another kind of error: {e}"), + Ok(_) => panic!("{name}: a module that uses it must be refused"), + } + } +} + #[test] fn budgets_stop_runaway_plugins() { let spin = ok_module("(loop $l (br $l)) (i64.const 0)"); diff --git a/docs/plugins.md b/docs/plugins.md index 09bb7c8dbd..d81eb153e0 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -353,10 +353,13 @@ each time it is drawn, and a paint linked to a global swatch is drawn in that sw ## ABI v1 -A plug-in is a core WebAssembly module (MVP + the usual post-MVP features: bulk memory, sign extension, multi-value, -saturating conversions; no SIMD, no memory64) with these exports and no imports other than the [host -imports](#host-imports-api-level-2) of API level 2. Integers are `i32` unless noted; pointers are offsets into the -module's memory. +A plug-in is a core WebAssembly module (MVP + these post-MVP features: mutable globals, sign extension, saturating +conversions, multi-value, bulk memory, reference types, tail calls and extended constant expressions) with one memory, +these exports and no imports other than the [host imports](#host-imports-api-level-2) of API level 2. Before wasmi reads +a module, the host validates it with only these features on, and refuses a module that uses any other feature. The error +names the feature for SIMD, relaxed SIMD, memory64, multi-memory, custom page sizes, wide arithmetic, threads, +exceptions, function references and garbage collection. Integers are `i32` unless noted; +pointers are offsets into the module's memory. | Export | Signature | Meaning | |---|---|---|