startup: refuse a buffer load with no operands instead of indexing an empty list #2352

Merged
ChazY11 merged 1 commit from upstream-pr/2360 into main 2026-11-25 14:17:26 +00:00
ChazY11 commented 2026-11-25 13:36:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T13:36:34Z.


What

op_buffer_load_dword and tbuffer read ops[0] before buffer_address() can reach its own operand-count check, so an instruction with no operands at all came back as an index error from inside the handler:

before:  buffer_load_dword              -> translator crash in buffer_load_dword: IndexError
after:   buffer_load_dword              -> buffer load with 0 operands, expected 4
         buffer_load_dword v4           -> buffer load with 1 operands, expected 4
         tbuffer_load_format_xyzw       -> buffer load with 0 operands, expected 4

translator crash in … is the batch translator's marker for a translator defect (Stage.translate records any non-Unsupported exception that way), not for an instruction it does not lower, and the single-shader path does not catch it at all.

Two lines, each in the one body several handlers share: op_buffer_load_dword covers buffer_load_dword/x2/x3/x4, tbuffer covers tbuffer_load_format_x/xy/xyz/xyzw. Eight handlers.

Why the guard is "empty list" and not "not four"

A short operand list that still carries a modifier must keep refusing on the modifier. The lds form decodes to three operands, and startup_buffer_address_check.py asserts exactly that:

must_refuse_bytes(encode_mubuf(imm=8, offen=1, vaddr=2, lds=1), 'buffer modifier lds')

Checking len(ops) != 4 at the head of the body shadows that message — I tried it first and this fixture caught it, which is the reason the guard is as narrow as it is. The shared message did gain the expected count (buffer load with %d operands, expected 4), matching the phrasing the scalar handlers use; nothing asserted the old text.

Fail-first, one control per guard

Removing each guard alone, with the flag this check exists to catch:

op_buffer_load_dword head guard removed
  AssertionError: ('buffer_load_dword', (), [('translator crash in buffer_load_dword: IndexError', 'ps')])

tbuffer head guard removed
  AssertionError: ('tbuffer_load_format_x', (), [('translator crash in tbuffer_load_format_x: IndexError', 'ps')])

both restored
  startup gfx2hlsl operand arity: 33 accepted instructions, 99 scalar refusals and 16 load refusals   exit 0

The check

tests/startup_gfx2hlsl_handler_arity_check.py gains a refuse() helper for handlers whose accepted form needs a bound descriptor, and sixteen assertions: the empty and one-operand form of each of the eight loads. It asserts three things per case — exactly one refusal, no emitted block, and that the refusal is not translator crash … — so a handler that starts raising something else fails here too.

Verification

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_handler_arity_check.py
out/pe-env/Scripts/python.exe -B tests/startup_buffer_address_check.py

Both exit 0, and all 57 tests/startup_gfx2hlsl_*_check.py exit 0 on this branch.

What this does not touch

