fix(agc): check unregistered host memory with mach_vm_region on macOS, which has no /proc/self/maps #20057

Merged
XIIYAZ merged 1 commit from upstream-pr/20057 into main 2026-11-22 01:15:29 +00:00
XIIYAZ commented 2026-11-14 14:12:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T14:12:36Z.


What

On macOS, a label stored into unregistered host memory is always refused with reason 3. That makes agc_driver_unregistered_label_on_gpu and agc_driver_unregistered_label_behind_write_back fail:

unregistered label: a label in unregistered host memory returned reason 3 instead of 0
wide store without a write-back: a 16-byte store into unregistered memory ... was handled with reason 3, expected 0

UnregisteredHostImport imports such memory only when privateAnonymousReadWrite confirms the range is private, anonymous, read-write memory. Outside Windows, that check reads /proc/self/maps. macOS has no /proc, so fopen fails and every range counts as not private.

On macOS the check now asks mach_vm_region about the region that contains the range, and requires all of the following:

  • the region covers the whole range;
  • current protection is exactly VM_PROT_READ | VM_PROT_WRITE (the Linux rw- check);
  • inheritance is VM_INHERIT_COPY; a MAP_SHARED mapping has VM_INHERIT_SHARE (the Linux p check);
  • external_pager == 0, so the region is not backed by a file (the Linux inode == 0 check).

The region's share mode is not used. Once Metal imports part of a malloc region (here the registered label block beside the test's block), the whole region reports SM_TRUESHARED, while the memory is still private to the process.

What mach_vm_region reports for each case the test uses, under Rosetta on an M4:

memory protection inheritance external pager accepted
aligned_alloc block rw copy 0 yes
MAP_PRIVATE | MAP_ANON rw copy 0 yes
MAP_SHARED | MAP_ANON rw share 0 no
MAP_PRIVATE file mapping rw copy 1 no

A page made read-only with mprotect fails the protection check. The test covers that case.

Tested

Setup:

  • Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0
  • full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under Rosetta
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
  • on main with the open macOS build fixes applied

Results:

  • agc_driver_unregistered_label_on_gpu and agc_driver_unregistered_label_behind_write_back fail before this change and pass after it. That includes the cases that must still be refused: read-only, MAP_SHARED anonymous and private file mappings.
  • ctest -R 'label|write_tracking|unit_flags|shadow_label|recorder_failed|host_import|import' (95 tests): the other failures there are the same before and after. They are different issues (label ordering over the host import and write tracking) and are not changed here.
  • I ran ctest -R '^(agc|shader|job)' (1578 tests, -j 1) and compared it with the same run before this change: the two tests above are fixed and nothing new fails (18 failures before, 16 after).

Linux and Windows are unchanged; the new code is under __APPLE__.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5312133183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20057). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T14:12:36Z. --- ### What On macOS, a label stored into unregistered host memory is always refused with reason 3. That makes `agc_driver_unregistered_label_on_gpu` and `agc_driver_unregistered_label_behind_write_back` fail: ``` unregistered label: a label in unregistered host memory returned reason 3 instead of 0 wide store without a write-back: a 16-byte store into unregistered memory ... was handled with reason 3, expected 0 ``` `UnregisteredHostImport` imports such memory only when `privateAnonymousReadWrite` confirms the range is private, anonymous, read-write memory. Outside Windows, that check reads `/proc/self/maps`. macOS has no `/proc`, so `fopen` fails and every range counts as not private. On macOS the check now asks `mach_vm_region` about the region that contains the range, and requires all of the following: - the region covers the whole range; - current protection is exactly `VM_PROT_READ | VM_PROT_WRITE` (the Linux `rw-` check); - inheritance is `VM_INHERIT_COPY`; a `MAP_SHARED` mapping has `VM_INHERIT_SHARE` (the Linux `p` check); - `external_pager == 0`, so the region is not backed by a file (the Linux `inode == 0` check). The region's share mode is not used. Once Metal imports part of a malloc region (here the registered label block beside the test's block), the whole region reports `SM_TRUESHARED`, while the memory is still private to the process. What `mach_vm_region` reports for each case the test uses, under Rosetta on an M4: | memory | protection | inheritance | external pager | accepted | |---|---|---|---|---| | `aligned_alloc` block | rw | copy | 0 | yes | | `MAP_PRIVATE \| MAP_ANON` | rw | copy | 0 | yes | | `MAP_SHARED \| MAP_ANON` | rw | share | 0 | no | | `MAP_PRIVATE` file mapping | rw | copy | 1 | no | A page made read-only with `mprotect` fails the protection check. The test covers that case. ### Tested Setup: - Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0 - full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`; tests run under Rosetta - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` - on `main` with the open macOS build fixes applied Results: - `agc_driver_unregistered_label_on_gpu` and `agc_driver_unregistered_label_behind_write_back` fail before this change and pass after it. That includes the cases that must still be refused: read-only, `MAP_SHARED` anonymous and private file mappings. - `ctest -R 'label|write_tracking|unit_flags|shadow_label|recorder_failed|host_import|import'` (95 tests): the other failures there are the same before and after. They are different issues (label ordering over the host import and write tracking) and are not changed here. - I ran `ctest -R '^(agc|shader|job)'` (1578 tests, `-j 1`) and compared it with the same run before this change: the two tests above are fixed and nothing new fails (18 failures before, 16 after). Linux and Windows are unchanged; the new code is under `__APPLE__`. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-14 15:39:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T15:39:14Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected macOS range checks match the stated private, anonymous, read-write requirements.

