Skip to content

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

Closed
Delciob wants to merge 1 commit into
boykopovar:mainfrom
Delciob:feat/libkernel-dlopen-module-lookup
Closed

Delciob wants to merge 1 commit into
boykopovar:mainfrom
Delciob:feat/libkernel-dlopen-module-lookup

Conversation

@Delciob

@Delciob Delciob commented Oct 21, 2026 •

Copy link
Copy Markdown

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp
PR 10552 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp
PR 10627 core/libs/prx/libkernel/Module/src/DynamicLoader.cpp

@ycbuzi

ycbuzi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Windows run of 5e55c4a merged onto main a6dcea9 (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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Review of 5e55c4a against main a6dcea9 (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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@maximilianfv

Copy link
Copy Markdown
Contributor

@Delciob #10627 touches the same Windows dlopen failure block in DynamicLoader.cpp (it keeps the whole LoadFailure message for dlerror), and git merge-tree reports a conflict there with this PR. A reviewer asked us to agree a merge order. Would you be OK with #10627 landing first? If yours merges first, I'll rebase mine and keep your fallback search.

AI-assisted: yes

@adriadam10 adriadam10 self-assigned this Oct 25, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Delciob Thanks. Since this was opened, main's RelinkedModulePath resolves <name>.guest.prx, matches case-insensitively and searches the prx/, sce_module/ and sce_modules/ subdirectories, and the Windows dlopen path was rewritten (it now reports the loader error). Does the load you saw still fail on current main, and if so, which title and module layout needs the game_modules/ and libs/ directories? Reusing any loaded module with the same file name could also pick up an unrelated module, so a concrete case would help decide what's needed.

@adriadam10

Copy link
Copy Markdown
Collaborator

Closing: main has since rewritten the Windows dlopen path (RelinkedModulePath now resolves <name>.guest.prx, matches case-insensitively and searches prx/, sce_module/ and sce_modules/, and the loader error is reported), and the points from the earlier review are still open:

  • GetModuleHandleW(filename) matches any loaded module with the same base name, system DLLs included, so a missing or non-PE file makes dlopen succeed with an unrelated module.
  • The reused handle is owned = false, so InitializeDeferredModule is skipped and module_start/initializers never run.
  • No title or path is named for the game_modules/ and libs/ candidates, and nothing in FreeBSD's rtld refers to them (@oneandonlydean asked the same and got no answer).
  • std::filesystem::exists overwrites GetLastError().
  • No test, and What/Tested give no concrete scenario.

Feel free to reopen, or open a new PR rebased on main, with the exact title/module layout that still fails on current main and a test that fails without the change.

@adriadam10 adriadam10 closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants