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.
This commit is contained in:
@@ -629,7 +629,7 @@ describe("function comparison Swift string literals", () => {
|
||||
).toMatchObject({
|
||||
status: "unknown",
|
||||
limitations: [
|
||||
"Swift String literal coverage differs or is partial; referenced strings and names match, but decoded literals were not compared.",
|
||||
"Swift String literal coverage differs or is partial; referenced strings and names match, and no use site decoded on both sides disagrees.",
|
||||
],
|
||||
});
|
||||
});
|
||||
@@ -670,6 +670,32 @@ describe("function comparison Swift string literals", () => {
|
||||
),
|
||||
).toMatchObject({ status: "unknown" });
|
||||
});
|
||||
|
||||
it("reports a conflicting literal at a shared use site despite partial coverage", () => {
|
||||
const partial = (base: "0x1000" | "0x2000", value: string) =>
|
||||
functionDossierSchema.parse({
|
||||
...withLiterals(base, [value]),
|
||||
swift_string_literal_omissions: [
|
||||
{
|
||||
source_address: base === "0x1000" ? "0x1004" : "0x2004",
|
||||
representation: "large",
|
||||
address: "0x3000",
|
||||
byte_length: 64,
|
||||
reason: "capacity",
|
||||
registers: ["x0", "x1"],
|
||||
words: ["0xd000000000000040", "0x8000000000002fe0"],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(
|
||||
stringsDimension(
|
||||
"b",
|
||||
partial("0x1000", "SleepDisabled"),
|
||||
"c",
|
||||
partial("0x2000", "Holding awake"),
|
||||
),
|
||||
).toMatchObject({ status: "changed" });
|
||||
});
|
||||
});
|
||||
|
||||
const expectInlineComparisonEvidence = (
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
referenceProjection,
|
||||
sorted,
|
||||
stringAndNameProjection,
|
||||
swiftLiteralSitesConflict,
|
||||
} from "./functionComparisonNormalization.js";
|
||||
import type {
|
||||
DimensionName,
|
||||
@@ -162,13 +163,20 @@ const compareStringsAndNames = (
|
||||
context,
|
||||
);
|
||||
if (complete || compared.status === "changed") return compared;
|
||||
// A different literal at a use site both dossiers decoded is a change despite partial coverage.
|
||||
if (swiftLiteralSitesConflict(left, right)) {
|
||||
const leftAll = stringAndNameProjection(left);
|
||||
const rightAll = stringAndNameProjection(right);
|
||||
if (leftAll !== null && rightAll !== null)
|
||||
return compareValues("strings_names", leftAll, rightAll, context);
|
||||
}
|
||||
return unresolvedDimension({
|
||||
dimension: "strings_names",
|
||||
links: context.links,
|
||||
leftCount: null,
|
||||
rightCount: null,
|
||||
limitations: [
|
||||
"Swift String literal coverage differs or is partial; referenced strings and names match, but decoded literals were not compared.",
|
||||
"Swift String literal coverage differs or is partial; referenced strings and names match, and no use site decoded on both sides disagrees.",
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
@@ -194,6 +194,47 @@ export const stringAndNameProjection = (
|
||||
: sorted(values);
|
||||
};
|
||||
|
||||
/** Whether both dossiers decoded different literals at one use site that neither left partial. */
|
||||
export const swiftLiteralSitesConflict = (
|
||||
left: FunctionSnapshot,
|
||||
right: FunctionSnapshot,
|
||||
): boolean => {
|
||||
const leftSites = swiftLiteralSites(left);
|
||||
const rightSites = swiftLiteralSites(right);
|
||||
for (const [site, values] of leftSites.values) {
|
||||
const other = rightSites.values.get(site);
|
||||
if (other === undefined || rightSites.omitted.has(site)) continue;
|
||||
if (canonicalJson(values) !== canonicalJson(other)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const swiftLiteralSites = (snapshot: FunctionSnapshot) => {
|
||||
const base = snapshot.dossier.procedure.address;
|
||||
const site = (sourceAddress: string, registers: readonly string[]) => {
|
||||
const offset = relativeAddress(sourceAddress, base);
|
||||
return offset === null ? null : canonicalJson([offset, registers]);
|
||||
};
|
||||
const omitted = new Set(
|
||||
(snapshot.dossier.swift_string_literal_omissions ?? []).map(
|
||||
({ source_address: sourceAddress, registers }) =>
|
||||
site(sourceAddress, registers),
|
||||
),
|
||||
);
|
||||
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 || omitted.has(key)) continue;
|
||||
values.set(
|
||||
key,
|
||||
[...(values.get(key) ?? []), literal.value].sort(
|
||||
compareUnicodeCodePoints,
|
||||
),
|
||||
);
|
||||
}
|
||||
return { values, omitted };
|
||||
};
|
||||
|
||||
export const sorted = (values: readonly unknown[]): readonly unknown[] =>
|
||||
values
|
||||
.map((value) => ({
|
||||
|
||||
Reference in New Issue
Block a user