shader: refuse image descriptors whose loaded words were rewritten #3807

Manually merged
github merged 3 commits from github-bot/gh-3805 into gh-pr/3805/base 2026-12-02 08:57:23 +00:00 AGit
Owner

@⁠Leyukaka on GitHub · 2026-12-02 00:51 UTC · original
Leyukaka:py-image-desc → main · +320 −6 · 4 files · 3 commits · merged 2026-12-02 08:56 UTC

Problem

s_load_dwordx8 reads an eight-word image descriptor from a descriptor table, and tools/startup/gfx2hlsl.py records one symbol per word. Stage.descriptor() names the resource by the first word only, and the image lowerings bind that resource without reading the other words back.

A source-built witness shows the defect on main: after s_mov_b32 of a literal into word 1 (format) or word 2 (size) of the loaded descriptor, image_sample_lz and image_load through that operand were still bound to the unmodified table image. The guest samples an image with another format or size; the translation silently used the original one. Further cases were bound the same way: a word taken from another descriptor, a pointer or descriptor-count value moved into a word, and a word edited in place by scalar ALU.

This is part of making the translator base more solid: a form is either translated exactly or refused.

Change

  • New Stage.image_descriptor(), used by the image sample, load, store, gather and comparison lowerings in place of descriptor(). It requires every word of the operand that held a loaded descriptor word to still hold the original one: same source, same byte position, same load tag. A descriptor loaded in two halves by two s_load_dwordx4 keeps its positions and is still accepted.
  • Refused, with a message that names the operand, the register and the word: a word replaced by other data (immediate or run-time s_mov_b32, a word of another descriptor, a pointer copy), a word modified in place by scalar ALU, and pointer or descriptor-count data in a word position.
  • set_s and the descriptor ALU path record the SGPRs where a loaded descriptor word was replaced.
  • A register that never held a descriptor word is left as before, and the compute fixed-table path keeps its own check of all eight words.
  • One expectation in tests/startup_gfx2hlsl_readfirstlane_uniform_check.py is widened: for the rewritten-descriptor cases the image access itself is now refused first, with the more precise image descriptor s[16:23]: ... message, before the uniformity proof of the read is reached.

Validation

Run on Windows (MSVC) with the dev preset:

ctest --preset dev --no-tests=error -R "startup_gfx2hlsl_(image_desc_word_witness|compute|scalar_arity|join_symbols|mask_overwrite|readfirstlane|load_desc|image_)"
  • startup_gfx2hlsl_image_desc_word_witness is new. Its witnesses (word 1 or word 2 rewritten, a word from another descriptor, pointer and descriptor-count moves, in-place ALU edits, each through image_sample_lz and image_load) failed on main and pass with this change. The test accepts a refusal or a binding that differs from the unmodified table image.
  • Its controls pass before and after: the unmodified descriptor binds the table image, a write to an SGPR outside the descriptor changes nothing, a rewritten first word is refused, and a descriptor loaded in two halves binds the table image.
  • The neighbouring gfx2hlsl tests pass: compute, scalar_arity, join_symbols, mask_overwrite, the readfirstlane checks, load_desc and the image checks.
  • On a frozen local corpus of 216 translation requests, the generated HLSL, the manifests, the refusals and the compiled outputs are byte-identical before and after (measured with this change and its companion image-descriptor/SALU change applied together). No existing output changes and no new refusal appears there: the fix closes a silent mistranslation that this corpus does not reach.

The fixtures are synthetic instruction words and symbols built in the test from the field layouts that tools/disasm_rdna2.py decodes; the decoded text of every word is checked first.

**@⁠Leyukaka** on GitHub · 2026-12-02 00:51 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3805) `Leyukaka:py-image-desc` → `main` · +320 −6 · 4 files · 3 commits · merged 2026-12-02 08:56 UTC ## Problem `s_load_dwordx8` reads an eight-word image descriptor from a descriptor table, and `tools/startup/gfx2hlsl.py` records one symbol per word. `Stage.descriptor()` names the resource by the first word only, and the image lowerings bind that resource without reading the other words back. A source-built witness shows the defect on `main`: after `s_mov_b32` of a literal into word 1 (format) or word 2 (size) of the loaded descriptor, `image_sample_lz` and `image_load` through that operand were still bound to the unmodified table image. The guest samples an image with another format or size; the translation silently used the original one. Further cases were bound the same way: a word taken from another descriptor, a pointer or descriptor-count value moved into a word, and a word edited in place by scalar ALU. This is part of making the translator base more solid: a form is either translated exactly or refused. ## Change - New `Stage.image_descriptor()`, used by the image sample, load, store, gather and comparison lowerings in place of `descriptor()`. It requires every word of the operand that held a loaded descriptor word to still hold the original one: same source, same byte position, same load tag. A descriptor loaded in two halves by two `s_load_dwordx4` keeps its positions and is still accepted. - Refused, with a message that names the operand, the register and the word: a word replaced by other data (immediate or run-time `s_mov_b32`, a word of another descriptor, a pointer copy), a word modified in place by scalar ALU, and pointer or descriptor-count data in a word position. - `set_s` and the descriptor ALU path record the SGPRs where a loaded descriptor word was replaced. - A register that never held a descriptor word is left as before, and the compute fixed-table path keeps its own check of all eight words. - One expectation in `tests/startup_gfx2hlsl_readfirstlane_uniform_check.py` is widened: for the rewritten-descriptor cases the image access itself is now refused first, with the more precise `image descriptor s[16:23]: ...` message, before the uniformity proof of the read is reached. ## Validation Run on Windows (MSVC) with the dev preset: ```text ctest --preset dev --no-tests=error -R "startup_gfx2hlsl_(image_desc_word_witness|compute|scalar_arity|join_symbols|mask_overwrite|readfirstlane|load_desc|image_)" ``` - `startup_gfx2hlsl_image_desc_word_witness` is new. Its witnesses (word 1 or word 2 rewritten, a word from another descriptor, pointer and descriptor-count moves, in-place ALU edits, each through `image_sample_lz` and `image_load`) failed on `main` and pass with this change. The test accepts a refusal or a binding that differs from the unmodified table image. - Its controls pass before and after: the unmodified descriptor binds the table image, a write to an SGPR outside the descriptor changes nothing, a rewritten first word is refused, and a descriptor loaded in two halves binds the table image. - The neighbouring gfx2hlsl tests pass: compute, scalar_arity, join_symbols, mask_overwrite, the readfirstlane checks, load_desc and the image checks. - On a frozen local corpus of 216 translation requests, the generated HLSL, the manifests, the refusals and the compiled outputs are byte-identical before and after (measured with this change and its companion image-descriptor/SALU change applied together). No existing output changes and no new refusal appears there: the fix closes a silent mistranslation that this corpus does not reach. The fixtures are synthetic instruction words and symbols built in the test from the field layouts that `tools/disasm_rdna2.py` decodes; the decoded text of every word is checked first.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 00:53:53 +00:00
github-bot force-pushed github-bot/gh-3805 from 5c4e141147 to eef11ee2c1 2026-12-02 04:17:35 +00:00 Compare
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-02 04:13 UTC · original

