Skip to content

fix(linux): enter the guest entry on a main stack at the console's address - #7532

Merged
oneandonlydean merged 13 commits into
boykopovar:mainfrom
hugodearaujo:fix/linux-guest-main-stack
Nov 3, 2026
Merged

oneandonlydean merged 13 commits into
boykopovar:mainfrom
hugodearaujo:fix/linux-guest-main-stack

Conversation

@hugodearaujo

@hugodearaujo hugodearaujo commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

On Linux the relinked executable entered the title's entry point on the host process stack, near the top of the address space (0x7ffc...). On the console a process's main thread stack sits inside the user area: a payload on a PS5 (FW 13.60) reports its main thread stack at 0x7eedfc000-0x7eeffc000 through pthread_attr_get_np, right above a worker thread's stack at 0x7eede8000-0x7eedf8000.

When the executable loads libkernel.prx, its Linux entry stub now calls EnterGuestMainStack_nid_no_patch (libkernel) through a GLOB_DAT slot, with the initial stack pointer and the entry point.

  • The helper maps the main stack in the guest arena so that it ends where the console's does (0x7eeffc000, moved by the arena's offset in ASan builds). Below it is a 16 KiB guard. Its size is the host's RLIMIT_STACK (8 MiB when unlimited, at most 1 GiB).
  • It calls the entry point there with the same arguments. sceKernelIsStack and the main thread's attributes report that range.
  • An executable that does not load libkernel.prx keeps the stub as it was.

#3965 does the same on Windows, with a helper in libc that is built only there.

Title

Assassin's Creed Mirage (PPSA13960, 01.007.000) first stops on main at a kevent read event, which #6343 implements. With #6343 applied, it stops with SIGSEGV on the main thread 6 s after start, with nothing in the log.

  • Its allocator finds the pool that owns a pointer through a table indexed by pointer >> 28 (eboot 0x570b9c1-0x570b9ec).
  • The main thread asks it about an object on its own stack. 0x7fffffff42c8 gives index 0x7ffff, so the lookup reads past the table and dereferences what it finds there (0x8232f00).
  • On the console the main thread's stack lies below 0x7eeffc000, which gives index 0x7e.

With this PR and #6343 it passes that point and goes through its start-up screens (photosensitivity warning, Ubisoft logo, intro video, disclaimer, autosave notice; screenshots from two runs) to the next unimplemented path:

AGC graphics: guest cube texture descriptor does not contain a multiple of 6 array slices (format 22, 128x128x7, type 11, tile 9, mips 0..0 of 1) (base array 0)

Tested

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA13960 Assassin's Creed Mirage (PPSA13960) #7534
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6149 core/relinker/elfpatcher/tests/GuestModuleArgsHost.cpp L54-63
PR 7594 core/relinker/elfpatcher/src/general/EntryStubBuilder.cpp L26-37

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.

Pull request Shared files not compared
PR 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6740 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7531 core/relinker/CMakeLists.txt
PR 7804 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 8592 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 9019 core/libs/prx/libkernel/CMakeLists.txt
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9843 docs/dev/debt/libkernel.md
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 10025 docs/dev/debt/libkernel.md
PR 10319 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10861 core/relinker/CMakeLists.txt
PR 11831 core/libs/prx/libkernel/CMakeLists.txt
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11939 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12104 docs/dev/debt/libkernel.md
PR 12130 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12560 docs/dev/debt/libkernel.md
PR 12642 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12678 core/relinker/CMakeLists.txt
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 12970 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 13560 docs/dev/debt/libkernel.md
PR 14103 docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libkernel.md
PR 14264 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 14299 core/libs/prx/libkernel/CMakeLists.txt
PR 14310 docs/dev/debt/libkernel.md
PR 14832 docs/dev/debt/libkernel.md
PR 14854 docs/dev/debt/libkernel.md
PR 14886 docs/dev/debt/libkernel.md
PR 14894 docs/dev/debt/libkernel.md

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, the design looks right, and the stub, the 16-byte alignment and the bounds query all check out. One small safety change before merge: EnterGuestMainStack maps the stack with DoMapAnon(&mapping, bytes, 3, GUEST_MAP_FIXED) and exclusive = false, so ReplaceFixedOverlap silently replaces anything already registered in [top - bytes, top). With a large RLIMIT_STACK (up to 1 GiB) that range reaches well below the console stack. Nothing should be there at entry, but a collision would be clobbered rather than reported. Could it pass exclusive = true, so an overlap fails with ENOMEM and a named error?

On order: as discussed on #7594, this lands first and #7594 rebases with its MXCSR set before call [rip+slot].

@oneandonlydean oneandonlydean self-assigned this Oct 18, 2026
@simonbbby

Copy link
Copy Markdown
Contributor

I reproduced Dean's collision concern and tested the requested exclusive call locally on exact head 51e5eae15b432788ec554811a80c60ad74337e97. Ubuntu 24.04, kernel 7.0.0-38-generic, GCC 13.3, Release.

With soft RLIMIT_STACK=1 MiB, an external probe registers a 16 KiB RW anonymous guest mapping through DoMapAnon(..., exclusive=true), fills every dword with 0xa55a1234, then calls the real EnterGuestMainStack helper:

Existing sentinel page Submitted head Local exclusive-call proposal
Inside usable stack, at top - 0x10000 Enters; sentinel becomes zero Refuses; all 16 KiB and RW protection preserved
Inside guard, at top - 0x104000 Enters; page protection becomes 0 Refuses; all 16 KiB and RW protection preserved
Immediately below mapping, at top - 0x108000 Enters; sentinel preserved Same
No collision Enters Same

The local production change is only:

DoMapAnon(&mapping, bytes, 3, GUEST_MAP_FIXED, PS5_PAGE_SIZE, true)

Both refused cases report EnterGuestMainStack: cannot map the guest main stack before entry. This fits the mapping path: without exclusivity, the partial overlap reaches fixed-overlap replacement; with it, mutation.Overlaps returns ENOMEM before any replacement/protection change. The adjacent-page control rules out blanket refusal of nearby registered mappings.

Boundary/guard checks, on both the submitted head and the local proposal: an extension of GuestMainStack.cpp changes the soft limit after process startup and verifies these reported usable spans:

  • 1 MiB → 1 MiB; 3 MiB + 1 byte → 3 MiB + 16 KiB; 8 MiB → 8 MiB; 64 MiB → 64 MiB.
  • 2 GiB → 1 GiB; zero and unlimited → 8 MiB.

All 14 processes pass. Every reported upper bound is 0x7eeffc000; each run also forks a child with core dumps disabled/default SIGSEGV handling, and an actual read one byte below usable stack is terminated by SIGSEGV. The parent creates/joins a guest worker and keeps the original separate-stack assertions.

guest_main_stack, linux_main_stack_entry and entry_stub pass (3/3) with the proposal. Separately, restoring only the original HostStackLimits host-query behavior makes the stock test fail its upper-bound assertion at line 68. Restoring the exact PR source and rebuilding returns the three tests to passing.

I restored the submitted source; the worktree is clean. The exclusive call above is a tested local proposal, not a pushed change. Generated-input/runtime-helper checks only: no Mirage/title, Windows, ASan-offset or full-suite result.

@hugodearaujo

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Sorry for the slow reply, I missed this comment. Done in cdfddff, on top of the existing head:

  • EnterGuestMainStack maps the stack with DoMapAnon(&mapping, bytes, 3, GUEST_MAP_FIXED, PS5_PAGE_SIZE, true), so an overlap returns ENOMEM before anything is replaced, and entry stops with EnterGuestMainStack: the guest main stack [low, top) overlaps a guest mapping. Any other mapping failure keeps the old message.
  • guest_main_stack has a case for it: it maps 16 KiB of flexible memory at top - 0x10000, fills it with a pattern, and checks that entry is refused with that message, that the page keeps its RW protection and its pattern, and that the normal entry still works once the page is unmapped. With the old non-exclusive call the case fails (entry goes ahead over the page).
  • The TechnicalDebt entry says an overlap stops the entry.

