feat(native): add investigation features (#486)

* feat(native): add UI and dispatch investigation workflows

* feat(ghidra): support macOS host validation

* docs: update native investigation capabilities

* feat(artifacts): decode compiled Interface Builder archives

* feat(ghidra): expose bounded p-code value flow

* test(ghidra): split host and cross-format verification

* fix(verify): follow complete result contracts

* refactor: remove unused authorization and schema helpers

* refactor: remove unused evidence result alias

* fix(ghidra): report jump-table evidence precisely

* fix(verify): align browser scenario fixture schema

* fix(setup): compare parsed client config structurally

* test(ghidra): verify AArch64 jump-table mappings

* fix(ghidra): recover AArch64 byte jump tables

* test(artifacts): verify compiled interface builder archives

* feat(native): trace UI actions through handlers

* fix(tests): update native trace and shutdown fixtures

* fix(verification): diagnose browser scenario failures

* fix(browser): wrap scenario snapshots in capture state

* fix(native): bind and bound investigation workflows

* fix(artifacts): preserve bounded interface builder evidence

* fix(ghidra): enforce admitted hosts and preflight tools

* fix(native): preserve trace cancellation and bounds

* test(application): expect bound analysis target evidence

* fix(native): preserve investigation bounds and cancellation

* fix(application): preserve bundle and unresolved trace evidence

* style(test): format artifact subject fixture
This commit is contained in:
morluto
2026-09-30 00:41:05 +08:00
committed by GitHub
parent fa26affc86
commit ed933d1893
87 changed files with 6771 additions and 375 deletions
+55 -3
View File
@@ -12,6 +12,7 @@ import {
} from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { EvidenceSubjectTarget } from "../domain/evidence.js";
import type {
AnalysisClient,
AnalysisExecution,
@@ -302,7 +303,9 @@ export class BinarySession
unboundOperationError: (operation, route) =>
this.#providerRouter.unboundOperationError(operation, route),
lookupSnapshot: (target, profile, operation, parameters) =>
this.lookupSnapshot(target, profile, operation, parameters),
operation === "decode_interface_builder"
? undefined
: this.lookupSnapshot(target, profile, operation, parameters),
});
if (!prepared.ok) return prepared;
const { active, capability, profile, cacheable, cached } = prepared.value;
@@ -311,9 +314,18 @@ export class BinarySession
this.#calls.add(call);
try {
const result = await call;
const profiled = commitExecutionProfile(name, result, profile);
const profiled = bindExecutionTarget(
commitExecutionProfile(name, result, profile),
name,
active.target,
);
this.#observeRuntimeAvailability(name, profiled);
if (profiled.ok && cacheable && profile !== undefined)
if (
profiled.ok &&
cacheable &&
profile !== undefined &&
name !== "decode_interface_builder"
)
this.recordSnapshot({
target: active.target,
profile,
@@ -500,3 +512,43 @@ const commitExecutionProfile = (
analysisProfile: structuredClone(profile),
});
};
const bindExecutionTarget = (
result: Result<AnalysisExecution, AnalysisError>,
operation: AnalysisOperation,
target: BinaryTarget,
): Result<AnalysisExecution, AnalysisError> => {
if (!result.ok) return result;
const subject: EvidenceSubjectTarget = {
path: target.path,
sha256: target.sha256,
format:
target.format === "analysis-database"
? "analysis-database"
: target.format,
...(target.architecture === undefined
? {}
: { architecture: target.architecture }),
};
if (
result.value.subject !== null &&
result.value.subject.sha256 !== target.sha256
) {
if (!isArtifactInventorySubject(operation, result.value.subject, target))
return err(
new ProviderAdapterError(
result.value.provider.id,
`${operation}:subject`,
),
);
}
return ok({ ...result.value, subject: result.value.subject ?? subject });
};
const isArtifactInventorySubject = (
operation: AnalysisOperation,
subject: EvidenceSubjectTarget,
target: BinaryTarget,
): boolean =>
(operation === "inventory_artifact" || operation === "inspect_artifact") &&
subject.path === (target.sourcePath ?? target.path);
+6
View File
@@ -71,6 +71,12 @@ type AvailabilityContext = {
};
const ENHANCED_REQUIREMENTS: Readonly<Record<string, readonly string[]>> = {
trace_native_ui_action: [
"decode_interface_builder",
"list_names",
"procedure_callees",
],
inspect_native_dispatch_metadata: ["list_names"],
get_objc_classes: ["list_names"],
get_objc_protocols: ["list_names"],
batch_decompile: ["procedure_pseudo_code"],
@@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import { clientConfigurationValuesEqual } from "./ClientConfigurationDocument.js";
describe("client configuration comparison", () => {
it("ignores TOML parser object prototypes and key order", () => {
const parsedEnvironment = {
HOPPER_LAUNCHER_PATH: "/opt/rea/node",
};
const parsedRegistration = {
env: parsedEnvironment,
startup_timeout_sec: 30,
args: ["mcp"],
command: "/opt/rea/bin/rea",
};
Object.setPrototypeOf(parsedEnvironment, null);
Object.setPrototypeOf(parsedRegistration, null);
expect(
clientConfigurationValuesEqual(parsedRegistration, {
command: "/opt/rea/bin/rea",
args: ["mcp"],
startup_timeout_sec: 30,
env: { HOPPER_LAUNCHER_PATH: "/opt/rea/node" },
}),
).toBe(true);
});
it("still detects configuration value changes", () => {
expect(
clientConfigurationValuesEqual(
{ startup_timeout_sec: 30 },
{ startup_timeout_sec: 15 },
),
).toBe(false);
});
});
@@ -1,4 +1,5 @@
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
import { isDeepStrictEqual } from "node:util";
import { z } from "zod";
import type { SetupClient } from "./SupportedClients.js";
@@ -12,6 +13,27 @@ export interface ClientConfigurationDocument {
const objectSchema = z.record(z.string(), z.unknown());
/** Compare parsed configuration values without depending on parser prototypes. */
export const clientConfigurationValuesEqual = (
left: unknown,
right: unknown,
): boolean =>
isDeepStrictEqual(
normalizeConfigurationValue(left),
normalizeConfigurationValue(right),
);
const normalizeConfigurationValue = (value: unknown): unknown => {
if (Array.isArray(value)) return value.map(normalizeConfigurationValue);
if (typeof value !== "object" || value === null || value instanceof Date)
return value;
return Object.fromEntries(
Object.entries(value)
.sort(([left], [right]) => left.localeCompare(right))
.map(([key, nested]) => [key, normalizeConfigurationValue(nested)]),
);
};
/** Parse the client format and reject malformed root or server-table values. */
export const parseClientConfiguration = (
text: string,
+14 -4
View File
@@ -49,10 +49,12 @@ type DirectAnalysisTool =
| "read_function_instructions"
| "analyze_function"
| "inspect_native_api"
| "inspect_native_dispatch_metadata"
| "search_strings"
| "search_procedures"
| "xrefs"
| "trace_feature";
| "trace_feature"
| "trace_native_ui_action";
/**
* Open one binary, execute one tool, and always release provider resources.
@@ -220,6 +222,7 @@ const runAnalysis = async (
const evidenceProfile = analysisProfileForEvidence(session, tool);
const bindingProfile = session.analysisProfile();
if (
tool !== "trace_native_ui_action" &&
snapshot !== undefined &&
evidenceProfile !== undefined &&
bindingProfile !== undefined
@@ -243,7 +246,11 @@ const runAnalysis = async (
evidenceProfile,
});
if (evidence !== undefined) session.recordEvidence(evidence);
if (snapshotPath !== undefined && evidence !== undefined) {
if (
tool !== "trace_native_ui_action" &&
snapshotPath !== undefined &&
evidence !== undefined
) {
const snapshot = session.exportAnalysisSnapshot();
if (!snapshot.ok) return cliError(snapshot.error);
const written = await writeAnalysisSnapshot(
@@ -276,7 +283,9 @@ const executeAnalysisTool = async (input: {
tool === "binary_overview" ||
tool === "analyze_function" ||
tool === "inspect_native_api" ||
tool === "trace_feature"
tool === "inspect_native_dispatch_metadata" ||
tool === "trace_feature" ||
tool === "trace_native_ui_action"
) {
const result = await new EnhancedTools(session).execute(
tool,
@@ -398,7 +407,8 @@ const isWorkflowEvidenceTool = (
): boolean =>
tool === "binary_overview" ||
tool === "inspect_native_api" ||
tool === "trace_feature";
tool === "trace_feature" ||
tool === "trace_native_ui_action";
const fileExists = async (path: string): Promise<boolean> => {
try {
@@ -0,0 +1,503 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import {
createAnalysisExecution,
type AnalysisExecution,
type AnalysisOperation,
} from "./AnalysisProvider.js";
import type { AnalysisError } from "../domain/errors.js";
import { EnhancedTools } from "./EnhancedTools.js";
import {
nativeInvestigationGraphSchema,
nativeInvestigationTraceSchema,
} from "../domain/nativeInvestigationGraph.js";
import { err, ok } from "../domain/result.js";
import type { Result } from "../domain/result.js";
import { AnalysisCancelledError } from "../domain/errors.js";
const target = "a".repeat(64);
const provider = { id: "fixture", name: "Fixture", version: "1" };
const evidence = [
{
kind: "interface_builder_resource" as const,
description: "Fixture UI archive",
location: { address: null, file_offset: null },
artifact_path: null,
artifact_sha256: null,
},
];
const graph = {
target_sha256: target,
provider: {
id: provider.id,
version: provider.version,
tool_version: "fixture",
},
nodes: [
{
id: "button",
kind: "control" as const,
name: "Build",
location: null,
attributes: {
class_name: "UIButton",
interface_builder_object_id: "button",
},
evidence,
},
{
id: "action",
kind: "action" as const,
name: "buildTapped:",
location: null,
attributes: { selector: "buildTapped:" },
evidence,
},
{
id: "controller",
kind: "view_controller" as const,
name: "BuildViewController",
location: null,
attributes: { class_name: "BuildViewController" },
evidence,
},
{
id: "selector",
kind: "objc_selector" as const,
name: "buildTapped:",
location: null,
attributes: {},
evidence,
},
],
edges: [
{
id: "button-action",
from: "button",
to: "action",
relation: "target_action" as const,
resolution: "observed" as const,
evidence,
limitations: [],
},
{
id: "action-selector",
from: "action",
to: "selector",
relation: "target_action" as const,
resolution: "observed" as const,
evidence,
limitations: [],
},
{
id: "action-controller",
from: "action",
to: "controller",
relation: "target_action" as const,
resolution: "observed" as const,
evidence,
limitations: [],
},
],
coverage: [],
truncated: false,
};
const execution = (
operation: AnalysisOperation,
result: unknown,
sha256 = target,
) =>
ok(
createAnalysisExecution(result, provider, {
subject: {
sha256,
path: "/fixture.app",
format: "mach-o",
architecture: "arm64",
},
}),
);
const testAnalysis = (
overrides: Partial<
Record<
AnalysisOperation,
(
parameters: Readonly<Record<string, unknown>>,
) => Result<AnalysisExecution, AnalysisError>
>
> = {},
uiGraph: z.input<typeof nativeInvestigationGraphSchema> = graph,
) => ({
execute: async (
operation: AnalysisOperation,
parameters: Readonly<Record<string, unknown>>,
): Promise<Result<AnalysisExecution, AnalysisError>> => {
const override = overrides[operation];
if (override !== undefined) return override(parameters);
switch (operation) {
case "decode_interface_builder":
return execution(operation, {
target_sha256: target,
documents: [
{
relative_path: "Base.lproj/Main.nib",
archive_sha256: "b".repeat(64),
document_kind: "nib",
object_count: 4,
connection_count: 3,
hierarchy_complete: true,
},
],
graph: uiGraph,
limitations: [],
});
case "list_names":
return execution(operation, [
{ address: "0x1000", name: "-[BuildViewController buildTapped:]" },
]);
case "procedure_callees":
return execution(
operation,
parameters.procedure === "0x1000" ? ["0x2000"] : [],
);
default:
throw new Error(`Unexpected provider operation: ${operation}`);
}
},
});
describe("native UI action trace", () => {
it("builds the evidence path from the active app and native provider", async () => {
const tools = new EnhancedTools(testAnalysis());
const result = await tools.execute("trace_native_ui_action", {
action: "buildTapped:",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const trace = nativeInvestigationTraceSchema.parse(result.value);
expect(trace.start).toBe("action");
expect(trace.nodes.map(({ id }) => id)).toContain("native:function:0x1000");
expect(trace.nodes.map(({ id }) => id)).toContain("native:function:0x2000");
expect(trace.edges).toContainEqual(
expect.objectContaining({
relation: "objc_dispatch",
resolution: "inferred",
}),
);
expect(trace.edges).toContainEqual(
expect.objectContaining({
relation: "direct_call",
to: "native:function:0x2000",
}),
);
expect(trace.coverage).toContainEqual(
expect.objectContaining({
facet: "cross_function_value_flow",
status: "unsupported",
}),
);
});
});
describe("bounded native UI tracing", () => {
it("continues handler traversal from its own depth when another UI branch is deeper", async () => {
const deepGraph = {
...graph,
nodes: [
...graph.nodes,
{
id: "view",
kind: "view" as const,
name: "Container",
location: null,
attributes: {},
evidence,
},
{
id: "nested-view",
kind: "view" as const,
name: "Nested container",
location: null,
attributes: {},
evidence,
},
],
edges: [
...graph.edges,
{
id: "controller-view",
from: "controller",
to: "view",
relation: "contains" as const,
resolution: "observed" as const,
evidence,
limitations: [],
},
{
id: "view-nested-view",
from: "view",
to: "nested-view",
relation: "contains" as const,
resolution: "observed" as const,
evidence,
limitations: [],
},
],
};
const result = await new EnhancedTools(testAnalysis({}, deepGraph)).execute(
"trace_native_ui_action",
{ action: "buildTapped:", max_depth: 3 },
);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(
nativeInvestigationTraceSchema
.parse(result.value)
.nodes.map(({ id }) => id),
).toContain("native:function:0x2000");
});
it("returns a typed input error for malformed direct metadata requests", async () => {
const result = await new EnhancedTools(testAnalysis()).execute(
"inspect_native_dispatch_metadata",
{ max_records: 0 },
);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisInputError" },
});
});
it("binds dispatch metadata to the active target digest", async () => {
const result = await new EnhancedTools(testAnalysis()).execute(
"inspect_native_dispatch_metadata",
{ max_records: 10 },
);
expect(result).toMatchObject({
ok: true,
value: { target_sha256: target },
});
});
});
describe("native UI query outcomes", () => {
it("resolves a control object ID through its authored action edge", async () => {
const tools = new EnhancedTools(testAnalysis());
const result = await tools.execute("trace_native_ui_action", {
action: "button",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value).toMatchObject({ start: "action" });
});
it("rejects UI and native observations with different target identities", async () => {
const tools = new EnhancedTools(
testAnalysis({
list_names: () => execution("list_names", [], "c".repeat(64)),
}),
);
const result = await tools.execute("trace_native_ui_action", {
action: "buildTapped:",
});
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisOutputError",
operation: "decode_interface_builder",
},
});
});
it("returns candidates and a reason for an ambiguous selector", async () => {
const duplicateGraph = {
...graph,
nodes: [
...graph.nodes,
{
...graph.nodes[1],
id: "second-action",
},
],
};
const tools = new EnhancedTools(
testAnalysis({
decode_interface_builder: () =>
execution("decode_interface_builder", {
target_sha256: target,
documents: [],
graph: duplicateGraph,
limitations: [],
}),
}),
);
const result = await tools.execute("trace_native_ui_action", {
action: "buildTapped:",
});
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value).toMatchObject({
reason: "action_selector_matches_multiple_ui_connections",
nodes: [{ id: "action" }, { id: "second-action" }],
});
});
it("retains decoder truncation coverage when the action is absent", async () => {
const boundedGraph = {
...graph,
coverage: [
{
facet: "objects:Main.nib",
status: "partial" as const,
reason: "object_limit_reached",
examined: 2,
omitted: 1,
},
],
truncated: true,
};
const result = await new EnhancedTools(
testAnalysis({}, boundedGraph),
).execute("trace_native_ui_action", { action: "missing-selector" });
expect(result).toMatchObject({ ok: true, value: { truncated: true } });
if (!result.ok) return;
expect(result.value).toHaveProperty("coverage");
expect(result.value).toMatchObject({
coverage: expect.arrayContaining([
expect.objectContaining({
facet: "objects:Main.nib",
status: "partial",
reason: "object_limit_reached",
}),
]),
});
});
it("returns cancellation instead of a successful trace during callee expansion", async () => {
const result = await new EnhancedTools(
testAnalysis({
procedure_callees: () =>
err(new AnalysisCancelledError("procedure_callees")),
}),
).execute("trace_native_ui_action", { action: "buildTapped:" });
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError", operation: "procedure_callees" },
});
});
it("reports only callees left unprocessed by the edge limit", async () => {
const result = await new EnhancedTools(
testAnalysis({
procedure_callees: (parameters) =>
execution(
"procedure_callees",
parameters.procedure === "0x1000"
? ["0x2000", "0x2001", "0x2002"]
: [],
),
}),
).execute("trace_native_ui_action", {
action: "buildTapped:",
max_depth: 4,
max_edges: 5,
});
expect(result).toMatchObject({ ok: true });
if (!result.ok) return;
expect(result.value).toMatchObject({
coverage: expect.arrayContaining([
expect.objectContaining({
facet: "direct_native_calls",
status: "partial",
omitted: 1,
}),
]),
});
});
});
describe("native UI trace boundaries", () => {
it("retains unresolved dispatch evidence during callee expansion", async () => {
const result = await new EnhancedTools(
testAnalysis({ list_names: () => execution("list_names", []) }),
).execute("trace_native_ui_action", { action: "buildTapped:" });
expect(result).toMatchObject({ ok: true });
if (!result.ok) return;
expect(result.value).toMatchObject({
unresolved: [
expect.objectContaining({
relation: "objc_dispatch",
resolution: "unresolved",
}),
],
});
});
it("returns cancellation when archive decoding is cancelled", async () => {
const result = await new EnhancedTools(
testAnalysis({
decode_interface_builder: () =>
err(new AnalysisCancelledError("decode_interface_builder")),
}),
).execute("trace_native_ui_action", { action: "buildTapped:" });
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCancelledError",
operation: "decode_interface_builder",
},
});
});
it("matches handlers after the dispatch inventory's former default limit", async () => {
const names = Array.from({ length: 5_000 }, (_, index) => ({
address: `0x${(index + 1).toString(16)}`,
name: `unrelated_symbol_${index}`,
}));
names.push({
address: "0x2000",
name: "-[BuildViewController buildTapped:]",
});
const result = await new EnhancedTools(
testAnalysis({ list_names: () => execution("list_names", names) }),
).execute("trace_native_ui_action", { action: "buildTapped:" });
expect(result).toMatchObject({ ok: true });
if (!result.ok) return;
expect(result.value).toMatchObject({
nodes: expect.arrayContaining([
expect.objectContaining({ id: "native:function:0x2000" }),
]),
});
});
it("counts edges removed with nodes in bounded graph coverage", async () => {
const result = await new EnhancedTools(testAnalysis()).execute(
"trace_native_ui_action",
{ action: "missing-selector", max_nodes: 1 },
);
expect(result).toMatchObject({ ok: true });
if (!result.ok) return;
expect(result.value).toMatchObject({
coverage: expect.arrayContaining([
expect.objectContaining({
facet: "interface_builder_graph",
status: "partial",
omitted: 8,
}),
]),
});
});
});
+71
View File
@@ -27,6 +27,11 @@ import {
discoverObjcClasses,
discoverObjcProtocols,
} from "../domain/symbolAnalysis.js";
import {
inspectNativeDispatchMetadata,
nativeDispatchMetadataResultSchema,
} from "../domain/objcSwiftMetadata.js";
import { traceNativeUiAction } from "./NativeUiActionTrace.js";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import {
@@ -59,9 +64,25 @@ export class EnhancedTools {
name === "trace_call_path"
)
return this.#executeTracing(name, input, signal);
if (name === "trace_native_ui_action") {
const parsed =
enhancedInputSchemas.trace_native_ui_action.safeParse(input);
return parsed.success
? this.executeValidated({ name, input: parsed.data }, signal)
: invalidEnhancedInput(name, parsed.error);
}
if (name === "analyze_function" || name === "inspect_native_api")
return this.#executeFunctionAnalysis(name, input, signal);
switch (name) {
case "inspect_native_dispatch_metadata": {
const parsed =
enhancedInputSchemas.inspect_native_dispatch_metadata.safeParse(
input,
);
return parsed.success
? this.executeValidated({ name, input: parsed.data }, signal)
: invalidEnhancedInput(name, parsed.error);
}
case "get_objc_classes": {
const parsed = enhancedInputSchemas.get_objc_classes.safeParse(input);
return parsed.success
@@ -110,6 +131,11 @@ export class EnhancedTools {
signal?: AbortSignal,
): EnhancedResult {
switch (call.name) {
case "inspect_native_dispatch_metadata":
return this.#inspectNativeDispatchMetadata(
call.input.max_records,
signal,
);
case "get_objc_classes":
return this.#objcClasses(call.input.pattern, signal);
case "get_objc_protocols":
@@ -151,6 +177,9 @@ export class EnhancedTools {
call.input,
signal,
);
case "trace_native_ui_action": {
return traceNativeUiAction(this.analysis, call.input, signal);
}
}
}
@@ -217,6 +246,48 @@ export class EnhancedTools {
return names.ok ? ok(discoverObjcClasses(names.value, pattern)) : names;
}
async #inspectNativeDispatchMetadata(
maxRecords: number,
signal?: AbortSignal,
): EnhancedResult {
const execution = await this.analysis.execute(
"list_names",
{},
signal === undefined ? {} : { signal },
);
if (!execution.ok) return err(execution.error);
const names = z
.array(
z.union([
z.strictObject({ address: z.string(), value: z.string() }),
z.strictObject({ address: z.string(), name: z.string() }),
]),
)
.safeParse(execution.value.result);
if (!names.success)
return err(
new AnalysisOutputError(
"list_names",
"provider returned an invalid inventory",
),
);
return ok(
nativeDispatchMetadataResultSchema.parse({
target_sha256: execution.value.subject?.sha256 ?? null,
provider: execution.value.provider,
analysis_profile_digest:
execution.value.analysisProfile?.digest ?? null,
result: inspectNativeDispatchMetadata(
names.data.map((entry) => ({
address: entry.address,
name: "value" in entry ? entry.value : entry.name,
})),
maxRecords,
),
}),
);
}
async #objcProtocols(signal?: AbortSignal): EnhancedResult {
const names = await this.#allAddressed("list_names", signal);
return names.ok ? ok(discoverObjcProtocols(names.value)) : names;
@@ -0,0 +1,189 @@
import { existsSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { buildBinary } from "plist";
import { afterEach, describe, expect, it } from "vitest";
import { analyzeInterfaceBuilderBundle } from "./InterfaceBuilderAnalysis.js";
const roots: string[] = [];
afterEach(async () => {
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
describe("compiled Interface Builder bundle reader", () => {
it("reads nib plist archives without following symlinks and reports provenance", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-ib-test-"));
roots.push(root);
const bundle = join(root, "Example.app");
const nib = join(
bundle,
"Contents",
"Resources",
"Main.storyboardc",
"Main.nib",
);
await mkdir(nib, { recursive: true });
await writeFile(
join(nib, "objects.nib"),
buildBinary({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: [
"$null",
{ $class: { UID: 2 }, title: "Build" },
{
$classname: "UIButton",
$classes: ["UIButton", "UIControl", "UIView", "NSObject"],
},
],
$top: { root: { UID: 1 } },
}),
);
await writeFile(
join(bundle, "Contents", "Resources", "Main.storyboardc", "Info.plist"),
'<?xml version="1.0"?><plist><dict><key>notAnArchive</key><string>scene-index</string></dict></plist>',
);
await mkdir(join(bundle, "Contents", "Resources", "outside.nib"), {
recursive: true,
});
await writeFile(
join(bundle, "Contents", "Resources", "outside.nib", "not-nib.txt"),
"ignored",
);
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "b".repeat(64),
});
expect(analysis.documents).toMatchObject([
{
relative_path:
"Contents/Resources/Main.storyboardc/Main.nib/objects.nib",
document_kind: "storyboard_scene",
object_count: 1,
},
]);
expect(analysis.documents).toHaveLength(1);
expect(analysis.graph.target_sha256).toBe("b".repeat(64));
expect(analysis.graph.nodes.some(({ name }) => name === "Build")).toBe(
true,
);
});
it("honors cancellation during directory traversal", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-ib-test-"));
roots.push(root);
const controller = new AbortController();
controller.abort();
await expect(
analyzeInterfaceBuilderBundle({
bundlePath: root,
targetSha256: "c".repeat(64),
signal: controller.signal,
}),
).rejects.toMatchObject({ reason: "cancelled" });
});
});
describe("bounded Interface Builder archive decoding", () => {
it("counts malformed archives against the document limit", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-ib-test-"));
roots.push(root);
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
await writeFile(join(resources, "BadOne.nib"), Buffer.from("bplist00bad"));
await writeFile(join(resources, "BadTwo.nib"), Buffer.from("bplist00bad"));
const result = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "e".repeat(64),
limits: { max_documents: 1 },
});
expect(result.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "archive_decode",
status: "partial",
examined: 1,
omitted: 1,
}),
);
expect(result.graph.truncated).toBe(true);
});
it.skipIf(process.platform !== "darwin" || !existsSync("/usr/bin/ibtool"))(
"decodes an Xcode-compiled storyboard NIB and recovers its UI routes",
async () => {
const root = await mkdtemp(join(tmpdir(), "rea-ib-compiled-test-"));
roots.push(root);
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
const source = join(
process.cwd(),
"tests",
"fixtures",
"interface-builder",
"MacFixture.storyboard",
);
await mkdir(resources, { recursive: true });
execFileSync("/usr/bin/ibtool", [
"--compile",
join(resources, "MacFixture.storyboardc"),
source,
]);
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "d".repeat(64),
});
const names = analysis.graph.nodes.map(({ name }) => name);
expect(names).toContain("BuildViewController");
expect(names).toContain("Button");
expect(names).toContain("buildTapped:");
expect(
analysis.graph.edges.some(
({ relation }) => relation === "target_action",
),
).toBe(true);
const action = analysis.graph.nodes.find(
({ kind, name }) => kind === "action" && name === "buildTapped:",
);
expect(action).toBeDefined();
const actionSource = analysis.graph.edges.find(
({ from, relation, to }) =>
relation === "target_action" &&
to === action?.id &&
analysis.graph.nodes.find(({ id }) => id === from)?.kind ===
"control",
);
expect(actionSource).toBeDefined();
expect(
analysis.graph.edges.some(
({ from, relation, to }) =>
from === action?.id &&
relation === "target_action" &&
to !== null &&
analysis.graph.nodes.find(({ id }) => id === to)?.kind ===
"placeholder",
),
).toBe(true);
expect(
analysis.graph.edges.some(({ relation }) => relation === "contains"),
).toBe(true);
expect(analysis.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "archive_decode",
status: "complete",
}),
);
},
);
});
+306
View File
@@ -0,0 +1,306 @@
import { createHash } from "node:crypto";
import { parse, parseBinary } from "plist";
import { DirectoryArtifactReader } from "../artifacts/DirectoryArtifactReader.js";
import {
decodeNibArchive,
type NibArchiveDocument,
} from "../artifacts/NibArchive.js";
import type { ArtifactEntry } from "../artifacts/ArtifactReader.js";
import {
buildInterfaceBuilderAnalysis,
interfaceBuilderLimitsSchema,
type InterfaceBuilderDocumentInput,
} from "../domain/interfaceBuilderGraph.js";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
const MAX_DOCUMENT_BYTES = 64 * 1024 * 1024;
/** Decode compiled Interface Builder archives from a local app bundle. */
export const analyzeInterfaceBuilderBundle = async (input: {
readonly bundlePath: string;
readonly targetSha256: string;
readonly limits?: unknown;
readonly signal?: AbortSignal;
}) => {
const limits = interfaceBuilderLimitsSchema.parse(input.limits ?? {});
const reader = new DirectoryArtifactReader(input.bundlePath);
const documents: InterfaceBuilderDocumentInput[] = [];
const invalid: string[] = [];
let omitted = 0;
let attempted = 0;
try {
for await (const entry of reader.entries(input.signal)) {
if (entry.kind !== "file" || !isInterfaceBuilderArchive(entry.path))
continue;
if (attempted >= limits.max_documents) {
omitted += 1;
continue;
}
attempted += 1;
try {
const bytes = await readEntry(reader, entry, input.signal);
const raw =
bytes.subarray(0, 10).toString("ascii") === "NIBArchive"
? projectNibArchive(decodeNibArchive(bytes))
: decodePlist(bytes);
const documentHash = createHash("sha256").update(bytes).digest("hex");
documents.push({
relativePath: entry.path,
archiveSha256: documentHash,
documentKind: entry.path.includes(".storyboardc/")
? "storyboard_scene"
: "nib",
raw,
});
} catch (cause: unknown) {
if (input.signal?.aborted === true) throw cause;
invalid.push(
`${entry.path}: ${cause instanceof Error ? cause.message : "archive decode failed"}`,
);
}
}
} finally {
await reader.close();
}
const result = buildInterfaceBuilderAnalysis({
targetSha256: input.targetSha256,
toolVersion: "rea-interface-builder/1",
documents,
limits,
});
return {
...result,
graph: {
...result.graph,
coverage: [
...result.graph.coverage,
{
facet: "archive_decode",
status:
invalid.length > 0 ? ("partial" as const) : ("complete" as const),
reason: invalid.length > 0 ? "one_or_more_archives_invalid" : null,
examined: attempted,
omitted,
},
],
truncated: result.graph.truncated || omitted > 0 || invalid.length > 0,
},
limitations: [
...result.limitations,
"Compiled Interface Builder archives are private serialized object graphs. The decoder reports only recognized keyed-archive objects and connections; unrecognized object classes and fields remain unknown.",
...(invalid.length === 0
? []
: [
`Some Interface Builder archives could not be decoded: ${invalid.slice(0, 16).join("; ")}`,
]),
],
};
};
/** Project decoded NIB records into the same bounded graph input as ibtool. */
const projectNibArchive = (archive: NibArchiveDocument): JsonValue => {
const byId = new Map(archive.objects.map((object) => [object.id, object]));
const dereference = (value: JsonValue | undefined): JsonValue | undefined => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return value;
const ref = value.$nib_object_ref;
if (typeof ref !== "number") return value;
const target = byId.get(ref);
if (target === undefined) return undefined;
if (target.class_name.replace(/\0+$/u, "") === "NSString") {
const data = target.values["NS.bytes"];
if (typeof data === "object" && data !== null && !Array.isArray(data)) {
const encoded = data.$nib_data_base64;
if (typeof encoded === "string")
return Buffer.from(encoded, "base64").toString("utf8");
}
}
return { objectID: String(ref) };
};
const referencedString = (value: JsonValue | undefined): string | null => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return null;
const ref = value.$nib_object_ref;
if (typeof ref !== "number") return null;
const target = byId.get(ref);
const data = target?.values["NS.bytes"];
if (target?.class_name.replace(/\0+$/u, "") !== "NSString") return null;
if (typeof data !== "object" || data === null || Array.isArray(data))
return null;
const encoded = data.$nib_data_base64;
return typeof encoded === "string"
? Buffer.from(encoded, "base64").toString("utf8")
: null;
};
const runtimeClass = (objectId: number): string | null => {
const object = byId.get(objectId);
if (object === undefined) return null;
const storedClass = object.class_name.replace(/\0+$/u, "");
return storedClass === "NSClassSwapper"
? (referencedString(object.values.NSClassName) ?? storedClass)
: storedClass;
};
const objects = Object.fromEntries(
archive.objects
.filter((object) => {
const name = runtimeClass(object.id) ?? "";
return !/^(?:NSObject|NSIBObjectData|NSString|NSMutableString|NSNumber|NSArray|NSMutableArray|NSSet|NSMutableSet|NSDictionary|NSMutableDictionary|NSApplication|NSNib.*Connector)$/u.test(
name,
);
})
.map((object) => {
const className =
runtimeClass(object.id) ?? object.class_name.replace(/\0+$/u, "");
const strings = Object.entries(object.values).flatMap(
([key, value]) => {
const resolved = dereference(value);
return typeof resolved === "string" ? [[key, resolved]] : [];
},
);
const name =
strings.find(([key]) =>
/(?:title|label|identifier|accessibility|name|contents)/iu.test(
key ?? "",
),
)?.[1] ?? className;
return [
String(object.id),
{
customClass: className,
label: name,
objectID: String(object.id),
nibValues: Object.fromEntries(
Object.entries(object.values).map(([key, value]) => [
key,
dereference(value) ?? null,
]),
),
},
];
}),
);
const connections: Record<string, JsonValue[]> = {};
const hierarchy: JsonValue[] = [];
const reference = (value: JsonValue | undefined): number | null => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return null;
return typeof value.$nib_object_ref === "number"
? value.$nib_object_ref
: null;
};
for (const object of archive.objects) {
const className = object.class_name.replace(/\0+$/u, "");
const source = reference(object.values.NSSource);
const destination = reference(object.values.NSDestination);
if (className.includes("Connector") && source !== null) {
const label = referencedString(object.values.NSLabel);
const type = className.includes("Outlet")
? "outlet"
: className.includes("Control")
? "action"
: className;
const controlAction = className.includes("Control");
const connectionSource = controlAction ? destination : source;
const connectionDestination = controlAction ? source : destination;
if (connectionSource === null) continue;
(connections[String(connectionSource)] ??= []).push({
type,
"destination-id":
connectionDestination === null ? null : String(connectionDestination),
label,
source_id: String(connectionSource),
archive_object_id: String(object.id),
});
}
}
const hierarchyFor = (
objectId: number,
seen: Set<number>,
depth: number,
): JsonValue | null => {
if (depth > 32 || seen.has(objectId)) return null;
const object = byId.get(objectId);
if (object === undefined) return null;
const nextSeen = new Set(seen).add(objectId);
const children: JsonValue[] = [];
for (const [key, value] of Object.entries(object.values)) {
if (!/(?:subviews|contentview|childviewcontrollers|view)$/iu.test(key))
continue;
const direct = reference(value);
if (direct === null) continue;
const target = byId.get(direct);
if (target === undefined) continue;
if (/array|set/iu.test(target.class_name)) {
for (const candidate of Object.values(target.values)) {
const child = reference(candidate);
const nested =
child === null ? null : hierarchyFor(child, nextSeen, depth + 1);
if (nested !== null) children.push(nested);
}
} else {
const nested = hierarchyFor(direct, nextSeen, depth + 1);
if (nested !== null) children.push(nested);
}
}
return { objectID: String(objectId), children };
};
const ibData = archive.objects.find(
({ class_name }) => class_name.replace(/\0+$/u, "") === "NSIBObjectData",
);
const rootId = reference(ibData?.values.NSRoot);
const hierarchyRoots = rootId === null ? [] : [rootId];
for (const hierarchyRoot of hierarchyRoots) {
const root = hierarchyFor(hierarchyRoot, new Set(), 0);
if (root !== null) hierarchy.push(root);
}
return jsonValueSchema.parse({
"com.apple.ibtool.document.objects": objects,
"com.apple.ibtool.document.connections": connections,
"com.apple.ibtool.document.hierarchy": hierarchy,
"com.apple.ibtool.document.classes": Object.fromEntries(
archive.classes.map((name) => [name.replace(/\0+$/u, ""), {}]),
),
});
};
const isInterfaceBuilderArchive = (path: string): boolean => {
const lower = path.toLowerCase();
return lower.endsWith(".nib") || lower.endsWith("/objects.nib");
};
const readEntry = async (
reader: DirectoryArtifactReader,
entry: ArtifactEntry,
signal?: AbortSignal,
): Promise<Buffer> => {
if (entry.declaredSize !== null && entry.declaredSize > MAX_DOCUMENT_BYTES)
throw new RangeError(
"archive exceeds the 64 MiB per-document decode limit",
);
const stream = await reader.open(entry, signal);
const chunks: Buffer[] = [];
let length = 0;
for await (const chunk of stream) {
const bytes = Buffer.isBuffer(chunk)
? chunk
: Buffer.from(chunk as Uint8Array);
length += bytes.length;
if (length > MAX_DOCUMENT_BYTES) {
stream.destroy();
throw new RangeError(
"archive exceeded the 64 MiB per-document decode limit",
);
}
chunks.push(bytes);
}
return Buffer.concat(chunks, length);
};
const decodePlist = (bytes: Buffer): JsonValue => {
const parsed =
bytes.subarray(0, 8).toString("ascii") === "bplist00"
? parseBinary(bytes)
: parse(bytes.toString("utf8"));
return jsonValueSchema.parse(parsed);
};
+22 -1
View File
@@ -21,7 +21,6 @@ describe("native API inspection", () => {
mappings: [
{
target_address: "0x401020",
data_addresses: ["0x403000"],
},
],
},
@@ -60,6 +59,28 @@ describe("native API inspection", () => {
});
});
it("reports missing jump-table targets as an explicit unknown", () => {
const value = ghidraFunctionDossier();
const dossier = functionDossierSchema.parse(value);
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
if (table === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
const incomplete = functionDossierSchema.parse({
...dossier,
native_api: {
...boundary,
jump_tables: [{ ...table, mappings: [] }],
},
});
expect(projectNativeApiInspection(incomplete).residual_unknowns).toContain(
"Which case values and targets belong to the dispatch at 0x401010?",
);
});
it("rejects boundary types placed in the wrong ABI role", () => {
const boundary = functionDossierSchema.parse(
ghidraFunctionDossier(),
+4
View File
@@ -72,6 +72,10 @@ const availableResidualUnknowns = (
): readonly string[] => {
const unknowns: string[] = [];
for (const table of boundary.jump_tables) {
if (table.mappings.length === 0)
unknowns.push(
`Which case values and targets belong to the dispatch at ${table.dispatch_address}?`,
);
if (table.data_sources.length === 0)
unknowns.push(
`What backing data address produced the jump table dispatched at ${table.dispatch_address}?`,
+532
View File
@@ -0,0 +1,532 @@
import { z } from "zod";
import type { AnalysisOperationPort } from "./AnalysisProvider.js";
import type { EnhancedResult } from "./EnhancedToolTypes.js";
import type { enhancedInputSchemas } from "../contracts/enhancedInputs.js";
import { interfaceBuilderAnalysisSchema } from "../domain/interfaceBuilderGraph.js";
import {
nativeInvestigationGraphSchema,
nativeInvestigationTraceSchema,
traceNativeInvestigationGraph,
joinInterfaceBuilderDispatch,
} from "../domain/nativeInvestigationGraph.js";
import {
inspectNativeDispatchMetadata,
nativeDispatchMetadataResultSchema,
} from "../domain/objcSwiftMetadata.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { parseRelatedAddresses } from "../domain/hopperValues.js";
import { err, ok } from "../domain/result.js";
import {
AnalysisCancelledError,
AnalysisOutputError,
projectAnalysisError,
} from "../domain/errors.js";
type Input = z.output<typeof enhancedInputSchemas.trace_native_ui_action>;
const nativeNameSchema = z.array(
z.union([
z.strictObject({ address: z.string(), name: z.string() }),
z.strictObject({ address: z.string(), value: z.string() }),
]),
);
/** Build a UI-to-code trace from the active app and selected native provider. */
export const traceNativeUiAction = async (
analysis: AnalysisOperationPort,
input: Input,
signal?: AbortSignal,
): EnhancedResult => {
const [uiExecution, namesExecution] = await Promise.all([
analysis.execute(
"decode_interface_builder",
{
max_documents: 64,
max_objects: input.max_nodes,
max_connections: input.max_edges,
},
signal === undefined ? {} : { signal },
),
analysis.execute("list_names", {}, signal === undefined ? {} : { signal }),
]);
if (!uiExecution.ok && uiExecution.error._tag === "AnalysisCancelledError")
return err(new AnalysisCancelledError("decode_interface_builder"));
if (!namesExecution.ok) return err(namesExecution.error);
const parsedNames = nativeNameSchema.safeParse(namesExecution.value.result);
if (!parsedNames.success)
return err(
new AnalysisOutputError(
"list_names",
"provider returned an invalid name inventory",
),
);
const targetSha256 = namesExecution.value.subject?.sha256;
if (targetSha256 === undefined)
return err(
new AnalysisOutputError(
"list_names",
"provider did not bind the name inventory to a target SHA-256",
),
);
const metadata = nativeDispatchMetadataResultSchema.parse({
target_sha256: targetSha256,
provider: namesExecution.value.provider,
analysis_profile_digest:
namesExecution.value.analysisProfile?.digest ?? null,
result: inspectNativeDispatchMetadata(
parsedNames.data.map((entry) => ({
address: entry.address,
name: "name" in entry ? entry.name : entry.value,
})),
parsedNames.data.length,
),
});
let graph = emptyGraph(targetSha256, namesExecution.value.provider);
const coverage = [];
if (uiExecution.ok) {
const decoded = interfaceBuilderAnalysisSchema.safeParse(
uiExecution.value.result,
);
if (!decoded.success)
return err(
new AnalysisOutputError(
"decode_interface_builder",
"artifact provider returned an invalid interface graph",
),
);
if (decoded.data.target_sha256 !== targetSha256)
return err(
new AnalysisOutputError(
"decode_interface_builder",
"UI and native provider results refer to different target SHA-256 values",
),
);
graph = joinInterfaceBuilderDispatch(decoded.data.graph, metadata.result);
coverage.push({
facet: "interface_builder_archives",
status: decoded.data.documents.length > 0 ? "complete" : "unsupported",
reason:
decoded.data.documents.length > 0
? null
: "no_compiled_interface_builder_archives_found",
examined: decoded.data.documents.length,
omitted: 0,
});
const bounded = boundUiGraph(graph, input.max_nodes, input.max_edges);
graph = bounded.graph;
if (bounded.truncated)
coverage.push({
facet: "interface_builder_graph",
status: "partial",
reason: "native_trace_output_bounds_reached",
examined:
decoded.data.graph.nodes.length + decoded.data.graph.edges.length,
omitted: bounded.omitted,
});
} else {
coverage.push({
facet: "interface_builder_archives",
status: "unsupported",
reason: projectAnalysisError(uiExecution.error).message,
examined: 0,
omitted: 0,
});
}
const selected = resolveUiAction(graph, input.action);
if (selected.nodeId === null) {
coverage.push(...graph.coverage);
const trace = {
target_sha256: graph.target_sha256,
provider: graph.provider,
start: input.action,
direction: "forward" as const,
nodes: selected.candidates,
edges: [],
unresolved: [],
reached_depth: 0,
truncated: graph.truncated,
reason: selected.reason,
coverage,
limitations: [
"The query did not identify one unique authored UI action; no handler path was inferred.",
],
};
return ok(
jsonValueSchema.parse(nativeInvestigationTraceSchema.parse(trace)),
);
}
const initial = traceNativeInvestigationGraph(graph, {
start: selected.nodeId,
direction: "forward",
limits: {
max_depth: input.max_depth,
max_nodes: input.max_nodes,
max_edges: input.max_edges,
},
});
const expanded = await addDirectCallees({
analysis,
graph,
initial,
input,
targetSha256,
signal,
});
if (expanded.error !== undefined) return err(expanded.error);
coverage.push({
facet: "direct_native_calls",
status: "partial",
reason: "resolved_direct_calls_do_not_cover_all_indirect_dispatch",
examined: expanded.examined,
omitted: expanded.omitted,
});
coverage.push({
facet: "cross_function_value_flow",
status: "unsupported",
reason:
"call edges identify control-flow candidates but do not recover argument, return, or shared-memory value relationships",
examined: 0,
omitted: 0,
});
const finalGraph = nativeInvestigationGraphSchema.parse({
...graph,
nodes: expanded.nodes,
edges: expanded.edges,
coverage: [...graph.coverage, ...coverage],
truncated: graph.truncated || expanded.truncated,
});
const trace = traceNativeInvestigationGraph(finalGraph, {
start: selected.nodeId,
direction: "forward",
limits: {
max_depth: input.max_depth,
max_nodes: input.max_nodes,
max_edges: input.max_edges,
},
});
return ok(
jsonValueSchema.parse(
nativeInvestigationTraceSchema.parse({
...trace,
truncated: trace.truncated || expanded.truncated || graph.truncated,
reason:
expanded.truncated && trace.reason === null
? "native_call_traversal_bound_reached"
: graph.truncated && trace.reason === null
? "native_trace_output_bound_reached"
: trace.reason,
}),
),
);
};
const emptyGraph = (
targetSha256: string,
provider: {
readonly id: string;
readonly version: string | null;
},
) =>
nativeInvestigationGraphSchema.parse({
target_sha256: targetSha256,
provider: {
id: provider.id,
version: provider.version,
tool_version: "rea-native-ui-trace/1",
},
nodes: [],
edges: [],
coverage: [],
truncated: false,
});
const boundUiGraph = (
graph: z.infer<typeof nativeInvestigationGraphSchema>,
maxNodes: number,
maxEdges: number,
): {
readonly graph: z.infer<typeof nativeInvestigationGraphSchema>;
readonly truncated: boolean;
readonly omitted: number;
} => {
const nodes = graph.nodes.slice(0, maxNodes);
const nodeIds = new Set(nodes.map(({ id }) => id));
const eligibleEdges = graph.edges.filter(
(edge) =>
nodeIds.has(edge.from) && (edge.to === null || nodeIds.has(edge.to)),
);
const edges = eligibleEdges.slice(0, maxEdges);
const omitted =
graph.nodes.length - nodes.length + graph.edges.length - edges.length;
const truncated = omitted > 0;
return {
graph: nativeInvestigationGraphSchema.parse({
...graph,
nodes,
edges,
truncated: graph.truncated || truncated,
}),
truncated,
omitted,
};
};
const resolveUiAction = (
graph: z.infer<typeof nativeInvestigationGraphSchema>,
query: string,
): {
readonly nodeId: string | null;
readonly candidates: z.infer<typeof nativeInvestigationGraphSchema>["nodes"];
readonly reason: string | null;
} => {
const exactId = graph.nodes.find((node) => node.id === query);
if (exactId?.kind === "action")
return { nodeId: exactId.id, candidates: [], reason: null };
const actions = graph.nodes.filter(
(node) =>
node.kind === "action" &&
(node.name === query || node.attributes.selector === query),
);
if (actions.length === 1)
return { nodeId: actions[0]?.id ?? null, candidates: [], reason: null };
if (actions.length > 1)
return {
nodeId: null,
candidates: actions,
reason: "action_selector_matches_multiple_ui_connections",
};
const objectMatches = graph.nodes.filter(
(node) =>
["control", "view", "view_controller", "scene"].includes(node.kind) &&
(node.id === query ||
node.attributes.interface_builder_object_id === query ||
node.id.endsWith(`:object:${query}`)),
);
const actionIds = new Set(
graph.edges.flatMap((edge) =>
edge.resolution === "observed" &&
edge.relation === "target_action" &&
objectMatches.some(({ id }) => edge.from === id) &&
graph.nodes.some(({ id, kind }) => id === edge.to && kind === "action")
? [edge.to]
: [],
),
);
if (actionIds.size === 1)
return { nodeId: [...actionIds][0] ?? null, candidates: [], reason: null };
if (actionIds.size > 1)
return {
nodeId: null,
candidates: graph.nodes.filter(({ id }) => actionIds.has(id)),
reason: "interface_object_has_multiple_action_connections",
};
return {
nodeId: null,
candidates: [],
reason: "ui_action_or_object_not_found",
};
};
const addDirectCallees = async (input: {
readonly analysis: AnalysisOperationPort;
readonly graph: z.infer<typeof nativeInvestigationGraphSchema>;
readonly initial: z.infer<typeof nativeInvestigationTraceSchema>;
readonly input: Input;
readonly targetSha256: string;
readonly signal: AbortSignal | undefined;
}) => {
const nodes = new Map(input.initial.nodes.map((node) => [node.id, node]));
const edges = new Map(
[...input.initial.edges, ...input.initial.unresolved].map((edge) => [
edge.id,
edge,
]),
);
const addressNames = new Map<string, string>();
const depthByNode = graphNodeDepths(
input.initial.start,
input.initial.nodes,
input.initial.edges,
);
for (const node of nodes.values()) {
if (node.kind === "function" && node.location?.address !== null)
addressNames.set(node.location?.address ?? "", node.name);
}
const queue = input.initial.nodes.flatMap((node) =>
node.kind === "function" && node.location?.address != null
? [
{
node,
address: node.location.address,
depth: depthByNode.get(node.id) ?? input.initial.reached_depth,
},
]
: [],
);
const visited = new Set<string>();
let queueIndex = 0;
let examined = 0;
let omitted = 0;
let truncated = false;
while (queueIndex < queue.length) {
const current = queue[queueIndex++];
if (current === undefined || visited.has(current.address)) continue;
visited.add(current.address);
if (current.depth >= input.input.max_depth) {
truncated = true;
omitted += 1;
continue;
}
if (nodes.size >= input.input.max_nodes) {
truncated = true;
omitted += queue.length - queueIndex + 1;
break;
}
const execution = await input.analysis.execute(
"procedure_callees",
{ procedure: current.address },
input.signal === undefined ? {} : { signal: input.signal },
);
examined += 1;
if (!execution.ok) {
if (execution.error._tag === "AnalysisCancelledError")
return { error: execution.error };
const id = `unknown-callees:${current.address}`;
edges.set(id, {
id,
from: current.node.id,
to: null,
relation: "direct_call",
resolution: "unresolved",
reason: projectAnalysisError(execution.error).message,
evidence: [],
limitations: [
"The provider could not enumerate resolved direct callees.",
],
});
continue;
}
const related = parseRelatedAddresses(execution.value.result, "callees");
if (!related.ok) {
const id = `unknown-callees:${current.address}`;
edges.set(id, {
id,
from: current.node.id,
to: null,
relation: "direct_call",
resolution: "unresolved",
reason: "provider_returned_unreadable_callee_relationships",
evidence: [],
limitations: [
"The provider result could not be normalized into callee addresses.",
],
});
continue;
}
for (const [index, address] of related.value.entries()) {
if (edges.size >= input.input.max_edges) {
truncated = true;
omitted += related.value.length - index;
break;
}
const targetId = `native:function:${address}`;
const evidence = [
{
kind: "reference" as const,
description: `${execution.value.provider.name} resolved a direct callee from ${current.address} to ${address}`,
location: { address: current.address, file_offset: null },
artifact_path: null,
artifact_sha256: input.targetSha256,
},
];
if (!nodes.has(targetId)) {
if (nodes.size >= input.input.max_nodes) {
truncated = true;
omitted += 1;
const id = `unknown-node-bound:${current.address}:${address}`;
edges.set(id, {
id,
from: current.node.id,
to: null,
relation: "direct_call",
resolution: "unresolved",
reason: "native_trace_node_bound_reached",
evidence,
limitations: [],
});
continue;
}
const name = addressNames.get(address) ?? address;
const node = {
id: targetId,
kind: "function" as const,
name,
location: { address, file_offset: null },
attributes: { resolved_by: execution.value.provider.id },
evidence,
};
nodes.set(targetId, node);
queue.push({ node, address, depth: current.depth + 1 });
}
const edgeId = `call:${current.address}:${address}`;
edges.set(edgeId, {
id: edgeId,
from: current.node.id,
to: targetId,
relation: "direct_call",
resolution: "observed",
evidence,
limitations: [
"A resolved static call edge does not establish runtime reachability.",
],
});
}
}
return {
nodes: [...nodes.values()],
edges: [...edges.values()],
examined,
omitted,
truncated,
};
};
const graphNodeDepths = (
start: string,
nodes: readonly z.infer<
typeof nativeInvestigationGraphSchema
>["nodes"][number][],
edges: readonly z.infer<
typeof nativeInvestigationGraphSchema
>["edges"][number][],
): ReadonlyMap<string, number> => {
const known = new Set(nodes.map(({ id }) => id));
const depths = new Map([[start, 0]]);
const queue = [start];
for (let index = 0; index < queue.length; index += 1) {
const current = queue[index];
if (current === undefined) continue;
const depth = depths.get(current) ?? 0;
for (const edge of edges) {
if (
edge.resolution === "unresolved" ||
edge.from !== current ||
edge.to === null ||
!known.has(edge.to) ||
depths.has(edge.to)
)
continue;
depths.set(edge.to, depth + 1);
queue.push(edge.to);
}
}
return depths;
};
+18 -5
View File
@@ -1,4 +1,5 @@
import {
clientConfigurationValuesEqual,
parseClientConfiguration,
serializeClientConfiguration,
type ClientConfigurationDocument,
@@ -6,7 +7,6 @@ import {
import { constants as fsConstants } from "node:fs";
import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { isDeepStrictEqual } from "node:util";
import writeFileAtomic from "write-file-atomic";
import { PRODUCT_IDENTITY } from "../identity.js";
@@ -70,7 +70,12 @@ const configureClientDocument = async (
}
const { document, servers, serversKey } = parsed;
const desired = clientConfigurationDesired(client, environment, command);
if (isDeepStrictEqual(servers[PRODUCT_IDENTITY.mcpServerKey], desired))
if (
clientConfigurationValuesEqual(
servers[PRODUCT_IDENTITY.mcpServerKey],
desired,
)
)
return { status: "unchanged" };
const backupPath =
original === undefined ? undefined : `${client.configPath}.rea.backup`;
@@ -102,7 +107,7 @@ const configureClientDocument = async (
format,
);
if (
!isDeepStrictEqual(
!clientConfigurationValuesEqual(
readback.servers[PRODUCT_IDENTITY.mcpServerKey],
desired,
)
@@ -134,7 +139,10 @@ export const clientConfigurationAligned = async (
try {
const original = await readFile(client.configPath, "utf8");
const { servers } = parseClientConfiguration(original, client.format);
return isDeepStrictEqual(servers[PRODUCT_IDENTITY.mcpServerKey], desired);
return clientConfigurationValuesEqual(
servers[PRODUCT_IDENTITY.mcpServerKey],
desired,
);
} catch {
return false;
}
@@ -174,7 +182,12 @@ export const inspectClientConfiguration = async (
);
try {
const { servers } = parseClientConfiguration(original, client.format);
if (isDeepStrictEqual(servers[PRODUCT_IDENTITY.mcpServerKey], desired))
if (
clientConfigurationValuesEqual(
servers[PRODUCT_IDENTITY.mcpServerKey],
desired,
)
)
return { status: "already_current" };
} catch {
return {
+30 -1
View File
@@ -9,6 +9,7 @@ import {
} from "../application/AnalysisProvider.js";
import { inventoryArtifact } from "../application/ArtifactInventory.js";
import { extractArtifact } from "../application/ArtifactExtraction.js";
import { analyzeInterfaceBuilderBundle } from "../application/InterfaceBuilderAnalysis.js";
import {
ARTIFACT_ANALYSIS_OPERATIONS,
artifactInventoryInputSchema,
@@ -22,6 +23,7 @@ import {
type AnalysisError,
} from "../domain/errors.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { interfaceBuilderLimitsSchema } from "../domain/interfaceBuilderGraph.js";
import { err, ok } from "../domain/result.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import { ARTIFACT_GRAPH_PROVIDER } from "../application/InvestigationProviders.js";
@@ -50,7 +52,7 @@ export class ArtifactProvider implements AnalysisProvider {
reason: null,
effects: Object.freeze({
mutatesArtifact: false,
launchesProcess: true,
launchesProcess: operation !== "decode_interface_builder",
mayShowUi: false,
mayAccessNetwork: false,
mayWriteFilesystem: operation === "extract_artifact",
@@ -109,6 +111,33 @@ class ArtifactClient implements AnalysisClient {
const inspected = await this.inspectArtifact(parameters, options);
return inspected;
}
if (operation === "decode_interface_builder") {
if (
this.target.kind !== "executable" ||
this.target.sourcePath === undefined ||
!this.target.sourcePath.toLowerCase().endsWith(".app")
)
throw new ArtifactReaderFailure(
"unavailable",
"decode_interface_builder requires an active .app bundle target",
);
const limits = interfaceBuilderLimitsSchema.parse(parameters);
const result = await analyzeInterfaceBuilderBundle({
bundlePath: this.target.sourcePath,
targetSha256: this.target.sha256,
limits,
...(options?.signal === undefined ? {} : { signal: options.signal }),
});
return ok(
createAnalysisExecution(result, IDENTITY, {
limitations: result.limitations,
locations: result.documents.map(({ relative_path: path }) => ({
kind: "artifact-path" as const,
path,
})),
}),
);
}
if (operation === "extract_artifact") {
const parsed = artifactExtractionExecutionSchema.parse(parameters);
const result = await extractArtifact(
+141
View File
@@ -0,0 +1,141 @@
import { describe, expect, it } from "vitest";
import { decodeNibArchive } from "./NibArchive.js";
const encodeArchive = (input: {
readonly classes: readonly string[];
readonly objects: readonly {
readonly classIndex: number;
readonly values: Readonly<
Record<string, number | string | boolean | null | { ref: number }>
>;
}[];
}): Buffer => {
const keys = [
...new Set(input.objects.flatMap(({ values }) => Object.keys(values))),
];
const keyIndex = new Map(keys.map((key, index) => [key, index]));
const valueRecords = input.objects.flatMap(({ values }) =>
Object.entries(values).map(([key, value]) =>
Buffer.concat([varint(keyIndex.get(key) ?? 0), encodeValue(value)]),
),
);
let valueStart = 0;
const records = input.objects.map(({ classIndex, values }) => {
const count = Object.keys(values).length;
const result = Buffer.concat([
varint(classIndex),
varint(valueStart),
varint(count),
]);
valueStart += count;
return result;
});
const objects = Buffer.concat(records);
const encodedKeys = Buffer.concat(
keys.map((key) =>
Buffer.concat([varint(Buffer.byteLength(key)), Buffer.from(key)]),
),
);
const values = Buffer.concat(valueRecords);
const classes = Buffer.concat(
input.classes.map((name) => {
const bytes = Buffer.from(name);
return Buffer.concat([varint(bytes.length), varint(0), bytes]);
}),
);
const objectsOffset = 50;
const keysOffset = objectsOffset + objects.length;
const valuesOffset = keysOffset + encodedKeys.length;
const classesOffset = valuesOffset + values.length;
const header = Buffer.alloc(50);
header.write("NIBArchive", 0, "ascii");
header.writeUInt32LE(1, 10);
header.writeUInt32LE(10, 14);
header.writeUInt32LE(input.objects.length, 18);
header.writeUInt32LE(objectsOffset, 22);
header.writeUInt32LE(keys.length, 26);
header.writeUInt32LE(keysOffset, 30);
header.writeUInt32LE(valueStart, 34);
header.writeUInt32LE(valuesOffset, 38);
header.writeUInt32LE(input.classes.length, 42);
header.writeUInt32LE(classesOffset, 46);
return Buffer.concat([header, objects, encodedKeys, values, classes]);
};
const varint = (value: number): Buffer => {
const bytes: number[] = [];
let remaining = value;
do {
let byte = remaining & 0x7f;
remaining >>>= 7;
if (remaining === 0) byte |= 0x80;
bytes.push(byte);
} while (remaining > 0);
return Buffer.from(bytes);
};
const encodeValue = (
value: number | string | boolean | null | { ref: number },
): Buffer => {
if (typeof value === "boolean") return Buffer.from([value ? 4 : 5]);
if (value === null) return Buffer.from([9]);
if (typeof value === "number") {
const bytes = Buffer.alloc(5);
bytes[0] = 2;
bytes.writeInt32LE(value, 1);
return bytes;
}
if (typeof value === "object") {
const bytes = Buffer.alloc(5);
bytes[0] = 10;
bytes.writeUInt32LE(value.ref, 1);
return bytes;
}
const data = Buffer.from(value);
return Buffer.concat([Buffer.from([8]), varint(data.length), data]);
};
describe("NIBArchive decoder", () => {
it("decodes bounded object, key, class, and reference tables", () => {
const archive = encodeArchive({
classes: ["NSView\0", "NSString\0"],
objects: [
{ classIndex: 0, values: { child: { ref: 1 }, enabled: true } },
{ classIndex: 1, values: { text: "Button" } },
],
});
const decoded = decodeNibArchive(archive);
expect(decoded.objects).toEqual([
{
id: 0,
class_name: "NSView",
values: { child: { $nib_object_ref: 1 }, enabled: true },
},
{
id: 1,
class_name: "NSString",
values: { text: { $nib_data_base64: "QnV0dG9u" } },
},
]);
expect(decoded.coder_version).toBe(10);
});
it("rejects malformed references and unsupported format versions", () => {
const valid = encodeArchive({
classes: ["NSObject"],
objects: [{ classIndex: 0, values: {} }],
});
const invalidVersion = Buffer.from(valid);
invalidVersion.writeUInt32LE(2, 10);
expect(() => decodeNibArchive(invalidVersion)).toThrow(/Unsupported/u);
const invalidReference = encodeArchive({
classes: ["NSObject"],
objects: [{ classIndex: 0, values: { child: { ref: 4 } } }],
});
expect(() => decodeNibArchive(invalidReference)).toThrow(
/object reference/u,
);
});
});
+428
View File
@@ -0,0 +1,428 @@
import type { JsonValue } from "../domain/jsonValue.js";
import { TextDecoder } from "node:util";
const MAGIC = Buffer.from("NIBArchive", "ascii");
const MAX_RECORDS = 1_000_000;
interface NibObjectRecord {
readonly class_index: number;
readonly value_start: number;
readonly value_count: number;
}
interface NibValueRecord {
readonly key_index: number;
readonly value: JsonValue;
}
/** Parsed NIBArchive payload with the original object indices preserved. */
export interface NibArchiveDocument {
readonly format_version: number;
readonly coder_version: number;
readonly object_count: number;
readonly connection_object_count: number;
readonly objects: readonly {
readonly id: number;
readonly class_name: string;
readonly values: Readonly<Record<string, JsonValue>>;
}[];
readonly classes: readonly string[];
readonly limitations: readonly string[];
}
/** Decode Apple's length-prefixed NIBArchive representation without loading UI classes. */
export const decodeNibArchive = (bytes: Buffer): NibArchiveDocument => {
if (bytes.length < 50 || !bytes.subarray(0, MAGIC.length).equals(MAGIC))
throw new TypeError("NIBArchive magic or header is invalid");
const formatVersion = readU32(bytes, 10);
const coderVersion = readU32(bytes, 14);
if (formatVersion !== 1)
throw new TypeError(
`Unsupported NIBArchive format version ${formatVersion}`,
);
const objectCount = readCount(bytes, 18, "object");
const objectsOffset = readOffset(bytes, 22, bytes.length, "object");
const keyCount = readCount(bytes, 26, "key");
const keysOffset = readOffset(bytes, 30, bytes.length, "key");
const valueCount = readCount(bytes, 34, "value");
const valuesOffset = readOffset(bytes, 38, bytes.length, "value");
const classCount = readCount(bytes, 42, "class");
const classesOffset = readOffset(bytes, 46, bytes.length, "class");
assertOrderedOffsets(bytes.length, [
objectsOffset,
keysOffset,
valuesOffset,
classesOffset,
]);
const objectRecords = parseObjects(
bytes,
objectsOffset,
keysOffset,
objectCount,
valueCount,
classCount,
);
const keys = parseStrings(bytes, keysOffset, valuesOffset, keyCount, "key");
const classes = parseClasses(bytes, classesOffset, bytes.length, classCount);
const neededValues = valueIndexes(objectRecords, valueCount);
const values = parseValues(
bytes,
valuesOffset,
classesOffset,
valueCount,
keyCount,
neededValues,
);
for (const { value } of values.values()) {
if (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
typeof value.$nib_object_ref === "number" &&
value.$nib_object_ref >= objectCount
)
throw new TypeError(
"NIBArchive object reference is outside the object table",
);
}
const objects = objectRecords.flatMap((object, id) => {
const className = classes[object.class_index];
if (className === undefined) return [];
const fields: Record<string, JsonValue> = {};
for (
let index = object.value_start;
index < object.value_start + object.value_count;
index += 1
) {
const value = values.get(index);
if (value === undefined) continue;
const key = keys[value.key_index];
if (key !== undefined) fields[key] = value.value;
}
return [{ id, class_name: className, values: fields }];
});
let connectionObjectCount = 0;
for (const object of objects)
if (
/(?:Outlet|Connection|Segue|ActionConnection)/iu.test(object.class_name)
)
connectionObjectCount += 1;
return {
format_version: formatVersion,
coder_version: coderVersion,
object_count: objectCount,
connection_object_count: connectionObjectCount,
objects,
classes,
limitations: [
"NIBArchive class extensions and unsupported value encodings are retained as unavailable fields.",
"NIBArchive object relationships are decoded from serialized references; runtime object creation and execution are not performed.",
],
};
};
const parseObjects = (
bytes: Buffer,
start: number,
end: number,
count: number,
valueCount: number,
classCount: number,
): NibObjectRecord[] => {
const cursor = { offset: start };
const output: NibObjectRecord[] = [];
for (let index = 0; index < count; index += 1) {
const classIndex = readVarint(bytes, cursor, end);
const valueStart = readVarint(bytes, cursor, end);
const countForObject = readVarint(bytes, cursor, end);
if (
classIndex >= classCount ||
valueStart > valueCount ||
countForObject > valueCount - valueStart
)
throw new TypeError(
`NIBArchive object ${index} references an invalid table range`,
);
output.push({
class_index: classIndex,
value_start: valueStart,
value_count: countForObject,
});
}
if (cursor.offset > end)
throw new TypeError("NIBArchive object table overlaps the next table");
return output;
};
const parseStrings = (
bytes: Buffer,
start: number,
end: number,
count: number,
label: string,
): string[] => {
const cursor = { offset: start };
const output: string[] = [];
for (let index = 0; index < count; index += 1) {
const length = readVarint(bytes, cursor, end);
const next = checkedEnd(cursor.offset, length, end, `${label} ${index}`);
const value = decodeUtf8(bytes, cursor.offset, next, `${label} ${index}`);
cursor.offset = next;
output.push(value);
}
return output;
};
const parseClasses = (
bytes: Buffer,
start: number,
end: number,
count: number,
): string[] => {
const cursor = { offset: start };
const output: string[] = [];
for (let index = 0; index < count; index += 1) {
const length = readVarint(bytes, cursor, end);
const extraCount = readVarint(bytes, cursor, end);
if (extraCount > 16)
throw new TypeError(
`NIBArchive class ${index} has too many extension values`,
);
ensureRange(
cursor.offset,
extraCount * 4,
end,
`class ${index} extensions`,
);
cursor.offset += extraCount * 4;
const next = checkedEnd(cursor.offset, length, end, `class ${index}`);
const name = decodeUtf8(
bytes,
cursor.offset,
next,
`class ${index}`,
).replace(/\0+$/u, "");
cursor.offset = next;
output.push(name);
}
return output;
};
const parseValues = (
bytes: Buffer,
start: number,
end: number,
count: number,
keyCount: number,
needed: ReadonlySet<number>,
): Map<number, NibValueRecord> => {
const cursor = { offset: start };
const output = new Map<number, NibValueRecord>();
for (let index = 0; index < count; index += 1) {
const keyIndex = readVarint(bytes, cursor, end);
if (keyIndex >= keyCount)
throw new TypeError(
`NIBArchive value ${index} references an invalid key`,
);
ensureRange(cursor.offset, 1, end, `value ${index} type`);
const type = bytes[cursor.offset++] ?? -1;
const value = readValue(bytes, cursor, end, type, index);
if (needed.has(index)) output.set(index, { key_index: keyIndex, value });
}
if (cursor.offset > end)
throw new TypeError("NIBArchive value table overlaps the next table");
return output;
};
const readValue = (
bytes: Buffer,
cursor: { offset: number },
end: number,
type: number,
index: number,
): JsonValue => {
switch (type) {
case 0:
ensureRange(cursor.offset, 1, end, `value ${index}`);
return bytes.readInt8(cursor.offset++);
case 1:
ensureRange(cursor.offset, 2, end, `value ${index}`);
{
const value = bytes.readInt16LE(cursor.offset);
cursor.offset += 2;
return value;
}
case 2:
ensureRange(cursor.offset, 4, end, `value ${index}`);
{
const value = bytes.readInt32LE(cursor.offset);
cursor.offset += 4;
return value;
}
case 3:
ensureRange(cursor.offset, 8, end, `value ${index}`);
{
const value = bytes.readBigInt64LE(cursor.offset);
cursor.offset += 8;
return value <= BigInt(Number.MAX_SAFE_INTEGER) &&
value >= BigInt(Number.MIN_SAFE_INTEGER)
? Number(value)
: value.toString();
}
case 4:
return true;
case 5:
return false;
case 6:
ensureRange(cursor.offset, 4, end, `value ${index}`);
{
const value = bytes.readFloatLE(cursor.offset);
cursor.offset += 4;
return Number.isFinite(value) ? value : null;
}
case 7:
ensureRange(cursor.offset, 8, end, `value ${index}`);
{
const value = bytes.readDoubleLE(cursor.offset);
cursor.offset += 8;
return Number.isFinite(value) ? value : null;
}
case 8: {
const length = readVarint(bytes, cursor, end);
const next = checkedEnd(
cursor.offset,
length,
end,
`value ${index} data`,
);
const encoded = bytes.subarray(cursor.offset, next).toString("base64");
cursor.offset = next;
return { $nib_data_base64: encoded };
}
case 9:
return null;
case 10:
ensureRange(cursor.offset, 4, end, `value ${index} reference`);
{
const objectIndex = bytes.readUInt32LE(cursor.offset);
cursor.offset += 4;
return { $nib_object_ref: objectIndex };
}
default:
throw new TypeError(
`NIBArchive value ${index} has unsupported type ${type}`,
);
}
};
const valueIndexes = (
objects: readonly NibObjectRecord[],
total: number,
): ReadonlySet<number> => {
const indexes = new Set<number>();
for (const object of objects)
for (
let index = object.value_start;
index < object.value_start + object.value_count;
index += 1
)
indexes.add(index);
if (indexes.size > total)
throw new TypeError(
"NIBArchive object value ranges exceed the value table",
);
return indexes;
};
const readCount = (bytes: Buffer, offset: number, label: string): number => {
const count = readU32(bytes, offset);
if (count > MAX_RECORDS)
throw new TypeError(`NIBArchive ${label} count exceeds the decoder bound`);
return count;
};
const readOffset = (
bytes: Buffer,
offset: number,
fileLength: number,
label: string,
): number => {
const value = readU32(bytes, offset);
if (value < 50 || value > fileLength)
throw new TypeError(`NIBArchive ${label} table offset is invalid`);
return value;
};
const readU32 = (bytes: Buffer, offset: number): number => {
ensureRange(offset, 4, bytes.length, "header");
return bytes.readUInt32LE(offset);
};
const decodeUtf8 = (
bytes: Buffer,
start: number,
end: number,
label: string,
): string => {
try {
return new TextDecoder("utf-8", { fatal: true }).decode(
bytes.subarray(start, end),
);
} catch {
throw new TypeError(`NIBArchive ${label} is not valid UTF-8`);
}
};
const readVarint = (
bytes: Buffer,
cursor: { offset: number },
end: number,
): number => {
let value = 0;
let shift = 0;
for (let index = 0; index < 5; index += 1) {
ensureRange(cursor.offset, 1, end, "variable integer");
const byte = bytes[cursor.offset++] ?? 0;
value += (byte & 0x7f) * 2 ** shift;
if ((byte & 0x80) !== 0) {
if (value > 0x7fff_ffff)
throw new TypeError("NIBArchive variable integer is out of range");
return value;
}
shift += 7;
}
throw new TypeError("NIBArchive variable integer is too long");
};
const checkedEnd = (
start: number,
length: number,
limit: number,
label: string,
): number => {
if (!Number.isSafeInteger(length) || length < 0 || start + length > limit)
throw new TypeError(`NIBArchive ${label} exceeds its table`);
return start + length;
};
const ensureRange = (
start: number,
length: number,
limit: number,
label: string,
): void => {
checkedEnd(start, length, limit, label);
};
const assertOrderedOffsets = (length: number, offsets: readonly number[]) => {
let previous = 50;
for (const offset of offsets) {
if (offset < previous || offset > length)
throw new TypeError("NIBArchive table offsets are out of order");
previous = offset;
}
};
@@ -39,3 +39,31 @@ it("returns a complete requested text artifact beyond the former inline ceiling"
value: { bytes: 16 * 1_024 * 1_024 + 1 },
});
});
it("wraps URL, history, and storage snapshots in the capture state contract", async () => {
const page = {
url: () => "https://app.example.test/path?token=secret",
evaluate: async (expression: string) =>
expression.includes("window.history.length")
? {
length: 2,
navigation_entries: [
{ type: "navigate", name: "https://app.example.test/path" },
],
}
: { local_storage: [], session_storage: [] },
} as unknown as Page;
const context = { cookies: async () => [] } as unknown as BrowserContext;
const result = await capturePlaywrightStepArtifacts({
context,
page,
scenario,
secrets,
requested: new Set(["url", "history", "storage"]),
});
expect(result.url.state).toBe("captured");
expect(result.history.state).toBe("captured");
expect(result.storage.state).toBe("captured");
});
+6 -5
View File
@@ -126,7 +126,7 @@ const captureHistory = async (page: Page) => {
name: sanitizeBrowserUrl(name),
})),
};
return value;
return { state: "captured" as const, value };
} catch {
return missing("history capture failed");
}
@@ -166,7 +166,7 @@ const captureStorage = async (input: {
...secrets.fingerprint(value),
})),
};
return value;
return { state: "captured" as const, value };
} catch {
return missing("storage capture failed");
}
@@ -211,9 +211,10 @@ export const capturePlaywrightStepArtifacts = async (input: {
accessibility: await state("accessibility", () =>
captureAccessibility({ page, secrets }),
),
url: await state("url", async () =>
Promise.resolve(sanitizeBrowserUrl(page.url())),
),
url: await state("url", async () => ({
state: "captured" as const,
value: sanitizeBrowserUrl(page.url()),
})),
history: await state("history", () => captureHistory(page)),
storage: await state("storage", () =>
captureStorage({ context, page, scenario, secrets }),
+55
View File
@@ -13,6 +13,61 @@ export const registerArtifactCommands = (
): void => {
registerInspectionCommand(cli, logger);
registerExtractionCommand(cli, logger);
registerInterfaceBuilderCommand(cli, logger);
};
const registerInterfaceBuilderCommand = (
cli: CliInstance,
logger: Logger,
): void => {
cli.command(CLI_COMMANDS.decodeInterfaceBuilder, {
description:
"Decode compiled storyboard and nib archives from an app bundle",
args: z.object({
path: z.string().describe("Apple .app bundle path"),
}),
options: z.object({
maxDocuments: z
.number()
.int()
.min(1)
.max(64)
.default(64)
.describe("Maximum archives to decode"),
maxObjects: z
.number()
.int()
.min(1)
.max(20_000)
.default(20_000)
.describe("Maximum graph objects"),
maxConnections: z
.number()
.int()
.min(1)
.max(40_000)
.default(40_000)
.describe("Maximum decoded connections"),
}),
alias: {
maxDocuments: "max-documents",
maxObjects: "max-objects",
maxConnections: "max-connections",
},
run: ({ args, options }) =>
logCliCommand(logger, "decode-interface-builder", () =>
runProviderAnalysis(
args.path,
"decode_interface_builder",
{
max_documents: options.maxDocuments,
max_objects: options.maxObjects,
max_connections: options.maxConnections,
},
logger,
),
),
});
};
const registerExtractionCommand = (cli: CliInstance, logger: Logger): void => {
+101
View File
@@ -21,6 +21,107 @@ export const registerCoreAnalysisCommands = (
registerSearchCommand(cli, logger);
registerXrefsCommand(cli, logger);
registerTraceCommand(cli, logger);
registerNativeUiActionCommand(cli, logger);
registerNativeDispatchMetadataCommand(cli, logger);
};
const registerNativeDispatchMetadataCommand = (
cli: CliInstance,
logger: Logger,
): void => {
cli.command(CLI_COMMANDS.inspectNativeDispatchMetadata, {
description: "Inspect typed Objective-C and Swift dispatch metadata",
args: z.object({
path: z.string().describe("Target path used to bind the result evidence"),
}),
options: z.object({
maxRecords: z
.number()
.int()
.min(1)
.max(20_000)
.default(5_000)
.describe("Maximum symbol records to inspect"),
snapshot: z
.string()
.min(1)
.optional()
.describe("Load or update the local analysis snapshot"),
provider: providerSelectionOption,
}),
alias: { maxRecords: "max-records" },
run: async ({ args, options }) =>
logCliCommand(logger, "inspect-native-dispatch-metadata", () =>
runDirectAnalysis(
args.path,
"inspect_native_dispatch_metadata",
{ max_records: options.maxRecords },
directAnalysisOptions(logger, options.snapshot, options.provider),
),
),
});
};
const registerNativeUiActionCommand = (
cli: CliInstance,
logger: Logger,
): void => {
cli.command(CLI_COMMANDS.traceNativeUiAction, {
description:
"Trace a compiled UI action selector to its native handler and direct callees",
args: z.object({
path: z.string().describe("Target path used to bind the result evidence"),
action: z
.string()
.min(1)
.describe(
"A unique compiled UI action selector or interface object ID",
),
}),
options: z.object({
maxDepth: z
.number()
.int()
.min(0)
.max(32)
.default(8)
.describe("Maximum relationship depth"),
maxNodes: z
.number()
.int()
.min(1)
.max(2_000)
.default(250)
.describe("Maximum returned graph nodes"),
maxEdges: z
.number()
.int()
.min(1)
.max(5_000)
.default(500)
.describe("Maximum returned graph edges"),
provider: providerSelectionOption,
}),
alias: {
maxDepth: "max-depth",
maxNodes: "max-nodes",
maxEdges: "max-edges",
},
run: async ({ args, options }) =>
logCliCommand(logger, "trace-native-ui-action", () =>
runDirectAnalysis(
args.path,
"trace_native_ui_action",
{
action: args.action,
max_depth: options.maxDepth,
max_nodes: options.maxNodes,
max_edges: options.maxEdges,
},
directAnalysisOptions(logger, undefined, options.provider),
),
),
});
};
const registerCoreCommands = (cli: CliInstance, logger: Logger): void => {
+3
View File
@@ -9,6 +9,8 @@ export const CLI_COMMANDS = Object.freeze({
upgrade: "upgrade",
xrefs: "xrefs",
trace: "trace",
traceNativeUiAction: "trace-native-ui-action",
inspectNativeDispatchMetadata: "inspect-native-dispatch-metadata",
capabilities: "capabilities",
providers: "providers",
function: "function",
@@ -18,6 +20,7 @@ export const CLI_COMMANDS = Object.freeze({
importReferenceSource: "import-reference-source",
inspectArtifact: "inspect-artifact",
extractArtifact: "extract-artifact",
decodeInterfaceBuilder: "decode-interface-builder",
inspectManagedArtifact: "inspect-managed-artifact",
inspectManagedMembers: "inspect-managed-members",
inspectManagedNativeBoundaries: "inspect-managed-native-boundaries",
+10
View File
@@ -38,6 +38,7 @@ const exampleInputSchema = z.record(z.string(), jsonValueSchema);
const examples: Readonly<Record<string, Readonly<Record<string, unknown>>>> = {
inspect_artifact: {},
extract_artifact: {},
decode_interface_builder: {},
};
const artifact = <
@@ -77,6 +78,15 @@ export const ARTIFACT_TOOL_CONTRACTS = [
"Extract all regular files from the active archive or application package into a fresh temporary directory chosen by REA. The result includes its location. Rejects traversal and symlink escapes, never overwrites, enforces archive integrity checks, and verifies cleanup.",
artifactExtractionInputSchema,
),
artifact(
"decode_interface_builder",
"Decode compiled storyboard and nib archives inside the active Apple app bundle, including keyed property lists and NIBArchive object tables, into a bounded object, hierarchy, and connection graph. This is static archive parsing; unrecognized archive classes and unresolved code handlers remain explicit unknowns.",
z.strictObject({
max_documents: z.number().int().min(1).max(64).default(64),
max_objects: z.number().int().min(1).max(20_000).default(20_000),
max_connections: z.number().int().min(1).max(40_000).default(40_000),
}),
),
] as const satisfies readonly ToolContract[];
/** Provider operations; inventory remains an internal primitive of inspection. */
+12 -1
View File
@@ -1,5 +1,4 @@
import { z } from "zod";
const traceLiteralInputSchema = z.strictObject({
query: z.string().min(1),
case_sensitive: z.boolean().default(false),
@@ -7,6 +6,9 @@ const traceLiteralInputSchema = z.strictObject({
/** Input schemas shared by MCP registration and enhanced application dispatch. */
export const enhancedInputSchemas = {
inspect_native_dispatch_metadata: z.strictObject({
max_records: z.number().int().min(1).max(20_000).default(5_000),
}),
get_objc_classes: z.strictObject({ pattern: z.string().default("") }),
get_objc_protocols: z.strictObject({}),
batch_decompile: z.strictObject({
@@ -48,6 +50,15 @@ export const enhancedInputSchemas = {
.optional(),
direction: z.enum(["forward", "backward"]).default("forward"),
}),
trace_native_ui_action: z.strictObject({
action: z
.string()
.min(1)
.describe("A unique compiled UI action selector or interface object ID"),
max_depth: z.number().int().min(0).max(32).default(8),
max_nodes: z.number().int().min(1).max(2_000).default(250),
max_edges: z.number().int().min(1).max(5_000).default(500),
}),
} as const;
export type EnhancedToolName = keyof typeof enhancedInputSchemas;
+1
View File
@@ -44,6 +44,7 @@ export const TOOL_EXAMPLE_OVERRIDES: Readonly<
trace_feature: { query: "license" },
find_code_for_string: { query: "authorization failed" },
trace_call_path: { start: "0x1000", goal: "0x1100" },
trace_native_ui_action: { action: "buildTapped:" },
open_binary: { path: "/tmp/fixture" },
export_evidence_bundle: { path: "/tmp/evidence.json" },
inspect_address_context: { address: "0x1000" },
+2 -1
View File
@@ -16,11 +16,12 @@ import { APPLICATION_TOOL_CONTRACTS } from "./applicationToolContracts.js";
describe("tool contract inventory", () => {
it("publishes the canonical analysis and session tool inventory", () => {
expect(OFFICIAL_TOOL_CONTRACTS).toHaveLength(36);
expect(ENHANCED_TOOL_CONTRACTS).toHaveLength(12);
expect(ENHANCED_TOOL_CONTRACTS).toHaveLength(14);
expect(NATIVE_TOOL_CONTRACTS).toHaveLength(5);
expect(ARTIFACT_TOOL_CONTRACTS.map(({ name }) => name)).toEqual([
"inspect_artifact",
"extract_artifact",
"decode_interface_builder",
]);
expect(MANAGED_TOOL_CONTRACTS.map(({ name }) => name)).toEqual([
"inspect_managed_artifact",
+10
View File
@@ -295,6 +295,11 @@ export type OfficialToolName = (typeof OFFICIAL_TOOL_CONTRACTS)[number]["name"];
/** Workflow tools composed from one or more provider operations. */
export const ENHANCED_TOOL_CONTRACTS = [
enhanced(
"inspect_native_dispatch_metadata",
"Inspect the bound provider's name inventory for Objective-C class/method symbols and Swift mangled symbols, returning at most max_records decoded names. Includes target SHA-256 when available, provider identity/version, analysis-profile digest, facet coverage, and per-record Evidence. This is symbol-level evidence, not runtime metadata: ivar layouts, protocol conformances, witness/class tables, and relative pointers are explicitly marked unsupported unless a provider supplies those decoders. The provider controls how its name inventory is acquired.",
enhancedInputSchemas.inspect_native_dispatch_metadata,
),
enhanced(
"get_objc_classes",
"Discover and deduplicate Objective-C class labels, optionally filtering by literal substring.",
@@ -346,6 +351,11 @@ export const ENHANCED_TOOL_CONTRACTS = [
"Trace direct callers or callees from one exact procedure address until the graph is exhausted or the optional goal is reached. Returns visited nodes, direct-call edges, a shortest traversal path, provider failures, and residual unknowns; unresolved indirect calls remain unknown.",
enhancedInputSchemas.trace_call_path,
),
enhanced(
"trace_native_ui_action",
"Trace one unique compiled Interface Builder action selector or object ID through its authored UI connections to symbolized Objective-C handlers and bounded direct callees. REA decodes the active app bundle and reads the selected provider's symbol inventory itself. Authored connections, statically matched handlers, resolved direct calls, and unresolved dynamic dispatch remain distinct; runtime reachability and cross-function value flow are not claimed.",
enhancedInputSchemas.trace_native_ui_action,
),
] as const satisfies readonly ToolContract[];
/** Session-owned Evidence bundle export options. */
+3
View File
@@ -40,6 +40,8 @@ const sessionEvidence = effects({ mutatesSession: true });
/** Explicit effect audit for every public tool. */
export const TOOL_EFFECTS: Readonly<Record<string, ToolEffects>> = {
inspect_native_dispatch_metadata: evidence,
trace_native_ui_action: evidence,
address_name: evidence,
comment: evidence,
current_address: evidence,
@@ -99,6 +101,7 @@ export const TOOL_EFFECTS: Readonly<Record<string, ToolEffects>> = {
demangle_swift: nativeEvidence,
inspect_artifact: evidence,
extract_artifact: effects({ mutatesSession: true, writesFilesystem: true }),
decode_interface_builder: evidence,
inspect_managed_artifact: evidence,
inspect_managed_members: evidence,
inspect_managed_native_boundaries: evidence,
+8
View File
@@ -21,6 +21,7 @@ import {
} from "../domain/nativeInspection.js";
import { artifactExtractionResultSchema } from "../domain/artifactGraph.js";
import { artifactInspectionResultSchema } from "../domain/artifactInspection.js";
import { interfaceBuilderAnalysisSchema } from "../domain/interfaceBuilderGraph.js";
import {
managedArtifactInspectionSchema,
managedMemberInspectionSchema,
@@ -40,6 +41,8 @@ import { staticRuntimeCorrelationResultSchema } from "../domain/staticRuntimeCor
import { reconstructionVerificationResultSchema } from "../domain/reconstructionVerification.js";
import { replayMachineRunOutputSchema } from "../domain/replayMachineRun.js";
import { analysisErrorProjectionSchema } from "./errorSchemas.js";
import { nativeDispatchMetadataResultSchema } from "../domain/objcSwiftMetadata.js";
import { nativeInvestigationTraceSchema } from "../domain/nativeInvestigationGraph.js";
import {
addressList,
addressedValue,
@@ -219,6 +222,10 @@ const callPathTraceOutput = resultOf(
/** Exact structured-content schemas for composed analysis workflows. */
export const enhancedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
inspect_native_dispatch_metadata: resultOf(
nativeDispatchMetadataResultSchema,
),
trace_native_ui_action: resultOf(nativeInvestigationTraceSchema),
get_objc_classes: symbolDiscoveryOutput("classes"),
get_objc_protocols: symbolDiscoveryOutput("protocols"),
batch_decompile: resultOf(
@@ -306,6 +313,7 @@ export const nativeOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
export const artifactOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
inspect_artifact: resultOf(artifactInspectionResultSchema),
extract_artifact: resultOf(artifactExtractionResultSchema),
decode_interface_builder: resultOf(interfaceBuilderAnalysisSchema),
};
/** Exact Evidence schema for execution-free managed static analysis. */
+1
View File
@@ -275,6 +275,7 @@ export class ArtifactOperationError extends AnalysisError {
| "inventory_artifact"
| "inspect_artifact"
| "extract_artifact"
| "decode_interface_builder"
| "analyze_javascript_application",
readonly reason:
| "cancelled"
+36 -1
View File
@@ -92,7 +92,6 @@ export const ghidraNativeApiBoundary = () => ({
{
case_value: 0,
target_address: "0x401020",
data_addresses: ["0x403000"],
confidence: "medium" as const,
evidence: [
{
@@ -149,6 +148,42 @@ export const ghidraFunctionDossier = (includeAssembly = true): JsonValue => {
referenced_names: [],
basic_blocks: [{ start: "0x401000", end: "0x401006", successors: [] }],
native_api: ghidraNativeApiBoundary(),
native_value_flow: {
available: true,
provenance: "ghidra-high-pcode",
operations: [
{
id: "0x401000#0",
address: "0x401000",
sequence: 0,
opcode: "COPY",
is_dead: false,
inputs: [
{
kind: "constant",
size_bytes: 4,
location: null,
constant_hex: "2a",
},
],
output: {
kind: "register",
size_bytes: 4,
location: "register:0x0",
constant_hex: null,
},
},
],
def_use: [],
effects: [],
truncated: false,
omitted_operations_lower_bound: 0,
known_omitted_inputs: 0,
known_omitted_edges: 0,
limitations: [
"High p-code is a decompiler-derived intra-function representation, not original source or runtime behavior.",
],
},
limitations: [
"Unresolved computed or indirect flows without target addresses are not represented as reference edges.",
"Thunk and external classifications are Ghidra FunctionManager observations; they do not resolve targetless calls.",
+2
View File
@@ -4,6 +4,7 @@ import type { JsonValue } from "./jsonValue.js";
import { AnalysisOutputError, HopperProtocolError } from "./errors.js";
import { err, ok, type Result } from "./result.js";
import { nativeApiBoundarySchema } from "./nativeApiBoundary.js";
import { nativeValueFlowSchema } from "./nativeValueFlow.js";
export interface AddressedName {
readonly address: string;
@@ -176,6 +177,7 @@ export const functionDossierSchema = z
.strict(),
),
native_api: nativeApiBoundarySchema.nullable().default(null),
native_value_flow: nativeValueFlowSchema.nullable().default(null),
limitations: z.array(z.string()).default([]),
})
.strict();
+319
View File
@@ -0,0 +1,319 @@
import { describe, expect, it } from "vitest";
import { buildBinary, parseBinary } from "plist";
import {
buildInterfaceBuilderAnalysis,
interfaceBuilderLimitsSchema,
parseInterfaceBuilderRecords,
} from "./interfaceBuilderGraph.js";
const hash = "a".repeat(64);
describe("compiled Interface Builder graph projection", () => {
it("normalizes object, hierarchy, action, outlet, and segue facts", () => {
const raw = {
"com.apple.ibtool.document.objects": {
controller: { customClass: "StoreViewController", label: "Store" },
button: { class: "UIButton", title: "Build" },
next: { customClass: "BuildViewController" },
},
"com.apple.ibtool.document.connections": {
button: [
{
type: "action",
label: "buildTapped:",
destinationId: "controller",
},
{ type: "segue", identifier: "showBuild", destinationId: "next" },
],
controller: [
{ type: "outlet", label: "buildButton", destinationId: "button" },
],
},
"com.apple.ibtool.document.hierarchy": [
{ objectID: "controller", children: [{ objectID: "button" }] },
],
"com.apple.ibtool.document.classes": { StoreViewController: {} },
};
const decoded = parseInterfaceBuilderRecords(raw);
expect(decoded.objects).toHaveLength(3);
expect(decoded.connections.map(({ kind }) => kind)).toEqual([
"action",
"segue",
"outlet",
]);
const result = buildInterfaceBuilderAnalysis({
targetSha256: hash,
toolVersion: "test",
documents: [
{
relativePath: "Views/Main.storyboardc/scene.nib/objects.nib",
archiveSha256: hash,
documentKind: "storyboard_scene",
raw,
},
],
limits: interfaceBuilderLimitsSchema.parse({}),
});
expect(result.graph.nodes.some(({ name }) => name === "buildTapped:")).toBe(
true,
);
expect(result.graph.nodes[0]?.evidence[0]).toMatchObject({
artifact_path: "Views/Main.storyboardc/scene.nib/objects.nib",
artifact_sha256: hash,
});
expect(result.graph.edges).toContainEqual(
expect.objectContaining({
relation: "segue_to",
resolution: "observed",
}),
);
expect(result.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "hierarchy:Views/Main.storyboardc/scene.nib/objects.nib",
status: "complete",
}),
);
});
it("keeps missing external destinations as unresolved evidence", () => {
const result = buildInterfaceBuilderAnalysis({
targetSha256: hash,
toolVersion: "test",
documents: [
{
relativePath: "Legacy.nib",
archiveSha256: hash,
documentKind: "nib",
raw: {
"com.apple.ibtool.document.objects": {
button: { class: "UIButton" },
},
"com.apple.ibtool.document.connections": {
button: [
{ type: "action", label: "save:", destinationId: "external" },
],
},
},
},
],
limits: interfaceBuilderLimitsSchema.parse({ max_objects: 3 }),
});
expect(result.graph.edges).toContainEqual(
expect.objectContaining({
relation: "target_action",
resolution: "unresolved",
reason: "endpoint_omitted_by_object_limit",
evidence: [
expect.objectContaining({
kind: "interface_builder_resource",
artifact_path: "Legacy.nib",
artifact_sha256: hash,
}),
],
}),
);
const retainedNodes = new Set(result.graph.nodes.map(({ id }) => id));
expect(
result.graph.edges.every(
({ from, to }) =>
retainedNodes.has(from) && (to === null || retainedNodes.has(to)),
),
).toBe(true);
expect(result.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "graph_nodes",
status: "partial",
reason: "max_objects_reached",
}),
);
expect(result.graph.truncated).toBe(true);
});
it("reads recognized objects from a compiled NSKeyedArchiver object table", () => {
const bytes = buildBinary({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: [
"$null",
{ $class: { UID: 2 }, title: "Build", identifier: "build-button" },
{
$classname: "UIButton",
$classes: ["UIButton", "UIControl", "UIView", "NSObject"],
},
],
$top: { root: { UID: 1 } },
});
const parsed = parseInterfaceBuilderRecords(parseBinary(bytes));
expect(parsed.objects).toMatchObject([
{
id: "1",
kind: "control",
class_name: "UIButton",
name: "Build",
},
]);
});
});
describe("Interface Builder parser bounds", () => {
it("reports connections discarded by the parser's hard ceiling", () => {
const connections = Array.from({ length: 40_001 }, (_, index) => ({
type: "custom-connection",
label: `action${String(index)}:`,
}));
const raw = {
"com.apple.ibtool.document.objects": {
button: { class: "UIButton" },
},
"com.apple.ibtool.document.connections": { button: connections },
};
const parsed = parseInterfaceBuilderRecords(raw);
expect(parsed.connections).toHaveLength(40_000);
expect(parsed.omittedConnections).toBe(1);
const result = buildInterfaceBuilderAnalysis({
targetSha256: hash,
toolVersion: "test",
documents: [
{
relativePath: "Main.storyboardc/scene.nib",
archiveSha256: hash,
documentKind: "storyboard_scene",
raw,
},
],
limits: interfaceBuilderLimitsSchema.parse({}),
});
expect(result.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "connections:Main.storyboardc/scene.nib",
status: "partial",
omitted: 1,
}),
);
});
});
describe("keyed archive decoding and graph limits", () => {
it("preserves keyed archive UID identities, arrays, and control actions", () => {
const parsed = parseInterfaceBuilderRecords({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: [
"$null",
{
subviews: [{ UID: 2 }],
$class: { UID: 5 },
},
{ $class: { UID: 6 } },
{ $class: { UID: 7 } },
{
source: { UID: 3 },
destination: { UID: 2 },
label: "runAction:",
$class: { UID: 8 },
},
{ $classname: "UIView", $classes: ["UIView", "NSObject"] },
{ $classname: "UIButton", $classes: ["UIButton", "UIView"] },
{
$classname: "ViewController",
$classes: ["ViewController", "NSObject"],
},
{
$classname: "NSNibControlConnector",
$classes: ["NSNibControlConnector", "NSObject"],
},
[{ UID: 1 }],
],
$top: { root: { UID: 9 } },
});
expect(parsed.objects.map(({ id }) => id)).toContain("2");
expect(parsed.hierarchy).toMatchObject([
{
objectID: "1",
children: [{ objectID: "2" }],
},
]);
expect(parsed.connections).toContainEqual(
expect.objectContaining({
kind: "action",
source_id: "2",
destination_id: "3",
label: "runAction:",
}),
);
});
it("reports records omitted by the parser's hard object ceiling", () => {
const objects = Object.fromEntries(
Array.from({ length: 20_001 }, (_, index) => [
`object-${index}`,
{ class: "UIView" },
]),
);
const parsed = parseInterfaceBuilderRecords({
"com.apple.ibtool.document.objects": objects,
});
expect(parsed.objects).toHaveLength(20_000);
expect(parsed.objectCount).toBe(20_001);
expect(parsed.omittedObjects).toBe(1);
});
it("keeps hierarchy edges out of the connection edge budget", () => {
const relativePath = "Main.nib";
const result = buildInterfaceBuilderAnalysis({
targetSha256: hash,
toolVersion: "test",
documents: [
{
relativePath,
archiveSha256: hash,
documentKind: "nib",
raw: {
"com.apple.ibtool.document.objects": {
button: { class: "UIButton" },
controller: { customClass: "MainViewController" },
parent: { class: "UIView" },
child: { class: "UIView" },
},
"com.apple.ibtool.document.connections": {
button: [
{
type: "action",
label: "run:",
destinationId: "controller",
},
],
},
"com.apple.ibtool.document.hierarchy": [
{
objectID: "parent",
children: [
{ objectID: "child", children: [{ objectID: "button" }] },
],
},
],
},
},
],
limits: interfaceBuilderLimitsSchema.parse({
max_objects: 20,
max_connections: 1,
}),
});
expect(
result.graph.edges.filter(({ relation }) => relation === "target_action"),
).toHaveLength(3);
expect(result.graph.coverage).toContainEqual(
expect.objectContaining({
facet: `connections:${relativePath}`,
status: "complete",
omitted: 0,
}),
);
});
});
+880
View File
@@ -0,0 +1,880 @@
import { z } from "zod";
import {
nativeInvestigationGraphSchema,
nativeInvestigationTraceLimitsSchema,
} from "./nativeInvestigationGraph.js";
import { jsonValueSchema } from "./jsonValue.js";
const recordSchema = z.record(z.string(), z.unknown());
const objectNode = z.strictObject({
id: z.string().min(1),
kind: z.enum([
"view_controller",
"view",
"control",
"constraint",
"layout_guide",
"resource",
"placeholder",
"external_object",
"other",
]),
class_name: z.string().nullable(),
name: z.string().min(1),
attributes: z.record(z.string(), jsonValueSchema),
});
const connection = z.strictObject({
id: z.string().min(1),
kind: z.enum(["outlet", "action", "segue", "other"]),
source_id: z.string().min(1),
destination_id: z.string().nullable(),
label: z.string().nullable(),
attributes: z.record(z.string(), jsonValueSchema),
});
/** One compiled Interface Builder document projected from ibtool output. */
export const interfaceBuilderDocumentSchema = z.strictObject({
relative_path: z.string().min(1),
archive_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
document_kind: z.enum(["nib", "storyboard_scene"]),
object_count: z.number().int().nonnegative(),
connection_count: z.number().int().nonnegative(),
hierarchy_complete: z.boolean(),
});
/** Normalized read-only Interface Builder graph with archive provenance. */
export const interfaceBuilderAnalysisSchema = z.strictObject({
target_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
documents: z.array(interfaceBuilderDocumentSchema).max(64),
graph: nativeInvestigationGraphSchema,
limitations: z.array(z.string()),
});
type InterfaceBuilderAnalysis = z.infer<typeof interfaceBuilderAnalysisSchema>;
export type InterfaceBuilderObject = z.infer<typeof objectNode>;
export type InterfaceBuilderConnection = z.infer<typeof connection>;
export interface InterfaceBuilderDocumentInput {
readonly relativePath: string;
readonly archiveSha256: string;
readonly documentKind: "nib" | "storyboard_scene";
readonly raw: unknown;
}
/** Parse ibtool dictionaries or compiled keyed archives into typed records. */
export const parseInterfaceBuilderRecords = (
value: unknown,
): {
readonly objects: readonly InterfaceBuilderObject[];
readonly connections: readonly InterfaceBuilderConnection[];
readonly hierarchy: readonly unknown[];
readonly classes: Readonly<Record<string, unknown>>;
readonly objectCount: number;
readonly omittedObjects: number;
readonly omittedConnections: number;
} => {
const root = record(value);
const keyed = parseKeyedArchive(root);
if (keyed !== null) return keyed;
const objectsRaw = record(root["com.apple.ibtool.document.objects"]);
const connectionsRaw = record(root["com.apple.ibtool.document.connections"]);
const hierarchyRaw = root["com.apple.ibtool.document.hierarchy"];
const classesRaw = record(root["com.apple.ibtool.document.classes"]);
const objects: InterfaceBuilderObject[] = [];
const connections: InterfaceBuilderConnection[] = [];
let omittedConnections = 0;
const objectEntries = Object.entries(objectsRaw);
for (const [id, raw] of objectEntries.slice(0, 20_000)) {
const attributes = record(raw);
const className = firstString(
attributes["customClass"],
attributes["class"],
attributes["isa"],
);
const label = firstString(
attributes["label"],
attributes["title"],
attributes["name"],
className,
);
objects.push(
objectNode.parse({
id,
kind: classifyObject(className),
class_name: className,
name: label ?? id,
attributes: jsonSafeRecord(attributes),
}),
);
}
for (const [sourceId, rawConnections] of Object.entries(connectionsRaw)) {
if (!Array.isArray(rawConnections)) continue;
for (const [index, raw] of rawConnections.entries()) {
if (connections.length >= 40_000) {
omittedConnections += rawConnections.length - index;
break;
}
const item = record(raw);
const type = firstString(item.type, item.connectionType) ?? "unknown";
const destination = firstString(
item["destination-id"],
item.destinationId,
);
const label = firstString(item.label, item.selector, item.identifier);
connections.push(
connection.parse({
id: `${sourceId}:connection:${index}`,
kind: classifyConnection(type),
source_id: sourceId,
destination_id: destination,
label,
attributes: jsonSafeRecord(item),
}),
);
}
}
return {
objects,
connections,
hierarchy: Array.isArray(hierarchyRaw) ? hierarchyRaw.slice(0, 20_000) : [],
classes: classesRaw,
objectCount: objectEntries.length,
omittedObjects: Math.max(0, objectEntries.length - 20_000),
omittedConnections,
};
};
/** Project class and connection objects from an NSKeyedArchiver object table. */
const parseKeyedArchive = (
root: Record<string, unknown>,
): ReturnType<typeof parseInterfaceBuilderRecords> | null => {
if (root.$archiver !== "NSKeyedArchiver" || !Array.isArray(root.$objects))
return null;
const objectTable = root.$objects;
const resolve = (value: unknown, depth = 0): unknown => {
if (depth > 16) return null;
const uid = record(value).UID;
if (typeof uid === "number") {
const target = objectTable[uid];
const resolved = target === undefined ? null : resolve(target, depth + 1);
return typeof resolved === "object" &&
resolved !== null &&
!Array.isArray(resolved)
? { ...resolved, archiveUID: uid }
: resolved;
}
if (Array.isArray(value))
return value.map((item) => resolve(item, depth + 1));
if (typeof value !== "object" || value === null) return value;
const output: Record<string, unknown> = {};
for (const [key, child] of Object.entries(value)) {
if (key === "$class") {
const classObject = record(
typeof record(child).UID === "number"
? objectTable[record(child).UID as number]
: child,
);
output.className = firstString(classObject.$classname);
} else output[key] = resolve(child, depth + 1);
}
return output;
};
const toHierarchy = (value: unknown, depth = 0): unknown[] => {
if (depth > 32) return [];
if (Array.isArray(value))
return value.flatMap((item) => toHierarchy(item, depth + 1));
if (typeof value !== "object" || value === null) return [];
const item = record(value);
const objectId = firstString(
item.objectID,
item["object-id"],
item.id,
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
);
const children = Object.entries(item)
.filter(([key]) =>
/(?:subviews|contentview|childviewcontrollers|children|views)$/iu.test(
key,
),
)
.flatMap(([, child]) => toHierarchy(child, depth + 1));
return objectId === null ? children : [{ objectID: objectId, children }];
};
const objects: InterfaceBuilderObject[] = [];
const connections: InterfaceBuilderConnection[] = [];
let objectCount = 0;
let omittedConnections = 0;
for (const [index, raw] of objectTable.entries()) {
if (index === 0) continue;
const resolved = record(resolve(raw));
const className = firstString(resolved.className);
if (className === null) continue;
objectCount += 1;
const fields = Object.fromEntries(
Object.entries(resolved).filter(([key]) => key !== "className"),
);
const authoredId = firstString(fields.objectID, fields["object-id"]);
const objectId =
authoredId ??
(typeof fields.archiveUID === "number"
? String(fields.archiveUID)
: String(index));
const kind = classifyObject(className);
if (objects.length < 20_000)
objects.push(
objectNode.parse({
id: objectId,
kind,
class_name: className,
name:
firstString(
fields.label,
fields.title,
fields.identifier,
fields.accessibilityLabel,
className,
) ?? `object ${String(index)}`,
attributes: jsonSafeRecord(fields),
}),
);
if (
/(?:Outlet|Connection|Segue|ActionConnection|ControlConnector)/iu.test(
className,
)
) {
const controlAction = /ControlConnector/iu.test(className);
const source = firstObjectReference(
controlAction ? fields.destination : fields.source,
controlAction ? fields.to : fields.from,
controlAction ? fields.target : fields.owner,
);
if (source === null) continue;
const kind = controlAction ? "action" : classifyConnection(className);
const parsedConnection = connection.parse({
id: `archive:${objectId}`,
kind,
source_id: source,
destination_id: firstObjectReference(
controlAction ? fields.source : fields.destination,
controlAction ? fields.from : fields.to,
controlAction ? fields.owner : fields.target,
),
label: firstString(
fields.label,
fields.selector,
fields.identifier,
fields.action,
),
attributes: jsonSafeRecord(fields),
});
if (connections.length < 40_000) connections.push(parsedConnection);
else omittedConnections += 1;
}
}
const top = record(root.$top);
const topObject = resolve(top.root ?? top.UITopLevelObjectsKey);
const hierarchy = toHierarchy(topObject);
const classes = Object.fromEntries(
objectTable.flatMap((raw) => {
const item = record(raw);
const name = firstString(item.$classname);
return name === null
? []
: [
[
name,
{
superclasses: Array.isArray(item.$classes) ? item.$classes : [],
},
],
];
}),
);
return {
objects,
connections,
hierarchy,
classes,
objectCount,
omittedObjects: Math.max(0, objectCount - 20_000),
omittedConnections,
};
};
/** Build the graph returned by the static Interface Builder archive decoder. */
export const buildInterfaceBuilderAnalysis = (input: {
readonly targetSha256: string;
readonly toolVersion: string;
readonly documents: readonly InterfaceBuilderDocumentInput[];
readonly limits: z.output<typeof interfaceBuilderLimitsSchema>;
}) => {
const nodes = new Map<
string,
z.infer<
typeof import("./nativeInvestigationGraph.js").nativeInvestigationNodeSchema
>
>();
const edges: z.infer<
typeof import("./nativeInvestigationGraph.js").nativeInvestigationEdgeSchema
>[] = [];
const coverage = [] as Array<{
facet: string;
status: "complete" | "partial" | "unsupported" | "not_requested";
reason: string | null;
examined: number;
omitted: number;
}>;
const summaries: Array<z.infer<typeof interfaceBuilderDocumentSchema>> = [];
let truncated = false;
let omittedObjects = 0;
let omittedConnections = 0;
let omittedGraphNodes = 0;
for (const document of input.documents.slice(0, input.limits.max_documents)) {
const parsed = parseInterfaceBuilderRecords(document.raw);
const prefix = `ib:${document.relativePath}:`;
const rootId = `${prefix}root`;
const objectIds = new Set(parsed.objects.map(({ id }) => id));
const evidenceFor = (description: string) => [
{
kind: "interface_builder_resource" as const,
description: `${document.relativePath}: ${description}`,
location: { address: null, file_offset: null },
artifact_path: document.relativePath,
artifact_sha256: document.archiveSha256,
},
];
const addNode = (
node: z.infer<
typeof import("./nativeInvestigationGraph.js").nativeInvestigationNodeSchema
>,
): boolean => {
if (nodes.has(node.id)) return true;
if (nodes.size >= input.limits.max_objects) {
truncated = true;
omittedGraphNodes += 1;
return false;
}
nodes.set(node.id, node);
return true;
};
const rootNode = {
id: rootId,
kind:
document.documentKind === "nib"
? ("scene" as const)
: ("storyboard" as const),
name: document.relativePath,
location: null,
attributes: { document_kind: document.documentKind },
evidence: evidenceFor("compiled document"),
};
addNode(rootNode);
let documentObjectCount = 0;
for (const object of parsed.objects.slice(0, input.limits.max_objects)) {
const id = `${prefix}object:${object.id}`;
const kind = object.kind === "other" ? "unknown" : object.kind;
if (
!addNode({
id,
kind,
name: object.name,
location: null,
attributes: {
...object.attributes,
interface_builder_object_id: object.id,
class_name: object.class_name,
},
evidence: evidenceFor(`object ${object.id}`),
})
) {
break;
}
documentObjectCount += 1;
}
let connectionEdgeCount = 0;
let hierarchyEdgeCount = 0;
let documentOmittedConnections = 0;
let documentOmittedHierarchyEdges = 0;
const objectNodeId = (objectId: string): string =>
`${prefix}object:${objectId}`;
const addEdge = (
edge: (typeof edges)[number],
isHierarchy: boolean,
): void => {
if (
isHierarchy
? hierarchyEdgeCount >= input.limits.max_objects
: connectionEdgeCount >= input.limits.max_connections * 3
) {
truncated = true;
if (isHierarchy) {
documentOmittedHierarchyEdges += 1;
} else {
omittedConnections += 1;
documentOmittedConnections += 1;
}
return;
}
edges.push(edge);
if (isHierarchy) hierarchyEdgeCount += 1;
else connectionEdgeCount += 1;
};
const connect = (
id: string,
from: string,
to: string | null,
relation: "contains" | "outlet_to" | "target_action" | "segue_to",
description: string,
reason?: string,
) => {
if (!nodes.has(from)) {
truncated = true;
if (relation === "contains" && id.includes(":hierarchy:")) {
documentOmittedHierarchyEdges += 1;
} else {
omittedConnections += 1;
documentOmittedConnections += 1;
}
return;
}
const evidence = evidenceFor(description);
const unresolvedEndpoint = to !== null && !nodes.has(to);
if (unresolvedEndpoint) truncated = true;
const edgeTo = unresolvedEndpoint ? null : to;
const edgeReason = unresolvedEndpoint
? "endpoint_omitted_by_object_limit"
: (reason ?? "destination_unresolved");
addEdge(
edgeTo === null
? {
id,
from,
to: null,
relation,
resolution: "unresolved",
reason: edgeReason,
evidence,
limitations: [],
}
: {
id,
from,
to: edgeTo,
relation,
resolution: "observed",
evidence,
limitations: [],
},
relation === "contains" && id.includes(":hierarchy:"),
);
};
let hierarchyCount = 0;
let hierarchyTruncated = false;
const visitHierarchy = (value: unknown, parentId: string): void => {
if (hierarchyCount >= input.limits.max_objects) {
truncated = true;
hierarchyTruncated = true;
return;
}
if (Array.isArray(value)) {
for (const child of value) visitHierarchy(child, parentId);
return;
}
if (typeof value !== "object" || value === null) return;
const item = record(value);
const objectId = firstString(
item.objectID,
item["object-id"],
item.id,
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
);
let nextParent = parentId;
if (objectId !== null) {
const known = objectIds.has(objectId);
const childId = known
? objectNodeId(objectId)
: `${prefix}unknown:${objectId}`;
if (
!known &&
!addNode({
id: childId,
kind: "unknown",
name: firstString(item.label, item.name) ?? objectId,
location: null,
attributes: { interface_builder_object_id: objectId },
evidence: evidenceFor(`hierarchy item ${objectId}`),
})
)
hierarchyTruncated = true;
connect(
`${prefix}hierarchy:${parentId}:${childId}`,
parentId,
childId,
"contains",
`hierarchy ${objectId}`,
);
nextParent = childId;
hierarchyCount += 1;
}
visitHierarchy(item.children, nextParent);
};
for (const hierarchy of parsed.hierarchy) visitHierarchy(hierarchy, rootId);
for (const [index, item] of parsed.connections
.slice(0, input.limits.max_connections)
.entries()) {
const sourceId = objectNodeId(item.source_id);
if (!nodes.has(sourceId))
addNode({
id: sourceId,
kind: "unknown",
name: item.source_id,
location: null,
attributes: { interface_builder_object_id: item.source_id },
evidence: evidenceFor(`connection source ${item.source_id}`),
});
const destinationId =
item.destination_id === null ? null : objectNodeId(item.destination_id);
if (item.kind === "action") {
const actionId = `${prefix}action:${item.id}`;
const selectorId =
item.label === null
? null
: `${prefix}selector:${item.destination_id ?? "unknown"}:${item.label}`;
addNode({
id: actionId,
kind: "action",
name: item.label ?? "unlabeled action",
location: null,
attributes: {
selector: item.label,
destination_id: item.destination_id,
...item.attributes,
},
evidence: evidenceFor(`action ${item.id}`),
});
connect(
`${actionId}:source`,
sourceId,
actionId,
"target_action",
`action source ${item.source_id}`,
);
if (selectorId !== null) {
addNode({
id: selectorId,
kind: "objc_selector",
name: item.label ?? "unknown selector",
location: null,
attributes: { destination_id: item.destination_id },
evidence: evidenceFor(`selector ${item.label}`),
});
connect(
`${actionId}:selector`,
actionId,
selectorId,
"target_action",
`selector ${item.label}`,
);
}
if (destinationId === null || !nodes.has(destinationId))
connect(
`${actionId}:destination`,
actionId,
null,
"target_action",
`action destination ${item.destination_id ?? "missing"}`,
"destination_object_missing_or_external",
);
else
connect(
`${actionId}:destination`,
actionId,
destinationId,
"target_action",
`action destination ${item.destination_id}`,
);
} else if (item.kind === "outlet") {
const outletId = `${prefix}outlet:${item.id}`;
addNode({
id: outletId,
kind: "outlet",
name: item.label ?? item.id,
location: null,
attributes: item.attributes,
evidence: evidenceFor(`outlet ${item.id}`),
});
connect(
`${outletId}:source`,
sourceId,
outletId,
"contains",
`outlet source ${item.source_id}`,
);
if (destinationId === null || !nodes.has(destinationId))
connect(
`${outletId}:destination`,
outletId,
null,
"outlet_to",
`outlet ${item.label ?? item.id}`,
"destination_object_missing_or_external",
);
else
connect(
`${outletId}:destination`,
outletId,
destinationId,
"outlet_to",
`outlet ${item.label ?? item.id}`,
);
} else if (item.kind === "segue") {
if (destinationId === null || !nodes.has(destinationId))
connect(
`${prefix}segue:${index}`,
sourceId,
null,
"segue_to",
`segue ${item.label ?? item.id}`,
"destination_scene_missing",
);
else
connect(
`${prefix}segue:${index}`,
sourceId,
destinationId,
"segue_to",
`segue ${item.label ?? item.id}`,
);
} else {
connect(
`${prefix}unknown-connection:${index}`,
sourceId,
null,
"contains",
`connection type ${item.kind}`,
"unsupported_connection_type",
);
}
}
const objectsTruncated =
parsed.omittedObjects > 0 || parsed.objects.length > documentObjectCount;
const parserOmittedConnections = parsed.omittedConnections;
const connectionsTruncated =
parserOmittedConnections > 0 ||
parsed.connections.length > input.limits.max_connections;
omittedObjects +=
parsed.omittedObjects +
Math.max(0, parsed.objects.length - documentObjectCount);
omittedConnections +=
Math.max(0, parsed.connections.length - input.limits.max_connections) +
parserOmittedConnections;
summaries.push({
relative_path: document.relativePath,
archive_sha256: document.archiveSha256,
document_kind: document.documentKind,
object_count: parsed.objectCount,
connection_count: parsed.connections.length + parserOmittedConnections,
hierarchy_complete: parsed.hierarchy.length > 0 && !truncated,
});
coverage.push(
{
facet: `objects:${document.relativePath}`,
status: objectsTruncated ? "partial" : "complete",
reason: objectsTruncated ? "object_limit_reached" : null,
examined: documentObjectCount,
omitted:
parsed.omittedObjects +
Math.max(0, parsed.objects.length - documentObjectCount),
},
{
facet: `connections:${document.relativePath}`,
status:
connectionsTruncated || documentOmittedConnections > 0
? "partial"
: "complete",
reason:
connectionsTruncated || documentOmittedConnections > 0
? "connection_limit_reached"
: null,
examined: Math.min(
parsed.connections.length,
input.limits.max_connections,
),
omitted:
Math.max(
0,
parsed.connections.length - input.limits.max_connections,
) +
documentOmittedConnections +
parserOmittedConnections,
},
{
facet: `hierarchy:${document.relativePath}`,
status:
parsed.hierarchy.length === 0
? "unsupported"
: hierarchyTruncated || documentOmittedHierarchyEdges > 0
? "partial"
: "complete",
reason:
parsed.hierarchy.length === 0
? "archive_hierarchy_not_decoded"
: hierarchyTruncated || documentOmittedHierarchyEdges > 0
? "graph_limit_reached"
: null,
examined: hierarchyCount,
omitted: documentOmittedHierarchyEdges,
},
{
facet: `classes:${document.relativePath}`,
status:
Object.keys(parsed.classes).length > 0 ? "complete" : "unsupported",
reason:
Object.keys(parsed.classes).length > 0
? null
: "archive_class_table_not_decoded",
examined: Object.keys(parsed.classes).length,
omitted: 0,
},
);
}
if (input.documents.length > input.limits.max_documents) {
truncated = true;
coverage.push({
facet: "documents",
status: "partial",
reason: "document_limit_reached",
examined: input.limits.max_documents,
omitted: input.documents.length - input.limits.max_documents,
});
}
if (omittedGraphNodes > 0)
coverage.push({
facet: "graph_nodes",
status: "partial",
reason: "max_objects_reached",
examined: nodes.size,
omitted: omittedGraphNodes,
});
return interfaceBuilderAnalysisSchema.parse({
target_sha256: input.targetSha256,
documents: summaries,
graph: {
target_sha256: input.targetSha256,
provider: {
id: "rea-artifact-graph",
version: "1",
tool_version: input.toolVersion,
},
nodes: [...nodes.values()],
edges,
coverage,
truncated: truncated || omittedObjects > 0 || omittedConnections > 0,
},
limitations: [
"The graph reflects recognized Interface Builder archive fields and connections; omitted private or unrecognized archive fields remain unknown.",
"A target-action selector is an Interface Builder observation and does not prove that the binary contains a matching implementation.",
"Decoded constraints, runtime visibility, dynamic UI creation, and pixel geometry are not inferred from these archives.",
],
});
};
const record = (value: unknown): Record<string, unknown> =>
recordSchema.safeParse(value).data ?? {};
const firstString = (...values: unknown[]): string | null => {
for (const value of values)
if (typeof value === "string" && value.length > 0) return value;
return null;
};
const firstObjectReference = (...values: unknown[]): string | null => {
for (const value of values) {
const direct = firstString(value);
if (direct !== null) return direct;
const item = record(value);
const objectId = firstString(
item.objectID,
item["object-id"],
item.id,
item.identifier,
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
);
if (objectId !== null) return objectId;
}
return null;
};
const classifyObject = (
className: string | null,
): InterfaceBuilderObject["kind"] => {
const value = className?.toLowerCase() ?? "";
if (value.includes("constraint")) return "constraint";
if (value.includes("layoutguide")) return "layout_guide";
if (value.includes("placeholder") || value.includes("firstresponder"))
return "placeholder";
if (
value.includes("image") ||
value.includes("color") ||
value.includes("font")
)
return "resource";
if (value.includes("external")) return "external_object";
if (value.includes("viewcontroller") || value.includes("windowcontroller"))
return "view_controller";
if (
value.includes("button") ||
value.includes("control") ||
value.includes("textfield") ||
value.includes("slider") ||
value.includes("menuitem")
)
return "control";
if (
value.includes("view") ||
value.includes("window") ||
value.includes("cell")
)
return "view";
return "other";
};
const classifyConnection = (
type: string,
): InterfaceBuilderConnection["kind"] => {
const value = type.toLowerCase();
if (value.includes("outlet")) return "outlet";
if (value.includes("action")) return "action";
if (value.includes("segue")) return "segue";
return "other";
};
const jsonSafeRecord = (value: Record<string, unknown>) =>
Object.fromEntries(
Object.entries(value).flatMap(([key, item]) => {
try {
const serialized = JSON.stringify(item);
if (serialized === undefined) return [];
const parsed = jsonValueSchema.safeParse(JSON.parse(serialized));
return parsed.success ? [[key, parsed.data]] : [];
} catch {
return [];
}
}),
);
/** Hard limits shared by Interface Builder archive aggregation and tracing. */
export const interfaceBuilderLimitsSchema = z.strictObject({
max_documents: z.number().int().min(1).max(64).default(64),
max_objects: z.number().int().min(1).max(20_000).default(20_000),
max_connections: z.number().int().min(1).max(40_000).default(40_000),
trace: nativeInvestigationTraceLimitsSchema.default({
max_depth: 8,
max_nodes: 250,
max_edges: 500,
}),
});
+1 -1
View File
@@ -52,6 +52,7 @@ const jumpTableDataSourceSchema = z
provenance: z.enum([
"ghidra-decompiler-load-table",
"ghidra-dispatch-block-data-reference",
"ghidra-aarch64-byte-table",
]),
entry_size_bytes: z.number().int().min(1).nullable(),
entry_count: z.number().int().min(0).nullable(),
@@ -64,7 +65,6 @@ const jumpTableMappingSchema = z
.object({
case_value: z.number().int().nullable(),
target_address: z.string().min(1),
data_addresses: z.array(z.string().min(1)),
confidence: inferenceConfidenceSchema,
evidence: z.array(inferenceEvidenceSchema).min(1),
})
+344
View File
@@ -0,0 +1,344 @@
import { describe, expect, it } from "vitest";
import {
nativeInvestigationGraphSchema,
joinInterfaceBuilderDispatch,
traceNativeInvestigationGraph,
} from "./nativeInvestigationGraph.js";
import {
buildInterfaceBuilderAnalysis,
interfaceBuilderLimitsSchema,
} from "./interfaceBuilderGraph.js";
const evidence = [
{
kind: "reference" as const,
description: "fixture edge",
location: { address: "0x1000", file_offset: 0 },
artifact_path: null,
artifact_sha256: null,
},
];
const graph = nativeInvestigationGraphSchema.parse({
target_sha256: "a".repeat(64),
provider: { id: "fixture", version: "1", tool_version: "test" },
nodes: [
{
id: "a",
kind: "control",
name: "button",
location: null,
attributes: {},
evidence,
},
{
id: "b",
kind: "function",
name: "handler",
location: null,
attributes: {},
evidence,
},
{
id: "c",
kind: "state_value",
name: "balance",
location: null,
attributes: {},
evidence,
},
],
edges: [
{
id: "1",
from: "a",
to: "b",
relation: "target_action",
resolution: "observed",
evidence,
limitations: [],
},
{
id: "2",
from: "b",
to: "c",
relation: "writes",
resolution: "inferred",
evidence,
limitations: ["static slice"],
},
{
id: "3",
from: "b",
to: null,
relation: "swift_witness_dispatch",
resolution: "unresolved",
reason: "witness table unavailable",
evidence,
limitations: [],
},
],
coverage: [],
truncated: false,
});
describe("native investigation graph traces", () => {
it("retains observed and inferred route edges plus unresolved dispatch", () => {
const trace = traceNativeInvestigationGraph(graph, { start: "a" });
expect(trace.nodes.map(({ id }) => id)).toEqual(["a", "b", "c"]);
expect(trace.edges.map(({ resolution }) => resolution)).toEqual([
"observed",
"inferred",
]);
expect(trace.unresolved).toHaveLength(1);
expect(trace.unresolved[0]).toMatchObject({
reason: "witness table unavailable",
});
const fromHandler = traceNativeInvestigationGraph(graph, { start: "b" });
expect(fromHandler.unresolved[0]).toMatchObject({
relation: "swift_witness_dispatch",
reason: "witness table unavailable",
});
});
it("stops at explicit node and edge bounds", () => {
const trace = traceNativeInvestigationGraph(graph, {
start: "a",
limits: { max_nodes: 2, max_edges: 1 },
});
expect(trace.truncated).toBe(true);
expect(trace.reason).toBe("max_edges_reached");
expect(trace.nodes).toHaveLength(2);
});
it("does not retain a resolved edge beyond the depth boundary", () => {
const trace = traceNativeInvestigationGraph(graph, {
start: "a",
limits: { max_depth: 0 },
});
expect(trace.nodes.map(({ id }) => id)).toEqual(["a"]);
expect(trace.edges).toEqual([]);
expect(trace.truncated).toBe(true);
expect(trace.reason).toBe("max_depth_reached");
});
it("reports an unknown seed instead of treating it as an empty answer", () => {
expect(
traceNativeInvestigationGraph(graph, { start: "missing" }),
).toMatchObject({
reason: "start_node_missing",
nodes: [],
truncated: false,
});
});
});
describe("Interface Builder dispatch joins", () => {
it("joins a UI action to the target controller class, not the source control", () => {
const ui = buildInterfaceBuilderAnalysis({
targetSha256: "b".repeat(64),
toolVersion: "test",
documents: [
{
relativePath: "Main.storyboardc/scene.nib/objects.nib",
archiveSha256: "c".repeat(64),
documentKind: "storyboard_scene",
raw: {
"com.apple.ibtool.document.objects": {
controller: { customClass: "BuildViewController" },
button: { class: "UIButton", title: "Build" },
},
"com.apple.ibtool.document.connections": {
button: [
{
type: "action",
label: "buildTapped:",
destinationId: "controller",
},
],
},
},
},
],
limits: interfaceBuilderLimitsSchema.parse({}),
});
const joined = joinInterfaceBuilderDispatch(ui.graph, {
objc_classes: [],
objc_protocols: [],
swift_decls: [],
objc_ivars: [],
objc_dispatch_implementations: [
{
class_name: "BuildViewController",
selector: "buildTapped:",
method_type: "instance",
implementation_address: "0x1000",
location: { address: "0x1000", file_offset: 0 },
decode: { status: "decoded", reason: null },
evidence,
},
],
swift_conformances: [],
swift_dispatch_slots: [],
swift_symbols: [],
relative_pointers: [],
coverage: [],
db_save_result: null,
});
expect(joined.edges).toContainEqual(
expect.objectContaining({
relation: "objc_dispatch",
resolution: "inferred",
to: "native:function:0x1000",
}),
);
});
it("chooses an instance method and matches category-qualified owner names", () => {
const ui = buildInterfaceBuilderAnalysis({
targetSha256: "b".repeat(64),
toolVersion: "test",
documents: [
{
relativePath: "Main.nib",
archiveSha256: "c".repeat(64),
documentKind: "nib",
raw: {
"com.apple.ibtool.document.objects": {
controller: { customClass: "BuildViewController" },
button: { class: "UIButton" },
},
"com.apple.ibtool.document.connections": {
button: [
{
type: "action",
label: "buildTapped:",
destinationId: "controller",
},
],
},
},
},
],
limits: interfaceBuilderLimitsSchema.parse({}),
});
const metadata = {
objc_classes: [],
objc_protocols: [],
swift_decls: [],
objc_ivars: [],
objc_dispatch_implementations: [
{
class_name: "BuildViewController",
selector: "buildTapped:",
method_type: "class" as const,
implementation_address: "0x9999",
location: { address: "0x9999", file_offset: 0 },
decode: { status: "decoded" as const, reason: null },
evidence,
},
{
class_name: "BuildViewController(BuilderActions)",
selector: "buildTapped:",
method_type: "instance" as const,
implementation_address: "0x1000",
location: { address: "0x1000", file_offset: 0 },
decode: { status: "decoded" as const, reason: null },
evidence,
},
],
swift_conformances: [],
swift_dispatch_slots: [],
swift_symbols: [],
relative_pointers: [],
coverage: [],
db_save_result: null,
};
const joined = joinInterfaceBuilderDispatch(ui.graph, metadata);
expect(joined.edges).toContainEqual(
expect.objectContaining({
relation: "objc_dispatch",
resolution: "inferred",
to: "native:function:0x1000",
}),
);
expect(joined.edges).not.toContainEqual(
expect.objectContaining({ to: "native:function:0x9999" }),
);
});
});
describe("placeholder dispatch joins", () => {
it("keeps a unique selector match inferred when the receiver is only a placeholder", () => {
const ui = buildInterfaceBuilderAnalysis({
targetSha256: "b".repeat(64),
toolVersion: "test",
documents: [
{
relativePath: "Main.storyboardc/scene.nib/objects.nib",
archiveSha256: "c".repeat(64),
documentKind: "storyboard_scene",
raw: {
"com.apple.ibtool.document.objects": {
receiver: { class: "NSStoryboardPlaceholder" },
button: { class: "UIButton", title: "Build" },
},
"com.apple.ibtool.document.connections": {
button: [
{
type: "action",
label: "buildTapped:",
destinationId: "receiver",
},
],
},
},
},
],
limits: interfaceBuilderLimitsSchema.parse({}),
});
const joined = joinInterfaceBuilderDispatch(ui.graph, {
objc_classes: [],
objc_protocols: [],
swift_decls: [],
objc_ivars: [],
objc_dispatch_implementations: [
{
class_name: "BuildViewController",
selector: "buildTapped:",
method_type: "instance",
implementation_address: "0x1000",
location: { address: "0x1000", file_offset: null },
decode: { status: "partial", reason: "symbol_name_only" },
evidence,
},
],
swift_conformances: [],
swift_dispatch_slots: [],
swift_symbols: [],
relative_pointers: [],
coverage: [],
db_save_result: null,
});
expect(joined.edges).toContainEqual(
expect.objectContaining({
relation: "objc_dispatch",
resolution: "inferred",
to: "native:function:0x1000",
limitations: [
expect.stringContaining("receiver is an unresolved placeholder"),
],
}),
);
expect(joined.coverage).toContainEqual(
expect.objectContaining({
facet: "ui_to_objc_dispatch",
status: "partial",
reason: "some_action_receivers_remain_placeholders",
}),
);
});
});
+424
View File
@@ -0,0 +1,424 @@
import { z } from "zod";
import {
nativeMetadataEvidenceSchema,
nativeMetadataLocationSchema,
type ObjcSwiftMetadata,
} from "./objcSwiftMetadata.js";
import { jsonValueSchema } from "./jsonValue.js";
/** Entity kinds shared by decoded Interface Builder archives and native code. */
export const nativeInvestigationNodeKindSchema = z.enum([
"storyboard",
"scene",
"view_controller",
"view",
"control",
"constraint",
"layout_guide",
"resource",
"placeholder",
"external_object",
"outlet",
"action",
"segue",
"objc_class",
"objc_selector",
"swift_declaration",
"swift_requirement",
"dispatch_table",
"dispatch_slot",
"thunk",
"function",
"state_value",
"global_value",
"field",
"branch_condition",
"sink",
"unknown",
]);
type NativeInvestigationNodeKind = z.infer<
typeof nativeInvestigationNodeKindSchema
>;
/** One UI, dispatch, or value entity with optional decoded source location. */
export const nativeInvestigationNodeSchema = z.strictObject({
id: z.string().min(1),
kind: nativeInvestigationNodeKindSchema,
name: z.string().min(1),
location: nativeMetadataLocationSchema.nullable(),
attributes: z.record(z.string(), jsonValueSchema),
evidence: z.array(nativeMetadataEvidenceSchema),
});
type NativeInvestigationNode = z.infer<typeof nativeInvestigationNodeSchema>;
/** Relationship types preserve UI wiring, native dispatch, and value flow. */
export const nativeInvestigationRelationSchema = z.enum([
"contains",
"outlet_to",
"target_action",
"segue_to",
"objc_dispatch",
"swift_witness_dispatch",
"swift_vtable_dispatch",
"thunk_to",
"direct_call",
"reads",
"writes",
"flows_to",
"controls",
]);
type NativeInvestigationRelation = z.infer<
typeof nativeInvestigationRelationSchema
>;
/** An observed, inferred, or unresolved relationship between entities. */
export const nativeInvestigationEdgeSchema = z.discriminatedUnion(
"resolution",
[
z.strictObject({
id: z.string().min(1),
from: z.string().min(1),
to: z.string().min(1),
relation: nativeInvestigationRelationSchema,
resolution: z.enum(["observed", "inferred"]),
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
limitations: z.array(z.string()),
}),
z.strictObject({
id: z.string().min(1),
from: z.string().min(1),
to: z.null(),
relation: nativeInvestigationRelationSchema,
resolution: z.literal("unresolved"),
reason: z.string().min(1),
evidence: z.array(nativeMetadataEvidenceSchema),
limitations: z.array(z.string()),
}),
],
);
export type NativeInvestigationEdge = z.infer<
typeof nativeInvestigationEdgeSchema
>;
/** Explicit inventory coverage so missing graph parts are not read as absent. */
export const nativeInvestigationCoverageSchema = z.strictObject({
facet: z.string().min(1),
status: z.enum(["complete", "partial", "unsupported", "not_requested"]),
reason: z.string().nullable(),
examined: z.number().int().nonnegative(),
omitted: z.number().int().nonnegative(),
});
/** Complete provider-neutral graph bound to one immutable target identity. */
export const nativeInvestigationGraphSchema = z.strictObject({
target_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
provider: z.strictObject({
id: z.string().min(1),
version: z.string().nullable(),
tool_version: z.string().min(1),
}),
nodes: z.array(nativeInvestigationNodeSchema),
edges: z.array(nativeInvestigationEdgeSchema),
coverage: z.array(nativeInvestigationCoverageSchema),
truncated: z.boolean(),
});
export type NativeInvestigationGraph = z.infer<
typeof nativeInvestigationGraphSchema
>;
/** Caller-controlled hard limits for a bounded relationship trace. */
export const nativeInvestigationTraceLimitsSchema = z.strictObject({
max_depth: z.number().int().min(0).max(32).default(8),
max_nodes: z.number().int().min(1).max(2000).default(250),
max_edges: z.number().int().min(1).max(5000).default(500),
});
export type NativeInvestigationTraceLimits = z.infer<
typeof nativeInvestigationTraceLimitsSchema
>;
/** Exact graph route and any unresolved edges encountered along the route. */
export const nativeInvestigationTraceSchema = z.strictObject({
target_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
provider: nativeInvestigationGraphSchema.shape.provider,
start: z.string().min(1),
direction: z.enum(["forward", "backward"]),
nodes: z.array(nativeInvestigationNodeSchema),
edges: z.array(nativeInvestigationEdgeSchema),
unresolved: z.array(nativeInvestigationEdgeSchema),
reached_depth: z.number().int().nonnegative(),
truncated: z.boolean(),
reason: z.string().nullable(),
coverage: z.array(nativeInvestigationCoverageSchema),
limitations: z.array(z.string().min(1)),
});
export type NativeInvestigationTrace = z.infer<
typeof nativeInvestigationTraceSchema
>;
/** Trace known relationships while retaining unknown indirect dispatch edges. */
export const traceNativeInvestigationGraph = (
graph: NativeInvestigationGraph,
input: {
readonly start: string;
readonly direction?: "forward" | "backward";
readonly limits?: Partial<NativeInvestigationTraceLimits>;
},
): NativeInvestigationTrace => {
const start = graph.nodes.find(({ id }) => id === input.start);
if (start === undefined)
return {
target_sha256: graph.target_sha256,
provider: graph.provider,
start: input.start,
direction: input.direction ?? "forward",
nodes: [],
edges: [],
unresolved: [],
reached_depth: 0,
truncated: false,
reason: "start_node_missing",
coverage: graph.coverage,
limitations: [
"The requested start node is absent from the supplied graph.",
],
};
const limits = nativeInvestigationTraceLimitsSchema.parse(input.limits ?? {});
const direction = input.direction ?? "forward";
const nodeById = new Map(graph.nodes.map((node) => [node.id, node]));
const visited = new Set([start.id]);
const selectedEdges = new Map<string, NativeInvestigationEdge>();
const unresolved = new Map<string, NativeInvestigationEdge>();
const queue: Array<{ id: string; depth: number }> = [
{ id: start.id, depth: 0 },
];
let reachedDepth = 0;
let truncated = false;
let reason: string | null = null;
while (queue.length > 0) {
const current = queue.shift();
if (current === undefined) break;
reachedDepth = Math.max(reachedDepth, current.depth);
const adjacent = graph.edges.filter((edge) =>
direction === "forward"
? edge.from === current.id
: edge.to === current.id,
);
for (const edge of adjacent) {
if (selectedEdges.size + unresolved.size >= limits.max_edges) {
truncated = true;
reason = "max_edges_reached";
break;
}
if (edge.resolution === "unresolved") {
unresolved.set(edge.id, edge);
continue;
}
const nextId = direction === "forward" ? edge.to : edge.from;
const alreadyVisited = visited.has(nextId);
if (!alreadyVisited && current.depth >= limits.max_depth) {
truncated = true;
reason ??= "max_depth_reached";
continue;
}
selectedEdges.set(edge.id, edge);
if (alreadyVisited) continue;
if (visited.size >= limits.max_nodes) {
truncated = true;
reason = "max_nodes_reached";
break;
}
if (!nodeById.has(nextId)) {
truncated = true;
reason ??= "edge_target_missing_from_graph";
continue;
}
visited.add(nextId);
queue.push({ id: nextId, depth: current.depth + 1 });
}
if (reason === "max_edges_reached" || reason === "max_nodes_reached") break;
}
return {
target_sha256: graph.target_sha256,
provider: graph.provider,
start: start.id,
direction,
nodes: [...visited].flatMap((id) => {
const node = nodeById.get(id);
return node === undefined ? [] : [node];
}),
edges: [...selectedEdges.values()],
unresolved: [...unresolved.values()],
reached_depth: reachedDepth,
truncated,
reason,
coverage: graph.coverage,
limitations: [
"Static analysis does not prove that an authored UI action is reachable or executed at runtime.",
"Provider call graphs may omit unresolved indirect dispatch.",
],
};
};
/** Join authored UI actions to exact Objective-C method symbols when known. */
export const joinInterfaceBuilderDispatch = (
graph: NativeInvestigationGraph,
metadata: ObjcSwiftMetadata,
): NativeInvestigationGraph => {
const nodes = new Map(graph.nodes.map((node) => [node.id, node]));
const edges = [...graph.edges];
const existingEdgeIds = new Set(edges.map(({ id }) => id));
const selectorNodesByAction = new Map<string, string>();
for (const edge of edges) {
if (
edge.resolution === "observed" &&
edge.relation === "target_action" &&
nodes.get(edge.to)?.kind === "objc_selector"
)
selectorNodesByAction.set(edge.from, edge.to);
}
let examined = 0;
let resolved = 0;
let inferredWithoutOwnerClass = 0;
for (const action of nodes.values()) {
if (action.kind !== "action") continue;
const selector = action.attributes.selector;
if (typeof selector !== "string" || selector.length === 0) continue;
const selectorNodeId = selectorNodesByAction.get(action.id);
if (selectorNodeId === undefined) continue;
const destinationEdge = edges.find(
(edge) =>
edge.resolution === "observed" &&
edge.relation === "target_action" &&
edge.from === action.id &&
["view_controller", "placeholder", "external_object"].includes(
nodes.get(edge.to)?.kind ?? "",
),
);
if (
destinationEdge === undefined ||
destinationEdge.resolution !== "observed"
)
continue;
const owner = nodes.get(destinationEdge.to);
examined += 1;
const declaredClass = owner?.attributes.class_name;
const className =
typeof declaredClass === "string" &&
declaredClass.length > 0 &&
owner?.kind === "view_controller"
? declaredClass
: null;
const candidates = metadata.objc_dispatch_implementations.filter(
(candidate) =>
candidate.method_type === "instance" &&
candidate.selector === selector &&
(className === null ||
candidate.class_name.replace(/\([^)]*\)$/u, "") === className),
);
const implementation = candidates.length === 1 ? candidates[0] : undefined;
const edgeId = `dispatch:${selectorNodeId}:${className ?? owner?.id ?? "unknown"}:${selector}`;
if (existingEdgeIds.has(edgeId)) continue;
existingEdgeIds.add(edgeId);
if (
implementation === undefined ||
implementation.implementation_address === null
) {
edges.push({
id: edgeId,
from: selectorNodeId,
to: null,
relation: "objc_dispatch",
resolution: "unresolved",
reason:
candidates.length > 1
? "selector_has_multiple_candidate_implementations"
: "implementation_not_resolved_from_provider_symbols",
evidence: [
...action.evidence,
...candidates.flatMap(({ evidence }) => evidence),
],
limitations: [
"A missing or ambiguous symbol match does not prove that the runtime implementation is absent.",
],
});
continue;
}
const functionId = `native:function:${implementation.implementation_address}`;
if (!nodes.has(functionId))
nodes.set(functionId, {
id: functionId,
kind: "function",
name: `${implementation.class_name} ${selector}`,
location: {
address: implementation.implementation_address,
file_offset: implementation.location.file_offset,
},
attributes: {
class_name: implementation.class_name,
selector,
method_type: implementation.method_type,
},
evidence: implementation.evidence,
});
edges.push({
id: edgeId,
from: selectorNodeId,
to: functionId,
relation: "objc_dispatch",
resolution: "inferred",
evidence: [...action.evidence, ...implementation.evidence],
limitations:
className === null
? [
"The selector uniquely matches a symbolized method, but the Interface Builder receiver is an unresolved placeholder; runtime dispatch was not observed.",
]
: [
"The Interface Builder target and symbolized method agree on class and selector; runtime dispatch and dynamically supplied targets were not observed.",
],
});
resolved += 1;
if (className === null) inferredWithoutOwnerClass += 1;
}
return nativeInvestigationGraphSchema.parse({
...graph,
provider: {
id: "rea-native-investigation",
version: null,
tool_version: "rea-native-investigation/1",
},
nodes: [...nodes.values()],
edges,
coverage: [
...graph.coverage,
{
facet: "ui_to_objc_dispatch",
status:
examined === resolved && inferredWithoutOwnerClass === 0
? "complete"
: "partial",
reason:
examined === resolved && inferredWithoutOwnerClass === 0
? null
: inferredWithoutOwnerClass > 0
? "some_action_receivers_remain_placeholders"
: "some_action_implementations_not_resolved",
examined,
omitted: examined - resolved + inferredWithoutOwnerClass,
},
],
});
};
+66
View File
@@ -0,0 +1,66 @@
import { z } from "zod";
/** A decompiler p-code varnode location or literal value. */
export const nativeVarnodeSchema = z.strictObject({
kind: z.enum(["constant", "address", "register", "unique", "other"]),
size_bytes: z.number().int().nonnegative(),
location: z.string().nullable(),
constant_hex: z
.string()
.regex(/^[0-9a-f]+$/u)
.nullable(),
});
/** One high-p-code operation recovered for a single function. */
export const nativePcodeOperationSchema = z.strictObject({
id: z.string().min(1),
address: z.string().min(1),
sequence: z.number().int().nonnegative(),
opcode: z.string().min(1),
is_dead: z.boolean(),
inputs: z.array(nativeVarnodeSchema),
output: nativeVarnodeSchema.nullable(),
});
/** A def-use relation between operations in one recovered HighFunction. */
export const nativePcodeDefUseSchema = z.strictObject({
definition: z.string().min(1),
use: z.string().min(1),
input_index: z.number().int().nonnegative(),
});
/** Memory and branch operations as directly named by Ghidra p-code. */
export const nativePcodeEffectSchema = z.strictObject({
operation: z.string().min(1),
kind: z.enum([
"memory_read",
"memory_write",
"conditional_branch",
"indirect_branch",
"direct_call",
"indirect_call",
]),
operand_input: z.number().int().nonnegative().nullable(),
value_input: z.number().int().nonnegative().nullable(),
});
/** Bounded data-flow observations; STORE is not necessarily a persistent/global write. */
export const nativeValueFlowSchema = z.discriminatedUnion("available", [
z.strictObject({
available: z.literal(true),
provenance: z.literal("ghidra-high-pcode"),
operations: z.array(nativePcodeOperationSchema),
def_use: z.array(nativePcodeDefUseSchema),
effects: z.array(nativePcodeEffectSchema),
truncated: z.boolean(),
omitted_operations_lower_bound: z.number().int().nonnegative(),
known_omitted_inputs: z.number().int().nonnegative(),
known_omitted_edges: z.number().int().nonnegative(),
limitations: z.array(z.string().min(1)),
}),
z.strictObject({
available: z.literal(false),
reason: z.string().min(1),
limitations: z.array(z.string().min(1)),
}),
]);
+28
View File
@@ -5,6 +5,7 @@ import {
isDbSaveComplete,
isGetterSelector,
isSetterSelector,
inspectNativeDispatchMetadata,
objcSwiftMetadataSchema,
propertyNameFromSelector,
swiftDeclsByKind,
@@ -101,4 +102,31 @@ describe("ObjC/Swift metadata", () => {
const result = objcSwiftMetadataSchema.safeParse(meta);
expect(result.success).toBe(true);
});
it("bounds symbol projections and labels symbol-only metadata as partial", () => {
const result = inspectNativeDispatchMetadata(
[
{ address: "ram:0x1000", name: "OBJC_CLASS_$_StoreController" },
{ address: "ram:0x2000", name: "-[StoreController buildTapped:]" },
{ address: "ram:0x3000", name: "$s5Store7launchyyF" },
],
2,
);
expect(result.objc_classes).toHaveLength(1);
expect(result.objc_dispatch_implementations).toHaveLength(1);
expect(result.swift_symbols).toHaveLength(0);
expect(result.coverage).toContainEqual(
expect.objectContaining({
facet: "objc_class_symbols",
status: "partial",
reason: expect.stringContaining("record_limit_reached"),
}),
);
expect(result.coverage).toContainEqual(
expect.objectContaining({
facet: "swift_dispatch_tables",
status: "unsupported",
}),
);
});
});
+304
View File
@@ -64,6 +64,130 @@ export const objcClassSchema = z.strictObject({
});
export type ObjcClass = z.infer<typeof objcClassSchema>;
/** Exact location of a decoded native metadata value when the provider knows it. */
export const nativeMetadataLocationSchema = z.strictObject({
address: z.string().nullable(),
file_offset: z.number().int().nonnegative().nullable(),
});
export type NativeMetadataLocation = z.infer<
typeof nativeMetadataLocationSchema
>;
/** Provider-neutral decode state; absence never silently means unsupported. */
export const nativeMetadataDecodeSchema = z.strictObject({
status: z.enum(["decoded", "partial", "unsupported", "invalid"]),
reason: z.string().nullable(),
});
export type NativeMetadataDecode = z.infer<typeof nativeMetadataDecodeSchema>;
/** Evidence attached to one dispatch edge or metadata record. */
export const nativeMetadataEvidenceSchema = z.strictObject({
kind: z.enum([
"binary_metadata",
"interface_builder_resource",
"symbol",
"reference",
"decompiler",
]),
description: z.string().min(1),
location: nativeMetadataLocationSchema,
artifact_path: z.string().min(1).nullable().default(null),
artifact_sha256: z
.string()
.regex(/^[a-f0-9]{64}$/u)
.nullable()
.default(null),
});
export type NativeMetadataEvidence = z.infer<
typeof nativeMetadataEvidenceSchema
>;
/** Objective-C ivar layout, retaining encoded type and exact storage offset. */
export const objcIvarSchema = z.strictObject({
class_name: z.string().min(1),
name: z.string().min(1),
type_encoding: z.string().nullable(),
offset: z.number().int().nonnegative().nullable(),
location: nativeMetadataLocationSchema,
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type ObjcIvar = z.infer<typeof objcIvarSchema>;
/** Selector implementation resolved from a class or metaclass method list. */
export const objcDispatchImplementationSchema = z.strictObject({
class_name: z.string().min(1),
selector: z.string().min(1),
method_type: z.enum(["instance", "class"]),
implementation_address: z.string().nullable(),
location: nativeMetadataLocationSchema,
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type ObjcDispatchImplementation = z.infer<
typeof objcDispatchImplementationSchema
>;
/** Swift protocol conformance and its associated witness-table location. */
export const swiftConformanceSchema = z.strictObject({
type_name: z.string().min(1),
protocol_name: z.string().min(1),
module: z.string().nullable(),
witness_table: nativeMetadataLocationSchema,
location: nativeMetadataLocationSchema,
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type SwiftConformance = z.infer<typeof swiftConformanceSchema>;
/** A typed slot in a Swift protocol witness table or class vtable. */
export const swiftDispatchSlotSchema = z.strictObject({
owner: z.string().min(1),
table_kind: z.enum(["witness_table", "class_vtable"]),
slot_index: z.number().int().nonnegative(),
requirement: z.string().nullable(),
implementation: z.string().nullable(),
implementation_address: z.string().nullable(),
thunk_address: z.string().nullable(),
location: nativeMetadataLocationSchema,
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type SwiftDispatchSlot = z.infer<typeof swiftDispatchSlotSchema>;
/** Signed-relative pointer as stored in an Apple binary metadata structure. */
export const relativePointerSchema = z.strictObject({
field: nativeMetadataLocationSchema,
target: nativeMetadataLocationSchema,
displacement: z.number().int(),
indirectable: z.boolean(),
indirect: z.boolean().nullable(),
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type RelativePointer = z.infer<typeof relativePointerSchema>;
/** A raw Swift-mangled symbol when a provider has not decoded its metadata. */
export const swiftSymbolSchema = z.strictObject({
mangled_name: z.string().min(1),
address: z.string().min(1),
decode: nativeMetadataDecodeSchema,
evidence: z.array(nativeMetadataEvidenceSchema).min(1),
});
export type SwiftSymbol = z.infer<typeof swiftSymbolSchema>;
/** Per-facet coverage; empty arrays are meaningful only with this record. */
export const nativeMetadataCoverageSchema = z.strictObject({
facet: z.string().min(1),
status: z.enum(["complete", "partial", "unsupported", "not_requested"]),
reason: z.string().nullable(),
examined: z.number().int().nonnegative(),
decoded: z.number().int().nonnegative(),
});
export type NativeMetadataCoverage = z.infer<
typeof nativeMetadataCoverageSchema
>;
/** Swift declaration kind. */
export const swiftDeclKindSchema = z.enum([
"class",
@@ -129,10 +253,190 @@ export const objcSwiftMetadataSchema = z.strictObject({
objc_classes: z.array(objcClassSchema).default([]),
objc_protocols: z.array(objcProtocolSchema).default([]),
swift_decls: z.array(swiftDeclSchema).default([]),
objc_ivars: z.array(objcIvarSchema).default([]),
objc_dispatch_implementations: z
.array(objcDispatchImplementationSchema)
.default([]),
swift_conformances: z.array(swiftConformanceSchema).default([]),
swift_dispatch_slots: z.array(swiftDispatchSlotSchema).default([]),
swift_symbols: z.array(swiftSymbolSchema).default([]),
relative_pointers: z.array(relativePointerSchema).default([]),
coverage: z.array(nativeMetadataCoverageSchema).default([]),
db_save_result: dbSaveResultSchema.nullable(),
});
export type ObjcSwiftMetadata = z.infer<typeof objcSwiftMetadataSchema>;
/** Provider and target identity for one normalized dispatch-metadata result. */
export const nativeDispatchMetadataResultSchema = z.strictObject({
target_sha256: z
.string()
.regex(/^[a-f0-9]{64}$/u)
.nullable(),
provider: z.strictObject({
id: z.string().min(1),
name: z.string().min(1),
version: z.string().nullable(),
}),
analysis_profile_digest: z.string().min(1).nullable(),
result: objcSwiftMetadataSchema,
});
export type NativeDispatchMetadataResult = z.infer<
typeof nativeDispatchMetadataResultSchema
>;
/** Decode only facts visible in a provider's existing name inventory. */
export const inspectNativeDispatchMetadata = (
names: readonly { readonly address: string; readonly name: string }[],
maxRecords = 5_000,
): ObjcSwiftMetadata => {
const symbolEvidence = (name: string, address: string) => [
{
kind: "symbol" as const,
description: `Provider symbol ${name}`,
location: { address, file_offset: null },
artifact_path: null,
artifact_sha256: null,
},
];
const classNames = new Set<string>();
const implementations = new Map<string, ObjcDispatchImplementation>();
const swiftSymbols: SwiftSymbol[] = [];
const examinedNames = names.slice(0, maxRecords);
for (const entry of examinedNames) {
const classMatch = /(?:OBJC_CLASS_|OBJC_METACLASS_)\$_([^\s]+)/u.exec(
entry.name,
);
if (classMatch?.[1] !== undefined) classNames.add(classMatch[1]);
const methodMatch = /^([+-])\[([^\s\]]+)\s+([^\]]+)\]$/u.exec(entry.name);
if (methodMatch?.[1] !== undefined && methodMatch[2] && methodMatch[3]) {
const methodType = methodMatch[1] === "+" ? "class" : "instance";
const selector = methodMatch[3];
implementations.set(`${methodMatch[2]}:${selector}:${methodType}`, {
class_name: methodMatch[2],
selector,
method_type: methodType,
implementation_address: entry.address,
location: { address: entry.address, file_offset: null },
decode: { status: "partial", reason: "symbol_name_only" },
evidence: symbolEvidence(entry.name, entry.address),
});
classNames.add(methodMatch[2]);
}
if (/^(?:_?\$s|_Tt)/u.test(entry.name))
swiftSymbols.push({
mangled_name: entry.name,
address: entry.address,
decode: { status: "partial", reason: "mangled_symbol_not_demangled" },
evidence: symbolEvidence(entry.name, entry.address),
});
}
const methodsByClass = new Map<string, ObjcMethod[]>();
for (const implementation of implementations.values()) {
const methods = methodsByClass.get(implementation.class_name) ?? [];
methods.push({
selector: implementation.selector,
method_type: implementation.method_type,
address: parseMetadataAddress(implementation.implementation_address),
is_required: false,
is_optional: false,
});
methodsByClass.set(implementation.class_name, methods);
}
const classes: ObjcClass[] = [...classNames].sort().map((name) => ({
name,
super_class: null,
is_meta_class: false,
is_root_class: false,
methods: methodsByClass.get(name) ?? [],
properties: [],
protocols: [],
ivar_count: 0,
instance_size: null,
}));
const decodedImplementations = [...implementations.values()].sort(
(left, right) =>
`${left.class_name}:${left.selector}`.localeCompare(
`${right.class_name}:${right.selector}`,
),
);
const swift = swiftSymbols.length > 0;
return objcSwiftMetadataSchema.parse({
objc_classes: classes,
objc_dispatch_implementations: decodedImplementations,
swift_symbols: swiftSymbols,
coverage: [
{
facet: "objc_class_symbols",
status: "partial",
reason:
names.length > examinedNames.length
? "record_limit_reached_and_symbol_names_are_not_runtime_class_metadata"
: "symbol_names_do_not_recover_all_runtime_class_metadata",
examined: examinedNames.length,
decoded: classes.length,
},
{
facet: "objc_dispatch_implementations",
status: decodedImplementations.length > 0 ? "partial" : "unsupported",
reason:
names.length > examinedNames.length
? "record_limit_reached_and_only_symbolized_methods_are_resolved"
: decodedImplementations.length > 0
? "only_symbolized_methods_are_resolved"
: "provider_name_inventory_has_no_objc_method_symbols",
examined: examinedNames.length,
decoded: decodedImplementations.length,
},
{
facet: "objc_ivar_layouts",
status: "unsupported",
reason: "symbol_inventory_does_not_expose_runtime_ivar_layouts",
examined: 0,
decoded: 0,
},
{
facet: "swift_declarations",
status: swift ? "partial" : "unsupported",
reason: swift
? "symbols_are_not_demangled_declarations"
: "no_swift_symbols_observed",
examined: swiftSymbols.length,
decoded: 0,
},
{
facet: "swift_dispatch_tables",
status: "unsupported",
reason:
"provider_name_inventory_does_not_decode_witness_or_class_vtables",
examined: 0,
decoded: 0,
},
{
facet: "relative_pointers",
status: "unsupported",
reason:
"provider_name_inventory_does_not_decode_binary_relative_pointers",
examined: 0,
decoded: 0,
},
],
db_save_result: null,
});
};
const parseMetadataAddress = (address: string | null): number | null => {
if (address === null) return null;
const match = /(?:^|:)0x([0-9a-f]+)$/iu.exec(address);
if (match?.[1] === undefined) return null;
const value = Number.parseInt(match[1], 16);
return Number.isSafeInteger(value) ? value : null;
};
/** Check if a selector name looks like a getter. */
export function isGetterSelector(selector: string): boolean {
return /^[a-z][a-zA-Z0-9_]*$/u.test(selector) && !selector.includes(":");
File diff suppressed because one or more lines are too long
+5 -1
View File
@@ -60,7 +60,11 @@ const selectedChecks = (
byName.get("architecture"),
installation,
...(installation?.status === "passed"
? [byName.get("version"), byName.get("headless")]
? [
byName.get("version"),
byName.get("headless"),
byName.get("native_decompiler"),
]
: []),
byName.get("java"),
].filter((candidate): candidate is GhidraInstallationCheck =>
+61 -1
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import type { JsonValue } from "../domain/jsonValue.js";
import { functionDossierSchema } from "../domain/hopperValues.js";
import {
GHIDRA_FUNCTION_OPERATIONS,
isGhidraFunctionOperation,
@@ -147,7 +148,6 @@ describe("Ghidra function-analysis result values", () => {
mappings: [
{
target_address: "0x401020",
data_addresses: ["0x403000"],
},
],
},
@@ -157,10 +157,70 @@ describe("Ghidra function-analysis result values", () => {
compilable: false,
},
},
native_value_flow: {
available: true,
provenance: "ghidra-high-pcode",
operations: [
expect.objectContaining({ id: "0x401000#0", opcode: "COPY" }),
],
truncated: false,
},
},
});
});
it("rejects p-code relationships that refer to omitted operation IDs", () => {
const dossier = ghidraFunctionDossier();
if (
typeof dossier !== "object" ||
dossier === null ||
Array.isArray(dossier)
)
throw new TypeError("Ghidra dossier fixture is invalid");
const flow = dossier.native_value_flow;
if (typeof flow !== "object" || flow === null || Array.isArray(flow))
throw new TypeError("Ghidra p-code fixture is invalid");
expect(
parseGhidraFunctionResult("analyze_function", {
...dossier,
native_value_flow: {
...flow,
def_use: [
{
definition: "0x401000#missing",
use: "0x401000#0",
input_index: 0,
},
],
},
}),
).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
});
});
describe("Ghidra jump-table mapping contract", () => {
it("keeps jump-table sources separate from case-to-target mappings", () => {
const parsed = parseGhidraFunctionResult(
"analyze_function",
ghidraFunctionDossier(),
);
if (!parsed.ok) throw parsed.error;
const boundary = functionDossierSchema.parse(parsed.value).native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const mapping = boundary.jump_tables[0]?.mappings[0];
expect(mapping).toMatchObject({
case_value: 0,
target_address: "0x401020",
});
expect(mapping).not.toHaveProperty("data_addresses");
});
});
describe("Ghidra function-analysis malformed results", () => {
it.each(malformedOutputs())(
"rejects malformed %s output",
(_name, operation, value) => {
+58 -5
View File
@@ -8,6 +8,7 @@ import {
} from "../domain/hopperValues.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { nativeApiBoundarySchema } from "../domain/nativeApiBoundary.js";
import { nativeValueFlowSchema } from "../domain/nativeValueFlow.js";
import { err, ok, type Result } from "../domain/result.js";
import {
ghidraIdentifierSchema,
@@ -151,10 +152,7 @@ const ghidraNativeApiBoundary = nativeApiBoundarySchema.superRefine(
const addresses = value.jump_tables.flatMap((table) => [
table.dispatch_address,
...table.data_sources.map(({ address }) => address),
...table.mappings.flatMap(({ target_address, data_addresses }) => [
target_address,
...data_addresses,
]),
...table.mappings.map(({ target_address }) => target_address),
]);
if (
addresses.some(
@@ -169,8 +167,63 @@ const ghidraNativeApiBoundary = nativeApiBoundarySchema.superRefine(
},
);
const ghidraNativeValueFlow = nativeValueFlowSchema.superRefine(
(value, context) => {
if (!value.available) return;
const operations = value.operations;
const addresses = operations.flatMap((operation) => [
operation.address,
...operation.inputs.flatMap(({ location }) =>
location === null ? [] : [location],
),
...(operation.output?.location == null
? []
: [operation.output.location]),
]);
if (
addresses.some(
(address) => !ghidraCanonicalAddressSchema.safeParse(address).success,
)
)
context.addIssue({
code: "custom",
message: "Ghidra p-code flow contains a non-canonical address",
});
const operationIds = new Set(operations.map(({ id }) => id));
if (operationIds.size !== operations.length)
context.addIssue({
code: "custom",
message: "Ghidra p-code operation identifiers are not unique",
});
if (
value.def_use.some(
({ definition, use }) =>
!operationIds.has(definition) || !operationIds.has(use),
) ||
value.effects.some(({ operation }) => !operationIds.has(operation))
)
context.addIssue({
code: "custom",
message: "Ghidra p-code relationship references a missing operation",
});
if (
value.truncated !==
(value.omitted_operations_lower_bound > 0 ||
value.known_omitted_inputs > 0 ||
value.known_omitted_edges > 0)
)
context.addIssue({
code: "custom",
message: "Ghidra p-code truncation flag contradicts omitted counts",
});
},
);
const ghidraFunctionDossier = functionDossierSchema
.extend({ native_api: ghidraNativeApiBoundary })
.extend({
native_api: ghidraNativeApiBoundary,
native_value_flow: ghidraNativeValueFlow,
})
.strict()
.superRefine((value, context) => {
const addresses = [
+28 -2
View File
@@ -12,6 +12,7 @@ import {
const INSTALL = "/opt/ghidra";
const PROPERTIES = `${INSTALL}/Ghidra/application.properties`;
const HEADLESS = `${INSTALL}/support/analyzeHeadless`;
const MAC_DECOMPILER = `${INSTALL}/Ghidra/Features/Decompiler/os/mac_arm_64/decompile`;
const WINDOWS_INSTALL = "C:\\tools\\ghidra_12.1.4_PUBLIC";
const WINDOWS_PROPERTIES = `${WINDOWS_INSTALL}\\Ghidra\\application.properties`;
const WINDOWS_HEADLESS = `${WINDOWS_INSTALL}\\support\\analyzeHeadless.bat`;
@@ -30,7 +31,7 @@ const host = (
path === PROPERTIES
? `application.version=${SUPPORTED_GHIDRA_VERSION}\n`
: undefined,
executable: (path) => path === HEADLESS,
executable: (path) => path === HEADLESS || path === MAC_DECOMPILER,
probeJava: () => JAVA,
...overrides,
});
@@ -73,6 +74,31 @@ describe("Ghidra installation inspection", () => {
});
});
it.each(["x64", "arm64"] as const)(
"keeps Ghidra unavailable on unadmitted macOS %s hosts",
(architecture) => {
const decompiler = `${INSTALL}/Ghidra/Features/Decompiler/os/${architecture === "arm64" ? "mac_arm_64" : "mac_x86_64"}/decompile`;
const macHost: GhidraInstallationHost = {
...host({
platform: "darwin",
architecture,
executable: (path) => path === HEADLESS || path === decompiler,
}),
};
expect(
inspectGhidraInstallation(
{ installDir: INSTALL, platform: "darwin", architecture },
macHost,
),
).toMatchObject({
status: "unavailable",
platform: "darwin",
architecture,
nativeDecompilerPath: decompiler,
});
},
);
it("accepts the exact Windows x64 batch launcher and JDK commitment", () => {
const windowsHost: GhidraInstallationHost = {
platform: "win32",
@@ -126,7 +152,7 @@ describe("Ghidra installation rejection diagnostics", () => {
name: "unsupported platform",
options: {
installDir: INSTALL,
platform: "darwin" as const,
platform: "aix" as const,
architecture: "x64" as const,
},
override: {},
+59 -6
View File
@@ -42,12 +42,14 @@ export type GhidraInstallationCheckName =
| "installation"
| "version"
| "headless"
| "native_decompiler"
| "java";
interface GhidraInstallationObservation {
readonly platform: NodeJS.Platform;
readonly architecture: NodeJS.Architecture;
readonly applicationPropertiesPath: string | null;
readonly nativeDecompilerPath: string | null;
readonly javaCommand: string;
readonly checks: readonly GhidraInstallationCheck[];
}
@@ -109,6 +111,7 @@ interface GhidraInstallationCoordinates {
readonly installDir: string | null;
readonly applicationPropertiesPath: string | null;
readonly analyzeHeadlessPath: string | null;
readonly nativeDecompilerPath: string | null;
readonly properties: Readonly<{ providerVersion: string | null }> | undefined;
readonly javaCommand: string;
readonly java: GhidraJavaObservation | undefined;
@@ -129,7 +132,12 @@ export const inspectGhidraInstallation = (
): GhidraInstallationInspection => {
const platform = options.platform ?? process.platform;
const architecture = options.architecture ?? process.arch;
const coordinates = installationCoordinates(options, platform, host);
const coordinates = installationCoordinates(
options,
platform,
architecture,
host,
);
const checks = installationChecks({
coordinates,
platform,
@@ -145,6 +153,7 @@ export const inspectGhidraInstallation = (
platform,
architecture,
applicationPropertiesPath: coordinates.applicationPropertiesPath,
nativeDecompilerPath: coordinates.nativeDecompilerPath,
javaCommand: coordinates.javaCommand,
checks,
};
@@ -193,6 +202,7 @@ const availableInstallation = (
const installationCoordinates = (
options: GhidraInstallationOptions,
platform: NodeJS.Platform,
architecture: NodeJS.Architecture,
host: GhidraInstallationHost,
): GhidraInstallationCoordinates => {
const path = platform === "win32" ? win32 : posix;
@@ -209,6 +219,36 @@ const installationCoordinates = (
"support",
platform === "win32" ? "analyzeHeadless.bat" : "analyzeHeadless",
);
const decompilerPlatform =
platform === "darwin"
? architecture === "arm64"
? "mac_arm_64"
: "mac_x86_64"
: null;
const nativeDecompilerPath =
installDir === null || decompilerPlatform === null
? null
: ([
path.join(
installDir,
"Ghidra",
"Features",
"Decompiler",
"os",
decompilerPlatform,
"decompile",
),
path.join(
installDir,
"Ghidra",
"Features",
"Decompiler",
"build",
"os",
decompilerPlatform,
"decompile",
),
].find((candidate) => host.executable(candidate)) ?? null);
const properties =
applicationPropertiesPath === null
? undefined
@@ -227,6 +267,7 @@ const installationCoordinates = (
installDir,
applicationPropertiesPath,
analyzeHeadlessPath,
nativeDecompilerPath,
properties:
properties === undefined
? undefined
@@ -259,16 +300,15 @@ const installationChecks = ({
passed: platform === "linux" || platform === "win32",
code: "unsupported_host",
detail: platform,
remediation:
"Use REA's Ghidra adapter on a supported Linux or Windows x64 host.",
remediation: "Use REA's Ghidra adapter on Linux or Windows x64.",
}),
installationCheck({
name: "architecture",
passed: architecture === "x64",
passed:
(platform === "linux" || platform === "win32") && architecture === "x64",
code: "unsupported_host",
detail: architecture,
remediation:
"Use the official x86-64 Ghidra distribution on an x64 Linux or Windows host.",
remediation: "Use x64 on Linux or Windows.",
}),
installationCheck({
name: "installation",
@@ -302,6 +342,18 @@ const installationChecks = ({
remediation:
"Restore support/analyzeHeadless or support/analyzeHeadless.bat from the official Ghidra release.",
}),
installationCheck({
name: "native_decompiler",
passed: platform !== "darwin" || coordinates.nativeDecompilerPath !== null,
code: "executable_missing",
detail:
platform !== "darwin"
? "not required on this platform"
: (coordinates.nativeDecompilerPath ??
"matching macOS native decompiler was not found"),
remediation:
"REA does not build native tools. Build Ghidra's native components for this macOS architecture or provide an installation containing Ghidra/Features/Decompiler/os/<platform>/decompile.",
}),
javaCheck(coordinates.java, coordinates.javaCommand, javaHome),
];
@@ -311,6 +363,7 @@ export const ghidraInstallationDiagnostics = (
): Readonly<Record<string, JsonValue>> => ({
install_dir: inspection.installDir,
analyze_headless_path: inspection.analyzeHeadlessPath,
native_decompiler_path: inspection.nativeDecompilerPath,
application_properties_path: inspection.applicationPropertiesPath,
provider_version: inspection.providerVersion,
java_command: inspection.javaCommand,
+7 -1
View File
@@ -190,7 +190,13 @@ export class GhidraProvider implements AnalysisProviderCandidate {
if (!prerequisites.ok) return unavailableClient(prerequisites.error);
const committedProfile = prerequisites.value.profile;
const providerLimitations =
installation.platform === "win32" ? windowsP0Limitations : [];
installation.platform === "win32"
? windowsP0Limitations
: installation.platform === "darwin"
? [
"macOS sessions require a matching executable Ghidra native decompiler; REA checks for it but does not build native components or change Gatekeeper quarantine state.",
]
: [];
const client = this.clientFactory({
launcher: new GhidraHeadlessLauncher({
analyzeHeadlessPath: prerequisites.value.analyzeHeadlessPath,
+20
View File
@@ -48,6 +48,7 @@ export const registerEnhancedTools = (
options: EnhancedToolRegistration,
): void => {
const [
nativeDispatchMetadata,
objcClasses,
objcProtocols,
batchDecompile,
@@ -60,7 +61,17 @@ export const registerEnhancedTools = (
traceFeature,
codeForString,
traceCallPath,
traceNativeUiAction,
] = ENHANCED_TOOL_CONTRACTS;
server.registerTool(
nativeDispatchMetadata.name,
toolRegistrationOptions(nativeDispatchMetadata),
(input, context) =>
executeEnhancedTool(analysis, options, nativeDispatchMetadata, {
validatedCall: { name: "inspect_native_dispatch_metadata", input },
context,
}),
);
server.registerTool(
objcClasses.name,
toolRegistrationOptions(objcClasses),
@@ -169,6 +180,15 @@ export const registerEnhancedTools = (
context,
}),
);
server.registerTool(
traceNativeUiAction.name,
toolRegistrationOptions(traceNativeUiAction),
(input, context) =>
executeEnhancedTool(analysis, options, traceNativeUiAction, {
validatedCall: { name: "trace_native_ui_action", input },
context,
}),
);
};
const executeEnhancedTool = async (