fix(libkernel): initialize guest rwlocks before they are shared #2275
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2275"
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 mrk1987 on 2026-10-09T14:13:51Z.
What
On Windows, guest
pthread_rwlock_*locks can fail to exclude. Two threads can hold the write lock at the same time, or a reader can run during a write.A guest rwlock is a
PthreadRwlockPrivateholding astd::shared_timed_mutex, created lazily on the guest's first call. With MinGW-w64 libstdc++ that mutex wraps a winpthreadspthread_rwlock_tset toPTHREAD_RWLOCK_INITIALIZER, which winpthreads turns into a real lock on first use. That first use is not safe when it is concurrent (winpthreadsrwlock.c):rwlock_static_initreturnsEINVALwhen it finds the lock already initialized. The second of two threads that both saw the initializer getsEINVALfrompthread_rwlock_wrlock/rdlock.pthread_rwlock_initstoresNULLbefore it publishes the new lock. A thread that reads it in that window also getsEINVAL.shared_timed_mutex::lock()/lock_shared()only__glibcxx_assertthe result, so in release builds the failed lock returns as if it succeeded.Standalone, on the project toolchain (GCC 15.2.0
x86_64-ucrt-posix-seh), with no AnyPS5 code involved:pthread_rwlock_rdlockon aPTHREAD_RWLOCK_INITIALIZERlock: 19,991 of 20,000 rounds returnedEINVAL.lock()on a newstd::shared_timed_mutex: both were inside the exclusive section in 1,732 of 20,000 rounds.The guest rwlock is now locked and unlocked once in its constructor, on the creating thread, before any other thread can reach it, so the winpthreads lock exists before it is contended.
docs/dev/TechnicalDebt.mdrecords the winpthreads behaviour and the hoststd::shared_mutexobjects that remain exposed.guest_pthread_rwlockgets a case where two guest threads race their firstpthread_rwlock_wrlockon a fresh lock, 500 times, and counts overlapping exclusive sections. On currentmainit fails (3/3 runs). With this change it passes (5/5 runs).Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release/Ninja on upstream main
d70b8998.guest_pthread_rwlockas above.ctest: 507/507 pass (1 skipped:guest_video_out_wqhd_detection_no_vulkan).tools/check_conventions.pyandgit diff --checkpass.pthread_rwlockwas read while another thread inserted into it. CDB showed the lock's winpthreads handle asNULLwhile a thread was inside its write section. With this change that crash no longer occurs (0 of 2 runs; it had occurred in every run that reached that point).Checklist
main; no other open PR implements the same functions🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T14:50:06Z.
Review: COMMENTED
First, the bar this has to clear: a
libkernelchange is merged when a title and the log line where that title goes wrong are shown — and this body names no title. If the impact is genuinely universal rather than one title's (any Windows title that shares a guest rwlock between threads), say that explicitly and show the measurement: the project has merged universal-impact changes on exactly that basis, with an occurrence count or a backtrace. Right now the strongest available framing is unwritten.Your cited mechanism is accurate — I checked it against the linked source rather than taking it on trust. In winpthreads
rwlock.c:So the second of two threads that both see the initializer does get
EINVAL, and theNULLstore happens before publication. Both halves of the description check out.What I measured on Linux (build clean, fresh test binary — I removed it first, since a stale one would have made this meaningless):
guest_pthread_rwlock_testspasses (exit 0).Pthread.hppreverted tomain, test kept): also passes (exit 0).That is not a gap in the test — it is the shape of the defect. The test's assertion (
overlaps == 0) is real, but on Linux the path under it cannot fail: the race is in winpthreads' static-init, which glibc's pthreads does not have. So the honest reading of my run is "no regression on Linux", not "the fix works" — the discriminating run is a Windows one, and there isn't one here.Which also means I cannot confirm the
TechnicalDebt.mdentry removal from this box: it is justified exactly as far as the Windows fix is real.Two notes that may help it land: the eager
lock(); unlock();in the constructor is the right place for it (the lock object is constructed before it can be shared, so no thread can observe an uninitializedshared_timed_mutex), and that is worth one sentence in the body — it is the part a reviewer will want to see argued rather than inferred.Not verified here: the Windows symptom itself, and whether the winpthreads version bundled by the project's MinGW-w64 is the one this file describes.
Written with AI assistance.