fix(relinker): accept a zero-size TLS export at the end of the macOS TLS block #10946

Merged
roboterspinne merged 1 commit from upstream-pr/10946 into main 2026-10-27 06:59:08 +00:00
roboterspinne commented 2026-10-25 11:18:02 +00:00 (Migrated from github.com)

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


What

WriteMacOsImage refused a TLS export whose st_value equals PT_TLS p_memsz: relinker --macos exited 2 with FAIL: Guest TLS export is outside the TLS block: _shared#guest for a guest module in sce_module/ (PT_TLS memsz 0x40, STT_TLS export at 0x40, size 0) and with FAIL: Guest TLS export is outside the TLS block: _FixtureTls for an executable relinked with --skip-sce-module (PT_TLS memsz 16, export at 16, size 0), while Linux (for the executable also with --lazy-binding) and --windows exited 0 on both inputs. Both inputs pass the earlier bounds, which allow value <= p_memsz and size <= p_memsz - value: GuestImageReader.cpp:192 (TLS symbol exceeds TLS segment) for guest modules, and for executables the pipeline's Executable TLS export exceeds PT_TLS (#10646) and MacOsMachOPatcher.cpp:91 (macOS executable TLS export exceeds PT_TLS). These two callers are the only code that fills TlsExports, and ScanTls rejects a second PT_TLS, so the deleted check was reached only for value == p_memsz, including value 0 in an empty PT_TLS that code accesses (0 >= 0).

  • Rule. The variable.Offset >= tls.Tls->MemorySize check in MacOsImage.cpp is deleted; the bound of GuestImageReader and MacOsMachOPatcher applies unchanged, and the message Guest TLS export is outside the TLS block no longer exists. gABI allows st_size 0, and an STT_TLS st_value is an offset into the TLS template, so offset p_memsz is the end of the block, as on Linux and Windows. The export's __APS5DATA entry now holds (TLS descriptor, p_memsz). Guest TLS export has no TLS block (no PT_TLS, or an empty one without TLS accesses) is unchanged. Changing >= to > was not done, since that check could never fire.
  • Tests. New test_macos_tls_exports.py, --macos only: guest module exports at 0x3f/size 1 and 0x40/size 0, executable exports at 15/0 and 16/0, and an executable with an empty PT_TLS (memsz 0, align 1, mov rax, fs:[0] at the entry) and an export at 0/0. Each must exit 0, and the export's __APS5DATA entry must hold (__APS5DATA vmaddr + 0x20, value). A control guest export at 0x40/size 1 must exit 2 with TLS symbol exceeds TLS segment: shared and write no module. On untouched code the test fails at guest-end with exit 2 and Guest TLS export is outside the TLS block: _shared#guest. The test imports convert, executable and exports from test_macos_executable_exports.py; #10646 changed only that file's main().
  • Overlap. pr_overlap.py reports no shared lines with open pull requests that merge with main. It cannot compare this branch with #9408 and #10249 (MacOsImage.cpp) and with #5021, #6713 and #9408 (core/relinker/CMakeLists.txt), because those do not merge with main. Checked by hand: #9408 changes MacOsImage.cpp at line 494 (the tlsExports slot, moved only with --trace-imports) and #10249 at line 595, not the deleted line 516; their CMakeLists.txt hunks sit next to macos_output, elf_header and linux_trace_imports, not next to macos_tls_overlap, after which this PR registers macos_tls_exports. The test also imports MachO from test_macos_output.py (#10712 adds lines to check_options only) and uses fixture() through executable() and main_fixture() (#10402 changes its e_entry, which both overwrite).

Tested

Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew, macOS outputs inspected as Mach-O files, not run.

  • Counter-check: macos_tls_exports on untouched c7d6660ef8 fails with AssertionError: ('guest-end', 2, 'FAIL: Guest TLS export is outside the TLS block: _shared#guest ...'); with the change it passes in 0.8 s. On the earlier base acbfd7c7fe a scratch driver ran all six inputs on both builds: on untouched code the guest 0x40/0, executable 16/0 and empty-PT_TLS 0/0 inputs exit 2 with --macos (... _shared#guest, ... _FixtureTls, ... _FixtureTls) and 0 on Linux, --lazy-binding (executables) and --windows; with the change all three exit 0 on every target run, with macOS entries (0x140012020, 0x40), (0x140011020, 16) and (0x140011020, 0). The 0x3f/1 and 15/0 inputs convert on both builds with the same entries, and 0x40/1 fails on every target on both builds.
  • ctest -j 4 -R '^(macos_|guest_tls$|guest_module_dir|empty_tls$|executable_tls_relocations$|linux_executable_exports$|tls_function_coverage_)': 48 tests passed in 30.0 s, among them macos_tls_exports (0.4 s), macos_executable_exports (0.4 s), macos_empty_tls (0.3 s), macos_tls_overlap (0.2 s), macos_tls_alu (0.3 s), macos_output (0.6 s), macos_gs_traps (0.9 s), guest_tls (1.7 s), guest_module_directories (2.0 s), executable_tls_relocations (1.5 s) and the 33 tls_function_coverage_* tests.
  • Full suite: ctest --test-dir build-relinker -j 3 on c7d6660ef8: 262 tests, 258 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture), patch_advisor_unit (path with spaces); shader_cache_runtime_sources timed out at 180 s under load and passed alone in 108.8 s
  • Rebased onto 16ff208dc3 (#10646 merged) and rebuilt: the 13 tests matching macos|executable_export_bounds|guest_tls|empty_tls|linux_executable_exports passed again, including macos_tls_exports and #10646's executable_export_bounds.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004227406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T11:18:02Z. --- ### What `WriteMacOsImage` refused a TLS export whose `st_value` equals PT_TLS `p_memsz`: `relinker --macos` exited 2 with `FAIL: Guest TLS export is outside the TLS block: _shared#guest` for a guest module in `sce_module/` (PT_TLS memsz 0x40, STT_TLS export at 0x40, size 0) and with `FAIL: Guest TLS export is outside the TLS block: _FixtureTls` for an executable relinked with `--skip-sce-module` (PT_TLS memsz 16, export at 16, size 0), while Linux (for the executable also with `--lazy-binding`) and `--windows` exited 0 on both inputs. Both inputs pass the earlier bounds, which allow `value <= p_memsz` and `size <= p_memsz - value`: `GuestImageReader.cpp:192` (`TLS symbol exceeds TLS segment`) for guest modules, and for executables the pipeline's `Executable TLS export exceeds PT_TLS` (#10646) and `MacOsMachOPatcher.cpp:91` (`macOS executable TLS export exceeds PT_TLS`). These two callers are the only code that fills `TlsExports`, and `ScanTls` rejects a second PT_TLS, so the deleted check was reached only for `value == p_memsz`, including value 0 in an empty PT_TLS that code accesses (`0 >= 0`). - **Rule.** The `variable.Offset >= tls.Tls->MemorySize` check in `MacOsImage.cpp` is deleted; the bound of `GuestImageReader` and `MacOsMachOPatcher` applies unchanged, and the message `Guest TLS export is outside the TLS block` no longer exists. gABI allows `st_size` 0, and an STT_TLS `st_value` is an offset into the TLS template, so offset `p_memsz` is the end of the block, as on Linux and Windows. The export's `__APS5DATA` entry now holds (TLS descriptor, `p_memsz`). `Guest TLS export has no TLS block` (no PT_TLS, or an empty one without TLS accesses) is unchanged. Changing `>=` to `>` was not done, since that check could never fire. - **Tests.** New `test_macos_tls_exports.py`, `--macos` only: guest module exports at 0x3f/size 1 and 0x40/size 0, executable exports at 15/0 and 16/0, and an executable with an empty PT_TLS (memsz 0, align 1, `mov rax, fs:[0]` at the entry) and an export at 0/0. Each must exit 0, and the export's `__APS5DATA` entry must hold (`__APS5DATA` vmaddr + 0x20, value). A control guest export at 0x40/size 1 must exit 2 with `TLS symbol exceeds TLS segment: shared` and write no module. On untouched code the test fails at `guest-end` with exit 2 and `Guest TLS export is outside the TLS block: _shared#guest`. The test imports `convert`, `executable` and `exports` from `test_macos_executable_exports.py`; #10646 changed only that file's `main()`. - **Overlap.** `pr_overlap.py` reports no shared lines with open pull requests that merge with `main`. It cannot compare this branch with #9408 and #10249 (`MacOsImage.cpp`) and with #5021, #6713 and #9408 (`core/relinker/CMakeLists.txt`), because those do not merge with `main`. Checked by hand: #9408 changes `MacOsImage.cpp` at line 494 (the `tlsExports` slot, moved only with `--trace-imports`) and #10249 at line 595, not the deleted line 516; their `CMakeLists.txt` hunks sit next to `macos_output`, `elf_header` and `linux_trace_imports`, not next to `macos_tls_overlap`, after which this PR registers `macos_tls_exports`. The test also imports `MachO` from `test_macos_output.py` (#10712 adds lines to `check_options` only) and uses `fixture()` through `executable()` and `main_fixture()` (#10402 changes its `e_entry`, which both overwrite). ### 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 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew, macOS outputs inspected as Mach-O files, not run. - Counter-check: `macos_tls_exports` on untouched c7d6660ef8 fails with `AssertionError: ('guest-end', 2, 'FAIL: Guest TLS export is outside the TLS block: _shared#guest ...')`; with the change it passes in 0.8 s. On the earlier base acbfd7c7fe a scratch driver ran all six inputs on both builds: on untouched code the guest 0x40/0, executable 16/0 and empty-PT_TLS 0/0 inputs exit 2 with `--macos` (`... _shared#guest`, `... _FixtureTls`, `... _FixtureTls`) and 0 on Linux, `--lazy-binding` (executables) and `--windows`; with the change all three exit 0 on every target run, with macOS entries (0x140012020, 0x40), (0x140011020, 16) and (0x140011020, 0). The 0x3f/1 and 15/0 inputs convert on both builds with the same entries, and 0x40/1 fails on every target on both builds. - `ctest -j 4 -R '^(macos_|guest_tls$|guest_module_dir|empty_tls$|executable_tls_relocations$|linux_executable_exports$|tls_function_coverage_)'`: 48 tests passed in 30.0 s, among them `macos_tls_exports` (0.4 s), `macos_executable_exports` (0.4 s), `macos_empty_tls` (0.3 s), `macos_tls_overlap` (0.2 s), `macos_tls_alu` (0.3 s), `macos_output` (0.6 s), `macos_gs_traps` (0.9 s), `guest_tls` (1.7 s), `guest_module_directories` (2.0 s), `executable_tls_relocations` (1.5 s) and the 33 `tls_function_coverage_*` tests. - Full suite: `ctest --test-dir build-relinker -j 3` on c7d6660ef8: 262 tests, 258 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture), `patch_advisor_unit` (path with spaces); `shader_cache_runtime_sources` timed out at 180 s under load and passed alone in 108.8 s - Rebased onto `16ff208dc3` (#10646 merged) and rebuilt: the 13 tests matching `macos|executable_export_bounds|guest_tls|empty_tls|linux_executable_exports` passed again, including `macos_tls_exports` and #10646's `executable_export_bounds`. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - Not tested on Linux or macOS; CI runs the tests on all three hosts. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-10-25 11:18:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:18:55Z.

Review: CHANGES_REQUESTED


I found one blocking issue in the new boundary test. The fixture declares the TLS symbol at offset 0x40 with size 0, equal to the TLS memory size, but still rejects via only? The guest-side check actually permits equality (), so the observed failure from the local base executable is expected and does not test the changed macOS writer. I am not requesting a product change on that basis. However, the hosted Conventions job fails because the PR body has no nonempty section (). Please fill in the required template and rerun it. I have not approved pending that required description correction.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642894389 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642894389). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:18:55Z. **Review:** CHANGES_REQUESTED --- I found one blocking issue in the new boundary test. The fixture declares the TLS symbol at offset 0x40 with size 0, equal to the TLS memory size, but still rejects via only? The guest-side check actually permits equality (), so the observed failure from the local base executable is expected and does not test the changed macOS writer. I am not requesting a product change on that basis. However, the hosted Conventions job fails because the PR body has no nonempty section (). Please fill in the required template and rerun it. I have not approved pending that required description correction.
MarcZX10 commented 2026-10-25 11:18:58 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


I found one validation issue in the new boundary test. The guest-end case exercises the guest reader's acceptance of a zero-size symbol at the TLS block end; that reader's bound check already permits value == MemorySize, so the local base executable's rejection is expected and does not exercise this PR's macOS writer change. I am not requesting a product change on that basis.

The hosted Conventions job does fail because the PR body has no nonempty What section (40188284382). Please fill in the required template and rerun it. I have not approved pending that required description correction.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642894558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642894558). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:18:58Z. **Review:** CHANGES_REQUESTED --- I found one validation issue in the new boundary test. The `guest-end` case exercises the guest reader's acceptance of a zero-size symbol at the TLS block end; that reader's bound check already permits `value == MemorySize`, so the local base executable's rejection is expected and does not exercise this PR's macOS writer change. I am not requesting a product change on that basis. The hosted Conventions job does fail because the PR body has no nonempty `What` section (`40188284382`). Please fill in the required template and rerun it. I have not approved pending that required description correction.
MarcZX10 commented 2026-10-25 11:19:29 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Thanks for filling the required What section. The Conventions run attached to this head still reports failure from the earlier template check; please rerun it after the edit so the updated body is validated. The relinker matrix is still running, so I will wait for those results before reviewing the product change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642895831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642895831). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:19:29Z. **Review:** COMMENTED --- Thanks for filling the required What section. The Conventions run attached to this head still reports failure from the earlier template check; please rerun it after the edit so the updated body is validated. The relinker matrix is still running, so I will wait for those results before reviewing the product change.
MarcZX10 commented 2026-10-25 11:21:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:21:28Z.

