refactor(wasm): interpret WABT section diagnostics once (#12285)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 11:36:52 +00:00
1 parent 9c25fb1e29
commit 4aeeb7e4fb
4 files changed
+145 -55

No files matched your search

+100 -2
View File
@@ -232,10 +232,9 @@ const assertCancellationDuringRootRelease = async (options: {
});
};
const createWabtArtifactFixture = async () => {
const createWabtArtifactFixture = async (bytes = Buffer.alloc(76, 1)) => {
const selectedRoot = await createTestTempDirectory("rea-wabt-seam-");
const path = join(selectedRoot, "selected.wasm");
const bytes = Buffer.alloc(76, 1);
await writeFile(path, bytes);
for (const tool of ["wasm-validate", "wasm-objdump", "wasm2wat"])
await writeFile(
@@ -250,6 +249,105 @@ const createWabtArtifactFixture = async () => {
return { selectedRoot, path, bytes, dump };
};
it.each([
{ label: "muted", diagnostic: "", tool: "wasm2wat", valid: true },
{
label: "corroborated",
diagnostic:
"000000d: error: invalid utf-8 encoding: section name\nmodule.wasm: warning: opaque producer detail \t\n",
tool: "wasm2wat",
valid: true,
},
{
label: "unmatched",
diagnostic: "000000c: error: invalid utf-8 encoding: section name\n",
tool: "wasm2wat",
valid: false,
},
{
label: "unrecognized",
diagnostic: "000000d: error: another diagnostic\n",
tool: "wasm-objdump",
valid: false,
},
])(
"preserves $label diagnostics for a byte-corroborated blank name",
async ({ diagnostic, tool, valid }) => {
const bytes = Buffer.from(
"0061736d01000000000a02fffe7061796c6f6164",
"hex",
);
const fixture = await createWabtArtifactFixture(bytes);
const dump =
"\nmodule.wasm:\tfile format wasm 0x1\n\nSections:\n\n" +
" Custom start=0x0000000a end=0x00000014 (size=0x0000000a) \n" +
"Section Details:\n";
const wat = "(module)\n";
const executed: string[] = [];
const provider = new WabtArtifactProvider(
{ REA_WABT_BIN_DIRECTORY: fixture.selectedRoot },
(input) => {
const version = input.arguments[0] === "--version";
const command = input.command.includes("wasm-objdump")
? "wasm-objdump"
: input.command.includes("wasm2wat")
? "wasm2wat"
: "wasm-validate";
if (!version) executed.push(command);
const stdout = version
? "1.0.42\n"
: command === "wasm-objdump"
? dump
: command === "wasm2wat"
? wat
: "";
const stderr = !version && command === tool ? diagnostic : "";
return spawnOwnedProviderProcess({
...input,
hostEnvironment: nodeHostEnvironment(input.hostEnvironment),
command: process.execPath,
arguments: [
"-e",
`process.stdout.write(${JSON.stringify(stdout)}); process.stderr.write(${JSON.stringify(stderr)});`,
],
});
},
);
try {
const result = await provider.inspect({
path: fixture.path,
glue_paths: [],
candidate_paths: [],
});
if (!valid) {
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisOutputError",
capturedOutput: {
stdout: tool === "wasm-objdump" ? dump : wat,
stderr: diagnostic,
},
},
});
expect(executed.at(-1)).toBe(tool);
return;
}
if (!result.ok) throw result.error;
expect(wasmArtifactSchema.parse(result.value.result)).toMatchObject({
validation: "valid",
debug_name_metadata: "unknown",
sections: [{ kind: "Custom", start: 10, end: 20, description: "" }],
custom_section_warnings:
diagnostic === "" ? [] : [{ tool, text: diagnostic }],
wat: { text: wat },
});
} finally {
await provider.close();
}
},
);
it.each(["stdout", "stderr"] as const)(
"rejects malformed UTF-8 in WABT %s while retaining literal replacement characters",
async (stream) => {
+42 -50
View File
@@ -88,17 +88,22 @@ type AcquiredGlue =
readonly references: [];
};
const OPERATION = "inspect_wasm_artifact";
const warningOutput = (text: string): readonly string[] | null => {
if (text === "") return [];
const lines = text.trimEnd().split("\n");
return lines.every(
(line) =>
/^\S+: warning: /u.test(line) ||
/^[\da-f]+: error: invalid utf-8 encoding: section name$/u.test(line),
)
? lines
: null;
/** Admit successful-command stderr and parse the offsets that require byte corroboration. */
const diagnosticNameEnds = (text: string): ReadonlySet<number> | null => {
const offsets = new Set<number>();
if (text === "") return offsets;
for (const line of text.trimEnd().split("\n")) {
if (/^\S+: warning: /u.test(line)) continue;
const offset =
/^([\da-f]+): error: invalid utf-8 encoding: section name$/u.exec(
line,
)?.[1];
if (offset === undefined) return null;
offsets.add(Number.parseInt(offset, 16));
}
return offsets;
};
/** Run caller-supplied WABT against private copies of exactly selected bytes. */
export class WabtArtifactProvider implements WasmArtifactPort {
readonly #resources = new ArtifactResourceScope();
@@ -572,26 +577,29 @@ export class WabtArtifactProvider implements WasmArtifactPort {
);
}
validationState = "valid";
const customSectionWarnings: {
const parsedDiagnostics: {
tool: (typeof commands)[number]["tool"];
text: string;
output: typeof validation;
nameEnds: ReadonlySet<number>;
}[] = [];
const optionalLimitations: string[] = [];
const unexpectedDiagnostics = (output: typeof validation) =>
new AnalysisOutputError(
OPERATION,
"Unexpected WABT diagnostic output.",
{
capturedOutput: capturedProviderOutput(output),
},
);
const retainWarnings = (
tool: (typeof commands)[number]["tool"],
output: typeof validation,
) => {
const warnings = warningOutput(output.stderr.text);
if (warnings === null)
throw new AnalysisOutputError(
OPERATION,
"Unexpected WABT diagnostic output.",
{
capturedOutput: capturedProviderOutput(output),
},
);
if (warnings.length > 0)
customSectionWarnings.push({ tool, text: output.stderr.text });
const text = output.stderr.text;
if (text === "") return;
const nameEnds = diagnosticNameEnds(text);
if (nameEnds === null) throw unexpectedDiagnostics(output);
parsedDiagnostics.push({ tool, output, nameEnds });
};
retainWarnings("wasm-validate", validation);
const dump = await execute(objdump, objdump.arguments);
@@ -623,33 +631,17 @@ export class WabtArtifactProvider implements WasmArtifactPort {
debugModuleNames,
selected.bytes,
);
for (const warning of customSectionWarnings) {
for (const line of warning.text.trimEnd().split("\n")) {
const error =
/^([\da-f]+): error: invalid utf-8 encoding: section name$/u.exec(
line,
);
if (
error !== null &&
!invalidNameEnds.includes(Number.parseInt(error[1] ?? "", 16))
)
throw new AnalysisOutputError(
OPERATION,
"Unexpected WABT diagnostic output.",
{
capturedOutput: capturedProviderOutput(
warning.tool === "wasm-validate"
? validation
: warning.tool === "wasm-objdump"
? dump
: wat,
),
},
);
}
}
for (const { output, nameEnds } of parsedDiagnostics)
for (const offset of nameEnds)
if (!invalidNameEnds.has(offset)) throw unexpectedDiagnostics(output);
const customSectionWarnings = parsedDiagnostics.map(
({ tool, output }) => ({
tool,
text: output.stderr.text,
}),
);
const debugNameMetadata =
invalidNameEnds.length > 0 ||
invalidNameEnds.size > 0 ||
(decodedNameMetadata === "complete" && customSectionWarnings.length > 0)
? "unknown"
: decodedNameMetadata;
@@ -665,7 +657,7 @@ export class WabtArtifactProvider implements WasmArtifactPort {
optionalLimitations.push(
"WABT reported optional custom-section interpretation warnings; exact warning output is retained in custom_section_warnings.",
);
if (invalidNameEnds.length > 0)
if (invalidNameEnds.size > 0)
optionalLimitations.push(
"An optional custom-section name is invalid UTF-8 and was omitted from decoded WAT. Its original bytes and objdump section range remain available; its identity and debug-name coverage are unknown.",
);
+1 -1
View File
@@ -24,7 +24,7 @@ it("corroborates a blank custom name with bounded invalid UTF-8 bytes", () => {
"",
].join("\n");
const parsed = parseWabtOutput(dump, bytes.length, [], [], bytes);
expect(parsed.invalidNameEnds).toEqual([13]);
expect(parsed.invalidNameEnds).toEqual(new Set([13]));
expect(parsed.sections).toMatchObject([{ kind: "Custom", description: "" }]);
const valid = Buffer.from(bytes);
valid[11] = 0x61;
+2 -2
View File
@@ -25,7 +25,7 @@ export const parseWabtOutput = (
let offset = prefix.length;
let customIndex = 0;
const sections = [];
const invalidNameEnds: number[] = [];
const invalidNameEnds = new Set<number>();
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(
@@ -43,7 +43,7 @@ export const parseWabtOutput = (
const invalidEnd = invalidCustomNameEnd(artifact, start, end);
if (invalidEnd === undefined)
throw new WabtAdmissionError("blank_custom_name");
invalidNameEnds.push(invalidEnd);
invalidNameEnds.add(invalidEnd);
description = "";
// WABT does not terminate a blank-name row when another section follows.
if (