From b3a3951a3fbb4b6a51ecaae17299d9fa4c41f502 Mon Sep 17 00:00:00 2001 From: ChazY11 <246129015+ChazY11@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:50:42 +0800 Subject: [PATCH] tests: ratchet the set of gfx2hlsl handlers that do not refuse malformed operands Every `Stage.op_*` handler is fed operand shapes the decoder cannot produce: none, one to four plain VGPRs, a truncated register and an attribute token. A handler must answer each one with `Unsupported`, because that is the only exception the translator treats as a refusal: * `Stage.translate` records `Unsupported` as a refusal and any other exception as `handler_error(...)`, which labels the entry `internal error` -- the project's marker for a translator defect, not a missing lowering, so #1723's census counts it as a gap it cannot act on; * with `refusals is None` (single-shader translation) nothing is caught at all, so the same input exits with a traceback instead of a refusal line. On main the sweep finds 260 escapes across 57 of 76 handlers: IndexError: 236 TypeError: 24 IndexError buffer_load_dword (no operands) IndexError image_load v0 / v0 v1 / v0 v1 v2 TypeError s_bfm_b32 v[0 v1 'NoneType' object is not subscriptable TypeError v_fma_mixhi_f16 not enough arguments for format string These shapes cannot come from `decode_instruction`, so this is not a live decode bug; it is a robustness invariant that was untested. Fixing all 57 handlers is not one reviewable change, so this pins the current state instead: `PENDING` lists the handlers that do not hold the invariant today. * a handler outside `PENDING` that starts escaping fails the check; * an entry in `PENDING` that stops escaping also fails, with "remove it from PENDING"; * a `PENDING` entry that is not a handler fails, so a rename shows up here. Verified with two controls against `tools/startup/gfx2hlsl.py`: # a compliant handler starts indexing an operand without a length check handler s_load_dword answers () with IndexError; it must raise Unsupported -> 1 failed # a pending handler gains a length check handler v_mov_b32 now refuses every shape; remove it from PENDING -> 1 failed Command and result: out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_handler_arity_check.py startup gfx2hlsl handler arity checks: 76 handlers, 57 pending exit 0 Registered as `startup_gfx2hlsl_handler_arity` in CMakeLists.txt, added to the platform-independent run list in `.github/workflows/windows.yml`, and listed in the AGENTS.md Pure-Python section so the documented list stays in step (the twelve older omissions are #2053's subject, not repeated here). --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 2 + tests/startup_gfx2hlsl_handler_arity_check.py | 133 ++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 tests/startup_gfx2hlsl_handler_arity_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 8c10eb6845..13dbf4f40f 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -228,6 +228,7 @@ jobs: tests/startup_stream_fetch_check.py \ tests/compute_entry_layout_check.py \ tests/startup_ngg_slice_check.py \ + tests/startup_gfx2hlsl_handler_arity_check.py \ tests/run_checks_parallel_check.py build-and-test: name: Build and test on Windows diff --git a/AGENTS.md b/AGENTS.md index 9eda0505e0..e26a66fb2a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -182,6 +182,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_f32_lg_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_f32_nan_check.py ./out/pe-env/bin/python -B tests/run_checks_parallel_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_handler_arity_check.py ``` `tests/pixel_aot_check.py binary` additionally needs LLVM with gfx1013 support diff --git a/CMakeLists.txt b/CMakeLists.txt index 7de4bf1399..9b7548df38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2196,6 +2196,8 @@ if(EXISTS "${INTAKE_PYTHON}") # gfx2hlsl lowers s_bitcmp1_b32 and s_bitcmp0_b32 (SOPC): CPU evaluation of the translated SCC test. add_test(NAME startup_gfx2hlsl_s_bitcmp COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_bitcmp_check.py") + add_test(NAME startup_gfx2hlsl_handler_arity COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_handler_arity_check.py") # VOP1/VOP2 sub-dword addressing (SDWA) in the translator (#1141), checked against the pixel AOT # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B diff --git a/tests/startup_gfx2hlsl_handler_arity_check.py b/tests/startup_gfx2hlsl_handler_arity_check.py new file mode 100644 index 0000000000..25ad234bfc --- /dev/null +++ b/tests/startup_gfx2hlsl_handler_arity_check.py @@ -0,0 +1,133 @@ +"""Refuse-everything audit for the gfx2hlsl handlers (startup translator). + +Every `Stage.op_*` handler is fed operand shapes it can never receive from the decoder: no operands, one to +three plain VGPRs, a truncated register and an attribute token. A handler must answer each one with +`Unsupported`, because that is the only exception the translator treats as a refusal: + + * `Stage.translate` records `Unsupported` as a refusal, but any other exception as + `handler_error(...)`, which labels the entry `internal error` -- the project's marker for a translator + defect rather than a missing lowering (#1723 counts those as gaps); + * the single-shader path (`refusals is None`) does not catch anything, so the same input exits with a + traceback instead of a refusal line. + +These shapes cannot come from `decode_instruction`, so this is robustness rather than a live decode bug. +It is still a checkable invariant, and this is a ratchet: `PENDING` lists the handlers that do not hold it +today. A handler outside the list that starts escaping fails the check, and an entry that no longer escapes +also fails -- remove it, and the list shrinks. +""" + +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent +sys.path.insert(0, str(ROOT.parent / 'tools' / 'startup')) +sys.path.insert(0, str(ROOT.parent / 'tools')) + +import gfx2hlsl as G # noqa: E402 + +# Operand shapes no decoder output produces: empty, 1..4 plain VGPRs, a truncated register, an attr token. +SHAPES = ( + (), + ('v0',), + ('v0', 'v1'), + ('v0', 'v1', 'v2'), + ('v[0', 'v1'), + ('attr0', 'v1'), + ('s0', 'v1', 'v2', 'v3'), +) + +# Handlers that answer at least one shape with something other than Unsupported on 489d5797. Each entry is a +# handler that indexes an operand before checking the count, or formats a variable number of operands into a +# fixed string. Removing an entry here is the goal; adding one is a regression. +PENDING = ( + 'buffer_load_dword', 'buffer_load_dwordx2', 'buffer_load_dwordx3', 'buffer_load_dwordx4', + 'image_load', 'image_load_mip', 'image_sample', 'image_sample_l', 'image_sample_lz', + 's_and_saveexec_b32', 's_and_saveexec_b64', 's_andn2_saveexec_b64', 's_andn2_wrexec_b32', + 's_andn2_wrexec_b64', 's_bfm_b32', + 's_cmp_eq_i32', 's_cmp_eq_u32', 's_cmp_ge_i32', 's_cmp_ge_u32', 's_cmp_gt_i32', 's_cmp_gt_u32', + 's_cmp_le_i32', 's_cmp_le_u32', 's_cmp_lg_i32', 's_cmp_lg_u32', 's_cmp_lt_i32', 's_cmp_lt_u32', + 's_cmpk_eq_i32', 's_cmpk_eq_u32', 's_cmpk_ge_i32', 's_cmpk_ge_u32', 's_cmpk_gt_i32', 's_cmpk_gt_u32', + 's_cmpk_le_i32', 's_cmpk_le_u32', 's_cmpk_lg_i32', 's_cmpk_lg_u32', 's_cmpk_lt_i32', 's_cmpk_lt_u32', + 's_cselect_b32', 's_cselect_b64', 's_or_saveexec_b32', 's_or_saveexec_b64', + 'tbuffer_load_format_x', 'tbuffer_load_format_xy', 'tbuffer_load_format_xyz', + 'tbuffer_load_format_xyzw', + 'v_cndmask_b32', 'v_cvt_i32_f32', 'v_cvt_pkrtz_f16_f32', 'v_fma_mixhi_f16', 'v_fma_mixlo_f16', + 'v_interp_mov_f32', 'v_interp_p1_f32', 'v_interp_p2_f32', 'v_mov_b32', 'v_readfirstlane_b32', +) + + +class Probe(G.Stage): + """A Stage with the tracking state a real stage has, without a pipeline.""" + + def __init__(self): + self.lines, self.sym = [], {} + self.used_s, self.used_v, self.uniform_v = set(), set(), set() + self.exec_known = True + self.attrs = {} + self.interp_p1 = None + self.compr_params = set() + self.blocks = [] + self.pc = 0 + self.kind = 'ps' + + +def escapes(name): + """Return the shapes this handler answers with something other than Unsupported.""" + bad = [] + for ops in SHAPES: + stage = Probe() + try: + stage.one(name, ' '.join((name,) + ops)) + except G.Unsupported: + continue + except Exception as error: # noqa: BLE001 - any escape is the finding + bad.append((ops, type(error).__name__)) + return bad + + +def handler_names(): + return sorted(attribute[3:] for attribute in dir(G.Stage) if attribute.startswith('op_')) + + +def check_every_handler_refuses_malformed_operands(failures): + names = handler_names() + assert len(names) > 50, 'expected the handler set, found %d' % len(names) + + offending = {} + for name in names: + bad = escapes(name) + if bad: + offending[name] = bad + + # A handler outside the pending list must not escape: that is the regression this check exists for. + new = sorted(set(offending) - set(PENDING)) + for name in new: + failures.append('handler %s answers %r with %s; it must raise Unsupported' + % (name, offending[name][0][0], offending[name][0][1])) + + # A pending entry that now refuses can be deleted; a stale entry hides the fact that the list is not the + # current state of the file. + fixed = sorted(set(PENDING) - set(offending)) + for name in fixed: + failures.append('handler %s now refuses every shape; remove it from PENDING' % name) + + # The list must be a subset of the handlers that exist, so a rename shows up here. + for name in sorted(set(PENDING) - set(names)): + failures.append('PENDING names %s, which is not a handler' % name) + + +def main(): + failures = [] + check_every_handler_refuses_malformed_operands(failures) + if failures: + for failure in failures: + print(' ' + failure) + print('startup gfx2hlsl handler arity checks: %d failed' % len(failures)) + return 1 + print('startup gfx2hlsl handler arity checks: %d handlers, %d pending' + % (len(handler_names()), len(PENDING))) + return 0 + + +if __name__ == '__main__': + sys.exit(main())