feat(libkernel): support named shared memory on Windows #5276

Closed
Nixitov wants to merge 1 commit from upstream-pr/5276 into main
Nixitov commented 2026-10-13 16:18:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-13T16:18:19Z.


What

Named shared memory was missing on Windows: shm_open/shm_unlink of a named object called NotImplemented_nid_no_patch, while the Linux path (host shm_open under /dev/shm, HostShmLock, NamedHostPath) already implements them. That kept both functions pending in the progress report and left 22 prepared titles that import shm_open (2 import shm_unlink) without named shared memory on Windows.

The host object is now a file under the temporary directory named after the SHA-1 of the guest name (the same digest the Linux path hashes), opened with CreateFileW and FILE_SHARE_DELETE so that shm_unlink while a descriptor is open removes the name as it does on the console. O_CREAT/O_EXCL/O_TRUNC select the matching CreateFileW disposition, and the host errors map to the guest's (EEXIST, ENOENT, EACCES, EINVAL, ENOSPC, EROFS, ENAMETOOLONG).

The validation and hashing move into a shared NamedHostObject (the SHA-1 helper was already portable and linked into libkernel), and NamedValidationGuestError maps the validation errors on Windows. The Linux path is otherwise unchanged.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • guest_shm_open now covers a named object — create, ftruncate, write, reopen, read back, O_EXCL on an existing name (EEXIST), unlink, reopen (ENOENT), and unlink of an absent name (ENOENT); it passes. The test previously asserted that the Windows named path threw.
  • tools/progress.py: libkernel 569 → 571 implemented (shm_open, shm_unlink); overall 88.12% → 88.17%.
  • python tools/check_conventions.py --base origin/main passes.
  • Not tested on Linux; the Linux path is unchanged apart from sharing NamedHostObject.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4846178679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-13T16:18:19Z. --- ### What Named shared memory was missing on Windows: `shm_open`/`shm_unlink` of a named object called `NotImplemented_nid_no_patch`, while the Linux path (host `shm_open` under `/dev/shm`, `HostShmLock`, `NamedHostPath`) already implements them. That kept both functions pending in the progress report and left 22 prepared titles that import `shm_open` (2 import `shm_unlink`) without named shared memory on Windows. The host object is now a file under the temporary directory named after the SHA-1 of the guest name (the same digest the Linux path hashes), opened with `CreateFileW` and `FILE_SHARE_DELETE` so that `shm_unlink` while a descriptor is open removes the name as it does on the console. `O_CREAT`/`O_EXCL`/`O_TRUNC` select the matching `CreateFileW` disposition, and the host errors map to the guest's (`EEXIST`, `ENOENT`, `EACCES`, `EINVAL`, `ENOSPC`, `EROFS`, `ENAMETOOLONG`). The validation and hashing move into a shared `NamedHostObject` (the SHA-1 helper was already portable and linked into libkernel), and `NamedValidationGuestError` maps the validation errors on Windows. The Linux path is otherwise unchanged. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - `guest_shm_open` now covers a named object — create, `ftruncate`, write, reopen, read back, `O_EXCL` on an existing name (`EEXIST`), unlink, reopen (`ENOENT`), and unlink of an absent name (`ENOENT`); it passes. The test previously asserted that the Windows named path threw. - `tools/progress.py`: libkernel 569 → 571 implemented (`shm_open`, `shm_unlink`); overall 88.12% → 88.17%. - `python tools/check_conventions.py --base origin/main` passes. - Not tested on Linux; the Linux path is unchanged apart from sharing `NamedHostObject`. ### 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
github-actions[bot] commented 2026-10-13 16:18:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T16:18:59Z.


Progress report (a98d42d → d753560)

