#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Mutation guardrails for the Phase-1.0M offline artifact.""" from __future__ import annotations import argparse import copy import importlib.util from pathlib import Path import sys def load(path: Path): spec = importlib.util.spec_from_file_location("phase10m", path) assert spec and spec.loader module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def require(value: bool, message: str) -> None: if not value: raise RuntimeError(message) def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--root", type=Path, required=True) args = parser.parse_args() root = args.root.resolve() validator = load(root / "tools/validate_retroarch_phase10m.py") record = validator.load_json( root / "manifests/retroarch/phase-1.0m-write-free-defaults.json" ) cases = [] def case(name): def register(function): cases.append((name, function)) return function return register @case("01 artifact is exact and ineligible") def _(): require(validator.artifact_is_exact_and_ineligible(record["artifact"]), "artifact rejected") @case("02 changed hash fails") def _(): value = dict(record["artifact"]); value["sha256"] = "0" * 64 require(not validator.artifact_is_exact_and_ineligible(value), "changed hash accepted") @case("03 execution eligibility fails") def _(): value = dict(record["artifact"]); value["execution_eligible"] = True require(not validator.artifact_is_exact_and_ineligible(value), "eligible artifact accepted") @case("04 correction is narrow") def _(): require(validator.correction_is_narrow(record["source_correction"]), "correction rejected") @case("05 lost path derivation fails") def _(): value = dict(record["source_correction"]); value["path_derivation_preserved"] = False require(not validator.correction_is_narrow(value), "lost derivation accepted") @case("06 runtime mkdir claim fails") def _(): value = dict(record["source_correction"]); value["runtime_effect"] = "PROVEN" require(not validator.correction_is_narrow(value), "runtime claim accepted") @case("07 firewall is preserved") def _(): require(validator.firewall_is_preserved(record["write_firewall"]), "firewall rejected") @case("08 missing wrapper fails") def _(): value = copy.deepcopy(record["write_firewall"]); value["linker_wrap_option_count"] = 16 require(not validator.firewall_is_preserved(value), "weakened firewall accepted") @case("09 successful write semantics fail") def _(): value = copy.deepcopy(record["write_firewall"]); value["wrapper_errno"] = "SUCCESS" require(not validator.firewall_is_preserved(value), "successful write accepted") @case("10 artifact audit is exact") def _(): require(validator.audit_is_exact(record["artifact_audit"]), "audit rejected") @case("11 RWX segment fails") def _(): value = copy.deepcopy(record["artifact_audit"]); value["rwx_load_segments"] = 1 require(not validator.audit_is_exact(value), "RWX accepted") @case("12 receive import fails") def _(): value = copy.deepcopy(record["artifact_audit"]); value["receive_import"] = True require(not validator.audit_is_exact(value), "receive import accepted") @case("13 GNM import fails") def _(): value = copy.deepcopy(record["artifact_audit"]); value["gnm_imports"] = ["sceGnmSubmit"] require(not validator.audit_is_exact(value), "GNM import accepted") @case("14 hardware evidence claim fails") def _(): value = copy.deepcopy(record["artifact_audit"]); value["hardware_evidence"] = True require(not validator.audit_is_exact(value), "hardware claim accepted") @case("15 all current authorizations are false") def _(): require(validator.all_false(record["current_authorizations"], validator.AUTHORIZATION_FIELDS), "authority active") @case("16 transfer authority fails") def _(): value = dict(record["current_authorizations"]); value["device_transfer_authorized"] = True require(not validator.all_false(value, validator.AUTHORIZATION_FIELDS), "transfer authority accepted") @case("17 execution authority fails") def _(): value = dict(record["current_authorizations"]); value["device_execution_authorized"] = True require(not validator.all_false(value, validator.AUTHORIZATION_FIELDS), "execution authority accepted") @case("18 no device action occurred") def _(): require(validator.all_false(record["phase_actions"], validator.DEVICE_ACTION_FIELDS), "device action recorded") @case("19 offline build is recorded") def _(): require(record["phase_actions"]["target_build_performed"] is True, "build omitted") @case("20 I04 remains unproven") def _(): require(record["inherited_runtime_contract"]["i04"] == "UNPROVEN", "I04 invented") failures = [] for name, function in cases: try: function() print(f"PASS {name}") except Exception as error: # noqa: BLE001 - mutation harness failures.append(f"{name}: {error}") print(f"FAIL {name}: {error}") if failures: return 1 print(f"Phase-1.0M guardrails passed: {len(cases)}") return 0 if __name__ == "__main__": raise SystemExit(main())