fix(relinker): reject non-TLS guest relocations bound to a guest TLS export #10732

Merged
roboterspinne merged 1 commit from upstream-pr/10732 into main 2026-10-25 16:59:00 +00:00
roboterspinne commented 2026-10-24 19:57:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T19:57:16Z.


What

A guest STT_NOTYPE import that resolves to another guest module's STT_TLS export was accepted when R_X86_64_GLOB_DAT, R_X86_64_64 or R_X86_64_JUMP_SLOT referenced it: Linux, Windows and macOS relinked with exit 0 and wrote both guest outputs. The Linux consumer kept GLOB_DAT against the undefined NOTYPE tls_value#guest, which glibc resolves to the provider's load base plus the TLS offset 0; Windows and macOS bind the slot to the provider's TLS export record (.tlsrefs in WindowsGuestModuleWriter.cpp, the TlsExports records in MacOsImage.cpp). None of these is the variable, while the same import typed STT_OBJECT or STT_FUNC already fails with Guest import/export type mismatch.

  • Rule. In the guest import loop of GuestModuleBuilder.cpp, right after the type-mismatch check, a NOTYPE import whose provider export is STT_TLS and that a relocation of the importing module references throws Non-TLS relocation against guest TLS export: <name>. The gABI lets only TLS relocations reference an STT_TLS symbol; the reader and the Windows and macOS guest writers already reject a non-TLS relocation against a TLS-typed import (Invalid guest symbol relocation), and the reader rejects TLS relocations against a NOTYPE symbol (Invalid guest TLS relocation) and RELATIVE against an undefined symbol (RELATIVE relocation has a symbol), so every relocation reaching the new check is R_X86_64_64, GLOB_DAT or JUMP_SLOT. Unreferenced NOTYPE entries bind nothing and stay accepted. Executable imports of guest exports have no type check at all; that is a separate follow-up.
  • Tests. New test_guest_tls_export_references.py, registered after guest_tls: provider a.prx with PT_TLS (memsz 0x40) and a global STT_TLS export, consumer b.prx with DT_NEEDED a.prx and an undefined tls_value. On Linux, Windows and macOS, NOTYPE with GLOB_DAT or R_X86_64_64 in DT_RELA and with JUMP_SLOT in DT_JMPREL exits 2 with the new message and writes no output; OBJECT with GLOB_DAT keeps the existing type-mismatch message; NOTYPE without a relocation, NOTYPE with GLOB_DAT against an STT_OBJECT export of a.prx, and STT_TLS with DTPMOD64 plus DTPOFF64 exit 0. On untouched code the three NOTYPE cases exit 0 on every target.
  • Overlap. pr_overlap.py cannot compare this branch with #5021 (core/relinker/CMakeLists.txt) and #8076 (GuestModuleBuilder.cpp), because those do not merge with main, and reports no other shared lines. Checked by hand: the CMakeLists.txt hunk of #5021 starts at line 505, this PR adds three lines after 618; the GuestModuleBuilder.cpp hunks of #8076 are at the includes and at lines 343 to 355, this PR adds one line after 291.

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, relinked outputs not run.

  • Counter-check on untouched main (50757b01c6): guest_tls_export_references fails with AssertionError: ('notype-glob-dat', 'linux', 0, ''); a probe of all 21 runs showed exit 0 with both guest outputs for the three NOTYPE cases on every target and exit 2 with Guest import/export type mismatch: tls_value for OBJECT.
  • With this change guest_tls_export_references passes in 1.0 s (21 relinks); guest_tls, guest_own_tls_relocations, guest_export_lookup and guest_symbol_names pass, and ctest -R 'guest|tls' passes 71 of 71.
  • ctest --test-dir build-relinker -j 4: 245 tests, 241 passed, long_paths skipped; select_tests (Windows symlink fixture, FileExistsError) and patch_advisor_unit (build path with spaces, fix open in #10667) fail on this machine on main as well; shader_cache_runtime_sources timed out at 180 s under parallel load and passed when rerun alone (108.1 s).
  • 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:4998646673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10732). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T19:57:16Z. --- ### What A guest `STT_NOTYPE` import that resolves to another guest module's `STT_TLS` export was accepted when `R_X86_64_GLOB_DAT`, `R_X86_64_64` or `R_X86_64_JUMP_SLOT` referenced it: Linux, Windows and macOS relinked with exit 0 and wrote both guest outputs. The Linux consumer kept `GLOB_DAT` against the undefined `NOTYPE` `tls_value#guest`, which glibc resolves to the provider's load base plus the TLS offset 0; Windows and macOS bind the slot to the provider's TLS export record (`.tlsrefs` in `WindowsGuestModuleWriter.cpp`, the `TlsExports` records in `MacOsImage.cpp`). None of these is the variable, while the same import typed `STT_OBJECT` or `STT_FUNC` already fails with `Guest import/export type mismatch`. - **Rule.** In the guest import loop of `GuestModuleBuilder.cpp`, right after the type-mismatch check, a `NOTYPE` import whose provider export is `STT_TLS` and that a relocation of the importing module references throws `Non-TLS relocation against guest TLS export: <name>`. The gABI lets only TLS relocations reference an `STT_TLS` symbol; the reader and the Windows and macOS guest writers already reject a non-TLS relocation against a TLS-typed import (`Invalid guest symbol relocation`), and the reader rejects TLS relocations against a `NOTYPE` symbol (`Invalid guest TLS relocation`) and `RELATIVE` against an undefined symbol (`RELATIVE relocation has a symbol`), so every relocation reaching the new check is `R_X86_64_64`, `GLOB_DAT` or `JUMP_SLOT`. Unreferenced `NOTYPE` entries bind nothing and stay accepted. Executable imports of guest exports have no type check at all; that is a separate follow-up. - **Tests.** New `test_guest_tls_export_references.py`, registered after `guest_tls`: provider `a.prx` with `PT_TLS` (memsz 0x40) and a global `STT_TLS` export, consumer `b.prx` with `DT_NEEDED a.prx` and an undefined `tls_value`. On Linux, Windows and macOS, `NOTYPE` with `GLOB_DAT` or `R_X86_64_64` in `DT_RELA` and with `JUMP_SLOT` in `DT_JMPREL` exits 2 with the new message and writes no output; `OBJECT` with `GLOB_DAT` keeps the existing type-mismatch message; `NOTYPE` without a relocation, `NOTYPE` with `GLOB_DAT` against an `STT_OBJECT` export of `a.prx`, and `STT_TLS` with `DTPMOD64` plus `DTPOFF64` exit 0. On untouched code the three `NOTYPE` cases exit 0 on every target. - **Overlap.** `pr_overlap.py` cannot compare this branch with #5021 (`core/relinker/CMakeLists.txt`) and #8076 (`GuestModuleBuilder.cpp`), because those do not merge with `main`, and reports no other shared lines. Checked by hand: the `CMakeLists.txt` hunk of #5021 starts at line 505, this PR adds three lines after 618; the `GuestModuleBuilder.cpp` hunks of #8076 are at the includes and at lines 343 to 355, this PR adds one line after 291. ### 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, relinked outputs not run. - Counter-check on untouched `main` (50757b01c6): `guest_tls_export_references` fails with `AssertionError: ('notype-glob-dat', 'linux', 0, '')`; a probe of all 21 runs showed exit 0 with both guest outputs for the three `NOTYPE` cases on every target and exit 2 with `Guest import/export type mismatch: tls_value` for `OBJECT`. - With this change `guest_tls_export_references` passes in 1.0 s (21 relinks); `guest_tls`, `guest_own_tls_relocations`, `guest_export_lookup` and `guest_symbol_names` pass, and `ctest -R 'guest|tls'` passes 71 of 71. - `ctest --test-dir build-relinker -j 4`: 245 tests, 241 passed, `long_paths` skipped; `select_tests` (Windows symlink fixture, `FileExistsError`) and `patch_advisor_unit` (build path with spaces, fix open in #10667) fail on this machine on `main` as well; `shader_cache_runtime_sources` timed out at 180 s under parallel load and passed when rerun alone (108.1 s). - `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-24 20:08:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:08:51Z.

