diff --git a/scripts/verify-wasm-artifact.mjs b/scripts/verify-wasm-artifact.mjs index 82ddd0fb0..b45c8ba7d 100644 --- a/scripts/verify-wasm-artifact.mjs +++ b/scripts/verify-wasm-artifact.mjs @@ -247,6 +247,7 @@ try { '"test\nline"', '"fake"\nSection Details:\n Type start=0x0000000a end=0x0000000b (size=0x00000001) count: 0\n"end"', '"unicode-é\\\t"', + '"visible"', ], ); if (debugNames) { diff --git a/src/wasm/WabtOutput.test.ts b/src/wasm/WabtOutput.test.ts index fa65230e3..655dd5dab 100644 --- a/src/wasm/WabtOutput.test.ts +++ b/src/wasm/WabtOutput.test.ts @@ -160,6 +160,19 @@ it.each([ ])("rejects incomplete decoded module-name metadata: %s", (text) => { expect(() => parseWabtWat(text)).toThrow(/metadata/u); }); +it("frames custom section names at their NUL-terminated objdump display", async () => { + const text = await golden(); + expect(parseWabtOutput(text, 76, ["test\0hidden"]).sections.at(-1)).toEqual({ + kind: "Custom", + start: 64, + end: 76, + bytes: 12, + description: '"test"', + }); + expect(() => parseWabtOutput(text, 76, ["other\0test"])).toThrow( + /custom-section header/u, + ); +}); it("extracts escaped WAT import/export forms while retaining multiline producer text", async () => { const text = await readFile( new URL("../../tests/fixtures/wasm/decoded.wat", import.meta.url), diff --git a/src/wasm/WabtOutput.ts b/src/wasm/WabtOutput.ts index e13e94f31..23883510d 100644 --- a/src/wasm/WabtOutput.ts +++ b/src/wasm/WabtOutput.ts @@ -1,3 +1,7 @@ +/** WABT prints names with %.*s, so an embedded NUL ends their display. */ +const wabtDisplayedName = (name: string | undefined): string | undefined => + name?.split("\0", 1)[0]; + /** Frame WABT's raw custom names using the ordered names from its escaped WAT output. */ export const parseWabtOutput = ( text: string, @@ -5,14 +9,10 @@ export const parseWabtOutput = ( customSectionNames: readonly string[], debugModuleNames: readonly string[] = [], ) => { - // WABT prints module names with printf's %.*s: embedded NUL ends the display. const preamble = "\nmodule.wasm:\tfile format wasm 0x1\n" + debugModuleNames - .map((name) => { - const displayedName = name.split("\0", 1)[0]; - return `module name: <${displayedName}>\n`; - }) + .map((name) => `module name: <${wabtDisplayedName(name)}>\n`) .join(""); const prefix = `${preamble}\nSections:\n\n`; if (!text.startsWith(prefix)) @@ -29,7 +29,7 @@ export const parseWabtOutput = ( offset += match[0].length; let description: string; if (match[1] === "Custom") { - const name = customSectionNames[customIndex++]; + const name = wabtDisplayedName(customSectionNames[customIndex++]); if (name === undefined || !text.startsWith(`"${name}"\n`, offset)) throw new Error( "WABT custom-section header does not match decoded WAT.", diff --git a/tests/fixtures/wasm/README.md b/tests/fixtures/wasm/README.md index 9743996dc..1ab586d9c 100644 --- a/tests/fixtures/wasm/README.md +++ b/tests/fixtures/wasm/README.md @@ -9,5 +9,6 @@ custom section. The custom payload is retained as an `@custom` annotation in dec Upstream tag commit: `ff0ef7e0009402740c805a9744c09b05be063e48`. The separate real-tool verifier generates its own fixtures with caller-supplied WABT and compares exact producer output against the public CLI/MCP surfaces. -`custom-names.wat` covers newlines, quotes, Unicode, backslashes and embedded -section markers in custom names; objdump displays those names without escaping. +`custom-names.wat` covers newlines, quotes, Unicode, backslashes, embedded +section markers and NUL in custom names; objdump displays those names without +escaping and ends each at its first NUL. diff --git a/tests/fixtures/wasm/custom-names.wat b/tests/fixtures/wasm/custom-names.wat index c3c2dc988..2231c040d 100644 --- a/tests/fixtures/wasm/custom-names.wat +++ b/tests/fixtures/wasm/custom-names.wat @@ -1,4 +1,5 @@ (module (@custom "test\0aline" "payload") (@custom "fake\22\0aSection Details:\0a Type start=0x0000000a end=0x0000000b (size=0x00000001) count: 0\0a\22end" "payload") - (@custom "unicode-\c3\a9\\\t" "payload")) + (@custom "unicode-\c3\a9\\\t" "payload") + (@custom "visible\00hidden" "payload"))