fix(tools): preserve contained segment alignment when unwrapping SELF #10923

Merged
1whythef2 merged 2 commits from upstream-pr/10923 into main 2026-10-26 21:56:22 +00:00
1whythef2 commented 2026-10-25 10:23:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 1whythef2 on 2026-10-25T10:23:11Z.


What

SELF unwrapping can emit a contained segment with p_offset % p_align != p_vaddr % p_align when that segment requires stronger alignment than its containing output region. For example, a PT_LOAD at virtual address 0x4000 with alignment 0x100 is placed at file offset 0x400; its PT_TLS at 0x4040, requiring alignment 0x1000, is then placed at 0x440 instead of an offset congruent to 0x40.

Combine the alignment constraints of every header assigned to an output region before placing that region. Keep each header's declared alignment, payload and offset relative to its containing region. Refuse incompatible constraints before staging output, and count any additional padding against the existing limit. Sort the regions before constraint collection and keep that order through header emission, so both phases choose the same region for recorded segments inside overlapping loads. Inferred segments retain their explicit load host.

This follows the ELF program-header alignment rule. No title compatibility or performance improvement is claimed.

Tested

  • Windows, Python 3.12.14; synthetic SELF fixtures only. No C++ build, game execution, Linux or macOS execution.
  • The new stronger-alignment regression failed in all eight subcases before the implementation change.
  • python -m unittest discover -s tools/tests -p test_unwrap_self.py: 61 tests, 57 passed and 4 skipped for platform or symlink availability. The run used a writable temporary directory and allowed the existing hardlink test.
  • Added coverage for recorded and inferred segment locations, empty contained segments, multiple alignment constraints in either order, nonzero alignment residues, incompatible constraints preserving absent or existing output, and the padding allocation limit.
  • Added the two recorded-overlap fixtures supplied in @roboterspinne's review. Both fail on the previous PR head and pass after moving region sorting before constraint collection. Output alignment, each segment's bytes and the untouched input are checked.
  • Added the inferred-overlap host-selection fixture from the same review. It passes on the corrected implementation and fails when constraint collection is deliberately changed to use the first matching source range instead of the explicit load host.
  • python tools/check_conventions.py --base origin/main: passed.
  • git diff --check origin/main HEAD: passed.
  • python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch fix/unwrap-contained-segment-alignment: completed, with no confirmed conflicts. It listed nine uncompleted comparisons because those PRs conflict with main: #6612, #9508, #9588, #9881, #10113, #10226, #10324, #10487 and #10513. Manually checked both shared files in all nine PR heads against the checked main: their blob IDs are identical to main, so none contains this alignment change.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5003874124 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923). Originally posted by [1whythef2](https://github.com/1whythef2) on 2026-10-25T10:23:11Z. --- ### What SELF unwrapping can emit a contained segment with `p_offset % p_align != p_vaddr % p_align` when that segment requires stronger alignment than its containing output region. For example, a `PT_LOAD` at virtual address `0x4000` with alignment `0x100` is placed at file offset `0x400`; its `PT_TLS` at `0x4040`, requiring alignment `0x1000`, is then placed at `0x440` instead of an offset congruent to `0x40`. Combine the alignment constraints of every header assigned to an output region before placing that region. Keep each header's declared alignment, payload and offset relative to its containing region. Refuse incompatible constraints before staging output, and count any additional padding against the existing limit. Sort the regions before constraint collection and keep that order through header emission, so both phases choose the same region for recorded segments inside overlapping loads. Inferred segments retain their explicit load host. This follows the [ELF program-header alignment rule](https://gabi.xinuos.com/elf/07-pheader.html#program-header-entry). No title compatibility or performance improvement is claimed. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows, Python 3.12.14; synthetic SELF fixtures only. No C++ build, game execution, Linux or macOS execution. - The new stronger-alignment regression failed in all eight subcases before the implementation change. - `python -m unittest discover -s tools/tests -p test_unwrap_self.py`: 61 tests, 57 passed and 4 skipped for platform or symlink availability. The run used a writable temporary directory and allowed the existing hardlink test. - Added coverage for recorded and inferred segment locations, empty contained segments, multiple alignment constraints in either order, nonzero alignment residues, incompatible constraints preserving absent or existing output, and the padding allocation limit. - Added the two recorded-overlap fixtures supplied in @roboterspinne's review. Both fail on the previous PR head and pass after moving region sorting before constraint collection. Output alignment, each segment's bytes and the untouched input are checked. - Added the inferred-overlap host-selection fixture from the same review. It passes on the corrected implementation and fails when constraint collection is deliberately changed to use the first matching source range instead of the explicit load host. - `python tools/check_conventions.py --base origin/main`: passed. - `git diff --check origin/main HEAD`: passed. - `python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch fix/unwrap-contained-segment-alignment`: completed, with no confirmed conflicts. It listed nine uncompleted comparisons because those PRs conflict with `main`: #6612, #9508, #9588, #9881, #10113, #10226, #10324, #10487 and #10513. Manually checked both shared files in all nine PR heads against the checked `main`: their blob IDs are identical to `main`, so none contains this alignment change. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 10:29:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T10:29:50Z.

Review: COMMENTED


Reviewed exact head d5635e5728769f18a19166db249dd29864133ed4.

I ran python3 -m unittest discover -s tools/tests -p test_unwrap_self.py from a complete archive of that exact head: 59 tests passed, 1 platform-dependent test skipped. git diff --check upstream/main...upstream/pr-10923 and python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10923 also pass. Hosted conventions, intake, and labels checks pass; the product build checks are not present on this tools-only path.

The new placement rule uses one shared region resolver for constraint collection and final header emission, retains each segment's own p_align, and refuses incompatible congruences before creating the output. I additionally exercised a contained PT_TLS at offset/address residue 0x40, and a region containing PT_DYNAMIC/PT_TLS constraints with residues 0x40 and 0x80; both emitted headers satisfy p_offset % p_align == p_vaddr % p_align. The overlap checker could not compare #9826/#9827 because they conflict with main, but I inspected both heads: their changed files are exclusively under core/libs/...; neither changes tools/unwrap_self.py or its tests.

I found no blocking issue in this diff. These results cover the focused Python tool and synthetic fixtures only; I did not run an AnyPS5 product build or test a real SELF/title.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642747760 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5642747760). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T10:29:50Z. **Review:** COMMENTED --- Reviewed exact head `d5635e5728769f18a19166db249dd29864133ed4`. I ran `python3 -m unittest discover -s tools/tests -p test_unwrap_self.py` from a complete archive of that exact head: 59 tests passed, 1 platform-dependent test skipped. `git diff --check upstream/main...upstream/pr-10923` and `python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10923` also pass. Hosted conventions, intake, and labels checks pass; the product build checks are not present on this tools-only path. The new placement rule uses one shared region resolver for constraint collection and final header emission, retains each segment's own `p_align`, and refuses incompatible congruences before creating the output. I additionally exercised a contained `PT_TLS` at offset/address residue `0x40`, and a region containing `PT_DYNAMIC`/`PT_TLS` constraints with residues `0x40` and `0x80`; both emitted headers satisfy `p_offset % p_align == p_vaddr % p_align`. The overlap checker could not compare #9826/#9827 because they conflict with `main`, but I inspected both heads: their changed files are exclusively under `core/libs/...`; neither changes `tools/unwrap_self.py` or its tests. I found no blocking issue in this diff. These results cover the focused Python tool and synthetic fixtures only; I did not run an AnyPS5 product build or test a real SELF/title.
MarcZX10 commented 2026-10-25 10:29:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T10:29:58Z.

Review: COMMENTED


Reviewed exact head d5635e5728769f18a19166db249dd29864133ed4.

I ran python3 -m unittest discover -s tools/tests -p test_unwrap_self.py from a complete archive of that exact head: 59 tests passed, 1 platform-dependent test skipped. git diff --check upstream/main...upstream/pr-10923 and python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10923 also pass. Hosted conventions, intake, and labels checks pass; the product build checks are not present on this tools-only path.

The new placement rule uses one shared region resolver for constraint collection and final header emission, retains each segment's own p_align, and refuses incompatible congruences before creating the output. I additionally exercised a contained PT_TLS at offset/address residue 0x40, and a region containing PT_DYNAMIC/PT_TLS constraints with residues 0x40 and 0x80; both emitted headers satisfy p_offset % p_align == p_vaddr % p_align. The overlap checker could not compare #9826/#9827 because they conflict with main, but I inspected both heads: their changed files are exclusively under core/libs/...; neither changes tools/unwrap_self.py or its tests.

I found no blocking issue in this diff. These results cover the focused Python tool and synthetic fixtures only; I did not run an AnyPS5 product build or test a real SELF/title.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642748344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5642748344). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T10:29:58Z. **Review:** COMMENTED --- Reviewed exact head `d5635e5728769f18a19166db249dd29864133ed4`. I ran `python3 -m unittest discover -s tools/tests -p test_unwrap_self.py` from a complete archive of that exact head: 59 tests passed, 1 platform-dependent test skipped. `git diff --check upstream/main...upstream/pr-10923` and `python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10923` also pass. Hosted conventions, intake, and labels checks pass; the product build checks are not present on this tools-only path. The new placement rule uses one shared region resolver for constraint collection and final header emission, retains each segment's own `p_align`, and refuses incompatible congruences before creating the output. I additionally exercised a contained `PT_TLS` at offset/address residue `0x40`, and a region containing `PT_DYNAMIC`/`PT_TLS` constraints with residues `0x40` and `0x80`; both emitted headers satisfy `p_offset % p_align == p_vaddr % p_align`. The overlap checker could not compare #9826/#9827 because they conflict with `main`, but I inspected both heads: their changed files are exclusively under `core/libs/...`; neither changes `tools/unwrap_self.py` or its tests. I found no blocking issue in this diff. These results cover the focused Python tool and synthetic fixtures only; I did not run an AnyPS5 product build or test a real SELF/title.
MarcZX10 commented 2026-10-25 10:30:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T10:30:17Z.

Review: COMMENTED


No blocking findings after focused review; #10923 is an isolated tools-only change, so the PR should be mergeable independently.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642749396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5642749396). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T10:30:17Z. **Review:** COMMENTED --- No blocking findings after focused review; #10923 is an isolated tools-only change, so the PR should be mergeable independently.
roboterspinne commented 2026-10-25 11:32:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T11:32:23Z.

Review: COMMENTED


Reviewed head d5635e572 merged onto upstream/main 63a863d3b0, which is its base; the merge is clean.

Environment: Windows 11 Home 10.0.26200, Python 3.13.9, TEMP on a path without the 8.3 short name.

Results

  • python -m unittest discover -s tools/tests -p test_unwrap_self.py on the merged tree: 59 tests, OK, with 4 skipped. Three of those are POSIX-only, and the symlink test needs a privilege on Windows.
  • Counter-check: with main's unwrap_self.py under this test file, 13 subtests fail:
    • all 8 of test_contained_segments_keep_stronger_alignment_than_their_load, which encodes the 0x4000/0x4040 example from the description;
    • test_contained_alignment_obeys_the_padding_limit;
    • both subtests of the incompatible-alignment test;
    • both subtests of the multiple-alignment test.
  • Property check: I generated random containers with one or two loads and up to two contained PT_TLS/PT_DYNAMIC, recorded or inferred, with random power-of-two alignments. I kept only inputs whose headers are all congruent: 1,684 inputs over two seeds. This branch writes congruent output with the payload intact for all of them, while main misplaces 63. So the change does what it says for the cases these generators reach. They did not produce the two overlapping-load shapes from the inline comment.
  • Test gap: replacing output_region with region_for at line 285 still passes all 59 tests. This inferred input catches it:
    [(PT_LOAD, 5, 0x1000, 0x1000, 0x100, 0x100), (PT_LOAD, 6, 0x1080, 0x3080, 0x100, 0x100), (PT_TLS, 4, 0x1090, 0x3090, 0x10, 0x1000)]
    
    It uses no records and the payload at 0x1000. main writes the TLS at 0x590 (misaligned), and this branch writes it at 0x1090 (correct).
  • check_conventions.py --base upstream/main passes with this title and body. Since you are a first-time contributor, the hosted relinker jobs were skipped; this file's tests are plain Python and don't need them.

Not checked: whether real PS5 SELF files have overlapping recorded loads of either shape above; Linux and macOS; Python 3.12, the version you ran.

The approach is right and fixes the single-load case it targets. I would settle the region choice for overlapping loads before merge, since one shape now refuses a container main handled. For disclosure: I have open relinker PRs and #10667 on tools/tests/test_suggest_patches.py; none touches tools/unwrap_self.py or its tests.

Written with AI assistance (Claude Code). I ran the tests, the counter-check, the property check and the inputs above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642961048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5642961048). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T11:32:23Z. **Review:** COMMENTED --- Reviewed head d5635e572 merged onto `upstream/main` 63a863d3b0, which is its base; the merge is clean. **Environment:** Windows 11 Home 10.0.26200, Python 3.13.9, TEMP on a path without the 8.3 short name. **Results** - `python -m unittest discover -s tools/tests -p test_unwrap_self.py` on the merged tree: 59 tests, OK, with 4 skipped. Three of those are POSIX-only, and the symlink test needs a privilege on Windows. - Counter-check: with `main`'s `unwrap_self.py` under this test file, 13 subtests fail: - all 8 of `test_contained_segments_keep_stronger_alignment_than_their_load`, which encodes the 0x4000/0x4040 example from the description; - `test_contained_alignment_obeys_the_padding_limit`; - both subtests of the incompatible-alignment test; - both subtests of the multiple-alignment test. - Property check: I generated random containers with one or two loads and up to two contained `PT_TLS`/`PT_DYNAMIC`, recorded or inferred, with random power-of-two alignments. I kept only inputs whose headers are all congruent: 1,684 inputs over two seeds. This branch writes congruent output with the payload intact for all of them, while `main` misplaces 63. So the change does what it says for the cases these generators reach. They did not produce the two overlapping-load shapes from the inline comment. - Test gap: replacing `output_region` with `region_for` at line 285 still passes all 59 tests. This inferred input catches it: ```python [(PT_LOAD, 5, 0x1000, 0x1000, 0x100, 0x100), (PT_LOAD, 6, 0x1080, 0x3080, 0x100, 0x100), (PT_TLS, 4, 0x1090, 0x3090, 0x10, 0x1000)] ``` It uses no records and the payload at 0x1000. `main` writes the TLS at 0x590 (misaligned), and this branch writes it at 0x1090 (correct). - `check_conventions.py --base upstream/main` passes with this title and body. Since you are a first-time contributor, the hosted relinker jobs were skipped; this file's tests are plain Python and don't need them. **Not checked:** whether real PS5 SELF files have overlapping recorded loads of either shape above; Linux and macOS; Python 3.12, the version you ran. The approach is right and fixes the single-load case it targets. I would settle the region choice for overlapping loads before merge, since one shape now refuses a container `main` handled. For disclosure: I have open relinker PRs and #10667 on `tools/tests/test_suggest_patches.py`; none touches `tools/unwrap_self.py` or its tests. Written with AI assistance (Claude Code). I ran the tests, the counter-check, the property check and the inputs above on my Windows 11 machine.
THEMANnt commented 2026-10-25 12:11:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-25T12:11:00Z.

Review: COMMENTED


Reviewed head 2a45d721f62d99c660694c446fec7239daf4919e. No actionable finding in this focused SELF layout review. Upstream Build authorization is still pending, so this is a comment rather than an approval.

The new alignment pass uses the same output-region selection as the final header rewrite. It combines compatible power-of-two alignment constraints, rejects incompatible residues before publication, and preserves the existing output-padding limit. The added synthetic containers cover stronger TLS/dynamic alignment, empty contained segments, both header orders, and recorded/inferred overlapping source regions, while checking payload bytes and input preservation.

Validation: Windows/Python 3.11.9: tools/tests/test_unwrap_self.py -v runs 61 tests: 57 passed, 4 skipped. Baseline has 51 passed, 4 skipped. New tests against baseline production produce 15 expected assertion failures exposing alignment/layout defects. Conventions, whitespace and script registration pass. The latest Conventions rerun succeeds; Build 40190220485 remains action_required.

Next step: authorize and complete the upstream Build. Synthetic conversion tests do not establish commercial-title or console compatibility.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643147458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5643147458). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-25T12:11:00Z. **Review:** COMMENTED --- Reviewed head `2a45d721f62d99c660694c446fec7239daf4919e`. No actionable finding in this focused SELF layout review. Upstream Build authorization is still pending, so this is a comment rather than an approval. The new alignment pass uses the same output-region selection as the final header rewrite. It combines compatible power-of-two alignment constraints, rejects incompatible residues before publication, and preserves the existing output-padding limit. The added synthetic containers cover stronger TLS/dynamic alignment, empty contained segments, both header orders, and recorded/inferred overlapping source regions, while checking payload bytes and input preservation. **Validation:** Windows/Python 3.11.9: `tools/tests/test_unwrap_self.py -v` runs 61 tests: 57 passed, 4 skipped. Baseline has 51 passed, 4 skipped. New tests against baseline production produce 15 expected assertion failures exposing alignment/layout defects. Conventions, whitespace and script registration pass. The latest Conventions rerun succeeds; [Build 40190220485](https://github.com/boykopovar/AnyPS5/actions/runs/40190220485) remains `action_required`. **Next step:** authorize and complete the upstream Build. Synthetic conversion tests do not establish commercial-title or console compatibility.
roboterspinne commented 2026-10-25 12:56:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T12:56:14Z.

Review: APPROVED


@1whythef2 Thanks, checked 2a45d721f merged onto upstream/main a5a780eaa (2026-10-25); the merge is clean. Windows 11, Python 3.13.9.

  • test_unwrap_self.py: 61 tests, OK, with the same 4 skipped as before.

  • Counter-check against the new tests:

    • The previous head's unwrap_self.py fails both subtests of test_recorded_overlaps_use_the_same_region_for_constraints_and_output.
    • On main, 15 subtests fail: the 13 from my review, the second recorded-overlap input and test_inferred_overlaps_apply_stronger_alignment_to_the_host. The first recorded-overlap input passes there, since main converted it correctly.
  • Mutants against the new tests:

    • Dropping the early regions.sort fails the recorded-overlap test.
    • Using region_for in the constraint loop fails both new tests.

    So the sort position and the host choice are both pinned now.

  • The random congruent containers from my review, 1,684 over two seeds, still all come out congruent with their payload intact.

  • check_conventions.py --base upstream/main passes with the updated description, and git diff --check is clean.

That settles the point from my review; I found nothing else to change.

Written with AI assistance (Claude Code). I ran the checks above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643282953 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10923#pullrequestreview-5643282953). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T12:56:14Z. **Review:** APPROVED --- @1whythef2 Thanks, checked 2a45d721f merged onto `upstream/main` a5a780eaa (2026-10-25); the merge is clean. Windows 11, Python 3.13.9. - `test_unwrap_self.py`: 61 tests, OK, with the same 4 skipped as before. - Counter-check against the new tests: - The previous head's `unwrap_self.py` fails both subtests of `test_recorded_overlaps_use_the_same_region_for_constraints_and_output`. - On `main`, 15 subtests fail: the 13 from my review, the second recorded-overlap input and `test_inferred_overlaps_apply_stronger_alignment_to_the_host`. The first recorded-overlap input passes there, since `main` converted it correctly. - Mutants against the new tests: - Dropping the early `regions.sort` fails the recorded-overlap test. - Using `region_for` in the constraint loop fails both new tests. So the sort position and the host choice are both pinned now. - The random congruent containers from my review, 1,684 over two seeds, still all come out congruent with their payload intact. - `check_conventions.py --base upstream/main` passes with the updated description, and `git diff --check` is clean. That settles the point from my review; I found nothing else to change. Written with AI assistance (Claude Code). I ran the checks above on my Windows 11 machine.
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!10923
No description provided.