fix(agc): clear a reused pooled buffer's unused tail on macOS #11303

Merged
iamthehimansh merged 3 commits from upstream-pr/11303 into main 2026-10-27 20:29:53 +00:00
iamthehimansh commented 2026-10-26 01:50:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by iamthehimansh on 2026-10-26T01:50:22Z.


What

Next part of splitting #8658: agc_driver_pooled_buffer_range fails on macOS. MoltenVK has no robustBufferAccess2, so a robust read past the end of a pooled buffer is clamped into its slot instead of returning zero. A 100-byte buffer placed in a 256-byte slot then reads the bytes an earlier user left there.

On macOS, when a host-coherent mapped slot is handed to a new buffer, Graphics/src/Resources.cpp now clears the bytes past the requested size to zero. That happens on both paths: a reused class allocation, and a new slot in a slab. Linux and Windows compile the same code as before.

I compared the added lines with all 1,377 open PRs; none carries this code.

Tested

Apple M2 Pro, macOS 26.6.2, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.0), x86_64 Release under Rosetta 2, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The validation layer loaded in every run (loader output shows it inserted, with no "was not found" line). It reports one VUID-VkDeviceCreateInfo-pProperties-04451 error per run on both sides, 3 of 3 without and 3 of 3 with this PR. That error comes from the test fixture creating its own device without VK_KHR_portability_subset (#10920). This PR adds no new VUID.

libSceAgcDriver does not link on macOS on main alone, so the test runs on a local base. It is main at b27cc8416, two commits behind this head's cbee22480, and those two only change libc's tanh. On top of that:

  • the open macOS PRs that merge cleanly: #10552 (8d601f9ec), #10898 (119e87952), #8311 (16e6ee822) and #9738 (7a77240aa);
  • local-only macOS patches for the files still failing there: Thread.cpp, Process.cpp and Exception.cpp from #11246, #11282 and #11249; DirectMemory.cpp, libc's GuestAllocations.cpp, and the AGC driver's Lifecycle.cpp (with its DriverMemory.hpp), GuestMemory.cpp, GuestBufferMemory.cpp and Texture.cpp from #8658's branch; and an extern char** environ; declaration in FrameRecorder.cpp.

On that base, Resources.cpp was main's file, then this PR's exact file at 1ac3e3561:

Resources.cpp agc_driver_pooled_buffer_range
main fail, 2 of 2: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory"
this PR pass, 3 of 3

@Gonic0 (M5 Pro) and @maximilianfv (M1 Pro) report the same split above.

The file's text outside __APPLE__ blocks equals main. python3 tools/check_conventions.py --base origin/main passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none to build; running the test on macOS needs libkernel, libc and the AGC driver to build there (#10552, #7804, #10898)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M2 Pro (MoltenVK 1.4.2, macOS 26.6.2); also Apple M5 Pro and M1 Pro per the reviews above
  • AI-assisted: yes (Cana)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5010818649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11303). Originally posted by [iamthehimansh](https://github.com/iamthehimansh) on 2026-10-26T01:50:22Z. --- ### What Next part of splitting #8658: `agc_driver_pooled_buffer_range` fails on macOS. MoltenVK has no `robustBufferAccess2`, so a robust read past the end of a pooled buffer is clamped into its slot instead of returning zero. A 100-byte buffer placed in a 256-byte slot then reads the bytes an earlier user left there. On macOS, when a host-coherent mapped slot is handed to a new buffer, `Graphics/src/Resources.cpp` now clears the bytes past the requested size to zero. That happens on both paths: a reused class allocation, and a new slot in a slab. Linux and Windows compile the same code as before. I compared the added lines with all 1,377 open PRs; none carries this code. ### Tested Apple M2 Pro, macOS 26.6.2, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.0), x86_64 Release under Rosetta 2, with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. The validation layer loaded in every run (loader output shows it inserted, with no "was not found" line). It reports one `VUID-VkDeviceCreateInfo-pProperties-04451` error per run on both sides, 3 of 3 without and 3 of 3 with this PR. That error comes from the test fixture creating its own device without `VK_KHR_portability_subset` (#10920). This PR adds no new `VUID`. `libSceAgcDriver` does not link on macOS on `main` alone, so the test runs on a local base. It is `main` at `b27cc8416`, two commits behind this head's `cbee22480`, and those two only change libc's `tanh`. On top of that: - the open macOS PRs that merge cleanly: #10552 (`8d601f9ec`), #10898 (`119e87952`), #8311 (`16e6ee822`) and #9738 (`7a77240aa`); - local-only macOS patches for the files still failing there: `Thread.cpp`, `Process.cpp` and `Exception.cpp` from #11246, #11282 and #11249; `DirectMemory.cpp`, libc's `GuestAllocations.cpp`, and the AGC driver's `Lifecycle.cpp` (with its `DriverMemory.hpp`), `GuestMemory.cpp`, `GuestBufferMemory.cpp` and `Texture.cpp` from #8658's branch; and an `extern char** environ;` declaration in `FrameRecorder.cpp`. On that base, `Resources.cpp` was `main`'s file, then this PR's exact file at `1ac3e3561`: | `Resources.cpp` | `agc_driver_pooled_buffer_range` | |---|---| | `main` | fail, 2 of 2: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory" | | this PR | pass, 3 of 3 | @Gonic0 (M5 Pro) and @maximilianfv (M1 Pro) report the same split above. The file's text outside `__APPLE__` blocks equals `main`. `python3 tools/check_conventions.py --base origin/main` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none to build; running the test on macOS needs `libkernel`, libc and the AGC driver to build there (#10552, #7804, #10898) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M2 Pro (MoltenVK 1.4.2, macOS 26.6.2); also Apple M5 Pro and M1 Pro per the reviews above - [x] AI-assisted: yes ([Cana](https://cana.build))
MarcZX10 commented 2026-10-26 04:43:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T04:43:49Z.

Review: CHANGES_REQUESTED


I reviewed exact head 8a9a741b684469cce9997266ed44d7e2d0e53aea. The Apple-only helper clears only the mapped, host-coherent allocation tail and bounds the range by both the buffer capacity and allocation size. The author and an independent M5 Pro run report the targeted regression passing with the patch and failing without it; the independent run also reports no new validation errors (the listed validation messages are present in both states).

Before approval, please refresh this branch onto current main (9b197b6ce4583e64bf2427e6beb7270bcfa1528e) and rerun the focused macOS test on that exact head. tools/check_main_sync.py reports this head is 1 commit ahead and 101 behind (merge base a3987a1ec), although a merge preview is clean. The hosted checks currently pass only policy/conventions/intake/labels/report; product jobs are path-skipped. I also manually checked the overlap bot's Resources.cpp warnings against #7642, #9508 and #10193: their changes in that file do not conflict with this tail-clear hunk. I have not approved pending the current-main refresh required by the repository's branch-up-to-date rule.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5647504276 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11303#pullrequestreview-5647504276). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T04:43:49Z. **Review:** CHANGES_REQUESTED --- I reviewed exact head `8a9a741b684469cce9997266ed44d7e2d0e53aea`. The Apple-only helper clears only the mapped, host-coherent allocation tail and bounds the range by both the buffer capacity and allocation size. The author and an independent M5 Pro run report the targeted regression passing with the patch and failing without it; the independent run also reports no new validation errors (the listed validation messages are present in both states). Before approval, please refresh this branch onto current `main` (`9b197b6ce4583e64bf2427e6beb7270bcfa1528e`) and rerun the focused macOS test on that exact head. `tools/check_main_sync.py` reports this head is 1 commit ahead and 101 behind (merge base `a3987a1ec`), although a merge preview is clean. The hosted checks currently pass only policy/conventions/intake/labels/report; product jobs are path-skipped. I also manually checked the overlap bot's `Resources.cpp` warnings against #7642, #9508 and #10193: their changes in that file do not conflict with this tail-clear hunk. I have not approved pending the current-main refresh required by the repository's branch-up-to-date rule.
MarcZX10 commented 2026-10-26 06:20:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T06:20:55Z.

Review: DISMISSED


The independent M5 Pro validation is useful: it reports the target test failing 16/16 without the change and passing 16/16 with it, with no new validation-layer errors. The tested tree is still based on #8658’s 27e9689743, though. Against current upstream/main 14c99b52ace303eef1667764670d1d1540140b3d, this exact PR head is 1 ahead and 121 behind (merge preview clean). Please refresh onto current main and rerun agc_driver_pooled_buffer_range on that exact head; the previous approval blocker is current-main validation, not the quality of the reported M5 reproduction.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648093336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11303#pullrequestreview-5648093336). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T06:20:55Z. **Review:** DISMISSED --- The independent M5 Pro validation is useful: it reports the target test failing 16/16 without the change and passing 16/16 with it, with no new validation-layer errors. The tested tree is still based on #8658’s `27e9689743`, though. Against current `upstream/main` `14c99b52ace303eef1667764670d1d1540140b3d`, this exact PR head is 1 ahead and 121 behind (merge preview clean). Please refresh onto current main and rerun `agc_driver_pooled_buffer_range` on that exact head; the previous approval blocker is current-main validation, not the quality of the reported M5 reproduction.
maximilianfv commented 2026-10-26 07:43:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-26T07:43:30Z.

Review: CHANGES_REQUESTED


Requesting changes for the PR text only. The code reads correct, and my A/B confirms the fix in both directions with no new validation errors. The body still needs the validation-layer statement and the GPU line that CONTRIBUTING asks for. The run below has the data.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at 8a9a741b6 to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. MoltenVK from the Vulkan SDK 1.4.363.0, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The layer loaded in every run (no was not found but was requested line).

agc_driver_pooled_buffer_range agc_static_buffer agc_write_tracking Validation Error lines per run
Without this PR fails 23 of 23: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory" bus error 3 of 3 (EXC_BAD_ACCESS in sceAgcGetStaticBuffer) fails 3 of 3: "a block the import window did not touch reads as written" 1
With this PR passes 23 of 23 same bus error 3 of 3 same failure 3 of 3 1

On each side, 3 runs of all three tests plus 20 more runs of agc_driver_pooled_buffer_range alone. The one validation error on both sides is VUID-VkDeviceCreateInfo-pProperties-04451, from the test creating its own device without VK_KHR_portability_subset (#10920). The PR adds no VUID-. agc_static_buffer and agc_write_tracking fail the same way without the PR on this tree and do not involve Resources.cpp.

I also checked the premise. MoltenVK reports vkGetBufferMemoryRequirements().size = 100 for the 100-byte buffer: I added a temporary print in BindExactBuffer, then removed it. So word 28 (byte 112) lies past the memory range bound to the buffer. Under robustBufferAccess, MoltenVK should not return the earlier user's bytes there. The read is a driver deviation, not behaviour the spec allows, and clearing the tail is a fair workaround.

Findings

  1. PR body, Tested and GPUs tested: CONTRIBUTING.md:68 is not met. An AGC change needs a run with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1 on main and on the change, a statement that the layer loaded and that the change adds no new VUID-, and vendor, model, driver and OS on the GPUs tested line. The body has none of the first three, and its GPU line lacks the OS. Fix: add your own validation run, or cite this one or Gonic0's, and write the line as e.g. Apple M2 Pro, MoltenVK 1.4.2, macOS 26. As MarcZX10 asked, refresh onto current main before that run. The merge base is a3987a1ec, and Resources.cpp has not changed on main since.

  2. Suggestion, not blocking, Resources.cpp:9, 18, 66, 110: key the clear on the driver rather than on __APPLE__. The deviation belongs to MoltenVK, not to the platform. On this Mac, vulkaninfo also lists KosmicKrisp, with robustBufferAccess2 = true, and the #ifdef runs the memset there for nothing. A check of VkPhysicalDeviceDriverProperties::driverID == VK_DRIVER_ID_MOLTENVK (or of the device lacking robustBufferAccess2, which VulkanDevice.cpp:2086 already computes but Graphics::Context does not carry) would be exact and would drop the #ifdef. The test's own fixture fills Context itself, so it would need to set the flag.

  3. Nit, Resources.cpp:9-12, 23: <cstddef> is included only under __APPLE__, but std::byte is used unconditionally in Buffer::Bytes(). There is also a blank line before the #endif at line 24 and none after it.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648798881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11303#pullrequestreview-5648798881). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-26T07:43:30Z. **Review:** CHANGES_REQUESTED --- Requesting changes for the PR text only. The code reads correct, and my A/B confirms the fix in both directions with no new validation errors. The body still needs the validation-layer statement and the GPU line that CONTRIBUTING asks for. The run below has the data. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `8a9a741b6` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. MoltenVK from the Vulkan SDK 1.4.363.0, with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. The layer loaded in every run (no `was not found but was requested` line). | | `agc_driver_pooled_buffer_range` | `agc_static_buffer` | `agc_write_tracking` | `Validation Error` lines per run | |---|---|---|---|---| | Without this PR | fails 23 of 23: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory" | bus error 3 of 3 (EXC_BAD_ACCESS in `sceAgcGetStaticBuffer`) | fails 3 of 3: "a block the import window did not touch reads as written" | 1 | | With this PR | passes 23 of 23 | same bus error 3 of 3 | same failure 3 of 3 | 1 | On each side, 3 runs of all three tests plus 20 more runs of `agc_driver_pooled_buffer_range` alone. The one validation error on both sides is `VUID-VkDeviceCreateInfo-pProperties-04451`, from the test creating its own device without `VK_KHR_portability_subset` (#10920). The PR adds no `VUID-`. `agc_static_buffer` and `agc_write_tracking` fail the same way without the PR on this tree and do not involve `Resources.cpp`. I also checked the premise. MoltenVK reports `vkGetBufferMemoryRequirements().size` = 100 for the 100-byte buffer: I added a temporary print in `BindExactBuffer`, then removed it. So word 28 (byte 112) lies past the memory range bound to the buffer. Under `robustBufferAccess`, MoltenVK should not return the earlier user's bytes there. The read is a driver deviation, not behaviour the spec allows, and clearing the tail is a fair workaround. ### Findings 1. **PR body, `Tested` and `GPUs tested`: [CONTRIBUTING.md:68](https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md) is not met.** An AGC change needs a run with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1` on `main` and on the change, a statement that the layer loaded and that the change adds no new `VUID-`, and vendor, model, driver and OS on the `GPUs tested` line. The body has none of the first three, and its GPU line lacks the OS. Fix: add your own validation run, or cite this one or Gonic0's, and write the line as e.g. `Apple M2 Pro, MoltenVK 1.4.2, macOS 26`. As MarcZX10 asked, refresh onto current `main` before that run. The merge base is `a3987a1ec`, and `Resources.cpp` has not changed on `main` since. 2. **Suggestion, not blocking, `Resources.cpp:9, 18, 66, 110`: key the clear on the driver rather than on `__APPLE__`.** The deviation belongs to MoltenVK, not to the platform. On this Mac, `vulkaninfo` also lists KosmicKrisp, with `robustBufferAccess2 = true`, and the `#ifdef` runs the memset there for nothing. A check of `VkPhysicalDeviceDriverProperties::driverID == VK_DRIVER_ID_MOLTENVK` (or of the device lacking `robustBufferAccess2`, which `VulkanDevice.cpp:2086` already computes but `Graphics::Context` does not carry) would be exact and would drop the `#ifdef`. The test's own fixture fills `Context` itself, so it would need to set the flag. 3. **Nit, `Resources.cpp:9-12, 23`:** `<cstddef>` is included only under `__APPLE__`, but `std::byte` is used unconditionally in `Buffer::Bytes()`. There is also a blank line before the `#endif` at line 24 and none after it. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).
maximilianfv commented 2026-10-26 21:54:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-26T21:54:56Z.

Review: APPROVED


Approving at 1ac3e3561. Findings 1 (body) and 3 (<cstddef>) from my earlier review are done. The body now states that the validation layer loaded and that the change adds no VUID-, and the GPU line gives the OS. The macOS-only <cstddef> is gone. You asked about finding 2 (the gate), and I answer it in note 2 below. Apart from the main merge, dropping <cstddef> is the only change since 8a9a741b6. My A/B at this head confirms the fix both ways. Current main (185596faff) does not fix the bug: its Resources.cpp is the same blob as this PR's base (ea37fed7ef). The notes below do not block the merge.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at 1ac3e3561 to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. The only commit to touch Resources.cpp on main since that base (4bee82086f) adds an include and a DestroyDeferredAtIdle call in CommandBatch::Submit(). Both are unrelated to the Buffer constructor, and BufferPool and the test have not changed. Both states were rebuilt before their runs. MoltenVK from the Vulkan SDK 1.4.363.0, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The layer loaded in every run (no was not found but was requested line).

agc_driver_pooled_buffer_range agc_static_buffer agc_write_tracking Validation Error lines per run
Without this PR fails 23 of 23: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory" bus error 3 of 3 (AGC driver memory at 0xfe0000000 is held by Rosetta) fails 3 of 3: "a block the import window did not touch reads as written" 1
With this PR passes 23 of 23 same bus error 3 of 3 same failure 3 of 3 1

On each side, I ran all three tests 3 times and agc_driver_pooled_buffer_range alone 20 more times. The one validation error on both sides is VUID-VkDeviceCreateInfo-pProperties-04451. It comes from the test fixture, which creates its own device without enabling VK_KHR_portability_subset (#10920). The PR adds no VUID-. agc_static_buffer and agc_write_tracking fail the same way without the PR and do not involve Resources.cpp.

Notes (non-blocking)

  1. Tested: "reported no VUID messages". With the same SDK, the layer reports this 04451 error once per run of this test on both sides for me (M1 Pro). Gonic0 counted 81 of them across 89 tests on an M5 Pro, even with the MoltenVK ICD marked non-portability. Please recheck how your logs were counted (grep -c 'Validation Error'). If the count is not 0, write "the same validation errors on both sides, none new". CONTRIBUTING's "no new VUID-" requirement is met either way, by my run and Gonic0's.

  2. Resources.cpp:9, 17, 65, 109: __APPLE__ compared with a check on the driver. You asked whether I'd prefer the runtime check, so here is my answer. Yes, but as a follow-up, not on this PR. main has no __APPLE__ block in Resources.cpp or anywhere under Graphics/src. It keys MoltenVK workarounds on driverID == VK_DRIVER_ID_MOLTENVK at runtime (lines on current main: VulkanDevice.cpp:1296, 1300, Float32FlushControl.hpp:15, FragmentBarycentric.hpp:9, TessellationPipelineCache.hpp:9). It also keys some on robustBufferAccess2. A flag on Graphics::Context set from one of those would match main and would cover KosmicKrisp. KosmicKrisp is also listed on this Mac and reports robustBufferAccess2 = true, so it does not need the clear.

  3. Resources.cpp:66: std::min<VkDeviceSize>(capacity, allocationBytes) is always capacity. ClassAllocationBytes never returns less than capacity, and slab allocations set allocationBytes = capacity (line 105). For a non-slab allocation, the mapping starts at offset 0 and covers the whole allocation (line 118). So allocationBytes is the exact bound of the clear. Passing it would remove the std::min and also clear the alignment padding past capacity. I found no way a different-sized earlier user leaves bytes in that padding, so this is a simplification, not a bug.

  4. Scope of the workaround. The clear covers host-visible, host-coherent pooled buffers that are mapped. It does not cover:

    • pooled Buffers in device-local memory, which have no mapping;
    • DeviceBuffer;
    • a fresh non-slab allocation (line 118). Vulkan does not promise zeroed memory there. I have not tested whether MoltenVK returns stale bytes in that case;
    • a read that goes past capacity.

    My probe on the earlier head showed that MoltenVK binds 100 bytes for this buffer but serves a read at byte 112. So the read is not limited to the bound range. If it is only limited by the VkDeviceMemory, a read past the slot in a 2 MiB slab block would return a live neighbour's bytes. I have not tested that. A line in docs/dev/debt/libSceAgcDriver.md would record what this change leaves open, next to the other MoltenVK entries there.

AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code with two independent reviews and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5659641719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11303#pullrequestreview-5659641719). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-26T21:54:56Z. **Review:** APPROVED --- Approving at `1ac3e3561`. Findings 1 (body) and 3 (`<cstddef>`) from my earlier review are done. The body now states that the validation layer loaded and that the change adds no `VUID-`, and the GPU line gives the OS. The macOS-only `<cstddef>` is gone. You asked about finding 2 (the gate), and I answer it in note 2 below. Apart from the `main` merge, dropping `<cstddef>` is the only change since `8a9a741b6`. My A/B at this head confirms the fix both ways. Current `main` (`185596faff`) does not fix the bug: its `Resources.cpp` is the same blob as this PR's base (`ea37fed7ef`). The notes below do not block the merge. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `1ac3e3561` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. The only commit to touch `Resources.cpp` on `main` since that base (`4bee82086f`) adds an include and a `DestroyDeferredAtIdle` call in `CommandBatch::Submit()`. Both are unrelated to the `Buffer` constructor, and `BufferPool` and the test have not changed. Both states were rebuilt before their runs. MoltenVK from the Vulkan SDK 1.4.363.0, with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. The layer loaded in every run (no `was not found but was requested` line). | | `agc_driver_pooled_buffer_range` | `agc_static_buffer` | `agc_write_tracking` | `Validation Error` lines per run | |---|---|---|---|---| | Without this PR | fails 23 of 23: "a robust read of word 28 of a 100-byte pooled buffer returned 0xabababab, the bytes a 256-byte earlier user left in its memory" | bus error 3 of 3 (AGC driver memory at `0xfe0000000` is held by Rosetta) | fails 3 of 3: "a block the import window did not touch reads as written" | 1 | | With this PR | passes 23 of 23 | same bus error 3 of 3 | same failure 3 of 3 | 1 | On each side, I ran all three tests 3 times and `agc_driver_pooled_buffer_range` alone 20 more times. The one validation error on both sides is `VUID-VkDeviceCreateInfo-pProperties-04451`. It comes from the test fixture, which creates its own device without enabling `VK_KHR_portability_subset` (#10920). The PR adds no `VUID-`. `agc_static_buffer` and `agc_write_tracking` fail the same way without the PR and do not involve `Resources.cpp`. ### Notes (non-blocking) 1. **`Tested`: "reported no `VUID` messages".** With the same SDK, the layer reports this 04451 error once per run of this test on both sides for me (M1 Pro). Gonic0 counted 81 of them across 89 tests on an M5 Pro, even with the MoltenVK ICD marked non-portability. Please recheck how your logs were counted (`grep -c 'Validation Error'`). If the count is not 0, write "the same validation errors on both sides, none new". CONTRIBUTING's "no new `VUID-`" requirement is met either way, by my run and Gonic0's. 2. **`Resources.cpp:9, 17, 65, 109`: `__APPLE__` compared with a check on the driver.** You asked whether I'd prefer the runtime check, so here is my answer. Yes, but as a follow-up, not on this PR. `main` has no `__APPLE__` block in `Resources.cpp` or anywhere under `Graphics/src`. It keys MoltenVK workarounds on `driverID == VK_DRIVER_ID_MOLTENVK` at runtime (lines on current `main`: `VulkanDevice.cpp:1296, 1300`, `Float32FlushControl.hpp:15`, `FragmentBarycentric.hpp:9`, `TessellationPipelineCache.hpp:9`). It also keys some on `robustBufferAccess2`. A flag on `Graphics::Context` set from one of those would match `main` and would cover KosmicKrisp. KosmicKrisp is also listed on this Mac and reports `robustBufferAccess2 = true`, so it does not need the clear. 3. **`Resources.cpp:66`: `std::min<VkDeviceSize>(capacity, allocationBytes)` is always `capacity`.** `ClassAllocationBytes` never returns less than `capacity`, and slab allocations set `allocationBytes = capacity` (line 105). For a non-slab allocation, the mapping starts at offset 0 and covers the whole allocation (line 118). So `allocationBytes` is the exact bound of the clear. Passing it would remove the `std::min` and also clear the alignment padding past `capacity`. I found no way a different-sized earlier user leaves bytes in that padding, so this is a simplification, not a bug. 4. **Scope of the workaround.** The clear covers host-visible, host-coherent pooled buffers that are mapped. It does not cover: - pooled `Buffer`s in device-local memory, which have no mapping; - `DeviceBuffer`; - a fresh non-slab allocation (line 118). Vulkan does not promise zeroed memory there. I have not tested whether MoltenVK returns stale bytes in that case; - a read that goes past `capacity`. My probe on the earlier head showed that MoltenVK binds 100 bytes for this buffer but serves a read at byte 112. So the read is not limited to the bound range. If it is only limited by the `VkDeviceMemory`, a read past the slot in a 2 MiB slab block would return a live neighbour's bytes. I have not tested that. A line in `docs/dev/debt/libSceAgcDriver.md` would record what this change leaves open, next to the other MoltenVK entries there. AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code with two independent reviews and wrote this review).
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!11303
No description provided.