Range checking misses soffset-addressed accesses: the argument-writer atomic that broke the transient pool is still unbounded #3

Closed
opened 2026-11-27 03:13:17 +00:00 by github-bot · 4 comments
Owner

@⁠RubAway on GitHub · 2026-11-24 13:59 UTC · original

Range checking misses soffset-addressed accesses — the argument-writer atomic that broke the transient pool is still unbounded

First: ccc277f and the buf_limit rewrite in 8b97849 encode exactly the semantics the title relies on ("append and argument writers run past their allocation, loops read zeros past an end") — great fix direction. I statically inspected the translator and two named kernels from a locally-owned 1.0.2.1 dump (no game code or content beyond five instruction lines quoted below), and I believe the writer your commit message blames ("an argument writer ran past its allocation … into the game's transient pool and broke its block-end nodes") still walks free past the new checks.

Evidence. fivestar shader extract of shaders.rpf + the exe (26,515 records, 25,908 programs, 0 failed — matching your numbers) and fivestar shader disasm of meshlet_culling~CS_AllocBatchCSCullOutput (sgd1_0c3dd3da…fssm), the slot allocator for the cull argument buffer:

002598  s_mul_i32 s11, s11, s0                        ; s11 = stride * index
00259c  s_load_dwordx4 s[0:3], s[4:5], 0x80           ; V# read from the constant bank (game-built)
0025a4  buffer_atomic_add v4, off, s[44:47], s11 glc  ; soffset = s11, vaddr = off (zero)
0025bc  v_add_nc_u32_e32 v4, s50, v4                  ; slot = counter + base
0025c4  buffer_store_format_xyzw v[0:3], v4, s[0:3], 0 idxen

The hole. This atomic addresses entirely through soffset: vaddr = off, no idxen, no offen. BufferAddress.Range (as of 8b97849, docstring still says "VGPR offset + instruction offset, not soffset") then returns offset = 0u + instruction offset, so offset < buf_limit(...) is vacuously true: the one instruction class whose runahead your commit message identified as the corruption mechanism is precisely the class the range check does not bound. The dependent idxen store is checked, but its slot index comes from the unbounded atomic, so a stale or wrongly-strided counter still marches records through memory — the buffer_store_format_xyzw/idxen pair above is exactly the 20-byte indirect-argument record pattern. With the prelude comment acknowledging "soffset is not checked", this may be a known limitation, but it changes what a retranslate is expected to fix: the allocator atomics need the check folded in before the transient-pool corruption can be expected to disappear.

Fix shape (as small as the current rewrite makes it comfortable): fold soffset into the checked offset in Range — bytes += operands.Read(soffset) — for MUBUF/MTBUF/sBUF forms whose addressing includes an SGPR offset; console OOB_SELECT checks the final address including soffset. The CS_AllocBatchCSCullOutput case above would then be the reference test case.

Secondary, all acknowledged in your own comments, ranked: (1) structured sel 0/1 omits the offset-vs-stride check — a store at offset ≥ stride inside record N silently overwrites the first dword of record N+1 (e.g. InstanceCount of the following indirect-arg record); (2) shim-built V#s leave OOB_SELECT at 0, so raw byte-indexed views get structured semantics — both false drops and missed drops; (3) V#s over the address map (root-descriptor-built, "through the address map" per the ccc277f message) have no num_records to check against unless the game itself wrote them — the address map knows each committed range's extent, and capping num_records at the distance to the end of that range is a sound upper bound for free.

