feat(ghidra): add function analysis and conformance

This commit is contained in:
N0zoM1z0
2026-07-16 04:17:08 +09:00
committed by N0zoM1z0
parent a45de27d9e
commit d8321defd7
131 changed files with 4224 additions and 301 deletions
+17 -2
View File
@@ -13,6 +13,7 @@ import {
isAutoName,
normalizeCfg,
project,
referenceKindProjection,
referenceProjection,
sorted,
stringAndNameProjection,
@@ -359,16 +360,30 @@ const compareReferences = (
rightCount: rightProjection.length,
textDelta: null,
limitations: [
"Reference endpoints differ; exact reference kinds remain unavailable.",
"Reference endpoints differ independently of provider reference-kind metadata.",
],
});
const leftKinds = referenceKindProjection(left);
const rightKinds = referenceKindProjection(right);
if (leftKinds !== null && rightKinds !== null)
return compareValues(
"references",
leftKinds,
rightKinds,
links,
true,
true,
providersDiffer,
);
return unresolvedDimension(
"references",
"unknown",
links,
leftProjection.length,
rightProjection.length,
["Reference kinds are unavailable in the current dossier contract."],
[
"At least one provider did not expose reference kinds, so equal endpoints do not prove equal edge semantics.",
],
);
};
@@ -83,6 +83,49 @@ export const referenceProjection = (snapshot: FunctionSnapshot) =>
})),
]);
export const referenceKindProjection = (
snapshot: FunctionSnapshot,
): readonly unknown[] | null => {
const edges = [
...snapshot.collections.incoming_references.items.map((edge) => ({
direction: "in",
edge,
})),
...snapshot.collections.outgoing_references.items.map((edge) => ({
direction: "out",
edge,
})),
];
if (edges.some(({ edge }) => !edge.kind.available)) return null;
return sorted(
edges.map(({ direction, edge }) => {
if (!edge.kind.available)
throw new TypeError(
"Reference-kind availability changed during projection",
);
return {
direction,
source: edge.source_procedure?.name ?? null,
target: edge.target_procedure?.name ?? null,
type: edge.kind.type,
flow: edge.kind.flow,
call: edge.kind.call,
jump: edge.kind.jump,
data: edge.kind.data,
read: edge.kind.read,
write: edge.kind.write,
indirect: edge.kind.indirect,
computed: edge.kind.computed,
conditional: edge.kind.conditional,
terminal: edge.kind.terminal,
primary: edge.kind.primary,
operand_index: edge.kind.operand_index,
external: edge.kind.external,
};
}),
);
};
export const commentProjection = (
snapshot: FunctionSnapshot,
): readonly unknown[] | null => {
+6 -1
View File
@@ -95,7 +95,12 @@ export const parseFunctionEvidence = (input: unknown): FunctionSnapshot => {
collections,
instructionScan: first.dossier.instruction_scan,
limitations: [
...new Set(pages.flatMap(({ evidence }) => evidence.limitations)),
...new Set(
pages.flatMap(({ evidence, dossier }) => [
...evidence.limitations,
...dossier.limitations,
]),
),
].sort((left, right) => left.localeCompare(right)),
};
};
+45 -7
View File
@@ -86,16 +86,28 @@ const addressedPageSchema = z
});
}
});
const unavailableSchema = z
const unavailableAnalysisFactSchema = z
.object({ available: z.literal(false), reason: z.string() })
.strict();
const procedureIdentitySchema = z
.object({ address: z.string(), name: z.string() })
export const procedureClassificationSchema = z
.object({
external: z.boolean(),
thunk: z.boolean(),
thunk_target: z.string().nullable(),
provenance: z.string().min(1),
})
.strict();
const localVariableSchema = z
export const procedureIdentitySchema = z
.object({
address: z.string(),
name: z.string(),
classification: procedureClassificationSchema.nullable().default(null),
})
.strict();
export const localVariableSchema = z
.object({
description: z.string(),
provenance: z.literal("hopper-public-python-api"),
provenance: z.string().min(1),
})
.strict();
const boundedSchema = <T extends z.ZodType>(item: T) =>
@@ -131,13 +143,37 @@ const boundedSchema = <T extends z.ZodType>(item: T) =>
});
}
});
const availableReferenceKindSchema = z
.object({
available: z.literal(true),
provenance: z.string().min(1),
type: z.string(),
flow: z.boolean(),
call: z.boolean(),
jump: z.boolean(),
data: z.boolean(),
read: z.boolean(),
write: z.boolean(),
indirect: z.boolean(),
computed: z.boolean(),
conditional: z.boolean(),
terminal: z.boolean(),
primary: z.boolean(),
operand_index: z.number().int(),
external: z.boolean(),
})
.strict();
export const referenceKindSchema = z.discriminatedUnion("available", [
unavailableAnalysisFactSchema,
availableReferenceKindSchema,
]);
const referenceEdgeSchema = z
.object({
source_address: z.string(),
target_address: z.string(),
source_procedure: procedureIdentitySchema.nullable(),
target_procedure: procedureIdentitySchema.nullable(),
kind: unavailableSchema,
kind: referenceKindSchema,
})
.strict();
export const functionDossierSchema = z
@@ -146,6 +182,7 @@ export const functionDossierSchema = z
.object({
address: z.string(),
name: z.string(),
classification: procedureClassificationSchema.nullable().default(null),
signature: z.string().nullable(),
locals: z.array(localVariableSchema),
})
@@ -226,13 +263,14 @@ export const functionDossierSchema = z
instruction_scan: z
.object({ scanned: z.number().int().min(0), truncated: z.boolean() })
.strict(),
limitations: z.array(z.string()).default([]),
})
.strict();
/** Strict analyzed-function dossier shared by provider and comparison boundaries. */
export type FunctionDossier = z.infer<typeof functionDossierSchema>;
/** Strictly parse a complete Hopper function dossier at the provider boundary. */
/** Strictly parse a complete provider-neutral function dossier. */
export const parseFunctionDossier = (
value: JsonValue,
): Result<JsonValue, AnalysisOutputError> => {