fix(libkernel): sceKernelReleaseDirectMemory returns 0 for a length of 0 #17553

Open
xXPBSXx wants to merge 5 commits from upstream-pr/17553 into main
xXPBSXx commented 2026-11-08 21:58:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-11-08T21:58:19Z.


What

sceKernelReleaseDirectMemory returned EINVAL for a length of 0, and guest_memory pinned Release(0, 0) as EINVAL without a console result. On a PS5, releasing a length of 0 at an aligned start returns 0, while a length of 1 and an unaligned start (phys + 1, 16 KiB) return EINVAL, and releasing a block and releasing it again return 0 (cases 9.44 to 9.48, lines 48 to 52 of the raw output). This returns 0 for a length of 0 at an aligned start before the range is claimed; the two EINVAL cases and a start below 0 or unaligned are unchanged. The test now expects Release(0, 0) to return 0 and pins the five measured results. A debt fragment, docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md, changes the sceKernelReleaseDirectMemory entry of docs/dev/debt/libkernel.md to record the measurement; an unaligned start with a length of 0, a start at the end of direct memory and a title process were not measured.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-07 at 08:55 UTC. A title process may differ. The calls released a block of the probe's own 4 MiB direct memory; the cases are 9.44 to 9.48, lines 48 to 52 of the raw output in the first comment.

Call PS5, payload process, firmware 11.60 AnyPS5 main before here
sceKernelReleaseDirectMemory(phys, 0) 0 EINVAL 0
sceKernelReleaseDirectMemory(phys, 1) 0x80020016 EINVAL 0x80020016
sceKernelReleaseDirectMemory(phys + 1, 16 KiB) 0x80020016 EINVAL 0x80020016
sceKernelReleaseDirectMemory(phys, 16 KiB), then again 0, then 0 0, then 0 0, then 0

On this branch, main eb2d16b170b, on 2026-11-17: with only the test commit b220bf30d12, guest_memory fails on Windows (MinGW, Release, 23:01 to 23:05 UTC, one run) with Guest memory check failed at core/libs/tests/GuestMemory.cpp:1052, which is Release(0, 0) == 0, and on Linux (WSL, Release, from 22:29 UTC, one run) with Subprocess aborted; with the fix commit 81531bed9b8 it passes in 3 of 3 runs on Linux, and with the docs commit 15a8b0b46f8 it passes in 1 run on Windows (23:04 to 23:07 UTC) and in 3 of 3 runs on Linux. The Linux commits were applied with git am to a clean checkout of main. tools/check_conventions.py --base upstream/main passes on each of the three commits on Linux. The probe source and its raw output are in the first comment. Not tested: macOS, the CI runner, a title process and any other firmware.

Overlap (2026-11-18, list of open pull requests read from 23:59 UTC on 2026-11-17 to 00:16 UTC, test-merged at 00:18 UTC against main 4568142448e, head 15a8b0b46f84): of the 1786 open pull requests, 12 change core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp or core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp (#22051, #22040, #22013, #21569, #20349, #19183, #18293, #17936, #17718, #16945, #16372 and #12315), and each was merged with and without this branch. None conflicts only with this branch: five conflict with main by themselves (#21569, #17718 and #16945 in DirectMemory/Export.cpp, #18293 in WindowsMappings.hpp, #12315 in libSceLibreSsl3) and the other seven merge cleanly with and without it. None of them changes sceKernelReleaseDirectMemory; #22013, #20349, #17718, #16945 and #12315 only add or keep calls of it in tests, and the hunk of #19183 sits in sceKernelCheckedReleaseDirectMemory. The branch no longer edits docs/dev/debt/libkernel.md, so the debt lines of #16869 (merged on 2026-11-12) and #16945 no longer meet this change, and no open pull request changes a debt line that names sceKernelReleaseDirectMemory.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (documentation and a host-independent memory check only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5215507192 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17553). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-11-08T21:58:19Z. --- ### What `sceKernelReleaseDirectMemory` returned `EINVAL` for a length of 0, and `guest_memory` pinned `Release(0, 0)` as `EINVAL` without a console result. On a PS5, releasing a length of 0 at an aligned start returns 0, while a length of 1 and an unaligned start (`phys + 1`, 16 KiB) return `EINVAL`, and releasing a block and releasing it again return 0 (cases 9.44 to 9.48, lines 48 to 52 of the raw output). This returns 0 for a length of 0 at an aligned start before the range is claimed; the two `EINVAL` cases and a start below 0 or unaligned are unchanged. The test now expects `Release(0, 0)` to return 0 and pins the five measured results. A debt fragment, `docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md`, changes the `sceKernelReleaseDirectMemory` entry of `docs/dev/debt/libkernel.md` to record the measurement; an unaligned start with a length of 0, a start at the end of direct memory and a title process were not measured. ### Tested Measured on a PS5 with firmware 11.60 (`kernel_get_fw_version` 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-07 at 08:55 UTC. A title process may differ. The calls released a block of the probe's own 4 MiB direct memory; the cases are 9.44 to 9.48, lines 48 to 52 of the raw output in the first comment. | Call | PS5, payload process, firmware 11.60 | AnyPS5 `main` before | here | |---|---|---|---| | `sceKernelReleaseDirectMemory(phys, 0)` | 0 | `EINVAL` | 0 | | `sceKernelReleaseDirectMemory(phys, 1)` | 0x80020016 | `EINVAL` | 0x80020016 | | `sceKernelReleaseDirectMemory(phys + 1, 16 KiB)` | 0x80020016 | `EINVAL` | 0x80020016 | | `sceKernelReleaseDirectMemory(phys, 16 KiB)`, then again | 0, then 0 | 0, then 0 | 0, then 0 | On this branch, `main` `eb2d16b170b`, on 2026-11-17: with only the test commit `b220bf30d12`, `guest_memory` fails on Windows (MinGW, Release, 23:01 to 23:05 UTC, one run) with `Guest memory check failed at core/libs/tests/GuestMemory.cpp:1052`, which is `Release(0, 0) == 0`, and on Linux (WSL, Release, from 22:29 UTC, one run) with `Subprocess aborted`; with the fix commit `81531bed9b8` it passes in 3 of 3 runs on Linux, and with the docs commit `15a8b0b46f8` it passes in 1 run on Windows (23:04 to 23:07 UTC) and in 3 of 3 runs on Linux. The Linux commits were applied with `git am` to a clean checkout of `main`. `tools/check_conventions.py --base upstream/main` passes on each of the three commits on Linux. The probe source and its raw output are in the first comment. Not tested: macOS, the CI runner, a title process and any other firmware. Overlap (2026-11-18, list of open pull requests read from 23:59 UTC on 2026-11-17 to 00:16 UTC, test-merged at 00:18 UTC against `main` `4568142448e`, head `15a8b0b46f84`): of the 1786 open pull requests, 12 change `core/libs/prx/libkernel/DirectMemory/Export.cpp`, `core/libs/tests/GuestMemory.cpp` or `core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp` (#22051, #22040, #22013, #21569, #20349, #19183, #18293, #17936, #17718, #16945, #16372 and #12315), and each was merged with and without this branch. None conflicts only with this branch: five conflict with `main` by themselves (#21569, #17718 and #16945 in `DirectMemory/Export.cpp`, #18293 in `WindowsMappings.hpp`, #12315 in `libSceLibreSsl3`) and the other seven merge cleanly with and without it. None of them changes `sceKernelReleaseDirectMemory`; #22013, #20349, #17718, #16945 and #12315 only add or keep calls of it in tests, and the hunk of #19183 sits in `sceKernelCheckedReleaseDirectMemory`. The branch no longer edits `docs/dev/debt/libkernel.md`, so the debt lines of #16869 (merged on 2026-11-12) and #16945 no longer meet this change, and no open pull request changes a debt line that names `sceKernelReleaseDirectMemory`. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a (documentation and a host-independent memory check only) <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-11-21 11:58:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T11:58:31Z.

Review: APPROVED


Reviewed — looks good

The change matches the documented PS5 payload observation, validates the range before returning success for zero length, and adds regression coverage for the changed behavior.

What I ran

Reviewed head 15a8b0b46f84bb8dfc86c39a63ce89e8fb1108a0 against the prepared diff. No tests or builds were run. Read core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md; the complete text of each changed file was available. The excluded-file list was not available as a separate artifact.

Results

Check Result Note
Diff and changed lines passed All added lines reviewed
Targeted code review passed Range validation precedes the zero-length return
Tests and build not run Author-reported runs only
Console behavior not run PS5 observation is author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports PS5 firmware 11.60 behavior and host test runs; these were not independently reproduced. No tests, builds, CI, title-process behavior, or other firmware were checked. No claims are made about excluded files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005045940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17553#pullrequestreview-6005045940). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T11:58:31Z. **Review:** APPROVED --- # Reviewed — looks good The change matches the documented PS5 payload observation, validates the range before returning success for zero length, and adds regression coverage for the changed behavior. ## What I ran Reviewed head `15a8b0b46f84bb8dfc86c39a63ce89e8fb1108a0` against the prepared diff. No tests or builds were run. Read `core/libs/prx/libkernel/DirectMemory/Export.cpp`, `core/libs/tests/GuestMemory.cpp`, and `docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md`; the complete text of each changed file was available. The excluded-file list was not available as a separate artifact. ## Results | Check | Result | Note | |---|---|---| | Diff and changed lines | passed | All added lines reviewed | | Targeted code review | passed | Range validation precedes the zero-length return | | Tests and build | not run | Author-reported runs only | | Console behavior | not run | PS5 observation is author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author reports PS5 firmware 11.60 behavior and host test runs; these were not independently reproduced. No tests, builds, CI, title-process behavior, or other firmware were checked. No claims are made about excluded files. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-22 12:20:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T12:20:20Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the targeted implementation and tests support the reported zero-length behavior.

What I ran

Reviewed head b561ad37603674c3f66d52781cacbe48b8ef2ea0 using the prepared diff and repo_read.py. No commands or tests were run in this review.

Results

Check Result Note
Diff and changed source passed Three changed files read
Tests not run Author-reported runs only

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run independently. The author reports PS5 measurements on firmware 11.60 in a payload process and host test runs; these were not independently verified. The changed files listed in the manifest were read: core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md. No changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012169582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17553#pullrequestreview-6012169582). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T12:20:20Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the targeted implementation and tests support the reported zero-length behavior. ## What I ran Reviewed head `b561ad37603674c3f66d52781cacbe48b8ef2ea0` using the prepared diff and `repo_read.py`. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source | passed | Three changed files read | | Tests | not run | Author-reported runs only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run independently. The author reports PS5 measurements on firmware 11.60 in a payload process and host test runs; these were not independently verified. The changed files listed in the manifest were read: `core/libs/prx/libkernel/DirectMemory/Export.cpp`, `core/libs/tests/GuestMemory.cpp`, and `docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md`. No changed files were excluded. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 13:50:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:50:39Z.

Review: COMMENTED


The Linux integration build passed the registered GuestMemory CTest, including zero-length release and neighboring argument checks.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171071673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17553#pullrequestreview-6171071673). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:50:39Z. **Review:** COMMENTED --- The Linux integration build passed the registered GuestMemory CTest, including zero-length release and neighboring argument checks. AI-assisted: yes.
Plyxen commented 2026-12-03 14:08:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T14:08:03Z.

Review: APPROVED


I read the diff at b561ad3760. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).

  • guest_memory: pass on both.
  • Control (the same tree with only this PR's source change taken out): on Windows it fails at line 742. On Linux, with the changed sources of the tested CPU PRs put back to main's, the same test fails too.

Not checked: a console.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171373731 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17553#pullrequestreview-6171373731). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-03T14:08:03Z. **Review:** APPROVED --- I read the diff at `b561ad3760`. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into `main` at `6c6b29320fe` together with other open PRs (so not an isolated build). - `guest_memory`: pass on both. - Control (the same tree with only this PR's source change taken out): on Windows it fails at line 742. On Linux, with the changed sources of the tested CPU PRs put back to `main`'s, the same test fails too. Not checked: a console. AI-assisted: yes (Claude Code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/17553:upstream-pr/17553'
git switch 'upstream-pr/17553'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/17553'
git switch 'upstream-pr/17553'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/17553'
git switch 'upstream-pr/17553'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/17553'
git switch 'main'
git merge --squash 'upstream-pr/17553'
git switch 'main'
git merge --ff-only 'upstream-pr/17553'
git switch 'main'
git merge 'upstream-pr/17553'
git push origin 'main'
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!17553
No description provided.