Review: APPROVED


Reviewed head cd7b8bff00965f63c1e77a9082a062c795ed9970. The new guard is placed after the existing symbol type-mismatch check and rejects only referenced NOTYPE imports resolved to guest STT_TLS exports. The fixture exercises the three non-TLS relocation forms across Linux, Windows and macOS, verifies no output is produced for rejected inputs, and includes unaffected controls for unreferenced imports, object exports and valid TLS relocations. The author reports the focused Windows suite and 245-test relinker suite with known baseline failures; the official Linux, Windows, macOS, ASAN, conventions, intake and labels checks pass. I could not independently run a local build here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639769470 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10732#pullrequestreview-5639769470). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:08:51Z. **Review:** APPROVED --- Reviewed head `cd7b8bff00965f63c1e77a9082a062c795ed9970`. The new guard is placed after the existing symbol type-mismatch check and rejects only referenced `NOTYPE` imports resolved to guest `STT_TLS` exports. The fixture exercises the three non-TLS relocation forms across Linux, Windows and macOS, verifies no output is produced for rejected inputs, and includes unaffected controls for unreferenced imports, object exports and valid TLS relocations. The author reports the focused Windows suite and 245-test relinker suite with known baseline failures; the official Linux, Windows, macOS, ASAN, conventions, intake and labels checks pass. I could not independently run a local build here.
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!10732
No description provided.