fix(relinker): frame red-zone leaves past an int3 in the middle #19336

Merged
zorde23 merged 1 commit from upstream-pr/19336 into main 2026-12-04 04:51:59 +00:00
zorde23 commented 2026-11-13 04:14:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zorde23 on 2026-11-13T04:14:55Z.


What

In --frame-red-zone, the framer decodes each function from its FDE start and stops the body at its first single-byte int3, taking it as padding. An int3 in the middle of a function (an inline breakpoint or trap before more code) therefore cuts the body short: a frame-pointer leaf with one is analyzed only up to it, and its exit after the int3 is never seen, so the leaf is not framed and keeps its red zone.

The body now stops at an int3 only when nothing but int3 follows it up to the FDE end, which is the "int3 padding before it" that docs/dev/debt/relinker.md already describes. int n (0xcd) was never a stop and is still decoded through.

  • core/relinker/codegen/src/x86/RedZoneFramer.cpp: the trailing-padding check.
  • New test framesAFramePointerLeafPastAnInterruptInTheMiddle in RedZoneFramerTests.cpp: a frame-pointer leaf with an int3, and one with int 0x41, in the middle of its body must be framed up to its ret, and the exit after the interrupt must raise rsp before pop rbp. On main the int3 case fails with "a frame-pointer leaf with an interrupt in the middle was not framed up to its ret".

Tested

Relinker-only build as in CI (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release), main at a6606d5bab and this PR on top of it, on two hosts:

cmake -S . -B build-relinker -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON
cmake --build build-relinker
ctest --test-dir build-relinker --output-on-failure --timeout 120 --parallel 4 --no-tests=error [--repeat after-timeout:2 on Windows]

Ubuntu 24.04, g++ 13.3.0

main (a6606d5bab) this PR
Relinker test suite n/a, see below 904/905
Failures output_fifo_macos output_fifo_macos
red_zone_framer fails with the new test added (without the fix) pass
red_zone_framer_execution pass pass

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs, as in CI)

main (a6606d5bab) this PR
Relinker test suite 924/927 924/927
Failures conventions_git_errors, test_script_registration, web_library_assets the same 3
red_zone_framer pass pass
red_zone_framer_execution pass pass

On Linux, output_fifo_macos fails the same way on main in that environment (AssertionError: regular in test_output_fifo.py) and passes on Windows. The three Windows failures call git, and the sources there were exported without .git; they fail the same way on main. Neither touches the framer. On Linux the full suite ran only for this PR; for main only red_zone_framer and output_fifo_macos were run.

