Repository navigation
fix(libkernel): refuse a stale equeue handle - #21726
oneandonlydean merged 1 commit into
Conversation
An equeue handle is (id << 32) | descriptor, and EqueuePin_nid_postfix resolved only the descriptor half, so once a deleted queue's descriptor number was recycled by a new queue a stale handle resolved to that other queue: Add event, Trigger event, Delete event, sceKernelWaitEqueue and the file event calls then added, triggered, deleted or waited on events of the wrong queue instead of answering SCE_KERNEL_ERROR_EBADF. sceKernelDeleteEqueue already compared owner->Handle() with eq, showing the check belongs in the pin. EqueuePin_nid_postfix now returns null when the queue registered at the descriptor does not match the whole handle, which covers every caller (also the AgcDriver and VideoOut event queues) without special-casing any handle value. Evidence: the fw 13.42 libkernel_sys entry in docs/dev/debt/libkernel.md records that the console creates a kqueue plus a named object, returns (id << 32) | descriptor and deletes the named object before closing the descriptor, so a stale handle cannot match a live queue there; the changed entry now states the pinned stale-handle result. Tests: guest_equeue_events gains VerifyStaleHandleRefusedAfterDescriptorReuse, which creates and deletes a queue, lets the descriptor recycle into a fresh queue and requires EBADF from the stale handle on add, trigger and delete user event, wait, delete queue and add/delete file event, then checks the fresh queue still works. Without the fix the test aborts; with it guest_equeue_events passes in 0.7 s, guest_kqueue in 5.2 s and guest_kqueue_cleared_race in 1.8 s. check_conventions passes; pr_overlap --branch reports no overlap with open pull requests.
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 9018a0ee7c:
- Conventions:
tools/check_conventions.pypassed;git diff --checkclean. - Build:
guest_equeue_event_testsbuilt cleanly. - CTest (
guest_equeue_events):1/1 Test #5319: guest_equeue_events .............. Passed 0.70 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel/Equeue/Equeue.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #5319: guest_equeue_events ..............Subprocess aborted***Exception: 0.37 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- core/libs/prx/libkernel/Equeue/Equeue.cpp:691-695 verifies that EqueuePin_nid_postfix compares owner->Handle() against the full 64-bit KernelEqueue handle eq after looking up the low 32-bit descriptor, returning nullptr on mismatch.
- core/libs/tests/GuestEqueueEvents.cpp:530-558 verifies that deleting an equeue and creating a new one with the recycled descriptor refuses the stale handle with SCE_KERNEL_ERROR_EBADF across all seven equeue operations (sceKernelAddUserEvent, sceKernelTriggerUserEvent, sceKernelDeleteUserEvent, sceKernelWaitEqueue, sceKernelDeleteEqueue, sceKernelAddFileEvent, sceKernelDeleteFileEvent) while preserving normal operation on the fresh handle.
- Checked concurrency and lifetime safety of owner->Handle() comparison against eq; m_handle is immutable after construction in KernelEqueuePrivate.
- Checked LP64/LLP64 portability of the 32-bit descriptor cast static_caststd::uint32_t(fresh) == static_caststd::uint32_t(stale) and 64-bit KernelEqueue equality comparison.
- docs/dev/debt/changes/fix-libkernel-equeue-stale-handle.md properly updates the technical debt documentation for sceKernelCreateEqueue and sceKernelGetKqueueFromEqueue in compliance with AGENTS.md.
Looks Good To Me
|
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.
|
|
Windows result for head Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release,
AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine. |
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the review found no verified defects in the inspected code.
What I ran
Reviewed head 9018a0ee7ccfd282ec31fb02e4e8dc275a7de2d5 using repo_read.py. No build or tests were run by this reviewer; the test runs and negative control in the description are author-reported.
Results
| Check | Result | Note |
|---|---|---|
| Source review | passed | All changed files and added lines reviewed |
| Build and tests | not run | Reported test runs were not independently verified |
| Negative control | not run | Author reports the test aborts with the fix stashed |
Findings
Blocking
None.
Non-blocking
None.
Not verified
Build and test results, including the reported negative control, were not independently run. Console behavior and the firmware disassembly claims were not independently confirmed. All files marked reviewed were read: core/libs/prx/libkernel/Equeue/Equeue.cpp, core/libs/tests/GuestEqueueEvents.cpp, and docs/dev/debt/changes/fix-libkernel-equeue-stale-handle.md. No changed files were excluded.
To get this to a good state
Nothing is required.
|
On Windows, merged onto Existing runs:
What this run adds.
Code.
Negative control and mutation. All of these ran on the merge, using
Tests.
Host:
AI-assisted: yes |
What
EqueuePin_nid_postfixmatched only the descriptor half of an equeue handle,(id << 32) | descriptor. AftersceKernelDeleteEqueueand a recycled descriptor number, a stale handle waited on, triggered or deleted events of the new queue instead of failing. Firmware 13.42'slibkernel_syscreates a named object, returns(id << 32) | descriptor, and deletes the named object before closing the descriptor, so a stale handle cannot match a live queue on the console (technical debt entry, disassembly).The pin now refuses a handle whose id half does not match the queue registered at its descriptor. Every caller —
sceKernelAddUserEvent,sceKernelTriggerUserEvent,sceKernelDeleteUserEvent,sceKernelWaitEqueue,sceKernelDeleteEqueue,sceKernelAddFileEventandsceKernelDeleteFileEvent— answersEBADFfor a stale handle, while a live handle keeps working normally.Tested
Linux (Omarchy 4.0.2), host build
ReleasewithBUILD_TESTING=ON:guest_equeue_events— Passed, 0.66–0.75 sguest_kqueue— Passed, 5.17 sguest_kqueue_cleared_race— Passed, 1.41–1.79 sThe new case creates a queue, deletes it, creates another that recycles the descriptor, then requires
EBADFfrom all seven stale-handle calls and normal operation on the fresh queue. Negative control: with the fix stashed the case aborts, so it genuinely covers the bug.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)