fix(libc): give the scripting heap hooks blocks from libc's own heap #6842
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6842"
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 milescoviello on 2026-10-16T14:04:55Z.
What
Since #5092 (merged 10-15), Metal Gear Solid Master Collection (PPSA18194) dies 0 s in on
main:EXIT-139with no message, 5 of 5 runs on82ddae75b(one of them with #6814 applied). Before #5092 its Unity launcher ran. In the 10-15 sweep (0029e62f9) it reached itsLoadExecintomgs4/mgs4.elfat 30 s, a 30 s run on that build reaches the limit, and with #5092 reverted on82ddae75bit reaches the limit 2 of 2.What happens:
libc.prx. At start, that libc initializes the title's malloc replacement, and Unity puts its heap in a 256 MiB direct mapping at0x200000000.scriptingGetMemby name (the only hook name inIl2cppUserAssemblies.prx) and now finds #5092's hook. The hook allocates throughApplicationHeapAlign. That heap is not initialized in this process (_init_envbelongs to the title's own libc), so the lazy initialization from #4774 reads the same replacement table from the process parameters and calls its initialize callback a second time.APS5_TRACE_MEMORY=1, a new 256 MiB mapping at0x215000000appears right before the fourscriptingGetMem(0x1000, 0x40000)calls. The run with #5092 reverted makes no such mapping.freeof a block from the first heap fails dlmalloc'sleast_addrcheck (the core hasmspace_free(0x215000010, 0x200001060)). The player then calls the title libc'sabort(int 0x45), which ends the process with SIGSEGV.How
The two hooks now allocate and free through libc's own guest heap (
GuestHeapAlign/GuestHeapFree) instead of the title's registered allocator, so calling them never initializes that allocator. The alignment rules stay as #5092 made them. A size that overflows now returns null instead of throwing. The debt entry records why, and that which console library exports the hooks is still unknown.Tested
Ubuntu 24.04, GCC 13.3, Release, RTX 4070 Ti (NVIDIA 595.99).
application_heap_scripting: with the process parameters installed and the heap not yet initialized, twoscriptingGetMem(0x1000, 0x40000)blocks are 4 KiB-aligned, distinct and writable. Neither they nor their frees reach the registered allocator, and_init_envafterwards initializes it exactly once.main'sApplicationHeap.cpp,application_heap_scriptingandapplication_heap_replacementfail. With the change, all 9application_heap_*tests pass 3 of 3, and the 396 tests matchingapplication_heap|guest_|libc|getifaddrs|heappass.82ddae75bit isEXIT-139at 0 s. With this PR, 3 of 3 30 s runs reach the limit with the same log as the 10-15 build (542–553 lines, the same textures and asset bundles), and a 120 s run stops where the 10-15 sweep did: at 30 s, onsceWebBrowserDialogOpenForPredeterminedContent not implemented(open #5904 and #6092).scriptingGetMemvariantEXIT-1390 sEXIT-1390 sEXIT-1390 sscriptingFreeMemSo the contents and bounds of the blocks don't matter; reaching the title's allocator does.
@mrfelek found the same crash in Hollow Knight: Silksong (PPSA12544), another IL2CPP title with its own
libc.prx. Onmainaf7db81e6it segfaults about 30 s in. With #5092 reverted, or with this PR (35ea8317e), it runs at about 1,050–1,200 presents per 10 s. Theirgit bisect --first-parentalso lands on #5092's merge.@karimibrahim07 checked it on Windows (Windows 11, WinLibs GCC 15.2.0, Release). All 9
application_heap_*tests pass 3 of 3. Withmain'sApplicationHeap.cpp,application_heap_scriptingandapplication_heap_replacementfail, the same two as on Linux.@utajum confirmed it on God of War: Sons of Sparta (PPSA28997) on Linux with RADV (Ryzen Z2 Go, Mesa 26.2.0-devel), on
maine09dbc2b4. That title ships its ownlibc.prx, so it is this PR's case, not #694's. Onmainit segfaults at start-up right afterglobal-metadata.datopens, in the sameil2cpp_init→ titlefree→ titleabortchain as MGS4. With this PR'sApplicationHeap.cppit boots, loads a save and plays, in 10 runs of 75–180 s with no crash.@imellag checked it on macOS (Apple M4 Pro, Rosetta 2, MoltenVK 1.4.2), with this head merged into #7332, which adds the macOS runtime. The 9
application_heap_*tests pass, and with the earlierApplicationHeap.cppthe same two abort. God of War: Sons of Sparta segfaults about a second afterglobal-metadata.datopens without this PR, in the same chain, and with it the game reaches gameplay.Not checked: a Unity title that runs on AnyPS5's libc, whose GC now gets its blocks from libc's heap instead of the title's allocator.
#6713 edits the same files for executables without process parameters; it does not touch these functions. @karimibrahim07 moved #6713's test block and debt line (
668d51adb), so the two merge cleanly in either order.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T14:39:53Z.
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.
docs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/tests/ApplicationHeap.cppcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/tests/ApplicationHeap.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T21:28:46Z.
An independent bisect agrees. I bisected
main(first-parent, 115 merges betweenb4a4fcac0and1b1f8483c) for this title's start-up crash, classifying each run by whether the fault is in the title'ssce_module/libc.prx.guest.prx. It lands on #5092 (7663bc63b) as the first bad merge;c10aa036d, the merge before it, runs.On current
mainbc3c0fc13with #6814 and #6237 merged (Linux, RTX 4070 Ti, pad input), the title still dies at 0 s with the fault in its own libc. With this PR added (35ea8317e, on the slightly newermain0dc15620dwith the same two PRs), 2 of 2 runs reach the 180 s limit, and the launcher'sLoadExecintomgs4/mgs4.elfhappens in both. #5904 is onmainnow, so this PR and #6237 are what stand betweenmainand the game here.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-16T22:40:32Z.
Hollow Knight: Silksong (PPSA12544), another IL2CPP title that ships its own
libc.prx, has the same startup crash since #5092, and this PR fixes it.Linux, Ryzen 9 5950X, RTX 3080 Ti, NVIDIA 615.71.09, GCC 15.3, Release:
mainaf7db81e6global-metadata.datis opened, no framesmainaf7db81e6withaea1dd601(#5092) revertedmainaf7db81e6+ this PR (35ea8317e)A
git bisect --first-parentfrom14036cb0e(Oct 13, good) toaf7db81e6, keeping the Oct 13 relink fixed, also lands on the #5092 merge (7663bc63b).AI-assisted: this comment was drafted with the help of Claude Code and checked against the logs by us.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T03:31:27Z.
Thanks @mrfelek. A second IL2CPP title, with an independent bisect that lands on #5092, makes this much clearer. I added it to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T03:52:25Z.
Review: COMMENTED
Windows check, since the description lists it as not checked. Head
35ea8317emerged ontomaine3a8e4430(clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release.application_heap_*tests pass,--repeat until-fail:3,application_heap_scriptingincluded.main'sApplicationHeap.cppand this PR's test,application_heap_scriptingandapplication_heap_replacementfail (0xc0000409), the same two tests as on Linux.Overlap with #6713 (mine): the bot found a textual conflict in
ApplicationHeap.cpp(tests) anddocs/dev/debt/libc.md, because both PRs added a block right after thelazycase and a line under thescriptingGetMementry. I moved #6713's test block and debt line away from yours (668d51adb), so the two now merge cleanly in either order. With both merged ontomain, all 10application_heap_*tests pass 3 of 3. As you say, #6713 doesn't touch these two functions: it only changesApplicationHeapInitializefor a null process-parameters pointer. With both applied, the hooks still never reach the registered allocator.Code: routing the hooks to
GuestHeapinstead ofApplicationHeapAlignmakes sense given the MGS4 and Silksong results: those titles' own libc owns the registered allocator, and the hooks were initializing it a second time. Catchingstd::length_erroralongsidebad_allocmatches the newSIZE_MAXcheck. I have no code requests.Not checked here: a title. I don't have MGS4, Silksong or a Unity title that uses AnyPS5's own libc (the PPSA28997 case you mention). For that case the GC's blocks now come from libc's heap instead of the title's allocator, so a run of it would be the useful remaining evidence.
AI-assisted: yes. I ran the build, the tests and the negative control above.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T03:58:16Z.
Thanks @karimibrahim07 for the Windows run and the negative control, and for moving #6713's block so the two merge in either order. I added it to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by utajum on 2026-10-17T13:08:47Z.
Confirmed on PPSA28997 (God of War: Sons of Sparta), Linux, RADV (Ryzen Z2 Go / REMBRANDT, Mesa 26.2.0-devel), main
e09dbc2b4.PPSA28997 ships its own
sce_module/libc.prx, so it is the title-libc case, not the #694 one.global-metadata.datopens. Core:il2cpp_init-> title libcfree(tIhsqj0qsFE) -> eboot allocator -> title libcabort(L1SBTkC+Cvw). Same chain as MGS4.ApplicationHeap.cpp: boots, reaches the title screen, loads an existing save and plays. 10 runs of 75-180 s, no crash.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T13:32:01Z.
Thanks @utajum, that's a useful run. It's a third IL2CPP title with its own
libc.prx, after MGS4 and Silksong, and it's also the first RADV result for this PR. I'd listed PPSA28997 as the #694 case under "Not checked", which was wrong. I've corrected that and added your run to the description.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-18T16:12:42Z.
Checked on macOS: Apple M4 Pro, macOS 27.0.1 (26A434), Rosetta 2, Apple clang 21.0.0, Vulkan SDK 1.4.363.0 (MoltenVK 1.4.2).
mainhas no macOS runtime yet, so this is headb4e129ff8merged into #7332 (690c044a6merged intomaina62c724c3), no conflicts, x86-64 build under Rosetta.application_heap_*: 9/9, and again with--repeat until-fail:3. With the merge'sApplicationHeap.cppswapped for the one before this PR,application_heap_replacementandapplication_heap_scriptingabort, the same two as on Linux and Windows.global-metadata.datopens, with nothing in the log. A temporary report in the write watch's fault handler (local only) shows the chain described above: the fault is theint $0x45in the title libc'sabort(L1SBTkC+Cvw), called from the eboot under the title libc'sfree(tIhsqj0qsFE), called fromIl2cppUserAssemblies. With this PR the title starts, loads its FMOD banks and reaches gameplay. That run also needs a local workaround for a separate macOS module-loading stop, reported on #7332.Not checked: other titles on macOS.
AI-assisted: yes (Claude Code). I ran all of the above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T18:45:40Z.
Thanks @imellag for the macOS run and the Sons of Sparta check. I added it to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T12:04:28Z.
Checked the two hooks against
GuestHeap(alignment and overflow paths:length_erroris thrown at GuestHeap.cpp:75/266 and now returns null), the newapplication_heap_scriptingtest (registered in ctest and in the Windows PATH list; it pins no allocator init/free before_init_env, so it fails onmain), and the updated libc debt entry. Windows, macOS and three titles confirmed by the reviewers above; merged.Imported from boykopovar/AnyPS5. Originally posted by madkyp on 2026-10-19T17:29:56Z.
Also fixes Plants vs. Zombies™: Replanted (PPSA29802, #6404): on Linux
47a266cit aborted in the title'sfree()(chunk from the first application heap at 0x200000000 freed into the second at 0x215e00000). On main7a015c094it reaches the profile menu and runs 3 min without a fatal. Ryzen 7 9800X3D + RX 9070 XT (RADV, Mesa 26.2.4), CachyOS.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T17:40:25Z.
@madkyp thanks for the report. Good to know Plants vs. Zombies: Replanted (PPSA29802) had the same cross-heap
free()abort (a chunk from the first application heap freed into the second) and now reaches the profile menu onmain7a015c094and runs 3 minutes without a fatal. #6404 returns a 404 for me, so if it lives somewhere else (a discussion?), feel free to link this there.AI-assisted: yes (Claude Code)