fix(relinker): align the stack in Windows lazy import failure stubs #10945

Merged
roboterspinne merged 1 commit from upstream-pr/10945 into main 2026-10-26 16:53:57 +00:00
roboterspinne commented 2026-10-25 11:17:34 +00:00 (Migrated from github.com)

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 (WindowsPePatcher writes the stub address, and the entry stub skips the GOT write when GetProcAddress fails); the stub promises STATUS_ENTRYPOINT_NOT_FOUND, but guest code calls it under the SysV ABI at RSP = 8 mod 16, and it called lstrlenA, GetStdHandle, WriteFile and RaiseException without adjusting RSP and without shadow space, writing [rsp+0x20], [rsp+0x38] and [rsp+0x3c] into the caller's frame. A synthetic executable with one unresolved GLOB_DAT function import, called as sub rsp,8; call [slot], relinks with exit 0, prints FAIL: unresolved ELF import NoSuchFunctionXyz and then exits with 0xC0000005 instead of 0xC0000139. Nothing earlier rejects the input (CliArgs.cpp rejects --lazy-binding only together with --trace-imports or --linux-fixed-base, and GuestModuleBuilder refuses it only when bundled modules are converted); --lazy-binding is deprecated and documented in USAGE.md as debug-only, and this PR only makes its failure path keep the fail-fast contract.

  • Rule. Each lazy failure stub now starts with 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. 0x40 bytes hold the shadow space and the highest local writeString uses ([rsp+0x3c..0x3f]). The stub never returns: RaiseException is called with EXCEPTION_NONCONTINUABLE and followed by ud2, so it does not need to restore RSP. After the FAIL: unresolved ELF import <name> line the program now exits with 0xC0000139.
  • Tests. New test_windows_lazy_imports.py (windows_lazy_imports, timeout 30 s) relinks the fixture with --windows --lazy-binding --skip-sce-module on every host and asserts exit 0 and an output that starts with MZ. On Windows it runs the program and asserts status 0xC0000139, the FAIL line on stderr, Transferring control to ELF entry point on stdout and no Searched libraries line, 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 subtracts 0x60, so it calls the ELF entry with RSP = 0 mod 16, and the fixture's sub rsp,8; call [slot] enters the stub at 8 mod 16, as every SysV-conforming call does. On untouched code the test fails with status 0xc0000005, stderr 'FAIL: unresolved ELF import NoSuchFunctionXyz\n'.
  • Overlap. pr_overlap.py reports no shared lines with open pull requests that merge with main. It cannot compare this branch with #3965 (WindowsEntryStubBuilder.cpp) and with #5021, #6713 and #9408 (core/relinker/CMakeLists.txt), because those do not merge with main. Checked by hand: #3965 changes the entry stub's exception directory and other code, not the lazy stub loop this PR changes (line 874); the CMakeLists.txt hunks of #5021, #6713 and #9408 sit next to macos_output, elf_header and linux_trace_imports, not next to windows_bss, after which this PR registers windows_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.

  • Counter-check: with only the WindowsEntryStubBuilder.cpp change reverted to c7d6660ef8, windows_lazy_imports fails on the status assertion with AssertionError: 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).
  • Focused tests, 29 passed: 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_conversion and cli_args, the others each under 3 s.
  • Full suite: ctest --test-dir build-relinker -j 3 on c7d6660ef8: 262 tests, 258 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture), patch_advisor_unit (path with spaces); shader_cache_runtime_sources timed out under load and passed alone in 101.55 s.
  • Rebased onto 16ff208dc3 (#10646 merged) and rebuilt: the 7 tests matching windows_lazy_imports|windows_fixed_base|windows_named_imports|windows_import_modules|windows_bss|relinker_conversion|cli_args passed again.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004224236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10945). Originally posted by [roboterspinne](https://github.com/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 (`WindowsPePatcher` writes the stub address, and the entry stub skips the GOT write when `GetProcAddress` fails); the stub promises `STATUS_ENTRYPOINT_NOT_FOUND`, but guest code calls it under the SysV ABI at RSP = 8 mod 16, and it called `lstrlenA`, `GetStdHandle`, `WriteFile` and `RaiseException` without adjusting RSP and without shadow space, writing `[rsp+0x20]`, `[rsp+0x38]` and `[rsp+0x3c]` into the caller's frame. A synthetic executable with one unresolved `GLOB_DAT` function import, called as `sub rsp,8; call [slot]`, relinks with exit 0, prints `FAIL: unresolved ELF import NoSuchFunctionXyz` and then exits with `0xC0000005` instead of `0xC0000139`. Nothing earlier rejects the input (`CliArgs.cpp` rejects `--lazy-binding` only together with `--trace-imports` or `--linux-fixed-base`, and `GuestModuleBuilder` refuses it only when bundled modules are converted); `--lazy-binding` is deprecated and documented in `USAGE.md` as debug-only, and this PR only makes its failure path keep the fail-fast contract. - **Rule.** Each lazy failure stub now starts with `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. `0x40` bytes hold the shadow space and the highest local `writeString` uses (`[rsp+0x3c..0x3f]`). The stub never returns: `RaiseException` is called with `EXCEPTION_NONCONTINUABLE` and followed by `ud2`, so it does not need to restore RSP. After the `FAIL: unresolved ELF import <name>` line the program now exits with `0xC0000139`. - **Tests.** New `test_windows_lazy_imports.py` (`windows_lazy_imports`, timeout 30 s) relinks the fixture with `--windows --lazy-binding --skip-sce-module` on every host and asserts exit 0 and an output that starts with `MZ`. On Windows it runs the program and asserts status `0xC0000139`, the FAIL line on stderr, `Transferring control to ELF entry point` on stdout and no `Searched libraries` line, 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 subtracts `0x60`, so it calls the ELF entry with RSP = 0 mod 16, and the fixture's `sub rsp,8; call [slot]` enters the stub at 8 mod 16, as every SysV-conforming call does. On untouched code the test fails with `status 0xc0000005, stderr 'FAIL: unresolved ELF import NoSuchFunctionXyz\n'`. - **Overlap.** `pr_overlap.py` reports no shared lines with open pull requests that merge with `main`. It cannot compare this branch with #3965 (`WindowsEntryStubBuilder.cpp`) and with #5021, #6713 and #9408 (`core/relinker/CMakeLists.txt`), because those do not merge with `main`. Checked by hand: #3965 changes the entry stub's exception directory and other code, not the lazy stub loop this PR changes (line 874); the `CMakeLists.txt` hunks of #5021, #6713 and #9408 sit next to `macos_output`, `elf_header` and `linux_trace_imports`, not next to `windows_bss`, after which this PR registers `windows_lazy_imports`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> 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. - Counter-check: with only the `WindowsEntryStubBuilder.cpp` change reverted to `c7d6660ef8`, `windows_lazy_imports` fails on the status assertion with `AssertionError: 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). - Focused tests, 29 passed: `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_conversion` and `cli_args`, the others each under 3 s. - Full suite: `ctest --test-dir build-relinker -j 3` on `c7d6660ef8`: 262 tests, 258 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture), `patch_advisor_unit` (path with spaces); `shader_cache_runtime_sources` timed out under load and passed alone in 101.55 s. - Rebased onto `16ff208dc3` (#10646 merged) and rebuilt: the 7 tests matching `windows_lazy_imports|windows_fixed_base|windows_named_imports|windows_import_modules|windows_bss|relinker_conversion|cli_args` passed again. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - Not tested on Linux or macOS; CI runs the tests on all three hosts. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-10-25 11:19:02 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642894732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10945#pullrequestreview-5642894732). Originally posted by [MarcZX10](https://github.com/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.
MarcZX10 commented 2026-10-25 11:19:06 +00:00 (Migrated from github.com)

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 emitted and rsp, -16; sub rsp, 0x40 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642894912 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10945#pullrequestreview-5642894912). Originally posted by [MarcZX10](https://github.com/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 emitted `and rsp, -16; sub rsp, 0x40` 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10945
No description provided.