fix(relinker): guard TLS instruction byte access at EOF #6401

Merged
MarcZX10 merged 1 commit from upstream-pr/6401 into main 2026-10-17 05:15:00 +00:00
MarcZX10 commented 2026-10-15 14:05:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T14:05:14Z.


What

  • Read the ModRM register byte only when registerAddress() has established that the instruction contains it.
  • Add an EOF regression fixture where a segment-prefixed instruction ends an executable PT_LOAD at EOF.
  • Fixes #6391.

Tested

  • Linux x86-64: relinker-only Debug build with AddressSanitizer completed.
  • ASAN_OPTIONS=detect_leaks=0 python3 core/relinker/relinker/tests/test_tls_function_coverage.py /tmp/AnyPS5-6391-build/core/relinker/relinker all passed.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check passed.

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 or are documented; not applicable
  • No notes, investigation .md files or images added
  • New third-party code is a submodule built from source; not applicable
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4874556523 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6401). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T14:05:14Z. --- ### What - Read the ModRM register byte only when `registerAddress()` has established that the instruction contains it. - Add an EOF regression fixture where a segment-prefixed instruction ends an executable PT_LOAD at EOF. - Fixes #6391. ### Tested - Linux x86-64: relinker-only Debug build with AddressSanitizer completed. - `ASAN_OPTIONS=detect_leaks=0 python3 core/relinker/relinker/tests/test_tls_function_coverage.py /tmp/AnyPS5-6391-build/core/relinker/relinker all` passed. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check` passed. ### 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 - [x] Unimplemented paths throw or are documented; not applicable - [x] No notes, investigation `.md` files or images added - [x] New third-party code is a submodule built from source; not applicable - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Dextroid17 commented 2026-10-15 15:39:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T15:39:43Z.


I ran the rejected TLS coverage tests on Linux, GCC 16.2.1. The fix removes the 1-byte read past the end, but in a normal build the new segment-prefix-at-eof case passes with or without the fix; it fails only when the relinker is built with AddressSanitizer.

Setup: head 2a65acd86 merged into main at 92e50418f (no conflicts; the PR's base is 59 commits behind), clean environment (env -i with only HOME and PATH set; for ctest also VK_ICD_FILENAMES), 3 runs each. "Fix reverted" means core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp put back to main's version, with the PR's test changes kept.

Default build (CMAKE_BUILD_TYPE empty), ctest -R '^tls_function_coverage_' (31 tests):

  • with this PR: 3 of 3 runs pass.
  • with the fix reverted: 3 of 3 runs also pass.

AddressSanitizer build (-DANYPS5_ENABLE_ASAN=ON -DANYPS5_RELINKER_ONLY=ON), running python3 core/relinker/relinker/tests/test_tls_function_coverage.py <relinker> rejected (I configured it without BUILD_TESTING, so ctest found no tests and I ran the script directly):

  • with this PR: 3 of 3 runs pass, no AddressSanitizer reports.
  • with the fix reverted: 3 of 3 runs fail at segment-prefix-at-eof, with "AddressSanitizer: heap-buffer-overflow", "READ of size 1" in Elfpatcher::Windows::WindowsTlsBuilder::Build.

No workflow in .github/workflows builds with AddressSanitizer, and the relinker job builds Release, so CI would not catch this if it returned. The extra byte is read but never used, so the result is the same with or without the fix; only a sanitizer (or a read that crosses into unmapped memory) shows it. Running the TLS coverage tests in an AddressSanitizer build would keep it from coming back.

I did not check Windows or macOS.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6196020201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6401#issuecomment-6196020201). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-15T15:39:43Z. --- I ran the `rejected` TLS coverage tests on Linux, GCC 16.2.1. The fix removes the 1-byte read past the end, but in a normal build the new `segment-prefix-at-eof` case passes with or without the fix; it fails only when the relinker is built with AddressSanitizer. Setup: head 2a65acd86 merged into main at 92e50418f (no conflicts; the PR's base is 59 commits behind), clean environment (`env -i` with only HOME and PATH set; for ctest also VK_ICD_FILENAMES), 3 runs each. "Fix reverted" means core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp put back to main's version, with the PR's test changes kept. Default build (CMAKE_BUILD_TYPE empty), `ctest -R '^tls_function_coverage_'` (31 tests): - with this PR: 3 of 3 runs pass. - with the fix reverted: 3 of 3 runs also pass. AddressSanitizer build (`-DANYPS5_ENABLE_ASAN=ON -DANYPS5_RELINKER_ONLY=ON`), running `python3 core/relinker/relinker/tests/test_tls_function_coverage.py <relinker> rejected` (I configured it without BUILD_TESTING, so ctest found no tests and I ran the script directly): - with this PR: 3 of 3 runs pass, no AddressSanitizer reports. - with the fix reverted: 3 of 3 runs fail at `segment-prefix-at-eof`, with "AddressSanitizer: heap-buffer-overflow", "READ of size 1" in `Elfpatcher::Windows::WindowsTlsBuilder::Build`. No workflow in .github/workflows builds with AddressSanitizer, and the relinker job builds Release, so CI would not catch this if it returned. The extra byte is read but never used, so the result is the same with or without the fix; only a sanitizer (or a read that crosses into unmapped memory) shows it. Running the TLS coverage tests in an AddressSanitizer build would keep it from coming back. I did not check Windows or macOS. AI-assisted: yes
MarcZX10 commented 2026-10-16 15:59:25 +00:00 (Migrated from github.com)

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


Thanks for testing the fix under AddressSanitizer and showing that reverting it produces a heap-buffer-overflow 3/3, while the normal Release test cannot distinguish the extra read. That is strong evidence for the bounds fix. The suggested ASan regression run is also useful: I will check how to cover this code path in the relinker validation workflow so the one-byte overread does not silently return.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6219415309 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6401#issuecomment-6219415309). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T15:59:25Z. --- Thanks for testing the fix under AddressSanitizer and showing that reverting it produces a heap-buffer-overflow 3/3, while the normal Release test cannot distinguish the extra read. That is strong evidence for the bounds fix. The suggested ASan regression run is also useful: I will check how to cover this code path in the relinker validation workflow so the one-byte overread does not silently return.
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!6401
No description provided.