feat(libkernel): support named shared memory on Linux #3555
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3555"
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 lisn0 on 2026-10-11T13:06:57Z.
What
Linux guests can create and reopen named shared-memory objects, size and map them through guest APIs, and unlink their names while existing descriptors and mappings remain usable. Recreating an unlinked name creates a distinct empty object.
shm_openandshm_unlink, translated guest flags/errors and unlink permission checks.TechnicalDebt.md.This is Linux-only. The separate experimental Windows work is not included.
Tested
Latest: rebased onto
0a8b1d32b12a6d6a3acd462787750b482bf78884(headdec6661d). The only conflict was thelibkernel_sys/Export.cppinclude block; main's WindowsCreateSymbolicLinkW/readlinkchanges are kept andumaskstill goes through the libc creation-mask helper. On an x86-64 Linux host:cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON, all targets) succeeded.guest_shm_open, the sixguest_named_shm_*tests (open, lifecycle, descriptor_reuse, permissions, io_descriptor_reuse, concurrency),guest_kernel_sys,guest_memory,guest_filesystem,guest_directory_entries,guest_file_timestamps,guest_dup,guest_file_vector,guest_aio,guest_kernel_fcntlandpatched_libraries. (guest_directory_bufferandguest_directory_closeare Windows-only targets.)Earlier rounds (contribution commits unchanged by
git range-diffacross rebases): repository conventions checker andgit diff --checkpassed; Clang static analysis reported no findings in the changed production translation units; standalone named shared-memory tests passed with ASan/UBSan and warnings as errors; syscall tracing verified namespace locking and lifecycle operations. Both new regressions reproduced failures before their corresponding fixes and passed afterward.Not tested: full test suite, Windows runtime, PS5 titles or console hardware. Full Linux and Windows workflow validation remains for the repository's maintainer-triggered Build workflow. Linux procfs and
/dev/shmare required; advisory locking does not cover external host operations, unrelated objects serialize, and descriptor operations require spare host descriptors. SHA-1 name collisions and invalid non-null name pointers remain documented limitations.Review corrections
_nid_no_patchsuffix survives NID patching; both libkernel and libkernel_sys resolve the same state.__FILE__opens with temporary-file fixtures in lifecycle and mmap descriptor-reuse tests. Old fixtures failed with source paths remapped to a nonexistent location; corrected fixtures passed with the same remapping.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T13:09:07Z.
I found a descriptor-reuse race in the pinning path.
SharedMemoryLockchecks/proc/self/fd/<fd>inIsHostSharedMemoryDescriptor(value), then acquires the/dev/shmnamespace flock and only afterward duplicatesvaluewithF_DUPFD_CLOEXEC. That flock does not serialize the emulator'sclose_nid_postfixordup2_nid_postfix(neither is routed through this lock). If another thread closes and reuses the descriptor for a different named shared-memory object between the check andfcntl, the duplicate still passes the broad/dev/shm/anyps5-shm-prefix test, and the bounded write/truncate/seek can operate on the wrong object.Could the guest descriptor close/replacement path be synchronized with this pin operation, or could the duplicated descriptor's file identity be verified against the original descriptor identity before use? A regression should race an operation against close plus reuse for a second named object and assert that the second object's size/data never changes. This is especially relevant to the PR's claim that these operations pin descriptors against reuse.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T13:25:57Z.
Progress report (
e2ea125→9ac63c0)📉 System libraries: 85.6% (-0.02%, +1 declared functions)
🆕 1 declared as stubs
shm_unlink_nid_postfixImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T13:33:07Z.
Review: CHANGES_REQUESTED
Built and ran this merged onto current
main(Linux). The five new tests pass and fail without the change, but there are three problems:patched_librariesfails because of this PR:libkernel_sys.prx: undefined symbol: _ZN4File33ExchangeCreationMask_nid_no_patchEt.umask_nid_postfix(core/libs/prx/libkernel_sys/Export.cpp:57) now calls a function defined in libkernel, which libkernel_sys cannot resolve, so the module no longer loads. Keep the mask somewhere both libraries can reach (or define it in libkernel_sys and let libkernel query it through an existing shared library), and rerunctest -R patched_libraries.core/libs/prx/libkernel/File/src/SharedMemory.cpp:234-243), as @MarcZX100 reported:IsHostSharedMemoryDescriptor(value)is checked, then the namespace flock is taken, thenvalueis duplicated.close/dup2do not take that lock, so another thread can close and reuse the fd for a different named object in between, and the bounded write/truncate/seek then hits the wrong object. The duplicate passing the/dev/shm/anyps5-shm-prefix test does not prove it is the same file. Please compare the duplicate'sfstatidentity (dev/ino) with the original descriptor's afterF_DUPFD_CLOEXEC, or synchronise the close/dup2 path with the pin, and add the regression MarcZX100 describes (race an operation against close + reuse for a second object and assert the second object never changes). The "pins descriptors against reuse" claim is not true until then.core/libs/tests/GuestNamedShmLifecycle.cpp:166opens__FILE__at run time as the "ordinary file". The test fails (guest errno=2) whenever the source path embedded in the binary does not exist at run time (cached or distributed builds, relocated trees). Create a temporary file in the test instead.Also not verified here: the Windows branches (the PR CI only runs Conventions) and anything that needs a real title.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T13:46:08Z.
Thanks for adding
GuestNamedShmDescriptorReuse.cpp; it covers descriptor replacement during themmappermission check. I still see the race I described in the other descriptor operations:SharedMemoryLockrecognizes the guest fd withIsHostSharedMemoryDescriptor(value).value./dev/shm/anyps5-shm-prefix.If another thread replaces the guest fd with a second named shared-memory object between steps 1 and 2, the duplicate passes that prefix check and
WriteSharedMemory,PwriteSharedMemory,TruncateSharedMemoryorSeekSharedMemorycan act on the second object. The new test replaces the fd with an ordinary file during the mmap path, so it does not exercise this constructor path.Could you add a test that interposes
F_DUPFD_CLOEXECafter the initial path check, swaps in a second named shared-memory fd, and verifies a bounded write/truncate through the first guest fd never changes the second object? The implementation needs to pin the descriptor before relying on its identity, or serialize this path with guest close/replacement.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T13:57:55Z.
Thanks for the follow-up. I reviewed
34acf247:SharedMemoryLocknow captures the original file identity and compares it with the pinned descriptor, and the new I/O descriptor-reuse test swaps in a second named object at both the path-probe and namespace-lock boundaries across six operations. That covers the race from my comment; the replacement object is checked for unchanged size, bytes and offset.Imported from boykopovar/AnyPS5. Originally posted by lisn0 on 2026-10-11T14:27:55Z.
thanks for the review @MarcZX100
@adriadam10 i pushed the fixes in 34acf247b and 2ebcd06f8, the shared mask is now in libc with c linkage, i added descriptor identity checks and reuse regressions, and replaced the source file opens with temporary files
the linux tests passed, the patched library loading check passed for libc, libkernel and libkernel_sys, and the fixtures also passed with source paths remapped to a missing location
can you have another look?
Imported from boykopovar/AnyPS5. Originally posted by lisn0 on 2026-10-11T21:29:43Z.
Rebased onto
0a8b1d32to resolve the conflict in libkernel_sys/Export.cpp (include block only; main's Windows symlink/readlink changes kept). Full Linux Release build is clean and all 17 relevant Linux tests pass, including patched_libraries. Description updated.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T21:38:39Z.
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.
main)docs/dev/TechnicalDebt.mdL437-438docs/dev/TechnicalDebt.mdL403core/libs/prx/libkernel/DirectMemory/DirectMemory.cppL832core/libs/prx/libkernel/DirectMemory/DirectMemory.hppL32docs/dev/TechnicalDebt.mdL403core/libs/prx/libkernel/File/src/Stdio.cppL191docs/dev/TechnicalDebt.mdL402-403Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:44:12Z.
Review: APPROVED
Checked on Linux merged onto current
main: scoped build + ctest 176/176, including the 7 new shm tests,patched_librariesandguest_ioctl_getsid. Write bounds andlseeklimits match FreeBSDuipc_shm.c(uiomove_object,shm_seek).Added one commit on your branch: restored two blank lines changed by accident (
TechnicalDebt.mdline 3,DirectMemory.cppbeforeEraseReservations). Rebased onto currentmain; build and ctest pass again after it. Merging once CI is green.Non-blocking notes: every write/pwrite/seek/ftruncate on any descriptor now does an
fstatplus a/proc/self/fdreadlink before falling back to the native call; andpwritenow returns the errno instead of throwing. A follow-up could cache the shm check if the cost shows up.