Skip to content

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

Merged
R0Y00 merged 6 commits into
R0Y00:mainfrom
RenzoWit35:startup/gfx2hlsl-malformed-root
Nov 25, 2026
Merged

R0Y00 merged 6 commits into
R0Y00:mainfrom
RenzoWit35:startup/gfx2hlsl-malformed-root

Conversation

@RenzoWit35

Copy link
Copy Markdown
Contributor

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 60c7233. 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.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.
@RenzoWit35

Copy link
Copy Markdown
Contributor Author

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 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

RenzoWit35 and others added 3 commits November 24, 2026 23:31
…ormed-root

# Conflicts:
#	.github/workflows/windows.yml
#	AGENTS.md
Since R0Y00#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.
@R0Y00

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

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
R0Y00 enabled auto-merge November 25, 2026 02:05
@R0Y00
R0Y00 merged commit 8700da5 into R0Y00:main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants