fix(relinker): wrap address-size LEA roots #20900

Merged
Queuse merged 1 commit from upstream-pr/20900 into main 2026-11-25 19:50:46 +00:00
Queuse commented 2026-11-15 17:50:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Queuse on 2026-11-15T17:50:23Z.


What

A 67h address-size override on a REX.W RIP-relative LEA computes a 32-bit effective address and zero-extends it into the 64-bit destination. The collector currently retains the high bits of the instruction address, so it can discard a callable low-address target and retain an unrelated high-address decoy. Truncate the effective address before the existing code/data classification; ordinary 64-bit LEAs keep their full address.

The production change is two lines. The regression suite covers positive/negative displacement, wrap across 4 GiB and below zero, ordinary 64-bit LEA, unwind functions, data targets, known function interiors and an SCE gap target.

Tested

Windows / WinLibs GCC 15.2 / Python 3.12, relinker-only build based on main b7aeb6f40619: four focused CTests passed (code_instruction_collector_address_size, code_analysis_address_data, strict_nid_filter, control_flow_graph). A control compiled from this exact main's CodeInstructionCollector.cpp, linked with the same test and remaining library objects, fails with 67h LEA failed to wrap effective address to 32-bit low callback. The changed collector passes.

Linux WSL / GCC 15.2: an actual assembled static ELF executes the address-size LEA and indirect call successfully, returning status 42. Feeding that ELF's real program headers and bytes into the collector misses callback on main and on #10861 at ef92693f7218, and finds it with this change. The full collector regression also fails on both controls and passes with this change. This demonstrates an executable input rather than a malformed-image-only assertion. No PS5 title was tested.

Native ELF reproducer (as --64 native67.s -o native67.o, ld -melf_x86_64 -T native67.ld native67.o -o native67, then ./native67; echo $? prints 42):

.section .text.low,"ax"
.global callback
callback:
mov $42,%eax
ret
.section .text.high,"ax"
.global _start
_start:
.byte 0x67,0x48,0x8d,0x05
.long 0xfff8
call *%rax
mov %eax,%edi
mov $60,%eax
syscall
.section .note.GNU-stack,"",@progbits

native67.ld:

ENTRY(_start)
PHDRS { low PT_LOAD FLAGS(5); high PT_LOAD FLAGS(5); stack PT_GNU_STACK FLAGS(6); }
SECTIONS {
  . = 0x20000; .text.low : { *(.text.low) } :low
  . = 0x100010000; .text.high : { *(.text.high) } :high
  /DISCARD/ : { *(.note*) *(.eh_frame*) }
}

python tools/check_conventions.py --base origin/main and git diff --check passed. The required overlap scan failed closed on 19 older branches. All reported shared-file patches were compared at their current heads: #6149, #7531, #10861, #11628, #11661, #11689, #11698, #12678, #13486, #13716, #15245, #15642, #15896, #15903, #17451, #17473, #17557, #17558, #18432. Their CMake changes introduce other targets; none implements this callable 32-bit effective-address case.

