fix(relinker): rebind a preloaded CRT's standard streams to the startup log #10157
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10157"
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 Usama171 on 2026-10-23T11:05:29Z.
What
A DLL that a global injector loads into every process (Windhawk's
windhawk.dllhere; antivirus hooks behave the same) can pullmsvcrt.dllorucrtbase.dllin before the converted executable's first instruction. In a--windows-guiprocess 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 laterSetStdHandleredirect toanyps5.lognever reaches those streams, so every write from a title module linked against that runtime is discarded as a successful no-op:anyps5.logholdsTransferring control to ELF entry pointand nothing from the module. On a machine with Windhawk installed,windows_import_modulesfails itsgui-dll-msvcrt-both-missingcase this way; preloadingucrtbase.dllreproduces the same loss for the ucrt host.After
SetCurrentDirectoryW, the entry stub now checks msvcrt.dll and ucrtbase.dll withGetModuleHandleW. For a runtime that is already loaded, it resolves__iob_funcor__acrt_iob_funcand_wfreopenfrom that module, and for each standard stream whose handle equals the stub's log handle it reopens the stream ontoanyps5.login 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_SUSPENDEDwithout standard handles, loads the stdio host's runtime into it withVirtualAllocEx/WriteProcessMemory/CreateRemoteThreadbefore resuming the main thread, and expects both probes inanyps5.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 withwindhawk.dllbeforemsvcrt.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, sowindows_import_modulesalso 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
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-23T11:08:38Z.
Review: CHANGES_REQUESTED
The exact-head branch is still 45 commits behind current
main(upstream/mainis not an ancestor of this head).CONTRIBUTING.mdrequires one branch per topic based on currentmain, and the build guidance says PRs that no longer merge cleanly must be rebased first. Please rebase onto currentmain, rerun the checks on the updated head, and request review again.\n\nI built the relinker target on Linux and ranwindows_import_moduleslocally: 1/1 passed. The PR reports 235/235 tests on Windows, including the active Windhawk reproduction; I have not independently run the Windows checks.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; focusedentry_stubandwindows_import_modulespassed (2/2), and the conventions and diff checks pass. Full CTest ran 240 tests: 238 passed; unrelatedweb_releaseandfile_identitytests failed in unchanged files (test_main_reconfigures_streamsmock stream lackswrite(), andSameFiledid 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 inWindowsEntryStubBuilder.cppanddocs/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 withDepends onso 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.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 toWindowsEntryStubBuilder.cppand its focused test;git diff --checkand 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.