The remaining handlers that index operands without a count check (image_load, image_sample*, v_cndmask_b32, v_mov_b32, s_bitcmp*, …) are unchanged: they do not share one body with the loads, so each needs its own line and its own accepted-form fixture. That is a separate slice.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6605726549 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2360). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T13:36:34Z. --- ## What `op_buffer_load_dword` and `tbuffer` read `ops[0]` before `buffer_address()` can reach its own operand-count check, so an instruction with **no operands at all** came back as an index error from inside the handler: ```text before: buffer_load_dword -> translator crash in buffer_load_dword: IndexError after: buffer_load_dword -> buffer load with 0 operands, expected 4 buffer_load_dword v4 -> buffer load with 1 operands, expected 4 tbuffer_load_format_xyzw -> buffer load with 0 operands, expected 4 ``` `translator crash in …` is the batch translator's marker for a translator defect (Stage.translate records any non-`Unsupported` exception that way), not for an instruction it does not lower, and the single-shader path does not catch it at all. Two lines, each in the one body several handlers share: `op_buffer_load_dword` covers `buffer_load_dword`/`x2`/`x3`/`x4`, `tbuffer` covers `tbuffer_load_format_x`/`xy`/`xyz`/`xyzw`. Eight handlers. ## Why the guard is "empty list" and not "not four" A short operand list that still carries a modifier must keep refusing on the modifier. The `lds` form decodes to **three** operands, and `startup_buffer_address_check.py` asserts exactly that: ```text must_refuse_bytes(encode_mubuf(imm=8, offen=1, vaddr=2, lds=1), 'buffer modifier lds') ``` Checking `len(ops) != 4` at the head of the body shadows that message — I tried it first and this fixture caught it, which is the reason the guard is as narrow as it is. The shared message did gain the expected count (`buffer load with %d operands, expected 4`), matching the phrasing the scalar handlers use; nothing asserted the old text. ## Fail-first, one control per guard Removing each guard alone, with the flag this check exists to catch: ```text op_buffer_load_dword head guard removed AssertionError: ('buffer_load_dword', (), [('translator crash in buffer_load_dword: IndexError', 'ps')]) tbuffer head guard removed AssertionError: ('tbuffer_load_format_x', (), [('translator crash in tbuffer_load_format_x: IndexError', 'ps')]) both restored startup gfx2hlsl operand arity: 33 accepted instructions, 99 scalar refusals and 16 load refusals exit 0 ``` ## The check `tests/startup_gfx2hlsl_handler_arity_check.py` gains a `refuse()` helper for handlers whose accepted form needs a bound descriptor, and sixteen assertions: the empty and one-operand form of each of the eight loads. It asserts three things per case — exactly one refusal, no emitted block, and that the refusal is not `translator crash …` — so a handler that starts raising something else fails here too. ## Verification ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_handler_arity_check.py out/pe-env/Scripts/python.exe -B tests/startup_buffer_address_check.py ``` Both exit 0, and all 57 `tests/startup_gfx2hlsl_*_check.py` exit 0 on this branch. ## What this does not touch The remaining handlers that index operands without a count check (`image_load`, `image_sample*`, `v_cndmask_b32`, `v_mov_b32`, `s_bitcmp*`, …) are unchanged: they do not share one body with the loads, so each needs its own line and its own accepted-form fixture. That is a separate slice. AI-assisted.
tedin7 commented 2026-11-25 14:05:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:39:45Z.

Review: COMMENTED


Review of head 74d9c46d4d (0 P1 / 0 P2).

Scope: tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_handler_arity_check.py only. A MUBUF or MTBUF load with an empty operand list used to hit ops[0] and surface as a translator crash (IndexError). It is now a clean Unsupported refusal. The new guard sits only on the empty-list path, so the shared check in buffer_address() still refuses the other short forms, including the lds form that decodes to three operands.

Checks (run locally):

  • python3 tests/startup_gfx2hlsl_handler_arity_check.py -> PASS (33 accepted, 99 scalar refusals, 16 load refusals)
  • python3 tests/docs_links_check.py -> PASS
  • python3 tests/bidi_controls_check.py -> PASS
  • git diff --check origin/main...HEAD -> clean

Mutations (each reverted with git checkout -- .):

  1. Disabled the if not ops: guard in op_buffer_load_dword. The test failed with translator crash in buffer_load_dword: IndexError.
  2. Replaced len(ops) != 4 with len(ops) < 0 in buffer_address(). The 1-operand case failed with the same IndexError.

