fix(relinker): convert stack-guard and thread-pointer reads with an empty PT_TLS #1911

Merged
TheAggie merged 1 commit from upstream-pr/1911 into main 2026-10-10 04:42:20 +00:00
TheAggie commented 2026-10-08 23:23:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-08T23:23:41Z.


What

A title built with -fstack-protector reads its stack guard from fs:0x28 even when it has no thread-local data and its linker emits an empty PT_TLS. The Windows TLS builder refused any guest TLS access in that case: Guest TLS access with empty PT_TLS. ccleste (PPSA59322) failed to relink because of it.

The guard (fs:0x28) and the thread pointer (fs:0) live in the thread control block, which the builder already emits. An empty PT_TLS with accesses now takes the normal path, where a zero-sized TLS block still rejects every displacement into thread-local data.

Test (empty_tls, core/relinker/relinker/tests/test_empty_tls.py): an empty PT_TLS with a stack-guard read and one with a thread-pointer read now relink. A read at fs:-8, which would be thread-local data, still fails with is not in the thread TLS block. The previous empty-access case, which expected the old error, is replaced by these.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • The full suite (489 tests) passes on current main with this PR and four other small PRs of mine merged together (SCHED_OTHER priority range, MAP_ALIGNED_SUPER, the compatible pad vibration mode, empty-PT_TLS stack-guard reads, pthread_attr scope/getters). It was run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine.
  • tools/check_conventions.py --base origin/main passes on this branch alone.

Not tested on Linux or macOS.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793973355 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1911). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-08T23:23:41Z. --- ### What A title built with `-fstack-protector` reads its stack guard from `fs:0x28` even when it has no thread-local data and its linker emits an empty `PT_TLS`. The Windows TLS builder refused any guest TLS access in that case: `Guest TLS access with empty PT_TLS`. ccleste (PPSA59322) failed to relink because of it. The guard (`fs:0x28`) and the thread pointer (`fs:0`) live in the thread control block, which the builder already emits. An empty `PT_TLS` with accesses now takes the normal path, where a zero-sized TLS block still rejects every displacement into thread-local data. Test (`empty_tls`, `core/relinker/relinker/tests/test_empty_tls.py`): an empty `PT_TLS` with a stack-guard read and one with a thread-pointer read now relink. A read at `fs:-8`, which would be thread-local data, still fails with `is not in the thread TLS block`. The previous `empty-access` case, which expected the old error, is replaced by these. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - The full suite (489 tests) passes on current `main` with this PR and four other small PRs of mine merged together (SCHED_OTHER priority range, MAP_ALIGNED_SUPER, the compatible pad vibration mode, empty-PT_TLS stack-guard reads, pthread_attr scope/getters). It was run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. - `tools/check_conventions.py --base origin/main` passes on this branch alone. Not tested on Linux or macOS. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
klosixgaming-arch commented 2026-10-09 00:40:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-09T00:40:55Z.

Review: APPROVED


Reasonable behaviour change: instead of hard-failing a Windows relink when guest code reads the thread pointer / stack guard (fs:[0] and fs:[0x28]) against an empty PT_TLS placeholder, the builder now emits a TLS directory to back those reads, while still rejecting reads outside the TLS block (fs:[-8] → "not in the thread TLS block"). The reworked test_empty_tls.py pins all three cases (guard read, thread-pointer read, out-of-block read) plus the no-access and non-empty baselines, and actually executes the produced .exe asserting the ret 42 path — a behavioural test, not just "TLS directory present".

Two things worth tightening before merge:

  1. The execution assertion only runs if os.name == "nt", so on the Linux CI runners the new fs:[0]/fs:[0x28] paths are never actually executed — the test there only checks the PE carries a non-zero TLS directory. If the goal is to guard the new lowering, either make the run assertion host-independent or note why Windows-only is sufficient (this is WindowsTlsBuilder, so likely fine — just make that explicit).
  2. The old explicit throw ... "Guest TLS access with empty PT_TLS" is removed. That's the intended relaxation per the title; just confirm no other call site or test depended on that exact message.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5464488784 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1911#pullrequestreview-5464488784). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-09T00:40:55Z. **Review:** APPROVED --- Reasonable behaviour change: instead of hard-failing a Windows relink when guest code reads the thread pointer / stack guard (fs:[0] and fs:[0x28]) against an empty PT_TLS placeholder, the builder now emits a TLS directory to back those reads, while still rejecting reads outside the TLS block (fs:[-8] → "not in the thread TLS block"). The reworked `test_empty_tls.py` pins all three cases (guard read, thread-pointer read, out-of-block read) plus the no-access and non-empty baselines, and actually executes the produced `.exe` asserting the `ret 42` path — a behavioural test, not just "TLS directory present". Two things worth tightening before merge: 1. The execution assertion only runs `if os.name == "nt"`, so on the Linux CI runners the new fs:[0]/fs:[0x28] paths are never actually executed — the test there only checks the PE carries a non-zero TLS directory. If the goal is to guard the new lowering, either make the run assertion host-independent or note why Windows-only is sufficient (this is `WindowsTlsBuilder`, so likely fine — just make that explicit). 2. The old explicit `throw ... "Guest TLS access with empty PT_TLS"` is removed. That's the intended relaxation per the title; just confirm no other call site or test depended on that exact message. Written with AI assistance.
