fix(native): preserve compact reference provenance (#1438)
Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
This commit is contained in:
co-authored by
N0zoM1z0
parent
2ec93ba166
commit
24d9337649
@@ -0,0 +1,60 @@
|
||||
import type { JsonValue } from "../jsonValue.js";
|
||||
|
||||
/** Wire-boundary controls; these do not establish real Ghidra behavior. */
|
||||
export const compactReference = () => ({
|
||||
source_address: "0x401000",
|
||||
target_address: "0x402000",
|
||||
type: "COMPUTED_CALL",
|
||||
call: true,
|
||||
jump: false,
|
||||
indirect: false,
|
||||
computed: true,
|
||||
operand_index: 1,
|
||||
data: false,
|
||||
read: false,
|
||||
write: false,
|
||||
primary: false,
|
||||
provenance: "ghidra-reference-manager",
|
||||
source: "analysis",
|
||||
});
|
||||
|
||||
export const compactInstruction = (references: JsonValue[] = []) => ({
|
||||
address: "0x401000",
|
||||
status: "decoded",
|
||||
procedure: "0x401000",
|
||||
architecture: "x86:LE:32:default",
|
||||
mode: "default",
|
||||
bytes: "ffd0",
|
||||
length: 2,
|
||||
mnemonic: "CALL",
|
||||
raw_disassembly: "CALL EAX",
|
||||
operands: [],
|
||||
flow: {
|
||||
kind: "call",
|
||||
conditional: false,
|
||||
computed: true,
|
||||
direct_destinations: [],
|
||||
},
|
||||
references,
|
||||
limitations: ["Static references do not establish runtime targets."],
|
||||
});
|
||||
|
||||
export const compactCallTargets = (references: JsonValue[] = []) => ({
|
||||
call_site: "0x401000",
|
||||
procedure: "0x401000",
|
||||
status: references.length === 0 ? "unresolved" : "resolved-indirect",
|
||||
mechanism: "computed",
|
||||
targets:
|
||||
references.length === 0
|
||||
? []
|
||||
: [
|
||||
{
|
||||
address: "0x402000",
|
||||
procedure: null,
|
||||
status: "resolved-indirect",
|
||||
basis: "provider-reference",
|
||||
references,
|
||||
},
|
||||
],
|
||||
limitations: ["Static references do not establish runtime targets."],
|
||||
});
|
||||
@@ -13,6 +13,33 @@ const reference = z.strictObject({
|
||||
indirect: z.boolean(),
|
||||
computed: z.boolean(),
|
||||
operand_index: z.number().int(),
|
||||
source_address: z.string().min(1).nullable().optional(),
|
||||
data: z.boolean().nullable().optional(),
|
||||
read: z.boolean().nullable().optional(),
|
||||
write: z.boolean().nullable().optional(),
|
||||
primary: z
|
||||
.boolean()
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
"Provider primary-reference status, not proof of literal addressing or a runtime access. Null or omission means unavailable.",
|
||||
),
|
||||
provenance: z
|
||||
.string()
|
||||
.min(1)
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
"Provider subsystem reporting this reference; null or omission means unavailable.",
|
||||
),
|
||||
source: z
|
||||
.string()
|
||||
.min(1)
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
"Provider reference origin, such as analysis, default, imported or user_defined; null or omission means unavailable.",
|
||||
),
|
||||
});
|
||||
/** Provider-decoded facts for one instruction; operand tokens preserve their native ordering. */
|
||||
export const nativeInstructionSchema = z.strictObject({
|
||||
|
||||
@@ -3,6 +3,11 @@ import { describe, expect, it } from "vitest";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { functionDossierSchema } from "../domain/hopperValues.js";
|
||||
import {
|
||||
compactReference,
|
||||
compactInstruction,
|
||||
compactCallTargets,
|
||||
} from "../domain/native/nativeInstruction.fixture.js";
|
||||
import {
|
||||
parseGhidraFunctionResult,
|
||||
type GhidraFunctionOperation,
|
||||
@@ -118,6 +123,75 @@ describe("Ghidra function-analysis result values", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ghidra compact reference provenance", () => {
|
||||
const operations = [
|
||||
["inspect_native_instruction", compactInstruction],
|
||||
["resolve_native_call_targets", compactCallTargets],
|
||||
] as const;
|
||||
|
||||
it.each(operations)(
|
||||
"preserves complete, legacy and unknown facts in %s",
|
||||
(operation, output) => {
|
||||
const complete = compactReference();
|
||||
const legacy = Object.fromEntries(
|
||||
Object.entries(complete).filter(
|
||||
([key]) =>
|
||||
![
|
||||
"source_address",
|
||||
"data",
|
||||
"read",
|
||||
"write",
|
||||
"primary",
|
||||
"provenance",
|
||||
"source",
|
||||
].includes(key),
|
||||
),
|
||||
);
|
||||
const unknown = {
|
||||
...legacy,
|
||||
source_address: null,
|
||||
data: null,
|
||||
read: null,
|
||||
write: null,
|
||||
primary: null,
|
||||
provenance: null,
|
||||
source: null,
|
||||
};
|
||||
for (const reference of [complete, legacy, unknown]) {
|
||||
const expected = output([reference]);
|
||||
const parsed = parseGhidraFunctionResult(operation, expected);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
expect(parsed.value).toEqual(expected);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.each(operations)(
|
||||
"rejects malformed present provenance in %s",
|
||||
(operation, output) => {
|
||||
for (const [field, value] of [
|
||||
["source_address", 1],
|
||||
["data", "true"],
|
||||
["read", 1],
|
||||
["write", 0],
|
||||
["primary", "false"],
|
||||
["provenance", ""],
|
||||
["source", false],
|
||||
] as const) {
|
||||
expect(
|
||||
parseGhidraFunctionResult(
|
||||
operation,
|
||||
output([{ ...compactReference(), [field]: value }]),
|
||||
),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisOutputError" },
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra jump-table mapping contract", () => {
|
||||
it("preserves evidenced defaults separately from cases and unresolved targets", () => {
|
||||
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
|
||||
|
||||
Reference in New Issue
Block a user