fix(libkernel): bound anonymous shared memory writes and seeks on Windows #4992
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4992"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-13T07:28:36Z.
What
On Windows, an anonymous
shm_openobject (a temporary file in the host temp directory) was not recognized as shared memory:IsHostSharedMemoryDescriptorreturned false off Linux. Awriteorpwritepast the object size grew the object, andlseekpast its end succeeded. FreeBSD 11.4 bounds both (sys/kern/uipc_shm.c):shm_seekreturns EINVAL for an offset outside[0, size], andshm_writegoes throughuiomove_object, which stops at the object size. The Linux build already does this.anyps5-shm-, the prefixCreateAnonymousFileuses._get_osfhandleruns with the invalid parameter handler suppressed, anderrnois preserved./dev/shmlock, so concurrent writes cannot grow the object. Windows objects are process-private (_O_NOINHERIT; named objects are unsupported).NativePositioned_nid_no_patchis split: the shared-memorypwritepath calls the newNativePositionedFile_nid_no_patch, since callingNativePositioned_nid_no_patchfrom it would recurse.TruncateSharedMemoryerror path returns an errno value, which is what the Windowsftruncatecaller expects.Unchanged and now asserted:
mmapof these objects on Windows still fails with error 45, because file-backed mappings remain unsupported there.Cost: on Windows,
write,lseek,pwriteandftruncateon an ordinary disk file now make oneGetFileTypeand oneGetFileInformationByHandleEx(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 areadlinkof/proc/self/fdper call.Overlap with #4973 (open), which adds a process-wide lock to
NativePositioned_nid_no_patchandNativeTransfer: whichever merges second should take that lock at the top ofNativePositioned_nid_no_patch, before the shared-memory hook, and not inNativePositionedFile_nid_no_patch. Otherwise a shared-memorypwritetakes the shared-memory lock before that lock, whilewritethroughNativeTransfertakes them in the opposite order.Tested
Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, full build with the system libraries.
guest_shm_openwith the new assertions aborts withmain'sSharedMemory.cpp,Stdio.cppandFile.hpp, and passes with this change.ctest: 1039/1039 passed, 7 skipped;tools/check_skips.py --job windows: all expected.tools/check_conventions.py --base main: passed.memfd) code path by reading it.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
main2a2a3dc65.guest_shm_openpasses with the PR; with the PR's source files replaced bymain's, same test, it aborts (0xc0000409).Not tested: Linux, a console, a title run.
AI-assisted (Claude Code).
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:FILE_ATTRIBUTE_TEMPORARYattribute plus theanyps5-shm-prefix.%TEMP%/anyps5-shm-<sha1>withFILE_ATTRIBUTE_NORMAL.So once both land, named objects would not be bounded (
writepast the size grows them, andlseekpast the end succeeds), while anonymous ones would. On Linux both kinds live under/dev/shmand get the same rules. Agreeing on one marker would keep the two kinds consistent: #5276 creating its files withFILE_ATTRIBUTE_TEMPORARYtoo (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 toguest_shm_open.The cost measurement (0.66 µs per call on ordinary files) is appreciated. That's fine for
write/lseek.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.
Pull request closed