fix(libkernel): measure the Apple guest stack at the call #16790
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16790"
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 wanderingbug on 2026-11-07T16:41:25Z.
What
Open #16801 (
fix/macos-test-compile,38bddbff44) edits onlyResourceRequest.cpp,RtcpTransportFeedback.cpp, andGuestCesGbSetters.cpp:<initializer_list>andResourceRequestBasevalue initialization. None of those symbols are in this pull request.Darwin has no
pthread_getattr_np.CallGuestOnMeasuredStacksamples the stack pointer, reads the existingHostStackLimitswindow, and calls the guest from that window. The extra host margin is Apple-only. The test isMeasuredGuestStack, with its own header and CMake entry.This replaces closed #12112. That patch edited
GuestThreadSelf.cpp. This one does not. Open #7804 also queries the Apple stack limits and is not in this branch.HostStackLimitsis already onmain, so this is not a code dependency on #7804. The Mac integration build did include test-only #7804 (5e944037) and #4630 (5f4bbc87).Tested
measured_guest_stackexited 0 in one cache-reuse run on an Apple M2 Max, macOS 27.2, x86_64 under Rosetta. The tree was snapshot7a6e44ece4plus overlays671d12cf,5f4bbc87,42f65a4b,e074d9c7,203b4744, ande72d276e. This head was not rebuilt by itself on currentmain. Not tested on a second machine; no game tested.Checklist
main; no other open PR implements the same functions. Unchecked: parent is30a863d12d, and upstreammainis nowdaad891ea6. #16801 does not implement these functions. Other open pull requests were not fully scanned.//comments. The debt note is indocs/dev/debt/libkernel.md..mdfiles or images added to the repository. Existing development documentation is edited; maintainer confirmation of this rule is still needed3rdparty/change.Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T17:07:46Z.
Review: COMMENTED
Two points from reading
bd60dd0d5b; not run here, since I have no Mac.stackAddressandstackSizechange after the thread is published.StartNativeThreadsetsstackAddressbeforeinitialized.set_value(), and the creator gets the handle from there. On Apple,CallGuestOnMeasuredStackthen writes both fields again from the new thread, without a lock.scePthreadAttrGet(Pthread/src/Attr.cpp) andpthread_attr_get_np(Pthread/Posix/Attr.cpp) read them underCpuClocks().lockthroughLockLiveGuestThread, so those reads and this write race. It is also visible without any race: for a thread created suspended,scePthreadAttrGetbeforescePthreadResumereturns the old window, the one the test shows does not hold the guest frames, and after the resume it returns another one. Writing the two fields underLockRegisteredGuestThread(self)would fix the race. The suspended case would still need a debt note, or the window chosen beforeinitialized.set_value(). Also, withRegisterStackmoved intoCallGuestOnMeasuredStackon Apple, a suspended thread is not inliveStacksuntil it resumes.MeasuredGuestStack.cpppasses without the reserve check.Require(overrun.low < shortHostLow + hostMargin || overrun.high == 0)is also true for a window that runs into the host margin, which is exactly the result the check exists to prevent. With theif (low < hostLow + hostReserve) return window;line removed fromMeasureGuestStackWindow, all four checks still pass in a Python model of the function, and the overrun case returns a window inside the margin.Require(overrun.high == 0 && overrun.low == 0)would catch that.A smaller one:
HostCallDepthmeasureshigh - rspin a probe thread's start routine, which is about whatMeasureStackReservemeasures, plus a page. It is not measured at the call inCallGuestOnMeasuredStack, belowStartNativeThread(with itsjmp_buf) andRunThread. The debt note's "the measured depth of a host probe at that call" overstates it: the extra page is an assumption.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-11T10:17:43Z.
Review: CHANGES_REQUESTED
Read through the diff; nothing was run (no Mac here, and the Apple path is
#ifdef __APPLE__). Points to fix:core/libs/prx/libkernel/Pthread/src/Thread.cpp:988-990:stackAddress/stackSizeare rewritten from the new thread afterStartNativeThreadalready published them, without a lock.scePthreadAttrGetandpthread_attr_get_npread them underLockLiveGuestThread, so this races. Write both underLockRegisteredGuestThread(self), or choose the window beforeinitialized.set_value().Thread.cpp:990/:1006: movingRegisterStackintoCallGuestOnMeasuredStackleaves a thread created suspended out ofliveStacksuntil it is resumed, andscePthreadAttrGetbefore the resume returns the old window. Fix it or record it in the debt file.core/libs/tests/MeasuredGuestStack.cpp:44:overrun.low < shortHostLow + hostMargin || overrun.high == 0is also true for a window that runs into the host margin, which is what the reserve check prevents. With thelow < hostLow + hostReserveline removed fromMeasureGuestStackWindowall checks still pass. UseRequire(overrun.high == 0 && overrun.low == 0).CallGuestOnMeasuredStack/HostCallDepth; please say in Tested how the Apple path itself was verified on this head (the reported run was a different tree).docs/dev/debt/libkernel.md:267: "the measured depth of a host probe at that call" is not accurate:HostCallDepth(Thread.cpp:861-875) measures in a probe thread's start routine, not belowStartNativeThread/RunThread, and the extra page is an assumption. Say so.main, tick the checklist, and fillFixes/Unblocks.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:02:58Z.
Review: COMMENTED
Reviewed — no blocking findings
The Apple-specific stack placement and its focused unit test are present in the reviewed head; the source and test code were read, but no build or test was run in this review.
What I ran
Reviewed head
bd60dd0d5b5ef909a00bbc1da9e62ad93b9fc336against basemain. Usedrepo_read.pyto inspect the prepared diff and changed-file source ranges; no commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
HostStackLimitsis already onmain. The reviewed diff callsHostStackLimitson the Apple path, but this review did not verify the base history or #7804's status.Not verified
No build, automated test, or runtime behavior was verified.
description.mdandcommits.mdwere read. All five changed files listed inmanifest.jsonwere read at the relevant changed lines, including the stack helper, the changed thread functions, the new test, the CMake test registration, and the debt note. No changed files were excluded.To get this to a good state
Nothing is required. Optional: reconcile the debt note's #7804 dependency statement with the current base and clarify the reported test tree's relationship to this head.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:20:33Z.
Review: COMMENTED
I compiled and ran the branch's
MeasuredGuestStackfixture on Linux. It passed the standalone window arithmetic checks. The branch conflicts with main, and the actual pthread stack measurement at the call site still needs macOS coverage.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.