fix(relinker): keep unmapped gaps out of the Windows load image #6466
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6466"
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 whamre on 2026-10-15T16:27:00Z.
Requested by Werner · project thread
What
WindowsLoadImagekept one buffer covering everything from the first to the last PT_LOAD, andBuildSectionscopied the unmapped pages between segments into a section. An image with distant segments therefore allocated the whole gap twice: the 32 KiBlinux_entry_argvfar-load fixture (segment at 0x7ff00000) needs about 4 GiB and 30-40 s with--windowsor--macos, against 0.03 s for Linux output. The web converter runs the same code.Segment pages are now stored in blocks of contiguous page ranges. A gap that no segment maps becomes a section with an empty
Dataand aVirtualSize(newPeSectionfield, read throughMemorySize()by the PE writer and the Mach-O writer). The PE section headers were already writing such gaps with a zero raw size, so the output does not change.test_windows_sparse_load.pyconverts an image with a segment at 0x7fe00000 to Windows and macOS and checks the gap section has no file data; it takes 0.07 s, and onmainit times out.Tested
Ubuntu 24.04, GCC 13.3, relinker-only build:
ctest182/182 pass.-DANYPS5_ENABLE_ASAN=ON: no sanitizer reports;guest_module_argsandguest_deferred_dependenciesfail there onmainas well ("ASan runtime does not come first"). The 810 ELF inputs the relinker tests generate, each converted for Linux, Windows and macOS withmainand with this branch: all 2,430 outputs and exit codes are byte-identical. Far-load fixture: 4 GiB / 28 s before, 10 MiB / 0.01 s after. Not tested on Windows or macOS hosts.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)🤖 Generated with Claude Code
https://claude.ai/code/session_01FU6SmPEEXPAj2ML4rNkdwV
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T16:37:24Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/elfpatcher/src/macos/MacOsImage.cppcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/elfpatcher/src/macos/MacOsImage.cppcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T17:39:25Z.
Review: COMMENTED
Tested on a macOS host, which the description lists as not tested.
Machine: Apple M2 Max, macOS 27.2, Xcode 27, Rosetta 2. Head
5aa9af566merged ontomain03c4cb5e7, relinker-only build, Release.windows_sparse_load./usr/bin/python3(3.9.6): 171/174. The 3 failures (pr_overlap_tools,release_package,linux_fixed_base) fail the same way onmain03c4cb5e7.I converted the
sparse_fixture()image with--macos --skip-sce-moduleon the Mac:main03c4cb5e7In the Mach-O, the gap is
__ELF1withvmsize 0x7fdff000,filesize 0and protection 0. The far segment is mapped at+0x7fe00000.The test only converts on macOS; it runs the output only on Windows. So I ran the Mach-O under Rosetta. I placed an x86_64
libs/libkernel.prxnext to it, with a minimalAps5StartGuest_nid_no_patchthat calls the guest entry and printsrax. dyld maps the 2 GiB no-access gap without problems. The entry returns0x2a, the 42 that the Windows branch of the test checks for, and the process stays at about 4 MB RSS.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T19:46:12Z.
Independent Windows verification of head
5aa9af5665found a test portability issue, alongside confirming the memory reduction.The original
windows_sparse_loadCTest fails here at CreateProcess with WinError 193 for the near-2-GiB fixture (SizeOfImage 0x7fe3e000). I rebuilt the parent production code,0029e62f97: it produces byte-identical PE output and the same launch error. This is not a new execution regression caused by the sparse representation. The exact Windows loader constraint remains undetermined.The attached test-only patch retains the original 0x7fe00000-gap PE layout/file-size checks and Mach-O conversion, then converts an additional 64-MiB-gap fixture for native Windows execution and requires exit 42. It does not skip execution or alter production code. The original test fails on this host; the adjusted test passes against the same PR production binary.
Native Windows 11, WinLibs GCC 15.2r7 UCRT, Release relinker-only, Python 3.12.14:
Three conversions per variant/format. Timing includes process startup and 2-ms sampling; memory uses Windows GetProcessMemoryInfo. Outputs are byte-identical between variants in each format, all six small Windows outputs execute with exit 42, and input bytes stay unchanged. This is a synthetic conversion measurement, not an emulator/FPS claim. The parent control restores all five changed C++ files to the parent; PR CMake/test registration is retained, other production files are identical.
Validation:
44df2163. Patch application and diff checks pass. This is not PR-vs-current-main validation.The bundle contains the applicable patch, original/candidate test, comparison scripts, hashes, results and logs. Some passing test entries contain platform skips; several original-suite tests exceed the 30-s budget. No full system-library/emulator build, GPU/title/FPS test, Linux/macOS host execution or composition with current main. AI-assisted investigation, patch and verification.
sparse-load-windows-review-pr6466.zip
Imported from boykopovar/AnyPS5. Originally posted by whamre on 2026-10-18T10:30:01Z.
Thanks @jotitadev and @simonbronk for testing on macOS and Windows.
@simonbronk, good catch: Windows refuses to start the image with the 0x7fe00000 gap, on
mainas well.24e5819keeps that image for the PE layout and macOS checks and runs a 64 MiB-gap image for native Windows execution, as in your patch. The production code is unchanged.Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-18T13:35:57Z.
Windows check of
24e5819, merged onto currentmain(b66d2dad3= PR head +upstream/main, clean merge).Windows 11 Pro 26200, WinLibs GCC (MinGW-w64), Ninja, Release,
ANYPS5_RELINKER_ONLY=ON,BUILD_TESTING=ON.windows_sparse_load: passed. The 64 MiB-gap image now runs natively and exits with 42.Negative control: I reverted the production part of
5aa9af566(WindowsLoadImage,WindowsPeWriter,WindowsPeFormat.hpp,MacOsImage.cpp), kept the new test and rebuilt.windows_sparse_loadstill passes. On currentmain,6158444f7(fix(relinker): leave zero-filled PT_LOAD memory out of Windows files) already trims the zero raw data. Both relinkers produce the same 243,200-byte.exefor the 0x7fe00000-gap fixture, so the size and raw-section assertions no longer tell the two apart.The change still has a measurable effect on the relinker's memory use. Same fixture,
relinker --skip-sce-module --windows, sampled peak working set:mainbehaviour)So the fix is still worth having, but the test does not cover it on
mainany more. Something that fails without the fix would close that gap, for example a memory-bounded relinker run or a check on the load image.Not run: Onimusha or other titles. One run each way.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by whamre on 2026-10-18T13:44:44Z.
@simonbronk thanks, you're right: since
6158444the size checks pass with or without the fix.3c84815merges current main and makes the test also check the peak memory of the Windows and macOS conversions (resource.getrusage, so Linux and macOS hosts; Windows skips that check). With the production change reverted it fails with4296777728 bytes of memory for a 0x7fe00000 byte address gap; with it, it passes. Full relinker suite: 198/198 on Linux.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T01:00:34Z.
Review: COMMENTED
Ran
3c84815d9on macOS, since the new peak-memory check has adarwinbranch and the description lists macOS as not tested.Machine: Apple M2 Max, macOS 27.2, Xcode 27 clang, relinker-only Release builds. Head merged onto
maina373e85e5(clean merge). Native arm64, and x86_64 under Rosetta with Xcode's/usr/bin/python33.9.6.mainpasses 196/196 on both.test_windows_sparse_load.pyagainstmain's relinker fails with4296491008 bytes of memory for a 0x7fe00000 byte address gap(arm64; 4297785344 under Rosetta).Peak RSS of one conversion of the test's fixture (
ru_maxrssfromRUSAGE_CHILDREN, one process per conversion):--windows--macosmain, arm64main, x86_64 under Rosettadarwinbranch is right. macOS reportsru_maxrssin bytes, as the 4296491008 above shows. Multiplying by 1024 there would turn the PR's ~5 MiB into ~5 GiB and fail the check (I worked that out from the numbers; I didn't run it).main's for both the0x7fe00000fixture and the0x4000000runnable one. I ran both under Rosetta, with an x86_64libs/libkernel.prxwhoseAps5StartGuest_nid_no_patchcalls the entry and exits with its result. Both exit with 42. The0x7fe00000gap loads as a 2 GiB__ELF1segment with no access and no file data.The macOS half of the test doesn't cover the
MacOsImage.cppchange. I putmain'sMacOsImage.cppback on top of this head, which sizes segments byData.size()again.windows_sparse_loadand the rest of the suite still pass, 197/197 on arm64. But the gap segment'svmsizedrops from0x7fdff000to 0, so the image no longer reserves the 2 GiB between the twoPT_LOADs. The executable still runs, so a size or exit-code check can't see it. If you want that covered, an assert on the gap segment'svmsizein the Mach-O, or a byte comparison withmain's output, would do it.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.