mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:51:05 +01:00
Plug-ins: the host refuses a function type past 32 results before validating code
Plugin::load validates every function body with wasmparser before wasmi reads the module, and wasmi's EnforcedLimits::strict() limit of 32 parameters and 32 results per function type applied only after that. A module whose type has 1,000 results could make the validator hold 1,000 operand entries for each call to it. check_features now reads the type section first and refuses a function type with more than 32 parameters or results, with wasmi's message, before any function body is validated.
This commit is contained in:
1 parent
1759064a3e
commit
f50b903e4a
2 files changed
+53
-2
No files matched your search
@@ -4,7 +4,7 @@ use wasmi::{
|
||||
Config, CustomFuelCosts, EnforcedLimits, Engine, Instance, Memory, Module, Store, StoreLimitsBuilder, TypedFunc, TypedResumableCall, Val,
|
||||
WasmParams, WasmResults,
|
||||
};
|
||||
use wasmparser::{Validator, WasmFeatures};
|
||||
use wasmparser::{CompositeInnerType, Parser, Payload, Validator, WasmFeatures};
|
||||
|
||||
use crate::host::{self, Bridge, Deadline, Host, Report, Run};
|
||||
use crate::manifest::{MAX_MANIFEST_BYTES, Manifest};
|
||||
@@ -42,12 +42,46 @@ const WASM_FEATURES: WasmFeatures = WasmFeatures::MUTABLE_GLOBAL
|
||||
.union(WasmFeatures::EXTENDED_CONST)
|
||||
.union(WasmFeatures::FLOATS);
|
||||
|
||||
/// The most parameters or results a function type may have: the limit `EnforcedLimits::strict()`
|
||||
/// sets in wasmi 2.0.0.
|
||||
const MAX_FUNC_TYPE_VALUES: usize = 32;
|
||||
|
||||
/// 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.
|
||||
/// uses any other proposal is refused with the validator's error, which names the proposal. A
|
||||
/// function type with more than [`MAX_FUNC_TYPE_VALUES`] parameters or results is refused before
|
||||
/// any function body is validated.
|
||||
fn check_features(bytes: &[u8]) -> Result<()> {
|
||||
check_func_types(bytes)?;
|
||||
Validator::new_with_features(WASM_FEATURES).validate_all(bytes).map(|_| ()).map_err(|e| Error::Module(e.to_string()))
|
||||
}
|
||||
|
||||
/// Refuses a function type in the type section with more than [`MAX_FUNC_TYPE_VALUES`] parameters
|
||||
/// or results, with wasmi's message. Reading stops at the code section. A section that doesn't
|
||||
/// parse is left to the validator, which reports it.
|
||||
fn check_func_types(bytes: &[u8]) -> Result<()> {
|
||||
for payload in Parser::new(0).parse_all(bytes) {
|
||||
let types = match payload {
|
||||
Ok(Payload::TypeSection(types)) => types,
|
||||
Ok(Payload::CodeSectionStart { .. }) | Err(_) => return Ok(()),
|
||||
Ok(_) => continue,
|
||||
};
|
||||
for group in types {
|
||||
let Ok(group) = group else { return Ok(()) };
|
||||
for ty in group.types() {
|
||||
if let CompositeInnerType::Func(f) = &ty.composite_type.inner {
|
||||
if f.params().len() > MAX_FUNC_TYPE_VALUES {
|
||||
return Err(Error::Module(format!("a function type exceeds the limit of {MAX_FUNC_TYPE_VALUES} parameters")));
|
||||
}
|
||||
if f.results().len() > MAX_FUNC_TYPE_VALUES {
|
||||
return Err(Error::Module(format!("a function type exceeds the limit of {MAX_FUNC_TYPE_VALUES} results")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
|
||||
@@ -197,6 +197,23 @@ fn a_plugin_may_use_the_listed_webassembly_proposals_and_no_others() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A function type with more than 32 parameters or results is refused before any function body is
|
||||
/// validated: the module's extra function has an invalid body, and the error names the limit. A
|
||||
/// type with 32 of each loads.
|
||||
#[test]
|
||||
fn a_function_type_past_32_values_is_refused_before_any_function_body_is_validated() {
|
||||
let i32s = |n: usize| vec!["i32"; n].join(" ");
|
||||
for (word, field) in [("parameters", "param"), ("results", "result")] {
|
||||
let fits = module(MANIFEST, tpl::ECHO, &format!("(type (func ({field} {})))", i32s(32)));
|
||||
assert!(Plugin::load(&fits, Limits::default()).is_ok(), "32 {word}");
|
||||
let past = module(MANIFEST, tpl::ECHO, &format!("(type (func ({field} {}))) (func (i32.add))", i32s(33)));
|
||||
match Plugin::load(&past, Limits::default()) {
|
||||
Err(Error::Module(m)) => assert_eq!(m, format!("a function type exceeds the limit of 32 {word}")),
|
||||
other => panic!("33 {word}: {:?}", other.err()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn budgets_stop_runaway_plugins() {
|
||||
let spin = ok_module("(loop $l (br $l)) (i64.const 0)");
|
||||
|
||||
Reference in new issue
Block a user