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

Manually merged
github merged 6 commits from gh-pr/1526/head into gh-pr/1526/base 2026-11-26 23:14:13 +00:00
Owner

@⁠RenzoWit35 on GitHub · 2026-11-24 13:38 UTC · original
RenzoWit35:startup/gfx2hlsl-malformed-root → main · +100 −5 · 5 files · 6 commits · merged 2026-11-25 02:06 UTC

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.

**@⁠RenzoWit35** on GitHub · 2026-11-24 13:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1526) `RenzoWit35:startup/gfx2hlsl-malformed-root` → `main` · +100 −5 · 5 files · 6 commits · merged 2026-11-25 02:06 UTC ## 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.
parse_root unpacked the root blob without length checks, so a truncated
DXBC header or an RTS0 part without a body raised struct.error. Only
Unsupported/OSError/UnicodeError are caught per request, so one such
request aborted the whole --pending run with a traceback and exit 1.
Bounds-check every read and raise Unsupported('root signature blob
truncated'); the request gets a .fail file and later requests are still
translated. The new check covers six bad blobs plus a valid request
that sorts after them.
Add a case with a type-1 root parameter whose descriptor table offset lies past the end of the blob. It must be refused as truncated, which pins the bounds check on that read. Also add a valid type-1 table and assert the parsed register, space and count.

This follows the review of the pull request.
# Conflicts:
#	.github/workflows/windows.yml
#	AGENTS.md
Since #1662 a request that translates is refused when no fxc.exe is
found, so on a host without the Windows SDK the check's valid request got
a .fail file instead of a manifest and the check failed. The check is
about root signature parsing, not compilation: run the tool with the
empty X2W_FXC, which publishes the manifest alone on every host, as the
other startup checks do.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:14:11 +00:00
github manually merged commit 2fb8343e8f into gh-pr/1526/base 2026-11-26 23:14:13 +00:00
Author
Owner

@⁠RenzoWit35 on GitHub · 2026-11-24 15:51 UTC · original

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.

**@⁠RenzoWit35** on GitHub · 2026-11-24 15:51 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1526#issuecomment-7136730930) 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-25 02:02 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-25 02:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1526#issuecomment-7150253359) 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.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 15:34 UTC · original · reviewed

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.

**@⁠tedin7** on GitHub · 2026-11-24 15:34 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1526#pullrequestreview-6042332266) · **reviewed** 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.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 17:50 UTC · original · reviewed

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.

**@⁠tedin7** on GitHub · 2026-11-24 17:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1526#pullrequestreview-6044494780) · **reviewed** 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.