fix(libkernel): read the module image through dlinfo and never own the program's own handle #16922

Merged
ycbuzi merged 4 commits from upstream-pr/16922 into main 2026-11-17 03:16:00 +00:00
ycbuzi commented 2026-11-07 22:36:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-11-07T22:36:07Z.


What

dlclose on Linux read the module's load address by casting the dlopen handle to link_map*. POSIX makes the handle opaque; glibc happens to return the link_map, but other loaders do not. Under box64 (Linux on an arm64 host) the handle is a small index, so dlclose read l_addr from address 0x6 and crashed. It now gets the link_map through dlinfo(RTLD_DI_LINKMAP), as InitializeLinuxGuestModule and the lookup above it already do, and reports an error if that fails.

dlopen(NULL) (used by sceKernelDlsym(0, …) on every lookup in the program's own scope) now marks the handle as not owned on Linux and macOS, as Windows already does: closing it no longer calls dlclose on the program or unregisters the program's image from the guest allocation registry.

Tested

  • TERRIFIER The ArtCade Game (PPSA26362), Linux build run under box64 0.4.5 + Turnip on a Meta Quest 2 (Snapdragon XR2, Android 14, Debian proot): on main (8f7c957ed0) it crashes with SIGSEGV in dlclose_nid_postfix reading 0x6 right after sceKernelDlsym(0, "scriptingGetMem"); with this change it gets past that point and stops later in an unrelated place (libc unwinding after opening ScriptingAssemblies.json, to be filed separately).
  • The libkernel loader tests, built on Linux x86-64 for both commits and run under box64 on the same Quest: guest_dynamic_loader, guest_dynamic_loader_fixed_base and guest_dynamic_loader_variant_names all crash in dlclose on main; with this change all three get past it and guest_dynamic_loader_variant_names passes. The other two then stop at a second, separate box64 difference (_sceKernelRtldThreadAtexitIncrement cannot reference the test binary); guest_module_info, guest_unwind_module_info and guest_unwind_module_info_failure give the same result on both commits.
  • On native Linux x86-64 the change builds; the loader tests could not run on the available Linux machine (Pentium N6000, stops at the AVX2 host check before any test), so native Linux and macOS rely on CI. Windows code is unchanged.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA26362 #8381
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5206484571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16922). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-11-07T22:36:07Z. --- ### What `dlclose` on Linux read the module's load address by casting the `dlopen` handle to `link_map*`. POSIX makes the handle opaque; glibc happens to return the `link_map`, but other loaders do not. Under box64 (Linux on an arm64 host) the handle is a small index, so `dlclose` read `l_addr` from address `0x6` and crashed. It now gets the `link_map` through `dlinfo(RTLD_DI_LINKMAP)`, as `InitializeLinuxGuestModule` and the lookup above it already do, and reports an error if that fails. `dlopen(NULL)` (used by `sceKernelDlsym(0, …)` on every lookup in the program's own scope) now marks the handle as not owned on Linux and macOS, as Windows already does: closing it no longer calls `dlclose` on the program or unregisters the program's image from the guest allocation registry. ### Tested - TERRIFIER The ArtCade Game (PPSA26362), Linux build run under box64 0.4.5 + Turnip on a Meta Quest 2 (Snapdragon XR2, Android 14, Debian proot): on `main` (8f7c957ed05) it crashes with SIGSEGV in `dlclose_nid_postfix` reading `0x6` right after `sceKernelDlsym(0, "scriptingGetMem")`; with this change it gets past that point and stops later in an unrelated place (libc unwinding after opening `ScriptingAssemblies.json`, to be filed separately). - The libkernel loader tests, built on Linux x86-64 for both commits and run under box64 on the same Quest: `guest_dynamic_loader`, `guest_dynamic_loader_fixed_base` and `guest_dynamic_loader_variant_names` all crash in `dlclose` on `main`; with this change all three get past it and `guest_dynamic_loader_variant_names` passes. The other two then stop at a second, separate box64 difference (`_sceKernelRtldThreadAtexitIncrement` cannot reference the test binary); `guest_module_info`, `guest_unwind_module_info` and `guest_unwind_module_info_failure` give the same result on both commits. - On native Linux x86-64 the change builds; the loader tests could not run on the available Linux machine (Pentium N6000, stops at the AVX2 host check before any test), so native Linux and macOS rely on CI. Windows code is unchanged. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA26362 #8381 - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 23:12:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T23:12:50Z.

Review: COMMENTED


Read d3980857b1 against main.

dlclose. Reading the link map through dlinfo(RTLD_DI_LINKMAP) (DynamicLoader.cpp:569-571) matches MainProgramMap and InitializeLinuxGuestModule. On glibc the handle is the link_map, so nothing changes there; a loader with opaque handles no longer has l_addr read from something that isn't one.

Program handle. owned = path != nullptr (:481) does on Linux and macOS what Windows already does (:449). sceKernelDlsym(0, …) opens and closes that handle on every lookup (Module.cpp:173-175), and closing it no longer unregisters the program image. glibc's dlopen(NULL) count on the main map is now never dropped, which is harmless since the main map is never unloaded.

Test. Nothing in the suite fails on main and passes here on glibc. A check after the dlopen_nid_postfix(nullptr, 2) / dlclose pair in GuestDynamicLoader.cpp:142-143 that the program image is still registered would pin the ownership half. Not blocking.

CI. The Linux job doesn't run on pull requests, so this Linux and macOS change is covered only by the runs in the description.

Not run here; this is from reading the code.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815504499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16922#pullrequestreview-5815504499). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T23:12:50Z. **Review:** COMMENTED --- Read `d3980857b1` against `main`. **dlclose.** Reading the link map through `dlinfo(RTLD_DI_LINKMAP)` (`DynamicLoader.cpp:569-571`) matches `MainProgramMap` and `InitializeLinuxGuestModule`. On glibc the handle is the `link_map`, so nothing changes there; a loader with opaque handles no longer has `l_addr` read from something that isn't one. **Program handle.** `owned = path != nullptr` (`:481`) does on Linux and macOS what Windows already does (`:449`). `sceKernelDlsym(0, …)` opens and closes that handle on every lookup (`Module.cpp:173-175`), and closing it no longer unregisters the program image. glibc's `dlopen(NULL)` count on the main map is now never dropped, which is harmless since the main map is never unloaded. **Test.** Nothing in the suite fails on `main` and passes here on glibc. A check after the `dlopen_nid_postfix(nullptr, 2)` / `dlclose` pair in `GuestDynamicLoader.cpp:142-143` that the program image is still registered would pin the ownership half. Not blocking. **CI.** The Linux job doesn't run on pull requests, so this Linux and macOS change is covered only by the runs in the description. Not run here; this is from reading the code. AI-assisted: yes.
milescoviello commented 2026-11-09 07:55:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-09T07:55:57Z.

Review: APPROVED


Read the head against main b66ca0e85a (the merge preview is clean).

dlinfo instead of the cast. The link_map is taken while the handle is still open: dlinfo runs before module.reset(), whose destructor is where ::dlclose happens. If dlinfo fails, the function returns -1 before the registry erase, so the handle stays registered and a retry still works. RTLD_DI_LINKMAP is what glibc and musl both implement for dlinfo, so this is the portable way to get l_addr, where the cast relied on glibc's handle layout.

dlopen(NULL) not owned. With owned false, dlclose skips both UnregisterImage and, through ~Module, ::dlclose, as on Windows. Skipping the native dlclose leaves the main map's open count raised, which is harmless because glibc never unloads the main program. Unregistering the program image on every sceKernelDlsym(0, …) close was the real bug, and CheckProgramImageStaysRegistered pins exactly that: the image registered before dlopen(nullptr) + dlclose is still registered after.

Coverage. The owned path, dlinfo → UnregisterImage for a real module, is exercised on every platform by the existing argv[1] cases in guest_dynamic_loader: the module is opened twice and the second dlclose is the last reference. So both branches of the new owned flag run on Linux CI.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5822992130 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16922#pullrequestreview-5822992130). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-09T07:55:57Z. **Review:** APPROVED --- Read the head against `main` `b66ca0e85a` (the merge preview is clean). **`dlinfo` instead of the cast.** The `link_map` is taken while the handle is still open: `dlinfo` runs before `module.reset()`, whose destructor is where `::dlclose` happens. If `dlinfo` fails, the function returns -1 before the registry erase, so the handle stays registered and a retry still works. `RTLD_DI_LINKMAP` is what glibc and musl both implement for `dlinfo`, so this is the portable way to get `l_addr`, where the cast relied on glibc's handle layout. **`dlopen(NULL)` not owned.** With `owned` false, `dlclose` skips both `UnregisterImage` and, through `~Module`, `::dlclose`, as on Windows. Skipping the native `dlclose` leaves the main map's open count raised, which is harmless because glibc never unloads the main program. Unregistering the program image on every `sceKernelDlsym(0, …)` close was the real bug, and `CheckProgramImageStaysRegistered` pins exactly that: the image registered before `dlopen(nullptr)` + `dlclose` is still registered after. **Coverage.** The owned path, `dlinfo` → `UnregisterImage` for a real module, is exercised on every platform by the existing `argv[1]` cases in `guest_dynamic_loader`: the module is opened twice and the second `dlclose` is the last reference. So both branches of the new `owned` flag run on Linux CI. AI-assisted: yes (Claude Code)
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!16922
No description provided.