fix(relinker): accept empty TLS images beyond EOF on Windows #11000

Merged
plexerPK merged 1 commit from upstream-pr/11000 into main 2026-10-26 16:35:56 +00:00
plexerPK commented 2026-10-25 13:56:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by plexerPK on 2026-10-25T13:56:57Z.


What

Accept a zero-length PT_TLS initialization image whose unused file offset is beyond EOF, matching the ELF reader contract. Keep bounds validation for nonempty images.

Tested

macOS arm64, Python 3.9.6, Release relinker-only build. Head 4f7f3318fa63f48e518a1773b2b1d1b796322f06 on main e05ba6feebd000cfc316c1d1f00e37e030aa3d8b.

Original code fails the new EOF+1 fixture; fixed code passes six added cases (zero-sized and BSS TLS at EOF, EOF+1 and UINT64_MAX). CTest: empty_tls, elf_reader_bounds, windows_tls_zero_fill and windows_tls_thread_pointer, 4/4 passed. Conventions and diff-check pass. Synthetic PE conversion only; no native Windows execution, game, console, GPU or hosted-CI result is claimed.

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:5005373731 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11000). Originally posted by [plexerPK](https://github.com/plexerPK) on 2026-10-25T13:56:57Z. --- ### What Accept a zero-length PT_TLS initialization image whose unused file offset is beyond EOF, matching the ELF reader contract. Keep bounds validation for nonempty images. ### Tested macOS arm64, Python 3.9.6, Release relinker-only build. Head `4f7f3318fa63f48e518a1773b2b1d1b796322f06` on main `e05ba6feebd000cfc316c1d1f00e37e030aa3d8b`. Original code fails the new EOF+1 fixture; fixed code passes six added cases (zero-sized and BSS TLS at EOF, EOF+1 and UINT64_MAX). CTest: empty_tls, elf_reader_bounds, windows_tls_zero_fill and windows_tls_thread_pointer, 4/4 passed. Conventions and diff-check pass. Synthetic PE conversion only; no native Windows execution, game, console, GPU or hosted-CI result is claimed. <!-- 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. --> ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-25 14:13:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:13:16Z.

Review: APPROVED


Reviewed the zero-size ELF TLS bounds rule. When FileSize == 0, the file offset is unused and no source bytes are copied; bounds checks remain in force for nonempty images. The six new EOF/EOF+1/UINT64_MAX cases cover both zero and nonzero memory sizes, while existing invalid-size and TLS-access cases remain. I configured and completed a clean Linux Release relinker-only build, then ran empty_tls, elf_reader_bounds, windows_tls_zero_fill and windows_tls_thread_pointer: 4/4 passed. Hosted Linux, Windows, macOS arm64, macOS x86_64 and ASan relinker checks also pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643617863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11000#pullrequestreview-5643617863). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:13:16Z. **Review:** APPROVED --- Reviewed the zero-size ELF TLS bounds rule. When `FileSize == 0`, the file offset is unused and no source bytes are copied; bounds checks remain in force for nonempty images. The six new EOF/EOF+1/UINT64_MAX cases cover both zero and nonzero memory sizes, while existing invalid-size and TLS-access cases remain. I configured and completed a clean Linux Release relinker-only build, then ran `empty_tls`, `elf_reader_bounds`, `windows_tls_zero_fill` and `windows_tls_thread_pointer`: 4/4 passed. Hosted Linux, Windows, macOS arm64, macOS x86_64 and ASan relinker checks also pass.
roboterspinne commented 2026-10-25 15:04:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T15:04:43Z.

Review: APPROVED


Reviewed head 4f7f3318f merged onto upstream/main 3a2c51afd (2026-10-25); the merge is clean.

Environment: Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.

Results

  • Full relinker-only suite on the merged tree: 292 tests, 290 pass, long_paths skipped. The one failure is patch_advisor_unit. It fails the same way on plain main on this machine, because the temp path contains a space (#10667), and it is unrelated. empty_tls, windows_tls_zero_fill, windows_tls_protected_entries, windows_tls_thread_pointer and macos_empty_tls pass.
  • Native Windows run: on Windows, test_empty_tls.py starts every converted PE and expects exit code 42. All cases pass here, including the six new ones (offsets 0x1000, 0x1001 and UINT64_MAX, with p_memsz 0 and 16). That covers the native execution your description leaves out.
  • Counter-check: with main's WindowsTlsBuilder.cpp under this test, the run stops at empty-image-0-1001, where the relinker exits 2 with Invalid or unsupported ELF TLS layout.
  • With p_filesz 0, nothing reads through tls->Offset after the check. The template builder returns early for an empty image (WindowsTlsTemplateBuilder.cpp:7), and the offset otherwise only appears in exception text. The && keeps source.size() - tls->Offset from being evaluated for these inputs. ElfReader already skips the range check for an empty image (if (tls.FileSize == 0) continue;, ElfReader.cpp:181), so "matching the ELF reader contract" is accurate.
  • check_conventions.py --base upstream/main passes with this title and body.

Notes, nothing to change here

  • For a non-empty image, the builder's own offset check can't be reached from the pipeline, because ElfReader rejects an out-of-file PT_TLS first (PT_TLS initialization image is outside the file). That is unchanged from main.

Not checked: Linux and macOS locally.

I found nothing to change. For disclosure: my open #10947 touches Windows TLS relocations in WindowsPePatcher.cpp and its test, not WindowsTlsBuilder.cpp or test_empty_tls.py, and none of my other open PRs touches these files.

Written with AI assistance (Claude Code). I ran the build, the full suite with the native PE runs and the counter-check above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643786332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11000#pullrequestreview-5643786332). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T15:04:43Z. **Review:** APPROVED --- Reviewed head 4f7f3318f merged onto `upstream/main` 3a2c51afd (2026-10-25); the merge is clean. **Environment:** Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9, `-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, Release. **Results** - Full relinker-only suite on the merged tree: 292 tests, 290 pass, `long_paths` skipped. The one failure is `patch_advisor_unit`. It fails the same way on plain `main` on this machine, because the temp path contains a space (#10667), and it is unrelated. `empty_tls`, `windows_tls_zero_fill`, `windows_tls_protected_entries`, `windows_tls_thread_pointer` and `macos_empty_tls` pass. - Native Windows run: on Windows, `test_empty_tls.py` starts every converted PE and expects exit code 42. All cases pass here, including the six new ones (offsets 0x1000, 0x1001 and `UINT64_MAX`, with `p_memsz` 0 and 16). That covers the native execution your description leaves out. - Counter-check: with `main`'s `WindowsTlsBuilder.cpp` under this test, the run stops at `empty-image-0-1001`, where the relinker exits 2 with `Invalid or unsupported ELF TLS layout`. - With `p_filesz` 0, nothing reads through `tls->Offset` after the check. The template builder returns early for an empty image (`WindowsTlsTemplateBuilder.cpp:7`), and the offset otherwise only appears in exception text. The `&&` keeps `source.size() - tls->Offset` from being evaluated for these inputs. `ElfReader` already skips the range check for an empty image (`if (tls.FileSize == 0) continue;`, `ElfReader.cpp:181`), so "matching the ELF reader contract" is accurate. - `check_conventions.py --base upstream/main` passes with this title and body. **Notes, nothing to change here** - For a non-empty image, the builder's own offset check can't be reached from the pipeline, because `ElfReader` rejects an out-of-file `PT_TLS` first (`PT_TLS initialization image is outside the file`). That is unchanged from `main`. **Not checked:** Linux and macOS locally. I found nothing to change. For disclosure: my open #10947 touches Windows TLS relocations in `WindowsPePatcher.cpp` and its test, not `WindowsTlsBuilder.cpp` or `test_empty_tls.py`, and none of my other open PRs touches these files. Written with AI assistance (Claude Code). I ran the build, the full suite with the native PE runs and the counter-check above on my Windows 11 machine.
MarcZX10 commented 2026-10-25 15:06:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:06:32Z.

Review: APPROVED


Reviewed exact head 4f7f3318fa63f48e518a1773b2b1d1b796322f06. I configured and built the relinker-only Release target locally. test_empty_tls.py passes against the exact binary; focused CTest empty_tls, elf_reader_bounds, windows_tls_zero_fill, and windows_tls_thread_pointer passes 4/4. git diff --check upstream/main...HEAD and conventions pass. The guard now bypasses file-offset bounds only for zero-file-size TLS images; nonempty images retain the original bounds check. Hosted Ubuntu, Windows, macOS arm64, macOS x86_64 and ASan relinker jobs pass; product jobs are skipped by path policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643793000 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11000#pullrequestreview-5643793000). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:06:32Z. **Review:** APPROVED --- Reviewed exact head `4f7f3318fa63f48e518a1773b2b1d1b796322f06`. I configured and built the relinker-only Release target locally. `test_empty_tls.py` passes against the exact binary; focused CTest `empty_tls`, `elf_reader_bounds`, `windows_tls_zero_fill`, and `windows_tls_thread_pointer` passes 4/4. `git diff --check upstream/main...HEAD` and conventions pass. The guard now bypasses file-offset bounds only for zero-file-size TLS images; nonempty images retain the original bounds check. Hosted Ubuntu, Windows, macOS arm64, macOS x86_64 and ASan relinker jobs pass; product jobs are skipped by path policy.
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!11000
No description provided.