fix(relinker): align the stack in Windows lazy import failure stubs #10945
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10945"
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 roboterspinne on 2026-10-25T11:17:34Z.
What
With
--windows --lazy-binding, a non-weak import that no searched library exports keeps a failure stub in its GOT slot (WindowsPePatcherwrites the stub address, and the entry stub skips the GOT write whenGetProcAddressfails); the stub promisesSTATUS_ENTRYPOINT_NOT_FOUND, but guest code calls it under the SysV ABI at RSP = 8 mod 16, and it calledlstrlenA,GetStdHandle,WriteFileandRaiseExceptionwithout adjusting RSP and without shadow space, writing[rsp+0x20],[rsp+0x38]and[rsp+0x3c]into the caller's frame. A synthetic executable with one unresolvedGLOB_DATfunction import, called assub rsp,8; call [slot], relinks with exit 0, printsFAIL: unresolved ELF import NoSuchFunctionXyzand then exits with0xC0000005instead of0xC0000139. Nothing earlier rejects the input (CliArgs.cpprejects--lazy-bindingonly together with--trace-importsor--linux-fixed-base, andGuestModuleBuilderrefuses it only when bundled modules are converted);--lazy-bindingis deprecated and documented inUSAGE.mdas debug-only, and this PR only makes its failure path keep the fail-fast contract.and rsp,-16; sub rsp,0x40(48 83 e4 f0 48 83 ec 40). The Win64 calling convention requires RSP to be 16-byte aligned at every call with 32 bytes of shadow space, while psABI 3.2.2 leaves RSP at 8 mod 16 on entry to the stub.0x40bytes hold the shadow space and the highest localwriteStringuses ([rsp+0x3c..0x3f]). The stub never returns:RaiseExceptionis called withEXCEPTION_NONCONTINUABLEand followed byud2, so it does not need to restore RSP. After theFAIL: unresolved ELF import <name>line the program now exits with0xC0000139.test_windows_lazy_imports.py(windows_lazy_imports, timeout 30 s) relinks the fixture with--windows --lazy-binding --skip-sce-moduleon every host and asserts exit 0 and an output that starts withMZ. On Windows it runs the program and asserts status0xC0000139, the FAIL line on stderr,Transferring control to ELF entry pointon stdout and noSearched librariesline, so the failure comes from the lazy stub and not from eager binding. The caller's alignment is deterministic: the entry stub pushes five registers and subtracts0x60, so it calls the ELF entry with RSP = 0 mod 16, and the fixture'ssub rsp,8; call [slot]enters the stub at 8 mod 16, as every SysV-conforming call does. On untouched code the test fails withstatus 0xc0000005, stderr 'FAIL: unresolved ELF import NoSuchFunctionXyz\n'.pr_overlap.pyreports no shared lines with open pull requests that merge withmain. It cannot compare this branch with #3965 (WindowsEntryStubBuilder.cpp) and with #5021, #6713 and #9408 (core/relinker/CMakeLists.txt), because those do not merge withmain. Checked by hand: #3965 changes the entry stub's exception directory and other code, not the lazy stub loop this PR changes (line 874); theCMakeLists.txthunks of #5021, #6713 and #9408 sit next tomacos_output,elf_headerandlinux_trace_imports, not next towindows_bss, after which this PR registerswindows_lazy_imports.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew; the relinked test program was run on this host.WindowsEntryStubBuilder.cppchange reverted toc7d6660ef8,windows_lazy_importsfails on the status assertion withAssertionError: status 0xc0000005, stderr 'FAIL: unresolved ELF import NoSuchFunctionXyz\n'(0.85 s); with the change restored it passes in three of three runs (--repeat until-fail:3: 2.26 s, 0.31 s, 0.35 s).windows_lazy_imports(0.39 s),windows_fixed_base(2.1 s, runs a resolved lazy import),windows_named_imports(1.25 s, eager unresolved imports),windows_import_modules(9.0 s),windows_trace_imports(1.8 s),windows_runpath(1.2 s),windows_guest_weak_imports,weak_imports,optional_plt,needed_libraries,copy_relocations,executable_tls_relocations,windows_dependency_diagnostics,windows_diagnostic_fixture_isolation,windows_kernel_variant_sys,windows_kernel_variant_web,windows_bss,windows_sparse_load,windows_address_space,windows_gui,windows_working_directory,windows_tls_zero_fill,windows_tls_protected_entries,windows_tls_thread_pointer,windows_guest_export_aliases,amd64_only_windows,entry_stub,relinker_conversionandcli_args, the others each under 3 s.ctest --test-dir build-relinker -j 3onc7d6660ef8: 262 tests, 258 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture),patch_advisor_unit(path with spaces);shader_cache_runtime_sourcestimed out under load and passed alone in 101.55 s.16ff208dc3(#10646 merged) and rebuilt: the 7 tests matchingwindows_lazy_imports|windows_fixed_base|windows_named_imports|windows_import_modules|windows_bss|relinker_conversion|cli_argspassed again.python tools/check_conventions.py --base upstream/mainwith this title and description passed.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:19:02Z.
Review: APPROVED
Reviewed exact head
6a66f2a. The new Windows lazy-import test builds and converts a fixture on all hosts, and executes it only on Windows to assert STATUS_ENTRYPOINT_NOT_FOUND and the diagnostic text. The emitted establishes the expected x64 stack alignment and shadow space before invoking the failure path. Local conventions and diff checks passed, and I ran the focused test against the available relinker binary; conversion passed. Runtime execution is Windows-only and is covered by hosted CI, not by my local run.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:19:06Z.
Review: APPROVED
Reviewed exact head
6a66f2a. The new Windows lazy-import test builds and converts a fixture on all hosts, and executes it only on Windows to assert STATUS_ENTRYPOINT_NOT_FOUND and the diagnostic text. The emittedand rsp, -16; sub rsp, 0x40establishes the expected x64 stack alignment and shadow space before invoking the failure path. Local conventions and diff checks passed, and I ran the focused test against the available relinker binary; conversion passed. Runtime execution is Windows-only and is covered by hosted CI, not by my local run.