From 263284ff4fb91004e66735bd37b3817cf3700947 Mon Sep 17 00:00:00 2001 From: elforeign <324243036+elforeign@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:34:00 -0600 Subject: [PATCH] isa, scripts: two more registry crash paths become diagnostics (#215) - A variant whose first duplicate base form is incomplete: the completeness check and the XML check now use the same base row (no KeyError). - qa rdna2 inventory and rank report a malformed encoding_variant (missing 'of', non-string operands) as an error with exit 2, not a traceback; the coverage reader checks the field types. Each has a test that fails on the previous code. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com> --- crates/isa/generate_semantics.py | 9 ++-- scripts/audit/semantic_coverage.py | 8 ++- scripts/qa/__main__.py | 14 +++++- tests/qa/test_rdna2_malformed_registry.py | 49 +++++++++++++++++++ .../test_generate_semantics.py | 9 ++++ 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 tests/qa/test_rdna2_malformed_registry.py diff --git a/crates/isa/generate_semantics.py b/crates/isa/generate_semantics.py index 00db2d0..38bf856 100755 --- a/crates/isa/generate_semantics.py +++ b/crates/isa/generate_semantics.py @@ -779,9 +779,10 @@ def _well_formed(rule): ) -def variant_row(registry, variant): +def variant_row(registry, variant, base=None): """The base form re-encoded: its declaration with the variant's identity.""" - base = next(form for form in registry["form"] if form.get("name") == variant["of"]) + if base is None: + base = next(form for form in registry["form"] if form.get("name") == variant["of"]) return dict( base, family=variant["family"], opcode=variant["opcode"], operands=variant["operands"], spec=variant["spec"], verify=variant["verify"], @@ -854,7 +855,9 @@ def _check_variants(registry, rules, documents, snapshot, seen, errors): if FORM_KEYS - set(base): errors.append(f"{label}: not checked, its form {variant['of']} is incomplete") continue - _check_xml(variant_row(registry, variant), rule, snapshot, errors) + # The same base row as the completeness check above (with duplicate + # declarations, the dict keeps the last one). + _check_xml(variant_row(registry, variant, base), rule, snapshot, errors) # Every explicit operand, destination included, has the base encoding's # data format, in the same order (VSRC1 of VOP2 is SRC1 of VOP3). base_xml = snapshot["variants"].get((base["name"], _family_encoding(base["family"]))) diff --git a/scripts/audit/semantic_coverage.py b/scripts/audit/semantic_coverage.py index 498a0e9..f7075a2 100644 --- a/scripts/audit/semantic_coverage.py +++ b/scripts/audit/semantic_coverage.py @@ -182,11 +182,15 @@ def read_declarations(registry, identified): # An encoding variant declares its base form in another encoding family. raw_variants = registry.get("encoding_variant", []) if not isinstance(raw_variants, list) or not all( - isinstance(variant, dict) and {"family", "of", "opcode", "operands"} <= set(variant) + isinstance(variant, dict) + and {"family", "of", "opcode", "operands"} <= set(variant) + and all(isinstance(variant[key], str) for key in ("family", "of", "operands")) + and type(variant["opcode"]) is int for variant in raw_variants ): raise ValueError( - f"{REGISTRY}: every encoding_variant needs family, of, opcode and operands" + f"{REGISTRY}: every encoding_variant needs string family, of and operands" + " and an integer opcode" ) variants = [ {"family": variant["family"], "name": variant["of"], "opcode": variant["opcode"], diff --git a/scripts/qa/__main__.py b/scripts/qa/__main__.py index 8876930..aeb0b48 100644 --- a/scripts/qa/__main__.py +++ b/scripts/qa/__main__.py @@ -172,7 +172,12 @@ def main(argv: list[str] | None = None) -> int: if args.command == "rdna2" and args.action == "rank": from .rdna2_rank import run_rank - return run_rank(_root(), args) + try: + return run_rank(_root(), args) + except ValueError as exc: + # A malformed registry is a diagnostic, not a traceback. + print(f"rdna2: {exc}", file=sys.stderr) + return 2 if args.command == "rdna2" and args.no_float: r.error("--no-float requires the rank action") root = _root() @@ -263,7 +268,12 @@ def main(argv: list[str] | None = None) -> int: else: print(json.dumps(payload, indent=2, sort_keys=True)) return 0 - data = load_inventories(root) + try: + data = load_inventories(root) + except ValueError as exc: + # A malformed registry is a diagnostic, not a traceback. + print(f"rdna2: {exc}", file=sys.stderr) + return 2 inv = summarize_inventory(data) exclude_families: list[str] = [] claimed_paths: list[str] = [] diff --git a/tests/qa/test_rdna2_malformed_registry.py b/tests/qa/test_rdna2_malformed_registry.py new file mode 100644 index 0000000..5cae41b --- /dev/null +++ b/tests/qa/test_rdna2_malformed_registry.py @@ -0,0 +1,49 @@ +"""A malformed encoding variant gives the planner a diagnostic, not a traceback.""" + +import contextlib +import io +import unittest +from unittest import mock + +import qa.__main__ as cli +from qa import rdna2_plan +from audit import semantic_coverage as coverage + + +def malformed_loader(variant): + def load(_root): + registry = { + "schema": 1, + "form": [{"family": "SOP2", "name": "S_ADD_U32", "opcode": 0, + "operands": "scalar", "verify": ["reference"]}], + "operand_rules": {"scalar": {"family": "SOP2", "unsupported": ["two_literals"]}}, + "encoding_variant": [variant], + } + # The real reader, as load_inventories uses it. + coverage.read_declarations(registry, {("SOP2", "S_ADD_U32"): 0}) + raise AssertionError("the malformed variant was accepted") + + return load + + +class MalformedVariant(unittest.TestCase): + def test_inventory_and_rank_report_registry_errors(self): + variants = { + "missing of": {"family": "VOP3", "opcode": 1, "operands": "x"}, + "list operands": {"family": "VOP3", "of": "S_ADD_U32", "opcode": 1, "operands": []}, + } + for action in ("inventory", "rank"): + for name, variant in variants.items(): + with self.subTest(action=action, variant=name): + stderr = io.StringIO() + loader = malformed_loader(variant) + with mock.patch.object(cli, "load_inventories", loader), \ + mock.patch.object(rdna2_plan, "load_inventories", loader), \ + contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(io.StringIO()): + code = cli.main(["rdna2", action]) + self.assertEqual(code, 2) + self.assertIn("encoding_variant needs", stderr.getvalue()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/semantics_generator/test_generate_semantics.py b/tests/semantics_generator/test_generate_semantics.py index fce2781..b2a5a1c 100644 --- a/tests/semantics_generator/test_generate_semantics.py +++ b/tests/semantics_generator/test_generate_semantics.py @@ -945,6 +945,15 @@ class Validate(unittest.TestCase): self.assertTrue(any("missing ['width']" in error for error in errors), errors) self.assertTrue(any("is incomplete" in error for error in errors), errors) + def test_a_variant_of_an_incomplete_first_duplicate_is_a_diagnostic(self): + reg = variants() + base = next(f for f in reg["form"] if f["name"] == reg["encoding_variant"][0]["of"]) + incomplete = copy.deepcopy(base) + del incomplete["width"] + reg["form"].insert(reg["form"].index(base), incomplete) + errors = self.errors(reg) + self.assertTrue(any("missing ['width']" in error for error in errors), errors) + def test_a_non_array_or_non_table_variant_is_a_diagnostic(self): reg = variants() reg["encoding_variant"] = 1