Linux: low-window direct-memory arena, default dlsym scope, scripting heap #780
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/780"
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-05T23:21:17Z.
What
Three Linux back-end gaps found while bringing up a IL2CPP PS5 title (PPSA28997) on Linux. Each commit is one concern and maps to an issue.
Linux: place direct memory in a low window like the Windows guest arenaLinux: resolve bundled module paths to their converted .guest.prxLinux: honour the default dlsym scope and provide the scripting heapWhy
Low window for direct memory.
MapPlaced()servedaddr == NULLwithmmap(nullptr, ...), i.e. a top-down address, while the Windows back end hands out its guest arena (0x200000000). The title checked the address it got, re-issued the identical 256 MB request, and then asserted inside its own allocator (call+ud2). Measured on both platforms with the same build:After the change Linux matches Windows (
0x200000000, then0x210000000) and the abort is gone. The reservation isMAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVEover0x200000000+16 GiB with alignedMAP_FIXEDchunks handed out of it, and it falls back to the old behaviour if the reservation fails. This also covers @alfalfa-star's #692 finding:sceLibcMspaceCreatein a bundledlibc.prxacceptsbase >= 0x400000 && base + capacity < 0xFC00000001, so flexible-memory mappings from this window work too (the sameMapPlaced()path serves them).Default
dlsymscope + scripting heap. The title resolvesscriptingGetMemby plain name withhandle == 0. Two defects combined to make that impossible:dlsym_nid_postfix_impl()only treated-2as the global scope (so0failed with "invalid or unsupported module handle"), and the global branch searched only themodulesmap - which contains modules AnyPS5dlopen()ed itself, so everything pulled in throughDT_NEEDEDwas invisible. Handle 0 now behaves likeRTLD_DEFAULT(glibc searches the executable first, matching "id 0 is the main program") and falls back to::dlsym(RTLD_DEFAULT, name)/ the NID spelling.scriptingGetMem(alignment, bytes)andscriptingFreeMem(ptr)are added on the runtime heap with the reference guards (alignment floored at0x10, non-power-of-two rejected).Module path remap.
dlopen("Media/Modules/<name>.prx")now resolves toapp0/sce_module/<name>.prx.guest.prxcase-insensitively, as the Windows branch already does - without it the title's managed runtime never becomes resident.APS5_GUEST_PRX_REMAP=0restores the old behaviour.Verification
cmake --build build --target libkernel libcis clean onmain(7887d88) with-DANYPS5_BSYMBOLIC_PRX=ON.vo459 /agc204 /pm4900 - identical counts to the working Windows build of the same commit).Not in this PR
The last blocker we hit is not covered here: after the movies finish,
sceKernelRaiseException(IL2CPP stop-the-world) needs a real interrupted context, per-thread stack bounds, and completion tracking by host thread id. The files that need it in our tree (libkernel/Umtx/Umtx.cpp, and thePthread/Exceptionshapes) do not exist or differ onmain, so that work is not portable as-is. Happy to port it if the Linux threading model here can take it - and @alfalfa-star, if you have a Linux arena PR in flight, this first commit is likely the same shape and I would rather not duplicate it.Fixes #692
Fixes #694
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T01:12:39Z.
Thanks for tracking these down, the traces in #692 and #694 are useful. A few things before this can go in:
.guest.prxremap commit (09111af7) overlaps #717, which has landed. Main now resolves the path throughRelinkedModulePathon Linux too, and that's what the current conflict inDynamicLoader.cppis about. I think that commit can be dropped. The new code comments and theAPS5_GUEST_PRX_REMAPswitch would also go under the no-comments rule in CONVENTIONS.md.5db9688b).GuestArena(libc/src/GuestArena.cpp) already models this window on Windows, with used ranges, holes and release. A Linux back end there seems better than a second bump allocator, and as written:cursoronly grows, soUnmap()leaves holes that are never reused. After enough map/unmap churn the code silently falls back to the old placement.GuestMapFixed | GuestMapNoOverwritemap into 0x2_0000_0000 to 0x6_0000_0000 now fails withEEXISTagainst thePROT_NONEreservation (MAP_FIXED_NOREPLACE). Windows treats a reserved arena range as free there.MapAtOrAbovereads/proc/self/maps, sees the whole reservation as used, and moves hinted maps above the window.LowArenaStart. Before 4.17,MAP_FIXED_NOREPLACEis only a hint, and the laterMAP_FIXEDcould then land on whatever already sits at 0x200000000.64f01e68). The::dlsym(RTLD_DEFAULT, name)fallback (which now also applies to -2) can return host glibc, libstdc++ or Vulkan symbols for common names such asmalloc, where main returned an error. It also skips the#guestspellingsFindSymboltries since #672, so a host symbol can shadow a guest export. Could handle 0 map to thedlopen(nullptr)module and go throughFindSymbolinstead? On hardware, doesscriptingGetMemcome from the title's own executable? If so, resolving it there would be better than a libc definition.dlsym(nullptr, ...)resolving a guest export without returning a host symbol.Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T09:21:59Z.
Thanks — all four points are fair. Answers and the rework plan:
On your question about
scriptingGetMem: no, it is not in the title's executable. It is neither exported nor imported anywhere in the title's own binaries:app.elfand every convertedapp0/sce_module/*.prxhave no definition of the plain name or of its NID (ayuoL6Vjz2k), and no module lists it as an undefined symbol either — so it can only be satisfied as a run-time lookup, which matches the references: KytyPS5 special-cases the name inKernelDlsymand points it at its ownKernelApplicationHeapGetMem, SharpEmu aliases it tomalloc, and Raeen reaches the same conclusion from a different the engine title. I will keep it as a runtime definition — tell me if you would rather it live inlibkernelnext to the other runtime-supplied symbols than inlibc.1. Split and rebase. Will do. The
.guest.prxremap commit is dropped (I had branched from a tree that predates #717, so I did not seeRelinkedModulePath); the comments and theAPS5_GUEST_PRX_REMAPswitch go with it, per the no-comments rule.2. Low window in
GuestArena. Agreed, and you are right that it already models this —ArenaStart,ApplicationAreaEnd, the used-map with hole search inAllocateAtOrAbove, andReleasesplitting ranges. The gap is that the constructor body is entirely#ifdef _WIN32, so on Linux_basestays 0 andAvailable()is false, which is exactly whyMapPlaced()falls through tommap(nullptr, ...). The rework:/proc/self/mapsover[ArenaStart, ApplicationAreaEnd), reserves each free span withMAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVEand compares the returned address with the request before accepting it (your pre-4.17 point), recording everything it could not reserve in_holes/_hostRegionslike the Windows path.MapPlaced()with no address asks the arena (Allocate), so hole reuse comes from the existing used-map rather than a monotonically growing cursor.GuestMapFixed | GuestMapNoOverwriteinside the window is judged against the arena's used ranges, not againstEEXISTfrom thePROT_NONEreservation, so reserved-but-unused space is free there as on Windows.MapAtOrAbove()delegates toGuestArenaAllocateAtOrAbove()instead of treating the whole reservation as used.DoMapDirect/UnmapcallMarkUsed/Releaseso the bookkeeping tracks reality.3.
dlsym. Agreed —RTLD_DEFAULTcan hand back hostmalloc/operator new/Vulkan symbols and skips the#guestspellings. Handle 0 will map to thedlopen(nullptr)module and go throughFindSymbol, so guest exports win and host symbols are never returned.4. Tests. I will add ctest cases that fail on
mainfor: anaddr = NULLdirect map landing inside the window; a fixed no-overwrite map into reserved-but-unused space; unmap-then-map reusing the hole; anddlsym(nullptr, ...)resolving a guest export without returning a host symbol.I will close this and send two PRs (arena in
GuestArena;dlsymscope plus the scripting heap). One question while I am in there: should the scripting heap honour the sameGuestArenawindow for its blocks, or is the host heap fine for that allocation?Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T10:15:31Z.
Closing in favour of #861, split as requested.
fix(guest-arena): place guest address space in the PS5 window on Linux). Reworked per your review: the window is seeded from existing mappings instead of reserved up front (an eagerPROT_NONEreservation of the whole range starves the kernel's own top-downmmap, which picks addresses just below the stack inside it - that is what crashed my first attempt in the guest heap's first allocation), hole reuse comes from the existing used map, no-overwrite and hints go through the arena rather thanMAP_FIXED_NOREPLACE//proc/self/maps, andCommit/Reset/SetProtectionare portable soGuestHeap's arena path works on Linux. The twoguest_memorychecks that were Windows-only now run on Linux; the window check fails onmainand passes on the branch, and the full-suite failure set is identical between the two (282 in this sandbox), so no regressions..guest.prxremap -> dropped, as you said: #717 already resolves it throughRelinkedModulePath.dlsym+ scripting heap -> separate PR to follow, reworked around handle 0 mapping to thedlopen(nullptr)module and going throughFindSymbol, with noRTLD_DEFAULTfallback (your shadowing objection is correct, and it would also have skipped the#guestspellings from #672).@alfalfa-star #861 is the Linux arena you asked about, built on
GuestArenarather than a second allocator - if you have one in flight, happy to close mine and rebase onto yours instead.Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T10:15:32Z.
Split into #861 (arena) plus a follow-up for the dlsym/scripting-heap change; the .guest.prx remap is dropped as overlapping #717.
Pull request closed