Linux (Arch, kernel 7.2.11, GCC 16.2.1, Release, RX 9070 XT with RADV, Mesa 26.2.5): full CTest 1439 of 1440, the failure being agc_driver_tessellation (#5444); guest_main_stack, linux_main_stack_entry and entry_stub pass.

@simonbbby Thanks for reproducing it and testing the exclusive call; the new case is your in-stack sentinel row.

@eliasbruno124-dev

Copy link
Copy Markdown
Contributor

Verified on Windows with MinGW GCC 15.2.0: acbbc2503e's Pthread.hpp and #3965's GuestStacks.hpp at 8029f87e58 compile together in both include orders with -std=c++20 -fsyntax-only -Werror.

As a negative control, the previous b0469fd3ac header combined with the same Windows header still fails with conflicting C language linkage declaration for EnterGuestMainStack_nid_no_patch. The Linux guard resolves that reported header conflict. This is a combined-header check only; no combined runtime or title result is claimed.

Prepared with AI assistance.

@hugodearaujo

Copy link
Copy Markdown
Contributor Author

Merged current main (74acfad), without conflicts, and updated Tested: the focused tests pass 90 of 90 on main and 92 of 92 on this head (the two new ones), both negative controls fail as described, and the full CTest matches main.

@milescoviello

Copy link
Copy Markdown
Contributor

@hugodearaujo a title that needs this: ProsperoCraft (PPSA99874 01.000.117), the catalog's Minecraft Java launcher. Its Java 25 HotSpot runtime mprotects guard pages at the bottom of each thread's stack, the main thread included. Linux, RTX 4070 Ti, release libraries, pad input:

Build (on main 750cf6cf48) ProsperoCraft, 120 s
#12436 (answers sysctl vm.loadavg, which the JVM reads first) JVM aborts at start-up: Native memory allocation (mprotect) failed to protect 49152 bytes. Error detail: memory to guard stack pages (stackOverflow.cpp:113), in the primordial thread, whose stack is the host process's (0x00007ffe107c1000-0x00007ffe10fbf000)
+ #7532 (0c55cfcc6a) the primordial thread gets past it; the same abort moves to the Reference Handler thread, stack 0x2737203d0-0x2738203d0, which doesn't start on a page boundary
+ #7532 + #9684 (2d1d41779e), merged clean as 27e6cd782a runs the full 120 s: the launcher's Instances screen, the new-instance form and its on-screen keyboard, all following the input

So this PR is what lets the JVM guard its main thread's stack; #9684 does the same for the threads it creates.

AI-assisted: yes (Claude Code)

@hugodearaujo

Copy link
Copy Markdown
Contributor Author

@milescoviello thank you, that is the first title I know of that this PR changes on Linux beyond the ones in Tested. I added your ProsperoCraft runs to the description (with #9684 for the threads it creates).

# Conflicts:
#	core/libs/prx/libkernel/CMakeLists.txt
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 29, 2026
# Conflicts:
#	core/relinker/elfpatcher/src/general/EntryStubBuilder.cpp
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 30, 2026
# Conflicts:
#	docs/dev/debt/libkernel.md
@github-actions github-actions Bot added review queue Next in the maintainer review queue: checks pass, no open change request, no conflict and removed needs rebase Conflicts with main, rebase needed labels Oct 30, 2026
@oneandonlydean
oneandonlydean merged commit 8835460 into boykopovar:main Nov 3, 2026
15 of 16 checks passed
@JJenkx

JJenkx commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Observed a narrow startup regression with Outer Wilds (PPSA08102) on plain upstream 4f41f4c375f5abb487dbd184fbc150f31994d762, freshly relinked after #7532 merged. Linux 7.2.11, GCC 16.2.1 Release, RX 6650 XT / RADV 26.2.6. Existing audited normalized ELF inputs and a private archived save seed were used; this differs from earlier title reports, so those are not a matched baseline.

Same generated executable and SDK Result
Ordinary stack-entry helper Native SIGSEGV / exit 139 at 2.1 s, before any window
GDB, unchanged helper Reproduces SIGSEGV in Il2CppUserAssemblies.prx.guest.prx; faulting read is 0x7eeffc118, 280 bytes above GUEST_MAIN_STACK_TOP (0x7eeffc000)
Only the helper overridden to enter on the original host stack Runs until scheduled close at 121 s, exit 0; opens a black window with only two completed presents

For the control, an LD_PRELOAD object exports only EnterGuestMainStack_nid_no_patch and tail-jumps to the original entry with its original argument block and second argument zero. Its mapping was verified in the actual native process; all SDK and generated executable/module hashes remained unchanged. This avoids the early crash but does not establish successful bring-up: the reviewed 75 s frame is black, with no menu/gameplay or performance result.

The faulting instruction bytes uniquely match file offset 0x13d8f6b / ELF virtual address 0x13d4f6b in the generated IL2CPP module. The first-stop registers show an upward scan near the new main-stack top. I am checking the scan bound and initial argument-block placement next; the underlying contract mismatch is not yet established.

The actual relinker command was relinker --registry --module-dir Media/Modules --module-dir Media/Plugins --rpath <4f41-build>/core/libs/libs --sce-module-path <normalized-input-root> <normalized-input-root>/eboot.elf <private-runtime>/app.elf. Every game/debugger/control run used our serialized native harness. No proprietary title binaries or memory dump are attached.

Follow-up: the argument-relocation-only control still exits 139. A deterministic CPU fixture now confirms a separate defect: call scePthreadSelf before EnterGuestMainStack_nid_no_patch, and the adopted record retains its original host-stack address/size after execution moves. The original late-adoption test passes on base, while the new early-adoption test fails. Updating that existing record under the live-thread lock preserves its identity and passes all eight focused stack/attribute/startup tests, with zero skips. Commit 4199ddb20550 is pushed; a native candidate run is queued and the upstream overlap check is running. No candidate game success is claimed yet.

Native follow-through: upstream fix #15436, head 4199ddb20550, now reaches a directly reviewed rendered title screen at 260 s and closes normally at 291 s, native/harness exit 0, with no preload/debugger. Same generated title app/modules and restored save seed; fresh shader caches. This is title-screen recovery only; inputs ended before cold loading completed, so no gameplay/audio/FPS gain claim. Eight focused CPU tests pass and the new pre-adoption case alone fails unchanged base production code. Full evidence is linked in the PR.

@github-actions github-actions Bot removed the review queue Next in the maintainer review queue: checks pass, no open change request, no conflict label Nov 3, 2026
@hugodearaujo

Copy link
Copy Markdown
Contributor Author

@JJenkx thanks for tracking this down, and sorry for the regression. My change moved execution to the guest stack but never refreshed a thread record that was adopted before the move, so sceKernelIsStack and the attribute queries kept the host-stack bounds. Your fix in #15436 is the missing piece.

I checked #15436 locally on its head 4199ddb205 (main 4f41f4c375), Arch Linux 7.2.11, GCC, Release:

  • With the new test and Thread.cpp taken from main, guest_main_stack passes and guest_main_stack_adopted fails at GuestMainStack.cpp:68, Require(high == ConsoleMainStackTop). sceKernelIsStack returns the old host-stack end as the top of the stack, which matches your read 280 bytes above GUEST_MAIN_STACK_TOP.
  • With the fix restored, the eight tests you listed 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_start and guest_pthread_stack.

I didn't run Outer Wilds or any other title with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libkernel relinker unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants