AMD: OpArrayLength counts storage buffer ranges rounded up to a multiple of 4 bytes, so bounds checks miss up to 3 bytes past a buffer #1595

Closed
opened 2026-11-26 10:01:21 +00:00 by github-bot · 6 comments
Owner

@⁠Nawid3333 on GitHub · 2026-10-17 12:05 UTC · original

On both of my AMD GPUs (RX 9070 XT with LLPC and the Ryzen 7600X iGPU with the other AMD compiler, driver 26.8.1 on Windows), OpArrayLength counts a storage buffer descriptor's range as if it were rounded up to a multiple of 4 bytes. It is the same with robustBufferAccess off, on, and with robustBufferAccess2. Binding 0 seen as uint[], uint16_t[] and uint8_t[]:

descriptor range (bytes) uint32 length uint16 length uint8 length
60 15 30 60
61, 62, 63 16 32 64
64 16 32 64

Kyty bounds-checks storage buffer accesses against OpArrayLength (access.length in PrepareStorageBufferResourceAccess), and NativeStorageBuffer binds each descriptor so that it ends where the guest buffer ends. So on AMD, when a guest buffer does not end on a multiple of 4 bytes, accesses to the 1-3 bytes after its end pass the check: loads return what is in memory instead of 0, and stores write past the buffer. On PS5 an access past the end of the buffer (V# NumRecords) is out of range, so the load returns 0 and the store is dropped.

This is why these tests fail on both GPUs:

  • the 36 multi-lane BufferStoreFormatD16HiX* and BufferStoreFormatD16Xyzw* cases (descriptor range wave * stride - 1 bytes): the last record is written;
  • BufferUshortHostOffsetFirst (12 bytes after a 2-byte offset, so the range is 14 bytes): the out-of-range load returns 0x0000face instead of 0.

A fix needs the exact end inside the shader. Two ways I can see:

  1. Put each buffer's descriptor range into the shader data and use it instead of OpArrayLength.
  2. Bind the range rounded up to a multiple of 4 bytes and pass the 0-3 extra bytes along with each buffer's byte offset (the 8-bit lanes that carry those offsets would grow to 16 bits).

@⁠Nmzik which way would you prefer, or would you rather handle it yourself? I can open a PR for it.

Written with Claude Code (AI): it measured this on my PC with a small Vulkan test program and traced it in the code. I checked the results and this text before posting.

**@⁠Nawid3333** on GitHub · 2026-10-17 12:05 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595) On both of my AMD GPUs (RX 9070 XT with LLPC and the Ryzen 7600X iGPU with the other AMD compiler, driver 26.8.1 on Windows), `OpArrayLength` counts a storage buffer descriptor's range as if it were rounded up to a multiple of 4 bytes. It is the same with robustBufferAccess off, on, and with robustBufferAccess2. Binding 0 seen as `uint[]`, `uint16_t[]` and `uint8_t[]`: | descriptor range (bytes) | uint32 length | uint16 length | uint8 length | |---|---|---|---| | 60 | 15 | 30 | 60 | | 61, 62, 63 | 16 | 32 | 64 | | 64 | 16 | 32 | 64 | Kyty bounds-checks storage buffer accesses against `OpArrayLength` (`access.length` in `PrepareStorageBufferResourceAccess`), and `NativeStorageBuffer` binds each descriptor so that it ends where the guest buffer ends. So on AMD, when a guest buffer does not end on a multiple of 4 bytes, accesses to the 1-3 bytes after its end pass the check: loads return what is in memory instead of 0, and stores write past the buffer. On PS5 an access past the end of the buffer (V# NumRecords) is out of range, so the load returns 0 and the store is dropped. This is why these tests fail on both GPUs: - the 36 multi-lane `BufferStoreFormatD16HiX*` and `BufferStoreFormatD16Xyzw*` cases (descriptor range `wave * stride - 1` bytes): the last record is written; - `BufferUshortHostOffsetFirst` (12 bytes after a 2-byte offset, so the range is 14 bytes): the out-of-range load returns `0x0000face` instead of 0. A fix needs the exact end inside the shader. Two ways I can see: 1. Put each buffer's descriptor range into the shader data and use it instead of `OpArrayLength`. 2. Bind the range rounded up to a multiple of 4 bytes and pass the 0-3 extra bytes along with each buffer's byte offset (the 8-bit lanes that carry those offsets would grow to 16 bits). @⁠Nmzik which way would you prefer, or would you rather handle it yourself? I can open a PR for it. Written with Claude Code (AI): it measured this on my PC with a small Vulkan test program and traced it in the code. I checked the results and this text before posting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:01:21 +00:00
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-17 14:38 UTC · original

