Repository navigation
Expand file tree
/
Copy pathbinary.ts
More file actions
182 lines (175 loc) · 6.39 KB
/
Copy pathbinary.ts
File metadata and controls
182 lines (175 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 { IdaProvider } from "../ida/IdaProvider.js";
import { KunaProvider } from "../kuna/KunaProvider.js";
import type { Logger } from "pino";
import { auxiliaryAnalysisProviderDeclarations } from "./auxiliaryAnalysisProviders.js";
import { AnalysisProviderRegistry } from "../application/binary/AnalysisProviderRegistry.js";
import { composeBinarySession } from "../application/binary/BinarySessionComposition.js";
import { LazyAnalysisProvider } from "../application/binary/LazyAnalysisProvider.js";
import { ManagedStaticProvider } from "../dotnet/ManagedStaticProvider.js";
import {
BUNDLE_ARTIFACT_RESOURCE_OPERATIONS,
BUNDLE_NATIVE_RESOURCE_OPERATIONS,
combineBundleResourceClients,
} from "../application/binary/BundleResourceSession.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.
*/
export const createBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
settings?: AnalysisSettingsReader,
): BinarySession =>
composeDeepBinarySession(
runtimeAnalysisConfig(config, settings),
logger,
selectedEnvironment,
undefined,
settings,
);
/** Prefer byte-backed dispatch inspection while retaining deep symbol fallback. */
export const createNativeDispatchBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
): BinarySession =>
composeDeepBinarySession(
config,
logger,
selectedEnvironment,
"inspect_native_dispatch_metadata",
);
const composeDeepBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
inspection?: "inspect_native_dispatch_metadata",
settings?: AnalysisSettingsReader,
): BinarySession => {
const environment = snapshotEnvironment(selectedEnvironment);
const hopper = new HopperProvider(config, logger, environment);
const ghidra = new GhidraProvider(config, logger, environment);
const ida = new IdaProvider(config, environment);
const kuna = new KunaProvider(environment, {
regexMatchingDeadline: config.regexMatchingDeadline,
regexWorkerHeapRequest: config.regexWorkerHeapRequest,
});
const auxiliaryDeclarations = auxiliaryAnalysisProviderDeclarations(
environment,
process.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, process.platform, settings),
);
};
const createBundleResourceFactory = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
settings?: AnalysisSettingsReader,
) => {
return {
createClient: async (
root: import("../application/AppBundleExecutable.js").ResolvedAppBundleResourceRoot,
) => {
const [{ ArtifactProvider }, { NativeMacOSProvider }] = await Promise.all(
[
import("../artifacts/ArtifactProvider.js"),
import("../native/NativeMacOSProvider.js"),
],
);
const artifact = new ArtifactProvider(environment, platform, settings);
const native = new NativeMacOSProvider(environment, undefined, platform);
const capabilities = [
...artifact
.capabilities()
.filter(({ operation }) =>
BUNDLE_ARTIFACT_RESOURCE_OPERATIONS.has(operation),
),
...native
.capabilities()
.filter(({ operation }) =>
BUNDLE_NATIVE_RESOURCE_OPERATIONS.has(operation),
),
];
return {
client: combineBundleResourceClients(
artifact.createBundleResourceClient(root),
native.createBundleResourceClient(root),
),
capabilities,
};
},
};
};
/** Compose an execution-free managed session without native provider selection. */
export const createManagedBinarySession = (): BinarySession =>
composeBinarySession(new AnalysisProviderRegistry([]), [
new ManagedStaticProvider(),
]);
/** Compose the artifact operations without candidates for deep analysis engines. */
export const createArtifactBinarySession = (
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform = process.platform,
): Promise<BinarySession> =>
import("../artifacts/ArtifactProvider.js").then(({ ArtifactProvider }) =>
composeBinarySession(
new AnalysisProviderRegistry([]),
[
new ArtifactProvider(
snapshotEnvironment(selectedEnvironment, platform),
platform,
),
],
createBundleResourceFactory(
snapshotEnvironment(selectedEnvironment, platform),
platform,
),
),
);
/** Compose native inspection utilities without candidates for deep analysis. */
export const createNativeBinarySession = (
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform = process.platform,
): Promise<BinarySession> =>
import("../native/NativeMacOSProvider.js").then(({ NativeMacOSProvider }) =>
composeBinarySession(
new AnalysisProviderRegistry([]),
[
new NativeMacOSProvider(
snapshotEnvironment(selectedEnvironment, platform),
undefined,
platform,
),
],
createBundleResourceFactory(
snapshotEnvironment(selectedEnvironment, platform),
platform,
),
),
);