Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
AltewamaPrime:fix/mtypeprotect-undefined-bits
Nov 21, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
AltewamaPrime:fix/mtypeprotect-undefined-bits

Conversation

@AltewamaPrime

@AltewamaPrime AltewamaPrime commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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 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

@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 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

@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 — 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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AltewamaPrime I compared this with #19183, which already refuses undefined bits in sceKernelMtypeprotect and records two console runs in which 0x80000000 was accepted, so the INT_MIN case in this test pins an answer the console does not give. Could you drop the sceKernelMtypeprotect part and its test here and leave it to #19183? The batch protect mask at Export.cpp:745 is also the same change as #22051. That leaves the batch type-protect refusal; please use the existing UndefinedProtection() helper for it and rebase on main.

@github-actions

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 19183 core/libs/prx/libkernel/DirectMemory/Export.cpp L578-579
core/libs/tests/GuestMemory.cpp L383
PR 22051 core/libs/prx/libkernel/DirectMemory/Export.cpp L742

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 18571 core/libs/tests/GuestMemory.cpp
PR 21569 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head 610498b8f1, applied with git apply on main 86d2ba2 together with the other eight PRs of one batch (#23921, #23955, #24031, #24070, #24072, #24077, #24081, #24130). All nine 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 runs: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon".

tests RTX 3080
guest_memory (with the new sceKernelMtypeprotect and sceKernelBatchMap2 checks), guest_memory_pool_stats, guest_memory_resource all pass

Whole batch: the full ctest -j 12 (6496 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main 86d2ba2 (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). The 2099 agc_/video tests on the iGPU ran twice. The second run fails only agc_driver_stencil_plane_memory and agc_driver_store_over_untracked, both known on main. The first run also failed agc_driver_image_table_packed_guards once; it passes 12 of 12 alone on both GPUs and did not recur.

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

AI-assisted: yes (Claude Code)

A batch map type-protect entry passed its protection to DoMtypeprotect,
whose protection validation throws std::invalid_argument for bits outside
0x3F7, so an undefined bit ended the title instead of returning an error.
The entry now answers SCE_KERNEL_ERROR_EINVAL (0x80020016) through the
existing UndefinedProtection() helper, before its range is looked at, and
leaves the range untouched.

The single sceKernelMtypeprotect change and the batch protect masking are
left to boykopovar#19183 and boykopovar#22051.
@AltewamaPrime
AltewamaPrime force-pushed the fix/mtypeprotect-undefined-bits branch from 610498b to 751c6fb Compare November 20, 2026 16:31
@AltewamaPrime AltewamaPrime changed the title fix(libkernel): refuse undefined protection bits in sceKernelMtypeprotect fix(libkernel): refuse undefined bits in the batch type-protect Nov 20, 2026
@AltewamaPrime

Copy link
Copy Markdown
Contributor Author

Done, rebased on main as 751c6fbf04, and the PR is now only the batch type-protect refusal:

The debt fragment records that this answer is not console-measured and that the single call it mirrors differs between console runs. Evidence and the negative control are updated in the description.

@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

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.

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Retested the new head 751c6fbf04, since my earlier run above was on 610498b8f1 before the rebase and the scope cut. Applied with git apply on main c8f72e9 alone, no 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. NVIDIA GeForce RTX 3080 (driver 621.24). No validation layer.

tests RTX 3080
guest_memory (with the batch type-protect refusal case), guest_memory_pool_stats, guest_memory_resource all pass

Whole tree: the full ctest -j 12 (6505 tests) has 4 failures, the same 4 as on main c8f72e9 (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). No new failures. This PR touches no GPU code, so I did not repeat the iGPU run.

This run is for this PR alone, not a combined tree. I did not run a negative control (the new test against main).

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 6325df5 into boykopovar:main Nov 21, 2026
3 of 5 checks passed
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.

5 participants