fix(libkernel): refuse undefined protection bits in the map calls with EINVAL #11005

Merged
harisibrar1 merged 3 commits from upstream-pr/11005 into main 2026-10-28 07:04:38 +00:00
harisibrar1 commented 2026-10-25 14:04:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-25T14:04:16Z.


What

The map calls throw on protection bits outside 0x3F7, where the console returns EINVAL.

@hugodearaujo measured this in #6825: PS5 firmware 13.60, payload process, probe output in that PR's description.

  • sceKernelMapFlexibleMemory accepts every defined bit up to 0x3F7, and refuses 0x8, 0x400, 0x800, 0x3FF and 0xFFFFFFF2 with 0x80020016:
    • MAPFLEX|req=00000008|rc=80020016
    • MAPFLEX|req=00000400|rc=80020016
    • MAPFLEX|req=000003f7|rc=00000000
  • sceKernelMapDirectMemory refuses the same values the same way: MAPDIRECT|req=00000008|rc=80020016, MAPDIRECT|req=fffffff2|rc=80020016.

On main all of these reach LinuxProtFromSce (DirectMemory.cpp). It throws std::invalid_argument there, and the return SCE_KERNEL_ERROR_EINVAL; above the throw is commented out. The sceKernelMprotect protection bits debt entry already records the difference ("here both throw for undefined bits").

This adds an UndefinedProtection check (prot & ~0x3F7) at the start of sceKernelMapFlexibleMemory, sceKernelMapNamedFlexibleMemory, sceKernelMapDirectMemory, sceKernelMapDirectMemory2 and sceKernelMapNamedDirectMemory. Each returns SCE_KERNEL_ERROR_EINVAL before any mapping is changed or any flexible memory is used. The named and 2 forms weren't probed separately; they take the same protection argument as the measured calls. sceKernelMapNamedFlexibleMemoryInternal goes through sceKernelMapNamedFlexibleMemory.

Left as they are:

  • sceKernelMtypeprotect (also measured as EINVAL) still throws, to keep clear of the DoMtypeprotect changes in #9882.
  • The batch map calls still throw.
  • The execute bit the console refuses on direct maps in a payload process is still mapped, as the debt entry explains.

The debt sentence now says exactly which calls return EINVAL and which still throw.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 4017310678 with this change: no failed steps.

  • guest_memory, guest_protection_report and guest_kernel_sys pass, 3 runs of 3/3.
  • GuestMemory.cpp pinned the throw in two places (try { sceKernelMapFlexibleMemory(&undefined, length, 0x400, 0); } catch (const std::invalid_argument&) and CheckStrictMemoryProtectionBits). Both now require EINVAL from all five calls, with the address left null and the available flexible size unchanged. The measured 0x800 and 0x3ff are added to the existing list. sceKernelMtypeprotect keeps its throw check.
  • Negative control: with main's DirectMemory/Export.cpp, guest_memory fails (2 runs).
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux and macOS. The check runs before any host call.

Overlap

