fix(relinker): rebind a preloaded CRT's standard streams to the startup log #10157

Merged
Usama171 merged 6 commits from upstream-pr/10157 into main 2026-10-24 23:45:30 +00:00
Usama171 commented 2026-10-23 11:05:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Usama171 on 2026-10-23T11:05:29Z.


What

A DLL that a global injector loads into every process (Windhawk's windhawk.dll here; antivirus hooks behave the same) can pull msvcrt.dll or ucrtbase.dll in before the converted executable's first instruction. In a --windows-gui process started without standard handles, that CRT initializes with no standard handles and pins its stdout and stderr to _file = -2, msvcrt's and ucrt's no-console sentinel. The entry stub's later SetStdHandle redirect to anyps5.log never reaches those streams, so every write from a title module linked against that runtime is discarded as a successful no-op: anyps5.log holds Transferring control to ELF entry point and nothing from the module. On a machine with Windhawk installed, windows_import_modules fails its gui-dll-msvcrt-both-missing case this way; preloading ucrtbase.dll reproduces the same loss for the ucrt host.

After SetCurrentDirectoryW, the entry stub now checks msvcrt.dll and ucrtbase.dll with GetModuleHandleW. For a runtime that is already loaded, it resolves __iob_func or __acrt_iob_func and _wfreopen from that module, and for each standard stream whose handle equals the stub's log handle it reopens the stream onto anyps5.log in append mode. A runtime that is not loaded, a stream that kept its own handle, a failed log creation and a runtime that first loads with a module (whose CRT init binds the redirected handles, as before) are unchanged.

The new test case starts the converted executable CREATE_SUSPENDED without standard handles, loads the stdio host's runtime into it with VirtualAllocEx/WriteProcessMemory/CreateRemoteThread before resuming the main thread, and expects both probes in anyps5.log. Without the fix it fails for both runtimes with ('msvcrt'|'ucrt', 'Transferring control to ELF entry point\n'), on any machine.

Diagnosis details (probe programs, msvcrt/ucrt stream dumps showing flag=0x2 file=-2, module list at entry with windhawk.dll before msvcrt.dll) are in issue #10152.

Tested

Windows 11 Home 26200, WinLibs GCC 15.3.0 (x86_64-ucrt-posix-seh r1), CMake 4.2.0, relinker-only build. ctest --test-dir build-relinker --output-on-failure: 235/235 pass. This machine has Windhawk 1.6 active, so windows_import_modules also exercises the fix against a real injector: it failed its msvcrt stdio cases before the change and passes after. With the stub change reverted and the new test kept, the preloaded-runtime case fails for both runtimes, confirming the test detects the bug without an injector installed.

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: #10152
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4981771645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10157). Originally posted by [Usama171](https://github.com/Usama171) on 2026-10-23T11:05:29Z. --- ### What A DLL that a global injector loads into every process (Windhawk's `windhawk.dll` here; antivirus hooks behave the same) can pull `msvcrt.dll` or `ucrtbase.dll` in before the converted executable's first instruction. In a `--windows-gui` process started without standard handles, that CRT initializes with no standard handles and pins its stdout and stderr to `_file = -2`, msvcrt's and ucrt's no-console sentinel. The entry stub's later `SetStdHandle` redirect to `anyps5.log` never reaches those streams, so every write from a title module linked against that runtime is discarded as a successful no-op: `anyps5.log` holds `Transferring control to ELF entry point` and nothing from the module. On a machine with Windhawk installed, `windows_import_modules` fails its `gui-dll-msvcrt-both-missing` case this way; preloading `ucrtbase.dll` reproduces the same loss for the ucrt host. After `SetCurrentDirectoryW`, the entry stub now checks msvcrt.dll and ucrtbase.dll with `GetModuleHandleW`. For a runtime that is already loaded, it resolves `__iob_func` or `__acrt_iob_func` and `_wfreopen` from that module, and for each standard stream whose handle equals the stub's log handle it reopens the stream onto `anyps5.log` in append mode. A runtime that is not loaded, a stream that kept its own handle, a failed log creation and a runtime that first loads with a module (whose CRT init binds the redirected handles, as before) are unchanged. The new test case starts the converted executable `CREATE_SUSPENDED` without standard handles, loads the stdio host's runtime into it with `VirtualAllocEx`/`WriteProcessMemory`/`CreateRemoteThread` before resuming the main thread, and expects both probes in `anyps5.log`. Without the fix it fails for both runtimes with `('msvcrt'|'ucrt', 'Transferring control to ELF entry point\n')`, on any machine. Diagnosis details (probe programs, msvcrt/ucrt stream dumps showing `flag=0x2 file=-2`, module list at entry with `windhawk.dll` before `msvcrt.dll`) are in issue #10152. ### Tested Windows 11 Home 26200, WinLibs GCC 15.3.0 (x86_64-ucrt-posix-seh r1), CMake 4.2.0, relinker-only build. `ctest --test-dir build-relinker --output-on-failure`: 235/235 pass. This machine has Windhawk 1.6 active, so `windows_import_modules` also exercises the fix against a real injector: it failed its msvcrt stdio cases before the change and passes after. With the stub change reverted and the new test kept, the preloaded-runtime case fails for both runtimes, confirming the test detects the bug without an injector installed. ### 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: none - [x] Fixes: #10152 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 11:08:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T11:08:38Z.

Review: CHANGES_REQUESTED


The exact-head branch is still 45 commits behind current main (upstream/main is not an ancestor of this head). CONTRIBUTING.md requires one branch per topic based on current main, and the build guidance says PRs that no longer merge cleanly must be rebased first. Please rebase onto current main, rerun the checks on the updated head, and request review again.\n\nI built the relinker target on Linux and ran windows_import_modules locally: 1/1 passed. The PR reports 235/235 tests on Windows, including the active Windhawk reproduction; I have not independently run the Windows checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627397179 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10157#pullrequestreview-5627397179). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T11:08:38Z. **Review:** CHANGES_REQUESTED --- The exact-head branch is still 45 commits behind current `main` (`upstream/main` is not an ancestor of this head). `CONTRIBUTING.md` requires one branch per topic based on current `main`, and the build guidance says PRs that no longer merge cleanly must be rebased first. Please rebase onto current `main`, rerun the checks on the updated head, and request review again.\n\nI built the relinker target on Linux and ran `windows_import_modules` locally: 1/1 passed. The PR reports 235/235 tests on Windows, including the active Windhawk reproduction; I have not independently run the Windows checks.
MarcZX10 commented 2026-10-23 13:06:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T13:06:33Z.

Review: CHANGES_REQUESTED


Re-review of exact head 1e79db706fc54426dfbe4d6e005a74a3b5493d8b: the rebased change and expanded suspended-process regression are coherent. Local Linux relinker-only Release build succeeded; focused entry_stub and windows_import_modules passed (2/2), and the conventions and diff checks pass. Full CTest ran 240 tests: 238 passed; unrelated web_release and file_identity tests failed in unchanged files (test_main_reconfigures_streams mock stream lacks write(), and SameFile did not throw for a missing file in this environment). Hosted conventions/labels pass; Windows 235/235 and live Windhawk results are author-reported, not run here. Current main has advanced six commits beyond this rebase, but none touches the PR’s relinker files and the source-only merge preview is clean.\n\nOne merge-order item remains: the overlap report identifies open #3965, and a direct merge preview of these two heads reports content conflicts in WindowsEntryStubBuilder.cpp and docs/dev/debt/libc.md; both currently insert into the same startup-stub declaration area and debt section. Please agree an order with #3965’s author and declare it with Depends on so the second landing preserves both changes. I manually checked #7594’s MXCSR insertion; its implementation is in a separate stub block, so I found no direct code dependency there. This is a sequencing request under CONTRIBUTING.md, not a request to close or abandon either PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5628628429 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10157#pullrequestreview-5628628429). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T13:06:33Z. **Review:** CHANGES_REQUESTED --- Re-review of exact head `1e79db706fc54426dfbe4d6e005a74a3b5493d8b`: the rebased change and expanded suspended-process regression are coherent. Local Linux relinker-only Release build succeeded; focused `entry_stub` and `windows_import_modules` passed (2/2), and the conventions and diff checks pass. Full CTest ran 240 tests: 238 passed; unrelated `web_release` and `file_identity` tests failed in unchanged files (`test_main_reconfigures_streams` mock stream lacks `write()`, and `SameFile` did not throw for a missing file in this environment). Hosted conventions/labels pass; Windows 235/235 and live Windhawk results are author-reported, not run here. Current main has advanced six commits beyond this rebase, but none touches the PR’s relinker files and the source-only merge preview is clean.\n\nOne merge-order item remains: the overlap report identifies open #3965, and a direct merge preview of these two heads reports content conflicts in `WindowsEntryStubBuilder.cpp` and `docs/dev/debt/libc.md`; both currently insert into the same startup-stub declaration area and debt section. Please agree an order with #3965’s author and declare it with `Depends on` so the second landing preserves both changes. I manually checked #7594’s MXCSR insertion; its implementation is in a separate stub block, so I found no direct code dependency there. This is a sequencing request under CONTRIBUTING.md, not a request to close or abandon either PR.
MarcZX10 commented 2026-10-23 13:56:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T13:56:54Z.

Review: APPROVED


Re-review of exact head 1e79db706fc54426dfbe4d6e005a74a3b5493d8b. The change rebinds both preloaded CRT standard-stream imports in the startup stub and adds a suspended-process regression covering both CRTs. It is limited to WindowsEntryStubBuilder.cpp and its focused test; git diff --check and the exact-head repository conventions checker pass. The author reports the 235-test Windows relinker-only suite passing on this head, including the live Windhawk case; I independently ran the focused Linux relinker-only tests (2/2) on the prior rebased code and checked the exact current source/merge preview. Hosted conventions and labels pass. Full product builds were skipped by path policy.

The PR is clean against current main. The overlap with #3965 is explicitly sequenced by both authors as #10157 first; an exact pairwise merge preview still shows conflicts in libc CMake and guest stack implementation/header files, so #3965 must integrate this change after #10157 lands and rerun its Windows stack tests. That does not block this independent PR. Approving this exact head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5629254392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10157#pullrequestreview-5629254392). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T13:56:54Z. **Review:** APPROVED --- Re-review of exact head `1e79db706fc54426dfbe4d6e005a74a3b5493d8b`. The change rebinds both preloaded CRT standard-stream imports in the startup stub and adds a suspended-process regression covering both CRTs. It is limited to `WindowsEntryStubBuilder.cpp` and its focused test; `git diff --check` and the exact-head repository conventions checker pass. The author reports the 235-test Windows relinker-only suite passing on this head, including the live Windhawk case; I independently ran the focused Linux relinker-only tests (2/2) on the prior rebased code and checked the exact current source/merge preview. Hosted conventions and labels pass. Full product builds were skipped by path policy. The PR is clean against current `main`. The overlap with #3965 is explicitly sequenced by both authors as #10157 first; an exact pairwise merge preview still shows conflicts in libc CMake and guest stack implementation/header files, so #3965 must integrate this change after #10157 lands and rerun its Windows stack tests. That does not block this independent PR. Approving this exact head.
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!10157
No description provided.