Skip to content

Truly independent web browser

LibWasm: Limit functions to 50000 locals, their parameters included

Andreas Kling5d1c94485157
diff --git a/Libraries/LibWasm/Module/src/parser.rs b/Libraries/LibWasm/Module/src/parser.rs
index 65a03a97065..d6ba8e9da8d 100644
--- a/Libraries/LibWasm/Module/src/parser.rs
+++ b/Libraries/LibWasm/Module/src/parser.rs
@@ -20,10 +20,6 @@ use crate::types::{
SubType, TableType, TagType, ValType,
};
-/// The most locals one (count, type) run of a function may declare. Every local takes a slot of the function's
-/// machine stack frame, so this bounds the frame size a module can ask for.
-pub const MAX_LOCALS_PER_RUN: u32 = 42069;
-
const SECTION_CUSTOM: u8 = 0;
const SECTION_TYPE: u8 = 1;
const SECTION_IMPORT: u8 = 2;
@@ -655,9 +651,6 @@ fn parse_function_body(section: &mut Reader<'_>) -> Result<FunctionBody> {
let mut declared_local_count = 0u32;
for _ in 0..run_count {
let count = body.read_u32()?;
- if count > MAX_LOCALS_PER_RUN {
- return Err(Error::malformed("too many locals"));
- }
declared_local_count = declared_local_count
.checked_add(count)
.ok_or_else(|| Error::malformed("too many locals"))?;
diff --git a/Libraries/LibWasm/Module/src/validator.rs b/Libraries/LibWasm/Module/src/validator.rs
index 635c289cb05..8c29913656c 100644
--- a/Libraries/LibWasm/Module/src/validator.rs
+++ b/Libraries/LibWasm/Module/src/validator.rs
@@ -139,6 +139,10 @@ impl FunctionInfo {
const MEMORY32_MAX_PAGES: u64 = 1 << 16;
const MEMORY64_MAX_PAGES: u64 = 1 << 48;
+/// The most locals a function may have, its parameters included, as the JS API's implementation limits say. Every
+/// local takes a slot of the function's machine stack frame, so this bounds the frame size a module can ask for.
+const MAX_FUNCTION_LOCALS: usize = 50_000;
+
fn invalid<T>(message: impl Into<String>) -> Result<T> {
Err(Error::invalid(message))
}
@@ -2379,6 +2383,9 @@ pub fn validate(module: &mut Module) -> Result<()> {
let function = module.types[type_index as usize]
.as_func()
.expect("function types are checked");
+ if function.params.len() + body.declared_local_count as usize > MAX_FUNCTION_LOCALS {
+ return invalid("too many locals");
+ }
let mut locals = function.params.clone();
locals.reserve(body.declared_local_count as usize);
for &(count, ty) in &body.locals {
diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-locals-limit.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-locals-limit.txt
index 717422f970a..392d4c8615f 100644
--- a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-locals-limit.txt
+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-locals-limit.txt
@@ -1,4 +1,4 @@
-50000 locals with the parameters: false
+50000 locals with the parameters: true
50001 locals with the parameters: false
-50001 locals without parameters: true
-84138 locals in two runs: true
+50001 locals without parameters: false
+84138 locals in two runs: false