fix(libkernel): map shared memory and files executable on Windows #12339

Merged
medazizktata merged 1 commit from upstream-pr/12339 into main 2026-10-28 18:28:16 +00:00
medazizktata commented 2026-10-28 10:16:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by medazizktata on 2026-10-28T10:16:11Z.


What

On Windows, a guest mmap with PROT_EXEC and MAP_SHARED of a shared-memory object or a regular file threw "an executable shared file mapping is not implemented on Windows" (#12337). FreeBSD 11.4 maps these calls, and so do the Linux and macOS branches of DoMapFileBacked_nid_no_patch:

  • shared memory: uipc_shm.c 878-879 gives execute to any descriptor opened for reading;
  • regular files: vfs_vnops.c vn_mmap allows execute unless the file system is mounted noexec.

Changes:

  • DirectMemory.cpp, Windows branch:
    • When execute is requested, the section is created on a second handle that ReOpenFile opens with GENERIC_READ | GENERIC_EXECUTE, plus GENERIC_WRITE for a read-write descriptor. Its protection is PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE, as CreateFileMapping requires. The extra handle is closed once the section exists. Mappings without execute take the existing path.
    • A shared writable mapping of a read-only descriptor fails with EACCES, as shm_mmap does (885-887). Before, shared-memory descriptors skipped the access check in PosixMemory.cpp.
  • SharedMemory.cpp: the owner ACE of named objects grants GX with GR, so a readable named object can be reopened with execute.
  • Tests:
    • New ctest guest_mmap_executable (guest_memory_tests executable-mappings, Windows and Linux). It runs shared protections 0-7 against a regular file opened read-write, read-only and write-only, an anonymous shm object, a named shm object opened read-write and read-only, and a JIT descriptor. For each it runs code from executable mappings, writes through writable ones, and checks the errno of refusals.
    • The executable block of GuestShmOpen.cpp now runs on Windows. Its last line (a pwrite past the end of an empty object returning 0) stays off Windows: it fails there independently of this change.
  • docs/dev/debt/libkernel.md: the Windows "no PROT_EXEC" gap is removed. What still differs is stated:
    • named objects created by earlier builds lack execute until fchmod;
    • write-only descriptors still differ from FreeBSD;
    • a file whose ACL denies execute fails with EACCES where FreeBSD maps it.

Tested

At this head (dbd826bc8, on main 3a40945e6), details:

tests only (no fix) with this change
Windows 11 Pro 10.0.26200, i5-12600KF, WinLibs GCC 15.2.0 r7, Release: guest_shm_open, guest_mmap_executable both fail 3/3 (0xc0000409) both pass
Windows: ctest -E agc — (main: 1685/1685) 1686/1686, the only difference is the new guest_mmap_executable
Pop!_OS 24.04, GCC 13.3, Release: guest_shm_open, guest_mmap_executable not run pass 5/5
Pop!_OS: ctest -E agc not run 1591/1605 at -j16; the 14 failures (12 of them timeouts) all pass rerun one at a time

Earlier, on commit 46fe6f24374a:

tests only (no fix) with this change
Windows: guest_shm_open, guest_mmap_executable both fail (0xc0000409) both pass
Windows: ctest -E "^agc" 1576/1578, the two target tests fail 1578/1578
Pop!_OS: ctest -E "^agc" 1500/1500 1500/1500
  • Linux: the tests pass without the change because only Windows code changes. To show they can fail there, a deliberate refusal added to the Linux branch makes both fail, and restoring it passes.
  • Windows deliberate breaks:
    • removing the ReOpenFile path makes both tests fail;
    • removing the EACCES check makes guest_mmap_executable fail on the errno check;
    • restoring the file byte-for-byte passes again.
  • Build logs show each changed file recompiled in every run.
  • tools/check_conventions.py --base origin/main: 0 errors. Two debt-length warnings, on entries that were already over 1000 characters on main (1752 and 2861; now 1843 and 3214).

Not tested:

  • what a PS5 does for these mappings (no console measurement exists);
  • macOS (not built);
  • a file whose ACL denies execute;
  • closing the guest descriptor while an executable view is mapped;
  • write-watch or GPU aliases over executable file views.

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: #12296 (#12296 lands first, the order its author proposes; this PR is then rebased on it and drops its now-redundant EACCES check in DoMapFileBacked_nid_no_patch)
  • Fixes: Fixes #12337
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5048786028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12339). Originally posted by [medazizktata](https://github.com/medazizktata) on 2026-10-28T10:16:11Z. --- ### What On Windows, a guest `mmap` with `PROT_EXEC` and `MAP_SHARED` of a shared-memory object or a regular file threw "an executable shared file mapping is not implemented on Windows" (#12337). FreeBSD 11.4 maps these calls, and so do the Linux and macOS branches of `DoMapFileBacked_nid_no_patch`: - shared memory: [`uipc_shm.c` 878-879](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_shm.c#L878-L879) gives execute to any descriptor opened for reading; - regular files: `vfs_vnops.c` `vn_mmap` allows execute unless the file system is mounted `noexec`. Changes: - `DirectMemory.cpp`, Windows branch: - When execute is requested, the section is created on a second handle that `ReOpenFile` opens with `GENERIC_READ | GENERIC_EXECUTE`, plus `GENERIC_WRITE` for a read-write descriptor. Its protection is `PAGE_EXECUTE_READ` or `PAGE_EXECUTE_READWRITE`, as [CreateFileMapping](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga) requires. The extra handle is closed once the section exists. Mappings without execute take the existing path. - A shared writable mapping of a read-only descriptor fails with `EACCES`, as `shm_mmap` does ([885-887](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_shm.c#L885-L887)). Before, shared-memory descriptors skipped the access check in `PosixMemory.cpp`. - `SharedMemory.cpp`: the owner ACE of named objects grants `GX` with `GR`, so a readable named object can be reopened with execute. - Tests: - New ctest `guest_mmap_executable` (`guest_memory_tests executable-mappings`, Windows and Linux). It runs shared protections 0-7 against a regular file opened read-write, read-only and write-only, an anonymous shm object, a named shm object opened read-write and read-only, and a JIT descriptor. For each it runs code from executable mappings, writes through writable ones, and checks the errno of refusals. - The executable block of `GuestShmOpen.cpp` now runs on Windows. Its last line (a `pwrite` past the end of an empty object returning 0) stays off Windows: it fails there independently of this change. - `docs/dev/debt/libkernel.md`: the Windows "no `PROT_EXEC`" gap is removed. What still differs is stated: - named objects created by earlier builds lack execute until `fchmod`; - write-only descriptors still differ from FreeBSD; - a file whose ACL denies execute fails with `EACCES` where FreeBSD maps it. ### Tested At this head (`dbd826bc8`, on `main` `3a40945e6`), [details](https://github.com/boykopovar/AnyPS5/pull/12339#issuecomment-6471064695): | | tests only (no fix) | with this change | |---|---|---| | Windows 11 Pro 10.0.26200, i5-12600KF, WinLibs GCC 15.2.0 r7, Release: `guest_shm_open`, `guest_mmap_executable` | both fail 3/3 (`0xc0000409`) | both pass | | Windows: `ctest -E agc` | — (`main`: 1685/1685) | 1686/1686, the only difference is the new `guest_mmap_executable` | | Pop!_OS 24.04, GCC 13.3, Release: `guest_shm_open`, `guest_mmap_executable` | not run | pass 5/5 | | Pop!_OS: `ctest -E agc` | not run | 1591/1605 at `-j16`; the 14 failures (12 of them timeouts) all pass rerun one at a time | Earlier, on commit `46fe6f24374a`: | | tests only (no fix) | with this change | |---|---|---| | Windows: `guest_shm_open`, `guest_mmap_executable` | both fail (`0xc0000409`) | both pass | | Windows: `ctest -E "^agc"` | 1576/1578, the two target tests fail | 1578/1578 | | Pop!_OS: `ctest -E "^agc"` | 1500/1500 | 1500/1500 | - **Linux:** the tests pass without the change because only Windows code changes. To show they can fail there, a deliberate refusal added to the Linux branch makes both fail, and restoring it passes. - **Windows deliberate breaks:** - removing the `ReOpenFile` path makes both tests fail; - removing the `EACCES` check makes `guest_mmap_executable` fail on the errno check; - restoring the file byte-for-byte passes again. - **Build logs** show each changed file recompiled in every run. - **`tools/check_conventions.py --base origin/main`:** 0 errors. Two `debt-length` warnings, on entries that were already over 1000 characters on `main` (1752 and 2861; now 1843 and 3214). **Not tested:** - what a PS5 does for these mappings (no console measurement exists); - macOS (not built); - a file whose ACL denies execute; - closing the guest descriptor while an executable view is mapped; - write-watch or GPU aliases over executable file views. ### 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: #12296 (#12296 lands first, the order its author proposes; this PR is then rebased on it and drops its now-redundant `EACCES` check in `DoMapFileBacked_nid_no_patch`) - [x] Fixes: Fixes #12337 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-28 11:20:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T11:20:44Z.

Review: COMMENTED


I reviewed the Windows section-creation and view-protection path, including the ReOpenFile access mask, named-object owner ACE, shared read-only write rejection, and the new protection/access matrix. I do not see a defect in those paths in this diff. The Windows and Linux test results in the description are from 46fe6f24374a, while the submitted head is dbd826bc88a03afc62cd9df46881fa06ea1d6be2; the description explicitly says the current-main application was not rerun. Please run at least guest_shm_open and guest_mmap_executable on Windows at this exact head (and the focused executable-mapping test on Linux if available), then update Tested. I have not approved pending that refreshed evidence; hosted checks currently cover conventions/intake/labels only.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681794540 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12339#pullrequestreview-5681794540). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T11:20:44Z. **Review:** COMMENTED --- I reviewed the Windows section-creation and view-protection path, including the `ReOpenFile` access mask, named-object owner ACE, shared read-only write rejection, and the new protection/access matrix. I do not see a defect in those paths in this diff. The Windows and Linux test results in the description are from `46fe6f24374a`, while the submitted head is `dbd826bc88a03afc62cd9df46881fa06ea1d6be2`; the description explicitly says the current-main application was not rerun. Please run at least `guest_shm_open` and `guest_mmap_executable` on Windows at this exact head (and the focused executable-mapping test on Linux if available), then update `Tested`. I have not approved pending that refreshed evidence; hosted checks currently cover conventions/intake/labels only.
MarcZX10 commented 2026-10-28 11:39:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T11:39:17Z.

Review: COMMENTED


Correction to my review comment: I overlooked the author’s October 28 10:47 follow-up in this thread. It reports a Windows run on the exact current head dbd826bc8 (Windows 11, WinLibs GCC 15.2): guest_shm_open passes, guest_mmap_executable passes, and ctest -E agc passes 1686/1686; the control with only the PR tests also reproduces the failures on main. My statement that these results were only from the earlier 46fe6f commit was incorrect. The exact-head Windows test-evidence request is satisfied. I have not yet cleared the separate coordination point about the overlapping Windows-memory debt edits in #12296.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681999538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12339#pullrequestreview-5681999538). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T11:39:17Z. **Review:** COMMENTED --- Correction to my review comment: I overlooked the author’s October 28 10:47 follow-up in this thread. It reports a Windows run on the exact current head `dbd826bc8` (Windows 11, WinLibs GCC 15.2): `guest_shm_open` passes, `guest_mmap_executable` passes, and `ctest -E agc` passes 1686/1686; the control with only the PR tests also reproduces the failures on main. My statement that these results were only from the earlier `46fe6f` commit was incorrect. The exact-head Windows test-evidence request is satisfied. I have not yet cleared the separate coordination point about the overlapping Windows-memory debt edits in #12296.
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!12339
No description provided.