fix(relinker): guard Windows entry-stub RVA allocations #6204

Merged
vrebel11 merged 2 commits from upstream-pr/6204 into main 2026-10-29 23:56:58 +00:00
vrebel11 commented 2026-10-15 02:54:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by vrebel11 on 2026-10-15T02:54:05Z.


What

  • Validate Windows entry-stub allocation endpoints before resizing the output buffer.
  • Reject allocations exceeding the supported signed 32-bit PE RVA boundary or output-buffer capacity.
  • Add Windows entry-stub boundary tests and a source-level endpoint guard.

This hardens Windows entry-stub allocation handling. It does not establish a fix for the intermittent GCC crash reported in #5927.

The runtime boundary test alone is not proven to fail against the previous implementation. The separate source-level guard verifies that endpoint checks precede buffer resizing.

Tested

  • Windows, WinLibs GCC 15.2.0 (MinGW-w64).
  • Release relinker-only build: 169/169 CTests passed.
  • windows_entry_stub_bounds: passed.
  • windows_entry_stub_endpoint_guard: passed.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • Staged patch formatting check: passed.
  • No PS5 title or homebrew execution tested.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • No new third-party code added
  • Generic behaviour, not specific to one title
  • Depends on: none known
  • Unblocks: none demonstrated
  • GPUs tested: n/a (no GPU-related changes)
  • AI-assisted: yes — ChatGPT assisted with implementation, tests, and pull request preparation. I am responsible for reviewing the submitted changes.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4867503181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6204). Originally posted by [vrebel11](https://github.com/vrebel11) on 2026-10-15T02:54:05Z. --- ### What - Validate Windows entry-stub allocation endpoints before resizing the output buffer. - Reject allocations exceeding the supported signed 32-bit PE RVA boundary or output-buffer capacity. - Add Windows entry-stub boundary tests and a source-level endpoint guard. This hardens Windows entry-stub allocation handling. It does not establish a fix for the intermittent GCC crash reported in #5927. The runtime boundary test alone is not proven to fail against the previous implementation. The separate source-level guard verifies that endpoint checks precede buffer resizing. ### Tested - Windows, WinLibs GCC 15.2.0 (MinGW-w64). - Release relinker-only build: **169/169 CTests passed**. - `windows_entry_stub_bounds`: passed. - `windows_entry_stub_endpoint_guard`: passed. - `python3 tools/check_conventions.py --base origin/main`: passed. - Staged patch formatting check: passed. - No PS5 title or homebrew execution tested. ### 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; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code added - [x] Generic behaviour, not specific to one title - [x] Depends on: none known - [x] Unblocks: none demonstrated - [x] GPUs tested: n/a (no GPU-related changes) - [x] AI-assisted: yes — ChatGPT assisted with implementation, tests, and pull request preparation. I am responsible for reviewing the submitted changes.
jotitadev commented 2026-10-15 03:18:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T03:18:08Z.

Review: COMMENTED


Built and tested this on Apple silicon (not Windows), so a few notes.

Machine: Apple M2 Max, macOS 27.2, AppleClang (Xcode 27), CMake 4.4.4 + Ninja.
Head: 0d429424a9c8750a037b12c63248311d3337c16e
Base: origin/main 760ffa6838
Build: relinker-only (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), both arm64 and x86_64 (-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3).

The PR compiles cleanly on both arches; no macOS-specific issues. relinker_core already compiles WindowsEntryStubBuilder.cpp on every host, so the hardening builds everywhere.

Two things worth flagging:

  1. The new tests are gated behind if(WIN32), so they never run on the Linux/macOS CI. That is inconsistent with the sibling Windows tests in the same file (windows_icon_resource, entry_stub), which are registered unconditionally and link the same Windows sources into relinker_core. I removed the if(WIN32) guard locally and both targets built and ran fine on arm64 and x86_64. Registering them unconditionally would give this hardening coverage on the default (non-Windows) CI.

  2. The runtime windows_entry_stub_bounds test does not fail against the previous implementation. With the fix reverted but the test kept, on arm64 it still passes:

    windows_entry_stub_bounds ........... Passed
    windows_entry_stub_endpoint_guard ...***Failed  (FAIL: allocation endpoint is not checked before resize)
    

    Only the source-text guard catches the regression; the runtime test passes with and without the fix. This matches your own note. The source-grep guard is also brittle (it matches literal source strings). A runtime case that actually drives the allocation past the signed-32-bit PE RVA boundary / vector capacity and expects the new RelinkerException would protect the behavior instead of the source text.

Full suite with the fix: arm64 165/165, x86_64 160/165 (the 5 failures are pre-existing on x86_64+Py3.9: pr_overlap_encoding, pr_overlap_tools, stubgen_stdout_newlines, release_package, linux_fixed_base).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5527628899 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6204#pullrequestreview-5527628899). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T03:18:08Z. **Review:** COMMENTED --- Built and tested this on Apple silicon (not Windows), so a few notes. Machine: Apple M2 Max, macOS 27.2, AppleClang (Xcode 27), CMake 4.4.4 + Ninja. Head: 0d429424a9c8750a037b12c63248311d3337c16e Base: origin/main 760ffa6838d0 Build: relinker-only (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`), both arm64 and x86_64 (`-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3`). The PR compiles cleanly on both arches; no macOS-specific issues. `relinker_core` already compiles `WindowsEntryStubBuilder.cpp` on every host, so the hardening builds everywhere. Two things worth flagging: 1. **The new tests are gated behind `if(WIN32)`**, so they never run on the Linux/macOS CI. That is inconsistent with the sibling Windows tests in the same file (`windows_icon_resource`, `entry_stub`), which are registered unconditionally and link the same Windows sources into `relinker_core`. I removed the `if(WIN32)` guard locally and both targets built and ran fine on arm64 and x86_64. Registering them unconditionally would give this hardening coverage on the default (non-Windows) CI. 2. **The runtime `windows_entry_stub_bounds` test does not fail against the previous implementation.** With the fix reverted but the test kept, on arm64 it still passes: ``` windows_entry_stub_bounds ........... Passed windows_entry_stub_endpoint_guard ...***Failed (FAIL: allocation endpoint is not checked before resize) ``` Only the source-text guard catches the regression; the runtime test passes with and without the fix. This matches your own note. The source-grep guard is also brittle (it matches literal source strings). A runtime case that actually drives the allocation past the signed-32-bit PE RVA boundary / vector capacity and expects the new `RelinkerException` would protect the behavior instead of the source text. Full suite with the fix: arm64 165/165, x86_64 160/165 (the 5 failures are pre-existing on x86_64+Py3.9: `pr_overlap_encoding`, `pr_overlap_tools`, `stubgen_stdout_newlines`, `release_package`, `linux_fixed_base`). Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
vrebel11 commented 2026-10-16 22:28:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by vrebel11 on 2026-10-16T22:28:57Z.


Built and tested this on Apple silicon (not Windows), so a few notes.

Machine: Apple M2 Max, macOS 27.2, AppleClang (Xcode 27), CMake 4.4.4 + Ninja. Head: 0d42942 Base: origin/main 760ffa6 Build: relinker-only (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), both arm64 and x86_64 (-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3).

The PR compiles cleanly on both arches; no macOS-specific issues. relinker_core already compiles WindowsEntryStubBuilder.cpp on every host, so the hardening builds everywhere.

Two things worth flagging:

  1. The new tests are gated behind if(WIN32), so they never run on the Linux/macOS CI. That is inconsistent with the sibling Windows tests in the same file (windows_icon_resource, entry_stub), which are registered unconditionally and link the same Windows sources into relinker_core. I removed the if(WIN32) guard locally and both targets built and ran fine on arm64 and x86_64. Registering them unconditionally would give this hardening coverage on the default (non-Windows) CI.

  2. The runtime windows_entry_stub_bounds test does not fail against the previous implementation. With the fix reverted but the test kept, on arm64 it still passes:

    windows_entry_stub_bounds ........... Passed
    windows_entry_stub_endpoint_guard ...***Failed  (FAIL: allocation endpoint is not checked before resize)
    

    Only the source-text guard catches the regression; the runtime test passes with and without the fix. This matches your own note. The source-grep guard is also brittle (it matches literal source strings). A runtime case that actually drives the allocation past the signed-32-bit PE RVA boundary / vector capacity and expects the new RelinkerException would protect the behavior instead of the source text.

Full suite with the fix: arm64 165/165, x86_64 160/165 (the 5 failures are pre-existing on x86_64+Py3.9: pr_overlap_encoding, pr_overlap_tools, stubgen_stdout_newlines, release_package, linux_fixed_base).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

Thanks for taking the time to build and test this on both macOS architectures and for the detailed feedback.

I agree with both points. I'll remove the Windows-only test registration guard and work on a runtime regression that fails against the original implementation, rather than relying on the source-text check.

I'll run the relevant tests again and update this PR with the changes. Appreciate the independent verification!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225901343 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6204#issuecomment-6225901343). Originally posted by [vrebel11](https://github.com/vrebel11) on 2026-10-16T22:28:57Z. --- > Built and tested this on Apple silicon (not Windows), so a few notes. > > Machine: Apple M2 Max, macOS 27.2, AppleClang (Xcode 27), CMake 4.4.4 + Ninja. Head: [0d42942](https://github.com/boykopovar/AnyPS5/commit/0d429424a9c8750a037b12c63248311d3337c16e) Base: origin/main [760ffa6](https://github.com/boykopovar/AnyPS5/commit/760ffa6838d08c13782a0dcc7e9f17319c27b049) Build: relinker-only (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`), both arm64 and x86_64 (`-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3`). > > The PR compiles cleanly on both arches; no macOS-specific issues. `relinker_core` already compiles `WindowsEntryStubBuilder.cpp` on every host, so the hardening builds everywhere. > > Two things worth flagging: > > 1. **The new tests are gated behind `if(WIN32)`**, so they never run on the Linux/macOS CI. That is inconsistent with the sibling Windows tests in the same file (`windows_icon_resource`, `entry_stub`), which are registered unconditionally and link the same Windows sources into `relinker_core`. I removed the `if(WIN32)` guard locally and both targets built and ran fine on arm64 and x86_64. Registering them unconditionally would give this hardening coverage on the default (non-Windows) CI. > 2. **The runtime `windows_entry_stub_bounds` test does not fail against the previous implementation.** With the fix reverted but the test kept, on arm64 it still passes: > ``` > windows_entry_stub_bounds ........... Passed > windows_entry_stub_endpoint_guard ...***Failed (FAIL: allocation endpoint is not checked before resize) > ``` > > > > > > > > > > > > Only the source-text guard catches the regression; the runtime test passes with and without the fix. This matches your own note. The source-grep guard is also brittle (it matches literal source strings). A runtime case that actually drives the allocation past the signed-32-bit PE RVA boundary / vector capacity and expects the new `RelinkerException` would protect the behavior instead of the source text. > > Full suite with the fix: arm64 165/165, x86_64 160/165 (the 5 failures are pre-existing on x86_64+Py3.9: `pr_overlap_encoding`, `pr_overlap_tools`, `stubgen_stdout_newlines`, `release_package`, `linux_fixed_base`). > > Written with AI assistance (Claude Code). I ran the builds and tests above on my machine. Thanks for taking the time to build and test this on both macOS architectures and for the detailed feedback. I agree with both points. I'll remove the Windows-only test registration guard and work on a runtime regression that fails against the original implementation, rather than relying on the source-text check. I'll run the relevant tests again and update this PR with the changes. Appreciate the independent verification!
adriadam10 commented 2026-10-18 17:15:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T17:15:49Z.

Review: CHANGES_REQUESTED


Thanks. The endpoint check before resize is reasonable, but the tests don't protect it yet:

  • WindowsEntryStubBoundsTests.cpp: per your own note (and @jotitadev's run) windows_entry_stub_bounds passes without the change, so it pins nothing. Add a case that crosses the 2 GiB limit only because of the new endpoint check (an allocation that starts below INT32_MAX and ends above it) and expects the RelinkerException; show that it fails on main.
  • WindowsEntryStubEndpointGuard.cmake: it greps literal source text, so it breaks on any reformat and pins no behaviour. Drop it once the runtime test above fails without the fix.
  • core/relinker/CMakeLists.txt:675: remove the if(WIN32) guard, as you already agreed with @jotitadev; the sibling entry_stub test is registered unconditionally and the sources build on every host.
  • WindowsEntryStubBoundsTests.cpp:99: add the missing trailing newline and drop the double blank lines.

Happy to re-review once pushed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5564019852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6204#pullrequestreview-5564019852). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-18T17:15:49Z. **Review:** CHANGES_REQUESTED --- Thanks. The endpoint check before `resize` is reasonable, but the tests don't protect it yet: - `WindowsEntryStubBoundsTests.cpp`: per your own note (and @jotitadev's run) `windows_entry_stub_bounds` passes without the change, so it pins nothing. Add a case that crosses the 2 GiB limit only because of the new endpoint check (an allocation that starts below `INT32_MAX` and ends above it) and expects the `RelinkerException`; show that it fails on `main`. - `WindowsEntryStubEndpointGuard.cmake`: it greps literal source text, so it breaks on any reformat and pins no behaviour. Drop it once the runtime test above fails without the fix. - `core/relinker/CMakeLists.txt:675`: remove the `if(WIN32)` guard, as you already agreed with @jotitadev; the sibling `entry_stub` test is registered unconditionally and the sources build on every host. - `WindowsEntryStubBoundsTests.cpp:99`: add the missing trailing newline and drop the double blank lines. Happy to re-review once pushed.
THEMANnt commented 2026-10-26 13:49:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-26T13:49:33Z.

Review: DISMISSED


Reviewed 0d429424a9c8 against merge base 760ffa6838d0. Changes requested for missing regression coverage of the new allocation-endpoint check.

Exercise an allocation that crosses the signed RVA limit. The new runtime test also passes when linked against the unchanged baseline implementation. It therefore does not detect removal of the endpoint check. The source-text guard fails on baseline, but that establishes a textual difference rather than the runtime boundary behavior.

Add an input whose allocation starts below the 2 GiB RVA limit but ends beyond it, assert RelinkerException, and demonstrate failure against baseline. Replace the brittle source-text guard with that runtime regression. Register these tests outside if(WIN32) so Linux/macOS exercise the platform-independent builder, consistent with neighboring tests.

Validation: Candidate focused tests pass 3/3; baseline entry-stub test passes 1/1. The candidate runtime test linked against baseline also exits 0. These findings concern coverage and CI visibility; no production-check defect is claimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5652990329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6204#pullrequestreview-5652990329). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-26T13:49:33Z. **Review:** DISMISSED --- Reviewed `0d429424a9c8` against merge base `760ffa6838d0`. Changes requested for missing regression coverage of the new allocation-endpoint check. **Exercise an allocation that crosses the signed RVA limit.** The new runtime test also passes when linked against the unchanged baseline implementation. It therefore does not detect removal of the endpoint check. The source-text guard fails on baseline, but that establishes a textual difference rather than the runtime boundary behavior. Add an input whose allocation starts below the 2 GiB RVA limit but ends beyond it, assert `RelinkerException`, and demonstrate failure against baseline. Replace the brittle source-text guard with that runtime regression. Register these tests outside `if(WIN32)` so Linux/macOS exercise the platform-independent builder, consistent with neighboring tests. **Validation:** Candidate focused tests pass 3/3; baseline entry-stub test passes 1/1. The candidate runtime test linked against baseline also exits 0. These findings concern coverage and CI visibility; no production-check defect is claimed.
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!6204
No description provided.