Not tested on a macOS host, and not with a title.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5289876663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19336). Originally posted by [zorde23](https://github.com/zorde23) on 2026-11-13T04:14:55Z. --- <!-- claude-projects-attribution: {"github_login":"zorde23"} --> ### What In `--frame-red-zone`, the framer decodes each function from its FDE start and stops the body at its first single-byte `int3`, taking it as padding. An `int3` in the middle of a function (an inline breakpoint or trap before more code) therefore cuts the body short: a frame-pointer leaf with one is analyzed only up to it, and its exit after the `int3` is never seen, so the leaf is not framed and keeps its red zone. The body now stops at an `int3` only when nothing but `int3` follows it up to the FDE end, which is the "int3 padding before it" that `docs/dev/debt/relinker.md` already describes. `int n` (`0xcd`) was never a stop and is still decoded through. - `core/relinker/codegen/src/x86/RedZoneFramer.cpp`: the trailing-padding check. - New test `framesAFramePointerLeafPastAnInterruptInTheMiddle` in `RedZoneFramerTests.cpp`: a frame-pointer leaf with an `int3`, and one with `int 0x41`, in the middle of its body must be framed up to its `ret`, and the exit after the interrupt must raise `rsp` before `pop rbp`. On `main` the `int3` case fails with "a frame-pointer leaf with an interrupt in the middle was not framed up to its ret". ### Tested Relinker-only build as in CI (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, Release), `main` at a6606d5bab and this PR on top of it, on two hosts: ``` cmake -S . -B build-relinker -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON cmake --build build-relinker ctest --test-dir build-relinker --output-on-failure --timeout 120 --parallel 4 --no-tests=error [--repeat after-timeout:2 on Windows] ``` **Ubuntu 24.04, g++ 13.3.0** | | `main` (a6606d5bab) | this PR | |---|---|---| | Relinker test suite | n/a, see below | 904/905 | | Failures | `output_fifo_macos` | `output_fifo_macos` | | `red_zone_framer` | fails with the new test added (without the fix) | pass | | `red_zone_framer_execution` | pass | pass | **Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs, as in CI)** | | `main` (a6606d5bab) | this PR | |---|---|---| | Relinker test suite | 924/927 | 924/927 | | Failures | `conventions_git_errors`, `test_script_registration`, `web_library_assets` | the same 3 | | `red_zone_framer` | pass | pass | | `red_zone_framer_execution` | pass | pass | On Linux, `output_fifo_macos` fails the same way on `main` in that environment (`AssertionError: regular` in `test_output_fifo.py`) and passes on Windows. The three Windows failures call `git`, and the sources there were exported without `.git`; they fail the same way on `main`. Neither touches the framer. On Linux the full suite ran only for this PR; for `main` only `red_zone_framer` and `output_fifo_macos` were run. Not tested on a macOS host, and not with a title. ### 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 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-15 20:59:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T20:59:58Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d46ab37d57:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: red_zone_framer_tests built cleanly.
  • CTest (red_zone_framer, red_zone_framer_execution):
    1/2 Test #4963: red_zone_framer ..................   Passed    0.00 sec
    2/2 Test #4964: red_zone_framer_execution ........   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/relinker/codegen/src/x86/RedZoneFramer.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4963: red_zone_framer ..................***Failed    0.00 sec
    red-zone framer tests failed: a frame-pointer leaf with an interrupt in the middle was not framed up to its ret
    2/2 Test #4964: red_zone_framer_execution ........   Passed    0.02 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified that int3 padding detection checks code[match.Offset] == 0xcc && match.Length == 1 and uses std::all_of across [matches.begin() + m, matches.end()) to ensure only trailing int3 sequences terminate function body decoding, correctly preserving inline 0xcc trap/breakpoint instructions.
    • core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified iterator and index bounds in std::all_of(matches.begin() + static_caststd::ptrdiff_t(m), matches.end(), int3) where m < matches.size() guarantees valid range offsets without signed/unsigned conversion defects or buffer overflows.
    • core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified that retained 0xcc instructions in body do not falsely trigger stack-tracking or control-flow filters (pushesOrPops, rspAdjustment, memoryUse), allowing downstream exit detection and red-zone displacement calculations to complete normally.
    • core/relinker/codegen/tests/RedZoneFramerTests.cpp:framesAFramePointerLeafPastAnInterruptInTheMiddle: Verified test coverage exercises both single-byte 0xcc (int3) and multi-byte 0xcd 0x41 (int 0x41) interrupts in the body of a frame-pointer leaf, validating that both entry and exit trampoline sites are generated and that the exit site raises rsp before popping rbp.
    • core/relinker/codegen/tests/RedZoneFramerTests.cpp:main: Verified that framesAFramePointerLeafPastAnInterruptInTheMiddle() is explicitly registered and invoked in main().
    • AGENTS.md: Verified strict compliance with project rules: zero code comments added, valid Conventional Commit PR title, no external third-party dependencies introduced, and no superfluous files committed.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5912908740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19336#pullrequestreview-5912908740). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T20:59:58Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d46ab37d57`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `red_zone_framer_tests` built cleanly. - **CTest** (`red_zone_framer`, `red_zone_framer_execution`): ``` 1/2 Test #4963: red_zone_framer .................. Passed 0.00 sec 2/2 Test #4964: red_zone_framer_execution ........ Passed 0.03 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/relinker/codegen/src/x86/RedZoneFramer.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #4963: red_zone_framer ..................***Failed 0.00 sec red-zone framer tests failed: a frame-pointer leaf with an interrupt in the middle was not framed up to its ret 2/2 Test #4964: red_zone_framer_execution ........ Passed 0.02 sec 50% tests passed, 1 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified that int3 padding detection checks code[match.Offset] == 0xcc && match.Length == 1 and uses std::all_of across [matches.begin() + m, matches.end()) to ensure only trailing int3 sequences terminate function body decoding, correctly preserving inline 0xcc trap/breakpoint instructions. - core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified iterator and index bounds in std::all_of(matches.begin() + static_cast<std::ptrdiff_t>(m), matches.end(), int3) where m < matches.size() guarantees valid range offsets without signed/unsigned conversion defects or buffer overflows. - core/relinker/codegen/src/x86/RedZoneFramer.cpp:frameSegment: Verified that retained 0xcc instructions in body do not falsely trigger stack-tracking or control-flow filters (pushesOrPops, rspAdjustment, memoryUse), allowing downstream exit detection and red-zone displacement calculations to complete normally. - core/relinker/codegen/tests/RedZoneFramerTests.cpp:framesAFramePointerLeafPastAnInterruptInTheMiddle: Verified test coverage exercises both single-byte 0xcc (int3) and multi-byte 0xcd 0x41 (int 0x41) interrupts in the body of a frame-pointer leaf, validating that both entry and exit trampoline sites are generated and that the exit site raises rsp before popping rbp. - core/relinker/codegen/tests/RedZoneFramerTests.cpp:main: Verified that framesAFramePointerLeafPastAnInterruptInTheMiddle() is explicitly registered and invoked in main(). - AGENTS.md: Verified strict compliance with project rules: zero code comments added, valid Conventional Commit PR title, no external third-party dependencies introduced, and no superfluous files committed. Looks Good To Me
MarcZX10 commented 2026-11-23 01:43:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T01:43:49Z.

Review: APPROVED


Reviewed — looks good

The change at head d46ab37d57c33bbf4b3e44ce9416bda422055a7d conditionally treats trailing int3 instructions as padding; the implementation and focused regression test support the stated fix. I reviewed both changed files; no checks were run by me.

What I ran

No commands or tests were run. I inspected the prepared diff and the changed files at the pinned head using repo_read.py.

Results

Check Result Note
Diff and changed source passed Both changed files read
Tests not run Test results in the description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the test suite or verify behavior on macOS or a physical console. The description reports test runs on Ubuntu and Windows; these were not performed by me. Both files listed in the manifest as reviewed were read. No files were excluded in files.json because it was not provided in the prepared materials.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015703157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19336#pullrequestreview-6015703157). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T01:43:49Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `d46ab37d57c33bbf4b3e44ce9416bda422055a7d` conditionally treats trailing `int3` instructions as padding; the implementation and focused regression test support the stated fix. I reviewed both changed files; no checks were run by me. ## What I ran No commands or tests were run. I inspected the prepared diff and the changed files at the pinned head using `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source | passed | Both changed files read | | Tests | not run | Test results in the description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the test suite or verify behavior on macOS or a physical console. The description reports test runs on Ubuntu and Windows; these were not performed by me. Both files listed in the manifest as reviewed were read. No files were excluded in `files.json` because it was not provided in the prepared materials. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 11:55:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:55:30Z.

Review: COMMENTED


Built this head with ANYPS5_RELINKER_ONLY=ON and ran red_zone_framer and red_zone_framer_execution on Linux. Both passed, covering the generated frame and its native execution.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6169256314 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19336#pullrequestreview-6169256314). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:55:30Z. **Review:** COMMENTED --- Built this head with `ANYPS5_RELINKER_ONLY=ON` and ran `red_zone_framer` and `red_zone_framer_execution` on Linux. Both passed, covering the generated frame and its native execution. AI-assisted: yes.
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!19336
No description provided.