Skip to content

rdna2spirv: an out-of-range s_buffer_load reads through the buffer's base (llvmpipe segfault) - #4573

Open
LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:smem-oob-safe-addr
Open

LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:smem-oob-safe-addr

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

Problem. An out-of-range s_buffer_load segfaults llvmpipe, the CI's GPU. The translated code is correct: an s_buffer_load whose bound is only known at run time guards each dword with an OpSelectionMerge on off + 4·i + 4 <= NUM_RECORDS · stride and gives 0 outside it. llvmpipe (Mesa 26.0.8, LLVM 21.1.8) executes the guarded load anyway.

Found on #3039. Running its fixture cs_w32_late_ext on llvmpipe locally, vk_compute_test crashes with SIGSEGV (exit 139). Every 32-wide GPU passed it, because their stray read returns garbage that is thrown away. Word 6 is s12 = -4, offset:20: 0xFFFFFFFC + 20 = 0x100000010 bytes, past NUM_RECORDS = 0xFFFFFFFF, so it must read 0 (#1948). The guarded load at that address is 4 GB past the buffer.

Reduction. Delta debugging brought it down to 6 lines that still crash: that load, a v_cmpx, a ds_mskor_rtn_b32 and a store. It doesn't crash with s12 = 12, which is in range. The module passes spirv-val. Two other offsets (-0x100, 0x7ffffff0) also crash, but they are in range for NUM_RECORDS = 0xFFFFFFFF, so they are real reads 4 GB away from a small buffer, not a translator issue.

Change. In scalar_load's run-time-bounded path, the guarded load now reads through OpSelect(in range, address, V# base), converted to a pointer the same way (host_addr).

  • Out of range: a hoisted load reads dword i of the buffer's base, at most 60 bytes on, instead of the out-of-range address.
  • Value: the same either way, because the OpPhi gives 0 out of range.
  • Scope: one OpSelect and one pointer per guarded dword. Constant bounds, unguarded loads and buffer_load* are unchanged.

Tests.

docs/design.md: new row after "Scalar/buffer memory".

No game files. Claimed on #4421 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

…base

