fix(libkernel): account for fixed flexible memory replacements #6491

Merged
Ondra97 merged 1 commit from upstream-pr/6491 into main 2026-10-19 02:30:18 +00:00
Ondra97 commented 2026-10-15 17:23:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-15T17:23:40Z.


What

sceKernelMapFlexibleMemory rejects a valid MAP_FIXED replacement when the flexible-memory budget is full, because it charges the requested range before crediting the flexible pages it replaces.

The budget check now subtracts the exact overlap with existing flexible mappings. It grants no credit for holes or MAP_NOOVERWRITE, and failed mappings retain their charge and data. The successful-map range splitting already in main is preserved.

The regression checks interior, prefix, multi-range and hole replacements, neighboring data, partial releases, full-budget interior and whole-range replacements, and rejection of growth beyond the budget. The rebase preserves the direct/reserve/POSIX accounting from #7112.

Tested

Base: cb1d6e5bd642c496d01ac583c614c82a8c654a41. Head: 393506c161a5eb8b1855e6fa77d73c3b56c8a395.

  • Windows (native host): Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER; WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, BUILD_TESTING=ON.
  • Linux (VirtualBox): Ubuntu 24.04.5 LTS, kernel 7.0.0-38-generic, GCC 13.3.0, 4 vCPUs, 8 GiB RAM, VirtualBox 7.2.20 on the same Windows host; Ninja, Release, BUILD_TESTING=ON. Graphics: not exercised. All 16,142 source/dependency files were verified against the Git blobs, with zero missing files or mismatches.

Windows (native host) negative control: with the final regression and unmodified base production code, guest_flexible_memory_limited fails at the full-budget interior-page replacement assertion. The preceding range-splitting checks pass. The final production source was restored and its SHA-256 verified before the final build.

Windows (native host), one run per variant with the same final regression and a 16 MiB configured budget:

Check Before (base production code) After (fix)
Replace an existing interior 16 KiB page at a full budget Fails with ENOMEM Succeeds; adjacent data and the budget are preserved
Environment / check Result
Windows (native host), full default build including patched libraries PASS
Windows (native host), selected memory/runtime CTests 24/24 PASS; 0 failed, 0 skipped (2.42 s)
Linux (VirtualBox), full default application/library/test build PASS
Linux (VirtualBox), selected memory/runtime CTests 24/24 PASS; 0 failed, 0 skipped (11.15 s)

The selected tests cover all five flexible-memory modes, guest mapping and fixed anonymous mapping, protection, memory-pool statistics, allocator failure handling, stacks, JIT/shared memory, page-table statistics, mlock, and patched libraries. Windows additionally runs write-watch; Linux (VirtualBox) additionally runs mapping lifetime. Five integration checks cover fiber/thread frame chains, user contexts (including return/exit), and Linux GNU segment preservation after the latest main updates.

cmake --build build --parallel 6
ctest --test-dir build --output-on-failure -j 2 -R '^(patched_libraries|windows_write_watch|fiber|linux_gnu_segments|guest_(user_context(_exit)?|allocator_failures|kernel_(is_stack|stack_protect)|pthread_(frame_chain|attr_stack|stack)|jit_shared_memory|memory(_(lifetime|pool_stats))?|mmap_fixed_anonymous|mprotect_unmapped|flexible_memory_.*|mlock|page_table_stats))$'

Linux (VirtualBox) uses --parallel 2 for the build and the same CTest selection. Conventions, whitespace, main-sync and test-registration audits passed; all 985 Windows and 997 Linux (VirtualBox) test executables have CTest registrations. These audit counts are not runtime test counts.

Linux graphics/Vulkan tests: NOT RUN — intentionally excluded from VirtualBox validation. Native/bare-metal Linux was not tested. Native Linux GPU validation: not tested. No console, retail/homebrew title or performance result is claimed. Hosted full platform builds require collaborator dispatch.

