fix(wasm): preserve debug module names in WABT output (#1967)

* fix(wasm): preserve debug module names in WABT output

* fix(native): accept otool dylib-use options

Preserve modern dylib options while requiring one encoding metadata row and retaining duplicate and multiline-output checks. Share field parsing between preservation and validation.

Verify delayed dylibs through real macOS CLI/MCP workflows and refresh the high-address golden with the provider's -h -l capture. Validated 85 focused native tests, npm run check, and verify:macos-bundle on macOS arm64.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Arthas
2026-10-12 05:44:15 +08:00
committed by GitHub
co-authored by morluto
parent 1d4c4eb030
commit b777e5d4d8
13 changed files with 262 additions and 56 deletions
+3 -3
View File
@@ -323,9 +323,8 @@ export class WabtArtifactProvider implements WasmArtifactPort {
OPERATION,
"Unexpected WABT decoding output.",
);
const { imports, exports, customSectionNames } = parseWabtWat(
wat.stdout.text,
);
const { imports, exports, customSectionNames, debugModuleNames } =
parseWabtWat(wat.stdout.text);
capturedOutput = {
stdout: dump.stdout.text,
stderr: dump.stderr.text,
@@ -335,6 +334,7 @@ export class WabtArtifactProvider implements WasmArtifactPort {
dump.stdout.text,
artifact.bytes,
customSectionNames,
debugModuleNames,
);
const limitations = [
"WAT is a decoded representation, not original source or proof of runtime behavior. Custom annotations may retain payloads, but WAT does not establish original binary layout or round-trip byte identity. Original bytes and objdump output remain authoritative.",
+24
View File
@@ -138,6 +138,28 @@ it("accepts an unpadded DataCount row and still rejects malformed output and bad
),
).toThrow(/header/u);
});
it.each([
{ displayed: [], decoded: ["named"] },
{ displayed: ["named"], decoded: [] },
{ displayed: ["other"], decoded: ["named"] },
])(
"rejects module-name headers inconsistent with decoded WAT: %j",
async ({ displayed, decoded }) => {
const text = (await golden()).replace(
"\n\nSections:",
`\n${displayed.map((name) => `module name: <${name}>\n`).join("")}\nSections:`,
);
expect(() => parseWabtOutput(text, 76, ["test"], decoded)).toThrow(
/header format/u,
);
},
);
it.each([
'(module (@custom "name" "\\00\\06\\05name"))',
'(module (@custom "name" "\\00\\05\\05name"))',
])("rejects incomplete decoded module-name metadata: %s", (text) => {
expect(() => parseWabtWat(text)).toThrow(/metadata/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),
@@ -147,6 +169,7 @@ it("extracts escaped WAT import/export forms while retaining multiline producer
imports: ['(import "env" "foo" (func (;0;) (type 0)))'],
exports: ['(export "memory" (memory 0))', '(export "run" (func 1))'],
customSectionNames: ["test"],
debugModuleNames: [],
});
expect(
parseWabtWat(
@@ -156,6 +179,7 @@ it("extracts escaped WAT import/export forms while retaining multiline producer
imports: ['(import "a.b" "c\\0aline" (func))'],
exports: ['(export "fake\\22Export[1]:" (func 0))'],
customSectionNames: [],
debugModuleNames: [],
});
expect(
parseWabtWat(
+14 -5
View File
@@ -3,12 +3,21 @@ export const parseWabtOutput = (
text: string,
artifactBytes: number,
customSectionNames: readonly string[],
debugModuleNames: readonly string[] = [],
) => {
const prefix =
/^\nmodule\.wasm:\tfile format wasm 0x1\n\nSections:\n\n/u.exec(text);
if (prefix === null)
// 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`;
})
.join("");
const prefix = `${preamble}\nSections:\n\n`;
if (!text.startsWith(prefix))
throw new Error("Unsupported WABT section header format.");
let offset = prefix[0].length;
let offset = prefix.length;
let customIndex = 0;
const sections = [];
while (!text.startsWith("\nSection Details:\n", offset)) {
@@ -61,6 +70,6 @@ export const parseWabtOutput = (
return {
sections,
headers: text.slice(0, separator),
details: `module.wasm:\tfile format wasm 0x1\n\n${text.slice(separator)}`,
details: `${preamble.slice(1)}\n${text.slice(separator)}`,
};
};
+62 -12
View File
@@ -1,8 +1,5 @@
/** Decode only a producer custom-section name to frame objdump's raw display text. */
const customSectionName = (form: string): string => {
const prefix = /^\(@custom\s+"/u.exec(form);
if (prefix === null) throw new Error("Unsupported WABT custom-section name.");
const start = prefix[0].length;
/** Decode a producer WAT string to frame objdump's raw display text. */
const watString = (form: string, start: number) => {
let end = start;
while (end < form.length && form[end] !== '"') {
if (form[end] === "\\") end++;
@@ -45,18 +42,53 @@ const customSectionName = (form: string): string => {
index += 2;
}
}
return new TextDecoder("utf-8", { fatal: true }).decode(
bytes.subarray(0, length),
);
return { bytes: bytes.subarray(0, length), end: end + 1 };
};
/** Extract exact top-level WABT WAT forms; no binary decoding or import/export name unescaping. */
/** Read only module-name metadata from WABT's preserved name-section payload. */
const moduleNames = (bytes: Uint8Array): string[] => {
const names: string[] = [];
let offset = 0;
const uint32 = (end: number): number => {
let value = 0;
for (let shift = 0; shift <= 28 && offset < end; shift += 7) {
const byte = bytes[offset++];
if (byte === undefined) break;
if (shift === 28 && byte > 15) break;
value += (byte & 127) * 2 ** shift;
if (byte < 128) return value;
}
throw new Error("Incomplete WABT name-section metadata.");
};
while (offset < bytes.length) {
const type = uint32(bytes.length);
const size = uint32(bytes.length);
const end = offset + size;
if (end > bytes.length)
throw new Error("Incomplete WABT name-section metadata.");
if (type === 0 && size !== 0) {
const length = uint32(end);
if (offset + length !== end)
throw new Error("Incomplete WABT module-name metadata.");
names.push(
new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
bytes.subarray(offset, end),
),
);
}
offset = end;
}
return names;
};
/** Extract exact top-level WABT WAT forms; no raw WASM decoding or import/export name unescaping. */
export const parseWabtWat = (text: string) => {
if (!/^\(module(?:\s|\))/u.test(text))
throw new Error("Unsupported WABT WAT module prefix.");
const imports: string[] = [];
const exports: string[] = [];
const customSectionNames: string[] = [];
const debugModuleNames: string[] = [];
let depth = 0;
let start = 0;
let quoted = false;
@@ -110,8 +142,26 @@ export const parseWabtWat = (text: string) => {
const form = text.slice(start, index + 1);
if (/^\(import\s/u.test(form)) imports.push(form);
else if (/^\(export\s/u.test(form)) exports.push(form);
else if (/^\(@custom\s/u.test(form))
customSectionNames.push(customSectionName(form));
else if (/^\(@custom\s/u.test(form)) {
const prefix = /^\(@custom\s+"/u.exec(form);
if (prefix === null)
throw new Error("Unsupported WABT custom-section name.");
const nameString = watString(form, prefix[0].length);
const name = new TextDecoder("utf-8", { fatal: true }).decode(
nameString.bytes,
);
customSectionNames.push(name);
if (name === "name") {
const payloadPrefix = /^\s+"/u.exec(form.slice(nameString.end));
if (payloadPrefix === null)
throw new Error("Unsupported WABT name-section payload.");
const payload = watString(
form,
nameString.end + payloadPrefix[0].length,
);
debugModuleNames.push(...moduleNames(payload.bytes));
}
}
}
if (--depth < 0) throw new Error("Unbalanced WABT WAT output.");
if (depth === 0) closed = true;
@@ -120,5 +170,5 @@ export const parseWabtWat = (text: string) => {
}
if (depth !== 0 || quoted || commentDepth !== 0)
throw new Error("Incomplete WABT WAT output.");
return { imports, exports, customSectionNames };
return { imports, exports, customSectionNames, debugModuleNames };
};