startup: refuse a scalar instruction with the wrong operand count (33 handlers) #2097

Merged
ChazY11 merged 3 commits from upstream-pr/2104 into main 2026-11-25 00:53:18 +00:00
ChazY11 commented 2026-11-25 00:23:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:23:58Z.


What

Thirty-three handlers in tools/startup/gfx2hlsl.py index ops[0]/ops[1]/ops[2] without checking how many operands they were given, so a
malformed instruction comes back as IndexError instead of Unsupported. In this translator that is not a cosmetic difference:

  • Stage.translate records Unsupported as a refusal and any other exception as handler_error(...), labelled internal error — its own marker
    for a translator defect, which #1723's census then counts as a gap nobody can 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.

Three guards, each in the one body several handlers share:

body handlers guard
the s_cmp/s_cmpk factory 24 len(ops) != 2
op_s_and_saveexec_b64 5 (s_and/s_or/s_andn2_saveexec_b32/b64) len(ops) != 2
op_s_andn2_wrexec_b64 2 len(ops) != 2
scalar_select 2 (s_cselect_b32/b64) len(ops) != 3

Measured

The sweep feeds every op_* handler operand shapes no decoder produces (none, one to four VGPRs, a truncated register, an attribute token):

before: 33 of the 33 escaped with IndexError
after:  none escape

Valid forms are unchanged, checked directly rather than by reading the diff:

s_cmp_lt_u32 s0, s1          -> scc = s0 < s1;
s_cmpk_gt_i32 s2, s3         -> scc = int(s2) > int(s3);
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;

The check this comes with

tests/startup_gfx2hlsl_handler_arity_check.py is the fixture that produced those numbers, and it is a ratchet rather than a snapshot:

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

PENDING holds the handlers that still do not refuse, and this PR takes it from 57 to 24. Registered as startup_gfx2hlsl_handler_arity in
CMakeLists.txt, added to the platform-independent list in .github/workflows/windows.yml, and listed in AGENTS.md.

Fail-first, one control per guard

Removing exactly one guard at a time and running the check names exactly the handlers that share that body:

s_cmp factory guard   exit 1   named 24: s_cmp_eq_i32 … s_cmpk_lt_u32
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.

Two notes on scope

  • This supersedes #2063, which is the same check without the guards. I combined them because every other change in this tree ships its check
    with the behaviour; if you would rather have the test alone first, say so and I will split it back and reopen #2063 against main.
  • The remaining 24 PENDING entries are the buffer_load_* / image_* / tbuffer_* / v_* handlers, which do not share bodies — each needs its
    own line, so that is a separate slice and deliberately not here.

What this does not establish

That any of these shapes comes from a real shader. They cannot: the decoder always produces the operand form of the encoding. This is robustness of
the text entry point the unit fixtures use, not a live decode bug, and the check says so in its docstring so the next reader does not over-read the
number.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594240521 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2104). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:23:58Z. --- ## What Thirty-three handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many operands they were given, so a malformed instruction comes back as `IndexError` instead of `Unsupported`. In this translator that is not a cosmetic difference: - `Stage.translate` records `Unsupported` as a refusal and **any other exception** as `handler_error(...)`, labelled `internal error` — its own marker for a translator defect, which `#1723`'s census then counts as a gap nobody can 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. Three guards, each in the one body several handlers share: | body | handlers | guard | |---|---|---| | the `s_cmp`/`s_cmpk` factory | 24 | `len(ops) != 2` | | `op_s_and_saveexec_b64` | 5 (`s_and`/`s_or`/`s_andn2_saveexec_b32`/`b64`) | `len(ops) != 2` | | `op_s_andn2_wrexec_b64` | 2 | `len(ops) != 2` | | `scalar_select` | 2 (`s_cselect_b32`/`b64`) | `len(ops) != 3` | ## Measured The sweep feeds every `op_*` handler operand shapes no decoder produces (none, one to four VGPRs, a truncated register, an attribute token): ```text before: 33 of the 33 escaped with IndexError after: none escape ``` Valid forms are unchanged, checked directly rather than by reading the diff: ```text s_cmp_lt_u32 s0, s1 -> scc = s0 < s1; s_cmpk_gt_i32 s2, s3 -> scc = int(s2) > int(s3); 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; ``` ## The check this comes with `tests/startup_gfx2hlsl_handler_arity_check.py` is the fixture that produced those numbers, and it is a ratchet rather than a snapshot: ```text a handler outside PENDING that starts escaping -> fails (the regression this exists for) an entry in PENDING that stops escaping -> fails, "remove it from PENDING" a PENDING entry that is not a handler -> fails (a rename shows up) ``` `PENDING` holds the handlers that still do not refuse, and this PR takes it from 57 to 24. Registered as `startup_gfx2hlsl_handler_arity` in `CMakeLists.txt`, added to the platform-independent list in `.github/workflows/windows.yml`, and listed in AGENTS.md. ## Fail-first, one control per guard Removing exactly one guard at a time and running the check names exactly the handlers that share that body: ```text s_cmp factory guard exit 1 named 24: s_cmp_eq_i32 … s_cmpk_lt_u32 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. ## Two notes on scope - **This supersedes #2063**, which is the same check without the guards. I combined them because every other change in this tree ships its check with the behaviour; if you would rather have the test alone first, say so and I will split it back and reopen #2063 against `main`. - The remaining 24 `PENDING` entries are the `buffer_load_*` / `image_*` / `tbuffer_*` / `v_*` handlers, which do not share bodies — each needs its own line, so that is a separate slice and deliberately not here. ## What this does not establish That any of these shapes comes from a real shader. They cannot: the decoder always produces the operand form of the encoding. This is robustness of the text entry point the unit fixtures use, not a live decode bug, and the check says so in its docstring so the next reader does not over-read the number. AI-assisted.
upstream-sync closed this pull request 2026-11-25 01:01:42 +00:00
R0Y00 commented 2026-11-25 01:02:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T00:48:58Z.


I retained the 33 scalar dispatch guards. I replaced the generic handler probe because it skipped successful returns and assigned the same operand shapes to unrelated handlers. The revised test uses the real batch Stage path and checks 33 accepted operand lists and 99 distinct arity refusals. A control that silently drops the extra compare operand is detected.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148906780 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2104#issuecomment-7148906780). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T00:48:58Z. --- I retained the 33 scalar dispatch guards. I replaced the generic handler probe because it skipped successful returns and assigned the same operand shapes to unrelated handlers. The revised test uses the real batch Stage path and checks 33 accepted operand lists and 99 distinct arity refusals. A control that silently drops the extra compare operand is detected.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2097
No description provided.