libkernel (Windows): mmap with PROT_EXEC of a shared-memory object or regular file fails #12337

Closed
opened 2026-10-28 10:09:33 +00:00 by medazizktata · 0 comments
medazizktata commented 2026-10-28 10:09:33 +00:00 (Migrated from github.com)
Contributor

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


What happens

On Windows hosts, a guest mmap with PROT_EXEC and MAP_SHARED of a shared-memory object (anonymous or named) or of a regular file fails. The Windows branch of DoMapFileBacked_nid_no_patch throws on purpose (core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1196 at 46fe6f243):

map file: an executable shared file mapping is not implemented on Windows

mmap_nid_postfix reports it as EOPNOTSUPP (the ENOTSUP catch in PosixMemory.cpp). The Linux and macOS branches of the same function map these calls, and FreeBSD 11.4 allows them:

The check in core/libs/tests/GuestShmOpen.cpp that runs code from such a mapping is compiled only off Windows. #7974, which added it, describes Windows as failing "by design". Was that meant as a policy, or as the current gap?

The cause, on the Windows side: an executable view needs a section created with PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE. That in turn needs a file handle with GENERIC_EXECUTE (CreateFileMapping: "must be created with the GENERIC_READ and GENERIC_EXECUTE access rights"). Descriptor handles do not have it, and the owner ACE of named objects (SharedMemory.cpp:286, GRWD) does not grant it either.

A related case: on Windows, a shared writable mapping of a shared-memory descriptor opened read-only does not fail with EACCES, as FreeBSD's shm_mmap does (lines 885-887). The access check in PosixMemory.cpp:208-215 is applied only to descriptors that are not shared memory.

Title, test or build step

guest_shm_open with its executable block enabled on Windows (homebrew or test code; no commercial title known to hit this).

AnyPS5 commit

46fe6f243

System

Windows 11 Pro 10.0.26200, Intel Core i5-12600KF, NVIDIA RTX 4060 (driver 610.88), WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

Steps to reproduce

  1. In core/libs/tests/GuestShmOpen.cpp, remove the #ifndef _WIN32 / #endif around the block that writes mov eax, 42; ret into the anonymous object and maps it with protection 5. The last line of the block (pwrite to third returning 0) checks a different behaviour and fails on Windows independently, so keep that one guarded.
  2. cmake --build build --target guest_shm_open_tests
  3. ctest --test-dir build -R "^guest_shm_open$" --output-on-failure

Log

1/1 Test #2383: guest_shm_open ...................Exit code 0xc0000409***Exception:   0.08 sec
FATAL: abort() (SIGABRT) on thread 2448

Fails 3 of 3 runs. With Require changed to print the failing line, it stops at Require(executable != reinterpret_cast<void*>(static_cast<std::uintptr_t>(-1))), right after the mmap.

