feat(libkernel): support sceKernelMprotect on foreign guest module images on Windows #5109

Merged
pushkarreddyy merged 4 commits from upstream-pr/5109 into main 2026-10-14 04:26:43 +00:00
pushkarreddyy commented 2026-10-13 11:55:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-13T11:55:19Z.


What

Supports sceKernelMprotect memory protection changes on Windows for foreign guest module images (.prx, .debug_prx, .sprx, .suprx, and images with .elf0 section headers) in addition to .guest.prx.

Addresses Dying Light 2: Stay Human (PPSA02262, Issue #1794 Section 4) where bundled NVIDIA Real-time Denoisers libNRD.prx calls sceKernelMprotect on its own image (prot=0x12 and 0x11) and previously terminated with:

terminate called after throwing an instance of 'std::invalid_argument'
  what(): Memory protection of a foreign image is not supported

Host system libraries (such as kernel32.dll) without guest image markers or suffixes continue to be rejected with std::invalid_argument.

Tested

  • Windows 11 host (x86_64 MinGW-w64 / MSVC)
  • Unit test in core/libs/tests/GuestMemory.cpp (CheckGuestModuleImageProtection):
    • Verified protection mutations succeed and are tracked in GuestAllocations for .prx and .debug_prx module copies
    • Verified host system library kernel32.dll continues to be rejected with std::invalid_argument
    • Verified dlclose unregisters the guest allocation range cleanly
  • python tools/check_conventions.py --base origin/main passed with 0 findings and 0 errors

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: PPSA02262 #1794
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4842822372 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-13T11:55:19Z. --- ### What Supports `sceKernelMprotect` memory protection changes on Windows for foreign guest module images (`.prx`, `.debug_prx`, `.sprx`, `.suprx`, and images with `.elf0` section headers) in addition to `.guest.prx`. Addresses *Dying Light 2: Stay Human* (`PPSA02262`, Issue #1794 Section 4) where bundled NVIDIA Real-time Denoisers `libNRD.prx` calls `sceKernelMprotect` on its own image (`prot=0x12` and `0x11`) and previously terminated with: ``` terminate called after throwing an instance of 'std::invalid_argument' what(): Memory protection of a foreign image is not supported ``` Host system libraries (such as `kernel32.dll`) without guest image markers or suffixes continue to be rejected with `std::invalid_argument`. ### Tested - Windows 11 host (x86_64 MinGW-w64 / MSVC) - Unit test in `core/libs/tests/GuestMemory.cpp` (`CheckGuestModuleImageProtection`): - Verified protection mutations succeed and are tracked in `GuestAllocations` for `.prx` and `.debug_prx` module copies - Verified host system library `kernel32.dll` continues to be rejected with `std::invalid_argument` - Verified `dlclose` unregisters the guest allocation range cleanly - `python tools/check_conventions.py --base origin/main` passed with 0 findings and 0 errors ### 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: PPSA02262 #1794 - [x] AI-assisted: yes
pushkarreddyy commented 2026-10-14 02:13:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-14T02:13:27Z.


Calling on @adriadam10, @oneandonlydean, and @MarcZX100 for review / merge clearance:

All CI checks passing and rebased clean on origin/main. Adds VirtualProtect support for guest module images with foreign allocation bases on Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159361184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159361184). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T02:13:27Z. --- Calling on @adriadam10, @oneandonlydean, and @MarcZX100 for review / merge clearance: All CI checks passing and rebased clean on `origin/main`. Adds `VirtualProtect` support for guest module images with foreign allocation bases on Windows.
pushkarreddyy commented 2026-10-14 02:14:42 +00:00 (Migrated from github.com)

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


Calling on @adriadam10, @oneandonlydean, and @MarcZX100 for review / merge clearance:

All CI checks passing and rebased clean on origin/main. Adds VirtualProtect support for guest module images with foreign allocation bases on Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159376508 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159376508). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T02:14:42Z. --- Calling on @adriadam10, @oneandonlydean, and @MarcZX100 for review / merge clearance: All CI checks passing and rebased clean on `origin/main`. Adds `VirtualProtect` support for guest module images with foreign allocation bases on Windows.
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.


Matching .prx also matches the emulator's own libraries (they're built with that suffix on Windows), so a guest sceKernelMprotect could change the protection of libkernel.prx and the rest. Can you drop the suffix list and decide by the .elf0 section only, as ModuleInfo.cpp already does for relinked images? The test then needs a relinked module rather than a renamed copy of the host-built one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159602083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159602083). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:34:10Z. --- Matching `.prx` also matches the emulator's own libraries (they're built with that suffix on Windows), so a guest `sceKernelMprotect` could change the protection of `libkernel.prx` and the rest. Can you drop the suffix list and decide by the `.elf0` section only, as `ModuleInfo.cpp` already does for relinked images? The test then needs a relinked module rather than a renamed copy of the host-built one.
pushkarreddyy commented 2026-10-14 02:43:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-14T02:43:01Z.


@oneandonlydean Thanks for the review! Dropped the suffix list entirely — IsGuestModuleImage now decides strictly by the .elf0 section in the PE headers, matching ModuleInfo.cpp.

Also updated the test fixture guest_memory_module to place its buffer into .elf0, verified that host libraries (libkernel.prx, kernel32.dll) without .elf0 are strictly rejected, and rebased cleanly onto latest origin/main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159703942 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159703942). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T02:43:01Z. --- @oneandonlydean Thanks for the review! Dropped the suffix list entirely — `IsGuestModuleImage` now decides strictly by the `.elf0` section in the PE headers, matching `ModuleInfo.cpp`. Also updated the test fixture `guest_memory_module` to place its buffer into `.elf0`, verified that host libraries (`libkernel.prx`, `kernel32.dll`) without `.elf0` are strictly rejected, and rebased cleanly onto latest `origin/main`.
pushkarreddyy commented 2026-10-14 02:50:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-14T02:50:36Z.


@oneandonlydean Updated to identify guest module images strictly by the .elf0 PE section header (mirroring ModuleInfo.cpp), completely removing the filename suffix check so host PRXs like libkernel.prx cannot be mutated. Also added unit test verification asserting that host libkernel.prx (&sceKernelMprotect) is rejected.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159789737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159789737). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T02:50:36Z. --- @oneandonlydean Updated to identify guest module images strictly by the .elf0 PE section header (mirroring ModuleInfo.cpp), completely removing the filename suffix check so host PRXs like libkernel.prx cannot be mutated. Also added unit test verification asserting that host libkernel.prx (&sceKernelMprotect) is rejected.
oneandonlydean commented 2026-10-14 03:00:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T03:00:09Z.


Both points are in, thanks. The rebase had replaced the Memory protection bits 0x100 and 0x200 entry in docs/dev/debt/libkernel.md with the new one; I pushed a commit putting it back next to yours. No other change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159896426 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6159896426). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T03:00:09Z. --- Both points are in, thanks. The rebase had replaced the `Memory protection bits 0x100 and 0x200` entry in `docs/dev/debt/libkernel.md` with the new one; I pushed a commit putting it back next to yours. No other change.
oneandonlydean commented 2026-10-14 03:12:30 +00:00 (Migrated from github.com)

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


The Windows build failed guest_memory: the shared test module got the .elf0 section, so CheckGuestModuleUsesFlexibleMemory no longer saw its data counted. I pushed a commit giving the mprotect test its own module with .elf0 and leaving the shared one as on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6160037182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6160037182). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T03:12:30Z. --- The Windows build failed `guest_memory`: the shared test module got the `.elf0` section, so `CheckGuestModuleUsesFlexibleMemory` no longer saw its data counted. I pushed a commit giving the mprotect test its own module with `.elf0` and leaving the shared one as on main.
oneandonlydean commented 2026-10-14 03:26:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T03:26:43Z.


The Windows run then hit the test's own sceKernelMprotect(&sceKernelMprotect, ...): on Windows that address is the import stub in the test executable, so it made the test's code read-only. It now takes libkernel's image from GetModuleHandleA("libkernel.prx").

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6160200082 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5109#issuecomment-6160200082). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T03:26:43Z. --- The Windows run then hit the test's own `sceKernelMprotect(&sceKernelMprotect, ...)`: on Windows that address is the import stub in the test executable, so it made the test's code read-only. It now takes libkernel's image from `GetModuleHandleA("libkernel.prx")`.
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!5109
No description provided.