Linux: low-window direct-memory arena, default dlsym scope, scripting heap #780

Closed
jeffrey12cali wants to merge 3 commits from upstream-pr/780 into main
jeffrey12cali commented 2026-10-05 23:21:17 +00:00 (Migrated from github.com)

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.

commit fixes
Linux: place direct memory in a low window like the Windows guest arena #692
Linux: resolve bundled module paths to their converted .guest.prx related to #476
Linux: honour the default dlsym scope and provide the scripting heap #694

Why

Low window for direct memory. MapPlaced() served addr == NULL with mmap(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:

Windows  map direct 0x200000000+0x10000000   then  reserve 0x210000000+0x100000
Linux    map direct 0x7fffdb200000+0x10000000  then  map direct of another 256 MB -> SIGABRT

After the change Linux matches Windows (0x200000000, then 0x210000000) and the abort is gone. The reservation is MAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVE over 0x200000000+16 GiB with aligned MAP_FIXED chunks handed out of it, and it falls back to the old behaviour if the reservation fails. This also covers @alfalfa-star's #692 finding: sceLibcMspaceCreate in a bundled libc.prx accepts base >= 0x400000 && base + capacity < 0xFC00000001, so flexible-memory mappings from this window work too (the same MapPlaced() path serves them).

Default dlsym scope + scripting heap. The title resolves scriptingGetMem by plain name with handle == 0. Two defects combined to make that impossible: dlsym_nid_postfix_impl() only treated -2 as the global scope (so 0 failed with "invalid or unsupported module handle"), and the global branch searched only the modules map - which contains modules AnyPS5 dlopen()ed itself, so everything pulled in through DT_NEEDED was invisible. Handle 0 now behaves like RTLD_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) and scriptingFreeMem(ptr) are added on the runtime heap with the reference guards (alignment floored at 0x10, non-power-of-two rejected).

before   [dl] dlsym((nil), scriptingGetMem) -> (nil)
after    [dl] dlsym((nil), scriptingGetMem) -> 0x7f03d8a71370
ours     a=0x1000 n=0x40000 -> 0x2003b0000, 0x2003f1000, 0x200432000, 0x200473000   (stride 0x41000)
KytyPS5  alignment=0x1000 size=0x40000 -> 0x2005b4000, 0x2005f5000, 0x200636000      (stride 0x41000)

Module path remap. dlopen("Media/Modules/<name>.prx") now resolves to app0/sce_module/<name>.prx.guest.prx case-insensitively, as the Windows branch already does - without it the title's managed runtime never becomes resident. APS5_GUEST_PRX_REMAP=0 restores the old behaviour.

