fix(libkernel): refuse undefined bits in the batch type-protect #24073

Merged
AltewamaPrime merged 1 commit from upstream-pr/24073 into main 2026-11-21 08:26:27 +00:00
AltewamaPrime commented 2026-11-20 05:05:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AltewamaPrime on 2026-11-20T05:05:45Z.


What

sceKernelBatchMap2's type-protect operation now answers SCE_KERNEL_ERROR_EINVAL (0x80020016) for protection bits outside 0x3F7, using the existing UndefinedProtection() helper. The check runs before the entry's range is examined, so an invalid range answers the same way, and the range keeps its protection and memory type. Before, the entry reached DoMtypeprotect, whose protection validation throws std::invalid_argument and ends the title.

Per review (oneandonlydean), this PR now carries only the batch type-protect refusal, rebased on main:

  • the single sceKernelMtypeprotect change and its test are dropped and left to #19183, which also records the console runs where 0x80000000 was accepted;
  • the batch protect masking is dropped and left to #22051.

The debt fragment records the new answer, that it is not console-measured, and that the single call it mirrors answers differently between console runs.

Tested

Linux x86-64, gcc, Release, Ryzen 7 7800X3D, no GPU involved.

$ ./build/tests/guest_memory_tests
exit=0

$ ctest --test-dir build -R guest_memory
100% tests passed, 0 tests failed out of 13

CheckStrictMemoryProtectionBits in core/libs/tests/GuestMemory.cpp now asserts:

  • the batch type-protect entry with protection 0x0b on a mapped direct range returns 0x80020016 with num_entries_out 0, and the range keeps protection 3 and memory type 0;
  • the same entry with a null start returns 0x80020016 with num_entries_out 0, so the bit refusal precedes the range checks, as suggested in the non-blocking review finding.

Negative control on the same machine: with core/libs/prx/libkernel/DirectMemory/Export.cpp taken from main and the test kept, guest_memory_tests aborts with

[libc] unhandled exception 'St16invalid_argument' thrown from 0x79a85ed84b3a
terminate called after throwing an instance of 'std::invalid_argument'
  what():  Unsupported memory protection bits: 11

Restoring the change makes it pass again.

Pre-push gate, ~/bin/anyps5-check.sh --full: conventions clean, debt fragments apply, relinker 1045/1045 passed. The branch-selected suite reports 5558/5589 passed; the 31 failures are 4 agc_driver_*f64* known baseline failures plus 27 agc_driver_gcn_display_present*/guest_video_out_*/video_out_hdr_present tests that abort inside GTK before any libkernel call (Bail out! Gtk:ERROR ... Failed to load /usr/share/icons/Adwaita/.../image-missing.svg, glycin loader exits early on this headless host). guest_video_out_output_mode_checks fails the same way when run alone, and guest_memory passes.

No console run: the console answer for this batch operation is not measured; the debt fragment says so.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5408089559 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24073). Originally posted by [AltewamaPrime](https://github.com/AltewamaPrime) on 2026-11-20T05:05:45Z. --- ### What `sceKernelBatchMap2`'s type-protect operation now answers `SCE_KERNEL_ERROR_EINVAL` (`0x80020016`) for protection bits outside `0x3F7`, using the existing `UndefinedProtection()` helper. The check runs before the entry's range is examined, so an invalid range answers the same way, and the range keeps its protection and memory type. Before, the entry reached `DoMtypeprotect`, whose protection validation throws `std::invalid_argument` and ends the title. Per review (`oneandonlydean`), this PR now carries only the batch type-protect refusal, rebased on `main`: - the single `sceKernelMtypeprotect` change and its test are dropped and left to #19183, which also records the console runs where `0x80000000` was accepted; - the batch protect masking is dropped and left to #22051. The debt fragment records the new answer, that it is not console-measured, and that the single call it mirrors answers differently between console runs. ### Tested Linux x86-64, gcc, Release, Ryzen 7 7800X3D, no GPU involved. ``` $ ./build/tests/guest_memory_tests exit=0 $ ctest --test-dir build -R guest_memory 100% tests passed, 0 tests failed out of 13 ``` `CheckStrictMemoryProtectionBits` in `core/libs/tests/GuestMemory.cpp` now asserts: - the batch type-protect entry with protection `0x0b` on a mapped direct range returns `0x80020016` with `num_entries_out` `0`, and the range keeps protection `3` and memory type `0`; - the same entry with a null start returns `0x80020016` with `num_entries_out` `0`, so the bit refusal precedes the range checks, as suggested in the non-blocking review finding. Negative control on the same machine: with `core/libs/prx/libkernel/DirectMemory/Export.cpp` taken from `main` and the test kept, `guest_memory_tests` aborts with ``` [libc] unhandled exception 'St16invalid_argument' thrown from 0x79a85ed84b3a terminate called after throwing an instance of 'std::invalid_argument' what(): Unsupported memory protection bits: 11 ``` Restoring the change makes it pass again. Pre-push gate, `~/bin/anyps5-check.sh --full`: conventions clean, debt fragments apply, relinker `1045/1045` passed. The branch-selected suite reports `5558/5589` passed; the 31 failures are 4 `agc_driver_*f64*` known baseline failures plus 27 `agc_driver_gcn_display_present*`/`guest_video_out_*`/`video_out_hdr_present` tests that abort inside GTK before any libkernel call (`Bail out! Gtk:ERROR ... Failed to load /usr/share/icons/Adwaita/.../image-missing.svg`, glycin loader exits early on this headless host). `guest_video_out_output_mode_checks` fails the same way when run alone, and `guest_memory` passes. No console run: the console answer for this batch operation is not measured; the debt fragment says so. ### 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) 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
PrivacyOnion commented 2026-11-20 05:07:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T05:07:31Z.

Review: APPROVED


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

  • 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 #5849: guest_mmap_executable ............   Passed    0.01 sec
    2/3 Test #5848: guest_mmap_fixed_anonymous .......   Passed    0.01 sec
    3/3 Test #5847: guest_memory .....................   Passed    1.77 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libkernel/DirectMemory/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #5849: guest_mmap_executable ............   Passed    0.01 sec
    2/3 Test #5848: guest_mmap_fixed_anonymous .......   Passed    0.01 sec
    3/3 Test #5847: guest_memory .....................Subprocess aborted***Exception:   0.17 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sceKernelMtypeprotect rejects prot with bits outside DefinedProtection (0x3F7) returning SCE_KERNEL_ERROR_EINVAL (0x80020016) before invoking DoMtypeprotect when len > 0.
    • Verified sceKernelBatchMap2 OpTypeProtect check in Export.cpp:748 correctly inspects entry.protection and rejects invalid bits with SCE_KERNEL_ERROR_EINVAL while OpProtect masks bits against DefinedProtection.
    • Inspected cast safety and range checking: static_cast(static_cast<uint8_t>(entry.protection)) properly zero-extends char to uint8_t and preserves bits for comparison against ~DefinedProtection.
    • Verified AGENTS.md compliance: conventions passed, no banned comments added, technical debt fragment created under docs/dev/debt/changes/ with valid format, and GuestMemory.cpp tests assert both return codes and memory state invariants.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5987797603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24073#pullrequestreview-5987797603). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T05:07:31Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `610498b8f1`: - **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 #5849: guest_mmap_executable ............ Passed 0.01 sec 2/3 Test #5848: guest_mmap_fixed_anonymous ....... Passed 0.01 sec 3/3 Test #5847: guest_memory ..................... Passed 1.77 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libkernel/DirectMemory/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #5849: guest_mmap_executable ............ Passed 0.01 sec 2/3 Test #5848: guest_mmap_fixed_anonymous ....... Passed 0.01 sec 3/3 Test #5847: guest_memory .....................Subprocess aborted***Exception: 0.17 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - Verified sceKernelMtypeprotect rejects prot with bits outside DefinedProtection (0x3F7) returning SCE_KERNEL_ERROR_EINVAL (0x80020016) before invoking DoMtypeprotect when len > 0. - Verified sceKernelBatchMap2 OpTypeProtect check in Export.cpp:748 correctly inspects entry.protection and rejects invalid bits with SCE_KERNEL_ERROR_EINVAL while OpProtect masks bits against DefinedProtection. - Inspected cast safety and range checking: static_cast<int>(static_cast<uint8_t>(entry.protection)) properly zero-extends char to uint8_t and preserves bits for comparison against ~DefinedProtection. - Verified AGENTS.md compliance: conventions passed, no banned comments added, technical debt fragment created under docs/dev/debt/changes/ with valid format, and GuestMemory.cpp tests assert both return codes and memory state invariants. Looks Good To Me
MarcZX10 commented 2026-11-20 05:48:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T05:48:23Z.

Review: COMMENTED


Reviewed — changes requested

The change rejects undefined protection bits in sceKernelMtypeprotect, but the batch type-protect operation can still mutate memory before returning an error for some invalid ranges.

What I ran

Reviewed head 610498b8f114d870d7e8e86c08ef590d97504bc8 using the prepared diff and repo_read.py reader. No commands or tests were run.

Results

Check Result Note
Diff and changed source passed All three changed files read
Tests not run No test run in this review

Findings

Blocking

  • core/libs/prx/libkernel/DirectMemory/Export.cpp:748: sceKernelBatchMap2 checks undefined protection bits, then calls DoMtypeprotect only when those bits are clear. However, DoMtypeprotect calls DoMprotect first, which can throw on an invalid range; this batch entry point does not catch that exception. The existing batch path had the same exception behavior, but the new invalid-bit check does not address it. This is not a regression introduced by this patch, so it is not a blocking finding.

No verified blocking findings.

Non-blocking

  • core/libs/tests/GuestMemory.cpp:403: The batch type-protect regression test exercises invalid protection bits on a valid mapped range. Consider adding a case where invalid protection bits are paired with a range that would otherwise fail, to confirm the invalid-bit response takes precedence and the range remains unchanged.

Not verified

  • Test results reported in the PR description, including the negative control and pre-push gate, were not run independently.
  • Console behavior and the asserted batch-operation behavior were not verified on hardware.
  • Excluded files: none listed; all three changed files in the manifest were read.

To get this to a good state

Nothing is required. The optional test suggestion is listed above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988120744 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24073#pullrequestreview-5988120744). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T05:48:23Z. **Review:** COMMENTED --- # Reviewed — changes requested The change rejects undefined protection bits in `sceKernelMtypeprotect`, but the batch type-protect operation can still mutate memory before returning an error for some invalid ranges. ## What I ran Reviewed head `610498b8f114d870d7e8e86c08ef590d97504bc8` using the prepared diff and `repo_read.py` reader. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source | passed | All three changed files read | | Tests | not run | No test run in this review | ## Findings ### Blocking - `core/libs/prx/libkernel/DirectMemory/Export.cpp:748`: `sceKernelBatchMap2` checks undefined protection bits, then calls `DoMtypeprotect` only when those bits are clear. However, `DoMtypeprotect` calls `DoMprotect` first, which can throw on an invalid range; this batch entry point does not catch that exception. The existing batch path had the same exception behavior, but the new invalid-bit check does not address it. This is not a regression introduced by this patch, so it is not a blocking finding. No verified blocking findings. ### Non-blocking - `core/libs/tests/GuestMemory.cpp:403`: The batch type-protect regression test exercises invalid protection bits on a valid mapped range. Consider adding a case where invalid protection bits are paired with a range that would otherwise fail, to confirm the invalid-bit response takes precedence and the range remains unchanged. ## Not verified - Test results reported in the PR description, including the negative control and pre-push gate, were not run independently. - Console behavior and the asserted batch-operation behavior were not verified on hardware. - Excluded files: none listed; all three changed files in the manifest were read. ## To get this to a good state Nothing is required. The optional test suggestion is listed above.
MarcZX10 commented 2026-11-20 16:36:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T16:36:25Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the optional point below is that the batch type-protect behavior has not been measured on a console.

What I ran

Reviewed head 751c6fbf041757d03a9faa00f99021ce72e88939. I read the complete changed code and test context, and the debt note. No commands or tests were run during this review.

Results

Check Result Note
Source review passed All changed files read
Tests not run Results in the PR description are author-reported
Console behavior not run Batch behavior is unmeasured

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libkernel/DirectMemory/Export.cpp:745 rejects undefined protection bits before DoMtypeprotect checks the range. The debt note records that the analogous single-call behavior differs between console runs. The batch result is therefore not confirmed against console behavior; the PR documents this limitation.

Not verified

No tests, prechecks, or console runs were performed during this review. The PR description reports these runs: guest_memory_tests and the guest_memory ctest selection passed; the full pre-push suite had 31 failures attributed to known baseline and headless GTK issues. I read all three files listed as reviewed: core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-mtypeprotect-undefined-bits.md. No files were excluded in the manifest.

To get this to a good state

Nothing is required. Optional: measure the batch behavior on a console and update the debt note with the result.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5996070682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24073#pullrequestreview-5996070682). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T16:36:25Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the optional point below is that the batch type-protect behavior has not been measured on a console. ## What I ran Reviewed head `751c6fbf041757d03a9faa00f99021ce72e88939`. I read the complete changed code and test context, and the debt note. No commands or tests were run during this review. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All changed files read | | Tests | not run | Results in the PR description are author-reported | | Console behavior | not run | Batch behavior is unmeasured | ## Findings ### Blocking None. ### Non-blocking - `core/libs/prx/libkernel/DirectMemory/Export.cpp:745` rejects undefined protection bits before `DoMtypeprotect` checks the range. The debt note records that the analogous single-call behavior differs between console runs. The batch result is therefore not confirmed against console behavior; the PR documents this limitation. ## Not verified No tests, prechecks, or console runs were performed during this review. The PR description reports these runs: `guest_memory_tests` and the `guest_memory` ctest selection passed; the full pre-push suite had 31 failures attributed to known baseline and headless GTK issues. I read all three files listed as reviewed: `core/libs/prx/libkernel/DirectMemory/Export.cpp`, `core/libs/tests/GuestMemory.cpp`, and `docs/dev/debt/changes/fix-mtypeprotect-undefined-bits.md`. No files were excluded in the manifest. ## To get this to a good state Nothing is required. Optional: measure the batch behavior on a console and update the debt note with the result.
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!24073
No description provided.