Notes: the change touches Python and test files only. GitHub checks for the fixtures and the Linux Python job pass. The Windows job was still running when this was written.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6058648278 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2360#pullrequestreview-6058648278). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T13:39:45Z. **Review:** COMMENTED --- Review of head 74d9c46d4df56afd8617e50954601b89c173a378 (0 P1 / 0 P2). Scope: `tools/startup/gfx2hlsl.py` and `tests/startup_gfx2hlsl_handler_arity_check.py` only. A MUBUF or MTBUF load with an empty operand list used to hit `ops[0]` and surface as a translator crash (IndexError). It is now a clean `Unsupported` refusal. The new guard sits only on the empty-list path, so the shared check in `buffer_address()` still refuses the other short forms, including the lds form that decodes to three operands. Checks (run locally): - `python3 tests/startup_gfx2hlsl_handler_arity_check.py` -> PASS (33 accepted, 99 scalar refusals, 16 load refusals) - `python3 tests/docs_links_check.py` -> PASS - `python3 tests/bidi_controls_check.py` -> PASS - `git diff --check origin/main...HEAD` -> clean Mutations (each reverted with `git checkout -- .`): 1. Disabled the `if not ops:` guard in `op_buffer_load_dword`. The test failed with `translator crash in buffer_load_dword: IndexError`. 2. Replaced `len(ops) != 4` with `len(ops) < 0` in `buffer_address()`. The 1-operand case failed with the same IndexError. Notes: the change touches Python and test files only. GitHub checks for the fixtures and the Linux Python job pass. The Windows job was still running when this was written. AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-25 14:05:12 +00:00 (Migrated from github.com)

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


Ready behind this one: the image bodies have the same defect

Same shape as the two buffer bodies, seven handlers instead of eight: image() reads vdata, an address and a descriptor and indexed them without a count
check, so a short list reached ops[1] or ops[2]:

image_sample v[8:11], v0     before: translator crash in image_sample: IndexError
                             after:  image instruction with 2 operands, expected at least 3
image_load v[8:11]           before: translator crash in image_load: IndexError
                             after:  image instruction with 1 operands, expected at least 3

Covered: image_sample, image_sample_l, image_sample_lz, image_sample_b, image_get_lod, image_load, image_load_mip — one guard, len(ops) < 3,
placed before the first index. Fail-first control removes exactly that guard and the check reports
('image_sample', ('v[8:11]',), [('translator crash in image_sample: IndexError', 'ps')]).

It is pushed as startup/image-arity in my fork and I am not opening it as a second PR yet, because both changes add the same refuse() helper at the same
anchor in tests/startup_gfx2hlsl_handler_arity_check.py — two PRs adding it would conflict in exactly that hunk, and you would have to resolve a duplicate you
did not write. Once this one lands I will rebase and open it as the one-file change it is; if you would rather see both together, say so and I will fold it in
here instead.

Baseline check for the "did it break anything" question, since a lot of startup_* checks need a renderer and fail with no arguments: the failing set of all
148 tests/startup_*_check.py run with no arguments is identical on this branch and on main (51 each, zero added, zero removed), so the only checks that
move are the ones this PR touches.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7164330883 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2360#issuecomment-7164330883). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T13:48:58Z. --- ## Ready behind this one: the image bodies have the same defect Same shape as the two buffer bodies, seven handlers instead of eight: `image()` reads vdata, an address and a descriptor and indexed them without a count check, so a short list reached `ops[1]` or `ops[2]`: ```text image_sample v[8:11], v0 before: translator crash in image_sample: IndexError after: image instruction with 2 operands, expected at least 3 image_load v[8:11] before: translator crash in image_load: IndexError after: image instruction with 1 operands, expected at least 3 ``` Covered: `image_sample`, `image_sample_l`, `image_sample_lz`, `image_sample_b`, `image_get_lod`, `image_load`, `image_load_mip` — one guard, `len(ops) < 3`, placed before the first index. Fail-first control removes exactly that guard and the check reports `('image_sample', ('v[8:11]',), [('translator crash in image_sample: IndexError', 'ps')])`. It is pushed as `startup/image-arity` in my fork and I am **not** opening it as a second PR yet, because both changes add the same `refuse()` helper at the same anchor in `tests/startup_gfx2hlsl_handler_arity_check.py` — two PRs adding it would conflict in exactly that hunk, and you would have to resolve a duplicate you did not write. Once this one lands I will rebase and open it as the one-file change it is; if you would rather see both together, say so and I will fold it in here instead. Baseline check for the "did it break anything" question, since a lot of `startup_*` checks need a renderer and fail with no arguments: the failing set of all 148 `tests/startup_*_check.py` run with no arguments is **identical** on this branch and on `main` (51 each, zero added, zero removed), so the only checks that move are the ones this PR touches. AI-assisted.
ChazY11 commented 2026-11-25 14:05:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T13:50:06Z.


