diff --git a/AGENTS.md b/AGENTS.md index 315a3fd6..93f2ba55 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -78,6 +78,12 @@ Helper tools live in `tools/decomp-assist/`: between your object and the original. This is the main iteration loop. - `disasm_fn.py ` — disassembly with relocation-resolved call names, so you can see which real functions are being called. +- `literal_reference_diff.py [unit ...]` — advisory check of actual narrow-string + bytes passed at corresponding calls, including addresses derived from a shared + data base. Defaults to functions scored 100% in the live report; use + `--all-functions` to include other equal-sized functions. Inspect every + candidate against the assembly. This does not follow mutable pointer globals, + infer compiler aliases, or prove complete data matching; skips are reported. - `pool_diff.py` — diffs the string pool in `.data` between your object and the original, string by string. See below; this is the highest-signal tool. - `export_pyghidra.py` — regenerates `ghidra_decomp.txt`, decompiled C for the diff --git a/tools/decomp-assist/literal_reference_diff.py b/tools/decomp-assist/literal_reference_diff.py new file mode 100644 index 00000000..453d56b6 --- /dev/null +++ b/tools/decomp-assist/literal_reference_diff.py @@ -0,0 +1,331 @@ +"""Audit statically addressed narrow-string call arguments after ctxdiff. + +Usage: python literal_reference_diff.py [unit ...] [--function NAME] +--function requires exactly one unit. With no units, inspect all units in build/43U/report.json. By default, select +functions scored 100%; --all-functions includes other equal-sized functions. +Requires pyelftools and the sibling jump_table_diff.py. + +This is an advisory audit, not a matching gate. It compares bytes, not relocation +names or offsets, and follows constant addresses through GPR moves/additions and +control-flow joins. It deliberately does not dereference mutable pointer globals, +infer dynamic indices, or establish which arguments a callee actually consumes. +Only corresponding calls with the same identity are compared. Binary data can +look like a string: inspect every candidate against the original assembly. +Missing exact symbol names are explicitly skipped in report-driven selection; +compiler aliases are never guessed. No candidates does not prove complete data matching. Exit 0 means no candidates, +1 means candidates require review, and 2 means invalid/incomplete input. +""" + +import argparse +from collections import deque +from dataclasses import dataclass +import json +from pathlib import Path +import sys + +from elftools.common.exceptions import ELFError +from elftools.elf.elffile import ELFFile + +import jump_table_diff as jump + +DATA_SECTIONS = {".data", ".rodata", ".sdata", ".sdata2"} +VOLATILE_GPRS = {0, *range(3, 13)} + + +@dataclass +class LiteralObject: + info: jump.ObjectInfo + data: dict + + def relocation(self, section, offset): + entries = self.info.relocs(section) + found = [entries[p] for p in (offset, offset + 2) if p in entries] + if len(found) > 1: + raise ValueError("multiple relocations in one instruction") + return found[0] if found else None + + def string(self, pointer, limit=512): + if pointer is None or pointer[0] != "address": + return None + _, section, offset = pointer + if section not in self.info.sections: + return None + if self.info.sections[section][0] not in DATA_SECTIONS or offset < 0: + return None + data = self.data.get(section, b"") + if offset >= len(data): + return None + raw = data[offset:offset + limit] + end = raw.find(b"\0") + if end < 0: + return None + raw = raw[:end] + if not all(c >= 32 or c in (9, 10, 13) for c in raw): + return None + # A relocated word is an address, not literal character bytes. + if any(offset <= at < offset + end + 1 + for at in self.info.relocs(section)): + return None + return raw + + +def read_object(path): + info = jump.read_object(path) + with open(path, "rb") as stream: + elf = ELFFile(stream) + data = {i: s.data() for i, s in enumerate(elf.iter_sections()) + if s["sh_type"] == "SHT_PROGBITS" and s["sh_flags"] & 2} + return LiteralObject(info, data) + + +def signed(value, bits): + return value - (1 << bits) if value & (1 << (bits - 1)) else value + + +def is_call(word): + op, xo = word >> 26, (word >> 1) & 1023 + return bool(word & 1) and (op == 18 or op == 19 and xo in (16, 528)) + + + +def is_tail_call(obj, function, offset, word): + if word >> 26 != 18 or word & 1: + return False + relocation = obj.relocation(function.section, function.value + offset) + if relocation is None or relocation.kind != 10: + return False + if not 0 <= relocation.symbol < len(obj.info.symbols): + raise ValueError("tail-call relocation symbol is out of range") + symbol = obj.info.symbols[relocation.symbol] + return bool(symbol.name) and (not isinstance(symbol.section, int) + or symbol.kind == "STT_FUNC" and symbol.name != function.name) + + +def call_identity(obj, function, offset, word): + relocation = obj.relocation(function.section, function.value + offset) + if relocation is not None: + if relocation.kind != 10: # R_PPC_REL24 + return None + if not 0 <= relocation.symbol < len(obj.info.symbols): + raise ValueError("call relocation symbol is out of range") + symbol = obj.info.symbols[relocation.symbol] + return ("direct", symbol.name, relocation.addend) + if word >> 26 == 19: + return ("indirect", (word >> 1) & 1023) + # A non-relocated direct call has no reliably paired cross-object identity. + return None + + +def transfer(obj, function, offset, word, incoming): + state = dict(incoming) + op = word >> 26 + rd, ra, rb = (word >> 21) & 31, (word >> 16) & 31, (word >> 11) & 31 + xo = (word >> 1) & 1023 + immediate = signed(word & 0xffff, 16) + if is_call(word): + return {r: p for r, p in state.items() if r not in VOLATILE_GPRS} + if op in (14, 15): + relocation = obj.relocation(function.section, function.value + offset) + if relocation and relocation.kind in (4, 6, 109): + location = obj.info.location(relocation) + if location is not None: + kind = "high" if relocation.kind == 6 else "address" + state[rd] = (kind, *location) + return state + pointer = incoming.get(ra) if ra else None + if op == 14 and pointer and pointer[0] == "address": + state[rd] = ("address", pointer[1], pointer[2] + immediate) + else: + state.pop(rd, None) + elif op == 31 and xo == 444 and rd == rb: # mr = or rA,rS,rS + state.pop(ra, None) + if rd in incoming: + state[ra] = incoming[rd] + elif op in (7, 8, 12, 13, 32, 34, 40, 42): + state.pop(rd, None) + elif op in (33, 35, 41, 43): + state.pop(rd, None) + state.pop(ra, None) + elif op in (20, 21, 23, 24, 25, 26, 27, 28, 29): + state.pop(ra, None) + elif op == 46: # lmw + state = {r: p for r, p in state.items() if r < rd} + elif op in (37, 39, 45, 49, 51, 53, 55, 57, 61): + state.pop(ra, None) + elif op == 4: + # Paired-single indexed updates may write rA. Forget it conservatively. + state.pop(ra, None) + elif op in (10, 11, 16, 18, 19, 36, 38, 44, 47, 48, 50, + 52, 54, 56, 59, 60, 63): + pass + elif op == 31: + if xo in (0, 32, 144, 467, 151, 215, 407, 535, 599, 663, 727, + 598, 854): + pass + elif xo in (28, 60, 124, 284, 316, 412, 444, 476, 24, 26, 536, + 792, 824, 922, 954): + state.pop(ra, None) + elif xo in (8, 10, 11, 19, 23, 40, 75, 87, 104, 136, + 138, 200, 202, 232, 234, 235, 266, 279, 339, + 343, 459, 491): + state.pop(rd, None) + elif xo in (55, 119, 311, 375): + state.pop(rd, None) + state.pop(ra, None) + elif xo in (183, 247, 439, 567, 631, 695, 759): + state.pop(ra, None) + else: + state.clear() + else: + state.clear() + return state + + +def call_strings(obj, name): + function = obj.info.symbol(name) + if function.kind != "STT_FUNC" or function.size % 4 or function.value % 4: + raise ValueError("expected an aligned function: " + name) + code = obj.data.get(function.section, b"") + if function.value + function.size > len(code): + raise ValueError("function code is unavailable: " + name) + code = code[function.value:function.value + function.size] + words = [int.from_bytes(code[i:i + 4], "big") for i in range(0, len(code), 4)] + targets = set() + for _, entries in jump.reference_tables(obj.info, function): + targets.update(entries) + states, pending = {0: {}}, deque([0]) + + def offer(offset, state): + if offset < 0 or offset >= function.size or offset % 4: + return + if offset not in states: + states[offset] = dict(state) + pending.append(offset) + return + old = states[offset] + merged = {r: p for r, p in old.items() if state.get(r) == p} + if merged != old: + states[offset] = merged + pending.append(offset) + + while pending: + offset = pending.popleft() + word = words[offset // 4] + op, xo = word >> 26, (word >> 1) & 1023 + state = transfer(obj, function, offset, word, states[offset]) + if is_tail_call(obj, function, offset, word): + continue + if op == 18 and not word & 1: + if word & 2: + continue # Absolute branches do not name local section offsets. + offer(offset + signed(word & 0x03fffffc, 26), state) + elif op == 16: + if word & 1: + raise ValueError("conditional branch-and-link is unsupported") + if not word & 2: + offer(offset + signed(word & 0xfffc, 16), state) + if ((word >> 21) & 0x14) != 0x14: + offer(offset + 4, state) + elif op == 19 and xo in (16, 528) and not word & 1: + if xo == 528: + for target in targets: + offer(target, state) + if ((word >> 21) & 0x14) != 0x14: + offer(offset + 4, state) + else: + offer(offset + 4, state) + + result = {} + for offset, state in states.items(): + word = words[offset // 4] + if not (is_call(word) or is_tail_call(obj, function, offset, word)): + continue + identity = call_identity(obj, function, offset, word) + if identity is None: + continue + for gpr in range(3, 11): + value = obj.string(state.get(gpr)) + if value is not None: + result[(offset, gpr)] = (identity, value) + return result + + +def compare_function(source, reference, name): + if source.info.symbol(name).size != reference.info.symbol(name).size: + return 0, [], "different function sizes" + actual = call_strings(source, name) + expected = call_strings(reference, name) + checked, differences = 0, [] + for key, (callee, value) in expected.items(): + # Short/binary-looking data has especially ambiguous string semantics. + if len(value) < 3 or key not in actual or actual[key][0] != callee: + continue + checked += 1 + if value != actual[key][1]: + differences.append({"offset": key[0], "register": key[1], + "expected_hex": value.hex(), + "actual_hex": actual[key][1].hex(), + "expected": value.decode("utf-8", "replace"), + "actual": actual[key][1].decode("utf-8", "replace")}) + return checked, differences, None + + +def unit_path(name): + path = Path(name.removeprefix("main/")) + if path.is_absolute() or ".." in path.parts or not path.parts: + raise ValueError("expected a relative unit path") + return path + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("units", nargs="*") + parser.add_argument("--function", action="append", default=[]) + parser.add_argument("--all-functions", action="store_true") + parser.add_argument("--build", type=Path, default=Path("build/43U")) + parser.add_argument("--report", type=Path) + args = parser.parse_args(argv) + if args.function and len(args.units) != 1: + parser.error("--function requires exactly one unit") + output = {"checked_arguments": 0, "analyzed_functions": 0, + "skipped_functions": [], "candidates": [], "errors": []} + try: + report = json.loads((args.report or args.build / "report.json").read_text()) + selected = {str(unit_path(n)) for n in args.units} + units = {str(unit_path(u["name"])): u for u in report["units"]} + if selected - units.keys(): + raise ValueError("unit is absent from report: " + ", ".join(sorted(selected - units.keys()))) + for unit in sorted(selected or units.keys()): + source = read_object(args.build / "src" / (unit + ".o")) + reference = read_object(args.build / "obj" / (unit + ".o")) + functions = args.function or [f["name"] for f in units[unit].get("functions", []) + if args.all_functions or f.get("fuzzy_match_percent") == 100] + for name in functions: + try: + if not args.function: + missing = [label for label, obj in (("source", source), ("reference", reference)) + if not any(s.name == name and isinstance(s.section, int) and s.size + for s in obj.info.symbols)] + if missing: + output["skipped_functions"].append({ + "unit": unit, "function": name, + "reason": "missing exact symbol name in " + ", ".join(missing) + + "; no compiler-alias inference"}) + continue + checked, differences, skipped = compare_function(source, reference, name) + if skipped: + output["skipped_functions"].append({"unit": unit, "function": name, "reason": skipped}) + continue + output["analyzed_functions"] += 1 + output["checked_arguments"] += checked + output["candidates"].extend(dict(unit=unit, function=name, **d) for d in differences) + except ValueError as error: + output["errors"].append({"unit": unit, "function": name, "error": str(error)}) + except (OSError, ELFError, ValueError, KeyError, TypeError) as error: + output["errors"].append({"error": str(error)}) + print(json.dumps(output, indent=2)) + return 2 if output["errors"] else 1 if output["candidates"] else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/decomp-assist/test_literal_reference_diff.py b/tools/decomp-assist/test_literal_reference_diff.py new file mode 100644 index 00000000..f08b7d19 --- /dev/null +++ b/tools/decomp-assist/test_literal_reference_diff.py @@ -0,0 +1,285 @@ +import contextlib +from dataclasses import replace +import io +import json +from pathlib import Path +import struct +import tempfile +import unittest +from unittest.mock import patch + +import jump_table_diff as jump +import literal_reference_diff as tool + + +def words(*values): + return b"".join(struct.pack(">I", value) for value in values) + + +def fixture(value=b"P_prdc_scrl_Left\0", code=None, call_offsets=(8,), + text_id=1, data_id=2, function_offset=0, data_offset=0, + references=None): + code = code or words(0x3c800000, 0x38840000, 0x48000001, 0x4e800020) + data = b"\0" * data_offset + value + symbols = [jump.Symbol("dispatch", text_id, function_offset, len(code), "STT_FUNC"), + jump.Symbol("literal", data_id, data_offset, len(value)), + jump.Symbol("consume", "SHN_UNDEF", 0, 0, "STT_FUNC")] + relocations = {text_id: { + function_offset + 2: jump.Relocation(function_offset + 2, 6, 1, 0), + function_offset + 6: jump.Relocation(function_offset + 6, 4, 1, 0), + }} + if references is not None: + relocations[text_id] = {function_offset + at: jump.Relocation( + function_offset + at, kind, 1, addend) for at, kind, addend in references} + for at in call_offsets: + relocations[text_id][function_offset + at] = jump.Relocation( + function_offset + at, 10, 2, 0) + sections = {text_id: (".text", function_offset + len(code)), + data_id: (".data", len(data))} + return tool.LiteralObject(jump.ObjectInfo(sections, symbols, relocations), + {text_id: b"\0" * function_offset + code, data_id: data}) + + +def compare(source, reference=None): + return tool.compare_function(source, reference or fixture(), "dispatch") + + +class LiteralReferenceTests(unittest.TestCase): + def test_equal_value_with_different_section_ids_and_offsets(self): + source = fixture(text_id=4, data_id=7, function_offset=0x40, data_offset=0x20) + self.assertEqual(compare(source), (1, [], None)) + + def test_detects_different_literal_despite_identical_code(self): + source = fixture(b"wrong_pane\0") + checked, differences, skipped = compare(source) + self.assertEqual(checked, 1) + self.assertIsNone(skipped) + self.assertEqual(differences[0]["expected"], "P_prdc_scrl_Left") + self.assertEqual(differences[0]["actual"], "wrong_pane") + + def test_source_empty_string_is_not_silently_ignored(self): + self.assertEqual(compare(fixture(b"\0"))[1][0]["actual_hex"], "") + + def test_fixed_width_record_regression(self): + names = [b"B_OffBtn", b"P_JPOffBtn", b"P_CNOffBtn", b"P_CNOnBtn", + b"B_prdc_scrl_Left", b"P_prdc_scrl_Left", + b"B_prdc_scrl_Rght", b"P_prdc_scrl_Rght"] + widths = [12] * 4 + [20] * 4 + correct = b"".join(n.ljust(w, b"\0") for n, w in zip(names, widths)).ljust(132, b"\0") + broken = b"".join(n.ljust(w - 1, b"\0") for n, w in zip(names, widths)).ljust(132, b"\0") + code = words(0x3fe00000, 0x3bff0000, 0x389f0044, 0x48000001, + 0x389f006c, 0x48000001, 0x4e800020) + expected = fixture(correct, code, (12, 20)) + actual = fixture(broken, code, (12, 20)) + self.assertEqual(len(correct), len(broken)) + self.assertEqual(correct.count(0), broken.count(0)) + checked, differences, _ = compare(actual, expected) + self.assertEqual(checked, 2) + self.assertEqual([d["actual"] for d in differences], ["c_scrl_Left", "scrl_Rght"]) + self.assertEqual(compare(expected, expected), (2, [], None)) + + def test_nonvolatile_base_survives_call(self): + code = words(0x3fe00000, 0x3bff0000, 0x389f0000, 0x48000001, + 0x389f0000, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12, 20)) + self.assertEqual(compare(obj, obj), (2, [], None)) + + def test_volatile_argument_does_not_survive_call(self): + obj = fixture(code=words(0x3c800000, 0x38840000, 0x48000001, + 0x48000001, 0x4e800020), call_offsets=(8, 12)) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_move_propagates_address(self): + code = words(0x3fe00000, 0x3bff0000, 0x7fe4fb78, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_sda21_literal_address(self): + obj = fixture(code=words(0x38800000, 0x48000001, 0x4e800020), + call_offsets=(4,), references=[(0, 109, 0)]) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_high_half_alone_is_not_a_pointer(self): + obj = fixture(code=words(0x3c800000, 0x48000001, 0x4e800020), + call_offsets=(4,), references=[(2, 6, 0)]) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_section_relative_literal_symbol(self): + obj = fixture(data_offset=0x40) + obj.info.symbols[1] = jump.Symbol("", 2, 0, 0, "STT_SECTION") + for at in (2, 6): + obj.info.relocations[1][at] = replace(obj.info.relocations[1][at], addend=0x40) + self.assertEqual(compare(obj), (1, [], None)) + + def test_no_assumption_of_unique_section_names(self): + obj = fixture(text_id=3, data_id=4) + obj.info.sections[1] = (".text", 16) + obj.data[1] = words(0, 0, 0, 0) + self.assertEqual(compare(obj), (1, [], None)) + + def test_cntlzw_invalidates_its_actual_destination(self): + code = words(0x3c800000, 0x38840000, 0x7ca40034, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_paired_single_update_cannot_leave_a_stale_base(self): + obj = fixture() + fn = obj.info.symbol("dispatch") + pointer = ("address", 2, 0) + state = tool.transfer(obj, fn, 0, (4 << 26) | (31 << 16) | 38, + {31: pointer, 30: pointer}) + self.assertNotIn(31, state) + self.assertEqual(state[30], pointer) + + def test_does_not_dereference_mutable_pointer_globals(self): + code = words(0x3c800000, 0x38840000, 0x80840000, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_relocated_data_word_is_not_character_bytes(self): + obj = fixture(b"ABCDEFG\0") + obj.info.relocations[2] = {0: jump.Relocation(0, 1, 0, 0)} + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_unterminated_and_control_data_are_not_strings(self): + for value in (b"unterminated", b"a\x01b\0"): + obj = fixture(value) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_negative_and_out_of_bounds_addresses_are_not_strings(self): + obj = fixture() + for offset in (-1, 999): + self.assertIsNone(obj.string(("address", 2, offset))) + + def test_different_callees_are_not_compared(self): + obj = fixture(b"different\0") + obj.info.symbols[2] = replace(obj.info.symbols[2], name="other_consumer") + self.assertEqual(compare(obj), (0, [], None)) + + def test_named_direct_tail_call_is_observed(self): + obj = fixture(code=words(0x3c800000, 0x38840000, 0x48000000), + call_offsets=(8,)) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_internal_branch_is_not_misclassified_as_tail_call(self): + obj = fixture(code=words(0x3c800000, 0x38840000, 0x48000000), + call_offsets=(8,)) + obj.info.relocations[1][8] = jump.Relocation(8, 10, 0, 0) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_indirect_call_is_observed(self): + obj = fixture(code=words(0x3c800000, 0x38840000, 0x4e800421, 0x4e800020), + call_offsets=()) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def branch_fixture(self, second_offset): + code = words(0x3fe00000, 0x3bff0000, 0x4182000c, 0x389f0000, + 0x48000008, 0x389f0000 | second_offset, 0x48000001, 0x4e800020) + return fixture(b"first\0\0\0second\0", code, (24,)) + + def test_conflicting_control_flow_addresses_are_unknown(self): + obj = self.branch_fixture(8) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_identical_control_flow_addresses_are_retained(self): + obj = self.branch_fixture(0) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_backward_loop_converges(self): + code = words(0x3fe00000, 0x3bff0000, 0x389f0000, + 0x48000001, 0x4082fff8, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_conditional_return_keeps_fallthrough(self): + code = words(0x3c800000, 0x38840000, 0x4d820020, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_unconditional_branch_skips_dead_call(self): + code = words(0x3c800000, 0x38840000, 0x42800008, 0x48000001, 0x4e800020) + obj = fixture(code=code, call_offsets=(12,)) + self.assertEqual(compare(obj, obj), (0, [], None)) + + def test_different_function_sizes_are_reported_as_skipped(self): + obj = fixture(code=words(0x3c800000, 0x38840000, 0x48000001, 0x60000000, 0x4e800020)) + self.assertEqual(compare(obj), (0, [], "different function sizes")) + + def test_duplicate_function_is_invalid(self): + obj = fixture() + obj.info.symbols.append(obj.info.symbols[0]) + with self.assertRaisesRegex(ValueError, "one defined symbol"): + compare(obj) + + def test_invalid_code_extent_is_rejected(self): + obj = fixture() + obj.data[1] = b"" + with self.assertRaisesRegex(ValueError, "code is unavailable"): + compare(obj) + + def test_conditional_link_is_explicitly_unsupported(self): + obj = fixture(code=words(0x41820009, 0x60000000, 0x4e800020), + call_offsets=(), references=[]) + with self.assertRaisesRegex(ValueError, "branch-and-link"): + compare(obj, obj) + + def test_switch_table_targets_are_reached(self): + code = words(0x3fe00000, 0x3bff0000, 0x4e800420, + 0x389f0000, 0x48000001, 0x4e800020, + 0x389f0008, 0x48000001, 0x4e800020) + obj = fixture(b"first\0\0\0second\0\0\0" + bytes(8), code, (16, 28)) + obj.info.symbols.append(jump.Symbol("jumptable_case", 2, 16, 8)) + obj.info.relocations[2] = { + 16: jump.Relocation(16, 1, 0, 12), + 20: jump.Relocation(20, 1, 0, 24), + } + self.assertEqual(compare(obj, obj), (2, [], None)) + + def test_bad_relocation_index_is_invalid(self): + obj = fixture() + obj.info.relocations[1][2] = replace(obj.info.relocations[1][2], symbol=999) + with self.assertRaisesRegex(ValueError, "symbol index"): + compare(obj) + + def test_shift_jis_bytes_are_preserved(self): + obj = fixture(bytes.fromhex("837483408343838b96bc82cc89fce28282f08c9f8f6f0a00")) + self.assertEqual(compare(obj, obj), (1, [], None)) + + def test_automatic_missing_name_is_disclosed_not_guessed(self): + with tempfile.TemporaryDirectory() as directory: + report = Path(directory) / "report.json" + report.write_text(json.dumps({"units": [{"name": "main/demo", "functions": [ + {"name": "compiler_alias", "fuzzy_match_percent": 100}]}]})) + output = io.StringIO() + with patch.object(tool, "read_object", return_value=fixture()), contextlib.redirect_stdout(output): + result = tool.main(["demo", "--report", str(report)]) + self.assertEqual(result, 0) + parsed = json.loads(output.getvalue()) + self.assertEqual(parsed["checked_arguments"], 0) + self.assertEqual(len(parsed["skipped_functions"]), 1) + output = io.StringIO() + with patch.object(tool, "read_object", return_value=fixture()), contextlib.redirect_stdout(output): + result = tool.main(["demo", "--report", str(report), "--function", "compiler_alias"]) + self.assertEqual(result, 2) + + def test_cli_reports_candidates_and_nonzero_exit(self): + with tempfile.TemporaryDirectory() as directory: + report = Path(directory) / "report.json" + report.write_text(json.dumps({"units": [{"name": "main/demo", "functions": [ + {"name": "dispatch", "fuzzy_match_percent": 100}]}]})) + output = io.StringIO() + def read(path): + return fixture(b"wrong\0") if "src" in path.parts else fixture() + with patch.object(tool, "read_object", side_effect=read), contextlib.redirect_stdout(output): + result = tool.main(["demo", "--report", str(report)]) + self.assertEqual(result, 1) + self.assertEqual(len(json.loads(output.getvalue())["candidates"]), 1) + + def test_rejects_unsafe_unit_paths(self): + for name in ("../outside", "/absolute"): + with self.assertRaisesRegex(ValueError, "relative unit"): + tool.unit_path(name) + + +if __name__ == "__main__": + unittest.main()