pr_overlap reports #9882 (@orezraey): it rewrites the neighbouring debt line (136) while this changes 137, so whichever lands second needs a trivial text merge. The other listed PRs (#4449, #6321, #7201, #10550) conflict with main and don't touch these functions or test lines.

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 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. Merge order agreed with @orezraey: this lands before #9882, which now lists Depends on: #11005 and will merge main keeping both debt entries
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005434970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11005). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-25T14:04:16Z. --- ### What The map calls throw on protection bits outside `0x3F7`, where the console returns `EINVAL`. @hugodearaujo measured this in #6825: PS5 firmware 13.60, payload process, probe output in that PR's description. - `sceKernelMapFlexibleMemory` accepts every defined bit up to `0x3F7`, and refuses `0x8`, `0x400`, `0x800`, `0x3FF` and `0xFFFFFFF2` with `0x80020016`: - `MAPFLEX|req=00000008|rc=80020016` - `MAPFLEX|req=00000400|rc=80020016` - `MAPFLEX|req=000003f7|rc=00000000` - `sceKernelMapDirectMemory` refuses the same values the same way: `MAPDIRECT|req=00000008|rc=80020016`, `MAPDIRECT|req=fffffff2|rc=80020016`. On main all of these reach `LinuxProtFromSce` (`DirectMemory.cpp`). It throws `std::invalid_argument` there, and the `return SCE_KERNEL_ERROR_EINVAL;` above the throw is commented out. The `sceKernelMprotect protection bits` debt entry already records the difference ("here both throw for undefined bits"). This adds an `UndefinedProtection` check (`prot & ~0x3F7`) at the start of `sceKernelMapFlexibleMemory`, `sceKernelMapNamedFlexibleMemory`, `sceKernelMapDirectMemory`, `sceKernelMapDirectMemory2` and `sceKernelMapNamedDirectMemory`. Each returns `SCE_KERNEL_ERROR_EINVAL` before any mapping is changed or any flexible memory is used. The named and `2` forms weren't probed separately; they take the same protection argument as the measured calls. `sceKernelMapNamedFlexibleMemoryInternal` goes through `sceKernelMapNamedFlexibleMemory`. Left as they are: - `sceKernelMtypeprotect` (also measured as `EINVAL`) still throws, to keep clear of the `DoMtypeprotect` changes in #9882. - The batch map calls still throw. - The execute bit the console refuses on direct maps in a payload process is still mapped, as the debt entry explains. The debt sentence now says exactly which calls return `EINVAL` and which still throw. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 4017310678 with this change: no failed steps. - `guest_memory`, `guest_protection_report` and `guest_kernel_sys` pass, 3 runs of 3/3. - `GuestMemory.cpp` pinned the throw in two places (`try { sceKernelMapFlexibleMemory(&undefined, length, 0x400, 0); } catch (const std::invalid_argument&)` and `CheckStrictMemoryProtectionBits`). Both now require `EINVAL` from all five calls, with the address left null and the available flexible size unchanged. The measured `0x800` and `0x3ff` are added to the existing list. `sceKernelMtypeprotect` keeps its throw check. - Negative control: with main's `DirectMemory/Export.cpp`, `guest_memory` fails (2 runs). - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux and macOS. The check runs before any host call. ### Overlap `pr_overlap` reports #9882 (@orezraey): it rewrites the neighbouring debt line (136) while this changes 137, so whichever lands second needs a trivial text merge. The other listed PRs (#4449, #6321, #7201, #10550) conflict with main and don't touch these functions or test lines. ### 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) - [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. Merge order agreed with @orezraey: this lands before #9882, which now lists `Depends on: #11005` and will merge `main` keeping both debt entries - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 14:08:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:08:04Z.

Review: APPROVED


Reviewed the firmware 13.60 measurements cited from #6825 and the five map entry points. The early guard matches the existing LinuxProtFromSce accepted-bit mask, and the tests verify the invalid-bit cases return EINVAL without changing the output address, flexible-memory availability, or existing mapping state. The sceKernelMtypeprotect and batch map behavior remains explicitly out of scope as documented. I verified git diff --check, the repository convention script (0 errors; it reports the existing long debt-entry warning), and a local C++20 syntax check of DirectMemory/Export.cpp. The PR includes Windows full-build and focused runtime results; Linux/macOS product runtime tests were not run here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643600935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11005#pullrequestreview-5643600935). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:08:04Z. **Review:** APPROVED --- Reviewed the firmware 13.60 measurements cited from #6825 and the five map entry points. The early guard matches the existing `LinuxProtFromSce` accepted-bit mask, and the tests verify the invalid-bit cases return EINVAL without changing the output address, flexible-memory availability, or existing mapping state. The `sceKernelMtypeprotect` and batch map behavior remains explicitly out of scope as documented. I verified `git diff --check`, the repository convention script (0 errors; it reports the existing long debt-entry warning), and a local C++20 syntax check of `DirectMemory/Export.cpp`. The PR includes Windows full-build and focused runtime results; Linux/macOS product runtime tests were not run here.
MarcZX10 commented 2026-10-25 14:43:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:43:58Z.

Review: DISMISSED


Requesting merge-order coordination with #9882 before this can be approved. CONTRIBUTING.md says overlapping PR authors should agree which goes first. I fetched both current heads and git merge-tree --write-tree confirms a content conflict in docs/dev/debt/libkernel.md. This PR’s checklist says Depends on: none (debt-line order with #9882 above), but I found no agreement from #9882’s author establishing that order. Please record mutual agreement and the corresponding dependency/order, then rebase or merge the debt text cleanly. The implementation and test review otherwise stands; this is specifically the unresolved contribution-rule blocker.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643715193 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11005#pullrequestreview-5643715193). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:43:58Z. **Review:** DISMISSED --- Requesting merge-order coordination with #9882 before this can be approved. CONTRIBUTING.md says overlapping PR authors should agree which goes first. I fetched both current heads and `git merge-tree --write-tree` confirms a content conflict in `docs/dev/debt/libkernel.md`. This PR’s checklist says `Depends on: none (debt-line order with #9882 above)`, but I found no agreement from #9882’s author establishing that order. Please record mutual agreement and the corresponding dependency/order, then rebase or merge the debt text cleanly. The implementation and test review otherwise stands; this is specifically the unresolved contribution-rule blocker.
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!11005
No description provided.