diff --git a/bridge/ghidra/ReaGhidraBridge.java b/bridge/ghidra/ReaGhidraBridge.java index 2e225152b..f780f89d6 100644 --- a/bridge/ghidra/ReaGhidraBridge.java +++ b/bridge/ghidra/ReaGhidraBridge.java @@ -3415,12 +3415,12 @@ public final class ReaGhidraBridge extends HeadlessScript { for (PcodeOp operation : instruction.getPcode()) { if (operation.getOpcode() == PcodeOp.RETURN) returns = true; } - // A jump leaves the function only toward a target outside its body; a computed jump - // without resolved targets is an indirect tail call, while a resolved jump table is not. + // A jump leaves the function only when every resolved target is outside its body; a + // computed jump without resolved targets is an indirect tail call. Address[] targets = instruction.getFlows(); boolean tailCall = flow.isJump() && !flow.isConditional() && (targets.length == 0 ? flow.isComputed() - : Arrays.stream(targets).anyMatch(target -> !function.getBody().contains(target))); + : Arrays.stream(targets).allMatch(target -> !function.getBody().contains(target))); if (!flow.isCall() && !returns && !tailCall) return pairs; // A String occupies two consecutive registers among x0-x7, or x0 and x1 when returned. int lastFirstRegister = returns ? 0 : 6; @@ -3435,18 +3435,25 @@ public final class ReaGhidraBridge extends HeadlessScript { return value != null && context.isConstant(value) ? value.getOffset() : null; } + // Follow register copies back to the defining instruction, since a select copied into an + // argument register forks just as independently as one written there directly. private boolean selectedValue(Instruction use, Register register) { Register target = register.getBaseRegister(); Address from = use.getFallFrom(); Instruction instruction = from == null ? null : currentProgram.getListing().getInstructionAt(from); while (instruction != null) { + boolean defines = false; for (Object result : instruction.getResultObjects()) { - if (result instanceof Register written && written.getBaseRegister().equals(target)) { - for (PcodeOp operation : instruction.getPcode()) { - if (operation.getOpcode() == PcodeOp.CBRANCH && operation.getInput(0).isConstant()) return true; - } - return false; + if (result instanceof Register written && written.getBaseRegister().equals(target)) defines = true; + } + if (defines) { + PcodeOp[] operations = instruction.getPcode(); + for (PcodeOp operation : operations) { + if (operation.getOpcode() == PcodeOp.CBRANCH && operation.getInput(0).isConstant()) return true; } + Register source = copiedRegister(operations); + if (source == null) return false; + target = source.getBaseRegister(); } from = instruction.getFallFrom(); instruction = from == null ? null : currentProgram.getListing().getInstructionAt(from); @@ -3454,6 +3461,14 @@ public final class ReaGhidraBridge extends HeadlessScript { return false; } + private Register copiedRegister(PcodeOp[] operations) { + if (operations.length != 1) return null; + int opcode = operations[0].getOpcode(); + Varnode input = operations[0].getInput(0); + if ((opcode != PcodeOp.COPY && opcode != PcodeOp.INT_ZEXT) || input == null || !input.isRegister()) return null; + return currentProgram.getRegister(input); + } + /** Validate the ABI layout of a pair; a small literal is decoded here, a large one only located. */ private SwiftLiteralLayout swiftLiteralLayout(long word, long object) { int discriminator = (int) (object >>> 60); @@ -3503,10 +3518,13 @@ public final class ReaGhidraBridge extends HeadlessScript { } /** - * Decoded text is retained as UTF-16 and again as JSON text, so every large literal of one - * dossier draws five bytes per UTF-8 byte from half of the heap that was free when decoding began. + * Every large literal of one dossier draws on half of the heap that was free when decoding + * began, charged at the worst case per UTF-8 byte: two bytes of UTF-16 text, then up to six + * escaped JSON characters ("\\u0000") held as UTF-16 in Gson's growing writer buffer (up to + * twice its length), again in the returned String, and once more as UTF-8 on the socket. */ private static final class SwiftLiteralBudget { + private static final long BYTES_PER_LITERAL_BYTE = 2 + 6 * 2 * 2 + 6 * 2 + 6; private long remaining; SwiftLiteralBudget() { @@ -3515,8 +3533,8 @@ public final class ReaGhidraBridge extends HeadlessScript { } boolean take(long bytes) { - if (bytes > remaining / 5) return false; - remaining -= bytes * 5; + if (bytes > remaining / BYTES_PER_LITERAL_BYTE) return false; + remaining -= bytes * BYTES_PER_LITERAL_BYTE; return true; } diff --git a/src/domain/functionComparison.test.ts b/src/domain/functionComparison.test.ts index 272eccf22..3a258bbc4 100644 --- a/src/domain/functionComparison.test.ts +++ b/src/domain/functionComparison.test.ts @@ -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 = ( diff --git a/src/domain/functionComparisonDimensions.ts b/src/domain/functionComparisonDimensions.ts index 63cc69698..2dcafa0a7 100644 --- a/src/domain/functionComparisonDimensions.ts +++ b/src/domain/functionComparisonDimensions.ts @@ -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.", ], }); }; diff --git a/src/domain/functionComparisonNormalization.ts b/src/domain/functionComparisonNormalization.ts index 0e27b7515..03412c3ee 100644 --- a/src/domain/functionComparisonNormalization.ts +++ b/src/domain/functionComparisonNormalization.ts @@ -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(); + 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) => ({