startup: refuse a malformed root signature blob per request in gfx2hlsl #1519

Merged
RenzoWit35 merged 6 commits from upstream-pr/1526 into main 2026-11-25 02:06:24 +00:00
RenzoWit35 commented 2026-11-24 13:38:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T13:38:05Z.


Change

parse_root in tools/startup/gfx2hlsl.py unpacked the root-signature blob without length checks. A truncated DXBC header, a part-offset table past the end, or an RTS0 part without a body raised struct.error. The per-request handler in translate_request only catches (Unsupported, OSError, UnicodeError), so one such request aborted the whole --pending run with a traceback and exit code 1, and later requests in the directory were never translated. An empty root blob was already refused cleanly.

Every read in parse_root now goes through a small bounds-checked helper that raises the tool's own Unsupported('root signature blob truncated'). The request gets a .fail file, the run continues and exits 0. No broad struct.error catch, no change to ALU lowering or the --capture branch.

New tests/startup_gfx2hlsl_root_check.py (pure Python, --pending subprocess), registered in CMakeLists.txt (startup_gfx2hlsl_root), the python-linux job and the AGENTS.md list.

Verification

Run from the repo root with Python 3.14.5:

  • python -B tests/startup_gfx2hlsl_root_check.py -> rc 0, "malformed root blobs refused per request, valid request still translated". Covers: empty blob, truncated header, part count past end, RTS0 without body, parameter table past end, container without RTS0, and a valid request that sorts after a bad one and is still translated (.txt written, no .fail).
  • Negative control: with tools/startup/gfx2hlsl.py reverted to origin/main, the same check fails with struct.error: unpack_from requires a buffer of at least 24 bytes ... (actual buffer size is 0) from parse_root.
  • Regression: startup_translator, startup_flat_param, startup_sampler_heap, startup_host_cpu, startup_image_sample_l, startup_image_3d, startup_image_load_mip, startup_image_2d_array, startup_inline_sampler _check.py (no arguments) all rc 0.

Evidence and follow-up

