Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for the module size limit #1642

Merged
merged 2 commits into from
May 2, 2023
Merged
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
50 changes: 50 additions & 0 deletions test/js-api/limits.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,53 @@ test(() => {
() => memory.grow(kJSEmbeddingMaxTableSize));
}, `Grow WebAssembly.Table object beyond the embedder-defined limit`);

function testModuleSizeLimit(size, expectPass) {
// We do not use `testLimit` here to avoid OOMs due to having multiple big
// modules alive at the same time.

// Define a WebAssembly module that consists of a single custom section which
// has an empty name. The module size will be `size`.
const buffer = new Uint8Array(size);
const header = [
kWasmH0, kWasmH1, kWasmH2, kWasmH3, // magic word
kWasmV0, kWasmV1, kWasmV2, kWasmV3, // version
0 // custom section
];
// We calculate the section length so that the total module size is `size`.
// For that we have to calculate the length of the leb encoding of the section
// length.
const sectionLength = size - header.length -
wasmSignedLeb(size).length;
const lengthBytes = wasmSignedLeb(sectionLength);
buffer.set(header);
buffer.set(lengthBytes, header.length);

if (expectPass) {
test(() => {
assert_true(WebAssembly.validate(buffer));
}, `Validate module size limit`);
test(() => {
new WebAssembly.Module(buffer);
}, `Compile module size limit`);
promise_test(t => {
return WebAssembly.compile(buffer);
}, `Async compile module size limit`);
} else {
test(() => {
assert_false(WebAssembly.validate(buffer));
}, `Validate module size over limit`);
test(() => {
assert_throws(
new WebAssembly.CompileError(),
() => new WebAssembly.Module(buffer));
}, `Compile module size over limit`);
promise_test(t => {
return promise_rejects(
t, new WebAssembly.CompileError(),
WebAssembly.compile(buffer));
}, `Async compile module size over limit`);
}
}

testModuleSizeLimit(kJSEmbeddingMaxModuleSize, true);
testModuleSizeLimit(kJSEmbeddingMaxModuleSize + 1, false);