fix(libkernel): track committed ranges accurately in memory pool block stats #23301

Merged
gurtgg merged 3 commits from upstream-pr/23301 into main 2026-11-27 02:48:03 +00:00
gurtgg commented 2026-11-18 21:47:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-18T21:47:42Z.


What

  • Track committed virtual address ranges in a merged interval map (committedRanges) in core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp so overlapping PoolCommit calls and partial/repeated PoolDecommit calls maintain exact committedBytes accounting without double-counting or underflow.
  • Reject addr + len address wraparound (len > UINTPTR_MAX - lo) in PoolCommit and PoolDecommit.
  • Rely on GuestCopyOut_nid_no_patch's built-in errno preservation in sceKernelMemoryPoolExpand.

Tested

  • guest_memory_pool (core/libs/tests/GuestMemoryPool.cpp): covers overlapping commits, partial/interior/duplicate decommits, address overflow, and sceKernelMemoryPoolExpand unwritable output pointer cleanup.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5384652755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23301). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-18T21:47:42Z. --- ### What - Track committed virtual address ranges in a merged interval map (`committedRanges`) in `core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp` so overlapping `PoolCommit` calls and partial/repeated `PoolDecommit` calls maintain exact `committedBytes` accounting without double-counting or underflow. - Reject `addr + len` address wraparound (`len > UINTPTR_MAX - lo`) in `PoolCommit` and `PoolDecommit`. - Rely on `GuestCopyOut_nid_no_patch`'s built-in `errno` preservation in `sceKernelMemoryPoolExpand`. ### Tested - `guest_memory_pool` (`core/libs/tests/GuestMemoryPool.cpp`): covers overlapping commits, partial/interior/duplicate decommits, address overflow, and `sceKernelMemoryPoolExpand` unwritable output pointer cleanup. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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
PrivacyOnion commented 2026-11-18 21:53:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T21:53:48Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 46c2696a4c:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_memory_pool_stats_tests built cleanly.
  • CTest (guest_memory_pool_stats):
    1/1 Test #976: guest_memory_pool_stats ..........   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #976: guest_memory_pool_stats ..........Subprocess aborted***Exception:   0.22 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Analyzed AddCommittedRange in core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp for lower_bound iterator invalidation and range coalescing. When target is emplace_hint-inserted with [lo, lo], mergedHi computation and loop over std::next(target) properly merges overlapping and adjacent intervals and increments committedBytes by exact delta.
    • Analyzed RemoveCommittedRange in core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp: checked lower_bound right split of intervals crossing hi, lower_bound lo lookup, overlap subtraction, interval truncation and erasure. Bounds and committedBytes accounting are exact.
    • Verified PoolCommit and PoolDecommit integer overflow checks (len > UINTPTR_MAX - lo), page alignment checks, null pointer checks, and lock acquisition with pool.mutex.
    • Verified errno preservation around GuestCopyOut_nid_no_patch in sceKernelMemoryPoolExpand and sceKernelMemoryPoolGetBlockStats.
    • Checked cross-platform LP64/LLP64 portability and 64-bit integer types across Linux and Windows.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5965782770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23301#pullrequestreview-5965782770). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T21:53:48Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `46c2696a4c`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_memory_pool_stats_tests` built cleanly. - **CTest** (`guest_memory_pool_stats`): ``` 1/1 Test #976: guest_memory_pool_stats .......... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #976: guest_memory_pool_stats ..........Subprocess aborted***Exception: 0.22 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Analyzed AddCommittedRange in core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp for lower_bound iterator invalidation and range coalescing. When target is emplace_hint-inserted with [lo, lo], mergedHi computation and loop over std::next(target) properly merges overlapping and adjacent intervals and increments committedBytes by exact delta. - Analyzed RemoveCommittedRange in core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp: checked lower_bound right split of intervals crossing hi, lower_bound lo lookup, overlap subtraction, interval truncation and erasure. Bounds and committedBytes accounting are exact. - Verified PoolCommit and PoolDecommit integer overflow checks (len > UINTPTR_MAX - lo), page alignment checks, null pointer checks, and lock acquisition with pool.mutex. - Verified errno preservation around GuestCopyOut_nid_no_patch in sceKernelMemoryPoolExpand and sceKernelMemoryPoolGetBlockStats. - Checked cross-platform LP64/LLP64 portability and 64-bit integer types across Linux and Windows. Looks Good To Me
MarcZX10 commented 2026-11-23 04:54:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T04:54:16Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the review evidence supports merging.

What I ran

Reviewed head 46c2696a4cc60d637c0c8b3695e7e183bab4281d against the supplied diff. I did not run commands or tests; this review environment provides read-only repository access through repo_read.py.

Results

Check Result Note
Diff and changed code passed Both changed files were read.
Tests not run No test execution was available.

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests, build, and runtime behavior were not verified. Both files listed as reviewed in manifest.json were read: core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp and core/libs/tests/GuestMemoryPoolStats.cpp. No changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016735549 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23301#pullrequestreview-6016735549). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T04:54:16Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the review evidence supports merging. ## What I ran Reviewed head `46c2696a4cc60d637c0c8b3695e7e183bab4281d` against the supplied diff. I did not run commands or tests; this review environment provides read-only repository access through `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Both changed files were read. | | Tests | not run | No test execution was available. | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests, build, and runtime behavior were not verified. Both files listed as reviewed in `manifest.json` were read: `core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp` and `core/libs/tests/GuestMemoryPoolStats.cpp`. No changed files were excluded. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-25 14:12:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T14:12:42Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head d7e34c81a8ceb9d9d793da6bc40259cef78757cd. No tests or build commands were run.

Results

Check Result Note
Code review passed Changed implementation and test read
Automated checks not run No build or test execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were executed. No files were excluded by the manifest; files.json was unavailable. The complete contents of the changed implementation and test files were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6059203610 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23301#pullrequestreview-6059203610). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T14:12:42Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `d7e34c81a8ceb9d9d793da6bc40259cef78757cd`. No tests or build commands were run. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Changed implementation and test read | | Automated checks | not run | No build or test execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were executed. No files were excluded by the manifest; `files.json` was unavailable. The complete contents of the changed implementation and test files were read. ## To get this to a good state Nothing is required.
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!23301
No description provided.