Thanks — and your second mutation is the one I would keep: disabling if not ops: only proves the empty case is covered, while
len(ops) != 4 → len(ops) < 0 proves the shared check still guards the short-but-nonempty forms. That pair is exactly the split I was worried about when
I first wrote a full len(ops) != 4 guard at the head of the body and startup_buffer_address_check.py rejected it on the lds case.

Two things to close the loop on what was still pending in your write-up:

Build and test on Windows           pass 3m40s
Assemble and translate gfx1013      pass 1m47s
Run platform-independent (Linux)    pass 2m7s

so all four jobs are green on this head; nothing is waiting on Windows.

One caution about reverting with git checkout -- . rather than the single file: in a branch like this one the working tree also holds the intended change, so a
blanket checkout drops the patch too, not just the mutation. I lost an earlier iteration of this exact PR that way — the safe order is commit first, then mutate.

Nothing else from my side; the image counterpart is pushed as startup/image-arity and waits behind this one as described above.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7164358275 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2360#issuecomment-7164358275). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T13:50:06Z. --- Thanks — and your second mutation is the one I would keep: disabling `if not ops:` only proves the empty case is covered, while `len(ops) != 4` → `len(ops) < 0` proves the **shared** check still guards the short-but-nonempty forms. That pair is exactly the split I was worried about when I first wrote a full `len(ops) != 4` guard at the head of the body and `startup_buffer_address_check.py` rejected it on the `lds` case. Two things to close the loop on what was still pending in your write-up: ```text Build and test on Windows pass 3m40s Assemble and translate gfx1013 pass 1m47s Run platform-independent (Linux) pass 2m7s ``` so all four jobs are green on this head; nothing is waiting on Windows. One caution about reverting with `git checkout -- .` rather than the single file: in a branch like this one the working tree also holds the intended change, so a blanket checkout drops the patch too, not just the mutation. I lost an earlier iteration of this exact PR that way — the safe order is commit first, then mutate. Nothing else from my side; the image counterpart is pushed as `startup/image-arity` and waits behind this one as described above. AI-assisted.
ChazY11 commented 2026-11-25 14:05:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T14:04:45Z.


Update on the image slice I said was waiting behind this PR: I took it out of your conflict path instead of waiting.

#2373 now carries it with a new check file (tests/startup_gfx2hlsl_image_arity_check.py) rather than adding a refuse() helper to startup_gfx2hlsl_handler_arity_check.py, so it touches none of the lines this PR edits and the two can land in either order. The production change is unrelated to this one — image() instead of the two buffer bodies.

If you would rather have one arity file with both families, say so and I will fold the image assertions into this PR and close #2373; the refusals are additive and the merge is mechanical. Otherwise both stand on their own.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7164723523 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2360#issuecomment-7164723523). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T14:04:45Z. --- Update on the image slice I said was waiting behind this PR: I took it out of your conflict path instead of waiting. `#2373` now carries it with a **new** check file (`tests/startup_gfx2hlsl_image_arity_check.py`) rather than adding a `refuse()` helper to `startup_gfx2hlsl_handler_arity_check.py`, so it touches none of the lines this PR edits and the two can land in either order. The production change is unrelated to this one — `image()` instead of the two buffer bodies. If you would rather have one arity file with both families, say so and I will fold the image assertions into this PR and close #2373; the refusals are additive and the merge is mechanical. Otherwise both stand on their own. AI-assisted.
upstream-sync closed this pull request 2026-11-25 15:01:43 +00:00
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!2352
No description provided.