Review: COMMENTED


I found one validation issue in the new boundary test. The guest-end case exercises the guest reader's acceptance of a zero-size symbol at the TLS block end; that reader's bound check already permits value == MemorySize, so the local base executable's rejection is expected and does not exercise this PR's macOS writer change. I am not requesting a product change on that basis.

The hosted Conventions job does fail because the PR body has no nonempty What section (40188284382). Please fill in the required template and rerun it. I have not approved pending that required description correction.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642901582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642901582). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:21:28Z. **Review:** COMMENTED --- I found one validation issue in the new boundary test. The `guest-end` case exercises the guest reader's acceptance of a zero-size symbol at the TLS block end; that reader's bound check already permits `value == MemorySize`, so the local base executable's rejection is expected and does not exercise this PR's macOS writer change. I am not requesting a product change on that basis. The hosted Conventions job does fail because the PR body has no nonempty `What` section (`40188284382`). Please fill in the required template and rerun it. I have not approved pending that required description correction.
MarcZX10 commented 2026-10-25 11:25:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:25:34Z.

Review: APPROVED


Reviewed exact head 81f6b28. The writer no longer rejects an export offset equal to TLS memory size, while the guest reader retains its / bounds check, so a zero-size symbol at the block end is accepted and a positive-size overrun remains rejected. The new fixture covers end offsets for guest modules and executables, empty TLS, and an out-of-range positive-size symbol. Local conventions and diff checks pass. The available local relinker binary is built from an older base and cannot validate the changed writer, so I did not count its expected failure as PR evidence. Hosted Linux, Windows, macOS x86_64 and ASan relinker jobs passed; macOS arm64 was still queued when checked. The PR body’s updated Conventions run passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642923677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642923677). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:25:34Z. **Review:** APPROVED --- Reviewed exact head 81f6b28. The writer no longer rejects an export offset equal to TLS memory size, while the guest reader retains its / bounds check, so a zero-size symbol at the block end is accepted and a positive-size overrun remains rejected. The new fixture covers end offsets for guest modules and executables, empty TLS, and an out-of-range positive-size symbol. Local conventions and diff checks pass. The available local relinker binary is built from an older base and cannot validate the changed writer, so I did not count its expected failure as PR evidence. Hosted Linux, Windows, macOS x86_64 and ASan relinker jobs passed; macOS arm64 was still queued when checked. The PR body’s updated Conventions run passes.
MarcZX10 commented 2026-10-25 11:25:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:25:38Z.

Review: APPROVED


Reviewed exact head 81f6b28. The writer no longer rejects an export offset equal to TLS memory size, while the guest reader retains its value > MemorySize / size > MemorySize - value bounds check, so a zero-size symbol at the block end is accepted and a positive-size overrun remains rejected. The new fixture covers end offsets for guest modules and executables, empty TLS, and an out-of-range positive-size symbol. Local conventions and diff checks pass. The available local relinker binary is built from an older base and cannot validate the changed writer, so I did not count its expected guest-end failure as PR evidence. Hosted Linux, Windows, macOS x86_64 and ASan relinker jobs passed; macOS arm64 was still queued when checked. The PR body’s updated Conventions run passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642924124 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10946#pullrequestreview-5642924124). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:25:38Z. **Review:** APPROVED --- Reviewed exact head 81f6b28. The writer no longer rejects an export offset equal to TLS memory size, while the guest reader retains its `value > MemorySize` / `size > MemorySize - value` bounds check, so a zero-size symbol at the block end is accepted and a positive-size overrun remains rejected. The new fixture covers end offsets for guest modules and executables, empty TLS, and an out-of-range positive-size symbol. Local conventions and diff checks pass. The available local relinker binary is built from an older base and cannot validate the changed writer, so I did not count its expected `guest-end` failure as PR evidence. Hosted Linux, Windows, macOS x86_64 and ASan relinker jobs passed; macOS arm64 was still queued when checked. The PR body’s updated Conventions run passes.
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!10946
No description provided.