Repository navigation
startup: refuse a malformed root signature blob per request in gfx2hlsl - #1526
Conversation
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
left a comment
There was a problem hiding this comment.
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.pyfrom main fails withstruct.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 raiseUnsupported('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.
|
Thanks for running the mutations and for the precise example. |
tedin7
left a comment
There was a problem hiding this comment.
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 missingiced_x86module.- Mutations in tools/startup/gfx2hlsl.py (restored with
git checkout --, green again): replacingreadwith a rawstruct.unpack_fromat 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 changingo + 8too + 4are 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 from12 * ito8 * 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.
…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.
|
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. |
Change
parse_rootintools/startup/gfx2hlsl.pyunpacked 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 raisedstruct.error. The per-request handler intranslate_requestonly catches(Unsupported, OSError, UnicodeError), so one such request aborted the whole--pendingrun 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_rootnow goes through a small bounds-checked helper that raises the tool's ownUnsupported('root signature blob truncated'). The request gets a.failfile, the run continues and exits 0. No broadstruct.errorcatch, no change to ALU lowering or the--capturebranch.New
tests/startup_gfx2hlsl_root_check.py(pure Python,--pendingsubprocess), registered in CMakeLists.txt (startup_gfx2hlsl_root), thepython-linuxjob 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 (.txtwritten, no.fail).tools/startup/gfx2hlsl.pyreverted toorigin/main, the same check fails withstruct.error: unpack_from requires a buffer of at least 24 bytes ... (actual buffer size is 0)fromparse_root.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/main60c7233. Aftercmake --preset dev,ctest --test-dir out/build/dev --no-tests=error -R '^startup_gfx2hlsl_root$'passed (1/1). Not run: thebinary/drawmodes of the inline-sampler check, GPU tests, and the new check on Linux (this PR'spython-linuxjob is the first Linux run). Inputs are synthetic. Well-formed roots take the same path as before. Open PR #1471 edits other parts ofgfx2hlsl.py(image sampling), notparse_root.AI-assisted.