Skip to content

fix(npcommon): validate guest address pointer in sceNpAllocateKernelMemoryNoAlignment - #22107

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceNpCommon-allocateKernelMemory-copyout
Dec 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceNpCommon-allocateKernelMemory-copyout

Conversation

@Kikoarl

@Kikoarl Kikoarl commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

Validate guest destination pointer via GuestCopyOut_nid_no_patch in sceNpAllocateKernelMemoryNoAlignment, unmapping memory on failure and preserving caller errno.

Tested

  • Code compiles cleanly in RAM-disk (/tmp/anyps5_build)
  • Unit test passing (guest_np_common_runtime_tests)
  • python3 tools/check_conventions.py --base origin/main passes cleanly

Checklist

  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: none
  • 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 10aa6c6c19:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_np_common_runtime_tests built cleanly.
  • CTest (guest_np_common_runtime):
    1/1 Test #900: guest_np_common_runtime ..........   Passed    0.36 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceNpCommon/Runtime.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #900: guest_np_common_runtime ..........***Exception: SegFault  0.23 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified GuestCopyOut_nid_no_patch safely handles unwritable guest destination pointers and prevents host segfaults
    • Verified sceNpFreeKernelMemory cleanup on GuestCopyOut_nid_no_patch failure to prevent memory leaks
    • Verified errno preservation across GuestCopyOut_nid_no_patch call
    • Verified documentation fragment docs/dev/debt/changes/fix-libSceNpCommon-allocateKernelMemory-copyout.md follows repo conventions and records console behavior status
    • Verified test coverage in core/libs/tests/GuestNpCommonRuntime.cpp correctly validates failure path with invalid address 0x10 and errno preservation

Looks Good To Me

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 10aa6c6c19, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

guest_np_common_runtime, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@bobczan

bobczan commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Built and ran it on Windows (11, MSYS2 UCRT64, GCC 16.2) at 10aa6c6c19:

guest_np_common_runtime ... Passed  0.42 s
100% tests passed out of 1

No defects, and the failure handling is the part worth calling out:

if (!copied) {
    sceNpFreeKernelMemory(memory);
    return NP_ERROR_INVALID_ARGUMENT;
}

Freeing on an unpublishable address is right, and it is safe from a double free for a specific reason — the caller never receives the address, so it has nothing to free later. Returning NP_ERROR_INVALID_ARGUMENT after a successful allocation rather than a success with an unset out-parameter is also the honest answer; the alternative silently hands back a garbage address field.

The added test line and the debt fragment both match the change. Nothing else to flag on an 18-line diff.

Run in a local Windows environment; no changes pushed.

AI-assisted: yes

@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

The change at head 10aa6c6c197844315706f2490e1cb51524985439 checks the guest output pointer, releases the mapped memory on copy failure, and preserves errno; the changed implementation and test support merging.

What I ran

  • Inspected the prepared diff and targeted source and test ranges using repo_read.py tools. No build or tests were run; the commands in the PR description are author-reported.

Results

Check Result Note
Changed implementation and adjacent helpers passed Allocation cleanup path inspected
Changed runtime test passed Invalid output pointer and errno assertion inspected
Build and tests not run Reported by author only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The complete changed-file texts under review/files/ and files.json were not accessible via the available reader tools. The implementation and test hunks were read from the pinned repository head. The documentation change was only inspected in the diff. Build, unit tests, conventions check, and console behavior were not independently verified. The PR description reports successful build, unit test, and conventions check; those results were not run by this review.

To get this to a good state

Nothing is required.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 10aa6c6c19, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24576, #24603, #14196, #14527, #17718, #18671, #22753, #22792, #23503, #24245, #24480, #24553, #24714, #21236, #21765, #21987, #22030, #22397, #22571, #23158, #23816, #24067, #18401, #19101, #19398, #19657, #21563, #24584, #24628). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_np_common_runtime (1 test, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6523 tests) on the RTX 3080 has 7 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #19101 (guest_video_out_window_margins_bounds); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): agc_driver_image_gather_lod. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_image_gather_lod, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_storage_write_back_host_retile, guest_video_out_window_margins_bounds (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@ycbuzi

ycbuzi commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Tested 10aa6c6c19 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 6589; 99% tests passed, 2 tests failed out of 6589. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6589; 99% tests passed, 1 tests failed out of 6589. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

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.

8 participants