Skip to content

fix(relinker): keep the bytes before a Linux PT_LOAD without file byt… - #24062

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
jpirz:fix-linux-shared-pages
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
jpirz:fix-linux-shared-pages

Conversation

@jpirz

@jpirz jpirz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

A PT_LOAD with p_filesz 0 that starts inside a page holding bytes of an earlier segment, such as a bss-only PT_LOAD right after .data, is now handled by SeparateSharedLoadPages in the Linux patcher like any other PT_LOAD that starts inside a page. Before, it was skipped:

  • Linux 6.7 and later map such a segment as zero pages from the start of its first page (elf_load() in fs/binfmt_elf.c, vm_brk_flags() from ELF_PAGESTART(p_vaddr)). The earlier segment's bytes in that page read 0 in the converted main executable, in the default and the --lazy-binding output.
  • Earlier kernels map that page from the file at p_offset minus the page offset of p_vaddr (elf_map()). An earlier segment's zero fill can then read other file bytes, and with an incongruent p_offset that mmap fails after the point of no return, so the process is killed with SIGSEGV while loading.

When the page would read other bytes under either behaviour, or p_offset and p_vaddr differ within the page, the segment is now copied to the end of the output after the page's image, with zero file bytes up to the end of that page or of the segment. Both kernels then map the page from the file and zero-fill the rest. p_memsz is unchanged. A page-aligned segment without file bytes needs nothing from the file and is left as it is.

Not changed here: a page shared by two PT_LOADs with different flags still gets the later segment's protection on Linux. test_linux_trace_imports.py (case rounded-nx) expects that. Guest modules are mapped by ld.so, which maps such a page from the file, and are not changed.

#23420 changes the same condition in SeparateSharedLoadPages for a different case (a larger p_align incongruent with the file offset) and keeps skipping every PT_LOAD without file bytes. The two do not depend on each other, but whichever lands second has to merge both conditions by hand so that a segment without file bytes that starts inside a shared page is still separated.

Tested

Ubuntu 24.04 x86-64 container (Linux 6.18, GCC 13.3), relinker-only build (Release), on d50640e.

  • linux_load_pages: MapLikeLinux now maps an output PT_LOAD without file bytes both as Linux 6.7 and later do and as earlier kernels do, and four layouts were added. Before the fix: data in the first page of a segment without file bytes on Linux 6.7 or later: byte at 0x1000 reads 0x00, not 0xaa; run first, the second layout fails with bss in the first page of a segment without file bytes on Linux before 6.7: byte at 0x1000 reads 0x55, not 0x00 and the incongruent one with PT_LOAD offset and address differ within a page. The fourth checks that a zero page below such a segment leaves it in place. After the fix: passed.
  • New linux_empty_load_page: converts main executables whose code checks the data byte at 0x27f0 below a bss-only PT_LOAD at 0x2800 and the zero fill above it (int3 if they match, ud2 if not), with a congruent and an incongruent p_offset, and runs them in the default and the --lazy-binding output. SIGILL before, SIGTRAP after. The pre-6.7 kernel behaviour is covered by the model in linux_load_pages only; no such kernel was run.
  • A static ET_EXEC with the same layout, run natively on Linux 6.18 without the relinker, exits with 0 instead of the stored 90.
  • Relinker-only suite (ctest -j3): 1046 of 1046 pass.
  • python3 tools/check_conventions.py --base d50640e264 --head HEAD: all conventions checks passed.

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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

…es in its page

A PT_LOAD with p_filesz 0 that starts inside a page holding bytes of an
earlier segment lost those bytes in the Linux output. Example: an RW
PT_LOAD at 0x1000 with p_filesz = p_memsz = 0x800, then an RW PT_LOAD
at 0x1800 with p_filesz 0 and p_memsz 0x1000. In the converted main
executable, in the default and the --lazy-binding output, the data at
0x1000-0x17ff reads 0 on Linux 6.7 and later. Before 6.7 the page is
read from the page of the file that holds p_offset instead, so an
earlier zero fill can show unrelated file bytes, and with an
incongruent p_offset the executable fails to load.

SeparateSharedLoadPages skipped every PT_LOAD without file bytes. Since
6.7, elf_load() in fs/binfmt_elf.c maps such a segment with
vm_brk_flags() from ELF_PAGESTART(p_vaddr), which replaces its whole
first page with zeros. Before 6.7, elf_map() mapped that page from the
file at p_offset minus the page offset of p_vaddr; when that is not
page-aligned the mmap fails after the point of no return and the
process is killed with SIGSEGV. A static ET_EXEC with the same layout,
run natively on Linux 6.18, exits with 0 instead of the data byte it
reads just below the segment without file bytes.

