Skip to content

rdna2spirv: bounds-check image_atomic_* on 2D/3D r32ui storage images - #5077

Closed
milas6669 wants to merge 1 commit into
ItsNotRexed:mainfrom
milas6669:fix/image-atomic-oob-bounds
Closed

milas6669 wants to merge 1 commit into
ItsNotRexed:mainfrom
milas6669:fix/image-atomic-oob-bounds

Conversation

@milas6669

@milas6669 milas6669 commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #5032 (comment) (new SHADER item, from @Vonsek's finding #4933 (comment))

What and why

image_atomic_* on a 2D or 3D r32ui storage image built its OpImageTexelPointer from the raw coordinates with no bounds check. An out-of-range texel pointer is undefined in Vulkan, while RDNA2 leaves memory untouched and, with glc, returns 0.

The translator now queries OpImageQuerySize, compares each coordinate unsigned (a negative coordinate wraps to a large value, so it is rejected too), and runs the texel pointer and the atomic only for lanes that are active and in range. With glc an active lane that is out of range gets 0 in vdata; an inactive lane keeps it. Covers add/sub/and/or/xor/smin/smax/umin/umax/cmpswap and swap/inc/dec/fmin/fmax/fcmpswap, a16 included.

The plain atomics and cmpswap now share emit_image_atomic_core (the old 2D and 3D copies, and an unreachable image_store branch in the 2D block, are gone). docs/design.md: the "Image atomics" contract row records the rule.

Tested

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

