test(libkernel): audit virtual memory management (madvise, mprotect) bounds and page alignment limits #20978

Open
slopgobblercobbler wants to merge 2 commits from upstream-pr/20978 into main
slopgobblercobbler commented 2026-11-15 18:56:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-11-15T18:56:34Z.


What

Fold new virtual memory management edge cases into core/libs/tests/GuestMemory.cpp per maintainer feedback:

  • mprotect with SIZE_MAX length returning -1 with EINVAL
  • Address plus length overflow in mprotect returning -1 with EINVAL
  • Undefined protection bits 0x8000 and 0x10000 masked and preserved
  • sceKernelMprotect with length 0 returning 0
  • madvise with advice 100 returning -1 with EINVAL

Tested

  • Ran conventions check python tools/check_conventions.py --base upstream/main: 0 findings.

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:5326027696 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-15T18:56:34Z. --- ### What Fold new virtual memory management edge cases into `core/libs/tests/GuestMemory.cpp` per maintainer feedback: - `mprotect` with `SIZE_MAX` length returning -1 with `EINVAL` - Address plus length overflow in `mprotect` returning -1 with `EINVAL` - Undefined protection bits `0x8000` and `0x10000` masked and preserved - `sceKernelMprotect` with length 0 returning 0 - `madvise` with advice 100 returning -1 with `EINVAL` ### Tested - Ran conventions check `python tools/check_conventions.py --base upstream/main`: 0 findings. ### 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
PrivacyOnion commented 2026-11-16 01:29:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T01:29:49Z.

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_kernel_virtual_memory_bounds_tests built cleanly.
  • CTest (guest_kernel_virtual_memory_bounds):
    1/1 Test #5362: guest_kernel_virtual_memory_bounds ...Subprocess aborted***Exception:   0.39 sec
    0% tests passed, 1 tests failed out of 1
    
    The following tests FAILED:
    

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914061068 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978#pullrequestreview-5914061068). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T01:29:49Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d23d59ad41`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_kernel_virtual_memory_bounds_tests` built cleanly. - **CTest** (`guest_kernel_virtual_memory_bounds`): ``` 1/1 Test #5362: guest_kernel_virtual_memory_bounds ...Subprocess aborted***Exception: 0.39 sec 0% tests passed, 1 tests failed out of 1 ``` ``` The following tests FAILED: ``` Requesting changes for the points above.
PrivacyOnion commented 2026-11-17 18:14:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T18:14:30Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 3a215f44ec:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_virtual_memory_bounds_tests built cleanly.
  • CTest (guest_kernel_virtual_memory_bounds):
    1/1 Test #5638: guest_kernel_virtual_memory_bounds ...   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Verified PosixMemory.cpp mprotect/madvise/posix_madvise bounds handling with nullptr, zero length, and page alignment behavior against GuestKernelVirtualMemoryBounds.cpp
    • Verified that mlock/munlock/mlockall cases were completely removed from the test suite per maintainer oneandonlydean instructions
    • Verified that GuestKernelVirtualMemoryBounds.cpp handles errno/error return checks without leaving modified memory unmapped or dirty
    • Verified CMakeLists.txt target registration, test properties, and WIN32 environment modification consistency
    • Verified AGENTS.md compliance: no comments in source code, no unauthorized files, clean convention checks

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5944083125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978#pullrequestreview-5944083125). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T18:14:30Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `3a215f44ec`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_virtual_memory_bounds_tests` built cleanly. - **CTest** (`guest_kernel_virtual_memory_bounds`): ``` 1/1 Test #5638: guest_kernel_virtual_memory_bounds ... Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 ``` - **Code reading**: - Verified PosixMemory.cpp mprotect/madvise/posix_madvise bounds handling with nullptr, zero length, and page alignment behavior against GuestKernelVirtualMemoryBounds.cpp - Verified that mlock/munlock/mlockall cases were completely removed from the test suite per maintainer oneandonlydean instructions - Verified that GuestKernelVirtualMemoryBounds.cpp handles errno/error return checks without leaving modified memory unmapped or dirty - Verified CMakeLists.txt target registration, test properties, and WIN32 environment modification consistency - Verified AGENTS.md compliance: no comments in source code, no unauthorized files, clean convention checks Looks Good To Me
MarcZX10 commented 2026-11-20 20:41:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:41:37Z.