One more OpArrayLength problem on the same two AMD GPUs: with a non-uniformly indexed descriptor, every lane gets the length of the descriptor the first active lane selected. A small test with two descriptors over one buffer (256 and 128 bytes), lanes alternating between them, NonUniform on the access-chain pointer and shaderStorageBufferArrayNonUniformIndexing enabled: every lane gets 64 (the first descriptor's length in words), or 32 when the first active lane picks the second descriptor. The validation layer reports nothing.

This is why IndirectBufferStore fails here. In its Shared layout, the RX 9070 XT lets lanes 20 and 28 of descriptor 0 store past its end (checked against descriptor 1's length), and the iGPU drops lane 12's store to descriptor 1 (checked against descriptor 0's length).

Option 1 above would fix this as well: the size would come from the shader data with the same non-uniform index that PrepareStorageBufferResourceAccess already uses for the byte offset, so no OpArrayLength is needed.

Written with Claude Code (AI): it measured this on my PC and traced it in the code.

**@⁠Nawid3333** on GitHub · 2026-10-17 14:38 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6235189739) One more `OpArrayLength` problem on the same two AMD GPUs: with a non-uniformly indexed descriptor, every lane gets the length of the descriptor the first active lane selected. A small test with two descriptors over one buffer (256 and 128 bytes), lanes alternating between them, `NonUniform` on the access-chain pointer and `shaderStorageBufferArrayNonUniformIndexing` enabled: every lane gets 64 (the first descriptor's length in words), or 32 when the first active lane picks the second descriptor. The validation layer reports nothing. This is why `IndirectBufferStore` fails here. In its `Shared` layout, the RX 9070 XT lets lanes 20 and 28 of descriptor 0 store past its end (checked against descriptor 1's length), and the iGPU drops lane 12's store to descriptor 1 (checked against descriptor 0's length). Option 1 above would fix this as well: the size would come from the shader data with the same non-uniform index that `PrepareStorageBufferResourceAccess` already uses for the byte offset, so no `OpArrayLength` is needed. Written with Claude Code (AI): it measured this on my PC and traced it in the code.
Author
Owner

@⁠theantipopau on GitHub · 2026-10-19 01:57 UTC · original

Recommendation: option 1 (exact descriptor range in shader data). Sketch: add a per-buffer exact-size dword next to the existing 8-bit memory-offset packing (new base alongside memory_offset_dword in bindings), fill it in RebindBuffers from the already-clamped buffer_sources size, and derive access.length in the emitter as exact_bytes / element_bytes instead of raw OpArrayLength. That makes every downstream check (EmitMemoryElementInBounds, BufferIndexInBounds byte math) exact with no per-access extra ALU, and it also covers truncated sentinel bindings uniformly. Option 2 (bind rounded-up + widen offset lanes to 16 bits) grows shader data for every buffer and repacks the offset path, all to preserve a value OpArrayLength already gives us imprecisely. One caveat for option 1: bindings layout/pipeline-cache hashing must version the new dwords so old cached pipelines mismatch cleanly. Happy to implement option 1 on that basis if you confirm.

**@⁠theantipopau** on GitHub · 2026-10-19 01:57 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6258017528) Recommendation: option 1 (exact descriptor range in shader data). Sketch: add a per-buffer exact-size dword next to the existing 8-bit memory-offset packing (new base alongside memory_offset_dword in bindings), fill it in RebindBuffers from the already-clamped buffer_sources size, and derive access.length in the emitter as exact_bytes / element_bytes instead of raw OpArrayLength. That makes every downstream check (EmitMemoryElementInBounds, BufferIndexInBounds byte math) exact with no per-access extra ALU, and it also covers truncated sentinel bindings uniformly. Option 2 (bind rounded-up + widen offset lanes to 16 bits) grows shader data for every buffer and repacks the offset path, all to preserve a value OpArrayLength already gives us imprecisely. One caveat for option 1: bindings layout/pipeline-cache hashing must version the new dwords so old cached pipelines mismatch cleanly. Happy to implement option 1 on that basis if you confirm.
Author
Owner

