feat(libkernel): add fallback search paths and loaded handle lookup i… #9415

Closed
Delciob wants to merge 1 commit from upstream-pr/9415 into main
Delciob commented 2026-10-21 21:10:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Delciob on 2026-10-21T21:10:28Z.


What

Enhance dlopen on Windows to reuse existing loaded module handles via GetModuleHandleW before attempting a new disk load. Add fallback search candidate paths for game_modules/ and libs/ subdirectories including .guest.prx suffix resolution.

Tested

Windows 10 x64, game module dynamic loading with secondary modules.

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: none
  • GPUs tested: n/a
  • AI-assisted: no
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4958408074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415). Originally posted by [Delciob](https://github.com/Delciob) on 2026-10-21T21:10:28Z. --- ### What Enhance dlopen on Windows to reuse existing loaded module handles via GetModuleHandleW before attempting a new disk load. Add fallback search candidate paths for game_modules/ and libs/ subdirectories including .guest.prx suffix resolution. ### Tested Windows 10 x64, game module dynamic loading with secondary modules. ### Checklist - [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: none - [x] GPUs tested: n/a - [x] AI-assisted: no
ycbuzi commented 2026-10-21 22:41:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T22:41:53Z.


Windows run of 5e55c4a463 merged onto main a6dcea9279 (3fd670a490, clean), no platform is listed in the description. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libkernel, libkernel_sys, libc and the loader and module test executables with no errors or warnings. ctest --repeat until-fail:2 on these 15 tests: guest_dynamic_loader, guest_dynamic_loader_variant_names, guest_module_args, guest_module_dir, guest_module_directories, guest_module_fini, guest_module_identity, guest_module_info, guest_needed_modules, guest_needed_nested_modules, guest_deferred_module_parallel, guest_deferred_module_pinned, guest_deferred_module_reentrant, guest_deferred_module_reload and guest_sysmodule: all pass on both repeats. The description names no test, so these are the dynamic loader and module tests. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328165962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415#issuecomment-6328165962). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T22:41:53Z. --- Windows run of 5e55c4a463 merged onto `main` a6dcea9279 (3fd670a490, clean), no platform is listed in the description. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. Built `libkernel`, `libkernel_sys`, `libc` and the loader and module test executables with no errors or warnings. `ctest --repeat until-fail:2` on these 15 tests: `guest_dynamic_loader`, `guest_dynamic_loader_variant_names`, `guest_module_args`, `guest_module_dir`, `guest_module_directories`, `guest_module_fini`, `guest_module_identity`, `guest_module_info`, `guest_needed_modules`, `guest_needed_nested_modules`, `guest_deferred_module_parallel`, `guest_deferred_module_pinned`, `guest_deferred_module_reentrant`, `guest_deferred_module_reload` and `guest_sysmodule`: all pass on both repeats. The description names no test, so these are the dynamic loader and module tests. Not run: a title.
MarcZX10 commented 2026-10-21 22:43:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T22:43:19Z.


The hosted conventions check is failing while the PR description still has empty What and Tested sections and the template instruction text in Tested. Please replace those with a short behavior summary and the exact checks run (or “Not tested” where applicable); the description currently gives reviewers no validation scope.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328188814 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415#issuecomment-6328188814). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T22:43:19Z. --- The hosted conventions check is failing while the PR description still has empty `What` and `Tested` sections and the template instruction text in `Tested`. Please replace those with a short behavior summary and the exact checks run (or “Not tested” where applicable); the description currently gives reviewers no validation scope.
wachi35676 commented 2026-10-21 23:44:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-21T23:44:34Z.


Review of 5e55c4a46 against main a6dcea927 (merges cleanly). The change is entirely inside the #ifdef _WIN32 branch of dlopen_nid_postfix (lines 176–220). On Linux, DynamicLoader.cpp compiled from main and from this PR gives byte-identical objects (GCC 13.3.0). On Windows it compiles without warnings using libkernel's flags (MinGW GCC 15.2.0, -Wall). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs MinGW-w64 GCC 15.2.0, Release relinker-only BUILD_TESTING=ON; Linux: WSL Ubuntu 24.04, GCC 13.3.0, same configuration.

To check what the fallbacks do, I copied the added block verbatim into a small Windows program (RelinkedModulePath_nid_no_patch replaced by identity), together with main's single LoadLibraryExW call, and ran it on Windows 11. A mod.prx DLL is first loaded from t\a\.

Request main This PR
t\nonexistent\kernel32.dll (missing) NULL, error 126 C:\WINDOWS\System32\KERNEL32.DLL, owned=false
t\b\mod.prx (missing) NULL, error 126 the module from t\a\mod.prx, owned=false
t\c\mod.prx (present, not a PE file) NULL, error 193 the module from t\a\mod.prx, owned=false
t\d\other.prx (missing; t\d\libs\other.prx present) NULL, error 126 t\d\libs\other.prx
t\e\none.prx (missing everywhere) NULL, error 126 NULL, GetLastError() 3 when the message is written

What this means for sceKernelLoadStartModule and dlopen:

  • GetModuleHandleW(filename) matches any loaded module with the same base name, whatever directory it came from, including system DLLs. When the requested file is missing or fails to load (error 193), this PR returns a handle to a different module and reports success.
  • That handle is owned=false, so InitializeDeferredModule is skipped. sceKernelLoadStartModule then returns a handle with *res = 0 and never runs module_start or the initializers.
  • FreeBSD's rtld does not search for a path that contains /. find_library returns it unchanged as a "Hard coded pathname" (rtld.c in 11.4, around line 1630). load_object reuses a loaded object only when the whole name matches (object_match_name) or the opened file has the same device and inode (around lines 2405 and 2454). The game_modules/ and libs/ directories added here are not on that path. I found no other reference to game_modules in the repository.
  • parent/<name>.guest.prx (the fourth candidate) is already tried by RelinkedModulePath_nid_no_patch, which runs before LoadLibraryExW.
  • When every fallback fails, the logged Windows loader error is no longer the original LoadLibraryExW error: the std::filesystem::exists calls overwrite GetLastError() (126 becomes 3 above).

The PR has no test and names no title. A title that needs this, with the path it passes, would show which of these lookups is actually required.

Not run: a full Windows build of the libraries or a title with this PR. The table above comes from the copied block, not from libkernel.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329077430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415#issuecomment-6329077430). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-21T23:44:34Z. --- Review of 5e55c4a46 against `main` a6dcea927 (merges cleanly). The change is entirely inside the `#ifdef _WIN32` branch of `dlopen_nid_postfix` (lines 176–220). On Linux, `DynamicLoader.cpp` compiled from `main` and from this PR gives byte-identical objects (GCC 13.3.0). On Windows it compiles without warnings using libkernel's flags (MinGW GCC 15.2.0, `-Wall`). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs MinGW-w64 GCC 15.2.0, Release relinker-only `BUILD_TESTING=ON`; Linux: WSL Ubuntu 24.04, GCC 13.3.0, same configuration. To check what the fallbacks do, I copied the added block verbatim into a small Windows program (`RelinkedModulePath_nid_no_patch` replaced by identity), together with `main`'s single `LoadLibraryExW` call, and ran it on Windows 11. A `mod.prx` DLL is first loaded from `t\a\`. | Request | `main` | This PR | |---|---|---| | `t\nonexistent\kernel32.dll` (missing) | NULL, error 126 | `C:\WINDOWS\System32\KERNEL32.DLL`, `owned=false` | | `t\b\mod.prx` (missing) | NULL, error 126 | the module from `t\a\mod.prx`, `owned=false` | | `t\c\mod.prx` (present, not a PE file) | NULL, error 193 | the module from `t\a\mod.prx`, `owned=false` | | `t\d\other.prx` (missing; `t\d\libs\other.prx` present) | NULL, error 126 | `t\d\libs\other.prx` | | `t\e\none.prx` (missing everywhere) | NULL, error 126 | NULL, `GetLastError()` 3 when the message is written | What this means for `sceKernelLoadStartModule` and `dlopen`: - `GetModuleHandleW(filename)` matches any loaded module with the same base name, whatever directory it came from, including system DLLs. When the requested file is missing or fails to load (error 193), this PR returns a handle to a different module and reports success. - That handle is `owned=false`, so `InitializeDeferredModule` is skipped. `sceKernelLoadStartModule` then returns a handle with `*res = 0` and never runs `module_start` or the initializers. - FreeBSD's rtld does not search for a path that contains `/`. `find_library` returns it unchanged as a "Hard coded pathname" (rtld.c in 11.4, around line 1630). `load_object` reuses a loaded object only when the whole name matches (`object_match_name`) or the opened file has the same device and inode (around lines 2405 and 2454). The `game_modules/` and `libs/` directories added here are not on that path. I found no other reference to `game_modules` in the repository. - `parent/<name>.guest.prx` (the fourth candidate) is already tried by `RelinkedModulePath_nid_no_patch`, which runs before `LoadLibraryExW`. - When every fallback fails, the logged `Windows loader error` is no longer the original `LoadLibraryExW` error: the `std::filesystem::exists` calls overwrite `GetLastError()` (126 becomes 3 above). The PR has no test and names no title. A title that needs this, with the path it passes, would show which of these lookups is actually required. Not run: a full Windows build of the libraries or a title with this PR. The table above comes from the copied block, not from libkernel. AI-assisted: yes (Claude Code); results above were run on this machine.
MarcZX10 commented 2026-10-22 05:04:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T05:04:56Z.

Review: CHANGES_REQUESTED


Requesting changes based on the still-unresolved findings already documented in the review thread. The Windows experiment in comment 6329077430 shows the fallback can return a different module by basename (including a system DLL) after the requested path fails or names a non-PE file, then mark it unowned so initialization is skipped; the fallback also changes the reported loader error. The same report found no title/path evidence for the added game_modules and libs searches. Please address those concrete cases with a focused test before keeping these search paths.

The PR is also 423 commits behind current main 524398151f1e (merge base 0c6a5bd5938f) and the hosted conventions checks still fail; the What and Tested sections remain empty. Please rebase, complete the PR template and rerun the relevant checks after correcting the behavior. The independent Windows report says the broader module test set passed on an older base, but its copied-block probe is not a test of the library build.

I manually compared the shared-file warnings: #5021 changes only Apple branches, #6879 only adds the Emscripten include, and #7201 changes Apple link-map handling. Those changes are distinct from this PR's Windows-only fallback block, so no dependency on those three is indicated by the current diffs.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609493127 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415#pullrequestreview-5609493127). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T05:04:56Z. **Review:** CHANGES_REQUESTED --- Requesting changes based on the still-unresolved findings already documented in the review thread. The Windows experiment in comment 6329077430 shows the fallback can return a different module by basename (including a system DLL) after the requested path fails or names a non-PE file, then mark it unowned so initialization is skipped; the fallback also changes the reported loader error. The same report found no title/path evidence for the added `game_modules` and `libs` searches. Please address those concrete cases with a focused test before keeping these search paths. The PR is also 423 commits behind current `main` `524398151f1e` (merge base `0c6a5bd5938f`) and the hosted conventions checks still fail; the `What` and `Tested` sections remain empty. Please rebase, complete the PR template and rerun the relevant checks after correcting the behavior. The independent Windows report says the broader module test set passed on an older base, but its copied-block probe is not a test of the library build. I manually compared the shared-file warnings: #5021 changes only Apple branches, #6879 only adds the Emscripten include, and #7201 changes Apple link-map handling. Those changes are distinct from this PR's Windows-only fallback block, so no dependency on those three is indicated by the current diffs.
adriadam10 commented 2026-10-25 13:05:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-25T13:05:51Z.

Review: CHANGES_REQUESTED


Thanks for the PR. Findings, most of them from wachi35676's experiment above (verified against the diff):

  • DynamicLoader.cpp:186-190: GetModuleHandleW(filename) matches any loaded module with the same base name, from any directory, system DLLs included. When the requested file is missing or is not a PE file, dlopen returns a different module and reports success. Match the whole resolved path, or the file identity, as FreeBSD's load_object does (object_match_name / same device and inode).
  • DynamicLoader.cpp:192-193,213: the reused handle is owned = false, so InitializeDeferredModule is skipped and sceKernelLoadStartModule never runs module_start or the initializers.
  • DynamicLoader.cpp:197-204: nothing in the repo or in FreeBSD's rtld refers to game_modules/ or libs/; a path containing / is used as given. Name the title and the exact path it passes, or drop these candidates. The fourth candidate (<name>.guest.prx) is already tried by RelinkedModulePath_nid_no_patch before LoadLibraryExW.
  • DynamicLoader.cpp:207: std::filesystem::exists overwrites GetLastError(), so the logged Windows loader error is no longer the original LoadLibraryExW one (126 becomes 3). Save the error before the fallbacks and restore it.
  • No test, and the description gives no behaviour summary or concrete validation; What/Tested need the exact scenario and checks.
  • #10627 touches the same failure block; please answer maximilianfv on the merge order, and rebase, since this conflicts with main.

Welcome to push a narrower version with a test that fails without it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643320224 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9415#pullrequestreview-5643320224). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-25T13:05:51Z. **Review:** CHANGES_REQUESTED --- Thanks for the PR. Findings, most of them from wachi35676's experiment above (verified against the diff): - `DynamicLoader.cpp:186-190`: `GetModuleHandleW(filename)` matches any loaded module with the same base name, from any directory, system DLLs included. When the requested file is missing or is not a PE file, `dlopen` returns a different module and reports success. Match the whole resolved path, or the file identity, as FreeBSD's `load_object` does (`object_match_name` / same device and inode). - `DynamicLoader.cpp:192-193,213`: the reused handle is `owned = false`, so `InitializeDeferredModule` is skipped and `sceKernelLoadStartModule` never runs `module_start` or the initializers. - `DynamicLoader.cpp:197-204`: nothing in the repo or in FreeBSD's rtld refers to `game_modules/` or `libs/`; a path containing `/` is used as given. Name the title and the exact path it passes, or drop these candidates. The fourth candidate (`<name>.guest.prx`) is already tried by `RelinkedModulePath_nid_no_patch` before `LoadLibraryExW`. - `DynamicLoader.cpp:207`: `std::filesystem::exists` overwrites `GetLastError()`, so the logged `Windows loader error` is no longer the original `LoadLibraryExW` one (126 becomes 3). Save the error before the fallbacks and restore it. - No test, and the description gives no behaviour summary or concrete validation; `What`/`Tested` need the exact scenario and checks. - #10627 touches the same failure block; please answer maximilianfv on the merge order, and rebase, since this conflicts with `main`. Welcome to push a narrower version with a test that fails without it.

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!9415
No description provided.