Linux: when a title bundles its own libc.prx, both libc instances run the application-heap handshake, so the title initialises its main pool twice #693
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 jeffrey12cali on 2026-10-05T16:02:21Z.
Symptom. On Linux the title builds its 256 MB main pool twice (two identical
sceKernelAllocateDirectMemory+sceKernelMapDirectMemory2pairs, sameprot=0xf3/align=0x200000/type=12, both returning success), while the Windows build of the same commit andKyty both build it once. Everything downstream of the pool table is then inconsistent (in our case the
managed runtime's collector later walks an allocation whose header was never written).
Cause. Titles that ship
sce_module/libc.prxend up with two libc implementations in the process:the vendored
libs/libc.prxand the convertedapp0/sce_module/<name>.prx.guest.prx. Each one drivesthe title's own allocator initialisation:
The bundled copy still has to be loaded - other bundled modules resolve real symbols from it (dropping
it via
--exclude-sce-module libc.prxgiveslibresonanceaudio.prx.guest.prx: undefined symbol: ...) -but its start-up callbacks must not run a second time.
Fix we use locally. After relinking, stub the bundled copy's start-up callbacks: for
DT_INITandevery entry of
DT_INIT_ARRAY, overwrite the first instruction withret. (ClearingDT_INITitselfis wrong - the loader calls it regardless and jumps into trap padding, giving a
SIGTRAPbefore anyoutput.) That leaves symbols, sections and relocations untouched and yields exactly one pool, matching
Windows.
Questions. Should the loader treat a bundled system
libc.prxas the same module as thevendored one (single lifecycle), or is suppressing the duplicate title-side handshake the accepted
approach? Related to #476, which covers the symbol-binding half of the same situation.
Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-05T23:21:51Z.
Confirmed, with the caller evidence pinned down (breaking by address, since name breakpoints do not
resolve inside
.prxfiles):So both libc instances really do drive the title's allocator initialisation, and the title's pool
builder takes a hard-coded
len=0x10000000, type=12, prot=0xf3- only the number of runs can differ,which is why this shows up as two 256 MB arenas on Linux and one on Windows/Kyty.
What we do locally, and it is worth an engine-side answer: after relinking, overwrite the first
instruction of the bundled copy's
DT_INITand eachDT_INIT_ARRAYentry withret. ClearingDT_INITitself is not a fix - the loader calls it regardless and lands in trap-filled padding(
SIGTRAPbefore any output, both log files empty). Excluding the module is not a fix either: otherbundled modules resolve real symbols from it (
libresonanceaudio.prx.guest.prx: undefined symbol: ...).Result: exactly one 256 MB main pool, matching both references.
Two notes for whoever picks this up. First,
ApplicationHeapInitializedoes run on Linux (traced via[heap] initialize params=0x...then[heap] register api=0x...), so the handshake itself is fine -it is the duplication that is wrong. Second, this issue alone was not the last blocker: the remaining
fault after fixing it (plus #692 and #694) was that
sceKernelRaiseExceptionhanded the title afabricated
mcontext, never filledPthreadPrivate::stackAddresson Linux, and treated a title threadhandle and AnyPS5's adopted record for the same host thread as unrelated objects - so IL2CPP's
stop-the-world returned success without pausing anything and the collector scanned a stack range it was
told about wrongly. Happy to file that separately if it is useful.
Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-07T11:42:48Z.
I traced the current lifecycle paths on main. Both replacement libs/libc.prx and the converted bundled libc.prx.guest.prx remain distinct loaded images, and Linux republishes the bundled module's DT_INIT/DT_INIT_ARRAY, so the native loader runs its lifecycle independently. Replacement libc also reaches the application's allocator initialization through its own ApplicationHeapInitialize path, whose once guard only covers that runtime.
Before implementing a fix, could you clarify the intended ownership model when both libc implementations coexist?
Should both remain active runtimes but share a single application-heap initialization/finalization lifecycle, or is the bundled libc intended primarily as an export provider whose init/fini may be omitted?
I can implement either direction with synthetic regression coverage, but I don't want to encode the title-specific ret workaround as generic policy without confirming the intended semantics.
Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-07T11:51:25Z.
Answering the ownership question directly: the second option. A bundled copy of a system library should be an export provider whose lifecycle is suppressed by the loader, not a second active runtime that shares state with the replacement. I would not encode the
retpatch as generic policy either - the behaviour is right, the place it is done is wrong.What we measured, which is what makes the first option (both active, one shared heap lifecycle) unattractive:
ApplicationHeapInitialize's guard is astd::once_flagper image, and the allocator registration is per-image state, so "share one heap lifecycle" needs process-wide shared state behind the replacement's API, not a once guard.ApplicationHeapRegistereither has to replace an active allocator or be ignored, and the title's own accounting then double-counts the arena. We saw two arenas where the reference trace has one.DT_INIT/DT_INIT_ARRAY/DT_FINIare the problem.So the general rule I would want in the engine: when the loader is about to initialise a converted module whose identity is a system library that is already present as a replacement (
libs/<name>.prx), run the replacement's lifecycle and record the converted copy as initialised without calling into it - i.e. suppress init/fini by identity, in the loader, not by rewriting bytes in the file. That also covers thesce_modulevslibsduplication for any other system library a title ships, and it keeps the "missing imports fail at startup" property intact, because the exports are still published by the copy that gets loaded.Our runner currently does the narrow thing your question anticipates: it writes
retover the bundledlibc.prx.guest.prxinit targets at prepare time, which is exactly a title-specific fix in the wrong layer, and I would be glad to delete it the moment the engine does the above. Happy to test any variant against this title, and I have the Windows reference trace of the same commit to compare heap and pool counts against - it shows one application-heap handshake, which is the number to match.Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-07T15:28:19Z.
I opened a PR implementing the identity-based lifecycle ownership discussed above: #1156
Bundled system libraries remain loaded for exports, while their init/fini lifecycle is suppressed when the matching AnyPS5 replacement is present. The change includes synthetic regression coverage for both Linux and Windows conversion paths.
Native title testing would be very helpful if you’re able to check it against PPSA28997 and compare the application-heap/pool count with your reference trace.
Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-07T16:47:00Z.
That is exactly the model I hoped someone would write, and the identity-based suppression is the right layer - thank you.
I cannot test it as-is on this project today: our tree is a fork pinned to
sos-v0.2.2(c8830699), which is 18 commits behindmain, and #1156 does not apply there (core/libs/CMakeLists.txt,core/relinker/CMakeLists.txt, both guest module writers andGuestImageall drift). Re-pinning ontomainis already on our list, and #1156 is a reason to do it sooner rather than later; I will report the heap/pool comparison against our Windows reference trace as soon as I can build on a matching base.Two things worth knowing from our measurements, in case they help the review:
retover the bundled copy's init targets, i.e. the same behaviour you are implementing, in the wrong layer - so when #1156 lands we delete ours.args/argpdelivery for runtime-loaded modules, from #918) also editsLinuxGuestModuleWriter.cpp, in the same lifecycle area - it replaces the zeroingDT_INITtrampoline with one that reads the pending pair. Both PRs are correct and complementary, but they will conflict, and whichever lands second needs to keep the other's shape. Worth a line in whichever description gets updated first.Also, since you asked about native testing generally: PPSA28997 now boots through its managed bootstrap and renders an in-engine scene, so it is a usable target for lifecycle regressions - and I can compare allocator/pool counts against a Windows reference trace of the same commit, which is the specific check you asked for.
Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-07T17:01:55Z.
Thanks, this is really helpful. I’ve added a note about the #1102 overlap in the PR so both changes can be preserved if there is a merge conflict.
Also, thanks for offering to test PPSA28997 later. The heap and pool count comparison against the Windows reference trace should be very useful once your branch is on a compatible base.
Imported from boykopovar/AnyPS5. Originally posted by igordev23 on 2026-10-09T00:16:36Z.
Still happening on Linux with PPSA28997 (God of War: Sons of Sparta) after #1515 reverted #1156. On current
mainthe duplicate heap handshake now aborts at start-up instead of corrupting memory later.Build
mainf1451396(2026-10-09) + #1482 (85c43cde, for_umtx_op, which the title imports)rsiinstead of NULL); without it the title faults incall_initreading(%rsi)sce_module/libc.prxResult (right after opening
/app0/Media/boot.config)Exit 134. The throw is the guard in
registerApplicationHeapApi(core/libs/prx/libkernel/System/src/Rtld.cpp):sceKernelRtldSetApplicationHeapAPIis called a second time with a different allocator table, i.e. both libc instances run the application-heap handshake, as described in this issue.Older build without that guard (
df16c4c, same title): no start-up error, but after running for a while glibc aborts withcorrupted size vs. prev_size(the process then hangs and is killed bytimeout). That fits the "everything downstream is inconsistent" description above.Could this issue be reopened, since the fix was reverted? Is there a planned follow-up that suppresses the second handshake without breaking Silksong (#1500)? I can test a branch on PPSA28997 / Linux and provide logs or a backtrace.
Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-11T14:46:17Z.
Reopening this, because @igordev23 is right and I am the author so the obligation is mine to check.
Why it is not resolved. #1156 was the fix. #1515 reverted #1156, and the reason given is sound: with the
bundled
sce_module/libc.prxloaded for exports but itsinit/finisuppressed, a title that still callsthat copy's
mallocgetsNULL, because its mspace was never set up (#1500). So the suppression traded onefailure for another and the duplicate handshake this issue describes is live again. The abort @igordev23
reports is the guard in
registerApplicationHeapApi(core/libs/prx/libkernel/System/src/Rtld.cpp), i.e. thesame second registration, now failing fast instead of corrupting the heap later. That is a strict improvement in
diagnosability and nothing else.
A data point from a tree where this title boots, with the caveat attached. Our fork does not suppress the
bundled libc's lifecycle at all. Instead, in
core/libs/prx/libc/src/ApplicationHeap.cpp, it adopts a workingallocator when none was registered:
With that,
PPSA28997- which does bundlesce_module/libc.prx- reaches its main menu and holds 60 FPSthrough 15 minutes of play on Linux. The caveat matters: our base predates the
registerApplicationHeapApiguard, so this is evidence that "make the second registration harmless rather than fatal or suppressed" can
work for a title that bundles libc. It is not a tested patch against current
main, and I am not claiming itwould fix
PPSA01474or the #1500 case.The design constraint, stated as I read it. #1500 rules out any approach that leaves the bundled copy's
allocator uninitialised, because titles call it by address. That leaves two families: give both copies a
functioning allocator, or route them to one. Suppression is in neither family, which is why #1156 could not
survive.
@igordev23 one thing that will save you a step: #1102 merged on 2026-10-09, so the local workaround you carry
for #918 (passing a zeroed placeholder in
rsifor moduleINITstubs) should no longer be needed - theparameter block is now forwarded on Linux and asserted by the
guest_module_argstest.Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-11T14:47:18Z.
Correction to the first line of my comment above: I wrote "Reopening this" and then found I cannot. The reopen
is refused for my account (
Could not reopen the issue), so #693 is still showing as closed. The request standsand @adriadam10 you closed this, so the question is yours: given that #1515 reverted the fix and the duplicate handshake
still aborts at start-up on
PPSA28997, this issue is not resolved.