Skip to content

tests: ratchet the set of gfx2hlsl handlers that do not refuse malformed operands - #2063

Closed
ChazY11 wants to merge 1 commit into
R0Y00:mainfrom
ChazY11:tests/pin-handler-arity
Closed

ChazY11 wants to merge 1 commit into
R0Y00:mainfrom
ChazY11:tests/pin-handler-arity

Conversation

@ChazY11

@ChazY11 ChazY11 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

A ratchet check for an invariant that was untested: every Stage.op_* handler must answer malformed operand shapes with Unsupported, never anything else.

Why it matters, in this repository's own terms:

  • Stage.translate records Unsupported as a refusal and any other exception as handler_error(...), labelled internal error — the marker for a translator defect. #1723's census reads those as gaps 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.

Measured on main

76 handlers × 7 shapes (none, one to four VGPRs, a truncated register, an attribute token):

260 escapes across 57 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

Reachability, stated plainly: these shapes cannot come from decode_instruction, which always produces the operand form of the encoding. So this is not a live decode bug — it is robustness for the text entry point the unit fixtures use. Fixing all 57 handlers is not one reviewable change, so this PR pins the current state instead.

The ratchet

a handler outside PENDING that starts escaping     -> fails   (the regression)
a PENDING entry that stops escaping                -> fails, "remove it from PENDING"
a PENDING entry that is not a handler              -> fails   (a rename shows up)

Two controls, run by hand against tools/startup/gfx2hlsl.py:

# a compliant handler gets an unguarded operand index
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

Verification

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

Neighbours still green: startup_gfx2hlsl_swap_check.py, startup_gfx2hlsl_mad_check.py, startup_gfx2hlsl_operand_count_check.py.

Registered as startup_gfx2hlsl_handler_arity in CMakeLists.txt, added to the platform-independent list in .github/workflows/windows.yml, and listed in the AGENTS.md section so the documented list stays in step — the twelve older omissions there are #2053's subject and are not repeated in this PR.

Scope

Test only; no handler is changed. The 57 entries are the honest current state, and the list is meant to shrink.

Notes

  • AI-assisted.

Rebased after #2024, and the ratchet already earned its keep

main added two handlers while this PR was open (s_bitcmp0_b32, s_bitcmp1_b32 from #2024). The rebase into CMakeLists.txt was a two-sided
addition and both registrations are kept. Re-run after the rebase:

startup gfx2hlsl handler arity checks: 78 handlers, 57 pending      exit 0
handlers: 78   pending: 57   compliant: 21
s_bitcmp 系列是否合规: ['s_bitcmp0_b32', 's_bitcmp1_b32']

Both new handlers are already compliant, so the check passes without touching them — and if either had indexed an operand before checking its count,
it would have failed here rather than silently adding a 58th entry later. That is the behaviour this PR exists for.

Neighbours after the rebase: startup_gfx2hlsl_s_bitcmp_check.py, startup_gfx2hlsl_swap_check.py, startup_triage_run_check.py — all exit 0.

@ChazY11
ChazY11 force-pushed the tests/pin-handler-arity branch 2 times, most recently from 8ccbcd8 to f7456d7 Compare November 24, 2026 23:53
@ChazY11
ChazY11 force-pushed the tests/pin-handler-arity branch from f7456d7 to fe87cd4 Compare November 25, 2026 00:10
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Ready behind this PR: one guard that empties twenty-four of the entries

Since the list in this check only shrinks when a handler actually stops escaping, the natural follow-up is to fix a slice. The cheapest slice is the
twenty-four s_cmp_* / s_cmpk_* handlers: they are generated by one factory, and that factory indexes ops[0]/ops[1] without checking the
count, so a single guard covers all of them.

Measured on a branch stacked on this one (startup/scalar-compare-arity, pushed to my fork):

before: 24 of 24 handlers escaped with IndexError, on 5 malformed shapes each
after:  none escape;  PENDING 57 -> 33

with the fail-first control being "remove exactly that guard":

handler s_cmp_eq_i32 answers () with IndexError; it must raise Unsupported
... (24 lines)
startup gfx2hlsl handler arity checks: 24 failed        exit 1

and two-operand lowers unchanged:

s_cmp_lt_u32 s0, s1   -> scc = s0 < s1;
s_cmpk_gt_i32 s2, s3  -> scc = int(s2) > int(s3);
s_cmp_eq_u32 s4, 5    -> scc = s4 == 0x00000005u;

All 28 tests/startup_gfx2hlsl_*_check.py exit 0 there, so the guard does not disturb the shared scalar path.

Why I am not opening it as a second PR yet

gh pr create --base cannot target a branch that exists only in a fork, and opening it against main would duplicate this PR's test file. So the
branch is pushed and waiting rather than racing this one:

  • if this PR lands, the follow-up is a one-file change in gfx2hlsl.py plus the list edit, and I will open it immediately;
  • if you would rather review them together, say so and I will fold the guard into this branch — but that mixes a test-only change with a production
    one, which is why I did not do it by default.

If the slice is not the one you would choose, the sweep is 40 lines and reruns in a second, so any group of the remaining 33 is a candidate — the
s_cmp family is simply the one where a shared factory makes the fix a single line.

AI-assisted.

ChazY11 added a commit to ChazY11/Xbox2Windows that referenced this pull request Nov 25, 2026
… handlers)

