Skip to content

fix(libc): share the guest SDK application allocator - #6610

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:fix/linux-sdk-heap-owner
Oct 19, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:fix/linux-sdk-heap-owner

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Use the loaded guest SDK allocator for HLE application allocations on Linux. The SDK CRT owns initialization and finalization, preventing HLE from resetting a heap that already has live allocations. The debugger showed the SDK CRT calling the application initializer, an SDK allocation, HLE lazy initialization calling that same initializer again, then SDK free rejecting the earlier allocation. Require all ten allocator entry points to belong to the same native module and retain its loader reference until process exit. Pure HLE and non-Linux paths keep their existing behavior.

Tested

Additional Linux title check, 2026-10-18 UTC: Snoopy & The Great Mystery Club (PPSA28795, v01.000.002) on main 1e2d9b369b versus that main plus exact PR head bbc16577c9 (local integration 1f6b6bb85e). All 1,628 package files were checked against the listing before the comparison. Main stops in the guest SDK allocator during startup (int $0x45, application return offset 0x1674e4b). With this PR, it loads the assets and reaches GPU initialization, then aborts after 4.04 seconds on the separate unsupported multisampled DCC register-clear code 0x20. All 10 application_heap_* tests pass on the integration, with no skips (0.05 s), and conventions pass. This confirms removal of this startup blocker only: no playable frame, gameplay, audio correctness or performance gain is claimed. Logs and debugger details remain local; no game files are attached.

Arch Linux x86-64, GCC 16.2.1. Full libraries and relinker build with bundled dependencies and build-only validator lifetime repair #6476. Eleven allocator tests pass, including a native SDK fixture initialized before HLE use; removing the adoption block makes that unchanged regression fail. The incomplete-provider rejection test also passes. Silksong PPSA12544 version 01.000.010: current main 5d9dcc4 plus build-only #6476 exits 139 before a window in 2.02 s; the same base plus this change reaches the rendered title menu and closes normally (exit 0, 123.81 s). A first candidate launch lacked the writable devlog/app directory and was excluded; the compared staged run includes the same required directories as baseline. A second native run reached the screen-size calibration screen with injected controller input, stayed active for 182.74 s, and closed normally (exit 0). Both runs restored the temporary udmabuf limit. A third completed run passed both calibration screens and the intro cinematic, reached the opening tutorial with HUD and Bind prompt, and responded to Circle/Bind by restoring health (one to four filled masks). Right input was also issued. Ordinary close returned 0 after 363.98 s. This verifies initial tutorial interaction only; later areas, audio quality, and sustained performance remain unverified. No speed claim.

God of War: Sons of Sparta PPSA28997 version 01.008.001 also reaches the rendered title and responds to controller input through the New Game/save-slot menus on Linux (main 5d9dcc4 plus this fix, build repair #6476 and tracing #6341; validation head d9aaee4). Navigation completed with ordinary close, exit 0 after 183.66 s. A targeted negative control using the same converted game/modules and graphics libraries, removing only the four-line SDK-adoption block from HLE libc, exits 139 before a window in 2.02 s in both runs. This is not a separate complete main-only build. Restoring the candidate reaches the rendered title again and closes normally, exit 0 after 63.81 s; eleven allocator tests pass after restoration. A further run on that same validation head passed the opening dialogue, reached the active Cyclops fight with player/boss HUDs, and responded to left-stick movement. It wrote a 2,036-byte player-save file and closed normally after 253.63 s (exit 0). A late capture went black after player health fell; the death/reload flow was not checked. Boss completion, save reloading, audio quality and controlled performance remain unverified. Details: #1742 (comment)

Additional scope check: isolated integration 7cb3bd78d924 = main 896e1b2b72f3 plus this commit builds the full runtime and passes all 11 selected allocator checks (no skips) on the same Linux/Ryzen host. Absolum PPSA28311 v01.002.000 still aborts during video-logo cleanup, just as on main, at a direct-memory release whose supplied range is not physically allocated. The separate memory trace shows a virtual address supplied where the allocation has a different physical offset. This PR does not resolve that title's cleanup failure; no kernel validation change or successful gameplay is claimed. The converted executable/modules and assets are unchanged between these native runs.

Independent title validation: fma-rc reports that Cult of the Lamb PPSA06464 hits the same allocator double initialization on main bf2053816 plus #6149. Adding this PR avoids the replacement allocator’s abort at about 1.5 s and keeps the process running through their 120 s test limit. The contributor later corrected the visual result: frame captures show the studio logo followed by a loading screen with its spinner; the log becomes silent after about 20 s. The remaining loading stall is tracked in #5621. They also report ten selected application-heap tests passing on Linux. This is contributor-reported evidence, not a local title run.

Current-main rebase validation (2026-10-17): rebased as one commit 5e649e6d2a on main 66702f1cff; preserved both additive libc technical-debt entries. The source patch is unchanged by the rebase. Fresh Release build of libc and the three allocator test executables with bundled dependencies succeeds; all 11 allocator tests pass (0.05 s, no skips), including SDK adoption and partial-provider rejection. Conventions and whitespace checks pass. Native title runs above were on their stated earlier bases; no native title rerun or performance gain is claimed for this rebase.

SDK/scripting compatibility refresh (2026-10-18 UTC): rebased as one commit on main a8581d3331. Reproduced the reported interaction with #6842: merging its head b4e129ff800826015a5b0df120225382bb99c0fd into the rebased, unchanged SDK test aborts at line 231. The SDK regression now calls ApplicationHeapAlign / ApplicationHeapFree directly instead of asserting that the separate scripting hooks use the application heap. It still verifies alignment/size, SDK ownership, one initialization, live allocation preservation and retained module lifetime. Existing scripting-hook tests remain unchanged by this PR. This PR alone builds and passes all 11 allocator checks; the clean temporary merge with #6842 builds and passes all 12, each repeated three times (36 executions, no failures/skips). Restored the standalone branch, rebuilt and reran its 11 checks successfully. Conventions and whitespace checks pass. Linux x86-64 Release, bundled dependencies; this is allocator integration testing, with no new native title or performance claim. The branches merge cleanly and neither requires the other.

The subsequent Snoopy bring-up report records the rendered title/menu result when this PR is combined with a separately published DCC register-clear fix, #6543 and #7250. New Game reaches a rendered loading screen and then fails on a later resident multisampled metadata write; gameplay remains unconfirmed. Those combined results are not attributed to this allocator change alone.

Checklist

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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 3987 docs/dev/debt/libc.md
PR 5021 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/ApplicationProcessParameters.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/ApplicationHeap.cpp
PR 6226 docs/dev/debt/libc.md
PR 6469 docs/dev/debt/libc.md
PR 6565 docs/dev/debt/libc.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6879 core/libs/prx/libc/src/ApplicationProcessParameters.cpp
PR 6975 docs/dev/debt/libc.md
PR 7039 docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7084 core/libs/prx/libc/CMakeLists.txt
PR 7201 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/ApplicationProcessParameters.cpp
core/libs/tests/ApplicationHeap.cpp
PR 7332 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/ApplicationHeap.cpp
core/libs/prx/libc/src/ApplicationProcessParameters.cpp
core/libs/tests/ApplicationHeap.cpp
docs/dev/debt/libc.md
PR 7473 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Manually reviewed the shared files that the overlap bot could not compare, using the archived patches and rechecking all 27 current PR head hashes against GitHub.

Depends on: none remains accurate. No claim that these other branches are mergeable: the bot specifically reports that their own base conflicts prevented automatic comparison. This PR itself remains mergeable against current main.

@fma-rc

fma-rc commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Cult of the Lamb (PPSA06464, Unity) hits the same double initialization, tracked in #5621. On main bf20538 with #6149 (needed to get past an earlier fault in UnifyPS5Plugin.prx) the game's replacement allocator builds a 256 MiB heap at 0x200000000 from the bundled libc.prx _malloc_init, then a second one at 0x216a00000 from ApplicationHeap's lazy init, and at 1.5 s Il2cppUserAssemblies.prx frees a block from the first heap and the replacement free aborts. With this PR added only the first heap is created, there is no abort, and the game keeps running until my 120 s limit. It then shows the studio logo and stays on the loading screen with its spinner (log silent after about 20 s), which I am following up in #5621. (Edited: I first wrote that no frame was presented and pointed at the draw path; frame captures show otherwise.)

The application_heap tests (10, including the two new SDK ones) pass on main plus this PR on Arch Linux with an RTX 5080, RADV and lavapipe.

@nicolasfalesy

Copy link
Copy Markdown
Contributor

Tested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, main at e3838332ee31af787ad311b7eadbe53d8d012e95. GPU tests ran on lavapipe (llvmpipe, LLVM 22.1.8).

#6610 and #6842 together break #6610's test application_heap_guest_sdk. Each PR alone passes it (#6842 does not have the test), main has no such test. Merged together, in this order (#6610 then #6842), it aborts (2 of 2):

application heap test failed at line 231

Line 231 of core/libs/tests/ApplicationHeap.cpp on #6610's head is require(scriptingGetMem_nid_postfix(64, 48) == storage.data()). The test expects the scripting hooks to take their blocks from the SDK's own allocator (storage.data()), which #6610 adopts. #6842 moves the hooks to libc's own heap on purpose (ApplicationHeap.cpp:209 and :218 on its head call GuestHeap::GuestHeapAlign_nid_postfix and GuestHeapFree_nid_postfix), so scriptingGetMem no longer returns the SDK block. The two PRs pick opposite owners for these hooks, so whichever lands second has to change the other's expectation.

AI-assisted: yes

@JJenkx
JJenkx force-pushed the fix/linux-sdk-heap-owner branch from 5e649e6 to bbc1657 Compare October 18, 2026 00:19
@JJenkx

JJenkx commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Reproduced on main a8581d3331: #6610 plus #6842 head b4e129ff800826015a5b0df120225382bb99c0fd aborts at the reported line 231. Fixed the SDK test to exercise ApplicationHeapAlign / ApplicationHeapFree directly. Its purpose is to verify adoption of the SDK application heap; it no longer makes a separate assertion about which allocator the scripting hooks select. The existing scripting tests are unchanged by this PR.

The refreshed branch contains one commit on current main. Standalone: all 11 allocator checks pass. Temporary clean merge with #6842: all 12 allocator checks pass, each repeated three times (36 executions, no failures/skips). Both Release builds pass with bundled dependencies. Restored and rebuilt the standalone branch and reran its 11 checks; conventions/whitespace checks also pass. No native title rerun or speed claim. Updated Tested with the exact integration head and scope.

@JJenkx

JJenkx commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Additional title evidence: Blue Prince (PPSA25009, v02.001.000), Linux, Ryzen 7950X / RX 6650 XT, RADV 26.2.4.

On main 1b0c7cfa02bec9eef9f55ec31c4cdd5ca087675e, startup exits with SIGSEGV after approximately 2.14 seconds. Read-only debugger breakpoints show the application allocator initializer called first by the loaded guest libc CRT and then again through HLE ApplicationHeapInitialize / scriptingGetMem. The second initialization replaces the heap state; reallocation of an earlier block subsequently reaches the guest allocator fatal trap.

Applying this PR’s exact head bbc16577c9a5d7963afcc2f8b18f36e9169a2325 onto that main (local integration 0ca04e01f8b10fac88115aa0779497372b292fb1) passes the original crash and renders the Blue Prince main menu with readable text. Full libraries build and all 11 allocator checks pass; conventions check passes.

This establishes startup and opening-cinematic progress, not verified gameplay or an FPS improvement. The window’s roughly 60 FPS counter is not a matched gameplay benchmark. An initial local observation script incorrectly retained an ABSOLUM window-title filter; its no-window result is invalid and has been corrected.

A subsequent 113-second native smoke run and a separate 92-second direct-capture run both closed through the window manager normally (exit 0, no surviving game PID, temporary kernel limit restored). In the latter, paired direct X11/XComposite captures agree on the main menu. Follow-up input diagnostics show scePadRead returns the injected Cross bit (0x4000) to the guest with handle 1 and return count 1. An early press occurred during the splash sequence; a press after the menu was visible entered the loading screen. No libScePad source change was needed.

A further native New Game run on the same integration verified the menu at 50 seconds, sent Cross at 61.19 seconds, and rendered the opening credits and cinematic with readable story text by 180 seconds. It closed normally at 187.21 seconds (exit 0, game PID gone, kernel limit restored). This run used no debugger or profiling instrumentation. At that point interactive gameplay and audio were unverified; this was not a performance comparison.

A longer follow-up on the same integration reached the first-person entrance hall with the 50-step HUD by 325 seconds. A recorded 1.5-second analog-stick pulse at 359.67 seconds changed the view from the hall toward the nearby wall in the 385-second capture, confirming camera control in the interactive scene. Forward movement was part of the same pulse and was not isolated; room progression, save reload and audio remain unverified. The run closed normally at 398.67 seconds (exit 0, game PID gone, kernel limit restored), without a debugger or profiling instrumentation. No matched FPS improvement is claimed.

A further 457.65-second run on the same pinned integration isolated movement: a 4-second forward-only left-stick input at 324.48 seconds (right stick neutral) moved from the entrance to the central flower table in the 335-second capture. Subsequent left-stick strafing and forward input reached the far wall/door area. This confirms translation as well as the previously verified camera control. Door interaction and room progression remain unverified. The game closed normally (exit 0, PID gone, temporary kernel limit restored). Relaunching the existing entrance-hall save files still offered New Game rather than Continue, so these files are not a verified resumable checkpoint.

AI-assisted: yes.

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 18, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@adriadam10
adriadam10 merged commit 096279e into boykopovar:main Oct 19, 2026
18 of 19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff: the SDK allocator adoption is limited to Linux and to a complete, single-module set of the ten exports (partial or mixed providers throw), and application_heap_guest_sdk / _partial fail without the adoption block. The debt entry is in place, and the earlier interaction with #6842 is fixed. Title evidence from the PR and third-party runs covers the startup allocator abort; merging through the queue.

@babyhuey

Copy link
Copy Markdown
Contributor

@JJenkx this looks like a Linux regression from this merge across many homebrew catalog titles.

Linux (Ubuntu 24.04 container, GCC 13, Release), headless with SDL's offscreen driver, so the titles normally stop at DisplayWindow: Vulkan support is either not configured in SDL or not available .... Catalog sweep of main 05b5d192f (before this merge) against e0a6f31d5 (after it): 41 of the 61 titles that reached that check now die earlier or differently, with [libc] abort() called from ..., SIGSEGV, SIGILL, or Mesa's ../radv-src/src/util/xmlconfig.c: 389: out of memory. (ccleste, RommPS, Mortalfin, Dusklight, Mario 64, Ship of Harkinian, Link's Awakening).

Same machine, same build directory, 30 s per title, e0a6f31d5 with and without git revert -m 1 096279ed2 (nothing else changed, rebuilt each time):

Title e0a6f31d5 with this merge reverted
ccleste (PPSA59322) abort(), then xmlconfig.c: 389: out of memory. DisplayWindow check
Mario 64 (PPSA99310) xmlconfig.c: 389: out of memory. DisplayWindow check
Dusklight (PPSA99305) xmlconfig.c: 389: out of memory. DisplayWindow check
Eternal Darkness (PPSA99314) abort() DisplayWindow check
PSXS5 (PPSA97510) abort() DisplayWindow check
ProsperoTV (PPSA99003) SIGSEGV after [HUI] gl 4.6 ... renderer="PS5 AGC" DisplayWindow check
Régie5 (PPSA99520) SIGSEGV DisplayWindow check
Media Player Classic (PPSA99015) SIGILL DisplayWindow check

The out of memory comes from Mesa's own allocation while it reads its driconf, so host allocations seem to fail once the guest SDK allocator is adopted. These titles bundle their own runtime, so perhaps the ten #guest exports are found in a module whose allocator is never initialized on this path: the adoption block returns before the replacement table's initializer runs. Is the initializer expected to have run already by the time ApplicationHeapInitialize_nid_no_patch adopts the exports?

@babyhuey

Copy link
Copy Markdown
Contributor

Follow-up with a likely cause and a guard to try. All of these titles bundle the SDK libc.prx in sce_module, so its relinked guest module (app0/sce_module/libc.prx.guest.prx) exports all ten allocator NIDs as #guest. The adoption block runs before ApplicationHeapInitialize_nid_no_patch looks at the title's own replacement table, so these titles get the guest allocator even if they never replaced it. Before this merge, an all-null table at +0x20 gave them the default heap through ApplicationHeapRegister_nid_no_patch.

I tried adopting only when the title's table has a non-null entry:

             std::array<void*, 10> api;
-            if (ApplicationGuestHeapApi_nid_no_patch(api.data())) {
+            std::array<void*, 10> table;
+            std::memcpy(table.data(), static_cast<const std::byte*>(replacement) + 0x20, sizeof(table));
+            const bool replaced = std::any_of(table.begin(), table.end(), [](const void* entry) { return entry != nullptr; });
+            if (replaced && ApplicationGuestHeapApi_nid_no_patch(api.data())) {

On main e12c91357, Linux, with only that change (local, not pushed):

  • The eight titles in the table above all reach the DisplayWindow check again, as they do with the merge reverted.
  • All ten application_heap_* tests pass 5 of 5 (--repeat until-fail:5), including application_heap_guest_sdk and application_heap_guest_sdk_partial.

I can't run Snoopy or Blue Prince. Do they have a non-null replacement table, so they keep the adoption with this guard? Also, 20 other catalog titles bundle libc.prx and were not affected by the merge, and I haven't worked out why. So the empty table may not be the whole story.

@root-over

Copy link
Copy Markdown
Contributor

Data point with a real display and GPU, past the DisplayWindow check: Linux (Ubuntu, kernel 7.0, Wayland), AMD Ryzen 5 5625U iGPU (Vega, RADV RENOIR, Mesa 26.0.8), GCC 15.2.0, Release, main 8816fd772. Catalog releases checked against their sha256, eboot.bin and sce_module/libc.prx unwrapped with tools/unwrap_self.py, 30-45 s per title.

Title main main + git revert -m 1 096279ed2 main + @babyhuey's guard
DriftCheck (PPSA98766) halts in its own start-up check, Hello World: unique ownership failed (new (std::nothrow) returned null), no window renders its tester UI renders its tester UI
DOOM (PPSA99666) I_AllocLow: cannot allocate 256000 bytes, exits renders the title menu renders the title menu
ccleste (PPSA59322) [libc] abort() at 3 s past start-up past start-up
VLC (PPSA85300, --linux-fixed-base 0x400000) [libc] abort() at 3 s past start-up not run
PS5SX2 (PPSA99203, --trap-sdk-syscall-stubs) std::bad_alloc at 3 s past start-up not run

"Past start-up" means they get through allocation and stop later on this GPU's missing features (no VK_KHR_fragment_shader_barycentric for ccleste and VLC, no mesh shaders for PS5SX2), unrelated to this change.

With the guard, the application_heap/libc tests pass (16/16 after a full rebuild).

DriftCheck's libc.prx is built by ps5-native-app-boilerplate's libc_builder.cpp: it exports the allocator NIDs from an observed API surface and only imports malloc, free and posix_memalign, which it hands to _sceKernelRtldSetApplicationHeapAPI in its initializer, so it has no allocator of its own to adopt. Other catalog titles generated from the same boilerplate likely behave the same way.

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

Labels

build docs libc 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