What I ran

Reviewed head 924f53b864d233bf1e2912191c2ede783b60f872. No build or tests were run. Read review/description.md, review/commits.md, review/manifest.json, review/diff.patch, and lines 1–90, 4920–5070 of core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp through repo_read.py.

Results

Check Result Note
Diff and changed code passed One changed source file inspected
Build and tests not run Author reports macOS and broader test runs

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full 7,920-line changed file was not read; only the changed regions, surrounding includes, and caller were inspected. Build and test results in the description are author-reported, not independently verified. The diff is the complete change; no other changed files were listed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5905476010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20057#pullrequestreview-5905476010). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T15:39:14Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected macOS range checks match the stated private, anonymous, read-write requirements. ## What I ran Reviewed head `924f53b864d233bf1e2912191c2ede783b60f872`. No build or tests were run. Read `review/description.md`, `review/commits.md`, `review/manifest.json`, `review/diff.patch`, and lines 1–90, 4920–5070 of `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` through `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | One changed source file inspected | | Build and tests | not run | Author reports macOS and broader test runs | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full 7,920-line changed file was not read; only the changed regions, surrounding includes, and caller were inspected. Build and test results in the description are author-reported, not independently verified. The diff is the complete change; no other changed files were listed. ## To get this to a good state Nothing is required.
kurexw commented 2026-11-16 23:01:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-16T23:01:50Z.

Review: COMMENTED


Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested.

Head 924f53b864 merged alone onto main 3b17977ba6, compared with 3b17977ba6 itself. I ran the 29 AGC tests that fail on main on this Mac, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer, through ctest --test-dir build -R <those 29> -V, three runs per side. Every Vulkan test logged Insert instance layer "VK_LAYER_KHRONOS_validation" and none logged was not found but was requested by env var VK_INSTANCE_LAYERS.

test main (3 runs) this PR (3 runs)
agc_driver_unregistered_label_behind_write_back Failed, Failed, Failed Passed, Passed, Passed
agc_driver_unregistered_label_on_gpu Failed, Failed, Failed Passed, Passed, Passed

The other 26 tests gave the same result on both sides.

agc_driver_label_after_import_copy is flaky on main as well: main Failed, Passed, Passed, this PR Failed, Failed, Failed. Every failure on either side is the same timing check ("round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied"), and it varied the same way across the other PRs I tested in the same session, so I don't attribute it to this change.

VUID- lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The VUID- lines on both sides are main's existing VUID-VkDeviceCreateInfo-sType-unique (a duplicated VkPhysicalDevicePortabilitySubsetFeaturesKHR in the device pNext chain) and VUID-VkSamplerCreateInfo-samplerMipLodBias-04467; this PR adds none.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5929447977 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20057#pullrequestreview-5929447977). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-16T23:01:50Z. **Review:** COMMENTED --- Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested. Head `924f53b864` merged alone onto main `3b17977ba6`, compared with `3b17977ba6` itself. I ran the 29 AGC tests that fail on main on this Mac, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer`, through `ctest --test-dir build -R <those 29> -V`, three runs per side. Every Vulkan test logged `Insert instance layer "VK_LAYER_KHRONOS_validation"` and none logged `was not found but was requested by env var VK_INSTANCE_LAYERS`. | test | main (3 runs) | this PR (3 runs) | | --- | --- | --- | | `agc_driver_unregistered_label_behind_write_back` | Failed, Failed, Failed | Passed, Passed, Passed | | `agc_driver_unregistered_label_on_gpu` | Failed, Failed, Failed | Passed, Passed, Passed | The other 26 tests gave the same result on both sides. `agc_driver_label_after_import_copy` is flaky on main as well: main Failed, Passed, Passed, this PR Failed, Failed, Failed. Every failure on either side is the same timing check ("round N: a CPU read after the label that followed the copy found K of 262144 destination bytes not yet copied"), and it varied the same way across the other PRs I tested in the same session, so I don't attribute it to this change. `VUID-` lines: identical per test on both sides (30 on main, 30 with this PR, over the three runs). The `VUID-` lines on both sides are main's existing `VUID-VkDeviceCreateInfo-sType-unique` (a duplicated `VkPhysicalDevicePortabilitySubsetFeaturesKHR` in the device pNext chain) and `VUID-VkSamplerCreateInfo-samplerMipLodBias-04467`; this PR adds none.
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!20057
No description provided.