fix(relinker): suppress duplicate system library lifecycle #1156

Merged
ahir061 merged 3 commits from upstream-pr/1156 into main 2026-10-08 09:03:21 +00:00
ahir061 commented 2026-10-07 15:26:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-07T15:26:34Z.


What

Closes #693.

When a title bundles a system library that AnyPS5 already provides as a replacement, the converted guest copy should remain available as an export provider but must not run as a second active runtime.

Previously, a bundled libc.prx and AnyPS5's replacement libs/libc.prx could both execute their lifecycle callbacks. On Linux this caused both libc instances to perform the application-heap handshake, producing two main pools instead of one.

This change makes lifecycle ownership generic and identity-based:

  • Builds a catalog of AnyPS5 replacement modules.
  • Determines a converted guest module's system-library identity from its SCE exported module name, then SONAME, then source filename.
  • Prefers exact replacement identity matches before the existing normalized module-name matching.
  • Keeps the bundled module loaded and its exports available.
  • Keeps the matching replacement library as a host dependency.
  • Suppresses the converted duplicate's init/fini lifecycle:
    • Linux omits DT_INIT, DT_INIT_ARRAY, DT_FINI_ARRAY, and DT_FINI.
    • Windows omits the corresponding explicit lifecycle RVAs.
  • Ordinary guest modules and system libraries without a replacement retain their existing lifecycle behavior.
  • Ambiguous or conflicting replacement identities fail instead of selecting one silently.

Both DT_OS_MODULE_INFO (0x6100000d) and DT_OS_MODULE_INFO_1 (0x61000043) are accepted for SCE module identity.

The guest code itself is not patched and the bundled module is not removed, so other bundled modules can continue resolving symbols from it.

Tested

macOS arm64, AppleClang, BUILD_TESTING=ON.

The relinker and the new lifecycle replacement fixture were built through CMake. The repository's GCC-specific static link flags required local command-line link-rule overrides on macOS.

The following CTests pass:

  • guest_lifecycle_ownership
  • guest_host_libc
  • windows_import_modules
  • guest_module_directories
  • guest_symbol_names
  • guest_absolute_symbols
  • guest_tls
  • guest_intel_trampolines

The new regression covers:

  • replacement-owned bundled module lifecycle suppression
  • preservation of bundled exports
  • dependent guest modules resolving those exports
  • direct init / init array / fini array / direct fini
  • ordinary and unreplaced guest-module lifecycle behavior
  • SCE module identity, SONAME and filename matching
  • both SCE module-info tag variants
  • exact-match precedence and normalized ambiguity
  • missing imports remaining failures

The new lifecycle regression fails on the previous behavior and passes with this change.

Native Linux/Windows lifecycle callback execution was not tested on this macOS arm64 host. No proprietary title was tested.

Independent Windows validation also confirmed the synthetic lifecycle behavior: main produced HIAaih, while this PR produced Hh, with the dependent guest export remaining callable in both cases.

Checklist

  • Based on current main; no other open PR implements the same fix
  • One topic per PR
  • No title-specific paths, hashes or behavior
  • Bundled system-library exports remain available
  • Missing imports still fail
  • No guest-code ret patching
  • git diff --check passes
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4774765588 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1156). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-07T15:26:34Z. --- ### What Closes #693. When a title bundles a system library that AnyPS5 already provides as a replacement, the converted guest copy should remain available as an export provider but must not run as a second active runtime. Previously, a bundled `libc.prx` and AnyPS5's replacement `libs/libc.prx` could both execute their lifecycle callbacks. On Linux this caused both libc instances to perform the application-heap handshake, producing two main pools instead of one. This change makes lifecycle ownership generic and identity-based: - Builds a catalog of AnyPS5 replacement modules. - Determines a converted guest module's system-library identity from its SCE exported module name, then SONAME, then source filename. - Prefers exact replacement identity matches before the existing normalized module-name matching. - Keeps the bundled module loaded and its exports available. - Keeps the matching replacement library as a host dependency. - Suppresses the converted duplicate's init/fini lifecycle: - Linux omits `DT_INIT`, `DT_INIT_ARRAY`, `DT_FINI_ARRAY`, and `DT_FINI`. - Windows omits the corresponding explicit lifecycle RVAs. - Ordinary guest modules and system libraries without a replacement retain their existing lifecycle behavior. - Ambiguous or conflicting replacement identities fail instead of selecting one silently. Both `DT_OS_MODULE_INFO` (`0x6100000d`) and `DT_OS_MODULE_INFO_1` (`0x61000043`) are accepted for SCE module identity. The guest code itself is not patched and the bundled module is not removed, so other bundled modules can continue resolving symbols from it. ### Tested macOS arm64, AppleClang, `BUILD_TESTING=ON`. The relinker and the new lifecycle replacement fixture were built through CMake. The repository's GCC-specific static link flags required local command-line link-rule overrides on macOS. The following CTests pass: - `guest_lifecycle_ownership` - `guest_host_libc` - `windows_import_modules` - `guest_module_directories` - `guest_symbol_names` - `guest_absolute_symbols` - `guest_tls` - `guest_intel_trampolines` The new regression covers: - replacement-owned bundled module lifecycle suppression - preservation of bundled exports - dependent guest modules resolving those exports - direct init / init array / fini array / direct fini - ordinary and unreplaced guest-module lifecycle behavior - SCE module identity, SONAME and filename matching - both SCE module-info tag variants - exact-match precedence and normalized ambiguity - missing imports remaining failures The new lifecycle regression fails on the previous behavior and passes with this change. Native Linux/Windows lifecycle callback execution was not tested on this macOS arm64 host. No proprietary title was tested. Independent Windows validation also confirmed the synthetic lifecycle behavior: main produced HIAaih, while this PR produced Hh, with the dependent guest export remaining callable in both cases. ### Checklist - [x] Based on current `main`; no other open PR implements the same fix - [x] One topic per PR - [x] No title-specific paths, hashes or behavior - [x] Bundled system-library exports remain available - [x] Missing imports still fail - [x] No guest-code `ret` patching - [x] `git diff --check` passes - [x] Depends on: none - [x] AI-assisted: yes
eliasbruno124-dev commented 2026-10-07 20:05:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliasbruno124-dev on 2026-10-07T20:05:42Z.

Review: COMMENTED


Windows validation of 3b6aab11af7a88d7a70ed79085761d713dbf24fe: the lifecycle ownership behavior passes the native Windows checks below.

Windows 11, WinLibs GCC 15.2.0 UCRT, Python 3.12. I used the preserved foreign-only relinker from a clean local integration of this contribution into main cea1ef13. Its original cf62b3c2 PR tree is identical to the current head's full tree. I reconstructed the recorded integration patch and checked that its resulting tree equals a fresh merge of the current head into that same base. This is not a new full-project build or a current-main claim.

  • The current head's unchanged test_guest_lifecycle_ownership.py, its imported helpers and a freshly compiled unchanged replacement fixture pass. On Windows this executes the generated PE cases and checks their lifecycle events and exported values. It also inspects Linux output; it does not execute Linux binaries.
  • Against the preserved main-only relinker, the same complete fixture fails at the Linux-output lifecycle-tag assertion for libc.prx, before its Windows cases.
  • A separate direct Windows comparison uses your guest, consumer and executable builders: renamed.prx declares exported module identity libc through tag 0x61000043, and consumer.prx imports its shared function. Main produces HIAaih; the PR produces Hh. Both processes return 42. Thus the replacement's constructor/destructor remain, the guest's direct/array lifecycle callbacks disappear, and its export remains callable by the dependent guest.

The earlier integration's full 31-group Windows relinker CTest suite also passed without skips; I revalidated that archived receipt and binary identity, but did not repeat the entire suite for this review. The focused checks above are fresh. The fixture DLL uses the PR's MinGW unwind/exception compile options. No proprietary code is included.

This validates the synthetic Windows lifecycle behavior, not a real-title heap handshake, native Linux execution, menu or gameplay. The implementation and fixture remain your contribution. Validation helpers and this report were prepared with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5447705403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1156#pullrequestreview-5447705403). Originally posted by [eliasbruno124-dev](https://github.com/eliasbruno124-dev) on 2026-10-07T20:05:42Z. **Review:** COMMENTED --- Windows validation of `3b6aab11af7a88d7a70ed79085761d713dbf24fe`: the lifecycle ownership behavior passes the native Windows checks below. Windows 11, WinLibs GCC 15.2.0 UCRT, Python 3.12. I used the preserved foreign-only relinker from a clean local integration of this contribution into main `cea1ef13`. Its original `cf62b3c2` PR tree is identical to the current head's full tree. I reconstructed the recorded integration patch and checked that its resulting tree equals a fresh merge of the current head into that same base. This is not a new full-project build or a current-main claim. - The current head's unchanged `test_guest_lifecycle_ownership.py`, its imported helpers and a freshly compiled unchanged replacement fixture pass. On Windows this executes the generated PE cases and checks their lifecycle events and exported values. It also inspects Linux output; it does not execute Linux binaries. - Against the preserved main-only relinker, the same complete fixture fails at the Linux-output lifecycle-tag assertion for `libc.prx`, before its Windows cases. - A separate direct Windows comparison uses your `guest`, `consumer` and `executable` builders: `renamed.prx` declares exported module identity `libc` through tag `0x61000043`, and `consumer.prx` imports its `shared` function. Main produces `HIAaih`; the PR produces `Hh`. Both processes return 42. Thus the replacement's constructor/destructor remain, the guest's direct/array lifecycle callbacks disappear, and its export remains callable by the dependent guest. The earlier integration's full 31-group Windows relinker CTest suite also passed without skips; I revalidated that archived receipt and binary identity, but did not repeat the entire suite for this review. The focused checks above are fresh. The fixture DLL uses the PR's MinGW unwind/exception compile options. No proprietary code is included. This validates the synthetic Windows lifecycle behavior, not a real-title heap handshake, native Linux execution, menu or gameplay. The implementation and fixture remain your contribution. Validation helpers and this report were prepared with AI assistance.
ahir061 commented 2026-10-07 20:49:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-07T20:49:01Z.


Thanks for the detailed validation. The direct HIAaih vs Hh comparison is especially useful, since it confirms the duplicate guest lifecycle is suppressed while the dependent guest can still call its export.
I also appreciate the clear scope around what was and wasn’t validated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6046577051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1156#issuecomment-6046577051). Originally posted by [ahir061](https://github.com/ahir061) on 2026-10-07T20:49:01Z. --- Thanks for the detailed validation. The direct HIAaih vs Hh comparison is especially useful, since it confirms the duplicate guest lifecycle is suppressed while the dependent guest can still call its export. I also appreciate the clear scope around what was and wasn’t validated.
adriadam10 commented 2026-10-08 08:36:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:36:17Z.


Checked the diff against the description and #693: identity resolution (SCE module info → SONAME → filename, exact before normalized, ambiguous/conflicting throws), lifecycle suppression on both writers (Linux dynamic tags, Windows RVAs), exports kept and the replacement kept as a host dependency. Linux and Windows CI are green.

Added on top of your commits (rebased on main):

  • The fixture in test_guest_lifecycle_ownership.py now has a non-executable PT_GNU_STACK header. Without it the converted .guest.prx has none (LinuxGuestModuleWriter only forwards the source's), and glibc 2.41+ refuses to dlopen it ("cannot enable executable stack"), so the native part of the test failed on current Arch/Fedora/Ubuntu while CI's older glibc passed. With the header it passes in full on glibc 2.44.

Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056029752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1156#issuecomment-6056029752). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:36:17Z. --- Checked the diff against the description and #693: identity resolution (SCE module info → SONAME → filename, exact before normalized, ambiguous/conflicting throws), lifecycle suppression on both writers (Linux dynamic tags, Windows RVAs), exports kept and the replacement kept as a host dependency. Linux and Windows CI are green. Added on top of your commits (rebased on `main`): - The fixture in `test_guest_lifecycle_ownership.py` now has a non-executable `PT_GNU_STACK` header. Without it the converted `.guest.prx` has none (`LinuxGuestModuleWriter` only forwards the source's), and glibc 2.41+ refuses to `dlopen` it ("cannot enable executable stack"), so the native part of the test failed on current Arch/Fedora/Ubuntu while CI's older glibc passed. With the header it passes in full on glibc 2.44. Merging once CI is green on the rebased head.
oneandonlydean commented 2026-10-08 10:50:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T10:50:11Z.


Reverting this in #1515 for now: with the bundled libc's init suppressed, titles still call its malloc, which returns NULL (#1500), and titles that bundle libc crash at launch on Linux. Bisected to this merge. The #693 double handshake comes back with the revert; happy to look at a reworked version that initialises the bundled copy or routes its allocator to ours, with a test that calls a title's bundled malloc.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058227923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1156#issuecomment-6058227923). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T10:50:11Z. --- Reverting this in #1515 for now: with the bundled libc's init suppressed, titles still call its `malloc`, which returns NULL (#1500), and titles that bundle libc crash at launch on Linux. Bisected to this merge. The #693 double handshake comes back with the revert; happy to look at a reworked version that initialises the bundled copy or routes its allocator to ours, with a test that calls a title's bundled `malloc`.
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!1156
No description provided.