fix(relinker): place the Linux extra PT_LOAD past the last guest page #5543
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5543"
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 mariuszkreft on 2026-10-13T23:19:35Z.
What
ComputeExtraBlockVaddrincore/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cppkeeps the ELF congruencep_vaddr == p_offset (mod 0x1000)for the extra RWXPT_LOADand bumped the result by one page only when it fell below the highest kept guest load end as a byte address:That comparison is on byte addresses, not pages. When the highest guest load end is not page aligned and
offsetRemainder >= end % 0x1000, the value is already>= end(no bump) yet still lies inside the guest end's last 4 KiB page. The extraPT_LOADis emitted after the guest loads, and the Linux kernel maps eachPT_LOADpage by page withMAP_FIXEDin program-header order, so that page's file-backed mapping replaces the guest's.datatail and zero-filled.bss.GuestModuleWriter::WriteLinuxalready rounds both the file and the address up to0x4000for the guest extra segment; the host executable path did not.Fix: round the highest kept
PT_LOADend up to the page before adding the offset remainder.end % 0x1000yields the byte-identical address it did before, so aligned retail-style inputs and the existing Python fixtures are unchanged.p_vaddr % 0x1000 == p_offset % 0x1000still holds, because the anchor is page aligned.PT_LOADnow spans to the end of its own page inp_memsz, so the page it takes over is fully covered rather than partially.WriteLayoutderives the extra block vaddr from its file offset (and throws if a request disagrees) instead of trustingExtraBlockVaddr, and one_makeLoadHeaderhelper replaces the two near-identical ones. The header block keepsPF_Rand its per-guest alignment choice.Tested
Linux, relinker-only profile (
cmake -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release): 155/155 ctest pass,python3 tools/check_conventions.py --base origin/mainclean.python3 tools/progress.pyunchanged at 3224/3656.New coverage, all of which fails on
main:core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp(testprogram_layout_ranges): pins the arithmetic (0x8A00 -> 0x3A00, boundary0x8800 -> 0x3800,0x1000 -> 0x2000) and writes real program-header tables to assert the structural property — the extra block starts on its own page, every load below it ends on a strictly earlier page, and nothing overlaps it. The fixture with a non-page-aligned end (0x1800) fails onmainat the arithmetic case; the two-load fixtures fail the page check.core/relinker/relinker/tests/test_linux_program_header_order.py(testlinux_program_header_order): newunaligned-tailend-to-end case, a guestPT_LOADwhosep_memsztail ends mid-page, relinked and executed toSIGTRAP.check_extra_block_pageasserts the extra block clears every guest page, that itsp_memszreaches its page end, and that the header block sits above it. Onmainthe extra block lands on the guest's page withp_memsz == p_filesz.amd64_only_convertercaught the intermediate version of this change (its stub moved into the gap and left the extra segment) and passes.Nothing is verified on a console; this is the relinker's own output layout, checked by reading and executing the relinked ELF on Linux x86-64.
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 CnRJay on 2026-10-14T01:51:04Z.
Review: COMMENTED
The fix looks right to me. Rounding the highest kept load end up to a page before adding the remainder is the condition the kernel's page-granular
MAP_FIXEDneeds. Deriving the vaddr insideWriteLayout, instead of trusting the request, removes a way for the two values to drift apart. #4919 makes the same arithmetic change, so only one of them should land; this one has the structural and end-to-end coverage.Some debugging scaffolding is still in the test and could be removed before merge:
gLineglobal and thegLine = 3000…3006/gLine=__LINE__markers inProgramLayoutRangeTests.cpp. If a location is needed, a message perRequirereads better;std::cerr << "case3005 off=…"line inRequireExtraBlockStartsOnItsOwnPage, which prints on every run whereFileSize > MemorySizebefore theRequirethrows;Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T05:36:23Z.
The fix is right, and retail executables hit it: on current main the extra PT_LOAD lands on the last guest page in 8 of 13 we converted for Linux (PPSA34299: guest end 0x6401d68, extra block at 0x6401d74, file deltas 0x418000 vs 0x416000).
Before merging, please remove the debugging scaffolding in ProgramLayoutRangeTests.cpp that CnRJay listed: the
gLineglobal and markers (a message perRequirereads better), thestd::cerr << "case3005 ..."line, and add the missing final newline.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T17:12:55Z.
@mariuszkreft, here is the
ProgramLayoutRangeTests.cppcleanup from @CnRJay's review that @oneandonlydean asked for before merging. It is a patch on58120ca41that only touches the test file, so you can save it and rungit apply:Require(bool ok, const char* what)throwsprogram layout: <what>, so each check names itself. ThegLineglobal (L13-L14) and all its markers are gone.case3005std::cerrline (L48) is deleted.#include <string>is added for the message concatenation, and the file ends with a newline again.Patch for ProgramLayoutRangeTests.cpp (+17/-19)
Relinker-only profile with the patch applied (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release, GCC 15.2 on WSL Ubuntu): ctest passes 155/155 on58120ca41, and 165/165 on a merge with current main (e34c47921). WithProgramHeaderLayoutBuilder.hpp/.cppput back to the PR's base7a0c838ca, the test still fails, and the message changes fromProgram layout range validation failed at line 82toprogram layout: vaddr for end 0x2800, offset 0x8A00. Main'stools/check_conventions.pyalso passes with this PR's title and body.AI-assisted: written with Claude Code; I checked that the patch above, applied to
58120ca41, gives exactly the tree I tested, and ran the relinker-only build and ctest on that tree and on a merge with main, the test before and after the patch against the base builder, and main's check_conventions.py.Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-15T01:50:52Z.
Applied the cleanup @nikowolf123 prepared, unchanged, as
7f73643e0—Requirenames each check, thegLineglobal and its markers are gone, the straystd::cerrline is gone, and the file ends with a newline.Verified locally on this host (Ubuntu, GCC 13.3, relinker-only profile
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release) on7f73643e0:ctest --test-dir build-relinker --output-on-failure -j 12→ 155/155 passed in 13.2 sprogram_layout_ranges_tests→21 program layout cases passedpython3 tools/check_conventions.py --base origin/main→All conventions checks passedProgramHeaderLayoutBuilder.{cpp,hpp}put back to the PR base7a0c838ca, the same binary fails withprogram layout: vaddr for end 0x2800, offset 0x8A00(exit 1); with the fix restored it passes (exit 0)No title was run — this is host-side relinker output, checked by reading and executing the relinked ELF.
AI-assisted: yes (Claude Code; the cleanup patch is @nikowolf123's, reviewed here).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:13:30Z.
Checked the merge onto main: build and relinker ctest 132/132 green;
program_layout_rangesandlinux_program_header_orderfail with the fix reverted and pass with it. The scaffolding cleanup from the earlier reviews is in; merging.