Verification

  • cmake --build build --target libkernel libc is clean on main (7887d88) with -DANYPS5_BSYMBOLIC_PRX=ON.
  • On the tree these patches came from, the title now reaches on-screen output: the PS boot logo and the publisher logo render correctly and the game holds ~60 FPS (vo 459 / agc 204 / pm4 900 - 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 the Pthread/Exception shapes) do not exist or differ on main, 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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4751953531 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/780). Originally posted by [jeffrey12cali](https://github.com/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. | commit | fixes | | --- | --- | | `Linux: place direct memory in a low window like the Windows guest arena` | #692 | | `Linux: resolve bundled module paths to their converted .guest.prx` | related to #476 | | `Linux: honour the default dlsym scope and provide the scripting heap` | #694 | ### Why **Low window for direct memory.** `MapPlaced()` served `addr == NULL` with `mmap(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: ``` Windows map direct 0x200000000+0x10000000 then reserve 0x210000000+0x100000 Linux map direct 0x7fffdb200000+0x10000000 then map direct of another 256 MB -> SIGABRT ``` After the change Linux matches Windows (`0x200000000`, then `0x210000000`) and the abort is gone. The reservation is `MAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVE` over `0x200000000`+16 GiB with aligned `MAP_FIXED` chunks handed out of it, and it falls back to the old behaviour if the reservation fails. This also covers @alfalfa-star's #692 finding: `sceLibcMspaceCreate` in a bundled `libc.prx` accepts `base >= 0x400000 && base + capacity < 0xFC00000001`, so flexible-memory mappings from this window work too (the same `MapPlaced()` path serves them). **Default `dlsym` scope + scripting heap.** The title resolves `scriptingGetMem` by *plain name* with `handle == 0`. Two defects combined to make that impossible: `dlsym_nid_postfix_impl()` only treated `-2` as the global scope (so `0` failed with "invalid or unsupported module handle"), and the global branch searched only the `modules` map - which contains modules AnyPS5 `dlopen()`ed itself, so everything pulled in through `DT_NEEDED` was invisible. Handle 0 now behaves like `RTLD_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)` and `scriptingFreeMem(ptr)` are added on the runtime heap with the reference guards (alignment floored at `0x10`, non-power-of-two rejected). ``` before [dl] dlsym((nil), scriptingGetMem) -> (nil) after [dl] dlsym((nil), scriptingGetMem) -> 0x7f03d8a71370 ours a=0x1000 n=0x40000 -> 0x2003b0000, 0x2003f1000, 0x200432000, 0x200473000 (stride 0x41000) KytyPS5 alignment=0x1000 size=0x40000 -> 0x2005b4000, 0x2005f5000, 0x200636000 (stride 0x41000) ``` **Module path remap.** `dlopen("Media/Modules/<name>.prx")` now resolves to `app0/sce_module/<name>.prx.guest.prx` case-insensitively, as the Windows branch already does - without it the title's managed runtime never becomes resident. `APS5_GUEST_PRX_REMAP=0` restores the old behaviour. ### Verification - `cmake --build build --target libkernel libc` is clean on `main` (`7887d88`) with `-DANYPS5_BSYMBOLIC_PRX=ON`. - On the tree these patches came from, the title now reaches **on-screen output**: the PS boot logo and the publisher logo render correctly and the game holds ~60 FPS (`vo` 459 / `agc` 204 / `pm4` 900 - 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 the `Pthread`/`Exception` shapes) do not exist or differ on `main`, 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
oneandonlydean commented 2026-10-06 01:12:39 +00:00 (Migrated from github.com)

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:

  1. Split and rebase. These are three separate topics: the direct-memory window, the module path remap, and dlsym scope plus the scripting heap. The .guest.prx remap commit (09111af7) overlaps #717, which has landed. Main now resolves the path through RelinkedModulePath on Linux too, and that's what the current conflict in DynamicLoader.cpp is about. I think that commit can be dropped. The new code comments and the APS5_GUEST_PRX_REMAP switch would also go under the no-comments rule in CONVENTIONS.md.
  2. Low window (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:
    • cursor only grows, so Unmap() leaves holes that are never reused. After enough map/unmap churn the code silently falls back to the old placement.
    • A GuestMapFixed | GuestMapNoOverwrite map into 0x2_0000_0000 to 0x6_0000_0000 now fails with EEXIST against the PROT_NONE reservation (MAP_FIXED_NOREPLACE). Windows treats a reserved arena range as free there.
    • MapAtOrAbove reads /proc/self/maps, sees the whole reservation as used, and moves hinted maps above the window.
    • The reservation's result isn't compared with LowArenaStart. Before 4.17, MAP_FIXED_NOREPLACE is only a hint, and the later MAP_FIXED could then land on whatever already sits at 0x200000000.
  3. dlsym (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 as malloc, where main returned an error. It also skips the #guest spellings FindSymbol tries since #672, so a host symbol can shadow a guest export. Could handle 0 map to the dlopen(nullptr) module and go through FindSymbol instead? On hardware, does scriptingGetMem come from the title's own executable? If so, resolving it there would be better than a libc definition.
  4. Tests. Each part needs a test that fails on main. For example, an addr = NULL direct map that lands in the window, a fixed no-overwrite map into the window, unmap then map reusing the space, and dlsym(nullptr, ...) resolving a guest export without returning a host symbol.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6007248403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/780#issuecomment-6007248403). Originally posted by [oneandonlydean](https://github.com/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: 1. **Split and rebase.** These are three separate topics: the direct-memory window, the module path remap, and dlsym scope plus the scripting heap. The `.guest.prx` remap commit (09111af7) overlaps #717, which has landed. Main now resolves the path through `RelinkedModulePath` on Linux too, and that's what the current conflict in `DynamicLoader.cpp` is about. I think that commit can be dropped. The new code comments and the `APS5_GUEST_PRX_REMAP` switch would also go under the no-comments rule in CONVENTIONS.md. 2. **Low window (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: - `cursor` only grows, so `Unmap()` leaves holes that are never reused. After enough map/unmap churn the code silently falls back to the old placement. - A `GuestMapFixed | GuestMapNoOverwrite` map into 0x2_0000_0000 to 0x6_0000_0000 now fails with `EEXIST` against the `PROT_NONE` reservation (`MAP_FIXED_NOREPLACE`). Windows treats a reserved arena range as free there. - `MapAtOrAbove` reads `/proc/self/maps`, sees the whole reservation as used, and moves hinted maps above the window. - The reservation's result isn't compared with `LowArenaStart`. Before 4.17, `MAP_FIXED_NOREPLACE` is only a hint, and the later `MAP_FIXED` could then land on whatever already sits at 0x200000000. 3. **dlsym (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 as `malloc`, where main returned an error. It also skips the `#guest` spellings `FindSymbol` tries since #672, so a host symbol can shadow a guest export. Could handle 0 map to the `dlopen(nullptr)` module and go through `FindSymbol` instead? On hardware, does `scriptingGetMem` come from the title's own executable? If so, resolving it there would be better than a libc definition. 4. **Tests.** Each part needs a test that fails on main. For example, an addr = NULL direct map that lands in the window, a fixed no-overwrite map into the window, unmap then map reusing the space, and `dlsym(nullptr, ...)` resolving a guest export without returning a host symbol.
jeffrey12cali commented 2026-10-06 09:21:59 +00:00 (Migrated from github.com)

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.elf and every converted app0/sce_module/*.prx have 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 in KernelDlsym and points it at its own KernelApplicationHeapGetMem, SharpEmu aliases it to malloc, 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 in libkernel next to the other runtime-supplied symbols than in libc.

1. Split and rebase. Will do. The .guest.prx remap commit is dropped (I had branched from a tree that predates #717, so I did not see RelinkedModulePath); the comments and the APS5_GUEST_PRX_REMAP switch 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 in AllocateAtOrAbove, and Release splitting ranges. The gap is that the constructor body is entirely #ifdef _WIN32, so on Linux _base stays 0 and Available() is false, which is exactly why MapPlaced() falls through to mmap(nullptr, ...). The rework:

  • Linux constructor walks /proc/self/maps over [ArenaStart, ApplicationAreaEnd), reserves each free span with MAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVE and compares the returned address with the request before accepting it (your pre-4.17 point), recording everything it could not reserve in _holes/_hostRegions like 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 | GuestMapNoOverwrite inside the window is judged against the arena's used ranges, not against EEXIST from the PROT_NONE reservation, so reserved-but-unused space is free there as on Windows.
  • MapAtOrAbove() delegates to GuestArenaAllocateAtOrAbove() instead of treating the whole reservation as used.
  • DoMapDirect/Unmap call MarkUsed/Release so the bookkeeping tracks reality.

3. dlsym. Agreed — RTLD_DEFAULT can hand back host malloc/operator new/Vulkan symbols and skips the #guest spellings. Handle 0 will map to the dlopen(nullptr) module and go through FindSymbol, so guest exports win and host symbols are never returned.

4. Tests. I will add ctest cases that fail on main for: an addr = NULL direct map landing inside the window; a fixed no-overwrite map into reserved-but-unused space; unmap-then-map reusing the hole; and dlsym(nullptr, ...) resolving a guest export without returning a host symbol.

I will close this and send two PRs (arena in GuestArena; dlsym scope plus the scripting heap). One question while I am in there: should the scripting heap honour the same GuestArena window for its blocks, or is the host heap fine for that allocation?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6013261148 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/780#issuecomment-6013261148). Originally posted by [jeffrey12cali](https://github.com/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.elf` and every converted `app0/sce_module/*.prx` have 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 in `KernelDlsym` and points it at its own `KernelApplicationHeapGetMem`, SharpEmu aliases it to `malloc`, 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 in `libkernel` next to the other runtime-supplied symbols than in `libc`. **1. Split and rebase.** Will do. The `.guest.prx` remap commit is dropped (I had branched from a tree that predates #717, so I did not see `RelinkedModulePath`); the comments and the `APS5_GUEST_PRX_REMAP` switch 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 in `AllocateAtOrAbove`, and `Release` splitting ranges. The gap is that the constructor body is entirely `#ifdef _WIN32`, so on Linux `_base` stays 0 and `Available()` is false, which is exactly why `MapPlaced()` falls through to `mmap(nullptr, ...)`. The rework: - Linux constructor walks `/proc/self/maps` over `[ArenaStart, ApplicationAreaEnd)`, reserves each free span with `MAP_FIXED_NOREPLACE | PROT_NONE | MAP_NORESERVE` and **compares the returned address with the request** before accepting it (your pre-4.17 point), recording everything it could not reserve in `_holes`/`_hostRegions` like 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 | GuestMapNoOverwrite` inside the window is judged against the arena's used ranges, not against `EEXIST` from the `PROT_NONE` reservation, so reserved-but-unused space is free there as on Windows. - `MapAtOrAbove()` delegates to `GuestArenaAllocateAtOrAbove()` instead of treating the whole reservation as used. - `DoMapDirect`/`Unmap` call `MarkUsed`/`Release` so the bookkeeping tracks reality. **3. `dlsym`.** Agreed — `RTLD_DEFAULT` can hand back host `malloc`/`operator new`/Vulkan symbols and skips the `#guest` spellings. Handle 0 will map to the `dlopen(nullptr)` module and go through `FindSymbol`, so guest exports win and host symbols are never returned. **4. Tests.** I will add ctest cases that fail on `main` for: an `addr = NULL` direct map landing inside the window; a fixed no-overwrite map into reserved-but-unused space; unmap-then-map reusing the hole; and `dlsym(nullptr, ...)` resolving a guest export without returning a host symbol. I will close this and send two PRs (arena in `GuestArena`; `dlsym` scope plus the scripting heap). One question while I am in there: should the scripting heap honour the same `GuestArena` window for its blocks, or is the host heap fine for that allocation?
jeffrey12cali commented 2026-10-06 10:15:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T10:15:31Z.


Closing in favour of #861, split as requested.

  • Arena -> #861 (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 eager PROT_NONE reservation of the whole range starves the kernel's own top-down mmap, 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 than MAP_FIXED_NOREPLACE//proc/self/maps, and Commit/Reset/SetProtection are portable so GuestHeap's arena path works on Linux. The two guest_memory checks that were Windows-only now run on Linux; the window check fails on main and passes on the branch, and the full-suite failure set is identical between the two (282 in this sandbox), so no regressions.
  • .guest.prx remap -> dropped, as you said: #717 already resolves it through RelinkedModulePath.
  • dlsym + scripting heap -> separate PR to follow, reworked around handle 0 mapping to the dlopen(nullptr) module and going through FindSymbol, with no RTLD_DEFAULT fallback (your shadowing objection is correct, and it would also have skipped the #guest spellings from #672).

@alfalfa-star #861 is the Linux arena you asked about, built on GuestArena rather than a second allocator - if you have one in flight, happy to close mine and rebase onto yours instead.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6014120666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/780#issuecomment-6014120666). Originally posted by [jeffrey12cali](https://github.com/jeffrey12cali) on 2026-10-06T10:15:31Z. --- Closing in favour of #861, split as requested. - **Arena** -> #861 (`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 eager `PROT_NONE` reservation of the whole range starves the kernel's own top-down `mmap`, 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 than `MAP_FIXED_NOREPLACE`/`/proc/self/maps`, and `Commit`/`Reset`/`SetProtection` are portable so `GuestHeap`'s arena path works on Linux. The two `guest_memory` checks that were Windows-only now run on Linux; the window check fails on `main` and passes on the branch, and the full-suite failure set is identical between the two (282 in this sandbox), so no regressions. - **`.guest.prx` remap** -> dropped, as you said: #717 already resolves it through `RelinkedModulePath`. - **`dlsym` + scripting heap** -> separate PR to follow, reworked around handle 0 mapping to the `dlopen(nullptr)` module and going through `FindSymbol`, with no `RTLD_DEFAULT` fallback (your shadowing objection is correct, and it would also have skipped the `#guest` spellings from #672). @alfalfa-star #861 is the Linux arena you asked about, built on `GuestArena` rather than a second allocator - if you have one in flight, happy to close mine and rebase onto yours instead.
jeffrey12cali commented 2026-10-06 10:15:32 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6014121028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/780#issuecomment-6014121028). Originally posted by [jeffrey12cali](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!780
No description provided.