From 60bfaff97e5f41404ccafd8abe81866079f5fe4a Mon Sep 17 00:00:00 2001 From: Ranveer Soni Date: Tue, 24 Nov 2026 18:55:53 +0530 Subject: [PATCH] wip(qa): checkpoint compatibility gap analysis Signed-off-by: Ranveer Soni --- scripts/qa/__main__.py | 20 +++++ scripts/qa/compat_gaps.py | 141 +++++++++++++++++++++++++++++++++++ tests/qa/test_compat_gaps.py | 39 ++++++++++ 3 files changed, 200 insertions(+) create mode 100644 scripts/qa/compat_gaps.py create mode 100644 tests/qa/test_compat_gaps.py diff --git a/scripts/qa/__main__.py b/scripts/qa/__main__.py index d435a12..8e90182 100644 --- a/scripts/qa/__main__.py +++ b/scripts/qa/__main__.py @@ -63,6 +63,7 @@ from .runtime_inventory import ( compact_runtime, dumps_runtime, ) +from .compat_gaps import analyze_gaps, compact_gaps, dumps_gaps, load_dependency_report from .stale_branch import classify_sync, format_sync_reports from .verify import dumps_evidence, evidence_is_stale, load_evidence, run_profile @@ -140,6 +141,11 @@ def main(argv: list[str] | None = None) -> int: rinv.add_argument("--compact", action="store_true") rinv.add_argument("--out", default="") + gaps = sub.add_parser("compat-gaps", help="read-only dependency gap analysis (no binary exec)") + gaps.add_argument("--deps", required=True, help="JSON dependency report path") + gaps.add_argument("--compact", action="store_true") + gaps.add_argument("--out", default="") + ver = sub.add_parser("verify", help="run a verification profile") ver.add_argument("profile", choices=("quick", "cpu", "gpu", "app", "full", "release")) ver.add_argument("--revision", default="") @@ -365,6 +371,20 @@ def main(argv: list[str] | None = None) -> int: print(dumps_runtime(inv), end="") return 0 + if args.command == "compat-gaps": + deps = load_dependency_report(Path(args.deps)) + rt = build_runtime_inventory(root) + report = analyze_gaps(deps, implemented=rt.implemented, partial=rt.partial) + payload = report.to_dict() + if args.out: + Path(args.out).parent.mkdir(parents=True, exist_ok=True) + Path(args.out).write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8") + if args.compact or args.out: + print(compact_gaps(report), end="") + else: + print(dumps_gaps(report), end="") + return 0 + if args.command == "verify": if args.check_stale: evidence = load_evidence(Path(args.check_stale).read_text(encoding="utf-8")) diff --git a/scripts/qa/compat_gaps.py b/scripts/qa/compat_gaps.py new file mode 100644 index 0000000..7ad8a73 --- /dev/null +++ b/scripts/qa/compat_gaps.py @@ -0,0 +1,141 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +"""Compatibility gap analysis over read-only dependency reports. + +Consumes JSON describing imports required by authorized workloads. Never +executes input binaries. Groups missing symbols by reusable subsystem. +""" +from __future__ import annotations + +import json +from collections import defaultdict +from dataclasses import asdict, dataclass, field +from pathlib import Path +from typing import Any, Iterable + + +# Heuristic subsystem buckets for missing Win64/CRT symbols (planning only). +SUBSYSTEM_RULES: tuple[tuple[str, tuple[str, ...]], ...] = ( + ("memory", ("VirtualAlloc", "VirtualFree", "HeapAlloc", "HeapFree", "LocalAlloc", "GlobalAlloc")), + ("threading", ("CreateThread", "OpenThread", "WaitFor", "Sleep", "Tls", "CriticalSection")), + ("process", ("GetCurrentProcess", "GetProcess", "TerminateProcess", "ExitProcess", "CreateProcess")), + ("error", ("GetLastError", "SetLastError", "FormatMessage")), + ("time", ("GetSystemTime", "QueryPerformance", "GetTickCount", "Sleep")), + ("crt-math", ("sqrt", "fabs", "sin", "cos", "pow", "exp", "log", "floor", "ceil")), + ("crt-string", ("strlen", "strcmp", "strcpy", "memcpy", "memmove", "memset", "sprintf", "snprintf")), + ("file-io", ("CreateFile", "ReadFile", "WriteFile", "CloseHandle", "GetFileSize")), + ("sync", ("WaitForSingleObject", "WaitForMultipleObjects", "CreateEvent", "CreateMutex", "ReleaseMutex")), +) + + +@dataclass +class GapItem: + module: str + symbol: str + subsystem: str + status: str # missing | partial | present + note: str = "" + + +@dataclass +class GapReport: + workload: str + total_imports: int + missing: list[dict[str, Any]] = field(default_factory=list) + partial: list[dict[str, Any]] = field(default_factory=list) + present: list[dict[str, Any]] = field(default_factory=list) + by_subsystem: dict[str, list[str]] = field(default_factory=dict) + shared_primitives: list[str] = field(default_factory=list) + notes: list[str] = field(default_factory=list) + + def to_dict(self) -> dict[str, Any]: + return asdict(self) + + +def classify_subsystem(symbol: str) -> str: + for name, needles in SUBSYSTEM_RULES: + for needle in needles: + if needle.lower() in symbol.lower(): + return name + return "other" + + +def _normalize_imports(payload: dict[str, Any]) -> list[tuple[str, str]]: + """Accept {workload, imports:[{module,symbol}| "mod!sym"]} or flat list.""" + items = payload.get("imports", payload if isinstance(payload, list) else []) + out: list[tuple[str, str]] = [] + for item in items: + if isinstance(item, str): + if "!" in item: + mod, sym = item.split("!", 1) + else: + mod, sym = "unknown", item + out.append((mod, sym)) + elif isinstance(item, dict): + mod = str(item.get("module") or item.get("dll") or "unknown") + sym = str(item.get("symbol") or item.get("name") or "") + if sym: + out.append((mod, sym)) + return out + + +def analyze_gaps( + dependency_report: dict[str, Any], + *, + implemented: Iterable[str], + partial: Iterable[str], +) -> GapReport: + """Compare required imports to runtime inventory keys (`dll!symbol`).""" + impl = {k.lower() for k in implemented} + part = {k.lower() for k in partial} + workload = str(dependency_report.get("workload", "unspecified")) + imports = _normalize_imports(dependency_report) + missing: list[GapItem] = [] + partial_hits: list[GapItem] = [] + present: list[GapItem] = [] + for module, symbol in imports: + key = f"{module}!{symbol}".lower() + # Also try basename match on symbol alone against inventory keys. + symbol_keys = {k for k in impl | part if k.endswith("!" + symbol.lower())} + if key in impl or any(k in impl for k in symbol_keys): + present.append(GapItem(module, symbol, classify_subsystem(symbol), "present")) + elif key in part or any(k in part for k in symbol_keys): + partial_hits.append(GapItem(module, symbol, classify_subsystem(symbol), "partial", + "registry status is partial")) + else: + missing.append(GapItem(module, symbol, classify_subsystem(symbol), "missing")) + + by_sub: dict[str, list[str]] = defaultdict(list) + for item in missing + partial_hits: + by_sub[item.subsystem].append(f"{item.module}!{item.symbol}") + # Shared primitives: subsystems with more than one missing/partial entry. + shared = sorted(sub for sub, items in by_sub.items() if len(items) > 1 and sub != "other") + return GapReport( + workload=workload, + total_imports=len(imports), + missing=[asdict(i) for i in missing], + partial=[asdict(i) for i in partial_hits], + present=[asdict(i) for i in present], + by_subsystem={k: sorted(v) for k, v in sorted(by_sub.items())}, + shared_primitives=shared, + notes=[ + "Read-only analysis of a dependency JSON report; binaries are not executed.", + "Subsystem grouping is advisory for prioritising shared primitives.", + "Does not claim Xbox or commercial workload compatibility.", + ], + ) + + +def load_dependency_report(path: Path) -> dict[str, Any]: + return json.loads(path.read_text(encoding="utf-8")) + + +def compact_gaps(report: GapReport) -> str: + return ( + f"workload={report.workload} imports={report.total_imports} " + f"missing={len(report.missing)} partial={len(report.partial)} " + f"present={len(report.present)} shared_subsystems={len(report.shared_primitives)}\n" + ) + + +def dumps_gaps(report: GapReport) -> str: + return json.dumps(report.to_dict(), indent=2, sort_keys=True) + "\n" diff --git a/tests/qa/test_compat_gaps.py b/tests/qa/test_compat_gaps.py new file mode 100644 index 0000000..6d40add --- /dev/null +++ b/tests/qa/test_compat_gaps.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +import unittest + +from qa.compat_gaps import analyze_gaps, classify_subsystem, compact_gaps + + +class CompatGaps(unittest.TestCase): + def test_groups_missing_by_subsystem(self): + report = analyze_gaps( + { + "workload": "synthetic-a", + "imports": [ + {"module": "kernel32.dll", "symbol": "VirtualAlloc"}, + {"module": "kernel32.dll", "symbol": "VirtualFree"}, + {"module": "kernel32.dll", "symbol": "GetCurrentProcess"}, + {"module": "ucrtbase.dll", "symbol": "sqrt"}, + {"module": "kernel32.dll", "symbol": "CreateFileW"}, + ], + }, + implemented=["kernel32.dll!GetCurrentProcess"], + partial=["kernel32.dll!VirtualAlloc", "ucrtbase.dll!sqrt"], + ) + self.assertEqual(report.workload, "synthetic-a") + self.assertEqual(len(report.present), 1) + self.assertEqual(len(report.partial), 2) + self.assertEqual(len(report.missing), 2) # VirtualFree, CreateFileW + self.assertIn("memory", report.by_subsystem) + self.assertIn("memory", report.shared_primitives) + self.assertIn("binaries are not executed", " ".join(report.notes).lower()) + self.assertIn("missing=2", compact_gaps(report)) + + def test_subsystem_rules(self): + self.assertEqual(classify_subsystem("VirtualAlloc"), "memory") + self.assertEqual(classify_subsystem("sqrtf"), "crt-math") + self.assertEqual(classify_subsystem("NoSuchThing"), "other") + + +if __name__ == "__main__": + unittest.main()