fix(libkernel): read the module image through dlinfo and never own the program's own handle #16922
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16922"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-11-07T22:36:07Z.
What
dlcloseon Linux read the module's load address by casting thedlopenhandle tolink_map*. POSIX makes the handle opaque; glibc happens to return thelink_map, but other loaders do not. Under box64 (Linux on an arm64 host) the handle is a small index, sodlclosereadl_addrfrom address0x6and crashed. It now gets thelink_mapthroughdlinfo(RTLD_DI_LINKMAP), asInitializeLinuxGuestModuleand the lookup above it already do, and reports an error if that fails.dlopen(NULL)(used bysceKernelDlsym(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 callsdlcloseon the program or unregisters the program's image from the guest allocation registry.Tested
main(8f7c957ed0) it crashes with SIGSEGV indlclose_nid_postfixreading0x6right aftersceKernelDlsym(0, "scriptingGetMem"); with this change it gets past that point and stops later in an unrelated place (libc unwinding after openingScriptingAssemblies.json, to be filed separately).guest_dynamic_loader,guest_dynamic_loader_fixed_baseandguest_dynamic_loader_variant_namesall crash indlcloseonmain; with this change all three get past it andguest_dynamic_loader_variant_namespasses. The other two then stop at a second, separate box64 difference (_sceKernelRtldThreadAtexitIncrementcannot reference the test binary);guest_module_info,guest_unwind_module_infoandguest_unwind_module_info_failuregive the same result on both commits.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T23:12:50Z.
Review: COMMENTED
Read
d3980857b1againstmain.dlclose. Reading the link map through
dlinfo(RTLD_DI_LINKMAP)(DynamicLoader.cpp:569-571) matchesMainProgramMapandInitializeLinuxGuestModule. On glibc the handle is thelink_map, so nothing changes there; a loader with opaque handles no longer hasl_addrread 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'sdlopen(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
mainand passes here on glibc. A check after thedlopen_nid_postfix(nullptr, 2)/dlclosepair inGuestDynamicLoader.cpp:142-143that 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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-09T07:55:57Z.
Review: APPROVED
Read the head against
mainb66ca0e85a(the merge preview is clean).dlinfoinstead of the cast. Thelink_mapis taken while the handle is still open:dlinforuns beforemodule.reset(), whose destructor is where::dlclosehappens. Ifdlinfofails, the function returns -1 before the registry erase, so the handle stays registered and a retry still works.RTLD_DI_LINKMAPis what glibc and musl both implement fordlinfo, so this is the portable way to getl_addr, where the cast relied on glibc's handle layout.dlopen(NULL)not owned. Withownedfalse,dlcloseskips bothUnregisterImageand, through~Module,::dlclose, as on Windows. Skipping the nativedlcloseleaves the main map's open count raised, which is harmless because glibc never unloads the main program. Unregistering the program image on everysceKernelDlsym(0, …)close was the real bug, andCheckProgramImageStaysRegisteredpins exactly that: the image registered beforedlopen(nullptr)+dlcloseis still registered after.Coverage. The owned path,
dlinfo→UnregisterImagefor a real module, is exercised on every platform by the existingargv[1]cases inguest_dynamic_loader: the module is opened twice and the seconddlcloseis the last reference. So both branches of the newownedflag run on Linux CI.AI-assisted: yes (Claude Code)