Base origin/main 60c72330. After cmake --preset dev, ctest --test-dir out/build/dev --no-tests=error -R '^startup_gfx2hlsl_root$' passed (1/1). Not run: the binary/draw modes of the inline-sampler check, GPU tests, and the new check on Linux (this PR's python-linux job is the first Linux run). Inputs are synthetic. Well-formed roots take the same path as before. Open PR #1471 edits other parts of gfx2hlsl.py (image sampling), not parse_root.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6583049512 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1526). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T13:38:05Z. --- ## Change `parse_root` in `tools/startup/gfx2hlsl.py` unpacked the root-signature blob without length checks. A truncated DXBC header, a part-offset table past the end, or an RTS0 part without a body raised `struct.error`. The per-request handler in `translate_request` only catches `(Unsupported, OSError, UnicodeError)`, so one such request aborted the whole `--pending` run with a traceback and exit code 1, and later requests in the directory were never translated. An empty root blob was already refused cleanly. Every read in `parse_root` now goes through a small bounds-checked helper that raises the tool's own `Unsupported('root signature blob truncated')`. The request gets a `.fail` file, the run continues and exits 0. No broad `struct.error` catch, no change to ALU lowering or the `--capture` branch. New `tests/startup_gfx2hlsl_root_check.py` (pure Python, `--pending` subprocess), registered in CMakeLists.txt (`startup_gfx2hlsl_root`), the `python-linux` job and the AGENTS.md list. ## Verification Run from the repo root with Python 3.14.5: - `python -B tests/startup_gfx2hlsl_root_check.py` -> rc 0, "malformed root blobs refused per request, valid request still translated". Covers: empty blob, truncated header, part count past end, RTS0 without body, parameter table past end, container without RTS0, and a valid request that sorts after a bad one and is still translated (`.txt` written, no `.fail`). - Negative control: with `tools/startup/gfx2hlsl.py` reverted to `origin/main`, the same check fails with `struct.error: unpack_from requires a buffer of at least 24 bytes ... (actual buffer size is 0)` from `parse_root`. - Regression: `startup_translator`, `startup_flat_param`, `startup_sampler_heap`, `startup_host_cpu`, `startup_image_sample_l`, `startup_image_3d`, `startup_image_load_mip`, `startup_image_2d_array`, `startup_inline_sampler` `_check.py` (no arguments) all rc 0. ## Evidence and follow-up Base `origin/main` 60c72330. After `cmake --preset dev`, `ctest --test-dir out/build/dev --no-tests=error -R '^startup_gfx2hlsl_root$'` passed (1/1). Not run: the `binary`/`draw` modes of the inline-sampler check, GPU tests, and the new check on Linux (this PR's `python-linux` job is the first Linux run). Inputs are synthetic. Well-formed roots take the same path as before. Open PR #1471 edits other parts of `gfx2hlsl.py` (image sampling), not `parse_root`. AI-assisted.
tedin7 commented 2026-11-24 16:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T15:34:49Z.

Review: COMMENTED


Technical review (Linux only; the Windows pipeline was not run).

Checked out the PR head (merge of current main) in a clean tree.

What I ran:

  • tests/startup_gfx2hlsl_root_check.py: passes.
  • Negative control: the same test against tools/startup/gfx2hlsl.py from main fails with struct.error (empty RTS0 body), so the test does detect the old behaviour.
  • startup_translator_check, startup_flat_param_check, startup_gfx2hlsl_bit_alu_check, startup_inline_sampler_check: all pass.
  • Ad-hoc probes of parse_root: a valid blob with one descriptor-table parameter parses to the expected register, space and count; a table offset past the end and a parameter count of 0xffffffff both raise Unsupported('root signature blob truncated').

Local mutation check (each mutation reverted afterwards; the tree is clean). I replaced one guarded read at a time with a raw struct.unpack_from, and made the bound check a no-op or length-only:

  • header count read: caught
  • offset table read: caught
  • RTS0 header read: caught
  • parameter header read: caught
  • bound check turned into pass: caught
  • bound check reduced to offset > len(data): caught
  • the descriptor-table read at if ptype == 1: (p['reg'], p['space'], p['count'] = read(...)): not caught. The test never gives a parameter of type 1 whose table offset lies outside the blob, so this branch is unguarded by any test even though the code is correct.

Suggestion (non-blocking): add one case to BAD with a type-1 parameter whose offset points past the end, for example struct.pack("<6I", 1, 1, 24, 0, 36, 0) + struct.pack("<3I", 1, 0, 400) inside an RTS0 part, expecting the "truncated" reason. A positive case with a valid type-1 table would also pin the parsed fields.

Observation (not caused by this PR): d = blob[o + 8:] is not bounded by the RTS0 part length, so a short part can read bytes of a following part without error. It cannot crash now, only mis-parse.

The CMake test entry, the workflow list and the AGENTS.md line are consistent with each other.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6042332266 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1526#pullrequestreview-6042332266). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T15:34:49Z. **Review:** COMMENTED --- Technical review (Linux only; the Windows pipeline was not run). Checked out the PR head (merge of current main) in a clean tree. What I ran: - `tests/startup_gfx2hlsl_root_check.py`: passes. - Negative control: the same test against `tools/startup/gfx2hlsl.py` from main fails with `struct.error` (empty RTS0 body), so the test does detect the old behaviour. - `startup_translator_check`, `startup_flat_param_check`, `startup_gfx2hlsl_bit_alu_check`, `startup_inline_sampler_check`: all pass. - Ad-hoc probes of `parse_root`: a valid blob with one descriptor-table parameter parses to the expected register, space and count; a table offset past the end and a parameter count of 0xffffffff both raise `Unsupported('root signature blob truncated')`. Local mutation check (each mutation reverted afterwards; the tree is clean). I replaced one guarded read at a time with a raw `struct.unpack_from`, and made the bound check a no-op or length-only: - header count read: caught - offset table read: caught - RTS0 header read: caught - parameter header read: caught - bound check turned into `pass`: caught - bound check reduced to `offset > len(data)`: caught - the descriptor-table read at `if ptype == 1:` (`p['reg'], p['space'], p['count'] = read(...)`): **not caught**. The test never gives a parameter of type 1 whose table offset lies outside the blob, so this branch is unguarded by any test even though the code is correct. Suggestion (non-blocking): add one case to `BAD` with a type-1 parameter whose offset points past the end, for example `struct.pack("<6I", 1, 1, 24, 0, 36, 0) + struct.pack("<3I", 1, 0, 400)` inside an RTS0 part, expecting the "truncated" reason. A positive case with a valid type-1 table would also pin the parsed fields. Observation (not caused by this PR): `d = blob[o + 8:]` is not bounded by the RTS0 part length, so a short part can read bytes of a following part without error. It cannot crash now, only mis-parse. The CMake test entry, the workflow list and the AGENTS.md line are consistent with each other. AI-assisted; commands listed were executed locally.
RenzoWit35 commented 2026-11-24 16:03:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T15:51:31Z.


Thanks for running the mutations and for the precise example.
I added a "descriptor table past end" case to BAD: a type-1 parameter whose table offset is 400, past the end of the blob, expecting the "truncated" reason. I used the suggested bytes unchanged.
I also added a positive case: a valid blob with one type-1 table, asserting that parse_root returns register 5, space 2 and count 3.
With your mutation (a raw struct.unpack_from at that read), the check now fails with a struct.error, while without the new case it still passed.
I left the point about d = blob[o + 8:] not being bounded by the RTS0 part length out of this pull request, to keep it to one behavior.
AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7136730930 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1526#issuecomment-7136730930). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T15:51:31Z. --- Thanks for running the mutations and for the precise example. I added a "descriptor table past end" case to BAD: a type-1 parameter whose table offset is 400, past the end of the blob, expecting the "truncated" reason. I used the suggested bytes unchanged. I also added a positive case: a valid blob with one type-1 table, asserting that parse_root returns register 5, space 2 and count 3. With your mutation (a raw struct.unpack_from at that read), the check now fails with a struct.error, while without the new case it still passed. I left the point about d = blob[o + 8:] not being bounded by the RTS0 part length out of this pull request, to keep it to one behavior. AI-assisted.
tedin7 commented 2026-11-24 18:02:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:50:13Z.

Review: COMMENTED


Re-review of head 38db697f8a (comment only, not an approval). My earlier review was at 99a4863cbb.

Verdict: 0 P1 / 0 P2.

Earlier points

  • Fixed: the unguarded type-1 descriptor-table read. The new "descriptor table past end" case and the positive case (register 5, space 2, count 3) cover it. The diff since 99a4863 is test-only (+7 lines).
  • Still open, out of scope as the author said: d = blob[o + 8:] is not bounded by the RTS0 part length. It can only mis-parse, not crash.

Run

  • python3 -B tests/startup_gfx2hlsl_root_check.py: pass. Also pass: startup_translator, startup_flat_param, startup_gfx2hlsl_bit_alu, startup_inline_sampler, startup_sampler_heap, startup_image_sample_l, startup_image_3d, startup_image_load_mip, startup_image_2d_array, docs_links_check (16 files, 346 links), bidi_controls_check. startup_host_cpu_check fails only on a missing iced_x86 module.
  • Mutations in tools/startup/gfx2hlsl.py (restored with git checkout --, green again): replacing read with a raw struct.unpack_from at each of five reads is caught every time (e.g. struct.error: unpack_from requires a buffer of at least 412 bytes ... at offset 400); swapping reg and space in the table read and changing o + 8 to o + 4 are caught.
  • NOT caught: loosening the bound to > len(data) + 1 (no test reads exactly at the end of the blob), and changing the parameter stride from 12 * i to 8 * i (the positive case has one parameter, so index 0 hides it).

Non-blocking

  • Add one positive case with two type-1 parameters (pins the stride and a second table) and one blob whose table ends exactly at the end (pins the bound).
  • The PR body says the new check was not run on Linux; it passes there.
  • #1631, #1699 and #1707 also edit gfx2hlsl.py but not parse_root.

Not run: CMake/ctest, the Windows job, GPU tests. The PR has no shader, ISA or C++ change, so llvm-mc and mingw had nothing to check.

Linux limits: no Windows, MSVC, DXC or D3D12 device here; LLVM 19 only. Anything that needs them was not run.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044494780 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1526#pullrequestreview-6044494780). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T17:50:13Z. **Review:** COMMENTED --- Re-review of head 38db697f8ab978b163c5adbd68885c8a12a66eb1 (comment only, not an approval). My earlier review was at 99a4863cbb. Verdict: 0 P1 / 0 P2. Earlier points - Fixed: the unguarded type-1 descriptor-table read. The new "descriptor table past end" case and the positive case (register 5, space 2, count 3) cover it. The diff since 99a4863 is test-only (+7 lines). - Still open, out of scope as the author said: `d = blob[o + 8:]` is not bounded by the RTS0 part length. It can only mis-parse, not crash. Run - `python3 -B tests/startup_gfx2hlsl_root_check.py`: pass. Also pass: startup_translator, startup_flat_param, startup_gfx2hlsl_bit_alu, startup_inline_sampler, startup_sampler_heap, startup_image_sample_l, startup_image_3d, startup_image_load_mip, startup_image_2d_array, docs_links_check (16 files, 346 links), bidi_controls_check. startup_host_cpu_check fails only on a missing `iced_x86` module. - Mutations in tools/startup/gfx2hlsl.py (restored with `git checkout --`, green again): replacing `read` with a raw `struct.unpack_from` at each of five reads is caught every time (e.g. `struct.error: unpack_from requires a buffer of at least 412 bytes ... at offset 400`); swapping reg and space in the table read and changing `o + 8` to `o + 4` are caught. - NOT caught: loosening the bound to `> len(data) + 1` (no test reads exactly at the end of the blob), and changing the parameter stride from `12 * i` to `8 * i` (the positive case has one parameter, so index 0 hides it). Non-blocking - Add one positive case with two type-1 parameters (pins the stride and a second table) and one blob whose table ends exactly at the end (pins the bound). - The PR body says the new check was not run on Linux; it passes there. - #1631, #1699 and #1707 also edit gfx2hlsl.py but not `parse_root`. Not run: CMake/ctest, the Windows job, GPU tests. The PR has no shader, ISA or C++ change, so llvm-mc and mingw had nothing to check. Linux limits: no Windows, MSVC, DXC or D3D12 device here; LLVM 19 only. Anything that needs them was not run. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 03:00:43 +00:00
R0Y00 commented 2026-11-25 03:01:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T02:02:46Z.


I resolved the Linux test-list overlap and kept both the malformed-root and SOPK checks. On Windows the patched batch rejects only the malformed requests, still translates the independent valid request, and passes the startup translator regression.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7150253359 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1526#issuecomment-7150253359). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T02:02:46Z. --- I resolved the Linux test-list overlap and kept both the malformed-root and SOPK checks. On Windows the patched batch rejects only the malformed requests, still translates the independent valid request, and passes the startup translator regression.
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!1519
No description provided.