Windows: a GUI title started without standard handles loses the stdio of msvcrt- and ucrt-linked modules to a preloaded CRT (injected DLLs: Windhawk, antivirus) #10152

Closed
opened 2026-10-23 10:56:07 +00:00 by Usama171 · 0 comments
Usama171 commented 2026-10-23 10:56:07 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Usama171 on 2026-10-23T10:56:07Z.


What happens

windows_import_modules fails its gui-dll-msvcrt-both-missing case: anyps5.log holds only "Transferring control to ELF entry point" and the module's "Windows DLL stdout probe"/"Windows DLL stderr probe" never arrive, because an injected DLL (Windhawk here) loads msvcrt.dll before the entry stub's SetStdHandle and msvcrt pins its streams to _file = -2 (no console) for the life of the process.

Title, test or build step

ctest windows_import_modules (relinker-only build), the gui-dll-msvcrt-both-missing case; the ucrt case passes only because nothing preloads ucrtbase.dll here.

AnyPS5 commit

main 348157914 (2026-10-23), no local changes.

System

Windows 11 Home 26200, WinLibs GCC 15.3.0 (x86_64-ucrt-posix-seh, r1), CMake 4.2.0. Windhawk 1.6 installed with its global injection active; no AppInit_DLLs. No GPU involved.

Steps to reproduce

cmake --build build-relinker --parallel
ctest --test-dir build-relinker -R windows_import_modules --output-on-failure

Fails after about 18 s, every run, on any machine where a globally injected DLL links msvcrt. Without an injector, loading msvcrt.dll or ucrtbase.dll into the suspended converted executable with CreateRemoteThread before its first instruction reproduces the same loss for either runtime.

Log

Log
200:   File "D:\anyps5\AnyPS5\core\relinker\relinker\tests\test_windows_import_modules.py", line 566, in main
200:     assert 'Windows DLL stdout probe' in content, (runtime, content)
200:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200: AssertionError: ('msvcrt', 'Transferring control to ELF entry point\n')

What was checked

  • The exit-status check before the assertion passes: the module's export ran and returned 11; only its stdio is lost. Not antivirus quarantine.
  • A GUI-subsystem probe without a CRT (-nostdlib, WriteFile only) enumerates its modules at entry: windhawk.dll is injected and msvcrt.dll is already loaded before the executable's first instruction (apphelp.dll, windhawk.dll, ADVAPI32.dll, msvcrt.dll, sechost.dll, RPCRT4.dll).
  • A second probe in the same no-handles GUI state reads msvcrt's __iob_func() records after SetStdHandle to a log file: stdout and stderr hold flag=0x2 file=-2. msvcrt sets _file = -2 when its CRT initializes without a standard handle, and fputs to such a stream returns success while writing nothing, so the loss is silent. A later SetStdHandle never rebinds them.
  • Calling msvcrt's own freopen(logPath, "a", __iob_func() + 48) in that state returns non-null, the record becomes flag=0x2 file=3, and the next fputs lands in the log.
  • ucrt has the same defect: a probe that loads ucrtbase.dll first, then sets the standard handles, loses fputs through __acrt_iob_func(1) the same way, and _wfreopen from ucrtbase rebinds it.
  • On a machine without a global injector the runtime first loads with the module itself, after the entry stub's SetStdHandle, so its CRT init sees the log handles and the case passes, which is why CI does not see this.
  • Beyond the test: a title's bundled Windows module linked against either runtime loses all its logging in anyps5.log on any machine with a global DLL injector (Windhawk, several antivirus products), silently.