What was checked

  • I have a local fix and can open a PR if this direction is wanted:
    • reopen the descriptor's file with GENERIC_EXECUTE (ReOpenFile) only when execute is requested, create the section as PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE, and close the extra handle once the section exists;
    • grant GX alongside GR in the named-object owner ACE;
    • return EACCES for the read-only case above.
  • With it, on the same commit: a matrix test (shared protections 0-7 x regular file read-write/read-only/write-only, anonymous shm, named shm read-write/read-only, JIT) fails without the change and passes with it on Windows. The full ctest -E "^agc" run (1578 tests) differs only in the two target tests.
  • On Linux (Pop!_OS 24.04, GCC 13.3) the full run is unchanged with the fix (1499 of 1499 pass). The change only touches Windows code.
  • Not verified: what a PS5 does for these mappings; there is no console measurement in the repo. macOS was not built.
  • AI-assisted: yes (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6076807049 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/12337). Originally posted by [medazizktata](https://github.com/medazizktata) on 2026-10-28T10:09:33Z. --- ### What happens On Windows hosts, a guest `mmap` with `PROT_EXEC` and `MAP_SHARED` of a shared-memory object (anonymous or named) or of a regular file fails. The Windows branch of `DoMapFileBacked_nid_no_patch` throws on purpose (`core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1196` at `46fe6f243`): ``` map file: an executable shared file mapping is not implemented on Windows ``` `mmap_nid_postfix` reports it as `EOPNOTSUPP` (the `ENOTSUP` catch in `PosixMemory.cpp`). The Linux and macOS branches of the same function map these calls, and FreeBSD 11.4 allows them: - shared memory: [`uipc_shm.c` `shm_mmap`, lines 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 (`maxprot |= VM_PROT_EXECUTE | VM_PROT_READ`); - regular files: [`vfs_vnops.c` `vn_mmap`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_vnops.c) allows execute unless the file system is mounted `noexec`. The check in `core/libs/tests/GuestShmOpen.cpp` that runs code from such a mapping is compiled only off Windows. #7974, which added it, describes Windows as failing "by design". Was that meant as a policy, or as the current gap? The cause, on the Windows side: an executable view needs a section created with `PAGE_EXECUTE_READ` or `PAGE_EXECUTE_READWRITE`. That in turn needs a file handle with `GENERIC_EXECUTE` ([CreateFileMapping](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga): "must be created with the GENERIC_READ and GENERIC_EXECUTE access rights"). Descriptor handles do not have it, and the owner ACE of named objects (`SharedMemory.cpp:286`, `GRWD`) does not grant it either. A related case: on Windows, a shared writable mapping of a shared-memory descriptor opened read-only does not fail with `EACCES`, as FreeBSD's `shm_mmap` does ([lines 885-887](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_shm.c#L885-L887)). The access check in `PosixMemory.cpp:208-215` is applied only to descriptors that are not shared memory. ### Title, test or build step `guest_shm_open` with its executable block enabled on Windows (homebrew or test code; no commercial title known to hit this). ### AnyPS5 commit `46fe6f243` ### System Windows 11 Pro 10.0.26200, Intel Core i5-12600KF, NVIDIA RTX 4060 (driver 610.88), WinLibs GCC 15.2.0 r7 (`x86_64-ucrt-posix-seh`), Release, `BUILD_TESTING=ON`. ### Steps to reproduce 1. In `core/libs/tests/GuestShmOpen.cpp`, remove the `#ifndef _WIN32` / `#endif` around the block that writes `mov eax, 42; ret` into the anonymous object and maps it with protection 5. The last line of the block (`pwrite` to `third` returning 0) checks a different behaviour and fails on Windows independently, so keep that one guarded. 2. `cmake --build build --target guest_shm_open_tests` 3. `ctest --test-dir build -R "^guest_shm_open$" --output-on-failure` ### Log ``` 1/1 Test #2383: guest_shm_open ...................Exit code 0xc0000409***Exception: 0.08 sec FATAL: abort() (SIGABRT) on thread 2448 ``` Fails 3 of 3 runs. With `Require` changed to print the failing line, it stops at `Require(executable != reinterpret_cast<void*>(static_cast<std::uintptr_t>(-1)))`, right after the `mmap`. ### What was checked - I have a local fix and can open a PR if this direction is wanted: - reopen the descriptor's file with `GENERIC_EXECUTE` (`ReOpenFile`) only when execute is requested, create the section as `PAGE_EXECUTE_READ` or `PAGE_EXECUTE_READWRITE`, and close the extra handle once the section exists; - grant `GX` alongside `GR` in the named-object owner ACE; - return `EACCES` for the read-only case above. - With it, on the same commit: a matrix test (shared protections 0-7 x regular file read-write/read-only/write-only, anonymous shm, named shm read-write/read-only, JIT) fails without the change and passes with it on Windows. The full `ctest -E "^agc"` run (1578 tests) differs only in the two target tests. - On Linux (Pop!_OS 24.04, GCC 13.3) the full run is unchanged with the fix (1499 of 1499 pass). The change only touches Windows code. - Not verified: what a PS5 does for these mappings; there is no console measurement in the repo. macOS was not built. - AI-assisted: yes (Claude Code).
Sign in to join this conversation.
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#12337
No description provided.