Skip to content

test(relinker): execute the in-place Rosetta replacements - #8236

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
javierUDP:test/rosetta-inplace-execution
Oct 26, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
javierUDP:test/rosetta-inplace-execution

Conversation

@javierUDP

Copy link
Copy Markdown
Contributor

What

The --to-rosetta replacements for RDPID, RDSEED and CLWB are checked through their bytes, but none of them is ever executed, while the SSE4A, SHA, CLZERO, reciprocal, RDPRU, MCOMMIT and RDTSCP stubs are. All three of these raise an illegal instruction on a Rosetta host, which is the reason the replacements exist, so running them is what shows they stand in correctly.

The execution section now calls each one and checks what it leaves behind:

  • RDPID: the stub returns 0 in the destination register and does not disturb the flags.
  • RDSEED: the replacement is the same length, reports a value through the carry flag, and is retried up to 16 times so a transient RDRAND failure cannot make the test flaky.
  • CLWB: the replacement is the same length, leaves its address register and an unrelated register untouched, and does not disturb the flags.

Tested

Apple M5, macOS 26.4, AppleClang 21.0.0, relinker-only build of current main.

  • -DCMAKE_OSX_ARCHITECTURES=x86_64, executed under Rosetta: 209/209, with the three new checks running.
  • Native arm64: 209/209; the section is x86-64 only and stays out of the build.
  • Control: inverting each new assertion fails the run with that assertion's message, so the stubs really execute rather than being skipped. Reverting them restores the pass.
  • Measured on this host, for context: RDPID, RDSEED and CLWB each die with SIGILL when executed directly under Rosetta, while RDRAND and the NOP that replaces CLWB run.
  • python3 tools/check_conventions.py --base origin/main passes.

No Linux or Windows host was available; the section also builds there and the checks are host-independent.

#7999 moves in-place AMD-only replacements into a short instruction window and touches the same file. These checks exercise that path rather than change it, so there is no functional dependency either way.

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: n/a, test-only change
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: n/a
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

RDPID, RDSEED and CLWB are checked through their replacement bytes but
never run, although all three raise an illegal instruction on a Rosetta
host, which is what the replacements exist for. The x86-64 execution
section now calls the RDPID stub and the RDSEED and CLWB replacements
and checks the value, the carry flag and the registers they leave.
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 8042 core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp

@javierUDP

Copy link
Copy Markdown
Contributor Author

On the overlap notice: #7918 does not merge into main on its own. Merged into main 1ed63f63c by itself it conflicts in core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp, the same file the notice lists, so the conflict is between that pull request and main rather than with this one, and it needs a rebase either way.

This branch only appends three functions to the x86-64 execution section of that file and three calls in main(), so once #7918 is rebased the two should compose without touching the same lines.

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and CI results.

Code: Adds execution tests for three Rosetta in-place replacements that were previously only checked through byte comparison:

  • RDPID: maps the stub into executable memory, calls it, verifies the destination register receives 0 and the flags are preserved (CF checked via stc + setc). Correct — the trampoline stub for RDPID returns processor 0 as documented.
  • RDSEED: verifies the replacement has the same byte length as the original, executes it, and retries up to 16 times to account for transient RDRAND failures. The carry-flag check (produced = carry == 1) is the right way to detect a valid seed value.
  • CLWB: verifies same-length replacement, executes it, and checks that both the address register and an unrelated register (rbx = 0x5A...) are preserved, plus flags unchanged. Correct — CLWB should be a no-op from the caller's perspective.

All three follow the established pattern from rdtscpExecution() / mcommitExecution() above: mmap executable page, copy stub, inline asm call with sub $128, %%rsp for red-zone, check results, munmap.

The non-x86-64 stubs (void rdpidExecution() {} etc.) are correctly added in the #else branch to keep the test compilable on all platforms.

CI: All green — conventions, intake, labels, eligibility pass. The relinker tests themselves pass on all four platform/compiler combinations: macOS ARM64, macOS x86_64 (Rosetta — where these stubs are actually exercised), Ubuntu g++, and Windows g++.

PR body: Complete template, honest about the host used (Apple M5 under Rosetta). Notes the overlap with #7999 and correctly explains why there's no functional dependency.

No issues found. Well-structured test addition that fills a real coverage gap.

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent run on Apple silicon under Rosetta 2, which the thread had not covered yet.

Apple M2 Max, macOS 27.2 (26B5091g), Apple clang, CMake 4.4.4, Ninja, relinker-only Release build. Head d5886fce8 merged onto current main 0c6a5bd59 (clean). Built arm64 and x86-64 (-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3, Python 3.9.6).

  • x86-64 under Rosetta: amd64_only_converter passes. The three new execution tests run the converted in-place replacements as real code: RDPID leaves processor 0 in RAX and keeps the carry flag set, RDSEED reports a random value (CF set) within the 16 attempts, and CLWB is a no-op that leaves its address register, an unrelated register and the flags unchanged.
  • arm64: the same test compiles (the executions are __x86_64__-only no-ops there) and amd64_only_converter passes.
  • No production code changes, so the tests also pass on main; this is the added execution coverage running on an Apple host under Rosetta.

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

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@javierUDP main now adds wideStoreExecution at the insertion point in the converter test file, so the branch conflicts. Could you rebase while retaining wideStoreExecution and all three new execution functions as complete functions, all four #else stubs, and their calls after rdtscpExecution? Then rerun amd64_only_converter.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@javierUDP this conflicted with main in Amd64OnlyConverterTests.cpp, so I merged main into the branch (099b865) keeping both sets of tests (main's wideStore/xsaveopt/scalarCompare, then your rdpid/rdseed/clwb), plus a small follow-up commit (e27a9d3) that restores the function layout the merge had broken. Next is CI on this head.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026

@THEMANnt THEMANnt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed e27a9d3e01e51eb1cc549ba80ba1b2f1b356c83a. Approve; the earlier blocker is resolved.

The earlier merge-conflict blocker is cleared. Reviewed the complete current test-only patch: RDPID and CLWB checks verify relevant register/flag preservation, RDSEED checks the replacement length and bounded success, and inline calls protect the red zone. The new execution bodies remain guarded for Linux/macOS x86-64.

Validation: The exact current file compiles in C++23 with current matching relinker headers. Hosted Build 40256933244 logs show amd64_only_converter passes on Ubuntu (job 121360738660) and macOS x86-64 (job 121360738706); the respective full relinker CTest runs report 309 and 299 tests passing. Current Conventions passes.

Scope: Local Windows syntax does not execute the guarded bodies; execution evidence comes from the hosted merged-PR jobs. No game/performance or broad CPU-feature portability claim.

@oneandonlydean
oneandonlydean merged commit fb5afb3 into boykopovar:main Oct 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants