From 0294aed3ddc263d8698f46c9c9c2c225d5ab07a6 Mon Sep 17 00:00:00 2001 From: xel1uz <16402913+xel1uz@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:06:02 +0100 Subject: [PATCH] buffer_format_fuzz: validate --budget --budget is a count of random elements per format, and a negative one was accepted with exit 0 and no message. `for _ in range(budget)` simply does not run, so -1, -100 and -1000000 produced exactly the run of 0 -- 1026 instead of 1106 elements for 8_8_8_8_UNORM -- and every format still reported "ok". --self-test was worse: it builds its own patterns with a hardcoded budget of 2 and never reads the option, so `--self-test --budget -100` and `--budget 1000000` both printed the same "self-test: ok" in the same 1.4 s. Before: $ python -I tools/buffer_format_fuzz.py --self-test --budget -1 self-test: ok # exit 0 $ python -I tools/buffer_format_fuzz.py --oracle hw --budget -3 hardware oracle unavailable: ... # exit 77, no GPU needed to know After, one check next to the other argument guards and before both the self-test and the oracle, so the value is validated even where it is not used: $ python -I tools/buffer_format_fuzz.py --self-test --budget -1 buffer_format_fuzz.py: error: --budget cannot be negative # exit 2 $ python -I tools/buffer_format_fuzz.py --self-test --budget 0 self-test: ok # 0 stays legal: sweeps only tests/test_buffer_format_fuzz.py (CPU only, hw_oracle stubbed) covers the negative value on both paths, that it is refused before the oracle is opened, that 0 stays legal and that -1 is otherwise indistinguishable from 0. 3 of its 7 tests fail without this change, 7 pass with it. --- tests/test_buffer_format_fuzz.py | 115 +++++++++++++++++++++++++++++++ tools/buffer_format_fuzz.py | 7 +- 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/test_buffer_format_fuzz.py diff --git a/tests/test_buffer_format_fuzz.py b/tests/test_buffer_format_fuzz.py new file mode 100644 index 000000000..37d8515cd --- /dev/null +++ b/tests/test_buffer_format_fuzz.py @@ -0,0 +1,115 @@ +"""CPU-only tests for tools/buffer_format_fuzz.py's --budget. + + python -I tests/test_buffer_format_fuzz.py + +A negative --budget used to be accepted (exit 0) and then silently skipped every random element: `for _ in +range(budget)` just does not run, so -1 and -1000000 produce exactly the run of 0 and the tool still reports "ok". +--self-test was worse: it builds its own patterns and never reads --budget at all, so 0, -100 and 1000000 all +printed the same "self-test: ok" with exit 0. A negative value is a usage error (2), raised before the self-test +runs and before the oracle is opened; 0 stays legal (the sweeps remain, no random extras are added). + +No GPU and no game file is used: the hardware path stubs hw_oracle. +""" +import contextlib +import io +import os +import random +import subprocess +import sys +import types +import unittest +from unittest.mock import patch + +REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TOOLS = os.path.join(REPO, "tools") +TOOL = os.path.join(TOOLS, "buffer_format_fuzz.py") +sys.path.insert(0, TOOLS) + +import buffer_format_fuzz as bff # noqa: E402 + +MESSAGE = "--budget cannot be negative" + + +def run(*args): + return subprocess.run([sys.executable, "-I", TOOL, *args], capture_output=True, text=True, check=False) + + +def make_hw_oracle_pkg(opened): + """A hw_oracle that answers like a missing GPU, remembering whether it was asked at all.""" + pkg = types.ModuleType("hw_oracle") + runner_mod = types.ModuleType("hw_oracle.runner") + asm_mod = types.ModuleType("hw_oracle.asm") + + class Unavailable(Exception): + pass + + def open_oracle(arch=None): + opened.append(arch) + raise Unavailable("no GPU here") + + runner_mod.Unavailable = Unavailable + runner_mod.open_oracle = open_oracle + asm_mod.family = lambda arch: "gfx11" + pkg.runner = runner_mod + pkg.asm = asm_mod + return {"hw_oracle": pkg, "hw_oracle.runner": runner_mod, "hw_oracle.asm": asm_mod} + + +def call_main(argv): + """main() with argv, hw_oracle stubbed: -> (SystemExit code, stderr, stdout).""" + mods = make_hw_oracle_pkg([]) + out, err = io.StringIO(), io.StringIO() + with patch.dict(sys.modules, mods): + with patch.object(sys, "argv", ["buffer_format_fuzz.py", *argv]): + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + with unittest.TestCase().assertRaises(SystemExit) as caught: + bff.main() + return caught.exception.code, err.getvalue(), out.getvalue() + + +class BudgetUsageErrorTests(unittest.TestCase): + def test_negative_budget_fails_the_self_test_with_2(self): + proc = run("--self-test", "--budget", "-1") + self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr) + self.assertIn(MESSAGE, proc.stderr) + self.assertNotIn("traceback", proc.stderr.lower()) + self.assertNotIn("self-test: ok", proc.stdout) + + def test_absurdly_negative_budget_fails_too(self): + proc = run("--self-test", "--budget", "-1000000") + self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr) + self.assertIn(MESSAGE, proc.stderr) + + def test_negative_budget_is_refused_before_the_oracle_is_opened(self): + code, err, out = call_main(["--oracle", "hw", "--budget", "-3"]) + self.assertEqual(code, 2, out + err) + self.assertIn(MESSAGE, err) + self.assertNotIn("hardware oracle unavailable", out) # a usage error, not "no GPU" + + def test_zero_budget_stays_legal(self): + proc = run("--self-test", "--budget", "0") + self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) + self.assertIn("self-test: ok", proc.stdout) + + +class BudgetValueTests(unittest.TestCase): + def test_zero_keeps_the_sweeps_and_adds_no_random_elements(self): + rng = random.Random(1) + sweeps = len(bff.load_patterns("8_8_8_8_UNORM", rng, 0)) + self.assertGreater(sweeps, 0) + self.assertEqual(len(bff.load_patterns("8_8_8_8_UNORM", random.Random(1), 0)), sweeps) + + def test_a_negative_budget_is_indistinguishable_from_zero(self): + """Why -1 must be a usage error rather than "just no random elements".""" + rng = random.Random(1) + zero = len(bff.load_patterns("8_8_8_8_UNORM", rng, 0)) + self.assertEqual(len(bff.load_patterns("8_8_8_8_UNORM", random.Random(1), -1)), zero) + + def test_a_budget_adds_two_elements_per_unit(self): + rng = random.Random(1) + zero = len(bff.load_patterns("8_8_8_8_UNORM", rng, 0)) + self.assertEqual(len(bff.load_patterns("8_8_8_8_UNORM", random.Random(1), 3)) - zero, 6) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/buffer_format_fuzz.py b/tools/buffer_format_fuzz.py index 2bee995d2..12af7ffaf 100644 --- a/tools/buffer_format_fuzz.py +++ b/tools/buffer_format_fuzz.py @@ -597,11 +597,16 @@ def main(): ap.add_argument("--formats", default="", help="GFX10 codes or names, comma separated (default: every GFX10 code " "the GPU family has)") ap.add_argument("--seed", type=int, default=1) - ap.add_argument("--budget", type=int, default=40, help="random elements per format on top of the sweeps") + ap.add_argument("--budget", type=int, default=40, + help="random elements per format on top of the sweeps (0: none; --self-test uses its own patterns)") ap.add_argument("--hw-arch", default=None) ap.add_argument("--negative-control", action="store_true") ap.add_argument("--self-test", action="store_true") a = ap.parse_args() + # A negative --budget is a usage error, not "no random elements": `range(-1)` runs zero times, so -1 used to be + # silently the run of 0, and --self-test never reads the value at all, so 0 and -1000000 both printed "ok". + if a.budget < 0: + ap.error("--budget cannot be negative") if a.self_test: return self_test() if a.oracle != "hw":