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.
This commit is contained in:
Vinícius Andrade
2026-10-12 08:41:55 +08:00
committed by GitHub
parent 4b653fe6ad
commit 1febc22907
5 changed files with 25 additions and 9 deletions
+1
View File
@@ -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) {
+13
View File
@@ -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),
+6 -6
View File
@@ -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.",
+3 -2
View File
@@ -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.
+2 -1
View File
@@ -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"))