From 49e74858a624f705db909e7472053a5b30760d3c Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 01:18:54 +0530 Subject: [PATCH] Tests: Let a verifier-level xfail name the rejection it expects Every xfail is strict with raises=Exception, so a verifier-level negative fixture counted any rejection as the expected one, including one caused by a codegen regression, or by bpftool failing. A verifier entry may now carry match = "..."; the harness attaches it as a verifier_match marker and test_kernel_verifier fails, through pytest.fail (not swallowed by raises=Exception), when the program is rejected for any other reason. xdp_devmap_helpers pins its documented "invalid bpf_context access". Co-Authored-By: Claude Fable 5.1 --- pyproject.toml | 1 + tests/conftest.py | 4 ++++ tests/framework/bpf_test_case.py | 1 + tests/framework/collector.py | 2 ++ .../{vmlinux => xdp}/xdp_tx.py | 0 tests/test_config.toml | 4 +++- tests/test_verifier.py | 11 ++++++++++- 7 files changed, 21 insertions(+), 2 deletions(-) rename tests/kernel_selftest_equivalent/{vmlinux => xdp}/xdp_tx.py (100%) diff --git a/pyproject.toml b/pyproject.toml index 9307f62..3c82f19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,6 +63,7 @@ python_files = ["test_*.py"] markers = [ "verifier: requires sudo/root for kernel verifier tests (not run by default)", "vmlinux: requires vmlinux.py for current kernel", + "verifier_match: substring a verifier-level xfail expects in the rejection", ] log_cli = false diff --git a/tests/conftest.py b/tests/conftest.py index 447a786..f70dee2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -103,6 +103,10 @@ def pytest_collection_modifyitems(items): raises=Exception, ) ) + # A verifier-level xfail may name the rejection it expects; + # any other rejection is then a real failure, not an xfail. + if item_level == "verifier" and case.xfail_match: + item.add_marker(pytest.mark.verifier_match(case.xfail_match)) # ── caplog level fixture: capture ERROR+ from pythonbpf ─────────────────── diff --git a/tests/framework/bpf_test_case.py b/tests/framework/bpf_test_case.py index a993166..cc1609b 100644 --- a/tests/framework/bpf_test_case.py +++ b/tests/framework/bpf_test_case.py @@ -23,6 +23,7 @@ class BpfTestCase: is_expected_fail: bool = False xfail_reason: str = "" xfail_level: str = "ir" # one of LEVELS + xfail_match: str = "" # verifier level: substring the rejection must contain needs_vmlinux: bool = False skip_reason: str = "" diff --git a/tests/framework/collector.py b/tests/framework/collector.py index 59b9608..2d5287d 100644 --- a/tests/framework/collector.py +++ b/tests/framework/collector.py @@ -49,6 +49,7 @@ def collect_all_test_files() -> list[BpfTestCase]: is_expected_fail = xfail_entry is not None xfail_reason = xfail_entry.get("reason", "") if xfail_entry else "" xfail_level = xfail_entry.get("level", "ir") if xfail_entry else "ir" + xfail_match = xfail_entry.get("match", "") if xfail_entry else "" cases.append( BpfTestCase( @@ -56,6 +57,7 @@ def collect_all_test_files() -> list[BpfTestCase]: rel_path=rel, is_expected_fail=is_expected_fail, xfail_reason=xfail_reason, + xfail_match=xfail_match, xfail_level=xfail_level, needs_vmlinux=needs_vmlinux, ) diff --git a/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py b/tests/kernel_selftest_equivalent/xdp/xdp_tx.py similarity index 100% rename from tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py rename to tests/kernel_selftest_equivalent/xdp/xdp_tx.py diff --git a/tests/test_config.toml b/tests/test_config.toml index 719137f..1f01001 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -6,6 +6,8 @@ # level "ir" = fails during pythonbpf IR generation (exception or ERROR log) # level "llc" = IR generates but llc rejects it # level "verifier" = IR and llc both succeed, but the kernel verifier rejects it +# A verifier-level entry may add match = "..." : the rejection must contain +# that text, otherwise the test fails instead of counting as expected. # # A failure at one level implies failure at every later one, so the declared # level marks that level and all later ones xfail. @@ -71,4 +73,4 @@ # prog_tests driver asserts that a plain load *fails*. The kernel rejects it # here with "invalid bpf_context access off=20 size=4", which is the pass # condition upstream. PythonBPF has no way to set expected_attach_type yet. -"kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py" = {reason = "Negative fixture: egress_ifindex needs expected_attach_type=BPF_XDP_DEVMAP, which cannot be set yet; the verifier rejection is upstream's pass condition", level = "verifier"} +"kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py" = {reason = "Negative fixture: egress_ifindex needs expected_attach_type=BPF_XDP_DEVMAP, which cannot be set yet; the verifier rejection is upstream's pass condition", level = "verifier", match = "invalid bpf_context access"} diff --git a/tests/test_verifier.py b/tests/test_verifier.py index 3966e3f..bd54058 100644 --- a/tests/test_verifier.py +++ b/tests/test_verifier.py @@ -54,7 +54,7 @@ def _get_rejection_reason(verifier_test_file: Path, output) -> str: _verifier_test_files(), ids=_verifier_test_ids(), ) -def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog): +def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog, request): """Compile the BPF test and verify it passes the kernel verifier.""" ll_path = tmp_path / "output.ll" obj_path = tmp_path / "output.o" @@ -70,4 +70,13 @@ def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog): assert obj_path.exists() and obj_path.stat().st_size > 0 ok, output = verify_object(obj_path) + expected = request.node.get_closest_marker("verifier_match") + if not ok and expected is not None and expected.args[0] not in output.stderr: + # pytest.fail raises an OutcomeException, which the xfail marker's + # raises=Exception does not swallow: a rejection for the wrong + # reason is a failure, not the expected one. + pytest.fail( + f"{verifier_test_file.name}: rejected, but not for the expected reason " + f"{expected.args[0]!r}:\n{output.stderr}" + ) assert ok, _get_rejection_reason(verifier_test_file, output)