[PR #1068] [CLOSED] rdna2spirv: form MUBUF byte offsets in 64 bits (SHADER-MUBUF-OFFSET-WRAP) #1107

Closed
opened 2026-11-26 20:20:06 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1068
Author: @Moriz82
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: split/shader-mubuf-offset-wrap


📝 Commits (1)

  • a656d4f rdna2spirv: form MUBUF byte offsets in 64 bits (SHADER-MUBUF-OFFSET-WRAP)

📊 Changes

4 files changed (+212 additions, -26 deletions)

View changed files

📝 docs/buffer_bounds.md (+5 -0)
📝 docs/design.md (+1 -1)
➕ tests/semantic/test_buffer_offset_wrap.py (+173 -0)
📝 tools/rdna2spirv.py (+33 -25)

📄 Description

Claimed in the coordination issue as: SHADER-MUBUF-OFFSET-WRAP (claim).

Part of the split of #984 (first child). Depends on: none. Overlaps: none found (searched open and merged pull requests for buffer_address, stride, index and offset wrap; #513 changes the CBV's V# stride in the runtime, not the translator's address arithmetic; #1021 edits docs/design.md near the swizzled-buffer row).

What and why

Gen.buffer_address in tools/rdna2spirv.py forms the byte offset of every MUBUF/MTBUF access, off + soffset + index * stride (and the swizzled form), in 32 bits and adds it to the 48-bit V# base. The hardware does not wrap there: the RDNA2 ISA (8.1.5) gives the address as base + stride * index + offset with a 48-bit base, and only off (vaddr + inst_offset) wraps at 32 bits. So an index of 0xFFFFFFFF with a 20-byte stride addresses base + 0x13_FFFF_FFEC on the console, while the translated shader turns it into base − 20:

  • OOB_SELECT 3 (raw) checks offset + soffset against NUM_RECORDS and does not look at the index;
  • OOB_SELECT 2 (disabled) checks only that the buffer is not empty.

Both let the wrapped access through, so a store writes 20 bytes below the buffer and a load reads them. When the view is one slice of a larger pool that is mapped as a whole, that is a silent write into a neighbour's records.

The change forms the linear and the swizzled offset as a 64-bit value and adds that to the base. Nothing else changes: each mode checks exactly what it checked before, and a literal V# and a run-time V# take the same path.

What changed since the first version

The first version also treated every offset above 0xFFFFFFFF as out of range. The reviews showed two faults in that, and both are fixed by removing the bound instead of patching it:

  • Mode 2 has no such limit. An access 5 GiB past the base that its mode allows was dropped. It is now made at its 64-bit address, and that case is a test.
  • A literal mode 0 or 1 V# skipped the check that a run-time V# took, so folding the descriptor changed the result. There is no check left to skip, and the matched literal and run-time cases are a test.

What this does not do

  • It does not make a wild index safe. Index −1 in mode 2 or 3 is no longer the 20 bytes below the buffer, and it is not dropped either: it addresses tens of GiB past the base. What the console does with such an address when nothing is mapped there is not measured. On Vulkan an access outside the buffer is undefined. The runtime's GPU address arena is at most 4095 MiB (docs/d3d12_vulkan.md), so there an offset of 4 GiB or more always leaves the arena. Whether the runtime should refuse an address outside the arena is a separate decision, with the arena size as the bound and not 2^32. It is not part of this pull request.
  • The non-wrapping product is five-star-recomp's hardware observation (ThatRecompDev, GPL-3, commit b9b57e2), credited in the source. It was not measured by us on AMD hardware; the contract row and docs/buffer_bounds.md say so. Someone with the tools/hw_oracle setup could pin it with one probe (raw view, stride 20, index −1).

Cost: two or three 64-bit conversions and adds and one 64-bit multiply per access, now for every access including folded structured views (the first version left those alone). On a sample of 200 compute programs of a retail library (translated locally with --wave64-emulate, not included) 114 modules change and the 200 grow by 0.45 % together; all 200 pass spirv-val before and after.

Tested

Linux (CachyOS, kernel 7.3.5), Python 3.14, LLVM llvm-mc 23.1.5 and SPIRV-Tools v2026.4 from the distribution. GPU suite on an NVIDIA GeForce RTX 4070, driver 620.73.04.

  • python3 -I tests/semantic/test_buffer_offset_wrap.py: 8 tests pass. With the translator of the base commit the same file has 13 failing subtests: index −1 in raw and disabled mode with a literal and a run-time V# (4), the mapped 5 GiB offset in disabled mode (2), a structured index below NUM_RECORDS whose product passes 4 GiB in modes 0 and 1 (4), a swizzled offset past 4 GiB with a literal and a run-time V# (2), and off 0xFFFFFFF0 + soffset 0x20 (1). The swizzled control below 4 GiB passes on both. The swizzled cases were added after a review noted that the swizzled path had moved to 64 bits without a case above 4 GiB.
  • python3 -I scripts/run_semantic_tests.py tests/semantic: 471 files, 6,176 tests, 0 files failed.
  • The standard-library and pytest files under tests/ the way ci.yml runs them: 520 files, 519 pass. tests/test_imhex_patterns.py fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change.
  • python3 -I scripts/check_no_game_data.py: passes.
  • python3 -I scripts/run_gpu_tests.py (run on the head before the swizzled tests were added; the translator has not changed since): 261 shaders translate and pass spirv-val; 202 of 252 tests pass, 49 are skipped and 1 fails (cs_imgstore_cube). The run with the base commit's translator gives the same 202, 49 and 1, test for test. The harness programs came from an older native build on this machine (2026-11-23) and were not rebuilt for this, which is the likely reason for that one failure; it is not a result of this change.

Not run by us: native Windows, macOS, an AMD GPU, the tools/hw_oracle probe. A reviewer ran the GPU suite on an AMD RX 9070 XT (236 of 252 pass, 16 skipped, 0 failed) on e01626be9, which has the same translator as this head.

GPUs tested: NVIDIA GeForce RTX 4070 (translation only; see above for what is not measured on hardware).

AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named.

Checklist

  • Based on upstream main 32950551e and merges cleanly into current main.
  • One topic.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • The rule is in the contract table of docs/design.md ("Buffer bounds checking"), with how it was confirmed and what is not measured.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1068 **Author:** [@Moriz82](https://github.com/Moriz82) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `split/shader-mubuf-offset-wrap` --- ### 📝 Commits (1) - [`a656d4f`](https://github.com/ubiquitarianism/leonida/commit/a656d4f0d3f68184053769390edd2320ef25760a) rdna2spirv: form MUBUF byte offsets in 64 bits (SHADER-MUBUF-OFFSET-WRAP) ### 📊 Changes **4 files changed** (+212 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `docs/buffer_bounds.md` (+5 -0) 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/test_buffer_offset_wrap.py` (+173 -0) 📝 `tools/rdna2spirv.py` (+33 -25) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-MUBUF-OFFSET-WRAP ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7139398473)). Part of the split of #984 (first child). Depends on: none. Overlaps: none found (searched open and merged pull requests for `buffer_address`, stride, index and offset wrap; #513 changes the CBV's V# stride in the runtime, not the translator's address arithmetic; #1021 edits `docs/design.md` near the swizzled-buffer row). ### What and why `Gen.buffer_address` in `tools/rdna2spirv.py` forms the byte offset of every MUBUF/MTBUF access, `off + soffset + index * stride` (and the swizzled form), in 32 bits and adds it to the 48-bit V# base. The hardware does not wrap there: the RDNA2 ISA (8.1.5) gives the address as base + stride * index + offset with a 48-bit base, and only `off` (vaddr + inst_offset) wraps at 32 bits. So an index of `0xFFFFFFFF` with a 20-byte stride addresses base + 0x13_FFFF_FFEC on the console, while the translated shader turns it into **base − 20**: - `OOB_SELECT 3` (raw) checks `offset + soffset` against `NUM_RECORDS` and does not look at the index; - `OOB_SELECT 2` (disabled) checks only that the buffer is not empty. Both let the wrapped access through, so a store writes 20 bytes below the buffer and a load reads them. When the view is one slice of a larger pool that is mapped as a whole, that is a silent write into a neighbour's records. The change forms the linear and the swizzled offset as a 64-bit value and adds that to the base. Nothing else changes: each mode checks exactly what it checked before, and a literal V# and a run-time V# take the same path. ### What changed since the first version The first version also treated every offset above `0xFFFFFFFF` as out of range. The reviews showed two faults in that, and both are fixed by removing the bound instead of patching it: - Mode 2 has no such limit. An access 5 GiB past the base that its mode allows was dropped. It is now made at its 64-bit address, and that case is a test. - A literal mode 0 or 1 V# skipped the check that a run-time V# took, so folding the descriptor changed the result. There is no check left to skip, and the matched literal and run-time cases are a test. ### What this does not do - It does not make a wild index safe. Index −1 in mode 2 or 3 is no longer the 20 bytes below the buffer, and it is not dropped either: it addresses tens of GiB past the base. What the console does with such an address when nothing is mapped there is **not measured**. On Vulkan an access outside the buffer is undefined. The runtime's GPU address arena is at most 4095 MiB (`docs/d3d12_vulkan.md`), so there an offset of 4 GiB or more always leaves the arena. Whether the runtime should refuse an address outside the arena is a separate decision, with the arena size as the bound and not 2^32. It is not part of this pull request. - The non-wrapping product is five-star-recomp's hardware observation (ThatRecompDev, GPL-3, commit `b9b57e2`), credited in the source. It was **not** measured by us on AMD hardware; the contract row and `docs/buffer_bounds.md` say so. Someone with the `tools/hw_oracle` setup could pin it with one probe (raw view, stride 20, index −1). Cost: two or three 64-bit conversions and adds and one 64-bit multiply per access, now for every access including folded structured views (the first version left those alone). On a sample of 200 compute programs of a retail library (translated locally with `--wave64-emulate`, not included) 114 modules change and the 200 grow by 0.45 % together; all 200 pass `spirv-val` before and after. ### Tested Linux (CachyOS, kernel 7.3.5), Python 3.14, LLVM `llvm-mc` 23.1.5 and SPIRV-Tools v2026.4 from the distribution. GPU suite on an NVIDIA GeForce RTX 4070, driver 620.73.04. - `python3 -I tests/semantic/test_buffer_offset_wrap.py`: 8 tests pass. With the translator of the base commit the same file has **13 failing subtests**: index −1 in raw and disabled mode with a literal and a run-time V# (4), the mapped 5 GiB offset in disabled mode (2), a structured index below `NUM_RECORDS` whose product passes 4 GiB in modes 0 and 1 (4), a swizzled offset past 4 GiB with a literal and a run-time V# (2), and `off 0xFFFFFFF0 + soffset 0x20` (1). The swizzled control below 4 GiB passes on both. The swizzled cases were added after a review noted that the swizzled path had moved to 64 bits without a case above 4 GiB. - `python3 -I scripts/run_semantic_tests.py tests/semantic`: 471 files, 6,176 tests, 0 files failed. - The standard-library and pytest files under `tests/` the way `ci.yml` runs them: 520 files, 519 pass. `tests/test_imhex_patterns.py` fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change. - `python3 -I scripts/check_no_game_data.py`: passes. - `python3 -I scripts/run_gpu_tests.py` (run on the head before the swizzled tests were added; the translator has not changed since): 261 shaders translate and pass `spirv-val`; 202 of 252 tests pass, 49 are skipped and 1 fails (`cs_imgstore_cube`). The run with the base commit's translator gives the same 202, 49 and 1, test for test. The harness programs came from an older native build on this machine (2026-11-23) and were not rebuilt for this, which is the likely reason for that one failure; it is not a result of this change. Not run by us: native Windows, macOS, an AMD GPU, the `tools/hw_oracle` probe. A reviewer ran the GPU suite on an AMD RX 9070 XT (236 of 252 pass, 16 skipped, 0 failed) on `e01626be9`, which has the same translator as this head. GPUs tested: NVIDIA GeForce RTX 4070 (translation only; see above for what is not measured on hardware). AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named. ### Checklist - [x] Based on upstream main `32950551e` and merges cleanly into current `main`. - [x] One topic. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] The rule is in the contract table of `docs/design.md` ("Buffer bounds checking"), with how it was confirmed and what is not measured. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:20:06 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1107
No description provided.