Files
rea/src/ghidra/GhidraAnalysisProfile.ts
T
af88184eee feat(ghidra): configurable import language and analysis seeds (#1667)
* feat(ghidra): configurable import language and analysis seeds

REA_GHIDRA_LANGUAGE_ID and REA_GHIDRA_COMPILER_SPEC_ID set the import
language when the header cannot identify the compiler (for example,
Borland C++ PE images).

REA_GHIDRA_SEED_FILE seeds functions, code and labels before
auto-analysis, for code reached only through computed jumps or data.

Both are validated before launch, recorded in the analysis profile and
verified in the bridge handshake.

* fix(ghidra): parse language XML and preserve seed ordering

* test(ghidra): use explicit platform paths for seed launch checks

* fix(javascript): retain cleanup failure reason in error causes

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-10-11 09:57:38 +08:00

162 lines
5.9 KiB
TypeScript

import type {
AnalysisProfileResolution,
ProviderIdentity,
} from "../application/AnalysisProvider.js";
import { createAnalysisProfile } from "../domain/analysisProfile.js";
import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import {
AnalysisCancelledError,
AnalysisUnsupportedTargetError,
} from "../domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { type AnalysisError } from "../domain/analysisErrorBase.js";
import { err, ok, type Result } from "../domain/result.js";
import type { GhidraInstallationInspection } from "./GhidraInstallation.js";
import {
ghidraMipsProfileParameters,
ghidraMipsUnsupportedReason,
} from "./GhidraMipsProfile.js";
/** Resolve version-bound, deterministic semantics before Ghidra imports a target. */
export const resolveGhidraAnalysisProfile = (
target: BinaryTarget,
identity: ProviderIdentity,
installation: GhidraInstallationInspection,
signal?: AbortSignal,
languageOverride?: GhidraLanguageOverride,
): Promise<Result<AnalysisProfileResolution, AnalysisError>> => {
if (signal?.aborted === true)
return Promise.resolve(err(new AnalysisCancelledError("open_binary")));
if (target.kind !== "executable")
return Promise.resolve(ok({ profile: null }));
if (installation.status === "unavailable")
return Promise.resolve(
err(new ProviderAdapterError(identity.id, "resolve_analysis_profile")),
);
const mipsReason = ghidraMipsUnsupportedReason(target);
if (mipsReason !== null)
return Promise.resolve(
err(
new AnalysisUnsupportedTargetError(
"resolve_analysis_profile",
target.path,
mipsReason,
),
),
);
const provider = { ...identity, version: installation.providerVersion };
const dosMz = target.format === "dos-mz";
const dosCom = target.format === "dos-com";
const dos = dosMz || dosCom;
// DOS imports already force an admitted loader, language, and context.
if (dos && languageOverride !== undefined)
return Promise.resolve(
err(
new AnalysisUnsupportedTargetError(
"resolve_analysis_profile",
target.path,
"REA_GHIDRA_LANGUAGE_ID cannot override the admitted DOS real-mode import.",
),
),
);
return Promise.resolve(
ok({
profile: createAnalysisProfile(provider, {
target_kind: target.kind,
target_format: target.format,
...(target.format === "pe"
? {
executable_role: target.executableRole ?? null,
managed: target.managed ?? null,
}
: {}),
architecture: target.architecture ?? null,
available_architectures: [
...(target.availableArchitectures ?? []),
].sort(),
import_mode: "ephemeral-source-immutable",
annotation_policy: "atomic-function-entry-metadata-v1",
load_image_observations: "source-mappings-entry-context-v2",
function_body_evidence: "complete-inclusive-ranges-v1",
function_references: "complete-body-and-entry-reference-manager-v2",
string_inventory_evidence: "defined-data-coverage-v1",
location_resolution: "explicit-address-exact-entry-symbol-first-v3",
instruction_flow_evidence: "decoded-return-pcode-v1",
function_boundary_observations: "ghidra-terminal-call-limitations-v1",
process_launch:
installation.platform === "win32"
? "official-headless-script-v1"
: "inspected-jvm-launch-support-v1",
...(dos
? {
load_image_evidence: dosCom
? "independent-com-mapping-context-v1"
: "independent-mz-mapping-relocations-v1",
}
: {}),
jump_table_evidence: "typed-case-default-blocks-v1",
decompiler_jump_loads: true,
loader: dosMz
? "MzLoader"
: dosCom
? "BinaryLoader"
: "auto-from-header",
language_id: dos
? "x86:LE:16:Real Mode"
: (languageOverride?.languageId ?? "auto-from-header"),
compiler_spec_id: dos
? "default"
: languageOverride === undefined
? "auto-default"
: (languageOverride.compilerSpecId ?? "language-default"),
...(languageOverride === undefined
? {}
: { language_selection: "configured-v1" }),
...(dos
? {
load_segment: "0x1000",
address_coordinates: "linear-byte-offset",
}
: {}),
...(dosCom
? {
entry_offset: "0x100",
register_context: {
CS: "0x1000",
DS: "0x1000",
ES: "0x1000",
SS: "0x1000",
},
entry_seed: "external-entry-and-function-before-analysis-v1",
}
: {}),
analyzer_preset: "ghidra-default",
...ghidraMipsProfileParameters(target),
}),
}),
);
};
/** Read the import-language commitment back from a resolved profile. */
export const ghidraProfileLanguageOverride = (
parameters: Readonly<Record<string, unknown>>,
): GhidraLanguageOverride | undefined => {
if (parameters.language_selection !== "configured-v1") return undefined;
const languageId = parameters.language_id;
const compilerSpecId = parameters.compiler_spec_id;
if (typeof languageId !== "string" || typeof compilerSpecId !== "string")
return undefined;
return compilerSpecId === "language-default"
? { languageId }
: { languageId, compilerSpecId };
};
/** Whether a committed profile and the active configuration select the same language. */
export const sameGhidraLanguageOverride = (
left: GhidraLanguageOverride | undefined,
right: GhidraLanguageOverride | undefined,
): boolean =>
left?.languageId === right?.languageId &&
left?.compilerSpecId === right?.compilerSpecId;