fix(libkernel): reprotect bundled guest module images on Windows #1803
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1803"
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 tv7 on 2026-10-08T19:32:33Z.
What
Fixes item 4 of #1794. On Windows,
sceKernelMprotectaccepted only the executable's image and threwMemory protection of a foreign image is not supportedfor any other image. Bundled title modules are relinked into DLLs named<module>.guest.prx, so a title that reprotects pages of its own bundled module could not start. Dying Light 2's bundledlibNRDdoes this at start-up (prot=0x12over 0x11278 bytes, then0x11over 0x178540 bytes).DoMprotectnow also accepts images whose file name ends in.guest.prx. It registers their committed pages inGuestAllocationsthe same way as the executable's:RegisterMainImage's Windows walk becomes a per-image registration, and the executable is one caller. Any other image (system DLLs, AnyPS5's own.prxlibraries) is still refused.Tested
Windows 11, MinGW GCC 15.2.0, RTX 3070:
guest_memoryhas a new Windows case. It loads a small DLL namedguest_memory_module.guest.prxand reprotects a data page read-only, then writable (writes and checks a byte), then read-only again, checking the host protection each time. It also checks that a page ofkernel32.dllis still refused. Without the fix it fails withMemory protection of a foreign image is not supported; with it, it passes.ctest: 477/477 pass.python tools/check_conventions.py --base origin/mainpasses.libNRDreprotections now succeed, and the title continues to its ownNet_StartUp failedassertion (item 5 of #1794, unrelated to this change). The other items of #1794 were worked around locally for this run and are not part of this PR.Linux is unchanged. I did not build or run it.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T02:48:06Z.
Review: CHANGES_REQUESTED
I found a module-lifetime issue in the new registration.
registerImageadds committed.guest.prxpages to the global allocation registry as non-releasable and records the base inregisteredImages, but guest modules can later be unloaded:sceKernelStopUnloadModulecallsdlclose_nid_postfix, whose WindowsModuledestructor callsFreeLibrary. Nothing removes those registry ranges or the base fromregisteredImageson unload. This leaves dead image ranges registered, can make later mappings at the released address fail, and can expose stale addresses to GPU builds. Please add unload cleanup coordinated with outstanding allocation leases and cover mprotect → unload → reload/address reuse in a Windows test before approval.Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-09T11:11:29Z.
Thanks, you're right: nothing undid the registration when a module was unloaded. Fixed in the new commit:
GuestAllocationsrecords each registered image's extent and getsGuestAllocationsUnregisterImage_nid_postfix. It waits for outstanding leases on that extent throughRequireUnpinned(same path, pin waiter and timeout as unmap/protect), reports the extent to the invalidator, then drops the image's non-releasable ranges and itsregisteredImagesentry.Moduledestructor inDynamicLoader.cppunregisters the image beforeFreeLibrary. If the wait fails (lease still held afterAPS5_PIN_WAIT_MS), the module stays loaded, the destructor prints the error, and it returns, so GPU work never sees a freed image.FreeLibraryonly drops a reference and the image stays mapped, the nextsceKernelMprotecton it registers it again from the current page protections.The test (
guest_memory, Windows) now loads the fixture throughdlopen_nid_postfixand checks:mprotect → registered →
dlclosewhile a lease is held (it blocks, image still mapped) → lease dropped →dlclosereturns, range no longer registered → a new allocation at the freed address registers → reload → mprotect registers again →dlclose→ unregistered.With the unregister call removed, the test fails at the "dlclose blocks while leased" check.
Full build + ctest: 477/477 pass (Windows, MinGW, RTX 3070).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:14:42Z.
~ModuleinDynamicLoader.cppcatches theUnregisterImagefailure, prints it and leaves the module loaded. Failures need to stay explicit here; please unregister indlclose_nid_postfixbefore theModuleis destroyed and let the exception propagate.Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-09T21:23:57Z.
@oneandonlydean done in 0040990d1:
~Moduleno longer touches the registry; it only frees the library.dlclose_nid_postfixcallsUnregisterImagebefore erasing the handle, so a failure (image still leased) propagates and the handle, symbols and protection stay valid. The handle is erased and the library freed only after unregistering succeeds.guest_memory: dlclose under a held lease now has to throw and leave the module usable and protected (APS5_PIN_WAIT_MS=1000in that test), then the existing close-after-release, reload and address-reuse checks run.Full build + ctest 477/477 on Windows (GCC 15.2 UCRT, RTX 3070);
check_conventions.pypasses.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:16:42Z.
Conflicts with main in
core/libs/tests/GuestMemory.cpp; please rebase. Thedlclosechange is right.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T08:28:35Z.
Review: APPROVED
Rebased onto current
mainas requested (single conflict incore/libs/tests/GuestMemory.cpp,main(): kept the_putenv_sunder_WIN32and the two new checks frommain). Built and ranguest_memoryand the libc/libkernel tests on Linux: green. The Windows case needs the Windows job; merging once CI is green.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T08:37:47Z.
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)core/libs/tests/GuestMemory.cppL536, L1269-1270core/libs/prx/libc/include/GuestAllocations.hppL60-61core/libs/prx/libc/include/GuestAllocations.hppL60-61core/libs/tests/GuestMemory.cppL21, L1269