Repository navigation
fix(libkernel): pass module args and argp to runtime-loaded modules on Linux - #1102
Conversation
0940f94 to
0597cfb
Compare
|
The overlap flag is right, and it changes how I would rather do this. I checked #846: it routes the shared function through a seam - Proposal, and I will do it unless told otherwise: rebase this branch onto #846 and turn it into the ELF half of that seam - Two facts that survive the restructuring and are worth keeping somewhere in the design:
Updated PR description now says |
|
Independent checks from a Linux/NVIDIA box (RTX 3080 Ti, driver 615.71.09, subgroup 32), since I had the same area open for #1009/#1024 and wanted to see whether the plumbing actually binds. It builds and the suite is green — The Both survive the patcher as plain exported names, so the converted module's The emitted stub is the instruction sequence it claims to be. I took the 31 bytes from So a guest initialiser receives One question, and the rebase onto #846 does not change it: And the answer to the naming question you put to me in #918: #846 introduces no pending-arguments accessor — it routes the shared function through |
|
The concurrency point is right and the fix is pushed - thank you for catching it. You are correct that the mutex made the store consistent rather than the handoff per-call, and that two threads inside thread_local PendingModuleArgs pendingModuleArgs;
thread_local int pendingModuleInitResult = 0;That is the natural scope rather than a workaround, because On the test: yes, please take it - you have the harness open and I would rather not guess at the fixture conventions. What I think it needs to be worth having:
Also noted on the naming question: since #846 introduces no pending-arguments accessor, there is nothing to converge on, and I will keep these names. Your Re-verified after the change: |
|
Adding the overlap the CI bot just reported, because it is a real one and the other author should know. #1249 edits @Raghav314 if your bucketed hash-table work changes how converted modules are laid out, tell me and I will rebase mine onto yours. My half is small and mechanical, so I would rather move than have you rework around it. The same applies to #1156, which also touches this writer to suppress a bundled system library's lifecycle. I noted that over there. |
|
Thanks,my #1249 doesn't change how converted modules are laid out, only what the DT_HASH table contains (one bucket per symbol instead of one bucket in total). Everything after it is still placed through The conflict is only the count line. Keep the bucketed table and take the count from the symbol bytes, as #1102 already does, so the two appended imports are hashed too: const auto count = static_cast<std::uint32_t>(symbols.size() / 24);I tried that merge locally, it builds, and the relinker tests pass. Edit: rebasing now isn't required, so it's up to you. The two only collide on the |
adriadam10
left a comment
There was a problem hiding this comment.
Build and ctest on main+PR are green (419/419), and I checked the stub encoding by hand (rip-relative displacements, 16-byte alignment, RWX extra segment). Requests before merging:
Module.cpp:148-150,157,162: the head still uses a globalstd::mutex+ plain globals. You said on Oct 7 thatthread_localwithout the mutex was pushed, but the only commit on the branch (0597cfb) predates that comment, so it never landed. With the current code two threads insceKernelLoadStartModulecan hand each other'sargs/argpand*res(the race @Dextroid17 reported). Please push thethread_localversion.LinuxGuestModuleWriter.cpp:111-125: the same stub is used forDT_FINI, so unloading also calls__aps5_set_module_init_resultand__aps5_get_pending_module_args(args are zero/stale there). Use the args/result stub forDT_INITonly and keep a plain call-through for fini.- No test exercises the new path. Please add one on Linux CI: a relinked module whose initialiser records
rdi/rsi, loaded viasceKernelLoadStartModulewith a knownargs/argp, plus a check that*rescarries the initialiser's return value. It must fail onmain. - Rebase on
mainonce #846 and #1249 settle (#1249 changes thecountline; usesymbols.size() / 24as you already do). - CI hasn't run on this head because workflows from first-time contributors wait for maintainer approval; I'll approve the run after your next push.
|
Status update, and a blocker I should not decide alone. I did not push a rebased head, because the rebase does not pass. I rebased this branch onto current That test loads a converted module with What I tried before stopping: I made the two imports weak and added a null guard to the stub, so a module that cannot resolve them initialises with Three ways forward, and I will implement whichever you pick:
My preference is 2, because it converges on the name #846 already introduced and removes the hand-placed stub entirely. But it touches the area #1156 just landed, so I am asking rather than assuming. For reference, the original evidence still stands: on PPSA28997, |
|
Main moved under the blocker six minutes after you posted, and it moved in your favour — the test you stopped on is gone, and so is the change it was pinning. #1515 (revert of #1156) merged at 10:58:39Z. It deletes the file that carried the failure, and nothing on So Why #1156 went, since it bears directly on option 2. #1515's body records that suppressing the bundled That reading makes option 2 the point of convergence rather than the risky one: the I only checked what landed on |
0597cfb to
42af7e6
Compare
|
Three of the four points are done and verified. Head is 1. 2. So unloading neither reads stale arguments nor reports a result, and a module with a finaliser but no initialiser gains no imports at all. 4. Rebased. #1515 removed the blocker I stopped on, so the rebase is clean: 3. The test is the remaining item, and it needs one decision from you. The assertion you want is an initialiser that records
I prefer the second, and I will write it that way unless you would rather have the first. It will assert three things: the recorded 5. Understood on CI - I will leave the head as pushed and wait for the run to be approved. |
adriadam10
left a comment
There was a problem hiding this comment.
Rebased head checked: build + ctest on main+PR 450/450, CI green on this head. thread_local state, separate plain jmp for DT_FINI, imports only when Init != 0, and the stub encoding (16-byte alignment, rip-relative displacements) all look right.
One thing left before merging:
- No test runs the new path (nothing in ctest calls
sceKernelLoadStartModulewith args or reads*res). Please add the DT_INIT fixture you proposed: an initialiser that recordsrdi/rsiand returns a known value, loaded with knownargs/argp, asserting all three. It has to fail onmain. Your second option (explicitDT_INITin the relinker python helpers) is fine; notectypes.CDLLneeds the two accessors resolvable, so the test must load libkernel or provide them.
Ping when pushed and it can merge.
|
A negative control for this change, measured on the title today. I reverted only the argument load in the relinker stub. The stub kept its shape and its result capture. It The result is a hard failure, not a slowdown. The run stops before the title's script initialisation. The So the parameter block is load-bearing for this title. The change is not cosmetic. One second test was inconclusive and I am reporting it as such. I also moved the pending block back to a |
|
The negative control is the right instrument, and it is the differential the change was missing: reverting only the two argument loads — reintroducing On the second run: holding it is the right call, and I would not spend the stamping fix on it — the run cannot separate the two forms however clean the copy gets. The shared-global pair and the Nothing else from me. The remaining item is the Written with AI assistance. |
42af7e6 to
7f2c8b1
Compare
|
The What the test does (
Control, as required. With Two things you should know about the base. The previous head was rebased onto my fork's Separately, Ready to merge when you are. Written with AI assistance. |
|
One more commit after the ping. Head is I re-read my own diff for this review and found a bug in the writer change. The Measured on a converted module whose
The test now converts that second module and asserts the relocation table and both imports. I also dropped Control re-run against |
|
Independent confirmation on Suite green on this head. Full incremental build, then The new test runs in CI and bites on — the converted module imports only Your last commit is the fix, isolated. Reverting only the writer to the pre-fix
On this box the native half also ran ( CI on the head now shows conventions / report / windows / relinker (macos, ubuntu, windows) green, with Written with AI assistance. |
sceKernelLoadStartModule discarded its args and argp parameters, so a module that validates its parameter block faulted at start-up. The relinker synthesised a DT_INIT that zeroed rdi, rsi and rdx before jumping to the real initialiser, which turned the discarded block into a guaranteed null dereference for any module that reads it. SaveData.prx from this title faults at its first instruction under that stub: the guest address corresponding to SaveData.prx+0xd0a executes a load from rsi while rsi is zero, next to a comparison of edi against 0x10, and PSN.prx shows the same shape. With the block forwarded, both modules reach their own logic. The kernel now records the block in thread-local storage around the dlopen, and the relinker emits a DT_INIT stub that reads it back, calls the initialiser, and forwards the return value to a setter the kernel reads into the caller's res pointer. The finaliser keeps a plain call-through and the two accessor imports are emitted only for modules that have an initialiser. The thread-local form is deliberate: a shared slot lets a concurrent load overwrite the block that another thread's initialiser is about to read. Proposed upstream as boykopovar/AnyPS5#1102, where it carries the measured negative control: reverting only the two argument loads turns this title from drawing its boot card into a hard failure before script initialisation.
|
Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head
No title was run. 🤖 Addressed by Claude Code |
5f4824e to
4459a01
Compare
adriadam10
left a comment
There was a problem hiding this comment.
Checked on main + PR (rebased on current main): build and full ctest green (549/549), stub encoding and relocation handling look right.
One problem in guest_module_args, fixed in a commit on your branch (4459a01, test only):
test_guest_module_args.py:os.uname().machine == 'x86-64'is never true (Linux reportsx86_64), so the native execution was always skipped, including on CI. The args/argp/result assertions had never run.- With it enabled the fixture crashed: the initialiser's
call [rip+disp]used0x2820 - 0x1016but the next instruction is at0x101a; andSetPendingArgswas called on the build-tree copy of the host library while the module binds to thelibs/libc.prxcopy, so the getter returned zeros. The loader now opens thelibs/libc.prxcopy.
With those fixed the module receives args=0x1122334455667788, argp=0x99AABBCCDDEEFF00 and *res reports 0x5A17. Merging once CI is green on the new head.
…n Linux sceKernelLoadStartModule discarded its arguments, and the relinker's Linux guest writer synthesized an initialisation trampoline that also cleared the argument registers before jumping to the module entry point, so a converted module always reached its own start-up code with args = 0 and argp = NULL. On PPSA28997 two of the title's own modules validate that parameter block as their first act and dereference it unconditionally: SaveData faults on `mov (%rsi),%eax` with rsi = 0 beside its `cmp $0x10` size checks, and PSN faults the same way at PSN+0x3ee7 with rbx = 0. The Windows branch delivers the block, so identical code reports its parameter-version error and continues instead of faulting. sceKernelLoadStartModule now records the pair before dlopen and clears it afterwards, and the emitted DT_INIT stub reads it through two accessors exported by libkernel. The initialiser is called rather than tail-jumped so its return value can be reported through *res as the PS5 API specifies. The state is thread_local, which makes the handoff per-call: ld.so runs a module's initialiser on the thread that called dlopen. DT_FINI keeps a plain call-through, so unloading neither reads stale arguments nor reports a result. The accessor names use the _nid_no_patch suffix so the NID resolver leaves them alone and a guest import can name them exactly.
…block Add a hand-built guest module with an explicit DT_INIT that forwards rdi and rsi to a host recorder without touching them and returns a known value. The test sets a known argument count and argument pointer, loads the converted module, and asserts that the initialiser received both values and that the stub forwarded its return value. The host recorder also provides the two accessor symbols, so the test resolves them without loading libkernel. The converted module must import both accessors, which is what distinguishes the new behaviour from the previous stub that zeroed the arguments.
The DT_RELA tags were still guarded on the input module's relocation table, while the new accessor relocations were appended to a copy of it. A module with an initialiser and no imports therefore kept the argument stub but lost both relocations that fill its GOT slots, so the stub called through a null pointer at load time. Guard on the extended table instead, and drop the unused start parameter from the displacement helper. The test now converts a module whose DT_RELASZ is zero and asserts that the output carries both accessor imports and a 48-byte relocation table.
The machine check compared against 'x86-64' (uname reports 'x86_64'), so the native execution was always skipped. With it enabled the fixture itself was wrong: the initialiser's rip-relative call used the wrong displacement, and the pending block was set on a different copy of the host library than the one the module binds to. Co-Authored-By: Claude <noreply@anthropic.com>
4459a01 to
8868d56
Compare
|
Build and ctest on the rebased head: 97/97 in libkernel + relinker. Merging once CI is green. |
…-on-linux fix(libkernel): pass module args and argp to runtime-loaded modules on Linux
|
Thank you for rebasing it and for catching the interaction with #1249. Taking the symbol count from Recording the far end of it for anyone who revisits this path: the parameter block is what let |
sceKernelLoadStartModule discarded its args and argp parameters, so a module that validates its parameter block faulted at start-up. The relinker synthesised a DT_INIT that zeroed rdi, rsi and rdx before jumping to the real initialiser, which turned the discarded block into a guaranteed null dereference for any module that reads it. SaveData.prx from this title faults at its first instruction under that stub: the guest address corresponding to SaveData.prx+0xd0a executes a load from rsi while rsi is zero, next to a comparison of edi against 0x10, and PSN.prx shows the same shape. With the block forwarded, both modules reach their own logic. The kernel now records the block in thread-local storage around the dlopen, and the relinker emits a DT_INIT stub that reads it back, calls the initialiser, and forwards the return value to a setter the kernel reads into the caller's res pointer. The finaliser keeps a plain call-through and the two accessor imports are emitted only for modules that have an initialiser. The thread-local form is deliberate: a shared slot lets a concurrent load overwrite the block that another thread's initialiser is about to read. Proposed upstream as boykopovar/AnyPS5#1102, where it carries the measured negative control: reverting only the two argument loads turns this title from drawing its boot card into a hard failure before script initialisation.
What
sceKernelLoadStartModulethrows away its arguments, and the relinker's Linux guest writer synthesizes an initialisation trampoline that also clears the argument registers:bytes.insert(bytes.end(), {0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xe9}); // xor edi, esi, edx; jmp targetSo a converted module always reaches its own start-up code with
args = 0andargp = NULL, whatever the caller passed. This PR delivers them.That is not cosmetic. On PPSA28997, two of the title's own modules validate the parameter block as the first thing they do and dereference it unconditionally:
The Windows branch delivers the block, so identical code takes its reported-error path and the title carries on. The gap is issue #918; #846 covers the Windows half of the same asymmetry, where the writer exports
__aps5_guest_initializeand the loader calls it withargs/argp.libkernelrecords the pair in thread-local storage beforedlopenand clears it after - thread-local becauseld.soruns the initialiser on the calling thread, which makes the handoff per-call rather than per-process, and the emitted stub reads it through two accessors it exports:Two pushes keep
rsp16-byte aligned at every call. Calling the initialiser instead of tail-jumping is what makes the*resreporting possible. Two undefined symbols and twoR_X86_64_GLOB_DATrelocations land in the extra block the writer already emits.dynsym,.dynstr,.gnu.hashand.rela.dyninto.Scope is kept honest in two ways: modules the executable needs at start-up still receive
(0, 0)- which is what both platforms do for them today - and a module with no initialiser gains no slots, no relocations and no imports at all, soguest_symbol_namespasses unmodified.The accessor names carry
_nid_no_patchbecauseResolveNidshashes any unsuffixed export into a NID name; on the first attempt here the built library exported neither name and a plain-name import could never resolve.Tested
Arch Linux, GCC,
Release.ctest --test-dir build --output-on-failureaftercmake -DBUILD_TESTING=ON: 392/392 passed (independently confirmed by @Dextroid17 on a second Linux/NVIDIA box, 390/390 at the time of that check).On the title (PPSA28997, Linux relink, default configuration, no diagnostic flags), with #906's twin-library gap worked around separately so the module can load at all:
dlopen(Media/Plugins/PSN.prx)(nil),undefined symbol: wTpfglkmv34, 4554 retriesundefined symbolERROR...stallReproduction of the quoted fault: relink PPSA28997 with the three launcher-deferred modules made start-up dependencies; the registers quoted are from that core, which reproduces in about two seconds before
main.Checklist
main; no other open PR implements the same functions (checked: only fix(libkernel): pass module args and argp to runtime-loaded modules on Linux #1102 touchesModule/src/Module.cpporLinuxGuestModuleWriter.cpp; feat(relinker): add --module-dir for modules loaded at run time #846 is the Windows counterpart and does not land these names)GuestLoadStartModule_nid_no_patch(Windows-only so far); I will rebase and land the ELF branch of that seam here rather than keep a parallel mechanism@Dextroid17 #846 picks the name
__aps5_guest_initializefor the Windows entry point. This lands the Linux half without renamingDT_INIThandling, and names its accessors__aps5_get_pending_module_args_nid_no_patch/__aps5_set_module_init_result_nid_no_patch. If you would rather have one shared accessor name across both platforms, say which and I will rename it - it is a two-line change here.