From 0f6bc14e32527aaccf162341ca123dcbc2d48992 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:02:37 +0800 Subject: [PATCH] refactor(providers): declare architecture admission exhaustively (#12166) --- src/domain/binaryTargetTypes.ts | 6 ---- src/ghidra/GhidraProvider.ts | 34 ++++++++++--------- src/hopper/HopperAnalysisProfile.ts | 52 ++++++++++++++--------------- src/ida/IdaProvider.ts | 40 ++++++++++++---------- src/kuna/KunaProvider.ts | 29 +++++++++++----- 5 files changed, 88 insertions(+), 73 deletions(-) diff --git a/src/domain/binaryTargetTypes.ts b/src/domain/binaryTargetTypes.ts index 9854dd761f..2f2f23180d 100644 --- a/src/domain/binaryTargetTypes.ts +++ b/src/domain/binaryTargetTypes.ts @@ -61,12 +61,6 @@ export const ELF_METADATA_ARCHITECTURES = [ export type ElfMetadataArchitecture = (typeof ELF_METADATA_ARCHITECTURES)[number]; -/** Families whose ISA and ABI need a separate ELF declaration to be admitted. */ -export const isElfMetadataArchitecture = ( - architecture: BinaryArchitecture, -): architecture is ElfMetadataArchitecture => - (ELF_METADATA_ARCHITECTURES as readonly string[]).includes(architecture); - /** Families that require a specialized target variant instead of generic headers. */ export const HEADER_ARCHITECTURE_EXCLUSIONS = [ ...ELF_METADATA_ARCHITECTURES, diff --git a/src/ghidra/GhidraProvider.ts b/src/ghidra/GhidraProvider.ts index d526f53564..a714e75601 100644 --- a/src/ghidra/GhidraProvider.ts +++ b/src/ghidra/GhidraProvider.ts @@ -50,6 +50,7 @@ import { } from "../domain/analysisProfile.js"; import { isMachOFormat, + BINARY_ARCHITECTURES, RAW_ONLY_ARCHITECTURES, type BinaryArchitecture, type BinaryTarget, @@ -100,24 +101,27 @@ import { } from "./GhidraAdmission.js"; import { errorMessage } from "../kernel/errorMessage.js"; -const SUPPORTED_ARCHITECTURES = new Set([ - "x86", - "x86_64", - "arm", - "arm64", - "mips", - "6502", - "riscv", - "powerpc", - "m68k", - "hexagon", - "xtensa", -]); +const GHIDRA_ARCHITECTURES = { + x86: true, + x86_64: true, + arm: true, + arm64: true, + arm64_32: false, + mips: true, + "6502": true, + riscv: true, + powerpc: true, + m68k: true, + hexagon: true, + xtensa: true, +} as const satisfies Record; /** Names every admitted family, so the refusal cannot drift from the set. */ const admittedArchitecturesText = (): string => { const rawOnly = new Set(RAW_ONLY_ARCHITECTURES); - const admitted = [...SUPPORTED_ARCHITECTURES]; + const admitted = BINARY_ARCHITECTURES.filter( + (family) => GHIDRA_ARCHITECTURES[family], + ); const detected = admitted.filter((family) => !rawOnly.has(family)); const raw = admitted.filter((family) => rawOnly.has(family)); return `Admitted families: ${detected.join(", ")}; raw-binary only: ${raw.join(", ")}.`; @@ -323,7 +327,7 @@ export class GhidraProvider implements AnalysisProviderCandidate { "Ghidra v1 cannot enforce the selected architecture when importing a universal Mach-O; analyze a thinned Mach-O slice instead.", diagnostics, }; - if (!SUPPORTED_ARCHITECTURES.has(target.architecture)) + if (!GHIDRA_ARCHITECTURES[target.architecture]) return { status: "unsupported", code: "architecture_unsupported", diff --git a/src/hopper/HopperAnalysisProfile.ts b/src/hopper/HopperAnalysisProfile.ts index fa9f935881..dd00fe3baa 100644 --- a/src/hopper/HopperAnalysisProfile.ts +++ b/src/hopper/HopperAnalysisProfile.ts @@ -26,11 +26,8 @@ import { createFunctionCoordinateContext } from "../domain/native/functionCoordi import { createAnalysisProfile } from "../domain/analysisProfile.js"; import { isMachOFormat, - isElfMetadataArchitecture, type BinaryArchitecture, type BinaryTarget, - type ElfMetadataArchitecture, - type HeaderArchitecture, } from "../domain/binaryTargetTypes.js"; import { AnalysisCapabilityUnavailableError, @@ -207,20 +204,34 @@ const resolveHopperAnalysisProfileInScope = async ( }); }; -/** Families with no verified Hopper lane, declined before any loader selection. */ -export type HopperDeclinedArchitecture = - | ElfMetadataArchitecture - | "hexagon" - | "xtensa" - | "arm64_32"; +// Raw-only families keep the existing format refusal before loader selection. +const HOPPER_ARCHITECTURE_FLAGS = { + x86: "--intel-32", + x86_64: "--intel-64", + arm: "--armv7", + arm64: "--aarch64", + arm64_32: null, + mips: null, + "6502": "raw-only", + riscv: null, + powerpc: null, + m68k: "raw-only", + hexagon: null, + xtensa: null, +} as const satisfies Record; + +type HopperDeclinedArchitecture = { + [ + Architecture in BinaryArchitecture + ]: (typeof HOPPER_ARCHITECTURE_FLAGS)[Architecture] extends null + ? Architecture + : never; +}[BinaryArchitecture]; export const isHopperDeclinedArchitecture = ( architecture: BinaryArchitecture, ): architecture is HopperDeclinedArchitecture => - isElfMetadataArchitecture(architecture) || - architecture === "hexagon" || - architecture === "xtensa" || - architecture === "arm64_32"; + HOPPER_ARCHITECTURE_FLAGS[architecture] === null; /** * Hopper states only what it owns: it does not admit the family. It routes the @@ -256,7 +267,7 @@ export const hopperLoaderArgsForTarget = ( hopperDeclinedArchitectureReason(architecture), ), ); - const flag = hopperArchitectureFlag(architecture); + const flag = HOPPER_ARCHITECTURE_FLAGS[architecture]; if (isMachOFormat(target.format)) { if (machoContainer === undefined) return err( @@ -286,19 +297,6 @@ export const hopperLoaderArgsForTarget = ( } }; -const hopperArchitectureFlag = (architecture: HeaderArchitecture): string => { - switch (architecture) { - case "x86": - return "--intel-32"; - case "x86_64": - return "--intel-64"; - case "arm": - return "--armv7"; - case "arm64": - return "--aarch64"; - } -}; - const readMachoMagic = ( path: string, resources: RetainedResourceScope, diff --git a/src/ida/IdaProvider.ts b/src/ida/IdaProvider.ts index 9570891f88..40c7f3b918 100644 --- a/src/ida/IdaProvider.ts +++ b/src/ida/IdaProvider.ts @@ -11,10 +11,9 @@ import { analysisProfilesEqual, } from "../domain/analysisProfile.js"; import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js"; -import { - isElfMetadataArchitecture, - type BinaryArchitecture, - type BinaryTarget, +import type { + BinaryArchitecture, + BinaryTarget, } from "../domain/binaryTargetTypes.js"; import { err, ok } from "../domain/result.js"; import { cancelledBy } from "../domain/analysisErrorCore.js"; @@ -59,16 +58,23 @@ import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import type { ProviderInspectionScope } from "../ports/ProviderInspectionScope.js"; import { errorMessage } from "../kernel/errorMessage.js"; -/** - * Families IDA admits today. Every other family is refused, including families - * that REA recognizes after this list was written. - */ -const IDA_ARCHITECTURES = new Set([ - "x86", - "x86_64", - "arm", - "arm64", -]); +const IDA_ARCHITECTURES = { + x86: "supported", + x86_64: "supported", + arm: "supported", + arm64: "supported", + arm64_32: "unsupported", + mips: "ghidra", + "6502": "unsupported", + riscv: "ghidra", + powerpc: "ghidra", + m68k: "unsupported", + hexagon: "ghidra", + xtensa: "unsupported", +} as const satisfies Record< + BinaryArchitecture, + "supported" | "unsupported" | "ghidra" +>; /** Bring-your-own IDA MCP candidate; discovery performs no provider startup. */ export class IdaProvider implements AnalysisProviderCandidate { @@ -288,14 +294,14 @@ export class IdaProvider implements AnalysisProviderCandidate { "IDA does not admit headerless raw binaries. Open the target with Ghidra and supply architecture and entry_point.", diagnostics, }; - if (!IDA_ARCHITECTURES.has(target.architecture)) + const lane = IDA_ARCHITECTURES[target.architecture]; + if (lane !== "supported") return { status: "unsupported", code: "architecture_unsupported", // Header-declared families that Ghidra admits keep the pointer there. reason: - isElfMetadataArchitecture(target.architecture) || - target.architecture === "hexagon" + lane === "ghidra" ? `REA's IDA adapter has no verified ${target.architecture} lane. Select a supported Ghidra profile instead.` : `REA's IDA adapter has no verified ${target.architecture} lane.`, diagnostics, diff --git a/src/kuna/KunaProvider.ts b/src/kuna/KunaProvider.ts index c8c867fd86..9394470964 100644 --- a/src/kuna/KunaProvider.ts +++ b/src/kuna/KunaProvider.ts @@ -1,3 +1,4 @@ +import type { BinaryArchitecture } from "../domain/binaryTargetTypes.js"; import { HOST_PLATFORM } from "../platform/HostPlatform.js"; import { KUNA_STRING_MIN_LENGTH } from "./KunaLimits.js"; @@ -50,12 +51,24 @@ import { KunaRunSlots } from "./KunaRunSlots.js"; import { KunaSessionClient } from "./KunaSessionClient.js"; import { kunaUnavailable } from "./KunaUnavailable.js"; -/** Architectures REA verified Kuna on: x86-64 and AArch64 ELF and Mach-O, plus ARM Thumb. */ -const VERIFIED_ARCHITECTURES: ReadonlySet = new Set([ - "x86_64", - "arm64", - "arm", -]); +const KUNA_ARCHITECTURE_LABELS = { + x86: null, + x86_64: "x86-64", + arm64: "AArch64", + arm: "ARM", + arm64_32: null, + mips: null, + "6502": null, + riscv: null, + powerpc: null, + m68k: null, + hexagon: null, + xtensa: null, +} as const satisfies Record; + +const kunaArchitectureLabels = new Intl.ListFormat("en-GB").format( + Object.values(KUNA_ARCHITECTURE_LABELS).filter((label) => label !== null), +); /** * Bring-your-own Kuna candidate. Discovery reads configuration and the @@ -160,11 +173,11 @@ export class KunaProvider implements AnalysisProviderCandidate { : `REA verified the Kuna provider on ELF and thin Mach-O images; ${target.format} is not admitted yet.`, diagnostics, }; - if (!VERIFIED_ARCHITECTURES.has(target.architecture)) + if (KUNA_ARCHITECTURE_LABELS[target.architecture] === null) return { status: "unsupported", code: "architecture_unsupported", - reason: `REA verified the Kuna provider on x86-64, AArch64 and ARM; ${target.architecture} is not admitted yet.`, + reason: `REA verified the Kuna provider on ${kunaArchitectureLabels}; ${target.architecture} is not admitted yet.`, diagnostics: { ...diagnostics, architecture: target.architecture }, }; return { status: "supported", code: null, reason: null, diagnostics };