Skip to content

runtime(spirv_words): validate SPIR-V header bound/schema and instruction word counts - #6258

Open
Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/spirv-words-header-and-instruction-framing
Open

Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:fix/spirv-words-header-and-instruction-framing

Conversation

@Persudes

Copy link
Copy Markdown
Contributor

Summary

Hardens leonida::read_spirv_words() in runtime/src/spirv_words.h to check SPIR-V physical layout framing (SPIR-V Specification Section 2.3) before handing words to Vulkan drivers:

  1. 5-Word Header Framing (words[3], words[4]):
    • Require words[3] != 0 (Bound >= 1, since all SPIR-V <id>s satisfy 0 < id < Bound).
    • Require words[4] == 0 (reserved instruction schema word must be 0).
  2. Instruction Stream WordCount Framing (words[5..]):
    • Walk instructions from word 5 to words.size() using inst_words = words[at] >> 16, rejecting zero-length instruction words (inst_words == 0) or truncated instructions (inst_words > words.size() - at) that end mid-instruction on a 4-byte boundary.

Hardware / Environment

  • OS: Windows 11 x64 (10.0.26100)
  • CPU / GPU: AMD Radeon RX 6900 XT (gfx1030, native wave64 subgroup 64), AMD driver 26.11.2 / 2.0.353 (32.0.21045.29003), Vulkan SDK 1.4.363.1
  • Toolchain: MSVC 19.52 (VS2026 Build Tools + Ninja), Python 3.15.2

Verification

  • Red-first (before runtime/src/spirv_words.h edits):
    • runtime\build\shader_library_test.exe -> failed at line 283: to_stream({0x07230203, 0x00010300, 0, 0, 0}).empty() (exit code 1)
  • Green (after runtime/src/spirv_words.h edits):
    • ctest --test-dir runtime/build -R "^(shader_library|d3d12_compute)$" --output-on-failure -V -> 2/2 Passed (SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK; d3d12_compute passed on device: AMD Radeon RX 6900 XT)
    • python -I -B tests/test_ci_registration.py -> Ran 8 tests OK
    • python -I -B tests/test_text_write_line_endings.py -> Ran 3 tests OK
    • python -I -B scripts/check_no_game_data.py -> exit 0
    • git diff --check -> exit 0
    • git merge-tree --write-tree upstream/main HEAD -> exit 0

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested and verified at commit \�d0b86d65\ on Windows 11 (Build 26300), MSVC 19.44: pass, exit 0.

  • \cmake --build runtime/build --target shader_library_test --config RelWithDebInfo: built cleanly.

  • untime\build\RelWithDebInfo\shader_library_test.exe: passed with exit 0 (\SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK).
  • Verified all 5 new framing test cases pass: valid instruction stream accepted, \Bound == 0\ rejected, \schema != 0\ rejected, zero-length instruction \WordCount == 0\ rejected, and truncated instructions spanning beyond stream bounds rejected.
  • \python -I tests/test_ci_registration.py: 8/8 passed.
  • \python -I tests/test_text_write_line_endings.py: 3/3 passed.
  • \git diff --check HEAD~1..HEAD: clean.

Approve. Clean and defensive validation of SPIR-V layout boundaries before handing to Vulkan.

@LLLL-zzzz0

Copy link
Copy Markdown

Independent Windows check of ad0b86d65 (merge base d0835dd9).

Environment: Windows 11 26100, MSVC 19.44.35213 (VS 2022 17.14), Windows SDK 10.0.26100. Fresh worktree from the PR head, RelWithDebInfo, cmake --build ... --target shader_library_test.

  • shader_library_test: pass (SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK).
  • Red control: reverting only runtime/src/spirv_words.h to the merge base (the new test stays) fails at the first added check, line 283: to_stream({0x07230203, 0x00010300, 0, 0, 0}).empty() (Bound == 0). So the new header and word-count validation is load-bearing and the test catches it.
  • The added checks reject only malformed input: schema != 0, Bound == 0, a zero instruction WordCount, and a truncated last instruction; a well-formed header plus a 2-word instruction is returned unchanged, and the walk is linear in the word count.
  • Scope: I built only this target and its dependencies, so no other runtime target's behaviour was exercised.

