#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Seeded one-line faults: each is applied to a fresh copy of the package and its judge must fail. A gate that cannot fail proves nothing (family methodology). Each fault names the file, the exact text replaced, and the judge: a pytest selection run with the faulty copy first on ``sys.path``. The copy is made under a temporary directory with fresh timestamps, so nothing stale is reused. A judge may be ``slow:SELECTION`` (a ``-m slow`` test: die traces, the serial tables, a game's own code), ``timeout:SELECTION`` (caught when the test does not end within two minutes: a window that never stops) or ``cmd:SCRIPT|ARG|...`` (a script run from a copy of ``scripts/`` beside the faulty package, caught on exit status 1: the frame hashes). python scripts/seeded_faults.py # every fault python scripts/seeded_faults.py --list python scripts/seeded_faults.py dpc-mask # by name """ from __future__ import annotations import argparse import os import shutil import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PACKAGE = ROOT / "src" / "a2600_python" # name: (file under the package, old text, new text, judge test selection) FAULTS = { "e0-fixed-slice": ( "cartridge/licensed.py", "self.slices = [*start_slices, 7]", "self.slices = [*start_slices, 6]", "tests/test_schemes.py::test_e0_slices_and_fixed_last", ), "3f-latches-reads": ( "cartridge/licensed.py", "if write and address < 0x40:", "if address < 0x40:", "tests/test_schemes.py::test_3f_latches_on_writes_below_40_only", ), "fe-polarity": ( "cartridge/licensed.py", "self.bank = 0 if value & 0x20 else 0x1000", "self.bank = 0x1000 if value & 0x20 else 0", "tests/test_schemes.py::test_fe_takes_d5_of_the_byte_after_01fe", ), "e7-block-select": ( "cartridge/licensed.py", "self.block = address & 3", "self.block = 0", "tests/test_schemes.py::test_e7_rom_pages_ram_windows_and_fixed_area", ), "e7-12k-map": ( "cartridge/licensed.py", "image = b\"\".join(pages[:2] + pages[:2] + pages[2:])", "image = b\"\".join(pages[:2] + pages[2:4] + pages[2:])", "tests/test_schemes.py::test_e7_12k_maps_pages_0_1_twice", ), "cv-windows-swapped": ( "cartridge/licensed.py", " if offset < 0x400:\n return self.ram[offset]", " if offset >= 0x400:\n return self.ram[offset & 0x3FF]", "tests/test_schemes.py::test_commavid_ram_read_low_write_high", ), "ua-bank-bit": ( "cartridge/licensed.py", "self.bank = (address & 0x40) << 6", "self.bank = (address & 0x20) << 7", "tests/test_schemes.py::test_ua_banks_from_0220_and_0240", ), "f0-no-step": ( "cartridge/licensed.py", "self.bank = (self.bank + 1) & 15", "self.bank = self.bank & 15", "tests/test_schemes.py::test_f0_steps_on_each_1ff0_access", ), "dpc-rng-tap": ( "cartridge/dpc.py", "(r >> 7) ^ (r >> 5) ^ (r >> 4) ^ (r >> 3)", "(r >> 7) ^ (r >> 5) ^ (r >> 4) ^ (r >> 2)", "tests/test_schemes.py::test_dpc_random_number_generator_has_255_states", ), "dpc-mask": ( "cartridge/dpc.py", " elif not shown:\n data = 0\n", " elif False:\n data = 0\n", "tests/test_schemes.py::test_dpc_fetcher_reads_decrement_and_mask_by_the_flag", ), "ar-four-transitions": ( "cartridge/supercharger.py", "WRITE_TRANSITIONS = 5", "WRITE_TRANSITIONS = 4", "tests/test_schemes.py::test_supercharger_writes_on_the_fifth_address_transition", ), "ar-counts-cycles": ( "cartridge/supercharger.py", " if address != self.last_address:\n", " if True:\n", "tests/test_schemes.py::test_supercharger_counts_transitions_not_cycles", ), # -- milestone 6: controllers, the SaveKey, palettes, the window ------------ "driving-pins-swapped": ( "controllers.py", "GRAY = tuple((1 - (n >> 1 & 1)) | (1 - ((n >> 1 ^ n) & 1)) << 1 for n in range(4))", "GRAY = tuple((1 - (n >> 1 & 1)) << 1 | (1 - ((n >> 1 ^ n) & 1)) for n in range(4))", "tests/test_controllers.py::test_the_driving_controller_reads_nocash_s_gray_code", ), "booster-wrong-pads": ( "controllers.py", " first = 2 * self.port\n", " first = 2 * (1 - self.port)\n", "tests/test_controllers.py::test_a_booster_grip_button_charges_its_pad_after_the_dump", ), "paddle-half-supply": ( "controllers.py", "NOCASH_LINES_AT_POT_MAX = 380", "NOCASH_LINES_AT_POT_MAX = 742", "tests/test_inputs.py::test_a_paddle_trips_after_its_rc_time", ), "savekey-no-page-rollover": ( "savekey.py", "self.pointer = base | ((self.pointer + 1) & (PAGE - 1))", "self.pointer = (self.pointer + 1) % SIZE", "tests/test_controllers.py::test_a_page_write_rolls_over_inside_its_64_byte_page", ), "savekey-acks-while-busy": ( "savekey.py", "self.state = CONTROL if cycle >= self.busy_until else IDLE", "self.state = CONTROL", "tests/test_controllers.py::test_the_chip_does_not_acknowledge_during_its_write_cycle", ), "savekey-any-chip-select": ( "savekey.py", "or (value >> 1) & 7 != CHIP_SELECT:", ":", "tests/test_controllers.py::test_another_control_code_or_chip_select_is_ignored", ), "savekey-no-read-wrap": ( "savekey.py", "self.pointer = (self.pointer + 1) % SIZE", "self.pointer = self.pointer + 1", "tests/test_controllers.py::test_a_sequential_read_wraps_from_the_last_byte_to_the_first", ), "savekey-sda-on-pin-2": ( "savekey.py", "self.sda_bit = 1 << (shift + 2) # pin 3", "self.sda_bit = 1 << (shift + 1) # pin 3", "slow:tests/test_controllers.py::test_go_fish_reads_its_block_back_through_the_savekey", ), "pal-hue-1-coloured": ( "palettes.py", "(0.000, 0.000), (0.000, 0.000), (-0.222, 0.032)", "(0.000, 0.000), (0.100, 0.000), (-0.222, 0.032)", "tests/test_frontend.py::test_palette_structure_follows_nocash", ), "window-never-stops": ( "play.py", "if args.frames is not None and frames >= args.frames:", "if args.frames is not None and frames >= args.frames and False:", "timeout:tests/test_frontend.py::test_the_window_runs_headless_and_saves_its_last_frame", ), # -- milestone 7: rows and revision fields ---------------------------------- "secam-colour-switch": ( "machine.py", 'VCS_SECAM = Machine("Atari VCS (SECAM)", SECAM, certified=False, palette="secam", switches=0x37)', 'VCS_SECAM = Machine("Atari VCS (SECAM)", SECAM, certified=False, palette="secam")', "tests/test_revisions.py::test_a_secam_console_has_no_colour_switch", ), "merge-always-steps": ( "tia.py", "merge_step = merge & revision.merge_steps", "merge_step = merge", "slow:tests/test_revisions.py::test_a_console_row_matches_every_cell_of_its_table[PAL_7800-early_HMOVE]", ), "strobe-never-drops": ( "tia.py", "((c.strobe_left and not revision.strobe_drops & bit) or held)", "(c.strobe_left or held)", "slow:tests/test_revisions.py::test_a_console_row_matches_every_cell_of_its_table[PAL_L6-HMOVE_RESPx_P0]", ), "vader-row-m0-only": ( "machine.py", 'TIARevision("PAL Vader (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1", strobe_drops=M0 | M1)', 'TIARevision("PAL Vader (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1", strobe_drops=M0)', "slow:tests/test_revisions.py::test_a_console_row_matches_every_cell_of_its_table[PAL_VADER-HMOVE_RESPx_M1]", ), "reference-merge-mask": ( "machine.py", " merge_steps: int = 0x1F\n", " merge_steps: int = 0x0F\n", "slow:tests/test_die_traces.py", ), "serial-judge-blind": ( "gates.py", " if mine == theirs:\n", " if True:\n", "tests/test_revisions.py::test_the_serial_judge_counts_every_cell_and_finds_a_wrong_one", ), # -- the 7800 sockets --------------------------------------------------------- "board-ignores-riot": ( "board.py", " if riot is None:\n", " if True:\n", "tests/test_sockets.py::test_the_board_uses_handed_in_chips_as_they_stand", ), "board-restarts-count": ( "board.py", " self.cycles = cycles #", " self.cycles = 0 #", "tests/test_sockets.py::test_the_board_uses_handed_in_chips_as_they_stand", ), "grid-unchecked": ( "board.py", "if machine.tia_clocks_cpu and (2", "if False and (2", "tests/test_sockets.py::test_a_2600_row_keeps_the_cycle_grid_and_a_hosted_row_does_not", ), "api-name-gone": ( "machine.py", "HOSTED_MACHINES = {m.name: m", "_HOSTED = {m.name: m", "tests/test_api.py::test_every_listed_name_exists", ), "die-gate-own-tia": ( "gates.py", " tia = tia_factory()\n", " tia = TIA()\n", "tests/test_sockets.py::test_the_die_gate_runs_on_a_supplied_tia", ), "lockstep-blind": ( "gates.py", " if ours_log[k] != theirs_log[k]:\n", " if False:\n", "tests/test_sockets.py::test_lockstep_finds_a_board_that_differs", ), # -- the DPC oscillator's clock per row ----------------------------------------- "dpc-row-clock-dropped": ( "system.py", ' options["cpu_hz"] = machine.timing.cpu_hz\n', " pass\n", "tests/test_dpc_clock.py::test_from_rom_gives_the_oscillator_the_rows_clock", ), "dpc-clock-ignored": ( "cartridge/dpc.py", "self.ticks_per_cycle = music_hz / cpu_hz", "self.ticks_per_cycle = music_hz / NTSC_CPU_HZ", "tests/test_dpc_clock.py::test_load_with_the_rows_clock_keeps_the_oscillators_hertz", ), "frame-hash-audio": ( "audio.py", "return 2 * v / (30 + v)", "return 2 * v / (31 + v)", # Pitfall! sounds from its first frame (Combat, Adventure and River # Raid are silent for 600 frames: the same fault on Combat is an # equivalent mutant there). "cmd:scripts/frame_hashes.py|--check|tests/frame_hashes.tsv|" "Pitfall! - Pitfall Harry's Jungle Adventure (USA)", ), } # fmt: skip def run(name: str) -> bool: relative, old, new, judge = FAULTS[name] with tempfile.TemporaryDirectory(prefix="a2600-fault-") as tmp: copy = Path(tmp) / "src" / "a2600_python" shutil.copytree(PACKAGE, copy, ignore=shutil.ignore_patterns("__pycache__")) for path in copy.rglob("*.py"): path.touch() # fresh timestamps target = copy / relative text = target.read_text(encoding="utf-8") if text.count(old) != 1: print(f"{name}: the seed text is not found exactly once in {relative}") return False target.write_text(text.replace(old, new), encoding="utf-8") env = {**os.environ, "PYTHONPATH": str(copy.parent)} kind, _, selection = judge.partition(":") if kind not in ("cmd", "slow", "timeout"): kind, selection = "", judge if kind == "cmd": # A script judge ("|"-separated argv): the scripts are copied beside # the faulty package, since each puts its own checkout's src first # on sys.path. shutil.copytree(ROOT / "scripts", Path(tmp) / "scripts") script, *arguments = selection.split("|") command = [sys.executable, str(Path(tmp) / script), *arguments] else: marks = ["-m", "slow"] if kind == "slow" else [] command = [sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider", "-o", f"pythonpath={copy.parent} tests", *marks, selection] # fmt: skip try: result = subprocess.run( command, cwd=ROOT, env=env, capture_output=True, text=True, check=False, timeout=120 if kind == "timeout" else 1800, ) # fmt: skip # 1: the judge failed; 2: it could not even be collected (an import # the fault broke). 5 (nothing collected) or 3-4 are errors of the # run itself, not a catch. caught = result.returncode in (1, 2) if result.returncode not in (0, 1): print(result.stdout[-2000:], result.stderr[-2000:]) except subprocess.TimeoutExpired: caught = kind == "timeout" # the judge's own bound: a window that never stops print(f"{name}: {'caught' if caught else 'MISSED'} by {selection.split('::')[-1]}") return caught def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("names", nargs="*") parser.add_argument("--list", action="store_true") args = parser.parse_args() if args.list: print("\n".join(FAULTS)) return 0 names = args.names or list(FAULTS) caught = sum(run(n) for n in names) print(f"{caught} of {len(names)} faults caught") return 0 if caught == len(names) else 1 if __name__ == "__main__": sys.exit(main())