Repository navigation
fix(relinker): keep the bytes before a Linux PT_LOAD without file byt… - #24062
Conversation
…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
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against db365391de:
- Conventions:
tools/check_conventions.pypassed. - Build:
linux_load_pages_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #6118: linux_load_pages .................***Failed 0.03 sec 0% tests passed, 1 tests failed out of 1 - 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
left a comment
There was a problem hiding this comment.
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.
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
|
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.
|
edglz
left a comment
There was a problem hiding this comment.
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.
|
Follow-up after reviewing #23420: both PRs rewrite the early-return condition in |
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
|
#23420 has now incorporated this PR's mid-page |
|
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. |
|
Superseded by the combined implementation on #23420; see the comment above for integration evidence and validation. |
|
@malekaifa thanks for folding the empty- AI-assisted: yes Generated by Claude Code |
What
A
PT_LOADwithp_filesz0 that starts inside a page holding bytes of an earlier segment, such as a bss-onlyPT_LOADright after.data, is now handled bySeparateSharedLoadPagesin the Linux patcher like any otherPT_LOADthat starts inside a page. Before, it was skipped:elf_load()infs/binfmt_elf.c,vm_brk_flags()fromELF_PAGESTART(p_vaddr)). The earlier segment's bytes in that page read 0 in the converted main executable, in the default and the--lazy-bindingoutput.p_offsetminus the page offset ofp_vaddr(elf_map()). An earlier segment's zero fill can then read other file bytes, and with an incongruentp_offsetthat 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_offsetandp_vaddrdiffer 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_memszis 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(caserounded-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
SeparateSharedLoadPagesfor a different case (a largerp_alignincongruent with the file offset) and keeps skipping everyPT_LOADwithout 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:MapLikeLinuxnow maps an outputPT_LOADwithout 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 withbss in the first page of a segment without file bytes on Linux before 6.7: byte at 0x1000 reads 0x55, not 0x00and the incongruent one withPT_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.linux_empty_load_page: converts main executables whose code checks the data byte at 0x27f0 below a bss-onlyPT_LOADat 0x2800 and the zero fill above it (int3if they match,ud2if not), with a congruent and an incongruentp_offset, and runs them in the default and the--lazy-bindingoutput. SIGILL before, SIGTRAP after. The pre-6.7 kernel behaviour is covered by the model inlinux_load_pagesonly; no such kernel was run.ET_EXECwith the same layout, run natively on Linux 6.18 without the relinker, exits with 0 instead of the stored 90.ctest -j3): 1046 of 1046 pass.python3 tools/check_conventions.py --base d50640e264 --head HEAD: all conventions checks passed.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)