AI-assisted.

@soldsawn

Copy link
Copy Markdown

Independent Windows 11 / MSVC 2022 (\RelWithDebInfo) check at exact head \�d0b86d65294ff678bd0a9c467bfb5b8c2c14134\ (claim: #6414 (comment)):

  • Built \shader_library_test\ (\RelWithDebInfo) and ran \shader_library_test.exe:
    • \SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK\ (exit 0).

Synthetic inputs only, no game data. AI-assisted.

@hazben

hazben commented Nov 23, 2026

Copy link
Copy Markdown

Tested ad0b86d65294ff678bd0a9c467bfb5b8c2c14134 merged onto main 4beffda8 on NVIDIA GeForce RTX 4070 SUPER (Ada), driver 621.14, Windows 11 (build 26200): pass. AI-assisted run, no game files.

  • Build: MSVC 14.44 (VS 2022), Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0. The PR's merge base d0835dd9 is 2,812 commits behind that main, so I tested the merge: it is clean, and against main it adds only this PR's 22 lines. I built the four test targets first. Then I built the whole runtime (636 steps), because shader_library.h includes spirv_words.h and the D3D12 layer loads shader modules through ShaderLibrary::spirv(). The compiler printed no warnings for spirv_words.h or the new test lines.
  • ctest -R "^(shader_library|d3d12_compute|d3d12_stores_atomics|vk_pipeline_check.*)$": 5/5 passed normally and 5/5 passed with LEONIDA_VK_VALIDATION=1 (device: NVIDIA GeForce RTX 4070 SUPER (validation on)). Two of these tests send real glslang-built modules through the stricter reader to the NVIDIA driver:
    • d3d12_compute loads its compute shader through ShaderLibrary::spirv(): compute: root constants, a root UAV and a descriptor table reached the shader through the GPU address arena; 64 elements matched in each of three dispatches
    • d3d12_stores_atomics reads ps_image_atomic.spv, ps_draw.spv and vs_fullscreen.spv with read_spirv_words(): the runtime's own device: fragmentStoresAndAtomics enabled, vertexPipelineStoresAndAtomics enabled; the PSO with the atomic pixel shader is created; validation errors: 0
    • shader_library: SHA-256, inflate (stored/fixed/dynamic), JSON, FSSM extraction, FSPX programs and index lookup OK
  • Negative control: I put main's spirv_words.h back and rebuilt shader_library_test. It then fails with line 284: to_stream({0x07230203, 0x00010300, 0, 0, 0}).empty() (line 283 at the PR head; CHECK stops at the first failure). To see the other three malformed cases too, I compiled the PR's five inputs into a small probe against each header. With main's header, all five inputs are accepted (4 of 5 cases wrong). With this PR's header, the four malformed inputs are rejected and the valid one is accepted (0 of 5 cases wrong). After restoring the PR header and rebuilding, the test passed again.
  • Full ctest (259 tests, gameinput disabled): 246 passed, 11 skipped and 1 failed normally. With validation, 247 passed, 10 skipped and 1 failed (vk_sync_validation runs only with validation on). No VUID lines appear in either log. The other skips are shader_library_corpus, the Bink tests, fsr_dispatch, bvh_builder, d3d12_rgb9e5_rtv, d3d12_boot_movie and d3d12_first_light.
  • Python and diff checks on the merged tree: tests/test_ci_registration.py ran 14 tests, OK. scripts/check_no_game_data.py and git diff --check origin/main exited 0. tests/test_text_write_line_endings.py has 1 failure, tools\f64_omod_probe.py:268. That line came from rdna2spirv: gate the binary64 conversion, frexp and div_fixup output modifiers by MODE #6314 on main, and main 4beffda8 fails the same way, so this PR did not cause it either.

Not covered: only one GPU (no AMD or Intel), and no Wine or Windows 10 run. No translated game shaders went through the new check: shader_library_corpus skips without game files, so only the GLSL-built test fixtures and the synthetic inputs were exercised.

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.

5 participants