fix(libkernel): notify rwlock waiters before unlocking state #6711
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6711"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-16T06:12:37Z.
What
A thread releasing a PS5 read/write lock could release its internal mutex before notifying waiters. A successor could acquire the lock, release it and destroy it while the first thread was still calling
NotifyAllon the freed object. The same lifetime gap existed after a timed writer stopped waiting. I now finish each notification while the internal mutex still protects the object.The regression covers 2,000 reader-to-writer and 2,000 writer-to-writer handoffs per run. It waits for the successor's readiness with
atomic::waitand a matching notification, and yields after an unsuccessfulTrywrlock. After acquiring the lock, the successor still immediately unlocks and destroys it, preserving the lifetime race. The 20-second timeout is unchanged.The requirement to finish wakeups before a successor can destroy a read/write lock is also described in the glibc implementation.
Rwlock.cpp, Windows (native host)std::system_errorRwlock.cpp, Windows (native host)Rwlock.cpp, Linux (VirtualBox VM)Rwlock.cpp, Linux (VirtualBox VM), AddressSanitizerheap-use-after-freeThe production baseline is
main5d9dcc42d2ec5401aebd089028b8d4d7410a77b4. The updated PR head is915e9f2d5d223a16e3f21d9ad52c18ecfd41c532. Full builds use integration commit339ee7999e82b1cbb51d01c6239d37c8a556583b, which adds only #6452's AGC test fixture correction needed by the supported Windows toolchain. That dependency is outside this PR. For each lifetime comparison, only productionRwlock.cppwas replaced; the updated regression stayed identical.Tested
BUILD_TESTING=ON. Full default build passed, including patched libraries. The ten kernel synchronization/thread tests below passed with zero skips. The updated regression passed 20 additional normal runs and 20 runs with process affinity restricted to one logical CPU. Restoring the originalRwlock.cppreproduced the lifetime failure; the fixed file was restored andlibkernelrebuilt before the final passing tests.On Windows, I restricted the parent test process to affinity mask
1for the single-CPU comparison; its child CTest/test processes inherited that restriction.339ee7999e82b1cbb51d01c6239d37c8a556583bpassed the full default Release build withBUILD_TESTING=ON, including patched libraries. The same ten selected non-graphics kernel tests passed with zero failures and zero skips. The updated regression passed 20 additional runs normally and 20 withtaskset -c 0. The previous test version, run with the same fixedRwlock.cppunder the one-CPU restriction, timed out at 20.69 s.RelWithDebInfobuild with the existing-DANYPS5_ENABLE_ASAN=ONoption built the regression and its dependent libraries. With the originalRwlock.cppfrommainand the updated test, the first run reportedheap-use-after-free. I restored the fixed file, rebuiltlibkernel, and observed 100/100 passing normal runs and 20/20 withtaskset -c 0.ASAN_OPTIONS=detect_leaks=0:halt_on_error=1focused this check on invalid memory access. The production file and test hashes matched the validated integration source at the end; the Linux source checkout was clean.On a regular Linux build, this race can pass without a visible failure. A passing unsanitized run alone does not establish protection against a future lifetime regression. The independent native Linux review covers the previous head
f2b4a71aedd082c90916c7b0464496e36c5a89ce; those are the reviewer's measurements.Linux graphics/Vulkan tests: NOT RUN — intentionally excluded from VirtualBox VM validation. Native/bare-metal Linux was not tested by me. Native Linux GPU validation: not tested. No title or homebrew was run. Full hosted PR builds require collaborator dispatch under
CONTRIBUTING.md; skipped jobs do not establish a build result.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)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T06:44:34Z.
Review: COMMENTED
Native Linux run on exact head
f2b4a71aedd082c90916c7b0464496e36c5a89ce, applied onmain5d9dcc42d(no other PR needed on Linux).Machine: Intel i7-8700 (6C/12T), Ubuntu 26.04, GCC 15.2, Ninja Release,
BUILD_TESTING=ON, full build.^guest_(pthread_rwlock|rwlock_retire|pthread_cancel.*|semaphore|sync_on_address|kernel_sync|once|pthread_barrier)$): 10/10 pass.guest_rwlock_retirewith--repeat until-fail:20: 20/20.ctest -E "^agc|gpu|vulkan|video_out"): 649/651, same asmain(648/650) plus the new test. The two failures (file_writer_binary,file_writer_text) also fail onmainon this machine.The regression test does not catch the bug on a plain Linux build. With only the test applied to
main(originalRwlock.cpp),guest_rwlock_retirepassed 200/200 under--repeat until-fail:200. On glibc,NotifyAllon the freed object locks astd::mutexin memory that was just freed but not yet reused, so nothing faults. It is a real use-after-free, though. I built the test withRwlock.cppandTimedWait.cppunder AddressSanitizer (g++ -fsanitize=address):Rwlock.cpp: 20/20 runs reportheap-use-after-freeinTimedWait::Condition::NotifyAll()called fromscePthreadRwlockUnlock(Rwlock.cpp:159). The memory was freed byscePthreadRwlockDestroyon the successor thread.Rwlock.cpp: 100/100 clean.So the fix is confirmed on Linux too, but the test only fails on Windows (or under a sanitizer). Linux CI will not catch a regression.
A smaller point: both threads busy-wait (
while (phase.load() == 1) {}and theTrywrlockloop). Pinned to one core (taskset -c 0), the test with the fix took 23.9 s, over the test'sTIMEOUT 20. On 12 threads it takes 0.02 s. Waiting onphaseinstead of spinning would avoid that on a 1-vCPU VM or runner.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T06:49:14Z.
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.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-16T08:53:21Z.
@jotitadev Thanks for the native Linux checks and the single-core reproducer.
I addressed the spinning in
915e9f2d5: the phase handshake now usesatomic::waitwith a matching notification, and theTrywrlockloop yields afterEBUSY. The successor still immediately unlocks and destroys the acquired lock, preserving the lifetime race.I reproduced the original test's single-CPU timeout on Windows (20.06 s) and Linux (VirtualBox VM, 20.69 s). The updated test passed 20/20 single-CPU runs in each environment; Windows runs took 0.03-0.12 s.
With the existing
ANYPS5_ENABLE_ASAN=ONconfiguration in Linux (VirtualBox VM), the updated test reportedheap-use-after-freeagainst the originalRwlock.cpp. The fixed implementation passed 100/100 normal runs and 20/20 single-CPU runs. Full builds and all ten selected kernel tests also passed on Windows and Linux (VirtualBox VM), using integration commit339ee7999e82b1cbb51d01c6239d37c8a556583b(this head plus #6452's AGC test fixture correction).I updated the PR description with the comparisons and commands, including the limitation that an unsanitized Linux pass does not reliably detect this regression.
AI-assisted: yes — Codex.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T09:04:01Z.
Checked the
Rwlock.cppchange by reading:NotifyAllnow completes while_stateis held in bothscePthreadRwlockUnlockand the timed-writer timeout path, so a successor (whoseTrywrlock/Wrlockneeds_state) cannot destroy the lock mid-notify. The regression test is registered in ctest, usesstd::abort(effective in Release) and no longer spins. Merged.