Skip to content

fix(libkernel): report is_committed from sceKernelVirtualQuery only for direct memory, as a PS5 does - #22013

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-virtual-query-committed-direct-only
Nov 29, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-virtual-query-committed-direct-only

Conversation

@brajankw

@brajankw brajankw commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

sceKernelVirtualQuery set is_committed for every registered or host-queried range. A PS5 payload (firmware 13.590.001) reports it only for direct memory, and 0 for flexible memory whether touched or not, stacks, images and reservations (the sceKernelVirtualQuery size and flags entry in docs/dev/debt/libkernel.md, which left it because mincore read the field).

  • QueryVirtualRange sets is_committed only for a direct mapping; host-queried memory (stacks, the title's own heap) now reports 0 too.
  • mincore was the only reader: it used is_committed to tell a mapping from a reservation. It now asks GuestReservation directly, so every mapped page is still resident and a reserved page is not.
  • GuestMemory.cpp: flexible pages and a stack address report is_committed 0; the 72-byte wire layout's flag byte is 0x01 (flexible) for flexible memory; mincore over a direct page followed by a reserved one gives 1, 0, also after the direct page is set to protection 0, which still reports is_committed; pages of the registered main image (a static global and a function) report neither committed, flexible nor direct. The existing mincore checks on anonymous mmap memory still expect 1.
  • The debt fragment drops the is_committed gap from the entry and notes that pooled memory was not measured.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Head 8f161a08b2, rebased onto main 11c9e7764e without conflicts. The runs after the first two bullets are from the earlier head on main ab5089590a.

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

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_memory_tests built cleanly.
  • CTest (guest_memory, guest_mmap_fixed_anonymous, guest_mmap_executable):
    1/3 Test #5420: guest_mmap_executable ............   Passed    0.04 sec
    2/3 Test #5419: guest_mmap_fixed_anonymous .......   Passed    0.03 sec
    3/3 Test #5418: guest_memory .....................   Passed    4.82 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #5420: guest_mmap_executable ............   Passed    0.03 sec
    2/3 Test #5419: guest_mmap_fixed_anonymous .......   Passed    0.03 sec
    3/3 Test #5418: guest_memory .....................Subprocess aborted***Exception:   0.61 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in Export.cpp:QueryVirtualRange that info->is_committed is correctly set to 1u for direct memory mappings and 0u for flexible/host/other ranges, adhering to real PS5 firmware measurements.
    • Verified in PosixMemory.cpp:mincore_nid_postfix that residency is determined by GuestReservation(pageCursor, &reservedStart, &reservedEnd), reporting resident (1) for mapped non-reserved pages and non-resident (0) for reservations.
    • Verified GuestReservation usage and pointer arguments (&reservedStart, &reservedEnd) against DirectMemory.hpp declaration.
    • Verified debt fragment docs/dev/debt/changes/fix-libkernel-virtual-query-committed-direct-only.md matches AGENTS.md conventions and documentation guidelines.
    • Verified test cases in GuestMemory.cpp for wire layout flags, flexible memory query, stack address query, and mincore over direct+reserved ranges.

Looks Good To Me

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

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 16372 core/libs/tests/GuestMemory.cpp
PR 18190 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
PR 21569 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp

This was referenced Nov 21, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the inspected code implements the reported direct-memory-only is_committed behavior and checks reservations directly in mincore.

What I ran

Reviewed head b56a3a67ac1125908dd341621250c28c23578a5d. No commands or tests were run in this review; repo_read.py was used to inspect the prepared diff and targeted source ranges. The description reports Linux checks, which were not run by me.

Results

Check Result Note
Diff and targeted source review passed Four changed files inspected
Tests in this review not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of all four changed files was read, as shown in manifest.json. The behavior of the PS5 payload, title process, pooled memory, Windows and macOS, and author-reported test results were not independently verified. Excluded files: none are listed in the prepared manifest; files.json was not available through the reader.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows run at b56a3a67ac, merged onto main 13be6afb21 as 67c91c1840. The merge passes the 12 memory and VirtualQuery tests three times, and the PR's own test fails on main at the first flexible is_committed check, as the description says. Seven mutations were run: five are caught and two survive. Each survivor marks a gap that a short check would close, and probes for both pass on the merge.

Existing runs: PrivacyOnion ran guest_memory, guest_mmap_fixed_anonymous and guest_mmap_executable on Linux (Ubuntu 24.04, GCC 13.3) at b56a3a6, and they passed. MarcZX10 read the code at the same head without running it. This run adds Windows, the negative control, the mutations and the probes.

Results

Tests: guest_memory, guest_mmap_executable, guest_mmap_fixed_anonymous, guest_virtual_query_arguments, guest_kernel_is_stack, guest_pthread_stack, guest_low_address_memory, guest_mprotect_unmapped, guest_protection_report, guest_system_flexible_memory, guest_memory_pool_stats, guest_memory_resource.

Tree Result
main 12/12
main + this PR's test hunks guest_memory fails at the flexible is_committed check (L4195)
merge, runs 1-3 12/12 each time

The control uses main's GuestMemory.cpp plus only this PR's test hunks. The head's whole GuestMemory.cpp is from before main's CheckMappedLeasesPreserveReadableRuns change, so on main 13be6af it fails earlier, at that function's lease check (L4137 of the head file), not at this PR's checks.

check_conventions --base 13be6afb21 and debt_fold --check pass, and debt_fold --show libkernel folds the fragment into the sceKernelVirtualQuery size and flags entry.

Mutations

Line numbers are in the head files.

Change Result
M1 registry branch sets is_committed = 1 again (Export.cpp L487) caught at L4195
M2 host-queried branch sets is_committed = 1 again, next to is_flexible = 1 (L545) caught at the stack check, L4199
M3 mincore reads info.is_committed again (PosixMemory.cpp L384) caught at the anonymous mmap check, L4895
M4 mincore reports every page resident caught at L1623
M5 mincore checks the reservation at the chunk start instead of at pageCursor caught at L1623
M6 registry branch sets is_committed = is_flexible ? 0 : 1 survives
M7 mincore treats protection == 0 as not resident survives

Survivors

  • M6. No test queries a registry range that is neither direct nor flexible, so an image can report committed without any test failing. The test executable's own data and code pages are not registered, so the query takes the host branch and reports flexible=1. A probe was appended to the end of the test's main(). It calls GuestAllocations::Mutation{}.RegisterMainImage() and then queries a static global and a function in the image.
    • On the merge, both pages report flexible=0 direct=0 committed=0. This matches the "images" case the debt entry gives for the PS5.
    • With M6 applied, both report committed=1 and the probe's check fails.
  • M7. No test runs mincore over a mapped page with no access, and a reservation also reports protection 0. A probe was added after the new mincore check in CheckReservedRangeIsNotCommitted (after L1623). It sets the direct page to protection 0 with sceKernelMprotect(fixed, page, 0), then runs mincore over the direct page and the reserved page.
    • On the merge, VirtualQuery gives protection=0 committed=1 and mincore gives 1,0.
    • With M7 applied, mincore gives 0,0 and the probe's check fails.

Host

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release, -O3 -DNDEBUG in CMAKE_CXX_FLAGS_RELEASE
  • Commits: head b56a3a6, main 13be6af, merge 67c91c1840. main has since moved 63 commits to adacf0bd83, none of them in the files cited here, and the head still merges cleanly.

AI-assisted: yes

…or direct memory, as a PS5 does

sceKernelVirtualQuery set is_committed for every registered or
host-queried range. A PS5 payload (firmware 13.590.001) reports it only
for direct memory, and 0 for flexible memory whether touched or not,
stacks, images and reservations. It is now set only for a direct
mapping.

mincore was the only reader of is_committed and used it to tell a
mapping from a reservation, so it now asks GuestReservation directly
and still reports every mapped page as resident and a reserved one as
not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brajankw

Copy link
Copy Markdown
Contributor Author

@Helldiverr Added both probes as checks at 8f161a08b2, rebased onto main 11c9e7764e:

  • CheckImagePagesAreNotCommitted registers the main image, then queries a static global and a function. Both report neither committed, flexible nor direct. It runs last in main(), so the registration does not affect the other checks.
  • CheckReservedRangeIsNotCommitted then sets the direct page to protection 0 with sceKernelMprotect. VirtualQuery must still report it committed and direct with protection 0, and mincore must still give 1, 0.

On Linux, M6 now fails guest_memory at the image check and M7 at the new mincore check.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the reviewed implementation and focused regression checks support the reported is_committed behavior.

What I ran

Read the prepared diff and relevant changed code at head 8f161a08b244649aff5725ca22a64dceb40be8c2, plus GuestReservation and its callers. No commands or tests were run; repo_read.py was unavailable as a runnable command in this interface.

Results

Check Result Note
Source review passed Four changed files read
Tests not run Test results in the PR description are author reported
Full file coverage passed All files listed as reviewed were read

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run independently. Windows and macOS behavior and console behavior were not verified. Read: core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-libkernel-virtual-query-committed-direct-only.md. The manifest lists all four as included; no changed files were excluded.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows run at 8f161a08b2. I cherry-picked the commit onto main 90c248ef7b without conflicts. Main 90c248e passes the same 12 memory and VirtualQuery tests once, and the cherry-pick passes them in each of three runs (36/36).

Both new checks fail as you describe on Windows too:

Mutation Fails at
M6: Export.cpp:487 set to info->is_flexible ? 0u : 1u GuestMemory.cpp:4871, the image check. Every earlier check passes.
M7: mincore also reports 0 when info.protection == 0 GuestMemory.cpp:1716, the mincore after sceKernelMprotect(fixed, page, 0). The one before it (1711) still passes.
M1: Export.cpp:487 set to 1 GuestMemory.cpp:4397, CheckVirtualQuerySplitFlexibleRanges
M3: mincore back to info.is_committed != 0 ? 1 : 0 GuestMemory.cpp:5128 in main()

With Export.cpp and PosixMemory.cpp from main and only the new GuestMemory.cpp, guest_memory fails at line 4397. py -3 tools/check_conventions.py --base 90c248ef7b passes and py -3 tools/debt_fold.py --check exits 0.

One small note, not something that has to change: CheckGpuAccessFollowsProtection (line 1447) already registers the main image earlier in main(), and registration returns early once it is done. So in the full run the RegisterMainImage() call in CheckImagePagesAreNotCommitted does nothing, and the check still holds because of the earlier registration.

AI-assisted: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants