fix(relinker): place the Linux extra PT_LOAD past the last guest page #5543

Merged
mariuszkreft merged 2 commits from upstream-pr/5543 into main 2026-10-15 11:31:07 +00:00
mariuszkreft commented 2026-10-13 23:19:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-13T23:19:35Z.


What

ComputeExtraBlockVaddr in core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp keeps the ELF congruence p_vaddr == p_offset (mod 0x1000) for the extra RWX PT_LOAD and bumped the result by one page only when it fell below the highest kept guest load end as a byte address:

const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1);
std::uint64_t vaddr = alignedFloor | offsetRemainder;
if (vaddr < highestVaddrEnd) vaddr += align;

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 extra PT_LOAD is emitted after the guest loads, and the Linux kernel maps each PT_LOAD page by page with MAP_FIXED in program-header order, so that page's file-backed mapping replaces the guest's .data tail and zero-filled .bss. GuestModuleWriter::WriteLinux already rounds both the file and the address up to 0x4000 for the guest extra segment; the host executable path did not.

Fix: round the highest kept PT_LOAD end up to the page before adding the offset remainder.

  • Only the overlapping input class changes value. A page-aligned end or a remainder smaller than end % 0x1000 yields the byte-identical address it did before, so aligned retail-style inputs and the existing Python fixtures are unchanged.
  • p_vaddr % 0x1000 == p_offset % 0x1000 still holds, because the anchor is page aligned.
  • The extra PT_LOAD now spans to the end of its own page in p_memsz, so the page it takes over is fully covered rather than partially.
  • WriteLayout derives the extra block vaddr from its file offset (and throws if a request disagrees) instead of trusting ExtraBlockVaddr, and one _makeLoadHeader helper replaces the two near-identical ones. The header block keeps PF_R and 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/main clean. python3 tools/progress.py unchanged at 3224/3656.

New coverage, all of which fails on main:

  • core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp (test program_layout_ranges): pins the arithmetic (0x8A00 -> 0x3A00, boundary 0x8800 -> 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 on main at the arithmetic case; the two-load fixtures fail the page check.
  • core/relinker/relinker/tests/test_linux_program_header_order.py (test linux_program_header_order): new unaligned-tail end-to-end case, a guest PT_LOAD whose p_memsz tail ends mid-page, relinked and executed to SIGTRAP. check_extra_block_page asserts the extra block clears every guest page, that its p_memsz reaches its page end, and that the header block sits above it. On main the extra block lands on the guest's page with p_memsz == p_filesz.

amd64_only_converter caught 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

  • 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
  • Unblocks: none
  • GPUs tested: n/a (relinker only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4850715220 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543). Originally posted by [mariuszkreft](https://github.com/mariuszkreft) on 2026-10-13T23:19:35Z. --- ### What `ComputeExtraBlockVaddr` in `core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp` keeps the ELF congruence `p_vaddr == p_offset (mod 0x1000)` for the extra RWX `PT_LOAD` and bumped the result by one page only when it fell below the highest kept guest load end **as a byte address**: ```cpp const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1); std::uint64_t vaddr = alignedFloor | offsetRemainder; if (vaddr < highestVaddrEnd) vaddr += align; ``` 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 extra `PT_LOAD` is emitted after the guest loads, and the Linux kernel maps each `PT_LOAD` page by page with `MAP_FIXED` in program-header order, so that page's file-backed mapping replaces the guest's `.data` tail and zero-filled `.bss`. `GuestModuleWriter::WriteLinux` already rounds both the file and the address up to `0x4000` for the guest extra segment; the host executable path did not. Fix: round the highest kept `PT_LOAD` end up to the page before adding the offset remainder. - Only the overlapping input class changes value. A page-aligned end or a remainder smaller than `end % 0x1000` yields the byte-identical address it did before, so aligned retail-style inputs and the existing Python fixtures are unchanged. - `p_vaddr % 0x1000 == p_offset % 0x1000` still holds, because the anchor is page aligned. - The extra `PT_LOAD` now spans to the end of its own page in `p_memsz`, so the page it takes over is fully covered rather than partially. - `WriteLayout` derives the extra block vaddr from its file offset (and throws if a request disagrees) instead of trusting `ExtraBlockVaddr`, and one `_makeLoadHeader` helper replaces the two near-identical ones. The header block keeps `PF_R` and 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/main` clean. `python3 tools/progress.py` unchanged at 3224/3656. New coverage, all of which fails on `main`: - `core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp` (test `program_layout_ranges`): pins the arithmetic (`0x8A00 -> 0x3A00`, boundary `0x8800 -> 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 on `main` at the arithmetic case; the two-load fixtures fail the page check. - `core/relinker/relinker/tests/test_linux_program_header_order.py` (test `linux_program_header_order`): new `unaligned-tail` end-to-end case, a guest `PT_LOAD` whose `p_memsz` tail ends mid-page, relinked and executed to `SIGTRAP`. `check_extra_block_page` asserts the extra block clears every guest page, that its `p_memsz` reaches its page end, and that the header block sits above it. On `main` the extra block lands on the guest's page with `p_memsz == p_filesz`. `amd64_only_converter` caught 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a (relinker only) - [x] AI-assisted: yes
CnRJay commented 2026-10-14 01:51:04 +00:00 (Migrated from github.com)

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_FIXED needs. Deriving the vaddr inside WriteLayout, 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:

  • the gLine global and the gLine = 3000…3006 / gLine=__LINE__ markers in ProgramLayoutRangeTests.cpp. If a location is needed, a message per Require reads better;
  • the unconditional std::cerr << "case3005 off=…" line in RequireExtraBlockStartsOnItsOwnPage, which prints on every run where FileSize > MemorySize before the Require throws;
  • the missing newline at the end of the file.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5512139674 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543#pullrequestreview-5512139674). Originally posted by [CnRJay](https://github.com/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_FIXED` needs. Deriving the vaddr inside `WriteLayout`, 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: - the `gLine` global and the `gLine = 3000…3006` / `gLine=__LINE__` markers in `ProgramLayoutRangeTests.cpp`. If a location is needed, a message per `Require` reads better; - the unconditional `std::cerr << "case3005 off=…"` line in `RequireExtraBlockStartsOnItsOwnPage`, which prints on every run where `FileSize > MemorySize` before the `Require` throws; - the missing newline at the end of the file.
oneandonlydean commented 2026-10-14 05:36:23 +00:00 (Migrated from github.com)

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 gLine global and markers (a message per Require reads better), the std::cerr << "case3005 ..." line, and add the missing final newline.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161696438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543#issuecomment-6161696438). Originally posted by [oneandonlydean](https://github.com/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 `gLine` global and markers (a message per `Require` reads better), the `std::cerr << "case3005 ..."` line, and add the missing final newline.
nikowolf123 commented 2026-10-14 17:12:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T17:12:55Z.


@mariuszkreft, here is the ProgramLayoutRangeTests.cpp cleanup from @CnRJay's review that @oneandonlydean asked for before merging. It is a patch on 58120ca41 that only touches the test file, so you can save it and run git apply:

  • Require(bool ok, const char* what) throws program layout: <what>, so each check names itself. The gLine global (L13-L14) and all its markers are gone.
  • The case3005 std::cerr line (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)
diff --git a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp
index 97633be66..458cd8bc2 100644
--- a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp
+++ b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp
@@ -4,14 +4,14 @@
 #include <iostream>
 #include <limits>
 #include <stdexcept>
+#include <string>
 
 class KeepAll : public Elfpatcher::ISegmentFilter {
 public:
     bool ShouldSkip(const Domain::ProgramHeader&) const override { return false; }
 };
 
-static int gLine=0;
-static void Require(bool ok) { if (!ok) throw std::runtime_error("Program layout range validation failed at line " + std::to_string(gLine)); }
+static void Require(bool ok, const char* what) { if (!ok) throw std::runtime_error(std::string("program layout: ") + what); }
 
 static Domain::ProgramHeader Load(std::uint64_t vaddr, std::uint64_t memorySize, std::uint64_t fileSize, std::uint32_t flags) {
     Domain::ProgramHeader load{};
@@ -39,29 +39,27 @@ static std::vector<Domain::ProgramHeader> MappedLoads(const std::vector<std::uin
 }
 
 static void RequireExtraBlockStartsOnItsOwnPage(std::vector<Domain::ProgramHeader> loads, std::uint64_t extraFileOffset) {
-    gLine = 3000;
     std::sort(loads.begin(), loads.end(), [](const Domain::ProgramHeader& left, const Domain::ProgramHeader& right) {
         return left.MappedAddress < right.MappedAddress;
     });
     std::size_t extraIndex = loads.size();
     for (std::size_t index = 0; index < loads.size(); ++index) {
-        if (loads[index].FileSize > loads[index].MemorySize) std::cerr << "case3005 off=" << std::hex << loads[index].Offset << " vaddr=" << loads[index].MappedAddress << " fsz=" << loads[index].FileSize << " msz=" << loads[index].MemorySize << "\n";
-        gLine = 3005; Require(loads[index].FileSize <= loads[index].MemorySize);
-        gLine = 3006; Require(loads[index].MappedAddress % loads[index].Alignment == loads[index].Offset % loads[index].Alignment);
+        Require(loads[index].FileSize <= loads[index].MemorySize, "load file size above its memory size");
+        Require(loads[index].MappedAddress % loads[index].Alignment == loads[index].Offset % loads[index].Alignment, "load vaddr and offset differ modulo alignment");
         if (loads[index].Offset == extraFileOffset)
             extraIndex = index;
     }
-    gLine = 3001; Require(extraIndex < loads.size());
+    Require(extraIndex < loads.size(), "no load at the extra block offset");
     const auto& extraBlock = loads[extraIndex];
-    gLine = 3002; Require(extraBlock.MemorySize >= extraBlock.FileSize);
+    Require(extraBlock.MemorySize >= extraBlock.FileSize, "extra block memory size below its file size");
     for (std::size_t index = 0; index < loads.size(); ++index) {
         if (index == extraIndex)
             continue;
         const auto& other = loads[index];
         if (other.MappedAddress < extraBlock.MappedAddress) {
-            gLine = 3003; Require((other.MappedAddress + other.FileSize - 1) / 0x1000 < extraBlock.MappedAddress / 0x1000);
+            Require((other.MappedAddress + other.FileSize - 1) / 0x1000 < extraBlock.MappedAddress / 0x1000, "load below the extra block ends on its page");
         } else {
-            gLine = 3004; Require(other.MappedAddress >= extraBlock.MappedAddress + extraBlock.FileSize);
+            Require(other.MappedAddress >= extraBlock.MappedAddress + extraBlock.FileSize, "load above the extra block overlaps it");
         }
     }
 }
@@ -75,17 +73,17 @@ int main() {
         load.Type = 1; load.MappedAddress = maximum - 3; load.MemorySize = 8;
         bool rejected = false;
         try { builder.ComputeExtraBlockVaddr({load}, 0x1000); } catch (const std::exception&) { rejected = true; }
-        gLine=__LINE__; Require(rejected);
+        Require(rejected, "overflowing load end accepted");
         load.MappedAddress = 0x1000; load.MemorySize = 0x1000;
-        gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x1003) == 0x2003);
+        Require(builder.ComputeExtraBlockVaddr({load}, 0x1003) == 0x2003, "vaddr for aligned end 0x2000, offset 0x1003");
         load.MemorySize = 0x1800;
-        gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x8A00) == 0x3A00);
-        gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x8800) == 0x3800);
+        Require(builder.ComputeExtraBlockVaddr({load}, 0x8A00) == 0x3A00, "vaddr for end 0x2800, offset 0x8A00");
+        Require(builder.ComputeExtraBlockVaddr({load}, 0x8800) == 0x3800, "vaddr for end 0x2800, offset 0x8800");
         load.MappedAddress = 0x1000; load.MemorySize = 0x1000;
-        gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x1000) == 0x2000);
+        Require(builder.ComputeExtraBlockVaddr({load}, 0x1000) == 0x2000, "vaddr for aligned end 0x2000, offset 0x1000");
         Elfpatcher::ProgramHeaderLayoutRequest valid{64, 56, 6, {load}, 0x1000, 0x2000, 0x100, 0x1000, 16, 0x1010, 8};
         std::vector<std::uint8_t> output(0x1100, 0xA5);
-        gLine=__LINE__; Require(builder.WriteLayout(output, valid) == 6);
+        Require(builder.WriteLayout(output, valid) == 6, "header count for a valid request");
         for (int kind = 0; kind < 11; ++kind) {
             auto request = valid;
             switch (kind) {
@@ -105,7 +103,7 @@ int main() {
             const auto before = bytes;
             rejected = false;
             try { builder.WriteLayout(bytes, request); } catch (const std::exception&) { rejected = true; }
-            gLine=__LINE__; Require(rejected && bytes == before);
+            Require(rejected && bytes == before, "invalid request accepted or output changed");
         }
         const std::uint16_t tableCount = 2 + 5;
         const auto writeLayout = [&builder, tableCount](const std::vector<Domain::ProgramHeader>& headers, std::uint64_t extraFileOffset, std::uint64_t extraFileSize) {
@@ -115,7 +113,7 @@ int main() {
                 builder.ComputeExtraBlockVaddr(headers, extraFileOffset), tableSize + extraFileSize,
                 extraFileOffset, 0x20, extraFileOffset + 0x20, 8};
             const std::uint16_t written = builder.WriteLayout(bytes, request);
-            gLine=__LINE__; Require(written == tableCount);
+            Require(written == tableCount, "written header count");
             RequireExtraBlockStartsOnItsOwnPage(MappedLoads(bytes, extraFileOffset, written), extraFileOffset);
         };
         writeLayout({Load(0x1000, 0x1800, 0x1800, 6), Load(0x3000, 0x1000, 0x1000, 4)}, 0x8A00, 0x400);
@@ -127,4 +125,4 @@ int main() {
         writeLayout({Load(0x1000, 0x1000, 0x1000, 5), Load(0x2000, 0x1000, 0x0400, 6)}, 0x8800, 0x400);
         std::cout << "21 program layout cases passed\n";
     } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }
-}
\ No newline at end of file
+}

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 on 58120ca41, and 165/165 on a merge with current main (e34c47921). With ProgramHeaderLayoutBuilder.hpp/.cpp put back to the PR's base 7a0c838ca, the test still fails, and the message changes from Program layout range validation failed at line 82 to program layout: vaddr for end 0x2800, offset 0x8A00. Main's tools/check_conventions.py also 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6173998206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543#issuecomment-6173998206). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-14T17:12:55Z. --- @mariuszkreft, here is the `ProgramLayoutRangeTests.cpp` cleanup from @CnRJay's [review](https://github.com/boykopovar/AnyPS5/pull/5543#pullrequestreview-5512139674) that @oneandonlydean [asked for](https://github.com/boykopovar/AnyPS5/pull/5543#issuecomment-6161696438) before merging. It is a patch on 58120ca41 that only touches the test file, so you can save it and run `git apply`: - `Require(bool ok, const char* what)` throws `program layout: <what>`, so each check names itself. The `gLine` global ([L13-L14](https://github.com/boykopovar/AnyPS5/blob/58120ca418c05b8500c77cab5b356fb426d5fb96/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp#L13-L14)) and all its markers are gone. - The `case3005` `std::cerr` line ([L48](https://github.com/boykopovar/AnyPS5/blob/58120ca418c05b8500c77cab5b356fb426d5fb96/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp#L48)) is deleted. - `#include <string>` is added for the message concatenation, and the file ends with a newline again. <details><summary>Patch for ProgramLayoutRangeTests.cpp (+17/-19)</summary> ```diff diff --git a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp index 97633be66..458cd8bc2 100644 --- a/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp +++ b/core/relinker/elfpatcher/tests/ProgramLayoutRangeTests.cpp @@ -4,14 +4,14 @@ #include <iostream> #include <limits> #include <stdexcept> +#include <string> class KeepAll : public Elfpatcher::ISegmentFilter { public: bool ShouldSkip(const Domain::ProgramHeader&) const override { return false; } }; -static int gLine=0; -static void Require(bool ok) { if (!ok) throw std::runtime_error("Program layout range validation failed at line " + std::to_string(gLine)); } +static void Require(bool ok, const char* what) { if (!ok) throw std::runtime_error(std::string("program layout: ") + what); } static Domain::ProgramHeader Load(std::uint64_t vaddr, std::uint64_t memorySize, std::uint64_t fileSize, std::uint32_t flags) { Domain::ProgramHeader load{}; @@ -39,29 +39,27 @@ static std::vector<Domain::ProgramHeader> MappedLoads(const std::vector<std::uin } static void RequireExtraBlockStartsOnItsOwnPage(std::vector<Domain::ProgramHeader> loads, std::uint64_t extraFileOffset) { - gLine = 3000; std::sort(loads.begin(), loads.end(), [](const Domain::ProgramHeader& left, const Domain::ProgramHeader& right) { return left.MappedAddress < right.MappedAddress; }); std::size_t extraIndex = loads.size(); for (std::size_t index = 0; index < loads.size(); ++index) { - if (loads[index].FileSize > loads[index].MemorySize) std::cerr << "case3005 off=" << std::hex << loads[index].Offset << " vaddr=" << loads[index].MappedAddress << " fsz=" << loads[index].FileSize << " msz=" << loads[index].MemorySize << "\n"; - gLine = 3005; Require(loads[index].FileSize <= loads[index].MemorySize); - gLine = 3006; Require(loads[index].MappedAddress % loads[index].Alignment == loads[index].Offset % loads[index].Alignment); + Require(loads[index].FileSize <= loads[index].MemorySize, "load file size above its memory size"); + Require(loads[index].MappedAddress % loads[index].Alignment == loads[index].Offset % loads[index].Alignment, "load vaddr and offset differ modulo alignment"); if (loads[index].Offset == extraFileOffset) extraIndex = index; } - gLine = 3001; Require(extraIndex < loads.size()); + Require(extraIndex < loads.size(), "no load at the extra block offset"); const auto& extraBlock = loads[extraIndex]; - gLine = 3002; Require(extraBlock.MemorySize >= extraBlock.FileSize); + Require(extraBlock.MemorySize >= extraBlock.FileSize, "extra block memory size below its file size"); for (std::size_t index = 0; index < loads.size(); ++index) { if (index == extraIndex) continue; const auto& other = loads[index]; if (other.MappedAddress < extraBlock.MappedAddress) { - gLine = 3003; Require((other.MappedAddress + other.FileSize - 1) / 0x1000 < extraBlock.MappedAddress / 0x1000); + Require((other.MappedAddress + other.FileSize - 1) / 0x1000 < extraBlock.MappedAddress / 0x1000, "load below the extra block ends on its page"); } else { - gLine = 3004; Require(other.MappedAddress >= extraBlock.MappedAddress + extraBlock.FileSize); + Require(other.MappedAddress >= extraBlock.MappedAddress + extraBlock.FileSize, "load above the extra block overlaps it"); } } } @@ -75,17 +73,17 @@ int main() { load.Type = 1; load.MappedAddress = maximum - 3; load.MemorySize = 8; bool rejected = false; try { builder.ComputeExtraBlockVaddr({load}, 0x1000); } catch (const std::exception&) { rejected = true; } - gLine=__LINE__; Require(rejected); + Require(rejected, "overflowing load end accepted"); load.MappedAddress = 0x1000; load.MemorySize = 0x1000; - gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x1003) == 0x2003); + Require(builder.ComputeExtraBlockVaddr({load}, 0x1003) == 0x2003, "vaddr for aligned end 0x2000, offset 0x1003"); load.MemorySize = 0x1800; - gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x8A00) == 0x3A00); - gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x8800) == 0x3800); + Require(builder.ComputeExtraBlockVaddr({load}, 0x8A00) == 0x3A00, "vaddr for end 0x2800, offset 0x8A00"); + Require(builder.ComputeExtraBlockVaddr({load}, 0x8800) == 0x3800, "vaddr for end 0x2800, offset 0x8800"); load.MappedAddress = 0x1000; load.MemorySize = 0x1000; - gLine=__LINE__; Require(builder.ComputeExtraBlockVaddr({load}, 0x1000) == 0x2000); + Require(builder.ComputeExtraBlockVaddr({load}, 0x1000) == 0x2000, "vaddr for aligned end 0x2000, offset 0x1000"); Elfpatcher::ProgramHeaderLayoutRequest valid{64, 56, 6, {load}, 0x1000, 0x2000, 0x100, 0x1000, 16, 0x1010, 8}; std::vector<std::uint8_t> output(0x1100, 0xA5); - gLine=__LINE__; Require(builder.WriteLayout(output, valid) == 6); + Require(builder.WriteLayout(output, valid) == 6, "header count for a valid request"); for (int kind = 0; kind < 11; ++kind) { auto request = valid; switch (kind) { @@ -105,7 +103,7 @@ int main() { const auto before = bytes; rejected = false; try { builder.WriteLayout(bytes, request); } catch (const std::exception&) { rejected = true; } - gLine=__LINE__; Require(rejected && bytes == before); + Require(rejected && bytes == before, "invalid request accepted or output changed"); } const std::uint16_t tableCount = 2 + 5; const auto writeLayout = [&builder, tableCount](const std::vector<Domain::ProgramHeader>& headers, std::uint64_t extraFileOffset, std::uint64_t extraFileSize) { @@ -115,7 +113,7 @@ int main() { builder.ComputeExtraBlockVaddr(headers, extraFileOffset), tableSize + extraFileSize, extraFileOffset, 0x20, extraFileOffset + 0x20, 8}; const std::uint16_t written = builder.WriteLayout(bytes, request); - gLine=__LINE__; Require(written == tableCount); + Require(written == tableCount, "written header count"); RequireExtraBlockStartsOnItsOwnPage(MappedLoads(bytes, extraFileOffset, written), extraFileOffset); }; writeLayout({Load(0x1000, 0x1800, 0x1800, 6), Load(0x3000, 0x1000, 0x1000, 4)}, 0x8A00, 0x400); @@ -127,4 +125,4 @@ int main() { writeLayout({Load(0x1000, 0x1000, 0x1000, 5), Load(0x2000, 0x1000, 0x0400, 6)}, 0x8800, 0x400); std::cout << "21 program layout cases passed\n"; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } -} \ No newline at end of file +} ``` </details> 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 on 58120ca41, and 165/165 on a merge with current main (e34c47921). With `ProgramHeaderLayoutBuilder.hpp`/`.cpp` put back to the PR's base 7a0c838ca, the test still fails, and the message changes from `Program layout range validation failed at line 82` to `program layout: vaddr for end 0x2800, offset 0x8A00`. Main's `tools/check_conventions.py` also 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.
mariuszkreft commented 2026-10-15 01:50:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-15T01:50:52Z.


Applied the cleanup @nikowolf123 prepared, unchanged, as 7f73643e0 — Require names each check, the gLine global and its markers are gone, the stray std::cerr line 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) on 7f73643e0:

  • ctest --test-dir build-relinker --output-on-failure -j 12 → 155/155 passed in 13.2 s
  • program_layout_ranges_tests → 21 program layout cases passed
  • python3 tools/check_conventions.py --base origin/main → All conventions checks passed
  • honesty check on the test itself: with ProgramHeaderLayoutBuilder.{cpp,hpp} put back to the PR base 7a0c838ca, the same binary fails with program 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6182549335 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543#issuecomment-6182549335). Originally posted by [mariuszkreft](https://github.com/mariuszkreft) on 2026-10-15T01:50:52Z. --- Applied the cleanup @nikowolf123 prepared, unchanged, as `7f73643e0` — `Require` names each check, the `gLine` global and its markers are gone, the stray `std::cerr` line 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) on `7f73643e0`: - `ctest --test-dir build-relinker --output-on-failure -j 12` → **155/155 passed** in 13.2 s - `program_layout_ranges_tests` → `21 program layout cases passed` - `python3 tools/check_conventions.py --base origin/main` → `All conventions checks passed` - honesty check on the test itself: with `ProgramHeaderLayoutBuilder.{cpp,hpp}` put back to the PR base `7a0c838ca`, the same binary fails with `program 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).
adriadam10 commented 2026-10-15 11:13:30 +00:00 (Migrated from github.com)

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_ranges and linux_program_header_order fail with the fix reverted and pass with it. The scaffolding cleanup from the earlier reviews is in; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190717392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5543#issuecomment-6190717392). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:13:30Z. --- Checked the merge onto main: build and relinker ctest 132/132 green; `program_layout_ranges` and `linux_program_header_order` fail with the fix reverted and pass with it. The scaffolding cleanup from the earlier reviews is in; merging.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5543
No description provided.