test(relinker): add C++ end-to-end fixture and --to-intel Linux stub test #431

Merged
adriadam10 merged 2 commits from upstream-pr/431 into main 2026-10-07 14:32:25 +00:00
adriadam10 commented 2026-10-03 05:57:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T05:57:25Z.


What

C++ infrastructure for end-to-end relinker tests, plus its first user.

  • core/relinker/tests/support/ElfFixture.hpp: MakeExecutable(code) builds a minimal PS5 executable (code PT_LOAD at vaddr 0, PT_DYNAMIC, spare program header slots), same layout the Python tests use. MappedImage maps the PT_LOAD segments of an output ELF with their own R/W/X permissions so its code can be called.
  • core/relinker/tests/support/RelinkerProcess.hpp: RunRelinker(relinker, args, log) (posix_spawn, exit code + combined output), TempDirectory, file helpers.
  • core/relinker/CMakeLists.txt: every core/relinker/tests/e2e/*.cpp becomes relinker_e2e_<Name> and receives $<TARGET_FILE:relinker>. New tests don't touch CMake. Registered on x86-64 Unix only, since the tests run relinked code.
  • e2e/ToIntelLinuxStubs.cpp: relinks an executable with one EXTRQ and one INSERTQ (immediate forms) with --skip-sce-module --to-intel, checks that both sites became jmp rel32 + NOP into a segment outside the original code, then calls the relinked function. The stubs run from their Linux segment and return through their branches. Until now the Linux placement was only covered by calling LinuxElfPatcher directly in Amd64OnlyConverterTests.cpp, without running the relinker or the stubs.

No change to the relinker itself.

Tested

Linux x86-64 (Ryzen 5 7535HS), full Release build, ctest 126/126.

  • The expected values were checked against the CPU: the same code before relinking, run natively (this CPU has SSE4a), matched the formula in the test on 10^6 random inputs.
  • The test fails if I break each piece it covers (patch applied, run, reverted):
    • return displacement of the stub +1: SIGSEGV
    • stub segment without PF_X: SIGSEGV
    • site not replaced by jmp: "SSE4a site was not replaced by a jump"
    • --to-intel conversion skipped in main.cpp: "Relinker did not report two stubs"
  • It passes on main. It adds coverage and doesn't fix a bug.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4724471036 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/431). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T05:57:25Z. --- ### What C++ infrastructure for end-to-end relinker tests, plus its first user. - `core/relinker/tests/support/ElfFixture.hpp`: `MakeExecutable(code)` builds a minimal PS5 executable (code PT_LOAD at vaddr 0, PT_DYNAMIC, spare program header slots), same layout the Python tests use. `MappedImage` maps the PT_LOAD segments of an output ELF with their own R/W/X permissions so its code can be called. - `core/relinker/tests/support/RelinkerProcess.hpp`: `RunRelinker(relinker, args, log)` (posix_spawn, exit code + combined output), `TempDirectory`, file helpers. - `core/relinker/CMakeLists.txt`: every `core/relinker/tests/e2e/*.cpp` becomes `relinker_e2e_<Name>` and receives `$<TARGET_FILE:relinker>`. New tests don't touch CMake. Registered on x86-64 Unix only, since the tests run relinked code. - `e2e/ToIntelLinuxStubs.cpp`: relinks an executable with one EXTRQ and one INSERTQ (immediate forms) with `--skip-sce-module --to-intel`, checks that both sites became `jmp rel32` + NOP into a segment outside the original code, then calls the relinked function. The stubs run from their Linux segment and return through their branches. Until now the Linux placement was only covered by calling `LinuxElfPatcher` directly in `Amd64OnlyConverterTests.cpp`, without running the relinker or the stubs. No change to the relinker itself. ### Tested Linux x86-64 (Ryzen 5 7535HS), full Release build, `ctest` 126/126. - The expected values were checked against the CPU: the same code before relinking, run natively (this CPU has SSE4a), matched the formula in the test on 10^6 random inputs. - The test fails if I break each piece it covers (patch applied, run, reverted): - return displacement of the stub +1: SIGSEGV - stub segment without `PF_X`: SIGSEGV - site not replaced by `jmp`: "SSE4a site was not replaced by a jump" - `--to-intel` conversion skipped in `main.cpp`: "Relinker did not report two stubs" - It passes on `main`. It adds coverage and doesn't fix a bug. ### 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
oneandonlydean commented 2026-10-04 07:10:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:10:27Z.


@boykopovar this one needs your call on test layout: it adds a new core/relinker/tests/{e2e,support} tree where every e2e/*.cpp is registered by file(GLOB ...), while the other relinker tests sit next to their component (relinker/tests/, codegen/tests/, elfpatcher/tests/) and are listed one by one in core/relinker/CMakeLists.txt.

The test itself is fine: I rebuilt it on today's main (after #432, #433 and #464) and it still passes, and the expected EXTRQ/INSERTQ values check out. My read: I'd keep the existing pattern (put it under relinker/tests/ and register it explicitly), but if you want a shared C++ end-to-end harness, this is a reasonable start for one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977588712 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/431#issuecomment-5977588712). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:10:27Z. --- @boykopovar this one needs your call on test layout: it adds a new `core/relinker/tests/{e2e,support}` tree where every `e2e/*.cpp` is registered by `file(GLOB ...)`, while the other relinker tests sit next to their component (`relinker/tests/`, `codegen/tests/`, `elfpatcher/tests/`) and are listed one by one in `core/relinker/CMakeLists.txt`. The test itself is fine: I rebuilt it on today's main (after #432, #433 and #464) and it still passes, and the expected EXTRQ/INSERTQ values check out. My read: I'd keep the existing pattern (put it under `relinker/tests/` and register it explicitly), but if you want a shared C++ end-to-end harness, this is a reasonable start for one.
adriadam10 commented 2026-10-07 14:08:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T14:08:08Z.


Followed @oneandonlydean's suggestion so this no longer needs a layout decision:

  • Moved the test next to the other relinker tests: core/relinker/relinker/tests/ToIntelLinuxStubsTests.cpp (+ ElfFixture.hpp, RelinkerProcess.hpp alongside), registered explicitly as to_intel_linux_stubs. No new tests/{e2e,support} tree, no file(GLOB).
  • Rebased on current main.

Rebuilt and ran the full ctest (397/397) locally. With the SSE4a stub body removed, the test fails ("computed the wrong value"). Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6039725061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/431#issuecomment-6039725061). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T14:08:08Z. --- Followed @oneandonlydean's suggestion so this no longer needs a layout decision: - Moved the test next to the other relinker tests: `core/relinker/relinker/tests/ToIntelLinuxStubsTests.cpp` (+ `ElfFixture.hpp`, `RelinkerProcess.hpp` alongside), registered explicitly as `to_intel_linux_stubs`. No new `tests/{e2e,support}` tree, no `file(GLOB)`. - Rebased on current `main`. Rebuilt and ran the full ctest (397/397) locally. With the SSE4a stub body removed, the test fails ("computed the wrong value"). Merging once CI is green.
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!431
No description provided.