The shared-file review found no second implementation of this remaining accounting fix. #6515 covers direct replacements; #6044 moves the original budget fixture unchanged, so these new assertions must be preserved at its destination if it merges first. The macOS query/locking changes in #5021, #6222, #7201 and #7332 do not implement this budget credit.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4877227831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6491). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-15T17:23:40Z. --- ### What `sceKernelMapFlexibleMemory` rejects a valid `MAP_FIXED` replacement when the flexible-memory budget is full, because it charges the requested range before crediting the flexible pages it replaces. The budget check now subtracts the exact overlap with existing flexible mappings. It grants no credit for holes or `MAP_NOOVERWRITE`, and failed mappings retain their charge and data. The successful-map range splitting already in `main` is preserved. The regression checks interior, prefix, multi-range and hole replacements, neighboring data, partial releases, full-budget interior and whole-range replacements, and rejection of growth beyond the budget. The rebase preserves the direct/reserve/POSIX accounting from #7112. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Base: `cb1d6e5bd642c496d01ac583c614c82a8c654a41`. Head: `393506c161a5eb8b1855e6fa77d73c3b56c8a395`. - Windows (native host): Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER; WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `BUILD_TESTING=ON`. - Linux (VirtualBox): Ubuntu 24.04.5 LTS, kernel 7.0.0-38-generic, GCC 13.3.0, 4 vCPUs, 8 GiB RAM, VirtualBox 7.2.20 on the same Windows host; Ninja, Release, `BUILD_TESTING=ON`. Graphics: not exercised. All 16,142 source/dependency files were verified against the Git blobs, with zero missing files or mismatches. Windows (native host) negative control: with the final regression and unmodified base production code, `guest_flexible_memory_limited` fails at the full-budget interior-page replacement assertion. The preceding range-splitting checks pass. The final production source was restored and its SHA-256 verified before the final build. Windows (native host), one run per variant with the same final regression and a 16 MiB configured budget: | Check | Before (base production code) | After (fix) | |---|---|---| | Replace an existing interior 16 KiB page at a full budget | Fails with `ENOMEM` | Succeeds; adjacent data and the budget are preserved | | Environment / check | Result | |---|---| | Windows (native host), full default build including patched libraries | PASS | | Windows (native host), selected memory/runtime CTests | 24/24 PASS; 0 failed, 0 skipped (2.42 s) | | Linux (VirtualBox), full default application/library/test build | PASS | | Linux (VirtualBox), selected memory/runtime CTests | 24/24 PASS; 0 failed, 0 skipped (11.15 s) | The selected tests cover all five flexible-memory modes, guest mapping and fixed anonymous mapping, protection, memory-pool statistics, allocator failure handling, stacks, JIT/shared memory, page-table statistics, `mlock`, and patched libraries. Windows additionally runs write-watch; Linux (VirtualBox) additionally runs mapping lifetime. Five integration checks cover fiber/thread frame chains, user contexts (including return/exit), and Linux GNU segment preservation after the latest main updates. ```sh cmake --build build --parallel 6 ctest --test-dir build --output-on-failure -j 2 -R '^(patched_libraries|windows_write_watch|fiber|linux_gnu_segments|guest_(user_context(_exit)?|allocator_failures|kernel_(is_stack|stack_protect)|pthread_(frame_chain|attr_stack|stack)|jit_shared_memory|memory(_(lifetime|pool_stats))?|mmap_fixed_anonymous|mprotect_unmapped|flexible_memory_.*|mlock|page_table_stats))$' ``` Linux (VirtualBox) uses `--parallel 2` for the build and the same CTest selection. Conventions, whitespace, main-sync and test-registration audits passed; all 985 Windows and 997 Linux (VirtualBox) test executables have CTest registrations. These audit counts are not runtime test counts. Linux graphics/Vulkan tests: NOT RUN — intentionally excluded from VirtualBox validation. Native/bare-metal Linux was not tested. Native Linux GPU validation: not tested. No console, retail/homebrew title or performance result is claimed. Hosted full platform builds require collaborator dispatch. The shared-file review found no second implementation of this remaining accounting fix. #6515 covers direct replacements; #6044 moves the original budget fixture unchanged, so these new assertions must be preserved at its destination if it merges first. The macOS query/locking changes in #5021, #6222, #7201 and #7332 do not implement this budget credit. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 17:29:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T17:29:28Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 6044 core/libs/tests/GuestFlexibleMemoryBudget.cpp
PR 7105 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 7201 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 7332 core/libs/prx/libkernel/DirectMemory/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6198204760 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6491#issuecomment-6198204760). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T17:29:28Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestFlexibleMemoryBudget.cpp` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` |
jotitadev commented 2026-10-15 18:09:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T18:09:31Z.

Review: COMMENTED


Linux run, since the PR has no Linux runtime result.

Ubuntu 26.04, i7-8700, gcc 15.2, Release, BUILD_TESTING=ON, full build. Head 8752bd691 applied on main 03c4cb5e7.

  • Test only (the PR's GuestFlexibleMemoryBudget.cpp on main's source): guest_flexible_memory_limited fails at the first interior-replacement check (line 80) with available=15564800 expected=15581184. That is the same 16 KiB extra charge as in your Windows table.
  • With the fix: the 5 flexible-memory tests pass, and they pass again together with guest_memory* and direct_memory* (8 tests) 10 times in a row.
  • Non-GPU suite (649 tests, -E "^agc|gpu|vulkan|video_out", -j8): same result as main. Only file_writer_binary and file_writer_text fail, on main too (this host's coreutils rejects the command they run).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5537798872 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6491#pullrequestreview-5537798872). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T18:09:31Z. **Review:** COMMENTED --- Linux run, since the PR has no Linux runtime result. Ubuntu 26.04, i7-8700, gcc 15.2, Release, `BUILD_TESTING=ON`, full build. Head `8752bd691` applied on `main` 03c4cb5e7. - Test only (the PR's `GuestFlexibleMemoryBudget.cpp` on `main`'s source): `guest_flexible_memory_limited` fails at the first interior-replacement check (line 80) with `available=15564800 expected=15581184`. That is the same 16 KiB extra charge as in your Windows table. - With the fix: the 5 flexible-memory tests pass, and they pass again together with `guest_memory*` and `direct_memory*` (8 tests) 10 times in a row. - Non-GPU suite (649 tests, `-E "^agc|gpu|vulkan|video_out"`, `-j8`): same result as `main`. Only `file_writer_binary` and `file_writer_text` fail, on `main` too (this host's coreutils rejects the command they run). Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!6491
No description provided.