#!/usr/bin/env python3 """disc_code_sweep.py — find code-bearing PAC payloads the onboarding may have missed (Phase-27 T7). The disc-completeness question the byte-gate is structurally blind to (R34): the build only touches binaries someone ONBOARDED, so a code payload no one onboarded is invisible to `make check-all` no matter how green it is. The Phase-27 audit found four such overlays (SC07 FILE_006/007/010/011, code at PAC entry 1 not 0). This sweeps EVERY extracted PAC payload and reports which decode as MIPS code, so the onboarded set can be reconciled against the disc rather than trusted. Method (reuses sig_image's exact rabbitizer decode): decode the first `--window` words of each raw payload as MIPS-LE, report the fraction rabbitizer calls valid. Overlay/EXE code runs ~0.97-1.00 valid; data/graphics/audio sit far lower. A payload above `--threshold` that is NOT already onboarded is a candidate the audit must explain (onboard it, or record why it is not a build binary — e.g. a type-1 blob that loads at its own address like the resident, needing load-address analysis first). COVERAGE-ASSERTED (R32): every extracted `{index}.{type}` payload is classified; the onboarded set is cross-checked so a hit that is already a binary is labelled, not re-flagged. tools/disc_code_sweep.py # sweep all types, print the report tools/disc_code_sweep.py --types 1,6,7 # only the non-type-4 unknowns """ import argparse import glob import os import re import struct import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "bfm_extract")) import sig_image # make_insn — the shared rabbitizer decode (GTE-aware) import lzss # decompress — the SAME game-semantics decoder the extractor uses REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # a raw payload is /. with NO .dec suffix; type-4 also has a .dec sibling _PAYLOAD = re.compile(r"/(\d+)\.(\d+)$") def onboarded_payloads(): """The set of payload paths already wired as a build binary (main, resident, every overlay). Read from the Makefile/overlays.mk *_EXE assignments — the single source of truth (R33). KEYED BY THE RAW PAYLOAD PATH (Phase-28 T7). A type-4 overlay's `_EXE` is the DECOMPRESSED path (`…/0.4.dec`), but this sweep iterates the RAW payloads (`…/0.4`) so it can decode the compressed layer itself. Store the `.dec`-stripped form too, or all 138 type-4 overlays — now correctly seen as code via decompression — would fail the onboarded match and flag as false HIDDEN hits.""" paths = set() for mk in ("Makefile", "config/overlays.mk"): p = os.path.join(REPO, mk) if not os.path.exists(p): continue for m in re.finditer(r"^\w+_EXE\s*:=\s*(\S+)", open(p).read(), re.M): norm = os.path.normpath(m.group(1)) paths.add(norm) if norm.endswith(".dec"): paths.add(norm[:-len(".dec")]) # the raw payload this .dec came from return paths _JR_RA = 0x03E00008 # `jr $ra` — a function return; the discriminator rabbitizer.isValid() lacks def _signals(data, window): n = min(window, len(data) // 4) if n == 0: return 0.0, 0.0, 0 ok = jr = 0 for k in range(n): word = struct.unpack_from("