feat(libkernel): map files through the guest's mmap on macOS hosts #7974

Merged
DavidSamir merged 4 commits from upstream-pr/7974 into main 2026-10-21 11:46:41 +00:00
DavidSamir commented 2026-10-19 16:31:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-19T16:31:11Z.


What

On macOS the guest's mmap of a file descriptor throws (DoMapFileBacked_nid_no_patch: "file-backed guest mmap is not supported on this host"), and PosixMemory.cpp accepts only shared memory objects that macOS never reports. So no regular file, anonymous shm_open object or JIT shared memory descriptor can be mapped there. Dusklight (PPSA99305) needs it for SQLite's WAL index: without it SQLite reports disk I/O error and the title's shader cache fails to start.

This takes the Linux path on macOS:

  1. feat(libkernel): check file and shared memory descriptors for the guest's mmap on macOS. PosixMemory.cpp pins the descriptor, checks its access mode against the protection and accepts a shared memory object or a regular file, as on Linux. An anonymous shm_open object on macOS is already an unlinked temporary file, so it passes as a regular file.
  2. feat(libkernel): map files through the guest's mmap on macOS, adding execute after mapping. DoMapFileBacked maps shared or private as on Linux. macOS refuses to create an executable file mapping (EPERM for PROT_READ | PROT_EXEC), so the mapping is made without execute and then given the full protection with mprotect, which macOS allows. #7937 does the same for the JIT views.
  3. test(libkernel): guest_shm_open writes mov eax, 42; ret into an anonymous object and runs it from a read-execute mapping of it (not on Windows, where an executable shared file mapping fails by design).
  4. docs(debt): a short macOS entry next to "Regular file mappings": the same checks as Linux, execute added after mapping, Rosetta keeping an old translation for code rewritten through another view (measured in #7937), and named shm_open objects still throwing.

Tested

Apple M2, macOS 26, MoltenVK 1.4.x (Vulkan SDK 1.4.363), x86-64 build under Rosetta 2, Release, on main with @mugurc's macOS series (ci/macos-series-2, which main alone needs to build on macOS), #7937 (without it libkernel does not compile on macOS) and #6836.

Test Without this PR With it
guest_shm_open (with the new executable case) aborts (exit 134) at the first mmap passes
guest_jit_shared_memory, with #7937's macOS skip of the mmap case removed fails at that case (line 110) passes
guest_msync passes passes

The other 35 libkernel/libc memory tests (ctest -R "mmap\|shm\|msync\|mprotect\|munmap\|jit\|mlock\|flexible\|direct\|virtual_query\|savedata\|mapping\|memory" -E agc) give the same result with and without this PR. guest_memory_lifetime, guest_kernel_app_archive_mmap and guest_flexible_memory_declared fail on macOS on both, so they are not caused by it.

Dusklight (PPSA99305), relinked with --macos --to-rosetta, APS5_TRACE_MEMORY=1:

  • without this PR: no map file line; SQLite version 3.51.3, then Failed to start transaction: disk I/O error, Failed to open schema check transaction, Failed to validate schema; abort after 17 s.
  • with it: map file 0x201400000+0x204000 offset=0x0 prot=0x3, two more file mappings, SQLite cache init succeeded, then the title runs to AGC graphics: device does not support mesh shaders after 22 s, which the mesh fallback in #6528 is about. Nothing past that is tested on this M2.

Not tested: Linux and Windows code is unchanged. Once #7937 and this PR are both in, the macOS skip of MmapOfTheDescriptorSharesTheObject in GuestJitSharedMemory.cpp can go; I will send that one-line follow-up.

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: #7937 (to build libkernel on macOS; the change itself applies to main alone)
  • Fixes: none
  • Unblocks: PPSA99305 on macOS (no bring-up discussion)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4924115365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7974). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-19T16:31:11Z. --- ### What On macOS the guest's `mmap` of a file descriptor throws (`DoMapFileBacked_nid_no_patch`: "file-backed guest mmap is not supported on this host"), and `PosixMemory.cpp` accepts only shared memory objects that macOS never reports. So no regular file, anonymous `shm_open` object or JIT shared memory descriptor can be mapped there. Dusklight (PPSA99305) needs it for SQLite's WAL index: without it SQLite reports `disk I/O error` and the title's shader cache fails to start. This takes the Linux path on macOS: 1. `feat(libkernel): check file and shared memory descriptors for the guest's mmap on macOS`. `PosixMemory.cpp` pins the descriptor, checks its access mode against the protection and accepts a shared memory object or a regular file, as on Linux. An anonymous `shm_open` object on macOS is already an unlinked temporary file, so it passes as a regular file. 2. `feat(libkernel): map files through the guest's mmap on macOS, adding execute after mapping`. `DoMapFileBacked` maps shared or private as on Linux. macOS refuses to create an executable file mapping (`EPERM` for `PROT_READ | PROT_EXEC`), so the mapping is made without execute and then given the full protection with `mprotect`, which macOS allows. #7937 does the same for the JIT views. 3. `test(libkernel)`: `guest_shm_open` writes `mov eax, 42; ret` into an anonymous object and runs it from a read-execute mapping of it (not on Windows, where an executable shared file mapping fails by design). 4. `docs(debt)`: a short macOS entry next to "Regular file mappings": the same checks as Linux, execute added after mapping, Rosetta keeping an old translation for code rewritten through another view (measured in #7937), and named `shm_open` objects still throwing. ### Tested Apple M2, macOS 26, MoltenVK 1.4.x (Vulkan SDK 1.4.363), x86-64 build under Rosetta 2, Release, on `main` with @mugurc's macOS series (`ci/macos-series-2`, which `main` alone needs to build on macOS), #7937 (without it libkernel does not compile on macOS) and #6836. | Test | Without this PR | With it | |---|---|---| | `guest_shm_open` (with the new executable case) | aborts (exit 134) at the first `mmap` | passes | | `guest_jit_shared_memory`, with #7937's macOS skip of the `mmap` case removed | fails at that case (line 110) | passes | | `guest_msync` | passes | passes | The other 35 libkernel/libc memory tests (`ctest -R "mmap\|shm\|msync\|mprotect\|munmap\|jit\|mlock\|flexible\|direct\|virtual_query\|savedata\|mapping\|memory" -E agc`) give the same result with and without this PR. `guest_memory_lifetime`, `guest_kernel_app_archive_mmap` and `guest_flexible_memory_declared` fail on macOS on both, so they are not caused by it. Dusklight (PPSA99305), relinked with `--macos --to-rosetta`, `APS5_TRACE_MEMORY=1`: - without this PR: no `map file` line; `SQLite version 3.51.3`, then `Failed to start transaction: disk I/O error`, `Failed to open schema check transaction`, `Failed to validate schema`; abort after 17 s. - with it: `map file 0x201400000+0x204000 offset=0x0 prot=0x3`, two more file mappings, `SQLite cache init succeeded`, then the title runs to `AGC graphics: device does not support mesh shaders` after 22 s, which the mesh fallback in #6528 is about. Nothing past that is tested on this M2. Not tested: Linux and Windows code is unchanged. Once #7937 and this PR are both in, the macOS skip of `MmapOfTheDescriptorSharesTheObject` in `GuestJitSharedMemory.cpp` can go; I will send that one-line follow-up. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #7937 (to build libkernel on macOS; the change itself applies to `main` alone) - [x] Fixes: none - [x] Unblocks: PPSA99305 on macOS (no bring-up discussion) - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-19 16:42:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T16:42:32Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4449 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestShmOpen.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 6977 docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7631 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 8344 docs/dev/debt/libkernel.md
PR 8483 docs/dev/debt/libkernel.md
PR 8520 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
docs/dev/debt/libkernel.md
PR 8553 docs/dev/debt/libkernel.md
PR 8658 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
docs/dev/debt/libkernel.md
PR 8670 docs/dev/debt/libkernel.md
PR 8726 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6272272828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7974#issuecomment-6272272828). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T16:42:32Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4449](https://www.github.com/boykopovar/AnyPS5/pull/4449) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `docs/dev/debt/libkernel.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestShmOpen.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `docs/dev/debt/libkernel.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7631](https://www.github.com/boykopovar/AnyPS5/pull/7631) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 8344](https://www.github.com/boykopovar/AnyPS5/pull/8344) | `docs/dev/debt/libkernel.md` | | [PR 8483](https://www.github.com/boykopovar/AnyPS5/pull/8483) | `docs/dev/debt/libkernel.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 8553](https://www.github.com/boykopovar/AnyPS5/pull/8553) | `docs/dev/debt/libkernel.md` | | [PR 8658](https://www.github.com/boykopovar/AnyPS5/pull/8658) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp`<br>`core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 8670](https://www.github.com/boykopovar/AnyPS5/pull/8670) | `docs/dev/debt/libkernel.md` | | [PR 8726](https://www.github.com/boykopovar/AnyPS5/pull/8726) | `docs/dev/debt/libkernel.md` |
MarcZX10 commented 2026-10-19 16:44:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T16:44:48Z.

Review: COMMENTED


I reviewed the macOS-specific path and its interaction with #7937. This PR adds the file-backed mapping path while #7937 covers the JIT shared-memory mapping; the dependency is called out in the description. The executable-protection fallback removes PROT_EXEC for the initial file mapping, applies the requested protection with mprotect, and unmaps on failure before registering the allocation. The reported M2 tests exercise the new regular-file and anonymous-object cases. I did not independently run the macOS tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5574766766 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7974#pullrequestreview-5574766766). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T16:44:48Z. **Review:** COMMENTED --- I reviewed the macOS-specific path and its interaction with #7937. This PR adds the file-backed mapping path while #7937 covers the JIT shared-memory mapping; the dependency is called out in the description. The executable-protection fallback removes `PROT_EXEC` for the initial file mapping, applies the requested protection with `mprotect`, and unmaps on failure before registering the allocation. The reported M2 tests exercise the new regular-file and anonymous-object cases. I did not independently run the macOS tests.
DavidSamir commented 2026-10-19 21:07:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-19T21:07:33Z.


Linux check of head 758e22d4f4 merged onto main bf2c63f85 (clean merge). Ubuntu 24.04 in WSL2, i7-11700F, GCC 13.3, Ninja, Release, BUILD_TESTING=ON.

  • guest_shm_open, guest_jit_shared_memory and guest_msync pass in 3 runs of 3.
  • The new guest_shm_open case is compiled into the test that ran. It writes mov eax, 42; ret into an anonymous shm_open object and calls it through a read-execute mmap, and it returns 42 on Linux. The __APPLE__ paths in DirectMemory.cpp and PosixMemory.cpp are not compiled here, so this run covers only the shared test case.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277342876 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7974#issuecomment-6277342876). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-19T21:07:33Z. --- Linux check of head `758e22d4f4` merged onto `main` `bf2c63f85` (clean merge). Ubuntu 24.04 in WSL2, i7-11700F, GCC 13.3, Ninja, Release, `BUILD_TESTING=ON`. - `guest_shm_open`, `guest_jit_shared_memory` and `guest_msync` pass in 3 runs of 3. - The new `guest_shm_open` case is compiled into the test that ran. It writes `mov eax, 42; ret` into an anonymous `shm_open` object and calls it through a read-execute `mmap`, and it returns 42 on Linux. The `__APPLE__` paths in `DirectMemory.cpp` and `PosixMemory.cpp` are not compiled here, so this run covers only the shared test case. AI-assisted: yes
adriadam10 commented 2026-10-21 11:48:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:48:19Z.


Checked the macOS path in DoMapFileBacked (map without PROT_EXEC, then mprotect, Unmap on failure before registering) and the widened descriptor checks in PosixMemory.cpp against the Linux path, the new executable case in guest_shm_open, and the debt entry. Linux and Windows behaviour is unchanged; the macOS runs are the author's (M2, Rosetta 2) and the Linux run is also theirs; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6315154627 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7974#issuecomment-6315154627). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T11:48:19Z. --- Checked the macOS path in `DoMapFileBacked` (map without `PROT_EXEC`, then `mprotect`, `Unmap` on failure before registering) and the widened descriptor checks in `PosixMemory.cpp` against the Linux path, the new executable case in `guest_shm_open`, and the debt entry. Linux and Windows behaviour is unchanged; the macOS runs are the author's (M2, Rosetta 2) and the Linux run is also theirs; merged.
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!7974
No description provided.