fix(libkernel): bound anonymous shared memory writes and seeks on Windows #4992

Closed
Ragbie wants to merge 1 commit from upstream-pr/4992 into main
Ragbie commented 2026-10-13 07:28:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-13T07:28:36Z.


What

On Windows, an anonymous shm_open object (a temporary file in the host temp directory) was not recognized as shared memory: IsHostSharedMemoryDescriptor returned false off Linux. A write or pwrite past the object size grew the object, and lseek past its end succeeded. FreeBSD 11.4 bounds both (sys/kern/uipc_shm.c): shm_seek returns EINVAL for an offset outside [0, size], and shm_write goes through uiomove_object, which stops at the object size. The Linux build already does this.

  • On Windows, a descriptor is recognized as an anonymous object when it is a disk file with the temporary attribute whose final path name starts with anyps5-shm-, the prefix CreateAnonymousFile uses. _get_osfhandle runs with the invalid parameter handler suppressed, and errno is preserved.
  • Windows shared-memory operations hold a process-wide mutex, as the Linux ones hold the /dev/shm lock, so concurrent writes cannot grow the object. Windows objects are process-private (_O_NOINHERIT; named objects are unsupported).
  • NativePositioned_nid_no_patch is split: the shared-memory pwrite path calls the new NativePositionedFile_nid_no_patch, since calling NativePositioned_nid_no_patch from it would recurse.
  • The Windows TruncateSharedMemory error path returns an errno value, which is what the Windows ftruncate caller expects.
  • The technical debt entry records how Windows objects are recognized.

Unchanged and now asserted: mmap of these objects on Windows still fails with error 45, because file-backed mappings remain unsupported there.

Cost: on Windows, write, lseek, pwrite and ftruncate on an ordinary disk file now make one GetFileType and one GetFileInformationByHandleEx(FileBasicInfo) call first: about 0.66 µs per call here (lseek(SEEK_CUR) on the same file: 0.26 µs), 200,000 calls, median of 5 runs. The Linux path makes a readlink of /proc/self/fd per call.

Overlap with #4973 (open), which adds a process-wide lock to NativePositioned_nid_no_patch and NativeTransfer: whichever merges second should take that lock at the top of NativePositioned_nid_no_patch, before the shared-memory hook, and not in NativePositionedFile_nid_no_patch. Otherwise a shared-memory pwrite takes the shared-memory lock before that lock, while write through NativeTransfer takes them in the opposite order.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, full build with the system libraries.

  • guest_shm_open with the new assertions aborts with main's SharedMemory.cpp, Stdio.cpp and File.hpp, and passes with this change.
  • Full ctest: 1039/1039 passed, 7 skipped; tools/check_skips.py --job windows: all expected.
  • tools/check_conventions.py --base main: passed.
  • Linux: not run. The new assertions were checked against the Linux (memfd) code path by reading it.

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
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839880403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4992). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-13T07:28:36Z. --- ### What On Windows, an anonymous `shm_open` object (a temporary file in the host temp directory) was not recognized as shared memory: `IsHostSharedMemoryDescriptor` returned false off Linux. A `write` or `pwrite` past the object size grew the object, and `lseek` past its end succeeded. FreeBSD 11.4 bounds both (`sys/kern/uipc_shm.c`): `shm_seek` returns EINVAL for an offset outside `[0, size]`, and `shm_write` goes through `uiomove_object`, which stops at the object size. The Linux build already does this. - On Windows, a descriptor is recognized as an anonymous object when it is a disk file with the temporary attribute whose final path name starts with `anyps5-shm-`, the prefix `CreateAnonymousFile` uses. `_get_osfhandle` runs with the invalid parameter handler suppressed, and `errno` is preserved. - Windows shared-memory operations hold a process-wide mutex, as the Linux ones hold the `/dev/shm` lock, so concurrent writes cannot grow the object. Windows objects are process-private (`_O_NOINHERIT`; named objects are unsupported). - `NativePositioned_nid_no_patch` is split: the shared-memory `pwrite` path calls the new `NativePositionedFile_nid_no_patch`, since calling `NativePositioned_nid_no_patch` from it would recurse. - The Windows `TruncateSharedMemory` error path returns an errno value, which is what the Windows `ftruncate` caller expects. - The technical debt entry records how Windows objects are recognized. Unchanged and now asserted: `mmap` of these objects on Windows still fails with error 45, because file-backed mappings remain unsupported there. Cost: on Windows, `write`, `lseek`, `pwrite` and `ftruncate` on an ordinary disk file now make one `GetFileType` and one `GetFileInformationByHandleEx(FileBasicInfo)` call first: about 0.66 µs per call here (`lseek(SEEK_CUR)` on the same file: 0.26 µs), 200,000 calls, median of 5 runs. The Linux path makes a `readlink` of `/proc/self/fd` per call. Overlap with #4973 (open), which adds a process-wide lock to `NativePositioned_nid_no_patch` and `NativeTransfer`: whichever merges second should take that lock at the top of `NativePositioned_nid_no_patch`, before the shared-memory hook, and not in `NativePositionedFile_nid_no_patch`. Otherwise a shared-memory `pwrite` takes the shared-memory lock before that lock, while `write` through `NativeTransfer` takes them in the opposite order. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, full build with the system libraries. - `guest_shm_open` with the new assertions aborts with `main`'s `SharedMemory.cpp`, `Stdio.cpp` and `File.hpp`, and passes with this change. - Full `ctest`: 1039/1039 passed, 7 skipped; `tools/check_skips.py --job windows`: all expected. - `tools/check_conventions.py --base main`: passed. - Linux: not run. The new assertions were checked against the Linux (`memfd`) code path by reading it. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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] AI-assisted: yes (Claude Code)
xXPBSXx commented 2026-10-13 09:16:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T09:16:36Z.