A segment without file bytes that starts inside a page is now checked
like the others. When its first page holds a nonzero byte of an earlier
segment, a byte that differs from the file page at its offset, or its
p_offset and p_vaddr differ within the page, it is copied to the end of
the output after the page's image with zero file bytes up to the end of
that page (or of the segment), so both kernels map the page from the
file and zero-fill the rest. A page-aligned one still needs nothing
from the file and stays as it is.

LinuxLoadPageTests now maps an output PT_LOAD without file bytes both
ways and checks four layouts: data below such a segment, which failed
in the 6.7 model; zero fill below it over other file bytes, which
failed in the earlier model; data below such a segment whose p_offset
differs within the page, which failed; and a zero page below it, which
must stay in place. linux_empty_load_page converts executables that
check the data byte below such a segment, with a congruent and an
incongruent p_offset, and runs them in the default and the
--lazy-binding output: they raised SIGILL before and reach their int3
now. The relinker-only suite passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014t2HhdnDYm3ps2BQNLaF2H

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against db365391de:

  • Conventions: tools/check_conventions.py passed.
  • Build: linux_load_pages_tests built cleanly.
  • CTest (linux_load_pages):
    1/1 Test #6118: linux_load_pages .................   Passed    0.10 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6118: linux_load_pages .................***Failed    0.03 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp: Verified SeparateSharedLoadPages handling when load.FileSize == 0; verified prefix calculation, page alignment, and differs condition under both incongruent and congruent cases
    • core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp: Verified buffer resizing, std::min(load.MemorySize, kDefaultLoadAlignment - prefix) calculation, std::copy_n with zero size, and load.FileSize / load.Offset updates
    • core/relinker/elfpatcher/tests/LinuxLoadPageTests.cpp: Verified MapLikeLinux simulation across both pre-6.7 (clearsEmptyLoadPage=false) and post-6.7 (clearsEmptyLoadPage=true) kernel behaviors and checked assertions across all four test fixtures
    • core/relinker/relinker/tests/test_linux_empty_load_page.py: Verified integration test execution flow across default and --lazy-binding modes, congruence vs incongruence, and correct signal trap assertions
    • AGENTS.md & Repository rules: Verified no forbidden comments, proper Conventional Commit formatting, and no prohibited file additions

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the inspected implementation and tests support merging.

What I ran

Reviewed head db365391de6716ac66cd434f7fe21d8c18fcd439 from the prepared diff and pinned source using repo_read.py. No build or tests were run during this review.

Results

Check Result Note
Changed lines passed All four changed files read
Build and tests not run Author-reported results only
Logic and edge cases passed Covered congruent and incongruent empty loads

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported build, ctest, convention checks, and runtime cases were not run in this review. I read core/relinker/CMakeLists.txt, core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, core/relinker/elfpatcher/tests/LinuxLoadPageTests.cpp, and core/relinker/relinker/tests/test_linux_empty_load_page.py. No changed files were excluded.

To get this to a good state

Nothing is required.

@ycbuzi

ycbuzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Tested db365391de merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • gha-linux-sandybridge: tests ran. 2 of the failing tests fail on main too.
  • gha-linux-westmere: tests ran. 6 of the failing tests fail on main too.
  • Windows 11 on ARM64 (GitHub-hosted runner), relinker-only build, WinLibs GCC 15.2.0: 99% tests passed, 15 tests failed out of 1068; 99% tests passed, 14 tests failed out of 1068. 14 of the failing tests fail on main too. run
  • Windows Server 2022 (GitHub-hosted runner), relinker-only build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 1068; 100% tests passed, 0 tests failed out of 1068. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 6149 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt
PR 11661 core/relinker/CMakeLists.txt
PR 11689 core/relinker/CMakeLists.txt
PR 11698 core/relinker/CMakeLists.txt
PR 12678 core/relinker/CMakeLists.txt
PR 13486 core/relinker/CMakeLists.txt
PR 13716 core/relinker/CMakeLists.txt
PR 15245 core/relinker/CMakeLists.txt
PR 17557 core/relinker/CMakeLists.txt
PR 18449 core/relinker/CMakeLists.txt
PR 21877 core/relinker/CMakeLists.txt
PR 23636 core/relinker/CMakeLists.txt
PR 23842 core/relinker/CMakeLists.txt
PR 23861 core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp
PR 24915 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp
PR 25483 core/relinker/CMakeLists.txt
PR 26232 core/relinker/CMakeLists.txt

