fix(libkernel): drop undefined protection bits in sceKernelBatchMap's protect operation, as sceKernelMprotect does #22051

Merged
brajankw merged 2 commits from upstream-pr/22051 into main 2026-12-03 21:48:21 +00:00
brajankw commented 2026-11-17 03:06:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T03:06:46Z.


What

Builds on #22040, which this branch contains; review its last commit. A batch map protect entry with a protection bit outside 0x3F7 threw from the host protection conversion. sceKernelMprotect drops those bits, as a PS5 (firmware 13.60) does (the sceKernelMprotect protection bits entry in docs/dev/debt/libkernel.md), and the batch protect operation is the same change of protection, so it drops them too. The map operations refuse them (#22040) and the type-protect operation still throws.

  • GuestKernelBatchMapGuestPointers.cpp: protecting a mapped page with 0x09 through the batch call succeeds and leaves protection 1.
  • The debt fragment of #22040 is updated here, as both change the same entry.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main ec7369ecb8.

  • ctest -R '^guest_kernel_batch_map_guest_pointers$': passed.
  • Negative control: with #22040's Export.cpp and this branch's test, guest_kernel_batch_map_guest_pointers aborts with Unsupported memory protection bits: 9.
  • Full ctest (5896 tests, -j 12) with this branch and my other twelve branches of this round merged onto main 521fb7fec4: 5880 passed. The 16 failures are the set that fails on main in the same environment: six agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own) and ten that need a .git directory, network or a passwd entry for the user.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch: failed closed on #16945, #17718, #17936 and #21569, which conflict with main; none of their diffs touches sceKernelBatchMap2. Otherwise no overlap besides #22040, which it builds on.
  • Not tested on Windows or macOS. No title.

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: #22040
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5349756933 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22051). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T03:06:46Z. --- ### What Builds on #22040, which this branch contains; review its last commit. A batch map protect entry with a protection bit outside 0x3F7 threw from the host protection conversion. `sceKernelMprotect` drops those bits, as a PS5 (firmware 13.60) does (the `sceKernelMprotect protection bits` entry in `docs/dev/debt/libkernel.md`), and the batch protect operation is the same change of protection, so it drops them too. The map operations refuse them (#22040) and the type-protect operation still throws. - `GuestKernelBatchMapGuestPointers.cpp`: protecting a mapped page with 0x09 through the batch call succeeds and leaves protection 1. - The debt fragment of #22040 is updated here, as both change the same entry. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Debian 13, GCC 14.2.0, Release), on `main` `ec7369ecb8`. - `ctest -R '^guest_kernel_batch_map_guest_pointers$'`: passed. - Negative control: with #22040's `Export.cpp` and this branch's test, `guest_kernel_batch_map_guest_pointers` aborts with `Unsupported memory protection bits: 9`. - Full `ctest` (5896 tests, `-j 12`) with this branch and my other twelve branches of this round merged onto `main` `521fb7fec4`: 5880 passed. The 16 failures are the set that fails on `main` in the same environment: six `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own) and ten that need a `.git` directory, network or a passwd entry for the user. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch`: failed closed on #16945, #17718, #17936 and #21569, which conflict with `main`; none of their diffs touches `sceKernelBatchMap2`. Otherwise no overlap besides #22040, which it builds on. - Not tested on Windows or macOS. No title. ### 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: #22040 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 03:10:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T03:10:44Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_batch_map_guest_pointers built cleanly.
  • CTest (guest_kernel_batch_map_guest_pointers):
    1/1 Test #5529: guest_kernel_batch_map_guest_pointers ...   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/DirectMemory/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5529: guest_kernel_batch_map_guest_pointers ...Subprocess aborted***Exception:   0.45 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: sceKernelBatchMap2 validates UndefinedProtection for OpMapDirect and OpMapFlexible returning SCE_KERNEL_ERROR_EINVAL before dispatch.
    • Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: OpProtect masks static_cast<uint8_t>(entry.protection) with 0x3F7 matching sceKernelMprotect behavior and debt documentation.
    • Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: OpTypeProtect is kept unchanged and continues delegating directly without masking, consistent with PS5 mtypeprotect behavior.
    • Verified core/libs/tests/GuestKernelBatchMapGuestPointers.cpp: CheckUndefinedProtection and CheckProtectDropsUndefinedBits verify rejection on map operations and masking down to 0x1 via sceKernelVirtualQuery for OpProtect with 0x09.
    • Verified technical debt documentation in docs/dev/debt/changes/fix-libkernel-batch-map-undefined-protection.md properly documents the behavior and follows AGENTS.md debt fragment conventions.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5932140543 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22051#pullrequestreview-5932140543). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T03:10:44Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `393aaeab12`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_batch_map_guest_pointers` built cleanly. - **CTest** (`guest_kernel_batch_map_guest_pointers`): ``` 1/1 Test #5529: guest_kernel_batch_map_guest_pointers ... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/DirectMemory/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5529: guest_kernel_batch_map_guest_pointers ...Subprocess aborted***Exception: 0.45 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: sceKernelBatchMap2 validates UndefinedProtection for OpMapDirect and OpMapFlexible returning SCE_KERNEL_ERROR_EINVAL before dispatch. - Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: OpProtect masks static_cast<uint8_t>(entry.protection) with 0x3F7 matching sceKernelMprotect behavior and debt documentation. - Verified core/libs/prx/libkernel/DirectMemory/Export.cpp: OpTypeProtect is kept unchanged and continues delegating directly without masking, consistent with PS5 mtypeprotect behavior. - Verified core/libs/tests/GuestKernelBatchMapGuestPointers.cpp: CheckUndefinedProtection and CheckProtectDropsUndefinedBits verify rejection on map operations and masking down to 0x1 via sceKernelVirtualQuery for OpProtect with 0x09. - Verified technical debt documentation in docs/dev/debt/changes/fix-libkernel-batch-map-undefined-protection.md properly documents the behavior and follows AGENTS.md debt fragment conventions. Looks Good To Me
MarcZX10 commented 2026-11-21 13:33:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T13:33:21Z.

Review: COMMENTED


Reviewed — comments

The mapped code applies the existing protection-bit mask consistently to batch protect operations; the host test results and console behavior were not independently verified.

What I ran

Read the prepared diff and all three changed files at head 393aaeab12eed1d23e822e98fd1c091869bbf22e, plus the protection-mask helper and the existing sceKernelMprotect implementation in Export.cpp. No commands or tests were run.

Results

Check Result Note
Source review passed All changed files read
Tests not run Only author-reported results available
Windows/macOS and console not run No independent verification

Findings

Blocking

None.

Non-blocking

  • The regression test exercises 0x09 via sceKernelBatchMap, while the change is in sceKernelBatchMap2's protect operation. sceKernelBatchMap delegates to sceKernelBatchMap2, so the path is covered, but a direct sceKernelBatchMap2 protect case would make the test more explicit (core/libs/tests/GuestKernelBatchMapGuestPointers.cpp:141).

Not verified

  • Author-reported Linux test, negative-control, full-suite, convention-check, and overlap-check results were not run independently.
  • Windows, macOS, and physical-console behavior were not independently verified.
  • Files read: core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestKernelBatchMapGuestPointers.cpp, docs/dev/debt/changes/fix-libkernel-batch-map-undefined-protection.md. No changed files were excluded; all three were marked included in the manifest.

To get this to a good state

Nothing is required. The direct sceKernelBatchMap2 test case is optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005517154 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22051#pullrequestreview-6005517154). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T13:33:21Z. **Review:** COMMENTED --- # Reviewed — comments The mapped code applies the existing protection-bit mask consistently to batch protect operations; the host test results and console behavior were not independently verified. ## What I ran Read the prepared diff and all three changed files at head `393aaeab12eed1d23e822e98fd1c091869bbf22e`, plus the protection-mask helper and the existing `sceKernelMprotect` implementation in `Export.cpp`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All changed files read | | Tests | not run | Only author-reported results available | | Windows/macOS and console | not run | No independent verification | ## Findings ### Blocking None. ### Non-blocking - The regression test exercises `0x09` via `sceKernelBatchMap`, while the change is in `sceKernelBatchMap2`'s protect operation. `sceKernelBatchMap` delegates to `sceKernelBatchMap2`, so the path is covered, but a direct `sceKernelBatchMap2` protect case would make the test more explicit (`core/libs/tests/GuestKernelBatchMapGuestPointers.cpp:141`). ## Not verified - Author-reported Linux test, negative-control, full-suite, convention-check, and overlap-check results were not run independently. - Windows, macOS, and physical-console behavior were not independently verified. - Files read: `core/libs/prx/libkernel/DirectMemory/Export.cpp`, `core/libs/tests/GuestKernelBatchMapGuestPointers.cpp`, `docs/dev/debt/changes/fix-libkernel-batch-map-undefined-protection.md`. No changed files were excluded; all three were marked included in the manifest. ## To get this to a good state Nothing is required. The direct `sceKernelBatchMap2` test case is optional.
ligmaSec commented 2026-12-03 15:12:46 +00:00 (Migrated from github.com)

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

Review: COMMENTED


I compiled and ran GuestKernelBatchMapGuestPointers from this branch on Linux against the existing runtime libraries. It passed the partial-progress checks for rejected map protections and the virtual-query check for protections applied by the protect operation. This overlaps the map-protection change in #22040. Please coordinate the merge order for the shared change.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6172591327 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22051#pullrequestreview-6172591327). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T15:12:46Z. **Review:** COMMENTED --- I compiled and ran `GuestKernelBatchMapGuestPointers` from this branch on Linux against the existing runtime libraries. It passed the partial-progress checks for rejected map protections and the virtual-query check for protections applied by the protect operation. This overlaps the map-protection change in #22040. Please coordinate the merge order for the shared change. AI-assisted: yes.
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!22051
No description provided.