fix(libkernel): create the JIT shared memory backing on macOS hosts #6928

Closed
jotitadev wants to merge 1 commit from upstream-pr/6928 into main
jotitadev commented 2026-10-16 19:29:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T19:29:23Z.


What

libkernel/DirectMemory/Jit.cpp creates the JIT shared memory backing with memfd_create(..., MFD_CLOEXEC) on every non-Windows host (#3787), so the file does not compile on macOS, which has neither. On macOS the backing is now an unlinked mkostemp file in the temporary directory, opened with O_CLOEXEC; the sizing, aliasing and mapping code is shared with Linux as before. Linux and Windows are unchanged: on Linux the only difference after preprocessing is the <filesystem> include, which moved out of the Windows block, and Windows includes the same headers.

A file and not a shm_open object, as @yripper found in #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6179438307): pages mapped from a shm_open object cannot be made executable, and the JIT needs executable aliases. The same unlinked temporary file is what macos-port uses for anonymous shm_open objects.

@yripper offered to review it (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6182812735).

Reported in #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6178527997); @mugurc asked for it as a separate PR (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6180063305).

Tested

macOS 27.2 on an Apple M2 Max, Xcode 27.0 (Apple clang 21.0.0, libc++). Base: main ab9a47684. I compiled the file with the libkernel command line of the full build (-std=gnu++20 -fexperimental-library -fPIC -O3, core/libs and core/libs/nid/include on the include path), for -arch x86_64 and -arch arm64:

  • main: error: use of undeclared identifier 'MFD_CLOEXEC' (line 143) on both.
  • This branch: compiles on both, no new warnings.

A standalone program with the same backing code (mkostemp + unlink + ftruncate, one shared read-write view and one shared read-only view switched to read-execute with mprotect) runs code written through the writable view, as x86-64 under Rosetta and as arm64. The descriptor has FD_CLOEXEC set. With an anonymous shm_open object the mprotect to read-execute fails with EACCES. One thing for the macOS mapping code, which is not part of this PR: on macOS 27.2 a single mmap with PROT_EXEC of this file (or of any regular file) returns EPERM, while PROT_READ followed by mprotect works.

Not run: libkernel does not link on macOS yet, so guest_jit_shared_memory does not run there. Not built on Linux or Windows; the preprocessed code for both is unchanged apart from the include move above.

Rebased onto main 1b1f8483c; the files this changes are unchanged since the tested base.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4893277280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6928). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-16T19:29:23Z. --- ### What `libkernel/DirectMemory/Jit.cpp` creates the JIT shared memory backing with `memfd_create(..., MFD_CLOEXEC)` on every non-Windows host (#3787), so the file does not compile on macOS, which has neither. On macOS the backing is now an unlinked `mkostemp` file in the temporary directory, opened with `O_CLOEXEC`; the sizing, aliasing and mapping code is shared with Linux as before. Linux and Windows are unchanged: on Linux the only difference after preprocessing is the `<filesystem>` include, which moved out of the Windows block, and Windows includes the same headers. A file and not a `shm_open` object, as @yripper found in #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6179438307): pages mapped from a `shm_open` object cannot be made executable, and the JIT needs executable aliases. The same unlinked temporary file is what `macos-port` uses for anonymous `shm_open` objects. @yripper offered to review it (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6182812735). Reported in #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6178527997); @mugurc asked for it as a separate PR (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6180063305). ### Tested macOS 27.2 on an Apple M2 Max, Xcode 27.0 (Apple clang 21.0.0, libc++). Base: `main` ab9a47684. I compiled the file with the libkernel command line of the full build (`-std=gnu++20 -fexperimental-library -fPIC -O3`, `core/libs` and `core/libs/nid/include` on the include path), for `-arch x86_64` and `-arch arm64`: - `main`: `error: use of undeclared identifier 'MFD_CLOEXEC'` (line 143) on both. - This branch: compiles on both, no new warnings. A standalone program with the same backing code (`mkostemp` + `unlink` + `ftruncate`, one shared read-write view and one shared read-only view switched to read-execute with `mprotect`) runs code written through the writable view, as x86-64 under Rosetta and as arm64. The descriptor has `FD_CLOEXEC` set. With an anonymous `shm_open` object the `mprotect` to read-execute fails with `EACCES`. One thing for the macOS mapping code, which is not part of this PR: on macOS 27.2 a single `mmap` with `PROT_EXEC` of this file (or of any regular file) returns `EPERM`, while `PROT_READ` followed by `mprotect` works. Not run: libkernel does not link on macOS yet, so `guest_jit_shared_memory` does not run there. Not built on Linux or Windows; the preprocessed code for both is unchanged apart from the include move above. Rebased onto `main` `1b1f8483c`; the files this changes are unchanged since the tested base. ### 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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-16 19:43:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T19:43:26Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7332 core/libs/prx/libkernel/DirectMemory/Jit.cpp L142-143

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 5021 core/libs/prx/libkernel/DirectMemory/Jit.cpp
PR 6222 core/libs/prx/libkernel/DirectMemory/Jit.cpp
PR 7201 core/libs/prx/libkernel/DirectMemory/Jit.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6223353512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6928#issuecomment-6223353512). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T19:43:26Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/DirectMemory/Jit.cpp` [L142-143](https://github.com/boykopovar/AnyPS5/blob/385ed71f2cfa8d67ff466d050d5e88e136d3ec28/core/libs/prx/libkernel/DirectMemory/Jit.cpp#L142-L143) | | | 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 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/Jit.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/DirectMemory/Jit.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/DirectMemory/Jit.cpp` |

Pull request closed

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!6928
No description provided.