Skip to content

Truly independent web browser

Tests/LibWeb: Add a test for the WebAssembly function size limit

Andreas Klingf250cc433224
diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-function-size-limit.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-function-size-limit.txt
new file mode 100644
index 00000000000..358fed552d5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-function-size-limit.txt
@@ -0,0 +1,2 @@
+Function body of 7654321 bytes: true
+Function body of 7654322 bytes: true
diff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-function-size-limit.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-function-size-limit.html
new file mode 100644
index 00000000000..c38d167855e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-function-size-limit.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ function leb(value) {
+ const bytes = [];
+ do {
+ let byte = value % 128;
+ value = Math.floor(value / 128);
+ if (value !== 0)
+ byte |= 0x80;
+ bytes.push(byte);
+ } while (value !== 0);
+ return bytes;
+ }
+
+ function section(id, contents) {
+ return [id, ...leb(contents.length), ...contents];
+ }
+
+ // A module with one function whose body is `size` bytes long: no locals, then nops up to its end.
+ function moduleWithFunctionOfSize(size) {
+ const prefix = [
+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
+ ...section(1, [0x01, 0x60, 0x00, 0x00]),
+ ...section(3, [0x01, 0x00]),
+ ];
+ const code = [0x01, ...leb(size)];
+ const codeSectionHeader = [10, ...leb(code.length + size)];
+ const bytes = new Uint8Array(prefix.length + codeSectionHeader.length + code.length + size);
+ bytes.set(prefix);
+ bytes.set(codeSectionHeader, prefix.length);
+ bytes.set(code, prefix.length + codeSectionHeader.length);
+ const bodyStart = prefix.length + codeSectionHeader.length + code.length;
+ bytes.fill(0x01, bodyStart + 1, bodyStart + size - 1);
+ bytes[bodyStart] = 0x00;
+ bytes[bodyStart + size - 1] = 0x0b;
+ return bytes;
+ }
+
+ test(() => {
+ println(`Function body of 7654321 bytes: ${WebAssembly.validate(moduleWithFunctionOfSize(7654321))}`);
+ println(`Function body of 7654322 bytes: ${WebAssembly.validate(moduleWithFunctionOfSize(7654322))}`);
+ });
+</script>