Windows counter-test: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto main 2a2a3dc65. guest_shm_open passes with the PR; with the PR's source files replaced by main's, same test, it aborts (0xc0000409).

Not tested: Linux, a console, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142326525 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4992#issuecomment-6142326525). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T09:16:36Z. --- Windows counter-test: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto `main` `2a2a3dc65`. `guest_shm_open` passes with the PR; with the PR's source files replaced by `main`'s, same test, it aborts (0xc0000409). Not tested: Linux, a console, a title run. AI-assisted (Claude Code).
CnRJay commented 2026-10-14 01:19:35 +00:00 (Migrated from github.com)

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

Review: COMMENTED


One interaction with #5276 (named shared memory on Windows), since both decide what an anyps5-shm-* file is:

  • This PR recognizes an object by the FILE_ATTRIBUTE_TEMPORARY attribute plus the anyps5-shm- prefix.
  • #5276 creates named objects as %TEMP%/anyps5-shm-<sha1> with FILE_ATTRIBUTE_NORMAL.

So once both land, named objects would not be bounded (write past the size grows them, and lseek past the end succeeds), while anonymous ones would. On Linux both kinds live under /dev/shm and get the same rules. Agreeing on one marker would keep the two kinds consistent: #5276 creating its files with FILE_ATTRIBUTE_TEMPORARY too (which I've also suggested there, to keep the data out of the lazy writer), or a recognition that doesn't depend on the attribute. Whichever lands second could also add a named-object case to guest_shm_open.

The cost measurement (0.66 µs per call on ordinary files) is appreciated. That's fine for write/lseek.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511962832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4992#pullrequestreview-5511962832). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:19:35Z. **Review:** COMMENTED --- One interaction with #5276 (named shared memory on Windows), since both decide what an `anyps5-shm-*` file is: - This PR recognizes an object by the `FILE_ATTRIBUTE_TEMPORARY` attribute plus the `anyps5-shm-` prefix. - #5276 creates named objects as `%TEMP%/anyps5-shm-<sha1>` with `FILE_ATTRIBUTE_NORMAL`. So once both land, named objects would *not* be bounded (`write` past the size grows them, and `lseek` past the end succeeds), while anonymous ones would. On Linux both kinds live under `/dev/shm` and get the same rules. Agreeing on one marker would keep the two kinds consistent: #5276 creating its files with `FILE_ATTRIBUTE_TEMPORARY` too (which I've also suggested there, to keep the data out of the lazy writer), or a recognition that doesn't depend on the attribute. Whichever lands second could also add a named-object case to `guest_shm_open`. The cost measurement (0.66 µs per call on ordinary files) is appreciated. That's fine for `write`/`lseek`.
oneandonlydean commented 2026-10-14 02:34:10 +00:00 (Migrated from github.com)

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


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:6159601907 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4992#issuecomment-6159601907). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:34:10Z. --- 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!4992
No description provided.