* fix(libkernel): validate the guest TLS index pointer in __tls_get_addr
* fix(tests): check VirtualProtect return value as BOOL
* perf(libkernel): use unsynced guest copy in __tls_get_addr
dlopen refused every flag outside 0x103. It now accepts RTLD_NODELETE
(0x1000) and RTLD_NOLOAD (0x2000), mapped as FreeBSD 11 rtld_dlopen maps
them to RTLD_LO_NODELETE and RTLD_LO_NOLOAD:
- RTLD_NOLOAD returns a new handle that holds a reference to an image the
process already has loaded (GetModuleHandleExW on Windows, the host
RTLD_NOLOAD elsewhere). For an existing file that is not loaded it
returns NULL and leaves dlerror untouched, as rtld load_object does; a
file that does not exist sets a dlerror message
- RTLD_NODELETE pins the image (GET_MODULE_HANDLE_EX_FLAG_PIN on Windows,
the host RTLD_NODELETE elsewhere), and the last dlclose keeps its guest
image registration
Other bits, such as RTLD_TRACE, are still refused; the gap is recorded in
the libkernel debt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Firmware 13.60's libkernel accepts only flags 1 and 2, checks the
caller's st_size, clears the whole structure for bad flags and when no
module matches, and keeps the caller's st_size on success. Flags 2
searches another per-module range and still throws.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
__elf_phdr_match_addr walked the caller's ModuleInfoEx segments in place,
so an unreadable record crashed the host. The four segment records and
their count are now read with one GuestCopyIn_nid_no_patch, keeping errno,
and an unreadable record throws, as a null one does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceKernelLoadStartModule and sceKernelStopUnloadModule stored res straight
through the caller's pointer. sceKernelLoadStartModule now writes it with
GuestCopyOut_nid_no_patch and returns EFAULT before loading for an
unwritable res; sceKernelStopUnloadModule checks res with
GuestWritable_nid_no_patch before unloading and returns EFAULT, leaving the
module loaded. errno is kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A module's sweep list is read from its metadata, which is cleared once the module has started, so a sweep skipped at a load without start arguments could never run at that module's next load. The skipped list is now remembered and run at the next deferred load that carries start arguments.
The deferred sweep starts every module another module depends on at the
first deferred load of the title. When that first load carries no
argument block (a module the title starts with args 0 and argp null),
the swept modules were started with the same empty block, and a module
that reads *argp in its module_start faulted: Darkest Dungeon II
(PPSA19591) aborted in NativePluginPS5Example a few seconds after start.
A load whose argp is null no longer triggers the sweep and leaves it
pending, so it runs at the next deferred load that carries start
arguments, which is the block the swept modules get when the title
starts them through their dependent. guest_deferred_module_sweep_no_arguments
loads a module with no arguments first and checks that the swept module
is not started, then loads it with arguments and checks that the sweep
ran with them.
Root cause found and reported by JJenkx in #18053.
The deferred-dependency start state in InitializeDeferredModule (the
resolving vector and the started-results map) were thread_local objects with
destructors. On Windows a thread_local destructor in a module runs after
emulated TLS has freed the object, and prx_thread_local_destructors fails on
main for it. Hold both in HostThreadLocal like the other prx modules; the
trivial depth counter stays thread_local. Behaviour is unchanged.
Firmware 13.42's libkernel_sys.sprx returns 0x80020016 from
sceKernelLoadStartModule for nonzero flags or a non-null opt, and from
sceKernelStopUnloadModule for nonzero flags, a non-null opt or handle 0.
Both ignored these arguments. sceKernelLoadStartModuleForSysmodule
accepts flags there, so it no longer forwards them.
Record that sceKernelStopUnloadModule never runs module_stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A module started as a dependency or by the sweep kept its start result, so a later
load of it by the title returned that result instead of 0 (guest_deferred_module_*
failed in the Windows job with 'module start result').
A swept module whose start throws is skipped with a log line, whatever it throws, and
the load that triggered the sweep, and later loads, still succeed.
Concurrent first loads now wait for the one sweep; a failed sweep is retried by the
next first load. The sweep no longer skips the module that triggered it, so a swept
module that depends on it starts after it, and the trigger keeps its start result.
A deferred module that only another deferred module names (PSNCommon, needed by
PSNCore) started after the plugins that use it (SPFXUnityPlugin) and crashed
Soul Hackers 2 on Windows. The first title load of any deferred module now starts
every deferred module some module depends on, in dependency order, with that
load's start arguments, and logs the sweep. Boot is unchanged.
On Windows a module the executable needs or imports from stays deferred, but its
dependencies were deferred too and started only when the title loads it, so a title
that calls it first ran them uninitialised (Soul Hackers 2, #15370 rerun). They start
at startup again, as on main. The loader prints one line per deferred start.
Since #12621 a Windows runtime module that another runtime module needs is deferred instead of started at boot, but loading a deferred module only started that module. A Unity plugin such as SPFXUnityPlugin that calls into a deferred module it needs (Il2CppUserAssemblies in Media/Modules) ran against code whose initializers had never run, and PPSA03743 crashed about 2.6 s in.
A deferred module's initialization table now lists the deferred modules it depends on (names, after the initializer array, behind a magic number so older output reads as having none). DynamicLoader starts those first, transitively and once each through the existing per-module state, with the start arguments of the load that began it, then starts the module itself. The module start result stays the module's own. Boot still starts no deferred module.
Linux is unchanged: its deferred modules still start only when loaded.
NativeStat, SharedMemory, Stdio, DynamicLoader, ModuleInfo and Rtld used
Linux-only APIs (st_atim, memfd_create, sys/vfs.h, link.h and dl_iterate_phdr),
so libkernel did not compile on macOS. Add macOS paths behind __APPLE__:
st_*timespec and a long-path fstatat walk, an unlinked temp file for shared
memory, statfs, dlopen/dladdr and Mach-O segment walks for module and rtld
queries. The Linux and Windows paths are unchanged.
Extracted from feat/macos-port (#8658).
Co-authored-by: Himansh Raj <iamthehimansh@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Return the main program's handle, the first one sceKernelGetModuleList
lists and one sceKernelGetModuleInfo accepts. reSOT (PPSA99023) imports
it from libkernel and passes the handle to sceKernelGetModuleInfo to find
its own code segment; before this its eboot failed to load on the
unresolved import.
The PS5 3.20 export lists have it in libkernel as well as libkernel_sys,
and on Windows a libkernel import never reaches libkernel_sys, so the
throwing libkernel_sys stub moves to libkernel as pselect did.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>