Dextroid17 commented 2026-10-09 03:06:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T03:06:31Z.

Review: COMMENTED


Verified on Linux — both arms, and the failure is a named assertion rather than a silent abort.

  • Arm A (this PR): cmake --build build --target relinker → clean; ctest --test-dir build -R '^empty_tls$' → Passed (0.07 s).
  • Arm B (control): WindowsTlsBuilder.cpp back at main with the test kept, rebuilt the same way → empty_tls Failed, AssertionError at test_empty_tls.py:41 with the relink output ending in the empty-PT_TLS refusal. So the test pins this: it is the builder's refusal the change removes, and the test would catch a regression.

Claim cross-check: after the change the file still carries the refusal for a displacement into thread-local data with a zero-sized block (one such message remains, where main has two), which is what the body says — the thread-control-block reads (fs:0, fs:0x28) take the normal path now, while TLS data access with an empty block is still refused.

Worth keeping: this one is a Python relinker test, so it runs on Linux as well as Windows — that is why both arms exist here instead of only the author's.

Not verified: Windows, and a real title relink end-to-end (the title is named, but relinking it here would only exercise the same builder path).

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5465259834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1911#pullrequestreview-5465259834). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T03:06:31Z. **Review:** COMMENTED --- Verified on Linux — both arms, and the failure is a named assertion rather than a silent abort. - **Arm A (this PR)**: `cmake --build build --target relinker` → clean; `ctest --test-dir build -R '^empty_tls$'` → **Passed** (0.07 s). - **Arm B (control)**: `WindowsTlsBuilder.cpp` back at `main` with the test kept, rebuilt the same way → `empty_tls` **Failed**, `AssertionError` at `test_empty_tls.py:41` with the relink output ending in the empty-`PT_TLS` refusal. So the test pins this: it is the builder's refusal the change removes, and the test would catch a regression. Claim cross-check: after the change the file still carries the refusal for a displacement into thread-local data with a zero-sized block (one such message remains, where `main` has two), which is what the body says — the thread-control-block reads (`fs:0`, `fs:0x28`) take the normal path now, while TLS *data* access with an empty block is still refused. Worth keeping: this one is a Python relinker test, so it runs on Linux as well as Windows — that is why both arms exist here instead of only the author's. Not verified: Windows, and a real title relink end-to-end (the title is named, but relinking it here would only exercise the same builder path). Written with AI assistance.
Xabilimon1 commented 2026-10-09 15:16:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Xabilimon1 on 2026-10-09T15:16:37Z.


Tested this branch with the relinker-only build on macOS (Apple M3). Besides ccleste (PPSA59322), it also fixes the same Guest TLS access with empty PT_TLS stop with windows for RetroArch (PPSA99169, v1.0.0-beta.1) and PS5MC (PPSA99420, 1.2). All three fail on main d70b8998 and relink with this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083772926 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1911#issuecomment-6083772926). Originally posted by [Xabilimon1](https://github.com/Xabilimon1) on 2026-10-09T15:16:37Z. --- Tested this branch with the relinker-only build on macOS (Apple M3). Besides ccleste (PPSA59322), it also fixes the same Guest TLS access with empty PT_TLS stop with windows for RetroArch (PPSA99169, v1.0.0-beta.1) and PS5MC (PPSA99420, 1.2). All three fail on main d70b8998 and relink with this PR.
danielradosa commented 2026-10-09 18:34:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by danielradosa on 2026-10-09T18:34:31Z.


Windows run with a title for this PR. PS5MC PPSA99420 1.2 (catalog release, sha256 as in the record, eboot.bin and sce_module/libc.prx unwrapped from their SELF containers), whose PT_TLS is empty (filesz and memsz 0). Windows 11 Pro 10.0.26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • main 0fdda7be (and 4b005b22): relinker --windows fails with FAIL: Guest TLS access with empty PT_TLS (offset 0x598c000). Linux output relinks.
  • main 0fdda7be + this PR (+ #1650, #1748, #1944, which do not touch TLS conversion): --windows relinks, and the converted executable loads its libraries and stops at its first absent import, FAIL: unresolved ELF import KiJEPEWRyUY from libkernel.prx (sigaction).

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086963937 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1911#issuecomment-6086963937). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-10-09T18:34:31Z. --- Windows run with a title for this PR. PS5MC PPSA99420 1.2 (catalog release, sha256 as in the record, `eboot.bin` and `sce_module/libc.prx` unwrapped from their SELF containers), whose `PT_TLS` is empty (filesz and memsz 0). Windows 11 Pro 10.0.26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - `main` 0fdda7be (and 4b005b22): `relinker --windows` fails with `FAIL: Guest TLS access with empty PT_TLS (offset 0x598c000)`. Linux output relinks. - `main` 0fdda7be + this PR (+ #1650, #1748, #1944, which do not touch TLS conversion): `--windows` relinks, and the converted executable loads its libraries and stops at its first absent import, `FAIL: unresolved ELF import KiJEPEWRyUY from libkernel.prx` (`sigaction`). AI-assisted: yes (Claude Code).
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!1911
No description provided.