@⁠theantipopau on GitHub · 2026-10-19 02:19 UTC · original

Confirming the non-uniform follow-up also points at option 1: if the exact byte size travels in shader data alongside the offset base and is indexed with the same non-uniform index PrepareStorageBufferResourceAccess already uses, the per-lane length is exact and no OpArrayLength query is needed at all - which sidesteps both the 4-byte round-up and the first-lane-broadcast behavior in one change. The ALU cost stays flat (exact_bytes / element_bytes replaces the array-length op) and truncated sentinels get the same treatment. Still happy to implement on that basis once confirmed.

**@⁠theantipopau** on GitHub · 2026-10-19 02:19 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6258271622) Confirming the non-uniform follow-up also points at option 1: if the exact byte size travels in shader data alongside the offset base and is indexed with the same non-uniform index PrepareStorageBufferResourceAccess already uses, the per-lane length is exact and no OpArrayLength query is needed at all - which sidesteps both the 4-byte round-up and the first-lane-broadcast behavior in one change. The ALU cost stays flat (exact_bytes / element_bytes replaces the array-length op) and truncated sentinels get the same treatment. Still happy to implement on that basis once confirmed.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-19 02:29 UTC · original

@⁠theantipopau Option 1 is already implemented in #1603 (open since 10-17): RebindBuffers writes each storage buffer's descriptor range into the shader data after the packed byte offsets (Bindings::MemoryRangeDword()), and the emitter bounds storage-buffer accesses by that range instead of OpArrayLength (only the GDS binding still uses it). Its second commit covers your non-uniform point: for a GPU-selected buffer the range is read with the same per-lane index as the byte offset. A review or a test of #1603 on your hardware would help more than a second implementation.

**@⁠Nawid3333** on GitHub · 2026-10-19 02:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6258370043) @⁠theantipopau Option 1 is already implemented in #1603 (open since 10-17): `RebindBuffers` writes each storage buffer's descriptor range into the shader data after the packed byte offsets (`Bindings::MemoryRangeDword()`), and the emitter bounds storage-buffer accesses by that range instead of `OpArrayLength` (only the GDS binding still uses it). Its second commit covers your non-uniform point: for a GPU-selected buffer the range is read with the same per-lane index as the byte offset. A review or a test of #1603 on your hardware would help more than a second implementation.
Author
Owner

@⁠theantipopau on GitHub · 2026-10-19 03:08 UTC · original

Thanks for the pointer - reviewed #1603 in full and approved. It implements option 1 exactly as discussed (exact ranges in shader data, per-lane index shared with the byte offset for the non-uniform path, GDS binding as the only remaining OpArrayLength user), with CI green on all three platforms. Withdrawing my offer to implement since it's already done - this issue can track #1603 to merge.

**@⁠theantipopau** on GitHub · 2026-10-19 03:08 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6258790902) Thanks for the pointer - reviewed #1603 in full and approved. It implements option 1 exactly as discussed (exact ranges in shader data, per-lane index shared with the byte offset for the non-uniform path, GDS binding as the only remaining OpArrayLength user), with CI green on all three platforms. Withdrawing my offer to implement since it's already done - this issue can track #1603 to merge.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-11-14 16:41 UTC · original

Closing: main has done this since ba7f9969 ("shader: bound storage buffers by pushed binding sizes instead of OpArrayLength") and dc3bd68d ("gpu: bound null storage bindings to zero bytes"). Each storage buffer's bound size now goes into the shader data and bounds its accesses; only the GDS binding still uses OpArrayLength. Neither AMD behaviour above (the range rounded up to 4 bytes, and the first lane's length for a non-uniform index) reaches the storage-buffer checks any more. #1603 was closed for the same reason.

Thanks @⁠theantipopau for the review.

**@⁠Nawid3333** on GitHub · 2026-11-14 16:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1595#issuecomment-6880852894) Closing: main has done this since ba7f9969 ("shader: bound storage buffers by pushed binding sizes instead of OpArrayLength") and dc3bd68d ("gpu: bound null storage bindings to zero bytes"). Each storage buffer's bound size now goes into the shader data and bounds its accesses; only the GDS binding still uses `OpArrayLength`. Neither AMD behaviour above (the range rounded up to 4 bytes, and the first lane's length for a non-uniform index) reaches the storage-buffer checks any more. #1603 was closed for the same reason. Thanks @⁠theantipopau for the review.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1595
No description provided.