* 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.
76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
/** 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,
|
|
artifactBytes: number,
|
|
customSectionNames: readonly string[],
|
|
debugModuleNames: readonly string[] = [],
|
|
) => {
|
|
const preamble =
|
|
"\nmodule.wasm:\tfile format wasm 0x1\n" +
|
|
debugModuleNames
|
|
.map((name) => `module name: <${wabtDisplayedName(name)}>\n`)
|
|
.join("");
|
|
const prefix = `${preamble}\nSections:\n\n`;
|
|
if (!text.startsWith(prefix))
|
|
throw new Error("Unsupported WABT section header format.");
|
|
let offset = prefix.length;
|
|
let customIndex = 0;
|
|
const sections = [];
|
|
while (!text.startsWith("\nSection Details:\n", offset)) {
|
|
const match =
|
|
/^[ \t]*(\w+) start=0x([\da-f]+) end=0x([\da-f]+) \(size=0x([\da-f]+)\) /u.exec(
|
|
text.slice(offset),
|
|
);
|
|
if (match === null) throw new Error("Unrecognized WABT section row.");
|
|
offset += match[0].length;
|
|
let description: string;
|
|
if (match[1] === "Custom") {
|
|
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.",
|
|
);
|
|
description = `"${name}"`;
|
|
offset += description.length + 1;
|
|
} else {
|
|
const end = text.indexOf("\n", offset);
|
|
if (end < 0) throw new Error("Incomplete WABT section row.");
|
|
description = text.slice(offset, end).trim();
|
|
offset = end + 1;
|
|
}
|
|
const section = {
|
|
kind: match[1] ?? "",
|
|
start: Number.parseInt(match[2] ?? "", 16),
|
|
end: Number.parseInt(match[3] ?? "", 16),
|
|
bytes: Number.parseInt(match[4] ?? "", 16),
|
|
description,
|
|
};
|
|
if (
|
|
section.end > artifactBytes ||
|
|
section.end - section.start !== section.bytes ||
|
|
section.start < 8 ||
|
|
section.start < (sections.at(-1)?.end ?? 8)
|
|
)
|
|
throw new Error("WABT section ranges do not match selected artifact.");
|
|
sections.push(section);
|
|
}
|
|
if (customIndex !== customSectionNames.length)
|
|
throw new Error(
|
|
"WABT custom-section inventory does not match decoded WAT.",
|
|
);
|
|
if ((sections.at(-1)?.end ?? 8) !== artifactBytes)
|
|
throw new Error(
|
|
"WABT section output does not cover the selected artifact.",
|
|
);
|
|
const separator = offset + 1;
|
|
return {
|
|
sections,
|
|
headers: text.slice(0, separator),
|
|
details: `${preamble.slice(1)}\n${text.slice(separator)}`,
|
|
};
|
|
};
|