… (SHADER item, claimed on ItsNotRexed#5032)

An out-of-range OpImageTexelPointer is undefined in Vulkan, while RDNA2 leaves
memory untouched and, with glc, returns 0. Query OpImageQuerySize, compare each
coordinate unsigned (a negative one wraps) and run the texel pointer and the atomic
only for active, in-range lanes; with glc an active out-of-range lane gets 0.

The plain atomics and cmpswap share emit_image_atomic_core; the old 2D/3D copies and an
unreachable image_store branch are removed. Contract row in docs/design.md updated.
Not GPU-verified.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@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 e8382a704 on Windows 11, RTX 5070 Ti.

  • tests/semantic/test_image_atomics_bounds.py -> 3 tests, OK; git diff --check clean.
  • GPU with this translator: run_gpu_tests.py --filter imgatomic -> 3/3 passed, 4 shaders spirv-val clean:
    • cs_imgatomic: all 64 words of the uint image, the sint image and the volume exact
    • cs_imgatomic3d: all 64 words exact
    • ps_imgatomic: 0 of 256 texels wrong; 0 of 4096 render-target pixels wrong (odd columns discarded)

The image_atomic_* path now bounds-checks the 2D/3D r32ui storage-image address before the atomic, refusing a coordinate outside the subresource instead of emitting an unchecked access; the semantic test covers the in-bounds, edge and out-of-bounds forms, and the GPU fixtures keep the measured words. No blocking issue.

@DenRakEiw

DenRakEiw commented Nov 23, 2026 •

Copy link
Copy Markdown

Independent hardware review of e8382a704861fc81661b6dfe9a68af03a19bb48d (merged cleanly onto main 3faac557; git diff --check clean).

Note on overlap with #5059: This PR and #5059 (opened earlier by @Vonsek for EGEN-IMGATOMIC-OOB) address the exact same finding (#4933) and modify the same image_atomic_* paths in tools/rdna2spirv.py and the same docs/design.md contract row — only one of the two should be merged.

Environment: Windows 11 x64, Python 3.12.10, SPIRV-Tools v2026.4 (Vulkan SDK 1.4.363.0), LLVM_MC (LLVM 21 via WSL), LEONIDA_W64_EMULATE=1.
GPUs: NVIDIA GeForce RTX 5090 (driver 32.0.16.1088) and AMD Radeon(TM) Graphics RDNA2 iGPU (driver 31.0.22044.1).

Results:

  • python -m unittest discover -s tests/semantic -p test_image_atomics_bounds.py: 3/3 OK
  • test_image_atomics_32.py: 3/3 OK
  • test_fspx_rules.py: 9/9 OK
  • scripts/run_gpu_tests.py --filter imgatomic (4/4 shaders translated, 4/4 pass spirv-val):
    • RTX 5090: 3/3 PASS (cs_imgatomic, cs_imgatomic3d, ps_imgatomic)
    • AMD Radeon(TM) Graphics (RDNA2 iGPU): 3/3 PASS (cs_imgatomic, cs_imgatomic3d, ps_imgatomic)

All tested semantic and in-range GPU checks pass; out-of-bounds coordinates are verified structurally in the new semantic tests (not executed on GPU by the stock imgatomic fixtures).

AI-assisted (Gemini 4 Neon).

@milas6669

Copy link
Copy Markdown
Author

Thanks @DenRakEiw and @Kam471 for the checks, and for flagging the overlap. I missed #5059: it is @Vonsek's PR for their own finding (EGEN-IMGATOMIC-OOB, #4933, "done" at 07:59), and I opened this one without re-searching the open PRs first. My claim on #5032 (07:48) was made before #5059 existed, but that doesn't change that the finder's PR came first.

#5059 also has what this PR lacks: a gfx1036 measurement of the out-of-range behaviour with tools/hw_oracle (this PR's rule is taken from the finding and is not GPU-verified), and independent runs on RADV, an RTX 4080 with a seeded out-of-range dispatch. The two do the same thing (query OpImageQuerySize, unsigned per-axis compare, skip the atomic and return 0 with glc); the visible difference is that this PR guards the zeroing with a second branch where #5059 uses an OpPhi.

I suggest #5059 goes forward. I'm leaving this PR open until a maintainer decides, and will close it in favour of #5059 if that is what they want (@ItsNotRexed, @KoertLichtendonk). If anything here helps #5059, the 28-variant spirv-val matrix and the structural checks in tests/semantic/test_image_atomics_bounds.py are available to borrow.

AI-assisted.

@milas6669

Copy link
Copy Markdown
Author

Closing in favour of #5059, which is @Vonsek's PR for their own finding (EGEN-IMGATOMIC-OOB) and came first, with the gfx1036 measurement. Thanks to everyone who checked this one; the results carry over, since the two make the same change. AI-assisted.

@milas6669 milas6669 closed this Nov 23, 2026
@ErayOzdayioglu

Copy link
Copy Markdown

Tested e8382a7 on NVIDIA GeForce RTX 5070 (driver 617.42) and AMD Radeon Graphics (Ryzen 5 7600 iGPU, AMD proprietary driver 2026.04.17), Windows 11 26200, Vulkan SDK 1.4.363, Python 3.12, llvm-mc 23.1.6: pass

  • python -I scripts/run_gpu_tests.py (runtime built from main 3faac55 with MSVC 14.51; the PR only changes Python): RTX 5070 170/217 passed, 47 skipped; Radeon 195/217 passed, 22 skipped; no failures on either. cs_imgatomic, cs_imgatomic3d and ps_imgatomic pass on both.
  • python -m unittest discover -s tests/semantic: 6,248 tests, 3 failures and 2 errors, all in test_loop_ballot_barrier (the five already failing on main, as the PR body says). test_image_atomics_bounds: 3/3 OK.
  • Out-of-range check on the GPU: a hand-written vk_store_test words fixture (not in this PR), 8 cases: 3D add at x = 4 and at x = -1, 3D cmpswap at z = 4 (whose comparand matches texel 0, so a wrapped coordinate would show up), 2D add at x = 8, 2D umin at y = 8, 2D swap (an extra op) at (8, 8), all with glc; a 3D add without glc at y = 4; and an in-range 3D add as a control. Expected: 0 in vdata with glc, vdata unchanged without it, every texel still holding its fill except the control's. With this PR it passes on both GPUs; spirv-val (vulkan1.2) accepts the module, which has 8 OpImageQuerySize guards.
  • Caveat: the same fixture translated by main's rdna2spirv.py (no guards) also passes on both drivers, so these two drivers happen to handle an out-of-range texel pointer quietly, and the fixture can't tell the two versions apart there. It shows the guarded path gives the right words on real hardware. It does not show a difference on these drivers. The RDNA2 behaviour itself (memory untouched, 0 with glc) is still not measured natively here.

Code read: the guard is computed before the OpSelectionMerge, inactive lanes keep vdata, and the image_store branch that was removed from the 2D atomic block was indeed unreachable (image_store returns through self.image_store just above it). Looks good to me.

AI-assisted: yes

@xelerb-debug

Copy link
Copy Markdown

Tested e8382a70 on NVIDIA GeForce RTX 4060 (Ada, subgroup 32..32), driver 621.01 (Vulkan 1.4.357), Windows 11 Pro 26200: pass.

Built from this head (base 3faac557), runtime rebuilt with -DLEONIDA_REQUIRE_VULKAN=ON; MSVC 19.51, Vulkan SDK 1.4.363.1 (SPIRV-Tools v2026.4), native llvm-mc 21.1.8, Python 3.15.2.

Check Result
tests/semantic -p "test_image_atomics*.py" (LEONIDA_REQUIRE_SPIRV_TOOLS=1) 6/6 OK
Negative control: test_image_atomics_bounds.py with LEONIDA_TOOLS_DIR = tools/ of 3faac557 43 failures, as the PR says
run_gpu_tests.py --filter atomic 4/4 translated + spirv-val; cs_imgatomic, cs_imgatomic3d, ps_imgatomic 3/3 PASS, all words exact
run_gpu_tests.py, native 221/221 translated + spirv-val; 170/217 passed, 47 skipped, 0 failed (same as main af70fc78 on this GPU)
run_gpu_tests.py, LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped, 0 failed (same as main)
tests/semantic (full) 6248 tests: 3 failures + 2 errors, all in test_loop_ballot_barrier.py: the known red on main that #4808 fixes; nothing new

Code read: the per-axis OpULessThan against OpImageQuerySize also rejects negative coordinates, and the removed image_store block in the 2D path was unreachable (image_store returns at the earlier if op == "image_store" before the atomic branch). Inactive lanes now skip the atomic instead of running it with the neutral operand, which keeps the existing fixtures exact here.

Not covered by this run: none of the GPU fixtures hits an out-of-range texel, so the new path (memory untouched, 0 returned with glc) is still only CPU-checked — as the docs/design.md row says.

No game files. AI-assisted (Claude Code).

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