fix(libkernel): refresh adopted thread bounds when entering the main stack #15436
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15436"
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 JJenkx on 2026-11-03T09:38:48Z.
What
When a module calls
scePthreadSelfbefore the Linux guest entry runs, the adopted thread records the original host-stack bounds.EnterGuestMainStack_nid_no_patchsubsequently moves execution to the guest stack but leaves that record unchanged. Stack queries and collectors can then combine a guest-stack pointer with the old host-stack end.Refresh the existing thread's stack address and size under the live-thread registry lock before entering the mapped main stack. Preserve the thread identity and the existing stack address, guard and argument-passing behavior. The default late-adoption path remains covered.
The regression explicitly adopts the thread before entry, then checks
sceKernelIsStack, thread identity, both SCE/POSIX attribute queries, the stack guard and a worker stack. A separate CTest case retains the original late-adoption path. This follows the merged #7532 and the startup evidence reported there.Tested
Linux 7.2.11, Ryzen 9 7950X, GCC 16.2.1, Release. Base
4f41f4c375f5abb487dbd184fbc150f31994d762, change4199ddb20550a83466b64df7caa938c5f1fc82fe.guest_main_stackpasses andguest_main_stack_adoptedfails. Zero skips.guest_kernel_is_stack,guest_main_stack,guest_main_stack_adopted,guest_pthread_attr,guest_pthread_attr_stack,guest_pthread_attr_errors,guest_pthread_start,guest_pthread_stack. Zero skips/failures.pr_overlap.py --base main --branch fix/linux-main-stack-adopted-attributes-20261103scanned 1,668 open PRs. It could not automatically compare 35 conflicting older branches; their reported shared-file diffs were inspected manually. They change other platform/thread/I/O paths or add unrelated tests; none supplies this main-stack refresh or the new regression. Merge-tree against the fetched current main is clean.2018ff198377cb5b572c344490a87cd282b420f966e599c8c6f0766c94d6660e.714cd5b9c1265631d13ea7fe40d38b8f4120d6edfaf816ab2a02b5f31ec77000, verified against GitHub's asset digest. No title binaries, assets, saves, raw memory or raw shader logs.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 XairRrR on 2026-11-03T09:47:32Z.
Review: APPROVED
Reviewed — approved
When a thread was adopted through
scePthreadSelfbeforeEnterGuestMainStack_nid_no_patch, it kept its old host-stack bounds; the fix updates them, the newguest_main_stack_adoptedtest fails without it and passes with it, and I found nothing blocking.What I ran
Head
4199ddb20550a83466b64df7caa938c5f1fc82fe,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged treedeb724ce6859ce62f57eb7d54bdab12c59683310(Debian 13 VM, Xeon Gold 6136, GCC 14.2.0, Release, Ninja).Results
git diff --checkcheck_conventions.pymain(AGC denormal/division,guest_interfaces, multicast, …) are unrelated to this PR. Skip report:guest_net_ctlis an unexpected skip, and it is skipped onmaintooguest_pthread_start, 1.87 s)guest_main_stack_adoptedfails,guest_main_stackpassesNegative control output (production file from
main)Negative control
I restored only
core/libs/prx/libkernel/Pthread/src/Thread.cppfrommainand rebuilt.guest_main_stack_adoptedfailed atGuestMainStack.cpp:68:sceKernelIsStackreports the old host-stack end where the guest main-stack top should be.guest_main_stack, the late-adoption path, still passed. This matches the description. After I put the merged file back and rebuilt, both tests passed, andgit status --porcelainwas empty.Code review
Thread.cpp:257-262:CurrentStack(Thread.cpp:414-421) preferscurrent->stackAddress/stackSizeoverHostStackLimits. So an adopted thread kept its host-stack range after the switch, whileHostStackLimitshad already moved to the guest main-stack range. Updating both fields fixessceKernelIsStack,GuestThreadStackand both attribute getters together.scePthreadAttrGet(src/Attr.cpp:93-96) andpthread_attr_get_np(Posix/Attr.cpp:25-29) read the same fields underLockLiveGuestThread. Theunique_lockis scoped to theifblock, so it is released before thenoreturninline asm.liveStacks(RegisterStackis only called fromRunThread,Thread.cpp:898), and they have noguestStackmapping. So no other stack record needs updating.__linux__block that begins atThread.cpp:138), and so is the CMake registration. It contains no comments and no title-specific logic. The new test case finishes in about 0.01 s.PPSA08102 #9744), GPUs testedn/a(correct for a CPU-only change), and AI-assisted.Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/Pthread/src/Thread.cpp:259).guestMainStackLow/guestMainStackHighare published (Thread.cpp:255-256).BindHostCpuClockand is never marked finished while it is running.stores, so the failure leaves no state behind.Not verified
pr_overlap.pyresult: I had no network to run it.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-03T09:54:47Z.
Review: COMMENTED
Reviewed — no blocking findings
I reviewed all three changed files at head
4199ddb20550a83466b64df7caa938c5f1fc82fe; the stack-bound update is protected by a scoped registry lock that ends before the stack switch, and the supplied test logs support the new regression case, but hosted checks are still pending so this is a COMMENTED result.What I ran
Reviewed exact head
4199ddb20550a83466b64df7caa938c5f1fc82feagainst base4f41f4c375f5abb487dbd184fbc150f31994d762. The exact-head merge preview against that base is clean. Environment: Debian 12, Linux x86_64, Python 3.11.2, Git 2.39.5, GCC 12.2.0; I did not compile with GCC. I read the full three-file diff, the thread-registry lock implementation and readers, the test registration, and the attached evidence archive. The archived test logs are author-provided evidence; I inspected but did not run them.Results
Commands and unedited outputs
Precheck summary:
Precheck table:
The precheck deliberately does not configure, build, or compile AnyPS5.git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...4199ddb20550a83466b64df7caa938c5f1fc82fe/usr/bin/python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 --head 4199ddb20550a83466b64df7caa938c5f1fc82fe --title fix(libkernel): refresh adopted thread bounds when entering the main stack --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-15436-4199ddb20550/pr-body.md/usr/bin/python3 tools/pr_abi_audit.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 --head 4199ddb20550a83466b64df7caa938c5f1fc82fe --jsonNo changed Python implementation files.No changed tools/ module has an existing sibling tools/tests/test_<module>.py suite.No changed test_*.py files./usr/bin/python3 /marc/AnyPS5/tools/pr_stack_check.py 15436 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-15436-4199ddb20550/stackNot run: C++ tests need exact-head binaries, which require a build; the precheck does not build./usr/bin/python3 /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-15436-4199ddb20550/head-4199ddb20550-82ea9f88/tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-15436-4199ddb20550/head-4199ddb20550-82ea9f88 --json core/libs/prx/libkernel/CMakeLists.txt core/libs/prx/libkernel/Pthread/src/Thread.cpp core/libs/tests/GuestMainStack.cppNo changed Python test module in tools/ can be run as a no-build base-source control.The precheck's two helper failures are snapshot-tooling limitations:
tools/pr_abi_audit.pyandtools/pr_test_quality.pyare not in the isolated PR snapshot. The ABI audit is not applicable to this diff. The precheck stderr for those rows was:I ran the workspace test-quality helper against the exact snapshot; its raw output was:
The exact-head CMake registration inspected manually is
add_test(NAME guest_main_stack_adopted COMMAND guest_main_stack_tests adopted). Stack report:/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-15436-4199ddb20550/stack/stack-report.md; it reports a clean merge preview and no declared dependency.Archive checksum output:
GitHub release asset metadata output:
Candidate focused log:
Baseline new-tests log:
Negative control
guest_main_stack_adoptedfailing on baseline; candidate log reports it passing.Findings
Blocking
None found in the three changed files.
Non-blocking
digest: nullfor the asset, so I could not independently verify the description's claim that this checksum was checked against a GitHub-supplied digest. I did not independently reproduce the Linux build, CTest run, or Outer Wilds run.Sources
Not verified
I did not build or run CTest, reproduce the baseline negative control, test Windows or macOS behavior, run Outer Wilds, or test on a PS5 console. The archived GPU/title diagnostics and CPU test logs are supplied evidence, not runs performed by me. No performance claim is made.
To get this to a good state
No source change is requested. Let the remaining applicable hosted checks complete. If the head changes, I will rerun the diff, conventions, precheck, merge preview, test-registration inspection and relevant tests on that new head before deciding whether approval is warranted.
Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-11-03T11:58:15Z.
Review: APPROVED
I wrote #7532, which caused this regression, so I built and ran the change to back up the logs in the archive.
What I ran: head
4199ddb205on main4f41f4c375(the PR's parent), Arch Linux 7.2.11, GCC, Release, Ninja. I built the test targets and ran them withctest --output-on-failure.guest_main_stackguest_main_stack_adoptedThread.cppfrommain(new test, old code)GuestMainStack.cpp:68The failing check is
Require(high == ConsoleMainStackTop). On the old code,sceKernelIsStackreports the host stack's end as the top of a thread that was adopted before the switch. A scan up to that end runs past the mapped guest stack, which matches the read 280 bytes aboveGUEST_MAIN_STACK_TOPin the report on #7532.With the fix, all eight selected cases pass (
100% tests passed out of 8):guest_kernel_is_stack,guest_main_stack,guest_main_stack_adopted,guest_pthread_attr,guest_pthread_attr_stack,guest_pthread_attr_errors,guest_pthread_startandguest_pthread_stack.Code: the update only runs when a thread was already adopted, keeps that record's identity, and holds the live-thread lock in a scope that ends before the stack switch. A thread adopted after the switch still reads the guest bounds as before, and
guest_main_stackcovers that path.Not verified: I didn't run Outer Wilds or any other title, and I didn't test Windows or macOS. The changed path is Linux-only.