There is a genuine shared-hunk integration point with #10861: its broader classifier treats address-size-32 LEAs as data even with REX.W. In an isolated temporary composition, allowing REX.W through its code classification and truncating its target before classification makes both the native ELF probe and this regression suite pass. #10861 alone fails both. If that branch lands first, this small correction needs to be carried through its classifier, retaining the regression test. #13716 changes pointer/code classification without wrapping this LEA operand; #15903 changes hash bounds; #18432 changes decoder reuse. The other conflicting branches were compared manually, not built in combination. No performance claim.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5325451262 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20900). Originally posted by [Queuse](https://github.com/Queuse) on 2026-11-15T17:50:23Z. --- ### What A `67h` address-size override on a REX.W RIP-relative LEA computes a 32-bit effective address and zero-extends it into the 64-bit destination. The collector currently retains the high bits of the instruction address, so it can discard a callable low-address target and retain an unrelated high-address decoy. Truncate the effective address before the existing code/data classification; ordinary 64-bit LEAs keep their full address. The production change is two lines. The regression suite covers positive/negative displacement, wrap across 4 GiB and below zero, ordinary 64-bit LEA, unwind functions, data targets, known function interiors and an SCE gap target. ### Tested Windows / WinLibs GCC 15.2 / Python 3.12, relinker-only build based on `main` `b7aeb6f40619`: four focused CTests passed (`code_instruction_collector_address_size`, `code_analysis_address_data`, `strict_nid_filter`, `control_flow_graph`). A control compiled from this exact main's `CodeInstructionCollector.cpp`, linked with the same test and remaining library objects, fails with `67h LEA failed to wrap effective address to 32-bit low callback`. The changed collector passes. Linux WSL / GCC 15.2: an actual assembled static ELF executes the address-size LEA and indirect call successfully, returning status 42. Feeding that ELF's real program headers and bytes into the collector misses `callback` on main and on #10861 at `ef92693f7218`, and finds it with this change. The full collector regression also fails on both controls and passes with this change. This demonstrates an executable input rather than a malformed-image-only assertion. No PS5 title was tested. Native ELF reproducer (`as --64 native67.s -o native67.o`, `ld -melf_x86_64 -T native67.ld native67.o -o native67`, then `./native67; echo $?` prints 42): ```asm .section .text.low,"ax" .global callback callback: mov $42,%eax ret .section .text.high,"ax" .global _start _start: .byte 0x67,0x48,0x8d,0x05 .long 0xfff8 call *%rax mov %eax,%edi mov $60,%eax syscall .section .note.GNU-stack,"",@progbits ``` `native67.ld`: ```ld ENTRY(_start) PHDRS { low PT_LOAD FLAGS(5); high PT_LOAD FLAGS(5); stack PT_GNU_STACK FLAGS(6); } SECTIONS { . = 0x20000; .text.low : { *(.text.low) } :low . = 0x100010000; .text.high : { *(.text.high) } :high /DISCARD/ : { *(.note*) *(.eh_frame*) } } ``` `python tools/check_conventions.py --base origin/main` and `git diff --check` passed. The required overlap scan failed closed on 19 older branches. All reported shared-file patches were compared at their current heads: #6149, #7531, #10861, #11628, #11661, #11689, #11698, #12678, #13486, #13716, #15245, #15642, #15896, #15903, #17451, #17473, #17557, #17558, #18432. Their CMake changes introduce other targets; none implements this callable 32-bit effective-address case. There is a genuine shared-hunk integration point with #10861: its broader classifier treats address-size-32 LEAs as data even with REX.W. In an isolated temporary composition, allowing REX.W through its code classification and truncating its target before classification makes both the native ELF probe and this regression suite pass. #10861 alone fails both. If that branch lands first, this small correction needs to be carried through its classifier, retaining the regression test. #13716 changes pointer/code classification without wrapping this LEA operand; #15903 changes hash bounds; #18432 changes decoder reuse. The other conflicting branches were compared manually, not built in combination. No performance claim. <!-- 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. --> ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-16 01:48:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T01:48:38Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: code_instruction_collector_address_size_tests built cleanly.
  • CTest (code_instruction_collector_address_size):
    1/1 Test #5482: code_instruction_collector_address_size ...   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5482: code_instruction_collector_address_size ...***Failed    0.01 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:335-343 verified that addAddressTakenRoot truncates target via static_cast<std::uint32_t>(target) when info.AddressSize32 is true, matching x86-64 67h address-size override semantics (32-bit EIP-relative effective address calculation with zero-extension) before isCode, inTable, inFunction, and addPointerRoot classification.
    • core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:337-339 verified that ordinary 64-bit RIP-relative REX.W LEA (info.AddressSize32 == false) preserves all 64 bits of target without alteration.
    • core/relinker/relinker/tests/CodeInstructionCollectorAddressSizeTests.cpp:103-260 verified regression test coverage across high-to-low 32-bit wrap (CheckAddressSize32WrapsHighInstructionToLowCallback), ordinary 64-bit LEA (CheckOrdinary64BitLeaPreservesHighBits), negative displacement and 32-bit wrap boundary cases (CheckNegativeDisplacementAnd32BitWrap), and .eh_frame/SCE branch interactions (CheckEhFrameAndSceBranchesHonorAddressSize32).
    • core/relinker/CMakeLists.txt:208-211 and all modified C++20 sources verified for LP64/LLP64 portability (std::uint64_t, std::uint32_t, std::int32_t, std::size_t usage) and strict AGENTS.md compliance.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914162890 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20900#pullrequestreview-5914162890). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T01:48:38Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `af78ab9a82`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `code_instruction_collector_address_size_tests` built cleanly. - **CTest** (`code_instruction_collector_address_size`): ``` 1/1 Test #5482: code_instruction_collector_address_size ... Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5482: code_instruction_collector_address_size ...***Failed 0.01 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:335-343 verified that `addAddressTakenRoot` truncates `target` via `static_cast<std::uint32_t>(target)` when `info.AddressSize32` is true, matching x86-64 `67h` address-size override semantics (32-bit EIP-relative effective address calculation with zero-extension) before `isCode`, `inTable`, `inFunction`, and `addPointerRoot` classification. - core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp:337-339 verified that ordinary 64-bit RIP-relative `REX.W LEA` (`info.AddressSize32 == false`) preserves all 64 bits of `target` without alteration. - core/relinker/relinker/tests/CodeInstructionCollectorAddressSizeTests.cpp:103-260 verified regression test coverage across high-to-low 32-bit wrap (`CheckAddressSize32WrapsHighInstructionToLowCallback`), ordinary 64-bit LEA (`CheckOrdinary64BitLeaPreservesHighBits`), negative displacement and 32-bit wrap boundary cases (`CheckNegativeDisplacementAnd32BitWrap`), and `.eh_frame`/SCE branch interactions (`CheckEhFrameAndSceBranchesHonorAddressSize32`). - core/relinker/CMakeLists.txt:208-211 and all modified C++20 sources verified for LP64/LLP64 portability (`std::uint64_t`, `std::uint32_t`, `std::int32_t`, `std::size_t` usage) and strict AGENTS.md compliance. Looks Good To Me