Reproduce: fivestar shader extract <dump>/shaders.rpf <out> --exe <dump>/game_scarlett_master.exe; open <out>/fssm/*CS_AllocBatchCSCullOutput*.fssm with fivestar shader disasm; read BufferAddress.Range at ec72819. Happy to run a fuller static pass over the cull chain (there are sibling kernels CS_CullMeshletsInitArgs, CS_BatchCullInitArgs, CS_InitVisibleCount, CS_CountVisibleMeshletsBatchCS etc. in the same pack) and report per-kernel addressing forms if useful.

Analysis from a neutral third party on request; all claims above are static disassembly + source reading, no runtime involved.

**@⁠RubAway** on GitHub · 2026-11-24 13:59 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/3) ### Range checking misses soffset-addressed accesses — the argument-writer atomic that broke the transient pool is still unbounded First: `ccc277f` and the `buf_limit` rewrite in `8b97849` encode exactly the semantics the title relies on ("append and argument writers run past their allocation, loops read zeros past an end") — great fix direction. I statically inspected the translator and two named kernels from a locally-owned 1.0.2.1 dump (no game code or content beyond five instruction lines quoted below), and I believe the writer your commit message blames ("an argument writer ran past its allocation … into the game's transient pool and broke its block-end nodes") still walks free past the new checks. **Evidence.** `fivestar shader extract` of `shaders.rpf` + the exe (26,515 records, 25,908 programs, 0 failed — matching your numbers) and `fivestar shader disasm` of `meshlet_culling~CS_AllocBatchCSCullOutput` (`sgd1_0c3dd3da…fssm`), the slot allocator for the cull argument buffer: ``` 002598 s_mul_i32 s11, s11, s0 ; s11 = stride * index 00259c s_load_dwordx4 s[0:3], s[4:5], 0x80 ; V# read from the constant bank (game-built) 0025a4 buffer_atomic_add v4, off, s[44:47], s11 glc ; soffset = s11, vaddr = off (zero) 0025bc v_add_nc_u32_e32 v4, s50, v4 ; slot = counter + base 0025c4 buffer_store_format_xyzw v[0:3], v4, s[0:3], 0 idxen ``` **The hole.** This atomic addresses entirely through soffset: `vaddr = off`, no `idxen`, no `offen`. `BufferAddress.Range` (as of `8b97849`, docstring still says "VGPR offset + instruction offset, not soffset") then returns offset = `0u + instruction offset`, so `offset < buf_limit(...)` is vacuously true: **the one instruction class whose runahead your commit message identified as the corruption mechanism is precisely the class the range check does not bound.** The dependent `idxen` store is checked, but its slot index comes from the unbounded atomic, so a stale or wrongly-strided counter still marches records through memory — the `buffer_store_format_xyzw`/`idxen` pair above is exactly the 20-byte indirect-argument record pattern. With the prelude comment acknowledging "soffset is not checked", this may be a known limitation, but it changes what a retranslate is expected to fix: the allocator atomics need the check folded in before the transient-pool corruption can be expected to disappear. **Fix shape** (as small as the current rewrite makes it comfortable): fold soffset into the checked offset in `Range` — `bytes += operands.Read(soffset)` — for MUBUF/MTBUF/sBUF forms whose addressing includes an SGPR offset; console OOB_SELECT checks the final address including soffset. The `CS_AllocBatchCSCullOutput` case above would then be the reference test case. **Secondary, all acknowledged in your own comments, ranked:** (1) structured sel 0/1 omits the offset-vs-stride check — a store at offset ≥ stride inside record N silently overwrites the first dword of record N+1 (e.g. `InstanceCount` of the following indirect-arg record); (2) shim-built V#s leave OOB_SELECT at 0, so raw byte-indexed views get structured semantics — both false drops and missed drops; (3) V#s over the address map (root-descriptor-built, "through the address map" per the `ccc277f` message) have no `num_records` to check against unless the game itself wrote them — the address map knows each committed range's extent, and capping `num_records` at the distance to the end of that range is a sound upper bound for free. **Reproduce:** `fivestar shader extract <dump>/shaders.rpf <out> --exe <dump>/game_scarlett_master.exe`; open `<out>/fssm/*CS_AllocBatchCSCullOutput*.fssm` with `fivestar shader disasm`; read `BufferAddress.Range` at `ec72819`. Happy to run a fuller static pass over the cull chain (there are sibling kernels `CS_CullMeshletsInitArgs`, `CS_BatchCullInitArgs`, `CS_InitVisibleCount`, `CS_CountVisibleMeshletsBatchCS` etc. in the same pack) and report per-kernel addressing forms if useful. Analysis from a neutral third party on request; all claims above are static disassembly + source reading, no runtime involved.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:13:18 +00:00
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 14:32 UTC · original

Thanks, this is a really useful read. We're checking it against the RDNA2 ISA now: as far as I know the hardware leaves soffset out of the range check, in which case the console relies on that counter being correct and our bug is the counter's value (a missing reset, or the init pass not running) rather than the check. Either way your third point (capping num_records at the committed range for address-map views) is a sound safety net and we're adding it. A heap corruption with INT_MAX/INT_MIN-like values still shows up occasionally, so this lines up with what we see. Will post what we find.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 14:32 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/3#issuecomment-7134534336) Thanks, this is a really useful read. We're checking it against the RDNA2 ISA now: as far as I know the hardware leaves soffset out of the range check, in which case the console relies on that counter being correct and our bug is the counter's value (a missing reset, or the init pass not running) rather than the check. Either way your third point (capping num_records at the committed range for address-map views) is a sound safety net and we're adding it. A heap corruption with INT_MAX/INT_MIN-like values still shows up occasionally, so this lines up with what we see. Will post what we find.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-24 14:38 UTC · original

Update: you were right about soffset, and my GCN-era recollection was wrong. Measured RDNA hardware (gfx1036 and gfx1011) checks raw buffers as offset + soffset + 4 > num_records, with the index not checked in raw mode; structured mode 0 also checks offset against stride. The translator now follows that table, buffer records carry a realistic OOB_SELECT per view kind (raw 3, structured 0, typed/CBV 1), and a WARP case with a soffset-only load fails without the fix (83572fc). One correction to the kernel name: the soffset-only slot atomic is in CS_BatchCullBatchCSMeshlets (and _Prev), plus eight sibling culling kernels. Thanks for the careful analysis.

**@⁠ThatRecompDev** on GitHub · 2026-11-24 14:38 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/3#issuecomment-7134704081) Update: you were right about soffset, and my GCN-era recollection was wrong. Measured RDNA hardware (gfx1036 and gfx1011) checks raw buffers as offset + soffset + 4 > num_records, with the index not checked in raw mode; structured mode 0 also checks offset against stride. The translator now follows that table, buffer records carry a realistic OOB_SELECT per view kind (raw 3, structured 0, typed/CBV 1), and a WARP case with a soffset-only load fails without the fix (83572fc). One correction to the kernel name: the soffset-only slot atomic is in CS_BatchCullBatchCSMeshlets (and _Prev), plus eight sibling culling kernels. Thanks for the careful analysis.
Author
Owner

@⁠RubAway on GitHub · 2026-11-24 15:22 UTC · original

Thanks — glad it landed, and the correction is taken: CS_BatchCullBatchCSMeshlets (+_Prev and the siblings) fits what we saw around the allocator chain; the soffset-only atomic we quoted sits in the same addressing family.

Two things from our side that might help with the INT_MAX/INT_MIN-shaped corruption that still shows up occasionally:

  1. A wraparound note on the measured raw rule: with offset + soffset + 4 > num_records evaluated in u32, a negative byte offset (two's complement, e.g. 0xFFFFFFxx from a signed slot = idx - count underflow) wraps the sum back under num_records and passes the check while aiming far below the buffer base. The hardware agrees with that wrap, so matching the console is still right — but it means the range check cannot catch signed underflow in the slot computation itself, and INT_MIN-like written values smell exactly like that underflow propagating through records rather than a bounds miss. If the log of dropped accesses is cheap, it may be worth also logging, per argument-writing kernel, the atomic return values that fall outside [0, capacity) — a slot index assert rather than a byte bounds check.

  2. We are re-running a full ahead-of-time translate of the 25,908 programs on the current main with your 83572fc semantics (soffset-inclusive raw checks, per-kind OOB_SELECT). When it finishes we can post a static listing from shader disasm: every program that contains soffset-only buffer writes and every one that writes the 20-byte indirect-argument record pattern — a map of which kernels the new checks actually bound, versus which rely on their counters being correct. Useful, say the word; otherwise we'll just keep watching main.

**@⁠RubAway** on GitHub · 2026-11-24 15:22 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/3#issuecomment-7135915851) Thanks — glad it landed, and the correction is taken: `CS_BatchCullBatchCSMeshlets` (+`_Prev` and the siblings) fits what we saw around the allocator chain; the soffset-only atomic we quoted sits in the same addressing family. Two things from our side that might help with the INT_MAX/INT_MIN-shaped corruption that still shows up occasionally: 1. A wraparound note on the measured raw rule: with `offset + soffset + 4 > num_records` evaluated in `u32`, a **negative** byte offset (two's complement, e.g. `0xFFFFFFxx` from a signed `slot = idx - count` underflow) wraps the sum back under `num_records` and passes the check while aiming far below the buffer base. The hardware agrees with that wrap, so matching the console is still right — but it means the range check cannot catch signed underflow in the slot computation itself, and INT_MIN-like written values smell exactly like that underflow propagating through records rather than a bounds miss. If the log of dropped accesses is cheap, it may be worth also logging, per argument-writing kernel, the atomic return values that fall outside `[0, capacity)` — a slot index assert rather than a byte bounds check. 2. We are re-running a full ahead-of-time translate of the 25,908 programs on the current main with your `83572fc` semantics (soffset-inclusive raw checks, per-kind OOB_SELECT). When it finishes we can post a static listing from `shader disasm`: every program that contains soffset-only buffer writes and every one that writes the 20-byte indirect-argument record pattern — a map of which kernels the new checks actually bound, versus which rely on their counters being correct. Useful, say the word; otherwise we'll just keep watching main.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-25 08:10 UTC · original

closing this one: soffset now counts in the range check for raw buffers (that's what the hardware measurements showed), and we audited every buffer access form the game uses against the GFX10 rules, with a WARP test per rule. if you still see the argument-writer atomic go out of range on current main, reopen

**@⁠ThatRecompDev** on GitHub · 2026-11-25 08:10 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/3#issuecomment-7156705854) closing this one: soffset now counts in the range check for raw buffers (that's what the hardware measurements showed), and we audited every buffer access form the game uses against the GFX10 rules, with a WARP test per rule. if you still see the argument-writer atomic go out of range on current main, reopen
This discussion has been locked. Commenting is limited to contributors.
No description provided.