Merge pull request #8901 from dotCooCoo/feat/plugin-wasm-features

This commit is contained in:
BFlat
2026-11-21 09:07:52 +07:00
committed by GitHub
6 changed files with 101 additions and 5 deletions
Generated
+1
View File
@@ -7064,6 +7064,7 @@ dependencies = [
"vectorcraft-doc",
"vectorcraft-geom 0.24.1",
"wasmi",
"wasmparser 0.228.0",
"wat",
"web-time",
]
+3
View File
@@ -98,6 +98,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"
+1
View File
@@ -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.
+41 -1
View File
@@ -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())
+48
View File
@@ -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)");
+7 -4
View File
@@ -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 |
|---|---|---|