Skip to content

Commit dec2738

Browse files
authored
Add test for the module size limit (#1642)
* Add test for the module size limit The limits test did not test the maximum supported module size of 1GB yet. This PR adds tests which create modules consisting of a single custom section. The first test checks if a module of size 1GB is allowed, the second test checks that a module of size 1GB + 1 byte gets rejected. * Some cleanup
1 parent 49e87dc commit dec2738

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/js-api/limits.any.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,53 @@ test(() => {
244244
() => memory.grow(kJSEmbeddingMaxTableSize));
245245
}, `Grow WebAssembly.Table object beyond the embedder-defined limit`);
246246

247+
function testModuleSizeLimit(size, expectPass) {
248+
// We do not use `testLimit` here to avoid OOMs due to having multiple big
249+
// modules alive at the same time.
250+
251+
// Define a WebAssembly module that consists of a single custom section which
252+
// has an empty name. The module size will be `size`.
253+
const buffer = new Uint8Array(size);
254+
const header = [
255+
kWasmH0, kWasmH1, kWasmH2, kWasmH3, // magic word
256+
kWasmV0, kWasmV1, kWasmV2, kWasmV3, // version
257+
0 // custom section
258+
];
259+
// We calculate the section length so that the total module size is `size`.
260+
// For that we have to calculate the length of the leb encoding of the section
261+
// length.
262+
const sectionLength = size - header.length -
263+
wasmSignedLeb(size).length;
264+
const lengthBytes = wasmSignedLeb(sectionLength);
265+
buffer.set(header);
266+
buffer.set(lengthBytes, header.length);
267+
268+
if (expectPass) {
269+
test(() => {
270+
assert_true(WebAssembly.validate(buffer));
271+
}, `Validate module size limit`);
272+
test(() => {
273+
new WebAssembly.Module(buffer);
274+
}, `Compile module size limit`);
275+
promise_test(t => {
276+
return WebAssembly.compile(buffer);
277+
}, `Async compile module size limit`);
278+
} else {
279+
test(() => {
280+
assert_false(WebAssembly.validate(buffer));
281+
}, `Validate module size over limit`);
282+
test(() => {
283+
assert_throws(
284+
new WebAssembly.CompileError(),
285+
() => new WebAssembly.Module(buffer));
286+
}, `Compile module size over limit`);
287+
promise_test(t => {
288+
return promise_rejects(
289+
t, new WebAssembly.CompileError(),
290+
WebAssembly.compile(buffer));
291+
}, `Async compile module size over limit`);
292+
}
293+
}
294+
295+
testModuleSizeLimit(kJSEmbeddingMaxModuleSize, true);
296+
testModuleSizeLimit(kJSEmbeddingMaxModuleSize + 1, false);

0 commit comments

Comments
 (0)