fix(ghidra): resolve secondary symbols at function entries (#1195)
* fix(ghidra): resolve secondary symbols at function entries * test(ghidra): derive interior bytes from decoded instructions
This commit is contained in:
@@ -13,6 +13,7 @@ import { verifyLegacyGhidraReferenceSnapshot } from "./ghidra-reference-snapshot
|
||||
import { verifyGhidraSnapshotLifecycle } from "./real-ghidra-snapshot-lifecycle.mjs";
|
||||
import { verifyGhidraTargetAdmission } from "./real-ghidra-target-admission.mjs";
|
||||
import { verifyGhidraLargeResults } from "./real-ghidra-large-results.mjs";
|
||||
import { verifyGhidraEntryAliases } from "./real-ghidra-entry-aliases.mjs";
|
||||
import { verifyGhidraNamespaceAnnotations } from "./real-ghidra-namespace-annotations.mjs";
|
||||
|
||||
/** Probe real Ghidra location, annotation and error contracts through public adapters. */
|
||||
@@ -278,7 +279,25 @@ export async function verifyGhidraBoundaries(
|
||||
await cli("inspect-native-instruction", address.toUpperCase()),
|
||||
baseline,
|
||||
);
|
||||
const interior = `0x${(BigInt(address) + 1n).toString(16)}`;
|
||||
// x86 function entries can begin with a one-byte PUSH. Locate an observed
|
||||
// multi-byte instruction instead of assuming entry + 1 is an interior byte.
|
||||
let multiByteInstruction;
|
||||
for (const range of original.procedure.body.ranges) {
|
||||
let cursor = BigInt(range.start);
|
||||
while (cursor <= BigInt(range.end)) {
|
||||
const instruction = await call("inspect_native_instruction", {
|
||||
address: `0x${cursor.toString(16)}`,
|
||||
});
|
||||
if (instruction.status === "decoded" && instruction.length > 1) {
|
||||
multiByteInstruction = instruction;
|
||||
break;
|
||||
}
|
||||
cursor += BigInt(instruction.length ?? 1);
|
||||
}
|
||||
if (multiByteInstruction !== undefined) break;
|
||||
}
|
||||
assert.ok(multiByteInstruction, "Fixture lacks a multi-byte instruction");
|
||||
const interior = `0x${(BigInt(multiByteInstruction.address) + 1n).toString(16)}`;
|
||||
assert.equal(
|
||||
(await call("inspect_native_instruction", { address: interior })).status,
|
||||
"not-instruction-boundary",
|
||||
@@ -693,6 +712,13 @@ export async function verifyGhidraBoundaries(
|
||||
entrypoint,
|
||||
env,
|
||||
});
|
||||
await verifyGhidraEntryAliases({
|
||||
call,
|
||||
reject: invalid,
|
||||
target,
|
||||
entrypoint,
|
||||
env,
|
||||
});
|
||||
await verifyGhidraLargeResults({
|
||||
call,
|
||||
reject: invalid,
|
||||
@@ -741,6 +767,7 @@ export async function verifyGhidraBoundaries(
|
||||
imported_source_identity_retained: true,
|
||||
equivalent_instruction_address_spellings: true,
|
||||
qualified_annotation_name_roundtrip: true,
|
||||
imported_entry_alias_selection: true,
|
||||
oversized_result_retention_and_complete_export: true,
|
||||
long_selector_rejection_and_provider_recovery: true,
|
||||
source_immutable: true,
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
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";
|
||||
|
||||
/** Verify imported entry aliases through CLI/MCP without admitting interior labels. */
|
||||
export async function verifyGhidraEntryAliases({
|
||||
call,
|
||||
reject,
|
||||
target,
|
||||
entrypoint,
|
||||
env,
|
||||
}) {
|
||||
const exec = promisify(execFile);
|
||||
const compiler = process.env.REA_CC ?? "cc";
|
||||
const workspace = await mkdtemp(join(tmpdir(), "rea-ghidra-entry-aliases-"));
|
||||
const source = fileURLToPath(
|
||||
new URL("../../tests/conformance/ghidra/entry-aliases.c", import.meta.url),
|
||||
);
|
||||
const path = join(workspace, "entry-aliases");
|
||||
const prefix = process.platform === "darwin" ? "_" : "";
|
||||
try {
|
||||
await exec(compiler, ["-O0", "-g", "-fno-inline", source, "-o", path], {
|
||||
env,
|
||||
timeout: 30000,
|
||||
});
|
||||
const bytes = await readFile(path);
|
||||
await call("close_binary");
|
||||
await call("open_binary", { path, provider_id: "ghidra" });
|
||||
const names = await call("list_names");
|
||||
const alias = names.find(
|
||||
({ value }) => value === `${prefix}rea_entry_alias`,
|
||||
);
|
||||
assert.ok(alias, "Fixture entry alias was not imported");
|
||||
assert.equal(
|
||||
alias.symbol.primary,
|
||||
false,
|
||||
"The regression must exercise a secondary entry symbol",
|
||||
);
|
||||
assert.equal(alias.symbol.source, "imported");
|
||||
const canonical = (await call("list_procedures")).find(
|
||||
({ address }) => address === alias.address,
|
||||
);
|
||||
assert.ok(canonical, "Fixture alias is not at a function entry");
|
||||
for (const procedure of [
|
||||
alias.value,
|
||||
"dead",
|
||||
canonical.value,
|
||||
alias.address,
|
||||
]) {
|
||||
assert.equal(
|
||||
await call("procedure_address", { procedure }),
|
||||
alias.address,
|
||||
);
|
||||
assert.equal(
|
||||
(await call("analyze_function", { procedure })).procedure.address,
|
||||
alias.address,
|
||||
);
|
||||
assert.equal(
|
||||
await call("procedure_pseudo_code", { procedure }),
|
||||
await call("procedure_pseudo_code", { procedure: alias.address }),
|
||||
);
|
||||
}
|
||||
// Leaf and qualified forms of the primary name must not duplicate a match.
|
||||
assert.equal(
|
||||
(await call("procedure_info", { procedure: alias.value })).entrypoint,
|
||||
alias.address,
|
||||
);
|
||||
const interior = names.find(
|
||||
({ value }) => value === `${prefix}rea_interior`,
|
||||
);
|
||||
assert.ok(interior);
|
||||
assert.notEqual(interior.address, alias.address);
|
||||
const failure = await reject("procedure_address", {
|
||||
procedure: interior.value,
|
||||
});
|
||||
assert.equal(failure.code, "invalid_request");
|
||||
assert.match(
|
||||
JSON.stringify(failure.details.issues),
|
||||
/Unknown Ghidra procedure name or address/u,
|
||||
);
|
||||
assert.equal(
|
||||
await call("procedure_address", { procedure: interior.address }),
|
||||
alias.address,
|
||||
"Explicit interior addresses keep their existing containment semantics",
|
||||
);
|
||||
const edited = await call("annotate_native_function", {
|
||||
procedure: alias.value,
|
||||
comment: "Selected by imported entry alias",
|
||||
});
|
||||
assert.equal(edited.annotations.address, alias.address);
|
||||
assert.equal(
|
||||
edited.annotations.comment,
|
||||
"Selected by imported entry alias",
|
||||
);
|
||||
assert.equal(
|
||||
await call("procedure_address", { procedure: alias.value }),
|
||||
alias.address,
|
||||
);
|
||||
await call("close_binary");
|
||||
const { stdout } = await exec(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"function",
|
||||
path,
|
||||
alias.value,
|
||||
"--provider",
|
||||
"ghidra",
|
||||
"--json",
|
||||
],
|
||||
{ env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
|
||||
);
|
||||
assert.equal(
|
||||
JSON.parse(stdout).normalized_result.procedure.address,
|
||||
alias.address,
|
||||
);
|
||||
assert.deepEqual(await readFile(path), bytes);
|
||||
await call("open_binary", { path: target.path, provider_id: "ghidra" });
|
||||
} finally {
|
||||
await rm(workspace, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user