* feat(ghidra): decode Swift String literals in function dossiers Swift passes a String as two words. Small literals keep their UTF-8 bytes inside those words, and immortal large literals keep a count plus a pointer to their bytes biased by 32. Ghidra creates no data reference for either, so analyze_function reported no strings for Swift code. On AArch64 programs that contain Swift metadata, the Ghidra bridge now runs constant propagation over the function and decodes register pairs where Swift consumes a String: consecutive argument registers at calls and tail calls, x0 and x1 at returns, and both registers of one STP. Large literals must resolve to terminated bytes inside one initialized block that is not executable. A pair whose registers both come from conditional selects is skipped, because Ghidra forks each select without correlating the condition and could otherwise pair words from different cases. Decoded values are a derivation from the ABI layout rather than observed references, so they appear in a new optional swift_string_literals field with their source instruction, registers and raw words, and in a matching inspect_analysis_view facet. referenced_strings is unchanged. Refs #2030 * fix(ghidra): bound Swift literal reads and compare decoded strings Large immortal literals are measured against the remaining heap before allocation, and a failed fit stays visible as a limitation. The canonical empty string is kept. Small and large literals now require matching address nullability, and function comparison treats decoded literals as string evidence without equating a missing record to an empty one. * fix(ghidra): budget Swift literal decoding and tighten use sites Large literals now draw on one heap budget per dossier, measured once before decoding, instead of a separate check per literal. A literal that does not fit is listed in a new swift_string_literal_omissions field with its address, byte length and reason, so consumers read omissions from structured data rather than from limitation text. strings_names comparison checks referenced strings and names first and reports a change there even when Swift literal coverage differs; it returns unknown only when those facts match and literal coverage is absent on one side or partial. A computed jump counts as a tail call only when it has no resolved target inside the function, so jump tables no longer qualify, and a return requires a RETURN p-code operation, so traps no longer qualify. Decoded literals keep control characters, which Swift strings may contain and JSON escapes. The Swift verifier builds the literal fixture for arm64 on any Mac host and now also covers the empty string and a literal containing a tab. * fix(ghidra): charge escaped JSON and follow copied selects The Swift literal budget now charges the worst case per UTF-8 byte: UTF-16 text, six escaped JSON characters held in Gson's growing buffer and in the returned String, and the UTF-8 bytes written to the socket. Literals made of control characters can no longer exceed the reserved heap. A jump counts as a tail call only when every resolved target lies outside the function. The conditional select check follows register copies back to their definition, so selects copied into argument registers are recognized like selects written there directly. strings_names reports a change when both dossiers decoded different literals at the same use site, even when either side has omissions; it returns unknown only when no such use site disagrees. * fix(ghidra): bound literal records and trace selects across branches Every Swift literal record now pays a fixed cost against the dossier budget in addition to its text, so many empty or small literals are bounded as well. The first record that does not fit is kept as an omission and decoding stops there, which keeps the omission record itself bounded; omissions now describe small or large literals. The conditional select check searches every predecessor path for the reaching definitions, following register copies, so selects made before a branch into the use block are recognized. A dossier records Swift literals and their omissions together. Coverage reads omissions first, and strings_names compares every use site that neither side omitted: a decoded literal is known where it was recorded, and an absence is known only in a dossier whose decoding examined every use site. * fix(ghidra): stop every flow at the literal budget and route omissions Ending one flow from the evaluator leaves other queued flows running, so the budget now records a shared stop that every flow checks before it examines another instruction. A record that exceeds the budget is routed by that capacity result rather than by whether text was decoded, so a small literal over budget becomes an omission instead of a literal. When coverage is partial, the per site comparison keeps each literal's representation as well as its value, matching the full strings_names projection. * fix(ghidra): keep unresolved Swift tail-call sites unknown --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
275 lines
8.6 KiB
TypeScript
275 lines
8.6 KiB
TypeScript
import { canonicalJson } from "./comparisonSemantics.js";
|
|
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
|
|
|
|
import type { FunctionSnapshot } from "./functionDossierEvidence.js";
|
|
import type { FunctionComparisonResult } from "./functionComparisonSchemas.js";
|
|
|
|
export const identityProjection = (snapshot: FunctionSnapshot) => ({
|
|
name: snapshot.dossier.procedure.name,
|
|
signature: snapshot.dossier.procedure.signature,
|
|
locals: snapshot.dossier.procedure.locals
|
|
.map(({ description }) => description)
|
|
.sort(),
|
|
});
|
|
|
|
export const functionMatch = (
|
|
left: FunctionSnapshot,
|
|
right: FunctionSnapshot,
|
|
): FunctionComparisonResult["function_match"] => {
|
|
const leftName = left.dossier.procedure.name;
|
|
const rightName = right.dossier.procedure.name;
|
|
const stableNames = !isAutoName(leftName) && !isAutoName(rightName);
|
|
if (stableNames && leftName === rightName)
|
|
return {
|
|
status: "matched",
|
|
method: "symbol",
|
|
left_name: leftName,
|
|
right_name: rightName,
|
|
};
|
|
return {
|
|
status: stableNames ? "mismatched" : "ambiguous",
|
|
method: "explicit",
|
|
left_name: leftName,
|
|
right_name: rightName,
|
|
};
|
|
};
|
|
|
|
export const normalizeCfg = (
|
|
blocks: FunctionSnapshot["dossier"]["basic_blocks"],
|
|
): readonly unknown[] | null => {
|
|
const parsed: {
|
|
block: (typeof blocks)[number];
|
|
start: bigint;
|
|
end: bigint;
|
|
}[] = [];
|
|
for (const block of blocks) {
|
|
const start = parseAddress(block.start);
|
|
const end = parseAddress(block.end);
|
|
if (start === null || end === null || end <= start) return null;
|
|
parsed.push({ block, start, end });
|
|
}
|
|
const ordered = parsed.sort((left, right) =>
|
|
left.start < right.start ? -1 : left.start > right.start ? 1 : 0,
|
|
);
|
|
if (new Set(ordered.map(({ start }) => start)).size !== ordered.length)
|
|
return null;
|
|
const indices = new Map(ordered.map(({ start }, index) => [start, index]));
|
|
const graph = [];
|
|
for (const { block, start, end } of ordered) {
|
|
const successors: number[] = [];
|
|
for (const address of block.successors) {
|
|
const parsedAddress = parseAddress(address);
|
|
if (parsedAddress === null) return null;
|
|
const index = indices.get(parsedAddress);
|
|
if (index === undefined) return null;
|
|
successors.push(index);
|
|
}
|
|
graph.push({
|
|
size: String(end - start),
|
|
successors: successors.sort((left, right) => left - right),
|
|
});
|
|
}
|
|
return graph;
|
|
};
|
|
|
|
export const referenceProjection = (snapshot: FunctionSnapshot) =>
|
|
sorted([
|
|
...snapshot.dossier.incoming_references.map((item) => ({
|
|
direction: "in",
|
|
source: item.source_procedure?.name ?? null,
|
|
target: item.target_procedure?.name ?? null,
|
|
})),
|
|
...snapshot.dossier.outgoing_references.map((item) => ({
|
|
direction: "out",
|
|
source: item.source_procedure?.name ?? null,
|
|
target: item.target_procedure?.name ?? null,
|
|
})),
|
|
]);
|
|
|
|
export const referenceKindProjection = (
|
|
snapshot: FunctionSnapshot,
|
|
): readonly unknown[] | null => {
|
|
const edges = [
|
|
...snapshot.dossier.incoming_references.map((edge) => ({
|
|
direction: "in",
|
|
edge,
|
|
})),
|
|
...snapshot.dossier.outgoing_references.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 => {
|
|
const values = snapshot.dossier.comments.map(({ address, kind, text }) => ({
|
|
offset: relativeAddress(address, snapshot.dossier.procedure.address),
|
|
kind,
|
|
text,
|
|
}));
|
|
return values.some(({ offset }) => offset === null) ? null : sorted(values);
|
|
};
|
|
|
|
export const stringAndNameProjection = (
|
|
snapshot: FunctionSnapshot,
|
|
includeSwiftLiterals = true,
|
|
): readonly unknown[] | null => {
|
|
const values = [
|
|
...snapshot.dossier.referenced_strings.map(
|
|
({ source_address: sourceAddress, value }) => ({
|
|
kind: "string",
|
|
source_offset: relativeAddress(
|
|
sourceAddress,
|
|
snapshot.dossier.procedure.address,
|
|
),
|
|
value,
|
|
}),
|
|
),
|
|
...snapshot.dossier.referenced_names.map(
|
|
({ source_address: sourceAddress, value }) => ({
|
|
kind: "name",
|
|
source_offset: relativeAddress(
|
|
sourceAddress,
|
|
snapshot.dossier.procedure.address,
|
|
),
|
|
value,
|
|
}),
|
|
),
|
|
...(includeSwiftLiterals
|
|
? (snapshot.dossier.swift_string_literals ?? [])
|
|
: []
|
|
).map(
|
|
({
|
|
source_address: sourceAddress,
|
|
value,
|
|
representation,
|
|
registers,
|
|
}) => ({
|
|
kind: "swift_string",
|
|
source_offset: relativeAddress(
|
|
sourceAddress,
|
|
snapshot.dossier.procedure.address,
|
|
),
|
|
value,
|
|
representation,
|
|
registers,
|
|
}),
|
|
),
|
|
];
|
|
return values.some(({ source_offset: offset }) => offset === null)
|
|
? null
|
|
: sorted(values);
|
|
};
|
|
|
|
/**
|
|
* A retained literal proves presence, not that every path through its site was examined.
|
|
* A differing retained fact proves a change only when the opposite dossier is complete
|
|
* and therefore establishes its absence.
|
|
*/
|
|
export const swiftLiteralSitesConflict = (
|
|
left: FunctionSnapshot,
|
|
right: FunctionSnapshot,
|
|
): boolean => {
|
|
const leftSites = swiftLiteralSites(left);
|
|
const rightSites = swiftLiteralSites(right);
|
|
for (const site of new Set([
|
|
...leftSites.values.keys(),
|
|
...rightSites.values.keys(),
|
|
])) {
|
|
const leftValues =
|
|
leftSites.values.get(site) ?? (leftSites.complete ? [] : undefined);
|
|
const rightValues =
|
|
rightSites.values.get(site) ?? (rightSites.complete ? [] : undefined);
|
|
if (leftValues === undefined || rightValues === undefined) continue;
|
|
const leftFacts = new Set(leftValues);
|
|
const rightFacts = new Set(rightValues);
|
|
if (
|
|
(rightSites.complete &&
|
|
leftValues.some((value) => !rightFacts.has(value))) ||
|
|
(leftSites.complete && rightValues.some((value) => !leftFacts.has(value)))
|
|
)
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
const swiftLiteralSites = (snapshot: FunctionSnapshot) => {
|
|
const base = snapshot.dossier.procedure.address;
|
|
const omissions = snapshot.dossier.swift_string_literal_omissions ?? [];
|
|
const site = (sourceAddress: string, registers: readonly string[]) => {
|
|
const offset = relativeAddress(sourceAddress, base);
|
|
return offset === null ? null : canonicalJson([offset, registers]);
|
|
};
|
|
// Keep the same literal facts the full strings_names projection compares.
|
|
const values = new Map<string, string[]>();
|
|
for (const literal of snapshot.dossier.swift_string_literals ?? []) {
|
|
const key = site(literal.source_address, literal.registers);
|
|
if (key === null) continue;
|
|
values.set(
|
|
key,
|
|
[
|
|
...(values.get(key) ?? []),
|
|
canonicalJson([literal.value, literal.representation]),
|
|
].sort(compareUnicodeCodePoints),
|
|
);
|
|
}
|
|
const complete =
|
|
snapshot.dossier.swift_string_literals !== undefined &&
|
|
omissions.length === 0;
|
|
return { values, complete };
|
|
};
|
|
|
|
export const sorted = (values: readonly unknown[]): readonly unknown[] =>
|
|
values
|
|
.map((value) => ({
|
|
value,
|
|
json: canonicalJson(value, "Function normalization"),
|
|
}))
|
|
.sort((left, right) => compareUnicodeCodePoints(left.json, right.json))
|
|
.map(({ value }) => value);
|
|
|
|
/** Recognize address-derived procedure names without excluding named FUN_ symbols. */
|
|
export const isAutoName = (name: string): boolean =>
|
|
/^(?:sub_[0-9a-f]+|fcn\.[0-9a-f]+|FUN_[0-9a-f]+)$/iu.test(name);
|
|
|
|
const parseAddress = (value: string): bigint | null =>
|
|
/^0x[0-9a-f]+$/iu.test(value) ? BigInt(value) : null;
|
|
|
|
const relativeAddress = (value: string, base: string): string | null => {
|
|
const parsed = parseAddress(value);
|
|
const parsedBase = parseAddress(base);
|
|
return parsed === null || parsedBase === null || parsed < parsedBase
|
|
? null
|
|
: String(parsed - parsedBase);
|
|
};
|