Nine handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many
operands they were given, and they answer a malformed instruction with `IndexError` instead of
`Unsupported`. The batch translator records that as `internal error` -- its own marker for a translator
defect, not a missing lowering -- and the single-shader path does not catch it at all.

Three guards, each in the one body that several handlers share, cover thirty-three handlers:

    op_s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
    s_and/or/andn2_saveexec_b32/b64           one body, 5 handlers       len(ops) != 2
    s_andn2_wrexec_b32/b64                    one body, 2 handlers       len(ops) != 2
    s_cselect_b32/b64                         scalar_select, 2 handlers  len(ops) != 3

Measured with the sweep that no decoder output can produce (none, one to four VGPRs, a truncated register,
an attribute token):

    before: 33 of the 33 escaped with IndexError
    after:  none escape;  PENDING 57 -> 24

Two- and three-operand lowers are unchanged, checked directly rather than by reading the diff:

    s_cmp_lt_u32 s0, s1        -> scc = s0 < s1;
    s_and_saveexec_b32 s0, s1  -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
    s_andn2_wrexec_b32 s2, s3  -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
    s_cselect_b32 s4,s5,s6     -> s4 = scc ? s5 : s6;

Fail-first, one control per guard -- remove exactly that guard and the ratchet names exactly the handlers
that share the body:

    saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                      s_or_saveexec_b32, s_or_saveexec_b64
    wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
    cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
    restored         exit 0   78 handlers, 24 pending

All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0.

This is a follow-up to R0Y00#2063, which introduces `tests/startup_gfx2hlsl_handler_arity_check.py`; the branch
is based on it so the list edit sits next to the guards that empty those entries.
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Update: the slice is now thirty-three handlers, in three guards

Both PRs are green here, so I extended the branch that is waiting behind this one. Instead of only s_cmp*, it now covers every handler that
indexes an operand without checking the count and shares a body with its siblings:

s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
s_and/or/andn2_saveexec_b32/b64        one body,     5 handlers   len(ops) != 2
s_andn2_wrexec_b32/b64                 one body,     2 handlers   len(ops) != 2
s_cselect_b32/b64                      one body,     2 handlers   len(ops) != 3

Measured on startup/scalar-compare-arity (rebased on this PR):

before: 33 of the 33 escaped with IndexError on the malformed shapes
after:  none escape;  PENDING 57 -> 24

One control per guard, removing exactly that guard:

saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                  s_or_saveexec_b32, s_or_saveexec_b64
wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
restored         exit 0   78 handlers, 24 pending

