fix(ghidra): make qualified function renames idempotent
This commit is contained in:
@@ -1011,6 +1011,8 @@ public final class ReaGhidraBridge extends HeadlessScript {
|
||||
for (String field : List.of("name", "comment", "inline_comment")) {
|
||||
if (params.has(field)) validateAnnotationText(requireText(params, field), field);
|
||||
}
|
||||
String leafName = params.has("name")
|
||||
? annotationLeafName(function, requireString(params, "name")) : null;
|
||||
int transaction = currentProgram.startTransaction("REA function annotations");
|
||||
boolean commit = false;
|
||||
try {
|
||||
@@ -1024,7 +1026,7 @@ public final class ReaGhidraBridge extends HeadlessScript {
|
||||
}
|
||||
}
|
||||
if (params.has("name"))
|
||||
function.setName(requireString(params, "name"), SourceType.USER_DEFINED);
|
||||
function.setName(leafName, SourceType.USER_DEFINED);
|
||||
monitor.checkCancelled();
|
||||
invalidateAnalysisCaches();
|
||||
JsonObject readback = annotationReadback(function);
|
||||
@@ -1032,7 +1034,7 @@ public final class ReaGhidraBridge extends HeadlessScript {
|
||||
if (!params.has(field)) continue;
|
||||
String requested = requireText(params, field);
|
||||
JsonElement measured = readback.get(field);
|
||||
String expected = !field.equals("name") && requested.isEmpty() ? null : requested;
|
||||
String expected = field.equals("name") ? leafName : requested.isEmpty() ? null : requested;
|
||||
String observed = field.equals("name") ? function.getName() :
|
||||
(measured.isJsonNull() ? null : measured.getAsString());
|
||||
if (!java.util.Objects.equals(expected, observed))
|
||||
@@ -1062,6 +1064,22 @@ public final class ReaGhidraBridge extends HeadlessScript {
|
||||
}
|
||||
}
|
||||
|
||||
private String annotationLeafName(Function function, String requested) {
|
||||
// Fully qualified readback must be usable as an idempotent rename input.
|
||||
if (requested.equals(procedureName(function))) return function.getName();
|
||||
String namespace = function.getParentNamespace().isGlobal()
|
||||
? "" : function.getParentNamespace().getName(true);
|
||||
String prefix = namespace + "::";
|
||||
if (!namespace.isEmpty() && requested.startsWith(prefix)) {
|
||||
String leaf = requested.substring(prefix.length());
|
||||
if (!leaf.isEmpty()) return leaf;
|
||||
throw new RequestFailure("invalid_function_name",
|
||||
"Function name has an empty leaf within namespace " + namespace + " at " +
|
||||
canonicalAddress(function.getEntryPoint()) + ": " + requested);
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
private static String requireText(JsonObject params, String field) {
|
||||
JsonElement value = params.get(field);
|
||||
if (value == null || !value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString())
|
||||
|
||||
@@ -317,6 +317,12 @@ unpaired Unicode surrogate; a rejection identifies the field and UTF-16 index
|
||||
and leaves every annotation unchanged. CRLF, supplementary Unicode characters,
|
||||
and combining characters are preserved. Windows P0 remains read-only. Ghidra has no
|
||||
GUI authority, and REA never falls back automatically to Hopper.
|
||||
Ghidra name edits preserve the existing namespace. Supply either a leaf name
|
||||
such as `renamed` or a fully qualified name in that namespace, such as
|
||||
`alpha::renamed`. The returned qualified name can be reused as an idempotent
|
||||
rename input. Edits retain the existing namespace; other namespace-like text
|
||||
remains literal leaf-name text. A qualified name with an empty leaf is rejected
|
||||
before any comment or name is changed.
|
||||
|
||||
- `npm run verify:ghidra`: host-native debug/stripped targets, native type layout,
|
||||
instruction/call facts, value dependencies and process/project cleanup.
|
||||
|
||||
@@ -342,6 +342,13 @@ inputs, plus real read-permission denial on a non-root host. Root runs report
|
||||
that permission-denial check as unverified. A focused filesystem integration
|
||||
check retains selected-platform routing and exclusive creation; two copy/digest
|
||||
checks duplicated by the real workflow have been removed.
|
||||
Namespace annotation probes compile a separate host C fixture with C++ ABI
|
||||
symbols, avoiding a C++ runtime prerequisite. Real Ghidra demangling supplies
|
||||
duplicate leaf names in two top-level namespaces and a nested namespace. The
|
||||
workflow verifies leaf and qualified renames, repeated reuse of fully qualified
|
||||
readback, lookup by the returned name, literal namespace-like leaf names,
|
||||
rejection of empty qualified leaf names without changing comments, CLI
|
||||
behavior, and independent CLI/MCP database ownership.
|
||||
Malformed annotation readback, memory completeness, and inventory data remain separate
|
||||
SDK/provider integration cases; success from a real
|
||||
provider cannot establish rejection of a contradictory provider response.
|
||||
|
||||
@@ -11,6 +11,7 @@ import { mcpTextValue, requireMcpResult } from "./mcp-verifier-results.mjs";
|
||||
import { verifyLegacyGhidraReferenceSnapshot } from "./ghidra-reference-snapshot-e2e.mjs";
|
||||
import { verifyGhidraSnapshotLifecycle } from "./real-ghidra-snapshot-lifecycle.mjs";
|
||||
import { verifyGhidraTargetAdmission } from "./real-ghidra-target-admission.mjs";
|
||||
import { verifyGhidraNamespaceAnnotations } from "./real-ghidra-namespace-annotations.mjs";
|
||||
|
||||
/** Probe real Ghidra location, annotation and error contracts through public adapters. */
|
||||
export async function verifyGhidraBoundaries(
|
||||
@@ -656,6 +657,13 @@ export async function verifyGhidraBoundaries(
|
||||
entry: address,
|
||||
runtimeRoot: env.TMPDIR,
|
||||
});
|
||||
await verifyGhidraNamespaceAnnotations({
|
||||
call,
|
||||
reject: invalid,
|
||||
target,
|
||||
entrypoint,
|
||||
env,
|
||||
});
|
||||
await assert.rejects(access(socketRoot), { code: "ENOENT" });
|
||||
await assert.rejects(access(runtimeRoot), { code: "ENOENT" });
|
||||
assert.equal(await call("procedure_address", { procedure: name }), address);
|
||||
@@ -696,6 +704,7 @@ export async function verifyGhidraBoundaries(
|
||||
missing_and_nonregular_source_rejected: true,
|
||||
imported_source_identity_retained: true,
|
||||
equivalent_instruction_address_spellings: true,
|
||||
qualified_annotation_name_roundtrip: true,
|
||||
source_immutable: true,
|
||||
reopen_discards_edits: true,
|
||||
long_tmpdir_private_socket_cleanup: true,
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
/** Exercise qualified annotation names against real demangled C++ ABI symbols. */
|
||||
export async function verifyGhidraNamespaceAnnotations({
|
||||
call,
|
||||
reject,
|
||||
target,
|
||||
entrypoint,
|
||||
env,
|
||||
}) {
|
||||
const exec = promisify(execFile);
|
||||
const compiler = process.env.REA_CC ?? "cc";
|
||||
try {
|
||||
await exec(compiler, ["--version"], { env, timeout: 30000 });
|
||||
} catch (cause) {
|
||||
throw new Error(
|
||||
`Ghidra namespace annotation verification requires a working host C compiler '${compiler}'.`,
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
const workspace = await mkdtemp(join(tmpdir(), "rea-ghidra-namespaces-"));
|
||||
const source = fileURLToPath(
|
||||
new URL("../../tests/conformance/ghidra/namespaces.c", import.meta.url),
|
||||
);
|
||||
const path = join(workspace, "namespaces");
|
||||
try {
|
||||
await exec(compiler, ["-O0", "-fno-inline", source, "-o", path], {
|
||||
env,
|
||||
timeout: 30000,
|
||||
});
|
||||
const bytes = await readFile(path);
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
await call("close_binary");
|
||||
await call("open_binary", { path, provider_id: "ghidra" });
|
||||
const procedures = await call("list_procedures");
|
||||
const namespaces = ["alpha", "beta", "outer::inner"];
|
||||
const entries = namespaces.map((namespace) => {
|
||||
const procedure = procedures.find(
|
||||
({ value }) => value === `${namespace}::same`,
|
||||
);
|
||||
assert.ok(
|
||||
procedure,
|
||||
`Missing demangled fixture function ${namespace}::same`,
|
||||
);
|
||||
return { namespace, address: procedure.address };
|
||||
});
|
||||
const ambiguous = await reject("procedure_address", { procedure: "same" });
|
||||
assert.equal(ambiguous.code, "invalid_request");
|
||||
for (const { address } of entries)
|
||||
assert.ok(JSON.stringify(ambiguous.details.issues).includes(address));
|
||||
for (const { namespace, address } of entries) {
|
||||
const leaf = await call("annotate_native_function", {
|
||||
procedure: address,
|
||||
name: "renamed",
|
||||
comment: "Preserve this comment",
|
||||
inline_comment: "Preserve this inline comment",
|
||||
});
|
||||
assert.equal(leaf.annotations.name, `${namespace}::renamed`);
|
||||
const qualified = await call("annotate_native_function", {
|
||||
procedure: address,
|
||||
name: `${namespace}::qualified`,
|
||||
});
|
||||
assert.equal(qualified.annotations.name, `${namespace}::qualified`);
|
||||
for (let repeat = 0; repeat < 2; repeat++) {
|
||||
const replay = await call("annotate_native_function", {
|
||||
procedure: qualified.annotations.name,
|
||||
name: qualified.annotations.name,
|
||||
});
|
||||
assert.deepEqual(replay.annotations, qualified.annotations);
|
||||
assert.equal(replay.dossier.procedure.address, address);
|
||||
}
|
||||
assert.equal(
|
||||
await call("procedure_address", {
|
||||
procedure: qualified.annotations.name,
|
||||
}),
|
||||
address,
|
||||
);
|
||||
for (const name of ["other::literal", `${namespace}::child::literal`]) {
|
||||
const literal = await call("annotate_native_function", {
|
||||
procedure: address,
|
||||
name,
|
||||
});
|
||||
const expected = name.startsWith(`${namespace}::`)
|
||||
? name
|
||||
: `${namespace}::${name}`;
|
||||
assert.equal(literal.annotations.name, expected);
|
||||
const repeated = await call("annotate_native_function", {
|
||||
procedure: expected,
|
||||
name: expected,
|
||||
});
|
||||
assert.deepEqual(repeated.annotations, literal.annotations);
|
||||
}
|
||||
await call("annotate_native_function", {
|
||||
procedure: address,
|
||||
name: qualified.annotations.name,
|
||||
});
|
||||
const failure = await reject("annotate_native_function", {
|
||||
procedure: address,
|
||||
name: `${namespace}::`,
|
||||
comment: "MUST NOT REPLACE",
|
||||
inline_comment: "MUST NOT REPLACE",
|
||||
});
|
||||
assert.equal(failure.code, "invalid_request");
|
||||
assert.deepEqual(failure.details.issues[0].path, ["name"]);
|
||||
assert.match(failure.details.issues[0].message, /empty leaf/u);
|
||||
const unchanged = await call("annotate_native_function", {
|
||||
procedure: address,
|
||||
name: qualified.annotations.name,
|
||||
});
|
||||
assert.deepEqual(unchanged.annotations, qualified.annotations);
|
||||
}
|
||||
const { stdout } = await exec(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"annotate-native-function",
|
||||
path,
|
||||
"alpha::same",
|
||||
"--name",
|
||||
"alpha::cli_renamed",
|
||||
"--provider",
|
||||
"ghidra",
|
||||
"--json",
|
||||
],
|
||||
{ env, timeout: 240000, maxBuffer: 72 * 1024 * 1024 },
|
||||
);
|
||||
assert.equal(
|
||||
JSON.parse(stdout).normalized_result.annotations.name,
|
||||
"alpha::cli_renamed",
|
||||
);
|
||||
assert.equal(
|
||||
(await call("analyze_function", { procedure: entries[0].address }))
|
||||
.procedure.name,
|
||||
"alpha::qualified",
|
||||
"The independent CLI session must not alter the active MCP database",
|
||||
);
|
||||
assert.deepEqual(await readFile(path), bytes);
|
||||
assert.equal((await call("binary_session")).sha256, sha256);
|
||||
await call("close_binary");
|
||||
await call("open_binary", { path: target.path, provider_id: "ghidra" });
|
||||
} finally {
|
||||
await rm(workspace, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ export const limitationsFor = (operation: string): readonly string[] => {
|
||||
case "annotate_native_function":
|
||||
return [
|
||||
...common,
|
||||
"Names use Ghidra USER_DEFINED source. Name writes preserve the existing namespace; readback uses the fully qualified name. Regular comments map to PRE and inline comments to EOL at the exact function entry. Changes commit together after readback and refreshed analysis; failure rolls them all back.",
|
||||
"Names use Ghidra USER_DEFINED source. Name writes accept a leaf name or a fully qualified name within the existing namespace; readback uses the fully qualified name and can be reused without adding namespace prefixes. Edits retain the current namespace; other namespace-like text remains literal leaf-name text. Regular comments map to PRE and inline comments to EOL at the exact function entry. Changes commit together after readback and refreshed analysis; failure rolls them all back.",
|
||||
"Annotation text rejects NUL and unpaired Unicode surrogates before mutation, with the field and UTF-16 index in the error. Supported Unicode and line endings are preserved.",
|
||||
"Metadata edits invalidate immutable analysis snapshots and are discarded on close. CLI returns the updated dossier before session cleanup; this is not a saved Ghidra project.",
|
||||
];
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Emit C++ ABI names with the host C compiler; no C++ runtime is required.
|
||||
#if defined(__APPLE__)
|
||||
#define REA_CPP_SYMBOL(name) "_" name
|
||||
#else
|
||||
#define REA_CPP_SYMBOL(name) name
|
||||
#endif
|
||||
|
||||
__attribute__((noinline, used)) int rea_namespace_alpha(int value)
|
||||
__asm__(REA_CPP_SYMBOL("_ZN5alpha4sameEi"));
|
||||
__attribute__((noinline, used)) int rea_namespace_beta(int value)
|
||||
__asm__(REA_CPP_SYMBOL("_ZN4beta4sameEi"));
|
||||
__attribute__((noinline, used)) int rea_namespace_nested(int value)
|
||||
__asm__(REA_CPP_SYMBOL("_ZN5outer5inner4sameEi"));
|
||||
|
||||
int rea_namespace_alpha(int value) { return value + 3; }
|
||||
int rea_namespace_beta(int value) { return value * 2; }
|
||||
int rea_namespace_nested(int value) { return value - 1; }
|
||||
|
||||
int main(void) {
|
||||
return rea_namespace_alpha(1) + rea_namespace_beta(2) +
|
||||
rea_namespace_nested(3);
|
||||
}
|
||||
Reference in New Issue
Block a user