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:
morluto
2026-10-12 08:59:14 +08:00
parent aada0ca812
commit b5ededd0f9
11 changed files with 593 additions and 134 deletions
+114 -10
View File
@@ -1,5 +1,12 @@
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { boundedUtf8Text } from "../process/boundedUtf8Text.js";
const MAX_CAUSE_DEPTH = 3;
const MAX_CAUSE_MESSAGE_BYTES = 2 * 1024;
const MAX_CAUSE_FIELD_BYTES = 1024;
const MAX_COMPILER_STDERR_BYTES = 8 * 1024;
/** Stable failure phase for one private Ghidra headless session. */
export type GhidraSessionFailureKind =
@@ -104,24 +111,121 @@ export const bindGhidraSessionFailure =
const describeCause = (
cause: unknown,
redact: (value: string) => string,
depth = 0,
): JsonValue => {
if (cause instanceof Error) {
const code = "code" in cause ? cause.code : undefined;
return describeErrorCause(cause, redact, depth);
}
if (typeof cause === "string") {
const message = boundedUtf8Text(redact(cause), MAX_CAUSE_MESSAGE_BYTES);
return {
name: redact(cause.name),
message: redact(cause.message),
...(typeof code === "string"
? { code: redact(code) }
: typeof code === "number" && Number.isFinite(code)
? { code }
: {}),
type: "string",
message: message.text,
...(message.truncated ? { message_truncated: true } : {}),
};
}
if (typeof cause === "string")
return { type: "string", message: redact(cause) };
if (cause === null || typeof cause === "boolean")
return { type: cause === null ? "null" : "boolean", value: cause };
if (typeof cause === "number" && Number.isFinite(cause))
return { type: "number", value: cause };
return { type: typeof cause };
};
const describeErrorCause = (
cause: Error,
redact: (value: string) => string,
depth: number,
): JsonValue => {
const message = boundedUtf8Text(
redact(cause.message),
MAX_CAUSE_MESSAGE_BYTES,
);
const name = boundedUtf8Text(redact(cause.name), MAX_CAUSE_FIELD_BYTES);
return {
name: name.text,
...(name.truncated ? { name_truncated: true } : {}),
message: message.text,
...(message.truncated ? { message_truncated: true } : {}),
...causeCode(cause, redact),
...causeSignal(cause, redact),
...causeCompilerOutcome(cause, redact),
...nestedErrorCause(cause, redact, depth),
};
};
const causeCode = (
cause: Error,
redact: (value: string) => string,
): Record<string, JsonValue> => {
const code = "code" in cause ? cause.code : undefined;
if (typeof code === "string") {
const bounded = boundedUtf8Text(redact(code), MAX_CAUSE_FIELD_BYTES);
return {
code: bounded.text,
...(bounded.truncated ? { code_truncated: true } : {}),
};
}
if (typeof code === "number" && Number.isFinite(code)) return { code };
return {};
};
const causeSignal = (
cause: Error,
redact: (value: string) => string,
): Record<string, JsonValue> => {
const signal = "signal" in cause ? cause.signal : undefined;
if (typeof signal === "string") {
const bounded = boundedUtf8Text(redact(signal), MAX_CAUSE_FIELD_BYTES);
return {
signal: bounded.text,
...(bounded.truncated ? { signal_truncated: true } : {}),
};
}
if (signal === null) return { signal: null };
return {};
};
const causeCompilerOutcome = (
cause: Error,
redact: (value: string) => string,
): Record<string, JsonValue> => {
if (!(cause instanceof DarwinProcessOwnershipInspectionError)) return {};
const outcome = cause.compilerOutcome;
if (outcome === undefined) return {};
const stderr = boundedUtf8Text(
redact(outcome.stderr),
MAX_COMPILER_STDERR_BYTES,
);
const code = boundedCompilerField(outcome.code, redact);
const signal = boundedCompilerField(outcome.signal, redact);
return {
compiler_outcome: {
...outcome,
code: code.value,
signal: signal.value,
stderr: stderr.text,
stderrTruncated: outcome.stderrTruncated || stderr.truncated,
...(code.truncated ? { codeTruncated: true } : {}),
...(signal.truncated ? { signalTruncated: true } : {}),
},
};
};
const boundedCompilerField = (
value: string | number | null,
redact: (value: string) => string,
): { readonly value: string | number | null; readonly truncated: boolean } => {
if (typeof value !== "string") return { value, truncated: false };
const bounded = boundedUtf8Text(redact(value), MAX_CAUSE_FIELD_BYTES);
return { value: bounded.text, truncated: bounded.truncated };
};
const nestedErrorCause = (
cause: Error,
redact: (value: string) => string,
depth: number,
): Record<string, JsonValue> => {
if (!("cause" in cause)) return {};
if (depth >= MAX_CAUSE_DEPTH) return { cause_truncated: true };
return { cause: describeCause(cause.cause, redact, depth + 1) };
};