MarcZX10 commented 2026-11-18 12:25:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T12:25:16Z.

Review: COMMENTED


Reviewed — no blocking findings

The reviewed change wraps address-size-32 LEA targets before classification; no blocking defect was verified, but I could not run the reported checks in this review environment.

What I ran

  • Reviewed head af78ab9a82fff70d12f26221743a070efaabd048 against the prepared diff; no merged-preview check was run.
  • Read all three changed files’ added lines: core/relinker/CMakeLists.txt, core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp, and core/relinker/relinker/tests/CodeInstructionCollectorAddressSizeTests.cpp.
  • No commands or tests were run by me. The four CTests and control results in the description are author-reported.

Results

Check Result Note
Changed-line review passed All changed lines read
Build and CTests not run Author reports focused CTests passed
Control test not run Author reports the control failed as expected

Negative control

I could not run a negative control. The description reports that a control built from main failed on the new wrapping assertion.

Findings

Blocking

None.

Non-blocking

  • The description identifies an integration point with #10861: if that change lands first, carry this target truncation and regression test through its classifier. The current diff’s changed code applies the 32-bit truncation before existing classification.

Sources

Not verified

No build or test execution, PS5 title, or console behavior was verified by me. The prepared manifest lists three changed files, all included; I read the added lines in each. No changed files were excluded.

To get this to a good state

Nothing is required. If #10861 lands first, retain the address-size-32 target wrapping and regression coverage when integrating its classifier.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5956494183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20900#pullrequestreview-5956494183). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-18T12:25:16Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The reviewed change wraps address-size-32 LEA targets before classification; no blocking defect was verified, but I could not run the reported checks in this review environment. ## What I ran - Reviewed head `af78ab9a82fff70d12f26221743a070efaabd048` against the prepared diff; no merged-preview check was run. - Read all three changed files’ added lines: `core/relinker/CMakeLists.txt`, `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp`, and `core/relinker/relinker/tests/CodeInstructionCollectorAddressSizeTests.cpp`. - No commands or tests were run by me. The four CTests and control results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed-line review | passed | All changed lines read | | Build and CTests | not run | Author reports focused CTests passed | | Control test | not run | Author reports the control failed as expected | ## Negative control I could not run a negative control. The description reports that a control built from `main` failed on the new wrapping assertion. ## Findings ### Blocking None. ### Non-blocking - The description identifies an integration point with #10861: if that change lands first, carry this target truncation and regression test through its classifier. The current diff’s changed code applies the 32-bit truncation before existing classification. ## Sources - [Collector change at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/af78ab9a82fff70d12f26221743a070efaabd048/core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp#L335-L347) - [Regression tests at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/af78ab9a82fff70d12f26221743a070efaabd048/core/relinker/relinker/tests/CodeInstructionCollectorAddressSizeTests.cpp) ## Not verified No build or test execution, PS5 title, or console behavior was verified by me. The prepared manifest lists three changed files, all included; I read the added lines in each. No changed files were excluded. ## To get this to a good state Nothing is required. If #10861 lands first, retain the address-size-32 target wrapping and regression coverage when integrating its classifier.
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!20900
No description provided.