📈 System libraries: 88.24% (+0.05%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel shm_open_nid_postfix
libkernel shm_unlink_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150051939 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276#issuecomment-6150051939). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T16:18:59Z. --- <!-- progress-report --> ### Progress report (a98d42d → d753560) 📈 **System libraries**: 88.24% (+0.05%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `shm_open_nid_postfix` | | libkernel | `shm_unlink_nid_postfix` | </details>
CnRJay commented 2026-10-14 01:15:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:15:30Z.

Review: COMMENTED


Reusing the Linux path's validation and SHA-1 naming is good. Two differences between a file under %TEMP% and the Linux backing (/dev/shm, tmpfs) are worth handling:

1. Lazy writer I/O. With FILE_ATTRIBUTE_NORMAL, the object is an ordinary NTFS file. Once a title maps it and writes to it, the cache manager's lazy writer flushes dirty pages to disk, and under memory pressure the pages are paged out to the file rather than the pagefile. For shared memory used as an IPC ring or a large shared buffer, that is continuous disk writes that tmpfs never does. FILE_ATTRIBUTE_TEMPORARY tells the cache manager to keep the data in memory and avoid writing it back while memory is available, which is the closest Windows equivalent to tmpfs. It's a one-flag change, in both CreateFileW calls.

2. Lifetime across reboots. /dev/shm is emptied at reboot, but %TEMP% isn't. An object a title created and never unlinked (common, since many programs rely on reboot to clean up) is still there on the next run, possibly days later. A later shm_open(name, O_CREAT | O_EXCL) then fails with EEXIST, and a plain O_CREAT reopens stale contents of the old size. On Linux that only happens within one boot, and the console doesn't keep its shm namespace across a title restart either. Options:

  • put the files in a per-session directory (for example anyps5-shm-<boot id or process start time>/) and remove it at startup if it's stale; or
  • remove anyps5-shm-* files older than the current boot at libkernel init.

Either keeps a crash from leaving objects behind for the next session. Whichever is chosen, the debt entry should note how it differs from tmpfs.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511940071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276#pullrequestreview-5511940071). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:15:30Z. **Review:** COMMENTED --- Reusing the Linux path's validation and SHA-1 naming is good. Two differences between a file under `%TEMP%` and the Linux backing (`/dev/shm`, tmpfs) are worth handling: **1. Lazy writer I/O.** With `FILE_ATTRIBUTE_NORMAL`, the object is an ordinary NTFS file. Once a title maps it and writes to it, the cache manager's lazy writer flushes dirty pages to disk, and under memory pressure the pages are paged out to the file rather than the pagefile. For shared memory used as an IPC ring or a large shared buffer, that is continuous disk writes that tmpfs never does. `FILE_ATTRIBUTE_TEMPORARY` tells the cache manager to keep the data in memory and avoid writing it back while memory is available, which is the closest Windows equivalent to tmpfs. It's a one-flag change, in both `CreateFileW` calls. **2. Lifetime across reboots.** `/dev/shm` is emptied at reboot, but `%TEMP%` isn't. An object a title created and never unlinked (common, since many programs rely on reboot to clean up) is still there on the next run, possibly days later. A later `shm_open(name, O_CREAT | O_EXCL)` then fails with `EEXIST`, and a plain `O_CREAT` reopens stale contents of the old size. On Linux that only happens within one boot, and the console doesn't keep its shm namespace across a title restart either. Options: - put the files in a per-session directory (for example `anyps5-shm-<boot id or process start time>/`) and remove it at startup if it's stale; or - remove `anyps5-shm-*` files older than the current boot at libkernel init. Either keeps a crash from leaving objects behind for the next session. Whichever is chosen, the debt entry should note how it differs from tmpfs.
Nixitov commented 2026-10-14 01:39:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nixitov on 2026-10-14T01:39:13Z.


Both taken, thanks.

1. FILE_ATTRIBUTE_TEMPORARY. The named object is now opened with it, so the cache manager keeps the data in memory while memory is available rather than writing it back to NTFS — the closest match to tmpfs. (The anonymous file already used _O_TEMPORARY.)

2. Lifetime across reboots. Named objects now live in %TEMP%\anyps5-shm, and on first use the directory is cleared when it predates the current boot (its last write is older than now - GetTickCount64). So an object a title left behind can't fail a later O_CREAT | O_EXCL or reopen stale contents — that session's objects are gone, matching /dev/shm being emptied at reboot. The debt entry now records how the Windows backing differs from tmpfs.

guest_shm_open (which covers a named object) passes and conventions pass.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158958190 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276#issuecomment-6158958190). Originally posted by [Nixitov](https://github.com/Nixitov) on 2026-10-14T01:39:13Z. --- Both taken, thanks. **1. `FILE_ATTRIBUTE_TEMPORARY`.** The named object is now opened with it, so the cache manager keeps the data in memory while memory is available rather than writing it back to NTFS — the closest match to tmpfs. (The anonymous file already used `_O_TEMPORARY`.) **2. Lifetime across reboots.** Named objects now live in `%TEMP%\anyps5-shm`, and on first use the directory is cleared when it predates the current boot (its last write is older than `now - GetTickCount64`). So an object a title left behind can't fail a later `O_CREAT | O_EXCL` or reopen stale contents — that session's objects are gone, matching `/dev/shm` being emptied at reboot. The debt entry now records how the Windows backing differs from tmpfs. `guest_shm_open` (which covers a named object) passes and conventions pass. AI-assisted: yes
github-actions[bot] commented 2026-10-14 01:46:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T01:46:33Z.


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
#4992 core/libs/prx/libkernel/File/src/SharedMemory.cpp L16-18
docs/dev/debt/libkernel.md L113
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159045737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276#issuecomment-6159045737). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T01:46:33Z. --- <!-- 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 | |---|---|---|---| | #4992 | `core/libs/prx/libkernel/File/src/SharedMemory.cpp` [L16-18](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libkernel/File/src/SharedMemory.cpp#L16-L18)<br>`docs/dev/debt/libkernel.md` [L113](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/docs/dev/debt/libkernel.md#L113) | | |
oneandonlydean commented 2026-10-14 02:30:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:30:03Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159553991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5276#issuecomment-6159553991). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:30:03Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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