Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/wasmparser/src/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// The following limits are imposed by wasmparser on WebAssembly modules.
// The limits are agreed upon with other engines for consistency.
//
// See https://webassembly.github.io/spec/js-api/#limits for details.
pub const MAX_WASM_TYPES: usize = 1_000_000;
pub const MAX_WASM_SUPERTYPES: usize = 1;
pub const MAX_WASM_FUNCTIONS: usize = 1_000_000;
Expand All @@ -26,7 +28,7 @@ pub const MAX_WASM_GLOBALS: usize = 1_000_000;
pub const MAX_WASM_ELEMENT_SEGMENTS: usize = 100_000;
pub const MAX_WASM_DATA_SEGMENTS: usize = 100_000;
pub const MAX_WASM_STRING_SIZE: usize = 100_000;
pub const MAX_WASM_FUNCTION_SIZE: usize = 128 * 1024;
pub const MAX_WASM_FUNCTION_SIZE: usize = 7_654_321;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that I'm not entirely sure what was going on with the previous limit of 128K here -- it wasn't actually used to enforce function-size limits (despite the name of the constant), only the number of branch-table targets. Given the name it seems reasonable to use it for what it says on the tin.

pub const MAX_WASM_FUNCTION_LOCALS: u32 = 50000;
pub const MAX_WASM_FUNCTION_PARAMS: usize = 1000;
pub const MAX_WASM_FUNCTION_RETURNS: usize = 1000;
Expand Down
8 changes: 8 additions & 0 deletions crates/wasmparser/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,14 @@ impl Validator {
) -> Result<FuncToValidate<ValidatorResources>> {
let offset = body.range().start;
self.state.ensure_module("code", offset)?;
check_max(
0,
u32::try_from(body.range().len())
.expect("usize already validated to u32 during section-length decoding"),
MAX_WASM_FUNCTION_SIZE,
"function body size",
offset,
)?;

let state = self.module.as_mut().unwrap();

Expand Down
28 changes: 28 additions & 0 deletions crates/wasmparser/tests/big-module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ fn big_type_indices() {
.validate_all(&wasm)
.unwrap();
}

#[test]
fn big_function_body() {
let mut module = Module::new();

let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);
let mut funcs = FunctionSection::new();
funcs.function(0);
module.section(&funcs);

let mut code = CodeSection::new();
let mut body = Function::new([]);
// Function body larger than the 7_654_321-byte implementation
// limit.
for _ in 0..8_000_000 {
body.instructions().unreachable();
}
body.instructions().end();
code.function(&body);
module.section(&code);

let wasm = module.finish();

let result = wasmparser::Validator::default().validate_all(&wasm);
assert!(result.is_err());
}
Loading