fix(providers): retain native parsing and compiler failure diagnostics
Parse otool field occurrences once and reject valueless required fields while retaining empty dylib options. Preserve compiler exit outcomes and stderr through capture errors and nested Ghidra causes, with explicit UTF-8 truncation after redaction. Strengthen provider failure assertions and document the manual Ghidra 12.1.4 macOS x64 decompiler build using upstream instructions. Validation: focused producer tests, nine fake-compiler preparation and cancellation tests, temporary-directory hygiene, static and documentation checks. No real Swift or Ghidra compilation was run locally.
This commit is contained in:
+32
-33
@@ -61,6 +61,7 @@ const DYLIB_FIELDS = [
|
||||
];
|
||||
// otool prints options for dylib_use_command and a timestamp for dylib_command.
|
||||
const DYLIB_ENCODING_FIELDS = ["time stamp", "options"];
|
||||
const EMPTY_VALUE_FIELDS = new Set(["options", "segname", "sectname"]);
|
||||
|
||||
/**
|
||||
* otool emits LF, but a capture replayed from another host or toolchain can
|
||||
@@ -201,11 +202,12 @@ const parseLoadCommand = (block: string, state: ParsedLoadCommands): void => {
|
||||
if (!Number.isSafeInteger(index))
|
||||
throw new TypeError("otool load-command index is unsafe");
|
||||
const header = block.split(/\n\s*Section\n/u)[0] ?? block;
|
||||
const fields = parseFields(header);
|
||||
const parsed = parseFields(header);
|
||||
const fields = parsed.values;
|
||||
const kind = stringField(fields, "cmd");
|
||||
if (kind === null) throw new TypeError("otool load command is missing cmd");
|
||||
for (const key of scalarFields(kind)) requireUniqueField(header, key);
|
||||
if (DYLIB_COMMANDS.has(kind)) validateDylibCommandLines(header, fields);
|
||||
for (const key of scalarFields(kind)) requireUniqueField(parsed, key);
|
||||
if (DYLIB_COMMANDS.has(kind)) validateDylibCommandLines(parsed);
|
||||
state.commands.push({
|
||||
index,
|
||||
kind,
|
||||
@@ -218,23 +220,18 @@ const parseLoadCommand = (block: string, state: ParsedLoadCommands): void => {
|
||||
collectSegment(kind, block, fields, state);
|
||||
};
|
||||
|
||||
const validateDylibCommandLines = (
|
||||
block: string,
|
||||
fields: Readonly<Record<string, string | number | null>>,
|
||||
): void => {
|
||||
const validateDylibCommandLines = (parsed: ParsedFields): void => {
|
||||
const encodingFields = DYLIB_ENCODING_FIELDS.filter((key) =>
|
||||
Object.hasOwn(fields, key),
|
||||
Object.hasOwn(parsed.values, key),
|
||||
);
|
||||
if (encodingFields.length !== 1)
|
||||
throw new TypeError(
|
||||
"otool dylib command must contain timestamp or options",
|
||||
);
|
||||
for (const key of encodingFields) requireUniqueField(block, key);
|
||||
for (const key of encodingFields) requireUniqueField(parsed, key);
|
||||
const allowed = new Set([...DYLIB_FIELDS, ...DYLIB_ENCODING_FIELDS]);
|
||||
for (const rawLine of block.split("\n").slice(1)) {
|
||||
const line = rawLine.trim();
|
||||
if (line.length === 0) continue;
|
||||
const key = parseField(line)?.[0];
|
||||
for (const field of parsed.rows) {
|
||||
const key = field?.[0];
|
||||
if (key === undefined || !allowed.has(key))
|
||||
throw new TypeError(
|
||||
"otool dylib command contains ambiguous multiline text",
|
||||
@@ -343,10 +340,18 @@ const collectSegment = (
|
||||
state.segments.push(segment);
|
||||
};
|
||||
|
||||
const parseFields = (block: string): Record<string, string | number | null> => {
|
||||
interface ParsedFields {
|
||||
readonly values: Record<string, string | number | null>;
|
||||
readonly rows: readonly (readonly [string, string] | undefined)[];
|
||||
}
|
||||
|
||||
const parseFields = (block: string): ParsedFields => {
|
||||
const fields: Record<string, string | number | null> = {};
|
||||
const rows: Array<readonly [string, string] | undefined> = [];
|
||||
for (const rawLine of block.split(/\r?\n/u).slice(1)) {
|
||||
if (rawLine.trim().length === 0) continue;
|
||||
const field = parseField(rawLine);
|
||||
rows.push(field);
|
||||
if (field === undefined) continue;
|
||||
const [key, value] = field;
|
||||
fields[key] =
|
||||
@@ -354,32 +359,25 @@ const parseFields = (block: string): Record<string, string | number | null> => {
|
||||
? value
|
||||
: (numeric(value) ?? value);
|
||||
}
|
||||
return fields;
|
||||
return { values: fields, rows };
|
||||
};
|
||||
|
||||
const parseField = (rawLine: string): readonly [string, string] | undefined => {
|
||||
const line = rawLine.trim();
|
||||
// A dylib-use command with no flags still has an empty options row.
|
||||
// Segment and section names are fixed char[16] fields that may be all NUL;
|
||||
// MH_OBJECT files put their sections in a segment with an empty segname.
|
||||
if (line === "options" || line === "segname" || line === "sectname")
|
||||
return [line, ""];
|
||||
// Match time stamp as a whole without swallowing `name stamp plugin.dylib`.
|
||||
const match = /^((?:time stamp)|\S+(?:\s+version)?)\s+(.+)$/u.exec(line);
|
||||
return match?.[1] === undefined || match[2] === undefined
|
||||
const match = /^((?:time stamp)|\S+(?:\s+version)?)(?:\s+(.+))?$/u.exec(line);
|
||||
return match?.[1] === undefined
|
||||
? undefined
|
||||
: [match[1], match[2].trim()];
|
||||
: [match[1], match[2]?.trim() ?? ""];
|
||||
};
|
||||
|
||||
const requireUniqueField = (block: string, key: string): void => {
|
||||
const escaped = key.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
||||
const pattern = new RegExp(`^\\s*${escaped}(?:\\s|$)`, "mu");
|
||||
const lines = block.split("\n").filter((line) => pattern.test(line));
|
||||
if (lines.length !== 1)
|
||||
const requireUniqueField = (parsed: ParsedFields, key: string): void => {
|
||||
const matches = parsed.rows.filter((field) => field?.[0] === key);
|
||||
const field = matches[0];
|
||||
if (matches.length !== 1 || field === undefined)
|
||||
throw new TypeError(`otool field ${key} is missing or repeated`);
|
||||
// parseField owns which labeled lines carry a value; a bare required label
|
||||
// is truncated output, except the empty dylib-use options row it admits.
|
||||
if (parseField(lines[0] ?? "")?.[0] !== key)
|
||||
// Only options and fixed-width Mach-O names may legitimately be empty.
|
||||
if (field[1] === "" && !EMPTY_VALUE_FIELDS.has(key))
|
||||
throw new TypeError(`otool field ${key} has no value`);
|
||||
};
|
||||
|
||||
@@ -402,7 +400,8 @@ const parseSections = (block: string): ParsedSection[] =>
|
||||
.split(/(?=\n\s*Section\n)/u)
|
||||
.slice(1)
|
||||
.map((sectionBlock) => {
|
||||
const fields = parseFields(`Section${sectionBlock}`);
|
||||
const parsed = parseFields(`Section${sectionBlock}`);
|
||||
const fields = parsed.values;
|
||||
for (const key of [
|
||||
"sectname",
|
||||
"segname",
|
||||
@@ -412,7 +411,7 @@ const parseSections = (block: string): ParsedSection[] =>
|
||||
"align",
|
||||
"flags",
|
||||
])
|
||||
requireUniqueField(sectionBlock, key);
|
||||
requireUniqueField(parsed, key);
|
||||
return {
|
||||
segment: nonEmptyName(stringField(fields, "segname")),
|
||||
name: nonEmptyName(stringField(fields, "sectname")),
|
||||
|
||||
Reference in New Issue
Block a user