* feat(ghidra): admit set_address_name and set_addresses_names Ghidra sessions rejected the existing naming tools with "operation is not declared by this provider", so agents had to use annotate_native_function, which returns a complete function dossier per rename and cannot name data addresses. The Java bridge now names a local function entry (namespace-aware, like annotate_native_function) or creates/renames the primary label at any other mapped address, verifying readback. set_addresses_names applies each address in its own transaction and reports per-address booleans. Both are mutating capabilities: excluded from read-only (Windows P0) handshakes and from snapshot replay. * fix(ghidra): normalize qualified symbol names and reject empty leaves Share namespace-aware leaf normalization across functions and primary labels. Invalid qualified leaves now return false for naming operations without marking the provider unavailable; function annotations retain typed invalid-input errors. Extend real CLI/MCP namespace verification with namespaced globals, readback replay, failed-name rollback, mixed batches, and capability checks. Validation: 75 focused tests and npm run check pass. Ghidra 12.1.4/JDK 21 namespace verification passes on macOS arm64 with unchanged source bytes and complete cleanup. The full host lane passes this workflow before the existing entry-alias fixture fails because the compiler places its interior label at the function entry. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
108 lines
3.8 KiB
TypeScript
108 lines
3.8 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import type { JsonValue } from "../domain/jsonValue.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import {
|
|
GHIDRA_INVENTORY_OPERATIONS,
|
|
GHIDRA_NAMING_OPERATIONS,
|
|
} from "./GhidraInventoryValues.js";
|
|
import { GHIDRA_FUNCTION_OPERATIONS } from "./GhidraFunctionValues.js";
|
|
import { ghidraExtensionResultSchema } from "./extensions/GhidraExtensions.js";
|
|
|
|
/** Exact methods proved by the bridge handshake after auto-analysis. */
|
|
export const GHIDRA_SESSION_CAPABILITIES = [
|
|
"ping",
|
|
"shutdown",
|
|
...GHIDRA_INVENTORY_OPERATIONS,
|
|
...GHIDRA_FUNCTION_OPERATIONS,
|
|
] as const;
|
|
|
|
/** Methods that edit the session database; read-only bridges omit them. */
|
|
export const GHIDRA_MUTATING_OPERATIONS: ReadonlySet<string> = new Set([
|
|
"annotate_native_function",
|
|
...GHIDRA_NAMING_OPERATIONS,
|
|
]);
|
|
|
|
const capabilitySchema = z.enum(GHIDRA_SESSION_CAPABILITIES);
|
|
|
|
const sessionInfoSchema = z
|
|
.object({
|
|
name: z.literal("REA Ghidra bridge"),
|
|
run_id: z.string().uuid(),
|
|
profile_digest: z.string().regex(/^[a-f0-9]{64}$/u),
|
|
provider: z
|
|
.object({
|
|
id: z.literal("ghidra"),
|
|
version: z.string().min(1),
|
|
})
|
|
.strict(),
|
|
read_only: z.boolean(),
|
|
analysis_complete: z.boolean(),
|
|
analysis_timed_out: z.boolean(),
|
|
analysis_extensions: z.array(ghidraExtensionResultSchema).optional(),
|
|
capabilities: z.array(capabilitySchema),
|
|
target: z
|
|
.object({
|
|
name: z.string().min(1),
|
|
language_id: z.string().min(1),
|
|
compiler_spec_id: z.string().min(1),
|
|
image_base: z.string().regex(/^0x[0-9a-f]+$/u),
|
|
default_address_space: z.string().min(1),
|
|
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
|
})
|
|
.strict(),
|
|
})
|
|
.strict();
|
|
|
|
/** Authenticated metadata produced after Ghidra finishes auto-analysis. */
|
|
export type GhidraSessionInfo = z.infer<typeof sessionInfoSchema>;
|
|
|
|
/** Require exact run, provider, and profile identity from the Java bridge. */
|
|
export const parseGhidraSessionInfo = (
|
|
value: JsonValue,
|
|
expected: {
|
|
readonly runId: string;
|
|
readonly providerVersion: string;
|
|
readonly profileDigest: string;
|
|
readonly targetSha256: string;
|
|
readonly expectedReadOnly?: boolean;
|
|
readonly expectedLanguageId?: string;
|
|
readonly expectedCompilerSpecId?: string;
|
|
},
|
|
): Result<GhidraSessionInfo, Error> => {
|
|
const parsed = sessionInfoSchema.safeParse(value);
|
|
const readOnly = expected.expectedReadOnly ?? false;
|
|
const expectedCapabilities = GHIDRA_SESSION_CAPABILITIES.filter(
|
|
(capability) => !readOnly || !GHIDRA_MUTATING_OPERATIONS.has(capability),
|
|
);
|
|
if (
|
|
!parsed.success ||
|
|
parsed.data.run_id !== expected.runId ||
|
|
parsed.data.read_only !== readOnly ||
|
|
parsed.data.capabilities.length !== expectedCapabilities.length ||
|
|
parsed.data.provider.version !== expected.providerVersion ||
|
|
parsed.data.profile_digest !== expected.profileDigest ||
|
|
parsed.data.target.sha256 !== expected.targetSha256 ||
|
|
(expected.expectedLanguageId !== undefined &&
|
|
parsed.data.target.language_id !== expected.expectedLanguageId) ||
|
|
(expected.expectedCompilerSpecId !== undefined &&
|
|
parsed.data.target.compiler_spec_id !==
|
|
expected.expectedCompilerSpecId) ||
|
|
new Set(parsed.data.capabilities).size !== expectedCapabilities.length ||
|
|
expectedCapabilities.some(
|
|
(capability) => !parsed.data.capabilities.includes(capability),
|
|
) ||
|
|
parsed.data.analysis_complete === parsed.data.analysis_timed_out
|
|
)
|
|
return err(new Error("Ghidra bridge handshake identity is invalid"));
|
|
return ok(parsed.data);
|
|
};
|
|
|
|
/** Require acknowledgement that the private bridge loop has ended. */
|
|
export const isGhidraShutdownAcknowledgement = (value: JsonValue): boolean =>
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
!Array.isArray(value) &&
|
|
value.shutdown === true &&
|
|
value.project_ephemeral === true;
|