test(wabt): consolidate framing and representation checks (#11890)

This commit is contained in:
morluto authored and GitHub committed 2026-11-18 07:14:50 +08:00
1 parent c728ab984b
commit ebb606a139
5 files changed
+44 -84

No files matched your search

+12 -6
View File
@@ -2,16 +2,20 @@ import { expect, it } from "vitest";
import { parseWabtWat } from "./WabtWat.js";
it("resolves imported index spaces and decoded escaped names without depending on body/type numbering", () => {
const imported = String.raw`(import "a\22b" "\ef\bb\bf\00\0a" (func (;0;) (type 0)))`;
const exported = '(export "" (func 1))';
const report = parseWabtWat(`(module
(type (;0;) (func (param i32) (result i64)))
(import "a\\22b" "\\ef\\bb\\bf\\00" (func (;0;) (type 0)))
${imported}
(func (;1;) (type 0) (param i32) (result i64) local.get 0 i64.extend_i32_s)
(export "" (func 1)))`).interface;
expect(report.entries).toMatchObject([
${exported})`);
expect(report.imports).toEqual([imported]);
expect(report.exports).toEqual([exported]);
expect(report.interface.entries).toMatchObject([
{
direction: "import",
module: 'a"b',
name: "\ufeff\0",
name: "\ufeff\0\n",
index: 0,
signature: { kind: "func", params: ["i32"], results: ["i64"] },
},
@@ -116,9 +120,11 @@ it("preserves custom memory page sizes and treats unadmitted nested declarations
page_size: "1",
});
const unknown = parseWabtWat(
'(module (memory 1 (future-proposal 0)) (export "m" (memory 0)))',
String.raw`(module (memory 1 (future-proposal "ignored\ff")) (export "m" (memory 0)))`,
).interface;
expect(unknown.entries[0]?.signature).toBeNull();
expect(unknown.entries).toMatchObject([
{ name: "m", signature: null, issue: expect.stringMatching(/\S/u) },
]);
});
it("recovers typed name expansion with a larger selected interface allowance", () => {
+1 -37
View File
@@ -6,7 +6,6 @@ const refused = (code: string) =>
import { parseWabtOutput } from "./WabtOutput.js";
import { parseWabtWat } from "./WabtWat.js";
import { associateWasmGlue } from "./WasmGlueAdapter.js";
import { absoluteCallerPath } from "../filesystem/callerPath.js";
const golden = () =>
readFile(
new URL("../../tests/fixtures/wasm/objdump.txt", import.meta.url),
@@ -245,7 +244,6 @@ it.each([
});
});
it.each([
String.raw`\00\06\05hello\01\02\01`,
String.raw`\00\07\05hello\00`,
String.raw`\00\06\05hello\00\06\05later`,
])(
@@ -305,7 +303,7 @@ it("preserves leading U+FEFF as custom-name data, not a debug-name marker", () =
expect(parsed.debugModuleNames).toEqual([]);
expect(() => parseWabtWat('(module (@custom "\\ff" "payload"))')).toThrow();
});
it("extracts escaped WAT import/export forms while retaining multiline producer text", async () => {
it("extracts recorded WAT forms and refuses malformed outer framing", async () => {
const text = await readFile(
new URL("../../tests/fixtures/wasm/decoded.wat", import.meta.url),
"utf8",
@@ -316,21 +314,6 @@ it("extracts escaped WAT import/export forms while retaining multiline producer
customSectionNames: ["test"],
debugModuleNames: [],
});
expect(
parseWabtWat(
'(module (import "a.b" "c\\0aline" (func)) (export "fake\\22Export[1]:" (func 0)))',
),
).toMatchObject({
imports: ['(import "a.b" "c\\0aline" (func))'],
exports: ['(export "fake\\22Export[1]:" (func 0))'],
customSectionNames: [],
debugModuleNames: [],
});
expect(
parseWabtWat(
'(module (; nested (; comment ;) ;) ;; comment\n (export "x" (func 0)))',
).exports,
).toEqual(['(export "x" (func 0))']);
expect(() => parseWabtWat('(module (export "unfinished"')).toThrow(
refused("incomplete_output"),
);
@@ -445,25 +428,6 @@ it("retains directive literals and matches their cooked escaped path", () => {
]);
});
it("matches a CLI-selected ./ module exactly against its glue reference", () => {
// The CLI makes `./module.wasm` absolute from /app with absoluteCallerPath.
const selected = absoluteCallerPath("./module.wasm", {
cwd: "/app",
platform: "linux",
});
expect(
associateWasmGlue('fetch("./module.wasm");', "/app/glue.js", [
selected,
"/other/module.wasm",
]).references,
).toMatchObject([
{
association: "local-path-candidate",
candidate_paths: ["/app/module.wasm"],
},
]);
});
it.each(["\n", "\r", "\r\n", "\u2028", "\u2029"])(
"retains complete .wasm query and fragment literals containing %j",
(separator) => {
+25 -32
View File
@@ -1,52 +1,46 @@
import { expect, it } from "vitest";
import { parseWabtWat } from "./WabtWat.js";
import { watString, watStringEnd } from "./WabtString.js";
it("frames escaped quotes and trailing backslashes without consuming the next form", () => {
const source = String.raw`prefix "a\"b\\" (export "next" (func 0))`;
const end = source.indexOf(" (export");
expect(watStringEnd(source, 8)).toBe(end);
expect(watString(source, 8)).toEqual({
bytes: Buffer.from('a"b\\'),
end,
});
expect(watStringEnd('""', 1)).toBe(2);
for (const count of [1, 2, 3, 4, 5]) {
for (const tail of ['"tail', ""]) {
const value = "\\".repeat(count) + tail;
const encoded = JSON.stringify(value);
const source = encoded + ' (export "next" (func 0))';
expect(watStringEnd(source, 1)).toBe(encoded.length);
expect(
Buffer.compare(watString(source, 1).bytes, Buffer.from(value)),
).toBe(0);
}
}
const longRun = "\\".repeat(5);
const names = [
"",
'a"b\\',
'\\"tail',
"\\\\",
'\\\\"tail',
longRun,
longRun + '"tail',
];
const imports = names.map(
(name) => `(import "env" ${JSON.stringify(name)} (func))`,
);
const exported = String.raw`(export "fake\22Export[1]:" (func 0))`;
const report = parseWabtWat(`(module ${imports.join(" ")} ${exported})`);
expect(report.imports).toEqual(imports);
expect(report.exports).toEqual([exported]);
expect(report.interface.entries.map(({ name }) => name)).toEqual([
...names,
'fake"Export[1]:',
]);
});
it("rejects missing terminators, including a dangling escape or escaped last quote", () => {
for (const source of ['"', '"payload', '"payload\\', '"payload\\"']) {
expect(() => watStringEnd(source, 1)).toThrow(
expect(() => parseWabtWat(`(module (import ${source}`)).toThrow(
expect.objectContaining({
_tag: "WabtAdmissionError",
code: "incomplete_string",
}),
);
expect(() => watString(source, 1)).toThrow(
expect.objectContaining({
_tag: "WabtAdmissionError",
code: "incomplete_string",
}),
);
expect(() => parseWabtWat(`(module (import ${source}`)).toThrow();
}
});
it("preserves quoted WAT punctuation, nested comments, escaped names and unknown signatures", () => {
it("treats comment and form punctuation inside quoted WAT as string data", () => {
const result = parseWabtWat(String.raw`(module
(; outer " (; inner ;) ;)
(import "env" "a\"b\\(;);;\00" (func (param i32)))
(memory 1 (future-proposal "ignored\ff"))
(memory 1)
(export "memory" (memory 0))
(data (i32.const 0) "(; );) ;; \" \\"))`);
expect(result.interface.entries).toMatchObject([
@@ -58,8 +52,7 @@ it("preserves quoted WAT punctuation, nested comments, escaped names and unknown
{
direction: "export",
name: "memory",
signature: null,
issue: expect.any(String),
signature: { kind: "memory", minimum: "1" },
},
]);
expect(() => parseWabtWat('(module (import "env" "\\q" (func)))')).toThrow(
+5 -9
View File
@@ -1,4 +1,5 @@
import { expect, it } from "vitest";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import {
DEFAULT_WASM_INTERFACE_REPRESENTATION_BYTES,
@@ -8,16 +9,12 @@ import { WasmInterfaceBudget } from "../domain/wasm/wasmInterfaceBudget.js";
it("refuses typed WASM interface growth without asking the caller to wait", () => {
const budget = new WasmInterfaceBudget();
expect(() =>
budget.reserve(DEFAULT_WASM_INTERFACE_REPRESENTATION_BYTES + 1),
).toThrowError();
try {
budget.reserve(DEFAULT_WASM_INTERFACE_REPRESENTATION_BYTES + 1);
throw new Error("Expected a representation refusal");
} catch (error) {
const projected = projectAnalysisError(
error as Parameters<typeof projectAnalysisError>[0],
);
if (!(error instanceof AnalysisResourceConstraintError)) throw error;
const projected = projectAnalysisError(error);
expect(projected).toMatchObject({
retryable: false,
details: {
@@ -43,9 +40,8 @@ it("does not suggest an impossible increase at the runtime representation maximu
budget.reserve(MAX_WASM_INTERFACE_REPRESENTATION_BYTES + 1);
throw new Error("Expected serialization capacity refusal");
} catch (error) {
const projected = projectAnalysisError(
error as Parameters<typeof projectAnalysisError>[0],
);
if (!(error instanceof AnalysisResourceConstraintError)) throw error;
const projected = projectAnalysisError(error);
expect(projected).toMatchObject({
details: { reported_limits: { source: "request" } },
});
@@ -92,6 +92,7 @@ ${body}
references: [
{
value: "./selected.wasm",
association: "local-path-candidate",
candidate_paths: [selected],
},
],