Valid forms unchanged, checked directly:

s_and_saveexec_b32 s0, s1   -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
s_andn2_wrexec_b32 s2, s3   -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
s_cselect_b32 s4, s5, s6    -> s4 = scc ? s5 : s6;

All 28 tests/startup_gfx2hlsl_*_check.py exit 0 there. The remaining 24 entries in PENDING are the
buffer_load_* / image_* / tbuffer_* / v_* handlers, which do not share bodies, so each needs its own line — a separate slice, not this one.

Still waiting on this PR rather than racing it: gh pr create --base cannot target a branch that exists only in a fork. Say the word and I will
either open it against main as a two-commit PR or fold these three guards in here.

AI-assisted.

…med 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 R0Y00#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 R0Y00#2053's subject, not repeated here).
@ChazY11
ChazY11 force-pushed the tests/pin-handler-arity branch from fe87cd4 to b3a3951 Compare November 25, 2026 00:18
ChazY11 added a commit to ChazY11/Xbox2Windows that referenced this pull request Nov 25, 2026
… handlers)

Nine handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many
operands they were given, and they answer a malformed instruction with `IndexError` instead of
`Unsupported`. The batch translator records that as `internal error` -- its own marker for a translator
defect, not a missing lowering -- and the single-shader path does not catch it at all.

Three guards, each in the one body that several handlers share, cover thirty-three handlers:

    op_s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
    s_and/or/andn2_saveexec_b32/b64           one body, 5 handlers       len(ops) != 2
    s_andn2_wrexec_b32/b64                    one body, 2 handlers       len(ops) != 2
    s_cselect_b32/b64                         scalar_select, 2 handlers  len(ops) != 3

Measured with the sweep that no decoder output can produce (none, one to four VGPRs, a truncated register,
an attribute token):

    before: 33 of the 33 escaped with IndexError
    after:  none escape;  PENDING 57 -> 24

Two- and three-operand lowers are unchanged, checked directly rather than by reading the diff:

    s_cmp_lt_u32 s0, s1        -> scc = s0 < s1;
    s_and_saveexec_b32 s0, s1  -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
    s_andn2_wrexec_b32 s2, s3  -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
    s_cselect_b32 s4,s5,s6     -> s4 = scc ? s5 : s6;

Fail-first, one control per guard -- remove exactly that guard and the ratchet names exactly the handlers
that share the body:

    saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                      s_or_saveexec_b32, s_or_saveexec_b64
    wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
    cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
    restored         exit 0   78 handlers, 24 pending

All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0.

This is a follow-up to R0Y00#2063, which introduces `tests/startup_gfx2hlsl_handler_arity_check.py`; the branch
is based on it so the list edit sits next to the guards that empty those entries.
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2104

I opened #2104, which is this check together with the guards that empty 33 of its PENDING entries (57 → 24). I combined them because every
other change in this tree ships its check with the behaviour, and because a test-only PR has to wait for a reviewer twice — once for the fixture, once
for the fix.

Nothing about this PR's content changes: tests/startup_gfx2hlsl_handler_arity_check.py is identical to the first commit of #2104. If you would rather
review the fixture alone first, close #2104 and merge this one, then I will reopen the guards as a one-file change against main.

Sorry for the duplicate in the queue — my fault for splitting it this way to begin with.

AI-assisted.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing this one myself: #2104 carries the same check verbatim except that its PENDING list is 33 entries shorter, because the guards in that PR make those handlers refuse.

Verified rather than assumed:

tests/startup_gfx2hlsl_handler_arity_check.py
  #2063   5781 bytes, 76 quoted entries
  #2104   5162 bytes, 43 quoted entries   (subset of the above)

So nothing in this PR is lost. Keeping both in the queue asked a reviewer to compare two versions of the same file for no benefit, and the decision I offered
("close one or the other") costs someone else time that I can spend instead.

