fix(libkernel): report the precommitted stack of an adopted thread on Windows #5918
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5918"
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 karimibrahim07 on 2026-10-14T15:39:29Z.
What
Java 25 LAN / DNS (PPSA99869, #5501) runs on
mainsince #5643, but its Java VM prints on screen:HotSpot (the PS5 port in mateo-cogeanu/ps5-java25-homebrew, patch
0019-ps5-preserve-native-reservations) commits the guard pages at the bottom of the main thread's stack withmprotect(bottom, 48 KiB, PROT_READ | PROT_WRITE), the bottom coming frompthread_attr_get_np. Its trace shows the call failing:The main thread is adopted (
scePthreadSelf), and on WindowsSetStackFromHostreports its whole host reservation, assceKernelIsStackdoes for a host thread before adoption. A relinked executable reserves 0x210000 bytes for that stack and commits the top 0x200000 (WindowsPeWriter.cpp), so the lowest 64 KiB of the reported stack hold only reserved pages and Windows' guard pages, and the hostmprotectinDoMprotect's guest stack branch fails there (EINVAL).Threads AnyPS5 creates already report only their committed stack (999a723a5).
HostStackLimitsnow does the same for a host thread when its stack is laid out that way: if everything above the bottom allocation granule is committed read-write, the stack starts there. Any other host stack keeps the whole range, so a stack that is not precommitted, like a test executable's 4 KiB commit, does not shrink. The top of the stack does not move, so collectors that scan from the top (Unity IL2CPP, #761) are unaffected. The committed-pages walk is shared withStartNativeThreadand throws whenVirtualQueryfails.The technical-debt entry records that host threads whose stack is not precommitted, and host threads on Linux, still report lower pages that
mprotectcannot change.Tested
Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7, Release, on
mainf3434892b.guest_thread_selflinks with the relinker's stack sizes (--stack=0x210000,0x200000) and checks, on the main thread and on a default-size host thread (throughsceKernelIsStackbefore adoption andscePthreadAttrGetafter), that the reported stack is committed read-write throughout and that its lowest page can be made read-write, inaccessible and read-write again. A host thread that commits only 64 KiB must report exactlyGetCurrentThreadStackLimits. The test fails without the change (0xc0000409), with the trim applied only at adoption, and with the trim applied regardless of what is committed; it passes with the change (3 runs).ctest(--parallel 4 --timeout 120): 1191 of 1192 pass from either shell. Fromcmd,check_main_syncfails because the file name it prints is not decoded as UTF-8; from Git Bash,windows_trace_importsfails because Defender deletes its generated executable in Git Bash's temporary directory. Each passes in the other shell, and this PR touches neither.python tools/check_conventions.py --base origin/mainpasses;tools/pr_overlap.py --branchreports no overlap with open pull requests.PPSA99869-lan-dns-pass-01.000.000.zip, SHA-2563cfeed2f…11cc, as in #5501), relinked with--windows --to-intel --trap-sdk-syscall-stubs --registry:mainwith #5643:FAIL: Native RW memory commit addr=0x400000 bytes=49152 mode=3 errno=22and the warning aboveDONE: Native RW memory commit addr=0x410000 bytes=49152 mode=3 errno=0, no warning,JNI_CreateJavaVM returned 0, andDestroyJavaVMcode 0. The LAN echo fails as before without its helper on a Mac; the DNS stage passed onmainwith #5643 and was not rerun with this change.DestroyJavaVM, HotSpot now also removes the main thread's guard pages, and that fails as it already does for the VM's other threads:Attempt to deallocate stack guard pages failed (0x0000000000410000-0x000000000041c000). HotSpot's BSDos::pd_uncommit_memorymaps fresh pages there withmmap(MAP_FIXED | MAP_NORESERVE | MAP_ANONYMOUS, PROT_NONE), whichmmap_nid_postfixdoes not accept for these pages, so those 48 KiB stay inaccessible after the VM is destroyed. That is a separate fix.Linux not tested; its path is unchanged.
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 CnRJay on 2026-10-14T17:24:27Z.
This overlaps in effect with #5703, which is about the same Windows host-stack layout from the other side. #5703 makes
DoMprotectcommit the reserved pages of a non-arena range before protecting them. That alone would probably let HotSpot'smprotect(bottom, 48 KiB, RW)succeed at the bottommainreports now. This PR instead moves the reported bottom up to the committed part.The difference a title can see is the reported stack bottom itself. Here it moves up by up to 64 KiB, so a collector or guard-page logic that uses
pthread_attr_get_np's range sees a smaller stack. With #5703 the range is unchanged and the pages are committed on demand. As you note, the top is unchanged, so top-down scanners are fine. It would help the maintainers if you and @EagleBearerYS agreed whether both are needed, or which model to keep.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T17:32:16Z.
Our case on Resident Evil Village (PPSA01556), to make the comparison concrete. The protect that stops is on thread 23012's own stack. A local trace of
DoMprotectshows the range is exactly the stack AnyPS5 reports for that thread:It's a default-size host thread, not one AnyPS5 creates: its lowest 0xd000 bytes were still only reserved, and threads AnyPS5 creates are precommitted since
999a723a5. So this PR's rule should apply to it, and Village would then protect only committed pages. We'll boot Village with this PR in place of #5703 and post whether it passes that stop. @karimibrahim07, could you try PPSA99869 with #5703 in place of this PR?On fidelity: on FreeBSD a thread's reported stack is all mapped, so a title can read or protect any part of it. On Windows the bottom of a host stack is only reserved, so neither holds there today. This PR reports only the committed part; #5703 keeps the reported range and commits what a protect needs. If one of them alone covers both titles, keep that one. If each covers only its own, keep both.
Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-14T20:18:21Z.
@EagleBearerYS here is PPSA99869 with #5703 in place of this PR, on
main55549e789, with the same setup as in the description:main+ #5703: HotSpot now commits its guard pages at the old bottom (DONE: Native RW memory commit addr=0x400000 bytes=49152 mode=3 errno=0). "Attempt to allocate stack guard pages failed" no longer appears,JNI_CreateJavaVMreturns 0, DNS printsJAVA_DNS_OK 1 addresses, andDestroyJavaVMreturns code 0. LAN fails as before, since it has no helper. So #5703 alone covers this title.main+ #5703 + #5918: the commit is at0x410000(errno=0), where #5703 finds nothing left to commit, and every stage gives the same result. In the first of two runs the DNS lookup went past the app's own 8-second limit; the rerun passed.guest_kernel_stack_protect,guest_thread_selfand the fullctest(1219 of 1219) pass on that tree.DestroyJavaVMstill prints "Attempt to deallocate stack guard pages failed" for the main thread, as it does for the VM's other threads. The address is0x400000with #5703 and0x410000with this PR.@CnRJay, the part only this PR changes is what the reported range contains. On a relinked executable's main thread and on default-size host threads, the lowest 64 KiB of the reported stack are 0xd000 reserved bytes and 0x3000 bytes of Windows guard pages. With #5703 alone they stay that way until something protects them, so reading the reserved part faults. With this PR, the stack that
pthread_attr_get_npandsceKernelIsStackreport for those threads is committed read-write throughout. That's also the case on FreeBSD, and for threads AnyPS5 creates since999a723a5. In the other direction, this PR leaves the whole range for a host thread whose stack isn't precommitted, like theSTACK_SIZE_PARAM_IS_A_RESERVATIONthread in #5703's test, and only #5703 makes a protect work there.The two PRs don't conflict and cover different cases, so I'd keep both. If the maintainers would rather take one, #5703 alone fixes this title. If both land, I'll update this PR's debt line, because
mprotectwould then commit those lower pages on Windows.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T20:23:12Z.
Thanks for running it both ways. Agreed, they don't conflict and cover different cases:
So both can land. Our Village boot with this PR in place of #5703 is still queued; I'll post whether it gets past that stop.
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T20:34:03Z.
The Village boot: Resident Evil Village (PPSA01556) with this PR in place of #5703 gets past that stop.
Setup:
maine34c47921, which has #5698;22091410aand #5815's two commits;What changed:
0x16ace0000+0x210000in an earlier run. It now covers only the committed part,[memory] protect 000000fcdeca0000+0x200000 prot=0xc2, and it succeeds.So each PR alone covers both titles, PPSA99869 and Village.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:11:39Z.
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.
main)core/libs/prx/libkernel/Pthread/src/Thread.cppL173-174, L676-683docs/dev/debt/libkernel.mdL137-138Some 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/Pthread/src/Thread.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/tests/GuestThreadSelf.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by dogukanatlihan on 2026-10-15T18:53:47Z.
I reproduced the remaining
DestroyJavaVMguard-cleanup warnings using the public LAN_DNS_Probe-v01.000.000 release, PPSA99869. A local libkernel patch removes both warnings while preserving fresh-anonymous-memory semantics.The release's
os::pd_uncommit_memorycallsmmap(addr, size, 0, 0x1012, -1, 0). Current fixed-anonymous handling requires GuestArena ownership, so it rejects the native Windows stack ranges. This is separate from the stack-bound reporting change here. The pinned BSD OpenJDK implementation and the release's reservation-preservation patch require replacement with fresh PROT_NONE pages; returning success or changing protection alone would retain old contents.The patch permits fixed anonymous replacement within one owned native Windows stack, holding the stack ownership lock through validation and replacement. It preserves the reservation, rejects foreign/cross-boundary/native PAGE_GUARD ranges before mutation, and decommits/recommits the target pages. It also preserves protection tracking on both sides of partial replacements so thread cleanup restores them correctly. Only POSIX fixed anonymous mmap opts into this path; other mapping APIs keep their previous behavior. Arena host-allocation holes are excluded from arena ownership. Current-stack replacements must end at least 64 KiB below active host frames; peer-stack callers must coordinate with the peer. No additional mmap flags are enabled.
Windows 11 / WinLibs GCC 15.2.0 results:
libkernel.prxdiffers across 174 runtime files. Both evidence runs use silent SDL audio and controller-file input; LAN/DNS rows are not run.Baseline:
Patched:
The patch is based on main
44df21632125dbe3113a19173e7ebb879da53003. Builds reusedfa9365ca74acae7c594618b0a857365774cc6f45plus the existing #6452 AGC test-alignment fix; all six affected source files have identical base blobs at these commits, and the built patched copies match the prepared sources by SHA-256. The code conventions checker passes. Linux GCC syntax checks pass for the changed translation units/test, but Linux runtime, host commit-failure injection, concurrent teardown stress and PS5 hardware behavior were not tested. A failed recommit returns an error and leaves the reservation intact; thread cleanup attempts to recommit it.Release ZIP SHA-256:
3cfeed2f321334e742fa93070e6983bed201332f7837d907af2db52d547c11cc. The app remains open after VM destruction; the harness terminates it after recording completion. No application-exit or performance claim is intended.Patch against main
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T16:55:37Z.
For coordination: I opened #6893 with the guard-size attribute-query fix. It shares
Pthread/src/Thread.cppandlibkernel/CMakeLists.txtwith this PR, but changes different behavior: #5918 adjusts reported stack bounds for adopted Windows threads; #6893 stores and reports the creation guard-size attribute. I tested the combined trees on currentmain66e290f1c; both changes apply cleanly, with no merge conflict or dependency between the PRs. Let me know if you see a runtime interaction that needs a specific order.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:40:58Z.
Checked the diff, the shared committed-pages walk with
StartNativeThread, and the debt entry (still true now that #5703 is onmain).guest_thread_selfand the libkernel/libs ctest scope pass on Linux with this merged onto currentmain(the new code is_WIN32only); the Windows behaviour rests on your run and @EagleBearerYS's Village boot, and the full Windows build is dispatched before merging. Merging.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T19:59:31Z.
Checked the diff, the shared committed-pages walk with
StartNativeThread, and the debt entry.guest_thread_selfand the libkernel/libs ctest scope pass on Linux with this merged onto currentmain(the new code is_WIN32only); the Windows behaviour rests on your run and @EagleBearerYS's Village boot, and the full Windows build is dispatched before merging. Merging.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:35:03Z.
Re-checked against current
main: same diff, no conflicts,guest_thread_selfand the libkernel/libs ctest scope pass on Linux (the new code is_WIN32only). The Windows behaviour rests on your run and @EagleBearerYS's Village boot; the full Windows build is dispatched before merging. Merging.Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T05:27:13Z.
@MarcZX100 I checked the interaction on Windows and found nothing that needs an order. I merged this PR and #6893 (
ece11f830) together ontomain16c607a93, along with my other open PRs. Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7, Release.guest_thread_self,guest_pthread_guard_sizeandguest_kernel_stack_protectpass,--repeat until-fail:2.JNI_CreateJavaVM returned 0,Hello25 returned normally,DestroyJavaVM complete). There is no failure to protect guard pages. The threeAttempt to deallocate stack guard pages failedwarnings at teardown are the onesmainprints too, and #6534 addresses them.The two changes touch different state: #6893 stores and reports the attribute's guard size, and this PR moves the reported bottom of an adopted host stack. Neither reads what the other writes.