A fix follows as a pull request: after the entry stub redirects the standard handles to anyps5.log, for each of msvcrt.dll and ucrtbase.dll that GetModuleHandleW reports already loaded, rebind the streams whose standard handle is the stub's log handle through that runtime's own exports (__iob_func or __acrt_iob_func, plus _wfreopen in append mode; the stub's log handle already shares read and write). Verified by the probes above, with a regression test that preloads the runtime into the suspended guest with CreateRemoteThread.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5996489636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10152). Originally posted by [Usama171](https://github.com/Usama171) on 2026-10-23T10:56:07Z. --- ### What happens windows_import_modules fails its gui-dll-msvcrt-both-missing case: anyps5.log holds only "Transferring control to ELF entry point" and the module's "Windows DLL stdout probe"/"Windows DLL stderr probe" never arrive, because an injected DLL (Windhawk here) loads msvcrt.dll before the entry stub's SetStdHandle and msvcrt pins its streams to _file = -2 (no console) for the life of the process. ### Title, test or build step ctest windows_import_modules (relinker-only build), the gui-dll-msvcrt-both-missing case; the ucrt case passes only because nothing preloads ucrtbase.dll here. ### AnyPS5 commit main 348157914 (2026-10-23), no local changes. ### System Windows 11 Home 26200, WinLibs GCC 15.3.0 (x86_64-ucrt-posix-seh, r1), CMake 4.2.0. Windhawk 1.6 installed with its global injection active; no AppInit_DLLs. No GPU involved. ### Steps to reproduce cmake --build build-relinker --parallel ctest --test-dir build-relinker -R windows_import_modules --output-on-failure Fails after about 18 s, every run, on any machine where a globally injected DLL links msvcrt. Without an injector, loading msvcrt.dll or ucrtbase.dll into the suspended converted executable with CreateRemoteThread before its first instruction reproduces the same loss for either runtime. ### Log ```text Log 200: File "D:\anyps5\AnyPS5\core\relinker\relinker\tests\test_windows_import_modules.py", line 566, in main 200: assert 'Windows DLL stdout probe' in content, (runtime, content) 200: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 200: AssertionError: ('msvcrt', 'Transferring control to ELF entry point\n') ``` ### What was checked - The exit-status check before the assertion passes: the module's export ran and returned 11; only its stdio is lost. Not antivirus quarantine. - A GUI-subsystem probe without a CRT (-nostdlib, WriteFile only) enumerates its modules at entry: windhawk.dll is injected and msvcrt.dll is already loaded before the executable's first instruction (apphelp.dll, windhawk.dll, ADVAPI32.dll, msvcrt.dll, sechost.dll, RPCRT4.dll). - A second probe in the same no-handles GUI state reads msvcrt's __iob_func() records after SetStdHandle to a log file: stdout and stderr hold flag=0x2 file=-2. msvcrt sets _file = -2 when its CRT initializes without a standard handle, and fputs to such a stream returns success while writing nothing, so the loss is silent. A later SetStdHandle never rebinds them. - Calling msvcrt's own freopen(logPath, "a", __iob_func() + 48) in that state returns non-null, the record becomes flag=0x2 file=3, and the next fputs lands in the log. - ucrt has the same defect: a probe that loads ucrtbase.dll first, then sets the standard handles, loses fputs through __acrt_iob_func(1) the same way, and _wfreopen from ucrtbase rebinds it. - On a machine without a global injector the runtime first loads with the module itself, after the entry stub's SetStdHandle, so its CRT init sees the log handles and the case passes, which is why CI does not see this. - Beyond the test: a title's bundled Windows module linked against either runtime loses all its logging in anyps5.log on any machine with a global DLL injector (Windhawk, several antivirus products), silently. A fix follows as a pull request: after the entry stub redirects the standard handles to anyps5.log, for each of msvcrt.dll and ucrtbase.dll that GetModuleHandleW reports already loaded, rebind the streams whose standard handle is the stub's log handle through that runtime's own exports (__iob_func or __acrt_iob_func, plus _wfreopen in append mode; the stub's log handle already shares read and write). Verified by the probes above, with a regression test that preloads the runtime into the suspended guest with CreateRemoteThread.
Sign in to join this conversation.
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#10152
No description provided.