@edglz edglz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

A bss-only PT_LOAD starting mid-page was skipped by SeparateSharedLoadPages, but Linux ≥ 6.7 maps it as zero pages from ELF_PAGESTART(p_vaddr) and older kernels map that page from the file at an offset that may be incongruent. Treating it like any other mid-page segment, and giving the relocated copy file bytes up to the page end (min(p_memsz, page - prefix)), makes both kernel behaviours read the earlier segment's bytes from the file. p_memsz is unchanged and page-aligned bss segments are untouched.

Verification

Environment: macOS 26, Apple M4 (arm64), Apple clang 21.0.0, CMake relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), PR diff applied on current main.

Check Result
Full ctest suite with this PR 948/948 pass (29 tooling tests timed out under -j10 load and passed on rerun)
linux_load_pages with the patcher change reverted Fails
linux_empty_load_page Passes on both here: its execution step only runs on Linux x86-64

Not verified: execution of the converted outputs on Linux or Windows; the tests that run them are host-gated.

Notes

None.

Verdict

Approved.

@edglz

edglz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Follow-up after reviewing #23420: both PRs rewrite the early-return condition in SeparateSharedLoadPages and treat FileSize == 0 segments differently. This PR relocates mid-page bss-only segments; #23420 keeps them in place and only fixes their alignment. My approval here stands, but whichever lands second needs to combine both rules (relocation of mid-page bss-only segments from here, full p_align congruence from #23420).

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head db365391de, applied with git apply on main fd4aeed483 together with the other sixteen PRs of one batch (#22799, #22850, #23003, #23528, #23597, #22752, #24129, #24082, #23717, #22967, #24555, #24631, #24658, #24610, #24607, #24597). All seventeen applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
linux_empty_load_page (1 test) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6524 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main fd4aeed483 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2126 agc_/video tests on the iGPU fail only agc_driver_stencil_plane_memory, also on main. guest_webrtc_time aborted once (0xc0000409) in the first full run of this batch; it passes 20 of 20 alone and the full guest_ set passed 3 of 3 on a rebuilt tree, and no PR of the batch touches webrtc, so I treat it as a one-off.

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@malekaifa

Copy link
Copy Markdown
Contributor

#23420 has now incorporated this PR's mid-page p_filesz == 0 relocation rule alongside its full-p_align congruence fix, after a dual-kernel regression reproduced the Linux 6.7+ clobber on the earlier #23420 head. Its current PR body declares Depends on: #24062, so the intended merge order is now: land #24062 first (including its dedicated linux_empty_load_page integration coverage), then rebase #23420 and retain only the additional alignment-congruence behavior plus its combined regression. I also confirmed the two current heads still conflict in LinuxElfPatcher.cpp and LinuxLoadPageTests.cpp, so they should not be merged independently in the opposite order without reconciliation. No source change from Prime A; this is cross-PR integration guidance only. AI-assisted: yes

@malekaifa

Copy link
Copy Markdown
Contributor

Superseded by the combined implementation now published on existing PR #23420 (head 586da64). The original #24062 empty-file-bytes regression coverage is retained there alongside #23420's PT_LOAD p_align congruence fix. The overlapping C++ implementation and LinuxLoadPageTests.cpp changes were explicitly reconciled; both regression classes pass in the combined branch. No duplicate PR was opened and no PR was merged. Closing this PR as superseded so the conflicting open PR pair is resolved; please review #23420 for the integrated result.

@malekaifa

Copy link
Copy Markdown
Contributor

Superseded by the combined implementation on #23420; see the comment above for integration evidence and validation.

@oneandonlydean
oneandonlydean merged commit 7ac4ea7 into boykopovar:main Nov 27, 2026
14 checks passed
@jpirz

jpirz commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@malekaifa thanks for folding the empty-p_filesz case into #23420 and checking the two heads together. I'd rather keep this PR open: it already has three approvals, it's the smaller change, and on Nov 23 we agreed to land it first and then rebase #23420 on top, leaving only the p_align congruence part there. Either way the fix gets in. If maintainers prefer the combined PR, I'm happy to close this one.

AI-assisted: yes


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants