fix(libkernel): report live mapping bounds in sceKernelVirtualQuery after partial unmap #2267

Closed
saranxzi wants to merge 1 commit from upstream-pr/2267 into main
saranxzi commented 2026-10-09 14:03:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by saranxzi on 2026-10-09T14:03:40Z.


What

When an allocation was partially unmapped via munmap, GuestAllocations split the live pieces and updated their active extents (address and bytes) while preserving allocationAddress and allocationBytes for the root OS reservation release. sceKernelVirtualQuery previously checked allocationAddress and allocationBytes instead of address and bytes.

The resulting wrong extent is structural, not title-specific: sceKernelVirtualQuery returned the root reservation bounds for any partially unmapped allocation, reporting unmapped holes as mapped and failing to advance to subsequent pieces under SCE_KERNEL_VQ_FIND_NEXT.

Commercial title import evidence:

  • PPSA26344 imports sceKernelVirtualQuery from libkernel.prx (NID rVjRvHJ0X6c, recovered from the title's paired SELF binary).

Update sceKernelVirtualQuery to inspect address and bytes, returning SCE_KERNEL_ERROR_EACCES for unmapped addresses and correctly locating the next live segment when SCE_KERNEL_VQ_FIND_NEXT is passed.

Closes #1894

Tested

  • Windows 11 (x86_64) & Linux (x86_64)
  • Regression test CheckVirtualQueryPartialMunmap in GuestMemory.cpp acts as an exact differential check:
    • Arm A (PR head): guest_memory passes, binary exits 0.
    • Arm B (negative control with main's Export.cpp): aborts with exit code 134 at Require(first.end == reinterpret_cast<std::uintptr_t>(memory) + page), confirming the bug reproduces deterministically on the old code.
  • 39/39 CTest tests in build-relinker passed.
  • 23/23 Python tests in tools/tests passed.
  • tools/check_conventions.py passed with 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 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4801505183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267). Originally posted by [saranxzi](https://github.com/saranxzi) on 2026-10-09T14:03:40Z. --- ### What When an allocation was partially unmapped via `munmap`, `GuestAllocations` split the live pieces and updated their active extents (`address` and `bytes`) while preserving `allocationAddress` and `allocationBytes` for the root OS reservation release. `sceKernelVirtualQuery` previously checked `allocationAddress` and `allocationBytes` instead of `address` and `bytes`. The resulting wrong extent is structural, not title-specific: `sceKernelVirtualQuery` returned the root reservation bounds for any partially unmapped allocation, reporting unmapped holes as mapped and failing to advance to subsequent pieces under `SCE_KERNEL_VQ_FIND_NEXT`. Commercial title import evidence: - PPSA26344 imports `sceKernelVirtualQuery` from `libkernel.prx` (NID `rVjRvHJ0X6c`, recovered from the title's paired SELF binary). Update `sceKernelVirtualQuery` to inspect `address` and `bytes`, returning `SCE_KERNEL_ERROR_EACCES` for unmapped addresses and correctly locating the next live segment when `SCE_KERNEL_VQ_FIND_NEXT` is passed. Closes #1894 ### Tested - Windows 11 (x86_64) & Linux (x86_64) - Regression test `CheckVirtualQueryPartialMunmap` in `GuestMemory.cpp` acts as an exact differential check: - Arm A (PR head): `guest_memory` passes, binary exits 0. - Arm B (negative control with main's `Export.cpp`): aborts with exit code 134 at `Require(first.end == reinterpret_cast<std::uintptr_t>(memory) + page)`, confirming the bug reproduces deterministically on the old code. - 39/39 CTest tests in `build-relinker` passed. - 23/23 Python tests in `tools/tests` passed. - `tools/check_conventions.py` passed with 0 findings. ### 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
Dextroid17 commented 2026-10-09 15:05:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T15:05:34Z.

Review: COMMENTED


Verified on Linux. Base d70b8998 (upstream/main) + head 8073bda3, merged clean; guest_memory_tests removed and rebuilt first so a stale binary could not make the pass meaningless.

  • Arm A (PR head): guest_memory Passed, binary exits 0.

  • Arm B (only DirectMemory/Export.cpp reverted to main, the PR's test kept): aborts — exit 134, and it is the test's own assertion, not a crash elsewhere:

    Guest memory check failed at core/libs/tests/GuestMemory.cpp:1079
    (gdb: #3 Require(bool, std::source_location) <- #4 main)
    

    That line is Require(first.end == reinterpret_cast<std::uintptr_t>(memory) + page) — with main's implementation the query answers with the root reservation's extent, which is exactly the misreport in the description. So the test has a negative control that fires on the old code and reports its own location, which is more than most of the changes I look at manage.

Reading the change: all three call sites move from allocationAddress/allocationBytes to address/bytes — including the findNext comparison, which is the one that would have been easy to miss while still passing a naive test. Good.

The bar this has to clear, and the evidence I could not supply. A libkernel change lands with a title and the log line where that title goes wrong. I checked the one title I have a full import registry for — PPSA26344 (Ghost of Yotei): its executable does not import any *VirtualQuery* NID at all (0 matches over its 702 unique imports), so I cannot hand you the title evidence and I am not going to invent one. Which leaves two framings worth writing into the body:

  • a title that does call sceKernelVirtualQuery and misreports after a partial unmap, or
  • the universal one, stated with its measurement: the wrong extent is structural, not title-specific — the query reports the root reservation for any partially unmapped allocation, which is what the test demonstrates.

The second is a real argument here (a wrong answer returned to any caller), and the project has merged universal-impact changes when the measurement was shown — but it has to be said, because silence reads as "one synthetic fixture".

Not verified: any title runtime (no title reaches this path in the logs I have), and the Windows behaviour of the same code path.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5471832540 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#pullrequestreview-5471832540). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T15:05:34Z. **Review:** COMMENTED --- Verified on Linux. Base `d70b8998` (upstream/main) + head `8073bda3`, merged clean; `guest_memory_tests` removed and rebuilt first so a stale binary could not make the pass meaningless. - **Arm A (PR head):** `guest_memory` **Passed**, binary exits 0. - **Arm B (only `DirectMemory/Export.cpp` reverted to `main`, the PR's test kept):** **aborts** — `exit 134`, and it is the test's own assertion, not a crash elsewhere: ``` Guest memory check failed at core/libs/tests/GuestMemory.cpp:1079 (gdb: #3 Require(bool, std::source_location) <- #4 main) ``` That line is `Require(first.end == reinterpret_cast<std::uintptr_t>(memory) + page)` — with `main`'s implementation the query answers with the root reservation's extent, which is exactly the misreport in the description. So the test has a negative control that fires on the old code and reports its own location, which is more than most of the changes I look at manage. Reading the change: all three call sites move from `allocationAddress`/`allocationBytes` to `address`/`bytes` — including the `findNext` comparison, which is the one that would have been easy to miss while still passing a naive test. Good. **The bar this has to clear, and the evidence I could not supply.** A `libkernel` change lands with a title and the log line where that title goes wrong. I checked the one title I have a full import registry for — PPSA26344 (Ghost of Yotei): its executable **does not import any `*VirtualQuery*` NID at all** (0 matches over its 702 unique imports), so I cannot hand you the title evidence and I am not going to invent one. Which leaves two framings worth writing into the body: - a title that *does* call `sceKernelVirtualQuery` and misreports after a partial unmap, or - the universal one, stated with its measurement: the wrong extent is structural, not title-specific — the query reports the root reservation for *any* partially unmapped allocation, which is what the test demonstrates. The second is a real argument here (a wrong answer returned to any caller), and the project has merged universal-impact changes when the measurement was shown — but it has to be said, because silence reads as "one synthetic fixture". Not verified: any title runtime (no title reaches this path in the logs I have), and the Windows behaviour of the same code path. Written with AI assistance.
Dextroid17 commented 2026-10-09 15:08:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T15:08:42Z.


Correction to my review above — the title-evidence sentence was wrong, and it was wrong in your favour.

I wrote that PPSA26344 "does not import any *VirtualQuery* NID at all". That check was made by name against the import registry, and the registry names only 44 of its 702 imports — the other 658 are NID-only entries. So "0 matches" was an artefact of my method, not a fact about the title.

Resolved properly, by NID: PPSA26344 does import sceKernelVirtualQuery.

  registry entry:  nid rVjRvHJ0X6c   library libkernel.prx   references 1   class implemented
  pairing check:   unpatched/libkernel.prx  sceKernelVirtualQuery  @ 0x463d0 size 3526
                   libkernel.prx            rVjRvHJ0X6c            @ 0x463d0 size 3526

The mapping is recovered by pairing the two artefacts the build emits for every module — unpatched/*.prx still carries symbol names, *.prx carries the NIDs, and the same function sits at the same address in both. That yields 7,986 name↔NID pairs over 116 module pairs and names 683 of the 702 imports, where the name field alone named 44.

What this does for the bar: the title half is satisfiable and now evidenced — a title that imports sceKernelVirtualQuery is PPSA26344, NID rVjRvHJ0X6c, from libkernel.prx. What is still missing is the other half, the log line: an import is not a call, and I have no run of that title reaching a partial unmap and re-querying. If the author (or anyone) has that log, it is worth one line in the body; if not, the universal framing I described above is the honest alternative.

Sorry for the noise — the retraction is mine, and the corrected result is more useful than the one I posted first.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083635250 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6083635250). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T15:08:42Z. --- **Correction to my review above — the title-evidence sentence was wrong, and it was wrong in your favour.** I wrote that PPSA26344 "does not import any `*VirtualQuery*` NID at all". That check was made **by name** against the import registry, and the registry names only **44 of its 702 imports** — the other 658 are NID-only entries. So "0 matches" was an artefact of my method, not a fact about the title. Resolved properly, by NID: **PPSA26344 does import `sceKernelVirtualQuery`.** ``` registry entry: nid rVjRvHJ0X6c library libkernel.prx references 1 class implemented pairing check: unpatched/libkernel.prx sceKernelVirtualQuery @ 0x463d0 size 3526 libkernel.prx rVjRvHJ0X6c @ 0x463d0 size 3526 ``` The mapping is recovered by pairing the two artefacts the build emits for every module — `unpatched/*.prx` still carries symbol **names**, `*.prx` carries the **NIDs**, and the same function sits at the same address in both. That yields **7,986 name↔NID pairs** over 116 module pairs and names **683 of the 702** imports, where the name field alone named 44. What this does for the bar: the title half is satisfiable and now evidenced — *a title that imports `sceKernelVirtualQuery`* is PPSA26344, NID `rVjRvHJ0X6c`, from `libkernel.prx`. What is still missing is the other half, the log line: an import is not a call, and I have no run of that title reaching a partial unmap and re-querying. If the author (or anyone) has that log, it is worth one line in the body; if not, the universal framing I described above is the honest alternative. Sorry for the noise — the retraction is mine, and the corrected result is more useful than the one I posted first. Written with AI assistance.
saranxzi commented 2026-10-09 15:39:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by saranxzi on 2026-10-09T15:39:38Z.


Thank you for the thorough verification and the negative control check on Linux!

I have updated the PR description with the universal structural framing and the commercial title evidence you uncovered (PPSA26344 importing sceKernelVirtualQuery via NID rVjRvHJ0X6c from libkernel.prx).

As demonstrated by the differential check in GuestMemory.cpp, the defect is structural across any caller: querying after a partial unmap was answering with the root OS reservation instead of the live sub-allocation extents, treating decommitted holes as valid memory.

Thanks again for the NID pairing analysis and review!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084160429 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6084160429). Originally posted by [saranxzi](https://github.com/saranxzi) on 2026-10-09T15:39:38Z. --- Thank you for the thorough verification and the negative control check on Linux! I have updated the PR description with the universal structural framing and the commercial title evidence you uncovered (PPSA26344 importing `sceKernelVirtualQuery` via NID `rVjRvHJ0X6c` from `libkernel.prx`). As demonstrated by the differential check in `GuestMemory.cpp`, the defect is structural across any caller: querying after a partial unmap was answering with the root OS reservation instead of the live sub-allocation extents, treating decommitted holes as valid memory. Thanks again for the NID pairing analysis and review!
Dextroid17 commented 2026-10-09 19:59:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T19:59:50Z.


Thank you for turning the framing around this quickly, and for giving the NID pairing its own line in the description — PPSA26344 importing sceKernelVirtualQuery via rVjRvHJ0X6c from libkernel.prx is the part a maintainer can act on, and it is the part that was missing before.

One precision worth keeping, since it is my evidence and I would rather it be accurate: that dump's eboot.bin is a PS4-format SELF (PPSA title IDs ship both formats), so the import is read out of the PS4 binary. NIDs are library-level and identical across the two, so the pairing applies either way — but if a PS5-binary sample turns up it would be a stronger citation than mine.

The differential check in GuestMemory.cpp is what convinced me, and describing the defect as structural across any caller rather than as a single title's symptom is the right call.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088288760 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6088288760). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T19:59:50Z. --- Thank you for turning the framing around this quickly, and for giving the NID pairing its own line in the description — `PPSA26344` importing `sceKernelVirtualQuery` via `rVjRvHJ0X6c` from `libkernel.prx` is the part a maintainer can act on, and it is the part that was missing before. One precision worth keeping, since it is my evidence and I would rather it be accurate: that dump's `eboot.bin` is a **PS4-format** SELF (PPSA title IDs ship both formats), so the import is read out of the PS4 binary. NIDs are library-level and identical across the two, so the pairing applies either way — but if a PS5-binary sample turns up it would be a stronger citation than mine. The differential check in `GuestMemory.cpp` is what convinced me, and describing the defect as structural across any caller rather than as a single title's symptom is the right call. Written with AI assistance.
saranxzi commented 2026-10-09 20:06:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by saranxzi on 2026-10-09T20:06:12Z.


Thanks for the clarification on the binary format! That makes complete sense regarding the dual-format SELFs under PPSA IDs and how the library-level NID pairing holds across both.

I really appreciate your thorough verification, the Linux differential check, and helping refine the evidence for this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088386450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6088386450). Originally posted by [saranxzi](https://github.com/saranxzi) on 2026-10-09T20:06:12Z. --- Thanks for the clarification on the binary format! That makes complete sense regarding the dual-format SELFs under PPSA IDs and how the library-level NID pairing holds across both. I really appreciate your thorough verification, the Linux differential check, and helping refine the evidence for this change.
MarcZX10 commented 2026-10-09 22:08:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T22:08:05Z.


The fresh overlap check flagged #1929, and the source diffs do overlap in sceKernelVirtualQuery: both switch the reported bounds from allocation extent to the live range address/bytes. #1929 also adds a flexible-range split/mprotect/partial-unmap test and a non-readable mapping test; #2267 adds a direct mmap partial-unmap regression, which may provide distinct coverage. Could you and the maintainers coordinate whether #1929 subsumes the implementation while the direct-mmap test is carried with attribution, or whether a separate behavior remains? I have not changed your branch or closed either PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090063728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6090063728). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T22:08:05Z. --- The fresh overlap check flagged #1929, and the source diffs do overlap in `sceKernelVirtualQuery`: both switch the reported bounds from allocation extent to the live range `address`/`bytes`. #1929 also adds a flexible-range split/mprotect/partial-unmap test and a non-readable mapping test; #2267 adds a direct `mmap` partial-unmap regression, which may provide distinct coverage. Could you and the maintainers coordinate whether #1929 subsumes the implementation while the direct-mmap test is carried with attribution, or whether a separate behavior remains? I have not changed your branch or closed either PR.
github-actions[bot] commented 2026-10-09 22:08:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:08:43Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1929 core/libs/tests/GuestMemory.cpp L1064-1066
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090071873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6090071873). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:08:43Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1929 | `core/libs/tests/GuestMemory.cpp` [L1064-1066](https://github.com/boykopovar/AnyPS5/blob/64fe74399528c84fd74c3f736e632f89f4a7f211/core/libs/tests/GuestMemory.cpp#L1064-L1066) | | |
MarcZX10 commented 2026-10-09 22:15:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T22:15:05Z.


To avoid maintaining two overlapping fixes for #1894, I carried your direct-mmap regression test into Marc’s existing PR #1929 as commit e06c4595; Git records you, Saranxzi, as that commit’s author. #1929 is the better carrier because it includes the live-extent fix plus coverage for split flexible ranges and non-readable mappings, and now also includes your test. On current main e51d4e1f, the combined guest_memory CTest passes (1/1), and conventions/diff checks pass. Your reported Windows/Linux results remain attributed to your original head. Please consider closing #2267 after reviewing the consolidation; I have not changed your branch or closed the PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090146992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6090146992). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T22:15:05Z. --- To avoid maintaining two overlapping fixes for #1894, I carried your direct-`mmap` regression test into Marc’s existing PR #1929 as commit `e06c4595`; Git records you, Saranxzi, as that commit’s author. #1929 is the better carrier because it includes the live-extent fix plus coverage for split flexible ranges and non-readable mappings, and now also includes your test. On current `main` `e51d4e1f`, the combined `guest_memory` CTest passes (1/1), and conventions/diff checks pass. Your reported Windows/Linux results remain attributed to your original head. Please consider closing #2267 after reviewing the consolidation; I have not changed your branch or closed the PR.
oneandonlydean commented 2026-10-09 22:34:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T22:34:32Z.


Closing as a duplicate of #1929, which makes the same sceKernelVirtualQuery change for #1894 and now carries your partial-unmap test (e06c4595, with you as its author).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090385911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2267#issuecomment-6090385911). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T22:34:32Z. --- Closing as a duplicate of #1929, which makes the same `sceKernelVirtualQuery` change for #1894 and now carries your partial-unmap test (e06c4595, with you as its author).

Pull request closed

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!2267
No description provided.