From 1febc22907f1cc8035dd1450616233121bd7f19d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Andrade?= <95101505+viniciusdsandrade@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:41:55 -0300 Subject: [PATCH] fix(wasm): inspect modules whose custom section names contain NUL (#1981) * fix(wasm): frame custom section names at their displayed NUL wasm-objdump prints a custom section name with printf's %.*s, so an embedded NUL ends the displayed name, as it does for module names. The section row was compared with the complete name decoded from WAT, so a valid module whose custom section name contains U+0000 failed inspection with "WABT custom-section header does not match decoded WAT". Compare the row with the displayed prefix instead. * test(wasm): cover NUL-terminated custom section names Frame the real objdump golden with a decoded custom section name that continues after a NUL, and keep rejecting a displayed prefix that differs from the decoded name. * test(wasm): verify a NUL-containing custom section name with real WABT Add a custom section named "visible\0hidden" to the real-tool custom name fixture. WABT 1.0.42 decodes it as an escaped WAT name and displays "visible" in wasm-objdump, so the verifier expects that description through the compiled CLI and MCP. * refactor(wasm): share WABT's NUL-terminated name display Module-name preamble lines and custom-section rows each truncated the decoded name at its first NUL. Keep that display rule in one helper so both framings follow the same producer behavior. Output is unchanged. --- scripts/verify-wasm-artifact.mjs | 1 + src/wasm/WabtOutput.test.ts | 13 +++++++++++++ src/wasm/WabtOutput.ts | 12 ++++++------ tests/fixtures/wasm/README.md | 5 +++-- tests/fixtures/wasm/custom-names.wat | 3 ++- 5 files changed, 25 insertions(+), 9 deletions(-) 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"))