fix(libkernel): report a reservation that find-next lands on as a reservation #27588

Open
brajankw wants to merge 1 commit from upstream-pr/27588 into main
brajankw commented 2026-11-26 18:49:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-26T18:49:47Z.


What

Problem: a find-next sceKernelVirtualQuery (flags bit 0) that lands on a virtual reservation reports it as committed flexible memory, while a direct query of the reservation's start reports it as a reservation (not committed, not flexible).

Cause: QueryVirtualRange checks GuestReservation only for the queried address. When find-next skips forward, the allocation registry returns the next allocation, and a reservation is registered there like any allocation, so the reply was built from the registry record.

Fix: when find-next returns an allocation above the queried address that GuestReservation reports as reserved, the reply is the reserved range, the same answer a direct query of that address gives. The reply is built the same way for every reservation, so this changes the find-next answer for every title, not only for one memory window. A mapping registered inside a reservation is still reported from its own record.

guest_memory gains CheckVirtualQueryFindsNextReservation: find-next from a free page below a user reservation must return exactly what a direct query of the address it lands on returns, for an untouched reservation, for one whose first half carries a flexible map, and for the reserved half left after that map is unmapped.

tools/pr_overlap.py --branch found no PR with the same lines or exports; it could not compare #21569 (range-name bounds), which edits other functions in the same two files.

Tested

  • Windows 11 Pro 26200, AMD Ryzen 9 9950X3D, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh r7), Release, -DBUILD_TESTING=ON:
    ctest --test-dir build -j6 --output-on-failure -R "^(guest_memory|guest_mmap_.*|guest_virtual_query_arguments|guest_amm_placement|guest_amm_pool|guest_ampr_markers|MemoryPoolTest)$": 8/8 passed.
  • Linux (Debian 13, kernel 6.12, Intel Core i5-13500T), GCC 14.2.0, Release, -DBUILD_TESTING=ON: the same command: 8/8 passed.
  • No title run.

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

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Virtual memory range queries now report the next guest reservation when it begins after the queried address, including uncommitted and partially committed ranges.
    • The reported range reflects the reservation’s bounds instead of a later allocation when a reservation is found. Existing allocation and host-region query behavior remains unchanged.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5515534648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27588). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-26T18:49:47Z. --- ### What Problem: a find-next `sceKernelVirtualQuery` (flags bit 0) that lands on a virtual reservation reports it as committed flexible memory, while a direct query of the reservation's start reports it as a reservation (not committed, not flexible). Cause: `QueryVirtualRange` checks `GuestReservation` only for the queried address. When find-next skips forward, the allocation registry returns the next allocation, and a reservation is registered there like any allocation, so the reply was built from the registry record. Fix: when find-next returns an allocation above the queried address that `GuestReservation` reports as reserved, the reply is the reserved range, the same answer a direct query of that address gives. The reply is built the same way for every reservation, so this changes the find-next answer for every title, not only for one memory window. A mapping registered inside a reservation is still reported from its own record. `guest_memory` gains `CheckVirtualQueryFindsNextReservation`: find-next from a free page below a user reservation must return exactly what a direct query of the address it lands on returns, for an untouched reservation, for one whose first half carries a flexible map, and for the reserved half left after that map is unmapped. `tools/pr_overlap.py --branch` found no PR with the same lines or exports; it could not compare #21569 (range-name bounds), which edits other functions in the same two files. ### Tested - Windows 11 Pro 26200, AMD Ryzen 9 9950X3D, WinLibs GCC 15.2.0 (`x86_64-ucrt-posix-seh` r7), Release, `-DBUILD_TESTING=ON`: `ctest --test-dir build -j6 --output-on-failure -R "^(guest_memory|guest_mmap_.*|guest_virtual_query_arguments|guest_amm_placement|guest_amm_pool|guest_ampr_markers|MemoryPoolTest)$"`: 8/8 passed. - Linux (Debian 13, kernel 6.12, Intel Core i5-13500T), GCC 14.2.0, Release, `-DBUILD_TESTING=ON`: the same command: 8/8 passed. - No title run. ### 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) 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Virtual memory range queries now report the next guest reservation when it begins after the queried address, including uncommitted and partially committed ranges. * The reported range reflects the reservation’s bounds instead of a later allocation when a reservation is found. Existing allocation and host-region query behavior remains unchanged. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-26 19:35:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T19:35:25Z.

Review: APPROVED


Reviewed — looks good

The changed implementation and test additions are complete and support merging at head ca688dabfa93a7fc4f3d88b78f809abca4f6cdeb; no checks were run in this review.

What I ran

Read the prepared diff and all changed lines, plus the relevant GuestReservation implementation. No build or tests were run.

Results

Check Result Note
Changed source review passed Implementation and test changes read
Build and tests not run Author reports 8/8 on Windows and Linux

Findings

Blocking

None.

Non-blocking

None.

Not verified

No commands, build, or tests were run during this review. Title behavior was not tested. The author reports 8/8 listed tests on Windows and Linux; those results were not independently verified. Both changed files listed in the manifest were read; files.json was not present in the review bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6079602888 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27588#pullrequestreview-6079602888). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T19:35:25Z. **Review:** APPROVED --- # Reviewed — looks good The changed implementation and test additions are complete and support merging at head `ca688dabfa93a7fc4f3d88b78f809abca4f6cdeb`; no checks were run in this review. ## What I ran Read the prepared diff and all changed lines, plus the relevant `GuestReservation` implementation. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | Implementation and test changes read | | Build and tests | not run | Author reports 8/8 on Windows and Linux | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No commands, build, or tests were run during this review. Title behavior was not tested. The author reports 8/8 listed tests on Windows and Linux; those results were not independently verified. Both changed files listed in the manifest were read; `files.json` was not present in the review bundle. ## To get this to a good state Nothing is required.
Plyxen commented 2026-11-27 03:11:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-27T03:11:20Z.

Review: APPROVED


Checked head ca688dabfa. I applied it to main ad7582bad4 together with #27587 and #27585, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release), tools/check_conventions.py --head on each head and tools/debt_fold.py --check passed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests).

guest_memory passed. With main's DirectMemory/Export.cpp and the new checks, it fails.

When a find-next query lands on an allocation that starts inside a reservation, it now returns the reservation's range with the other fields zero. That is the answer a direct query at that address already gives, and the test pins the two as equal with memcmp. A flexible mapping that starts the reservation is still returned as the mapping, and once it is unmapped, find-next lands on the remaining reserved fragment, as the test checks.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6082969216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27588#pullrequestreview-6082969216). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-27T03:11:20Z. **Review:** APPROVED --- Checked head `ca688dabfa`. I applied it to `main` `ad7582bad4` together with #27587 and #27585, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release), `tools/check_conventions.py --head` on each head and `tools/debt_fold.py --check` passed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests). `guest_memory` passed. With `main`'s `DirectMemory/Export.cpp` and the new checks, it fails. When a find-next query lands on an allocation that starts inside a reservation, it now returns the reservation's range with the other fields zero. That is the answer a direct query at that address already gives, and the test pins the two as equal with `memcmp`. A flexible mapping that starts the reservation is still returned as the mapping, and once it is unmapped, find-next lands on the remaining reserved fragment, as the test checks. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 14:12:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:12:30Z.

Review: COMMENTED


Built and ran guest_memory on Linux with this head integrated. It passed, including the find-next and direct-query comparison for untouched, partly mapped, and unmapped reservation ranges. The assertions cover the returned range and its committed and flexible status.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171448599 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27588#pullrequestreview-6171448599). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T14:12:30Z. **Review:** COMMENTED --- Built and ran `guest_memory` on Linux with this head integrated. It passed, including the find-next and direct-query comparison for untouched, partly mapped, and unmapped reservation ranges. The assertions cover the returned range and its committed and flexible status. 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/27588:upstream-pr/27588'
git switch 'upstream-pr/27588'

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