Files
reverse-engineering-anything/src/composition/binary.ts
T
ShuaixinHuang aa01be1d0d perf(artifacts): isolate offline session composition from deep providers (#15194)
* perf(artifacts): isolate offline session composition from deep providers

* test(mcp): use the artifact session factory in bundle resource tests
2026-11-30 17:48:15 +08:00

140 lines
5.3 KiB
TypeScript

import type { SessionLimitationDelivery } from "../application/binary/SessionLimitationContext.js";
import type { AnalysisSettingsReader } from "../domain/analysisSettings.js";
import { runtimeAnalysisConfig } from "../config/runtimeAnalysisSettings.js";
import { isAppleDispatchMetadataTarget } from "../domain/native/objcSwiftMetadata.js";
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
import type { AppConfig } from "../config/types.js";
import type { BinarySession } from "../application/binary/BinarySession.js";
import { HopperProvider } from "../hopper/HopperProvider.js";
import { GhidraProvider } from "../ghidra/GhidraProvider.js";
import { createIdaMcpConnection } from "../ida/IdaMcpConnection.js";
import { IdaProvider } from "../ida/IdaProvider.js";
import { KunaProvider } from "../kuna/KunaProvider.js";
import type { Logger } from "pino";
import {
auxiliaryAnalysisProviderDeclarations,
managedStaticProviderDeclaration,
} from "./auxiliaryAnalysisProviders.js";
import { AnalysisProviderRegistry } from "../application/binary/AnalysisProviderRegistry.js";
import { composeBinarySession } from "../application/binary/BinarySessionComposition.js";
import { LINUX_HOPPER_LAUNCHER_VERIFICATION } from "../application/LinuxHopper.js";
import { LazyAnalysisProvider } from "../application/binary/LazyAnalysisProvider.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { createBundleResourceFactory } from "./bundleResources.js";
/**
* Compose the target-switching runtime shared directly by CLI and MCP adapters.
* This is the sole production wiring point, so both adapters share identical
* provider selection, profile, lifecycle, and Evidence semantics. Only the
* session's lifetime differs: a persistent session states shared provider
* limitations once and references them (`sessionLimitations: "reference"`),
* while a one-call session keeps them inline.
*/
export const createBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
settings?: AnalysisSettingsReader,
sessionLimitations?: SessionLimitationDelivery,
): BinarySession =>
composeDeepBinarySession(
runtimeAnalysisConfig(config, settings),
logger,
selectedEnvironment,
undefined,
settings,
sessionLimitations,
);
/** Prefer byte-backed dispatch inspection while retaining deep symbol fallback. */
export const createNativeDispatchBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
): BinarySession =>
composeDeepBinarySession(
runtimeAnalysisConfig(config),
logger,
selectedEnvironment,
"inspect_native_dispatch_metadata",
);
const composeDeepBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
inspection?: "inspect_native_dispatch_metadata",
settings?: AnalysisSettingsReader,
sessionLimitations?: SessionLimitationDelivery,
): BinarySession => {
const platform = HOST_PLATFORM.platform;
const environment = snapshotEnvironment(selectedEnvironment, platform);
const hopper = new HopperProvider(
config,
logger,
environment,
platform,
LINUX_HOPPER_LAUNCHER_VERIFICATION,
);
const ghidra = new GhidraProvider(config, logger, environment);
const ida = new IdaProvider(config, environment, (registration, selected) =>
createIdaMcpConnection(registration, selected, { platform }),
);
const kuna = new KunaProvider(environment, {
regexMatchingDeadline: config.regexMatchingDeadline,
regexWorkerHeapRequest: config.regexWorkerHeapRequest,
});
const auxiliaryDeclarations = auxiliaryAnalysisProviderDeclarations(
environment,
platform,
settings,
);
const nativeInspectionAvailable =
inspection !== undefined &&
auxiliaryDeclarations.some(({ capabilities }) =>
capabilities.some(
({ operation, available }) => operation === inspection && available,
),
);
return composeBinarySession(
new AnalysisProviderRegistry(
[hopper, ghidra, ida, kuna],
config.analysisProvider,
nativeInspectionAvailable ? isAppleDispatchMetadataTarget : undefined,
),
auxiliaryDeclarations.map(
(declaration) => new LazyAnalysisProvider(declaration),
),
createBundleResourceFactory(environment, platform, settings),
sessionLimitations,
);
};
/** Compose an execution-free managed session without native provider selection. */
export const createManagedBinarySession = (): BinarySession =>
composeBinarySession(new AnalysisProviderRegistry([]), [
new LazyAnalysisProvider(managedStaticProviderDeclaration()),
]);
/** Compose native inspection utilities without candidates for deep analysis. */
export const createNativeBinarySession = (
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): Promise<BinarySession> =>
import("../native/NativeMacOSProvider.js").then(({ NativeMacOSProvider }) =>
composeBinarySession(
new AnalysisProviderRegistry([]),
[
new NativeMacOSProvider(
snapshotEnvironment(selectedEnvironment, platform),
undefined,
platform,
),
],
createBundleResourceFactory(
snapshotEnvironment(selectedEnvironment, platform),
platform,
),
),
);