Rebased onto main (0dd65da11) after #3806 merged; both witness registrations are kept in CMakeLists.txt. The gfx2hlsl checks from the description pass again on the new head.

**@⁠Leyukaka** on GitHub · 2026-12-02 04:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3805#issuecomment-7334692572) Rebased onto `main` (0dd65da11) after #3806 merged; both witness registrations are kept in `CMakeLists.txt`. The gfx2hlsl checks from the description pass again on the new head.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 05:19 UTC · original · requested changes

The new witness and the existing compute/readfirstlane checks pass. I found one remaining case in the same source-built fixture: insert s_mov_b32 s16, s2 between LOAD and either image instruction, where s2 is the table pointer. The image instruction is accepted on eef11ee2. Word 0 now holds ('ptr', ('table', 0)), words 1–7 still hold the old loaded descriptor, and the emitted resource key becomes (('table', 0), None) through descriptor()'s pointer fallback. image_descriptor() returns before consulting the overwrite witness when word 0 is a pointer. Please refuse this mixed operand and add a control for both sample and load, while preserving untouched pointer operands that the translator already accepts.

**@⁠R0Y00** on GitHub · 2026-12-02 05:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3805#pullrequestreview-6145183463) · **requested changes** The new witness and the existing compute/readfirstlane checks pass. I found one remaining case in the same source-built fixture: insert `s_mov_b32 s16, s2` between `LOAD` and either image instruction, where `s2` is the table pointer. The image instruction is accepted on `eef11ee2`. Word 0 now holds `('ptr', ('table', 0))`, words 1–7 still hold the old loaded descriptor, and the emitted resource key becomes `(('table', 0), None)` through `descriptor()`'s pointer fallback. `image_descriptor()` returns before consulting the overwrite witness when word 0 is a pointer. Please refuse this mixed operand and add a control for both sample and load, while preserving untouched pointer operands that the translator already accepts.
After s_load_dwordx8 s[16:23] and s_mov_b32 s16, s2 (the table pointer), word 0
held the pointer while words 1-7 still held the loaded T#, and descriptor() named
the operand by the pointer. image_descriptor() now refuses a pointer operand whose
word 0 replaced a loaded descriptor word or whose other words hold or replaced one.
A pointer operand with no loaded descriptor words is accepted as before.

The witness covers the mixed operand for image_sample_lz and image_load, and the
untouched pointer operand as a control.
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-02 06:12 UTC · original

Thanks. Fixed in 10cd8d847: a pointer in word 0 is refused when any word of the range holds or replaced a loaded descriptor word. Witness covers sample and load and passes on the new head; untouched pointer operands stay accepted. Limit: tracking follows write order, not control flow, so a reused register can be refused.

**@⁠Leyukaka** on GitHub · 2026-12-02 06:12 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3805#issuecomment-7336748471) Thanks. Fixed in `10cd8d847`: a pointer in word 0 is refused when any word of the range holds or replaced a loaded descriptor word. Witness covers sample and load and passes on the new head; untouched pointer operands stay accepted. Limit: tracking follows write order, not control flow, so a reused register can be refused.
github-bot changed target branch from main to gh-pr/3805/base 2026-12-02 08:57:20 +00:00
github manually merged commit 10cd8d8471 into gh-pr/3805/base 2026-12-02 08:57:22 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 08:55 UTC · original · approved these changes

The previous finding is fixed at 10cd8d84. I ran the descriptor-word witness and five nearby gfx2hlsl checks. Both sample and load now refuse the mixed pointer/descriptor operand, and both still bind an untouched table pointer. Temporarily disabling the new rejection made both mixed-word controls fail, then I restored the exact source.

**@⁠R0Y00** on GitHub · 2026-12-02 08:55 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3805#pullrequestreview-6147507392) · **approved these changes** The previous finding is fixed at `10cd8d84`. I ran the descriptor-word witness and five nearby gfx2hlsl checks. Both sample and load now refuse the mixed pointer/descriptor operand, and both still bind an untouched table pointer. Temporarily disabling the new rejection made both mixed-word controls fail, then I restored the exact source.
This discussion has been locked. Commenting is limited to contributors.
No description provided.