A run-time-bounded s_buffer_load guards each dword with an OpSelectionMerge
on the bound and gives 0 outside it. llvmpipe (Mesa 26.0.8, LLVM 21) runs the
guarded load anyway: ItsNotRexed#3039's s12 = -4, offset:20 read host memory 4 GB past
the buffer and segfaulted vk_compute_test (reduced to 6 lines; in-range
offsets run). The guarded load now reads through OpSelect(in range, address,
V# base), so a hoisted load reads the base; the OpPhi still gives 0.

tests/semantic/test_smem_oob_safe_address.py checks the selected address on
the translated SPIR-V and that values are unchanged; ItsNotRexed#3039's cs_w32_late_ext
now passes on llvmpipe. docs/design.md row added.
@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 76e312e2 on Linux (x86_64, Python 3.13.5, SPIRV-Tools on PATH, CPU/no-GPU): pass (code read + unit tests + spirv-val).

What I ran:

  • git diff --check d0835dd9..HEAD: clean.
  • PYTHONPATH=tests/semantic python3 -m unittest -v tests/semantic/test_smem_oob_safe_address.py tests/semantic/test_smem_const_offset.py: 6/6 OK.
  • Assembled and validated the s_buffer_load_dwordx2 test shader with spirv-as --target-env vulkan1.2 + spirv-val --target-env vulkan1.2: OK.

Code review:

  • Selecting between full and addr via OpSelect %ulong {ok} {full} {addr} before OpConvertUToPtr cleanly prevents llvmpipe from hoisting a 4 GiB out-of-range pointer read out of the OpBranchConditional.
  • Two small non-blocking edge-case notes:
    1. For wide loads (x4/x8/x16), OpAccessChain %_ptr_PSB_uint {sptr} {self.cu(i)} still steps 4*i bytes past addr (up to +60 bytes) when ok is false; if a tiny buffer sits at the end of a mapped page (or addr == 0 with NUM_RECORDS == 0), selecting full + 4*i vs addr and indexing sptr at 0 avoids stepping past addr + 0 (null-base V#s would still need a safe scratch address if llvmpipe hoists those too).
    2. In test_smem_oob_safe_address.py, checking that ptr_args[1] traces through host_addr to the OpSelect %ulong {cond} (instead of assertIn over the full spv text) would tighten the structural check.

@Atomic258

Copy link
Copy Markdown

Tested head 76e312e2b83f1a09af22112949d4bf24f80eb8bf on Linux, Python 3.10.12, standard library only, hand-written asm fixtures (no game files, no GPU, no SPIR-V tools).

  • python -I tests/semantic/test_smem_oob_safe_address.py on the PR head: Ran 2 tests — OK
  • Negative control, same test against main's tools/rdna2spirv.py: FAILED (failures=1) — the out-of-range s_buffer_load base-address rule is exercised by the new tests.

The llvmpipe segfault avoidance in the translator reads out-of-range buffer loads through the buffer's base as described. AI-assisted (Devin).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this at 76e312e2 on Windows 11, RTX 5070 Ti.

  • tests/semantic/test_smem_oob_safe_address.py -> 2 tests, OK; git diff --check clean. The structural check confirms both dwords of a run-time-bounded s_buffer_load_dwordx2 now take their pointer from OpSelect(bound, address, base); the value check runs the six documented offsets and gets the same words as before, constant and run-time sOFF.
  • GPU, run_gpu_tests.py --filter smem with this translator -> 4/4 passed, 4 shaders spirv-val clean: cs_smem_bounds 0x0 0x5eed0010 0x5eed000c 0x0 0x5eed000c 0x5eed0010 0x0 0x0, cs_smem_sload_neg, cs_smem_soffset_imm, cs_smem_stride_align all match their expected words.

The change is in the guarded path only: the out-of-range address becomes dword i of the buffer base (at most 60 bytes on), while the OpPhi still yields 0. I cannot reproduce the llvmpipe hoist on this NVIDIA host, but the selected address is now in-bounds by construction.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 no-regression check at head 76e312e (macOS 26.4, MoltenVK 1.4.2 direct, subgroup 4..32, llvm-mc 23.1.6, clean full build exit 0).

python -I scripts/run_gpu_tests.py --build runtime/build/linux: 213 shaders translated, 213 pass spirv-val; 127/210 passed, 76 skipped, 7 failed — identical to upstream/main aa2988c on this M2, with the same seven known MoltenVK expected failures. The llvmpipe SIGSEGV this fixes does not occur on this driver before or after; no new failure or skip. Head fetched read-only, no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified 76e312e2 on Windows 11 25H2 + AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, SPIRV-Tools v2026.4, llvm-mc 21.1.8).

GPU, semantic & negative-control checks (LEONIDA_REQUIRE_SPIRV_TOOLS=1)

  • LEONIDA_VK_VALIDATION=1 python -I scripts/run_gpu_tests.py --filter cs_w32 --require-pass: 13 shaders translated & spirv-val validated, 13/13 passed, 0 validation errors on AMD Radeon RX 6700 XT.
  • LEONIDA_VK_VALIDATION=1 python -I scripts/run_gpu_tests.py --filter cs_w64 --require-pass: 21 shaders translated & spirv-val validated, 21/21 passed, 0 validation errors.
  • test_smem_oob_safe_address.py: 2/2 OK (both direct python -I 0.186 s and unittest discover 0.119 s).
  • Negative control (HEAD~1 tools/rdna2spirv.py with the new test): FAILED (failures=1) (test_the_guarded_load_reads_through_a_selected_address: AssertionError: 'OpSelect %ulong %t65 ' not found).
  • Related suites: test_scalar_mem_smem.py 41/41 (0.015 s), test_smem_const_offset.py 4/4 (0.429 s), test_global_mem_offset.py 11/11 (0.021 s), tests/test_shader_checks.py 75/75 (1.361 s).
  • All 7 tests/test_docs_*.py suites: 7/7 passed; py_compile, scripts/check_no_game_data.py, and git diff --check clean (rc=0).

(Non-blocking code note: tools/rdna2spirv.py:9615 still emits ptr = OpConvertUToPtr %_ptr_PSB_arr ... unconditionally before the if/elif/else loop, so ptr is unused on the runtime-bounded else: branch where sptr is used.)

AI-assisted (Gemini, Antigravity).

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @justr4mzi (RX 6700 XT) and @bobcatchris (Apple M2). The unused pointer is fixed in 4c84d810. Every dword of one load takes the same path, so the unconditional OpConvertUToPtr of the load address is now emitted only for the unguarded paths, and the run-time-bounded dwords use only their selected pointer. The four smem test files pass, and the full semantic suite shows only main's known failures. On llvmpipe, #3039's cs_w32_late_ext still passes with this translator, and the full GPU suite is 139/210, identical to main.

@justr4mzi on ItsNotRexed#4573: the unconditional OpConvertUToPtr of the load address
was unused when every dword reads through its own selected pointer. Every
dword of one load takes the same path, so the base pointer is now emitted only
for the unguarded paths.
@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown

Verdict: approve-with-nits.

The fix is right: the value still comes from OpPhi as 0 and OpSelect only replaces the address the driver may speculatively load, so no observable semantics change. The bounds arithmetic is sound — zext(sOFF & ~3) + imm in 64 bits with no 32-bit wrap, lim compared in 64 bits.

Verified: pytest on the new file plus test_smem_const_offset, test_scalar_mem_smem, test_global_mem_offset → 58 passed / 151 subtests; the new test fails on d0835dd9 as claimed; spirv-as + spirv-val --target-env vulkan1.2 on the translated module → 0.

Nits / follow-ups:

  1. tests/semantic/test_smem_oob_safe_address.py:41-42,56-59 extracts ptr_args but never uses it: it only asserts that an OpSelect %ulong exists somewhere in the module, so a dead select would pass. Better: walk the AccessChain pointer through its OpIAdd to the OpSelect and compare its condition with cond.
  2. tools/rdna2spirv.py:9662: out of range it still loads from base + 4*i (up to 64 bytes), which can be outside the V#'s allocation. OpSelect(ok, full, self.cu(0)) would read 4 bytes of heap base from push constants, which is always mapped, and would also let docs/design.md:246 drop "at most 60 bytes on".
  3. MUBUF (tools/rdna2spirv.py:3155-3158, same branch shape) has the same hoisting hazard, while docs/design.md:246 says buffer_load* "keeps its own guards" without that caveat — worth a finding: in the coordination issue.
  4. tools/rdna2spirv.py:9626-9627: OpConvertUToPtr(full) is now dead on fully guarded loads and still converts a 4 GiB address.
  5. host_addr inside the loop costs ~7 ops (two push-constant loads) per dword, up to ×16; the base can be hoisted out of the loop.
  6. The evidence in the description points at cs_w32_late_ext from the open GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039, and there is no fixture with a far runtime bound, so llvmpipe CI cannot catch a regression of this rule. Either a small cs_* fixture or a note that it waits on GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039.
  7. The description references the claim but not the item ID.

AI-assisted review.

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.

7 participants