What changed my mind about which to keep: #2092 has just merged, and it is the same shape as #2104 — a production change that ships its check. That is the
shape this tree actually merges, so #2104 is the right vehicle and this one is the redundant half.

If you would rather review the fixture alone first, closing this does not block that: the fixture is the first commit of #2104 and its behaviour is identical
to the version here, apart from the list. Say so there and I will open it as a one-file PR against main.

AI-assisted.

@ChazY11 ChazY11 closed this Nov 25, 2026
ChazY11 added a commit to ChazY11/Xbox2Windows that referenced this pull request Nov 25, 2026
… handlers)

Nine handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many
operands they were given, and they answer a malformed instruction with `IndexError` instead of
`Unsupported`. The batch translator records that as `internal error` -- its own marker for a translator
defect, not a missing lowering -- and the single-shader path does not catch it at all.

Three guards, each in the one body that several handlers share, cover thirty-three handlers:

    op_s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
    s_and/or/andn2_saveexec_b32/b64           one body, 5 handlers       len(ops) != 2
    s_andn2_wrexec_b32/b64                    one body, 2 handlers       len(ops) != 2
    s_cselect_b32/b64                         scalar_select, 2 handlers  len(ops) != 3

Measured with the sweep that no decoder output can produce (none, one to four VGPRs, a truncated register,
an attribute token):

    before: 33 of the 33 escaped with IndexError
    after:  none escape;  PENDING 57 -> 24

Two- and three-operand lowers are unchanged, checked directly rather than by reading the diff:

    s_cmp_lt_u32 s0, s1        -> scc = s0 < s1;
    s_and_saveexec_b32 s0, s1  -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
    s_andn2_wrexec_b32 s2, s3  -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
    s_cselect_b32 s4,s5,s6     -> s4 = scc ? s5 : s6;

Fail-first, one control per guard -- remove exactly that guard and the ratchet names exactly the handlers
that share the body:

    saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                      s_or_saveexec_b32, s_or_saveexec_b64
    wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
    cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
    restored         exit 0   78 handlers, 24 pending

All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0.

This is a follow-up to R0Y00#2063, which introduces `tests/startup_gfx2hlsl_handler_arity_check.py`; the branch
is based on it so the list edit sits next to the guards that empty those entries.
ChazY11 added a commit to ChazY11/Xbox2Windows that referenced this pull request Nov 25, 2026
… handlers)

Nine handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many
operands they were given, and they answer a malformed instruction with `IndexError` instead of
`Unsupported`. The batch translator records that as `internal error` -- its own marker for a translator
defect, not a missing lowering -- and the single-shader path does not catch it at all.

Three guards, each in the one body that several handlers share, cover thirty-three handlers:

    op_s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
    s_and/or/andn2_saveexec_b32/b64           one body, 5 handlers       len(ops) != 2
    s_andn2_wrexec_b32/b64                    one body, 2 handlers       len(ops) != 2
    s_cselect_b32/b64                         scalar_select, 2 handlers  len(ops) != 3

Measured with the sweep that no decoder output can produce (none, one to four VGPRs, a truncated register,
an attribute token):

    before: 33 of the 33 escaped with IndexError
    after:  none escape;  PENDING 57 -> 24

Two- and three-operand lowers are unchanged, checked directly rather than by reading the diff:

    s_cmp_lt_u32 s0, s1        -> scc = s0 < s1;
    s_and_saveexec_b32 s0, s1  -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
    s_andn2_wrexec_b32 s2, s3  -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
    s_cselect_b32 s4,s5,s6     -> s4 = scc ? s5 : s6;

Fail-first, one control per guard -- remove exactly that guard and the ratchet names exactly the handlers
that share the body:

    saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                      s_or_saveexec_b32, s_or_saveexec_b64
    wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
    cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
    restored         exit 0   78 handlers, 24 pending

All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0.

This is a follow-up to R0Y00#2063, which introduces `tests/startup_gfx2hlsl_handler_arity_check.py`; the branch
is based on it so the list edit sits next to the guards that empty those entries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant