Skip to content

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

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
DavidSamir:feat/libkernel-file-mmap-macos
Oct 21, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
DavidSamir:feat/libkernel-file-mmap-macos

Conversation

@DavidSamir

Copy link
Copy Markdown
Contributor

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. feat(libkernel): JIT shared memory on macOS hosts #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 feat(libkernel): JIT shared memory on macOS hosts #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 AGC: run mesh-path draws on devices without mesh shaders through a compute pass and an indexed draw #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: feat(libkernel): JIT shared memory on macOS hosts #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)

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

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

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

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 adriadam10 self-assigned this Oct 21, 2026
@adriadam10
adriadam10 merged commit e335d5b into boykopovar:main Oct 21, 2026
11 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libkernel unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants