Repository navigation
fix(relinker): run guest preinit arrays before the module initializer - #6112
Conversation
|
Declared the merge order for the overlap: the checklist now reads @JJenkx if you would rather have this one go first, say so and I will flip the declaration. |
|
Thanks @milescoviello. Rebased on main in da00f6f: guest_preinit now goes through add_asan_test_options(guest_preinit verify_asan_link_order=0), the same as the sibling module tests, so it should pass in an ANYPS5_ENABLE_ASAN build now. No other changes in the rebase. |
|
@ayam04 one thing in the Windows preinit thunk ( The thunk pushes r12, r13, r14 but pops them in the same order. This is So the caller gets its r12 and r14 back swapped. Both are callee-saved under SysV and Win64. The only caller is The fix keeps the length, so --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
+++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
@@ -172,7 +172,7 @@
code.Emit({0x4c, 0x89, 0xe7, 0x4c, 0x89, 0xee, 0x4c, 0x89, 0xf2});
code.Rip({0x48, 0x8b, 0x05}, slotsRva + static_cast<std::uint32_t>(guest.PreInitArray.size()) * 8);
code.Emit({0xff, 0xd0});
- code.Emit({0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0xc3});
+ code.Emit({0x41, 0x5e, 0x41, 0x5d, 0x41, 0x5c, 0xc3});
auto thunk = code.TakeBytes();
while (thunk.size() % 8 != 0) thunk.push_back(0x90);
for (const auto slot : guest.PreInitArray) {
--- a/core/relinker/relinker/tests/test_guest_preinit.py
+++ b/core/relinker/relinker/tests/test_guest_preinit.py
@@ -173,7 +173,7 @@
restore = 15 + 23 * len(slots)
assert code[restore:restore + 12] == b'\x4c\x89\xe7\x4c\x89\xee\x4c\x89\xf2\x48\x8b\x05', code[restore:restore + 16]
assert code[restore + 16:restore + 18] == b'\xff\xd0', code[restore:restore + 25]
- assert code[restore + 18:restore + 25] == b'\x41\x5c\x41\x5d\x41\x5e\xc3', code[restore:restore + 25]
+ assert code[restore + 18:restore + 25] == b'\x41\x5e\x41\x5d\x41\x5c\xc3', code[restore:restore + 25]
slots_offset = code_size + (8 - code_size % 8) % 8
slots_rva = ginitc_rva + slots_offset
for index, slot in enumerate(slots):With that applied (Ubuntu 24.04, GCC 13.3, relinker-only Release), the converted thunk ends in No test runs the thunk today: AI-assisted: yes (Claude Code) |
|
Good catch @SandstromPL, and thanks for the repro. Fixed in f294956: the thunk now ends The failure mode you describe is the one that matters here: Length is unchanged, so Verified locally on Windows (mingw g++, relinker Release): On a dlopen test for the converted preinit module: agreed that nothing runs the thunk today. Happy to add one in this PR if @milescoviello wants it here, otherwise it can land as a follow-up so this stays a two-hunk fix. |
Converted guest modules never ran their DT_PREINIT_ARRAY entries: the reader validated the array and the unused-NID filter kept its targets, but no writer emitted or ran it. ld.so runs a shared object's preinit array only for the main executable, so the Linux output runs the entries from the DT_INIT stub before the module initializer; the macOS and Windows outputs run them before the initializer in their startup stubs, and the deferred Windows output exports a thunk that runs them before the initializer for runtime-loaded modules. A module with a preinit array and no initializer is still rejected, since the Linux stub exists only with one.
guest_preinit loads the instrumented guest_module_args_host.prx into Python, so in an ANYPS5_ENABLE_ASAN build it needs the same verify_asan_link_order=0 as the other module tests; without it, it fails with "ASan runtime does not come first in initial library list". add_asan_test_options does nothing outside ASan builds. Signed-off-by: Ayamullah Khan <ayamullahkhan04@gmail.com>
The Windows deferred preinit thunk pushed r12, r13 and r14 and then popped r12, r13 and r14, so the caller got r12 and r14 back swapped. InitializeDeferredModule keeps the module base, table and count live across the call to the thunk, so a compiler that keeps any of them in r12 or r14 sees corrupted loader state after opening a deferred module that has a preinit array. Pop in reverse push order instead and pin the new bytes in test_guest_preinit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
f294956 to
e751268
Compare
|
@ayam04 the dlopen test point came from @SandstromPL, not me, so it's their call. For my part, a follow-up is fine and keeps this a two-hunk fix. AI-assisted: yes (Claude Code) |
|
Merged while @adriadam10 is away: the review passed and full CI is green on this head. |
|
@ayam04 @oneandonlydean since this merge, every title I run on Linux aborts at start-up with AI-assisted: yes |
…zer (boykopovar#6112) This reverts commit 376c040.
revert(relinker): run guest preinit arrays before the module initializer (#6112)
Model a module whose initializer dispatches its own preinit callback and assert the exact once-only callback, preserved arguments and return value. Retain zero-address and no-preinit controls and cover deferred Linux initialization. Regression coverage for #7636, introduced by #6112 and reverted by #7646. The new test fails with the actual historical bad relinker at 376c040 and passes on main 9090972. AI-assisted: yes. Independently reviewed staged patch 6e437226c361065dad96478101fc899f65cde86caf51a39415369c01c1725168.
What
Converted guest modules never run their
DT_PREINIT_ARRAYentries. The reader validates the array and rejects one without an initializer, and the unused-NID filter keeps the array's targets as roots, but no writer emitted or ran it: the Linux output only tagsDT_INIT_ARRAY/DT_FINI_ARRAY, and the macOS and Windows startup stubs only call the initializer and the init array.ld.soruns a shared object's preinit array only for the main executable (_dl_initreads it frommain_map), so the Linux output cannot use the tag. The preinit entries now run from the module'sDT_INITstub before the initializer, with zeroed argument registers and a null check per slot, the way the init array entries are called. The macOS and Windows startup stubs call them before the initializer. A deferred Windows module exports a thunk as its.ginitentry that runs the preinit entries before the initializer, since the deferred loader's slot list runs after the entry. The reader records the entries like the init and fini arrays.A module with a preinit array and no initializer is still rejected: the Linux stub that would run the entries exists only with one.
Tested
Windows 11, WinLibs GCC 15.2.0 r7, Release, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, CMake 4.4.4, Ninja):python3 tools/check_conventions.py --base origin/main: all checks passedctest: 168/168 passed (167 onmainplus the newguest_preinit)guest_preinitconverts a synthetic module with a two-entry preinit array whose entries record their calls:DT_INITstub runs the entries before the initializer, and the static check decodes the stub and its branch targets. On a Linux host the converted module loads and records(0, 0)for each preinit entry, then the initializer with the pending arguments, then its return value, in order..ginitentry RVA is the emitted thunk, its slots hold the array slots and the initializer, and the base relocations cover the slots.0x12840is missing from the entry code).Linux native execution of the new test runs in the relinker matrix on Linux hosts; it was not run here (Windows host only), and the other relinker tests skip native execution on Windows as on
main.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)