fix(libkernel): count guest module segments against the flexible memory budget #3713

Merged
SleglJan merged 1 commit from upstream-pr/3713 into main 2026-10-11 21:54:59 +00:00
SleglJan commented 2026-10-11 15:49:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-11T15:49:43Z.


What

Refs #2883.

sceKernelAvailableFlexibleMemorySize and the flexible maps count only explicit flexible mappings, so on main the whole 448 MiB is available while the title's own code and data are loaded. The Last of Us Part II Remastered (PPSA15508) computes its code and data size as its 448 MiB budget minus the available size minus a 10 MiB module allowance; with 448 MiB available the unsigned result wraps and the title stops at start-up. PPSA15508 v02.001.010, relinked on main a11c8f7f7, Linux, Release, RTX 3080:

Code and Data section is too large. It is 17592186044416.000 and should be 146.000

after which the process ends with SIGSEGV (exit status 139). With this branch the stop is gone on Linux (see Tested).

  • The segments of the main executable and of every .guest.prx module now count against the budget while they are loaded, both for sceKernelAvailableFlexibleMemorySize and for the flexible maps (sceKernelMapFlexibleMemory, the named variants and sceKernelBatchMap2), which now return ENOMEM at the same limit. Reference: shadPS4 module.cpp L165 maps each segment of a module as VMAType::Flexible unless the module comes from its system module directory (VMAType::Code), and its flexible usage counts every Flexible mapping. Host system libraries are not counted here either.
  • Counted are the segments the title's ELF gives permissions, each rounded to 16 KiB pages (shadPS4 rounds p_memsz to p_align, 16 KiB for these images). On Linux the relinker maps its own block (import stubs, dynamic tables) read/write/execute, maps an ELF PT_LOAD without permissions read/write/execute in the executable and drops it from guest modules, and adds a header block at file offset 0 to the executable; those PT_LOADs are skipped. On Windows only the .elf sections with a protection count, the sections sceKernelGetModuleInfo takes a relinked image's segments from. The PT_LOAD without permissions holds the dynamic linking data (PT_DYNAMIC lies in it in both PPSA15508 images); shadPS4 reads the PS4 equivalent, PT_SCE_DYNLIBDATA, into host memory instead of a flexible mapping.
  • The Linux image carries no marker for the relinker's blocks, so they are told apart by those flags and that offset only: a title segment that is writable and executable, or starts at file offset 0, would not be counted. None of the PPSA15508 segments is.
  • The charge is computed from the loaded images when it is needed, so it ends when a module is unloaded.
  • A module whose segments exceed what is left still loads, and the available size then reads 0. shadPS4 fails the segment map instead (memory.cpp L519-L524 returns EINVAL, module.cpp L168-L169 asserts). Recorded in TechnicalDebt; this change adds no throw for it.
  • For PPSA15508, computed from the program headers with the same rule as the code: the executable counts 109.86 MiB and its libc.prx 1.09 MiB, the same as the permissioned PT_LOADs of the original ELFs, so with no flexible mapping yet sceKernelAvailableFlexibleMemorySize reports 337.05 MiB (0x1510C000). The title's own Memory Stats output in the run below prints Code And Global Data : 100.95 MB, against its 146 MiB limit. Counting the two segments without permissions as well would add about 5.4 MiB.
  • What the PS5 charges is not measured: which segments, the rounding, whether system modules use the title's budget, and when an unload releases it. Recorded in TechnicalDebt.

Overlap: live lists re-checked on 2026-10-11 (258 open; the 500 newest closed, #2797-#3699, 298 of them merged; the 800 newest merged, #1494-#3511), titles and files grepped for DirectMemory, Flexible and ModuleInfo. No pull request counts module segments against the flexible budget. Merged #952 (sceKernelReleaseFlexibleMemory) changed the flexible accounting, for released ranges only. Open #3514 caches unwind tables in ModuleInfo.cpp and applies cleanly on top of this branch. #3321 lists fork commit 369cfec (fix/flexible-memory-replacement-accounting), which corrects the counter when a fixed mapping replaces a flexible range; it was not opened upstream, and it rewrites _mapFlexible, so it would conflict with this change.

Tested

  • Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Ryzen 9 7950X3D, RTX 3080 (NVIDIA 615.71.09), Release with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON, this branch on main a11c8f7f7: the 444 tests outside the GPU, shader and video-out groups pass (ctest -E 'agc|spirv|shader|vulkan|Vulkan|video_out|videoout'), and so does agc_driver_pm4, which checks that the GDS is not charged to the flexible budget. The other GPU tests were not run.
  • guest_memory: the new check loads the guest_memory_module.guest.prx fixture (64 KiB data array; now built on Linux too), requires the available size to drop by at least 64 KiB, a flexible map of the previous available size to fail with ENOMEM, and the size to return after dlclose. Without the implementation it fails at the first of these; with it, it passes.
  • Windows: not built or tested here (no MinGW toolchain on this machine). The Windows path walks the sections the way sceKernelGetModuleInfo already does, but it now runs on every flexible map and size query: EnumProcessModules, then GetModuleFileNameW twice for each loaded .prx, without pinning the modules. A module unloaded during the walk can make it throw or read unmapped headers (the same race sceKernelGetModuleList already has), and the cost grows with the number of loaded modules.
  • tools/check_conventions.py --base upstream/main and git diff --check pass.
  • PPSA15508 v02.001.010, relinked with this branch and run in a bubblewrap sandbox: the Code and Data section is too large line no longer appears and the title runs on to its next stop, an unimplemented import outside this PR (sceSslLoadCert not implemented, exit status 134).

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 in TechnicalDebt
  • 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
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820295837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3713). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-11T15:49:43Z. --- ### What Refs #2883. `sceKernelAvailableFlexibleMemorySize` and the flexible maps count only explicit flexible mappings, so on `main` the whole 448 MiB is available while the title's own code and data are loaded. The Last of Us Part II Remastered (PPSA15508) computes its code and data size as its 448 MiB budget minus the available size minus a 10 MiB module allowance; with 448 MiB available the unsigned result wraps and the title stops at start-up. PPSA15508 v02.001.010, relinked on `main` a11c8f7f7, Linux, Release, RTX 3080: ``` Code and Data section is too large. It is 17592186044416.000 and should be 146.000 ``` after which the process ends with SIGSEGV (exit status 139). With this branch the stop is gone on Linux (see Tested). - The segments of the main executable and of every `.guest.prx` module now count against the budget while they are loaded, both for `sceKernelAvailableFlexibleMemorySize` and for the flexible maps (`sceKernelMapFlexibleMemory`, the named variants and `sceKernelBatchMap2`), which now return `ENOMEM` at the same limit. Reference: [shadPS4 `module.cpp` L165](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/module.cpp#L165) maps each segment of a module as `VMAType::Flexible` unless the module comes from its system module directory (`VMAType::Code`), and its flexible usage counts every `Flexible` mapping. Host system libraries are not counted here either. - Counted are the segments the title's ELF gives permissions, each rounded to 16 KiB pages (shadPS4 rounds `p_memsz` to `p_align`, 16 KiB for these images). On Linux the relinker maps its own block (import stubs, dynamic tables) read/write/execute, maps an ELF `PT_LOAD` without permissions read/write/execute in the executable and drops it from guest modules, and adds a header block at file offset 0 to the executable; those `PT_LOAD`s are skipped. On Windows only the `.elf` sections with a protection count, the sections `sceKernelGetModuleInfo` takes a relinked image's segments from. The `PT_LOAD` without permissions holds the dynamic linking data (`PT_DYNAMIC` lies in it in both PPSA15508 images); shadPS4 reads the PS4 equivalent, `PT_SCE_DYNLIBDATA`, into host memory instead of a flexible mapping. - The Linux image carries no marker for the relinker's blocks, so they are told apart by those flags and that offset only: a title segment that is writable and executable, or starts at file offset 0, would not be counted. None of the PPSA15508 segments is. - The charge is computed from the loaded images when it is needed, so it ends when a module is unloaded. - A module whose segments exceed what is left still loads, and the available size then reads 0. shadPS4 fails the segment map instead ([`memory.cpp` L519-L524](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/memory.cpp#L519-L524) returns `EINVAL`, [`module.cpp` L168-L169](https://github.com/shadps4-emu/shadPS4/blob/3a9f57265d28975658f1ca941daa5c879c7a74bd/src/core/module.cpp#L168-L169) asserts). Recorded in TechnicalDebt; this change adds no throw for it. - For PPSA15508, computed from the program headers with the same rule as the code: the executable counts 109.86 MiB and its `libc.prx` 1.09 MiB, the same as the permissioned `PT_LOAD`s of the original ELFs, so with no flexible mapping yet `sceKernelAvailableFlexibleMemorySize` reports 337.05 MiB (`0x1510C000`). The title's own Memory Stats output in the run below prints `Code And Global Data : 100.95 MB`, against its 146 MiB limit. Counting the two segments without permissions as well would add about 5.4 MiB. - What the PS5 charges is not measured: which segments, the rounding, whether system modules use the title's budget, and when an unload releases it. Recorded in TechnicalDebt. Overlap: live lists re-checked on 2026-10-11 (258 open; the 500 newest closed, #2797-#3699, 298 of them merged; the 800 newest merged, #1494-#3511), titles and files grepped for DirectMemory, Flexible and ModuleInfo. No pull request counts module segments against the flexible budget. Merged #952 (`sceKernelReleaseFlexibleMemory`) changed the flexible accounting, for released ranges only. Open #3514 caches unwind tables in `ModuleInfo.cpp` and applies cleanly on top of this branch. #3321 lists fork commit [369cfec](https://github.com/cookie42073-cmd/AnyPS5/commit/369cfec9fdce61c700e137975073bd1cdaad6051) (`fix/flexible-memory-replacement-accounting`), which corrects the counter when a fixed mapping replaces a flexible range; it was not opened upstream, and it rewrites `_mapFlexible`, so it would conflict with this change. ### Tested - Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Ryzen 9 7950X3D, RTX 3080 (NVIDIA 615.71.09), Release with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, this branch on `main` a11c8f7f7: the 444 tests outside the GPU, shader and video-out groups pass (`ctest -E 'agc|spirv|shader|vulkan|Vulkan|video_out|videoout'`), and so does `agc_driver_pm4`, which checks that the GDS is not charged to the flexible budget. The other GPU tests were not run. - `guest_memory`: the new check loads the `guest_memory_module.guest.prx` fixture (64 KiB data array; now built on Linux too), requires the available size to drop by at least 64 KiB, a flexible map of the previous available size to fail with `ENOMEM`, and the size to return after `dlclose`. Without the implementation it fails at the first of these; with it, it passes. - Windows: not built or tested here (no MinGW toolchain on this machine). The Windows path walks the sections the way `sceKernelGetModuleInfo` already does, but it now runs on every flexible map and size query: `EnumProcessModules`, then `GetModuleFileNameW` twice for each loaded `.prx`, without pinning the modules. A module unloaded during the walk can make it throw or read unmapped headers (the same race `sceKernelGetModuleList` already has), and the cost grows with the number of loaded modules. - `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. - PPSA15508 v02.001.010, relinked with this branch and run in a bubblewrap sandbox: the `Code and Data section is too large` line no longer appears and the title runs on to its next stop, an unimplemented import outside this PR (`sceSslLoadCert not implemented`, exit status 134). ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq
adriadam10 commented 2026-10-11 21:54:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:54:48Z.


Checked on the PR merged onto current main: scoped build and ctest (112 tests) pass, guest_memory fails without the change and passes with it, conventions and the TechnicalDebt entry are in place. The Windows path and the title run were not verified here; the deviations from shadPS4 and the unmeasured PS5 behaviour are recorded in TechnicalDebt. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114225794 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3713#issuecomment-6114225794). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:54:48Z. --- Checked on the PR merged onto current main: scoped build and ctest (112 tests) pass, `guest_memory` fails without the change and passes with it, conventions and the TechnicalDebt entry are in place. The Windows path and the title run were not verified here; the deviations from shadPS4 and the unmeasured PS5 behaviour are recorded in TechnicalDebt. Merging.
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!3713
No description provided.