Review: COMMENTED


Reviewed — no blocking findings

The change adds a standalone virtual memory bounds test and its CTest registration; I found no verified blocking defect in the code read.

What I ran

Inspected the prepared diff and changed source at head 57cd45d5336f8c2f06b024578e303ee941b1cc5f using repo_read.py. No test command was run.

Results

Check Result Note
Changed source review passed Both changed files read
CTest not run No test execution available

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports running ctest --test-dir build -R guest_kernel_virtual_memory_bounds_tests; I did not run it. No repository files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5999080347 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978#pullrequestreview-5999080347). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T20:41:37Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The change adds a standalone virtual memory bounds test and its CTest registration; I found no verified blocking defect in the code read. ## What I ran Inspected the prepared diff and changed source at head `57cd45d5336f8c2f06b024578e303ee941b1cc5f` using `repo_read.py`. No test command was run. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | Both changed files read | | CTest | not run | No test execution available | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author reports running `ctest --test-dir build -R guest_kernel_virtual_memory_bounds_tests`; I did not run it. No repository files were excluded by the manifest. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 01:42:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:42:34Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the added assertions match the current implementations for zero-length protection, overflow validation, ignored protection bits, and invalid advice values.

What I ran

Reviewed head 117f2a2832586554d1cbaabe81bcb8b8f195884d. No commands or tests were run.

Results

Check Result Note
Source review passed Added assertions checked against implementations
Tests not run No test execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

The test executable was not run. Reviewed core/libs/tests/GuestMemory.cpp and the relevant implementations in core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp and core/libs/prx/libkernel/DirectMemory/Export.cpp. No changed files were excluded; files.json was not available through the review reader.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6001908168 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978#pullrequestreview-6001908168). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T01:42:34Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the added assertions match the current implementations for zero-length protection, overflow validation, ignored protection bits, and invalid advice values. ## What I ran Reviewed head `117f2a2832586554d1cbaabe81bcb8b8f195884d`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Added assertions checked against implementations | | Tests | not run | No test execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The test executable was not run. Reviewed `core/libs/tests/GuestMemory.cpp` and the relevant implementations in `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` and `core/libs/prx/libkernel/DirectMemory/Export.cpp`. No changed files were excluded; `files.json` was not available through the review reader. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 17:26:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T17:26:33Z.

Review: COMMENTED


The undefined-bit cases call both APIs with read/write protection just after the fixture has already restored read/write protection. Their return-code and writable assertions would also pass if either call ignored the whole request. Can each check start from a different protection and verify the resulting permissions immediately after the call? That would show that the known bits still take effect when the unknown bit is present.

This finding is from source inspection. I confirmed the branch conflicts in GuestMemory.cpp against main at 9fcd37e4ea, which already has a SIZE_MAX protection-length check.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6174950533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20978#pullrequestreview-6174950533). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T17:26:33Z. **Review:** COMMENTED --- The undefined-bit cases call both APIs with read/write protection just after the fixture has already restored read/write protection. Their return-code and writable assertions would also pass if either call ignored the whole request. Can each check start from a different protection and verify the resulting permissions immediately after the call? That would show that the known bits still take effect when the unknown bit is present. This finding is from source inspection. I confirmed the branch conflicts in `GuestMemory.cpp` against `main` at `9fcd37e4ea`, which already has a `SIZE_MAX` protection-length check. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20978:upstream-pr/20978'
git switch 'upstream-pr/20978'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20978'
git switch 'upstream-pr/20978'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20978'
git switch 'upstream-pr/20978'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20978'
git switch 'main'
git merge --squash 'upstream-pr/20978'
git switch 'main'
git merge --ff-only 'upstream-pr/20978'
git switch 'main'
git merge 'upstream-pr/20978'
git push origin 'main'
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!20978
No description provided.