Skip to content

test(relinker): check MOVBE stores to RSP-relative memory and RSP operands - #22429

Closed
SchmashinPrunes wants to merge 2 commits into
boykopovar:mainfrom
SchmashinPrunes:test/movbe-stack-operands
Closed

SchmashinPrunes wants to merge 2 commits into
boykopovar:mainfrom
SchmashinPrunes:test/movbe-stack-operands

Conversation

@SchmashinPrunes

@SchmashinPrunes SchmashinPrunes commented Nov 17, 2026 •

Copy link
Copy Markdown

What

The MOVBE lowering test ran every form against [rsp+0x40] but checked the result only for loads: the shared lowering execution harness wrote the slot before the stub and never read it back, so a store to RSP-relative memory was not checked. Those stores are where the stub has to add its 160-byte spill frame back into the displacement. The test also skipped RSP as the register operand, which EmitMovbe handles in separate paths (lea of RSP for a store, _writeStackPointer for a load), so neither path ran.

  • LoweringExecution.hpp: State gets a StackMemory field, which the harness fills with the stub's [rsp+0x40] after it runs. It is added at the end, so existing offsets stay the same (checked with a static_assert), and the other five tests that use the harness are unchanged.
  • MovbeLoweringTests.cpp: stores to [rsp+0x40] are checked like the other stores. RSP now runs as the register operand for every width and addressing form. Loads are also checked to leave memory unchanged.
  • RedZoneFramerExecutionTests.cpp: the test executed its "leaf reading rsp through movbe" case on every host, so red_zone_framer_execution raised SIGILL on CPUs without MOVBE, such as Sandy Bridge, as the QEMU runs in the test report on this PR show. The leaf is now marked with hostHasMovbe(), as the BMI1 leaf is marked with hostHasBmi1(), and verifySkippedLeaves skips executing a leaf the host cannot run, as runLeaves already does. The framing checks still cover every leaf.
  • Debt: drops "A store to an RSP-relative operand is not covered by the execution test" from the MOVBE lowering entry, through a fragment.

Test-only change: MovbeLowering.cpp is not modified, and the new cases pass on it.

Tested

Debian (Linux x86-64), GCC 14.2, Ninja, Release, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON: ctest --test-dir build-relinker passed 966/966. No title tested.

red_zone_framer_execution under qemu-x86_64 10.0.13: before this PR, SIGILL at movbe [r9], rsp with -cpu SandyBridge and -cpu Westmere; after it, passes with -cpu SandyBridge (the MOVBE leaf is reported as not executed) and with -cpu Haswell and -cpu max (the leaf runs). It also passes natively.

To check that the new cases detect errors, I injected each of these faults into MovbeLowering.cpp and ran both this test and the one on main (not committed):

Fault in the lowering Test on main This PR
Store displacement ignores 8 bytes of the spill frame passes fails: MOVBE store 16 reg 0 tail 132: memory is …
Store of RSP stores RSP minus 8 passes fails: MOVBE store 16 reg 4 tail 6: memory is …
32/64-bit load into RSP reads the wrong result slot passes fails: MOVBE load 32 reg 4 tail 6: register 4 is …

python3 tools/check_conventions.py --base upstream/main passes, and python3 tools/pr_overlap.py --branch test/movbe-stack-operands reports no overlap.

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

🤖 Generated with Claude Code

…rands

The lowering execution harness now reads back the stack slot at
[rsp+0x40] after the stub runs, so the MOVBE test checks what a store
to RSP-relative memory writes instead of skipping it. The test also
runs RSP as the register operand, which the lowering handles in
separate paths for stores and loads, and checks that loads leave
memory unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion 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.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: movbe_lowering_tests built cleanly.
  • CTest (movbe_lowering):
    1/1 Test #5752: movbe_lowering ...................   Passed    0.08 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Checked LoweringExecution.hpp: State struct layout offsetof(State, StackMemory) == 50 * 8 and offsetof(State, InputFlags) == 49 * 8 verified by static_assert
    • Checked LoweringExecution.hpp stub epilogue assembly sequence: rip offset and memory move instructions correctly load and record [rsp+0x40] into State::StackMemory without clobbering other state fields
    • Checked MovbeLoweringTests.cpp: register exclusion (reg == 4 was RSP) removed, allowing reg 4 (RSP) to be exercised across 16-bit, 32-bit, and 64-bit widths and all addressing forms for both loads and stores
    • Checked MovbeLoweringTests.cpp: stack store verification trial.StackMemory is correctly tested against expected swapped value (swapped(trial.Input[reg], width)) and load invariance against initial memory value
    • Checked docs/dev/debt/changes/test-movbe-stack-operands.md technical debt fragment syntax and rules against AGENTS.md conventions

Looks Good To Me

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 267c1e0500, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

movbe_lowering, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@ycbuzi

ycbuzi commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Tested 267c1e0500 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • gha-linux-sandybridge: tests ran. 2 of the failing tests fail on main too.
  • gha-linux-westmere: tests ran. 6 of the failing tests fail on main too.
  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 6230; 99% tests passed, 4 tests failed out of 6230. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6230; 100% tests passed, 0 tests failed out of 6230. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

The skipped-leaf check executed "leaf reading rsp through movbe" on every
host, so red_zone_framer_execution raised SIGILL on CPUs without MOVBE,
such as Sandy Bridge. Mark the leaf with hostHasMovbe() as the BMI1 leaf
is marked with hostHasBmi1(), and have verifySkippedLeaves skip executing
a leaf the host cannot run, as runLeaves already does. The framing checks
still cover every leaf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SchmashinPrunes
SchmashinPrunes marked this pull request as draft November 18, 2026 16:33
@SchmashinPrunes

Copy link
Copy Markdown
Author

Thanks to @maskym and @ycbuzi for the runs.

The QEMU runs showed red_zone_framer_execution failing on main with -cpu SandyBridge and -cpu Westmere. I reproduced it with qemu-x86_64 10.0.13: the test executes its "leaf reading rsp through movbe" case on every host and stops with SIGILL at movbe [r9], rsp on CPUs without MOVBE. Since this PR is about MOVBE test coverage, I added 9ccd140 here: the leaf is marked with hostHasMovbe(), as the BMI1 leaf is marked with hostHasBmi1(), and verifySkippedLeaves skips executing a leaf the host cannot run, as runLeaves already does. The test now passes with -cpu SandyBridge, and still executes the leaf with -cpu Haswell, -cpu max and natively. The description is updated.

The amd64_only_converter segfault in the same runs has a different cause: QEMU user mode does not honour MAP_32BIT, so the 67h CLZERO stub case gets a buffer above 4 GiB and the stub's 32-bit address points at unmapped memory. It happens with -cpu max too, is unrelated to MOVBE, and is not changed here.

This comment was written by Claude Code, an AI assistant, at my request.

@SchmashinPrunes
SchmashinPrunes marked this pull request as ready for review November 18, 2026 16:33

@PrivacyOnion PrivacyOnion 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.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: movbe_lowering_tests, red_zone_framer_execution_tests built cleanly.
  • CTest (movbe_lowering, red_zone_framer_execution):
    1/2 Test #5888: movbe_lowering ...................   Passed    0.06 sec
    2/2 Test #5909: red_zone_framer_execution ........   Passed    0.06 sec
    100% tests passed, 0 tests failed out of 2
    
  • Code reading:
    • Verified LoweringExecution.hpp: State struct layout offsetof(State, InputFlags) == 49 * 8 and offsetof(State, StackMemory) == 50 * 8 confirmed by static_assert and matching the stub assembly offsets
    • Verified LoweringExecution.hpp stub epilogue correctly reads [rsp+0x40] from the simulated stack frame into State::StackMemory without clobbering caller-preserved registers or flags
    • Verified MovbeLoweringTests.cpp: exclusion for reg == 4 (RSP) was safely removed, enabling load and store coverage of RSP register operands across 16, 32, and 64-bit widths and addressing forms
    • Verified MovbeLoweringTests.cpp: stack store verification trial.StackMemory is correctly verified against swapped(trial.Input[reg], width) and loads verify that memory is left unaltered
    • Verified RedZoneFramerExecutionTests.cpp: hostHasMovbe() properly guards the MOVBE instruction sequence using __builtin_cpu_supports("movbe") so older hosts without MOVBE (e.g. Sandy Bridge) skip execution cleanly without SIGILL
    • Verified docs/dev/debt/changes/test-movbe-stack-operands.md technical debt fragment matches required format and adheres strictly to AGENTS.md rules

Looks Good To Me

@MarcZX10 MarcZX10 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 — looks good

The pinned head is 9ccd1407b9cd82088ae3bcd03337b9d65e48fd79; I inspected all four changed files, and the described test changes appear consistent with the harness and host-feature gating.

What I ran

No commands or tests were run. I read the prepared diff and the complete head text for all changed files.

Results

Check Result Note
Changed-file review passed All four changed files read
Tests not run Author-reported results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports passing tests and mutation checks, but I did not run them. review/files.json was not available through the reader, so excluded-file details were not independently checked; the manifest lists four changed files, and all four full texts were read.

To get this to a good state

Nothing is required.

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Retested head 9ccd1407b9 on Windows (the head moved since my earlier run), merged alone into main 6286d36449.

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. This PR adds no other build failure.

The tests this PR adds or extends, movbe_lowering, red_zone_framer_execution, pass 2/2, three runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@SchmashinPrunes

Copy link
Copy Markdown
Author

Re the overlap note on #18293: checked by hand. Both PRs touch RedZoneFramerExecutionTests.cpp, but in different functions. This PR changes hostHasMovbe, the MOVBE leaf in skippedDirectRspLeaves and verifySkippedLeaves. #18293 adds relocatedLeaves and changes load, runLeaves and main. git merge-tree merges the two heads (9ccd140 and #18293's current head) without conflicts, and none of the leaves #18293 adds uses MOVBE.

This comment was written by Claude Code, an AI assistant, at my request.

@SchmashinPrunes

Copy link
Copy Markdown
Author

Thanks to @maskym and @ycbuzi for the test runs on this one.

I'm closing it for now to free my one open PR slot for the FMA lowering under --to-avx, which I think matters more to the project. I'll reopen it, rebased, once I can have more than one PR open. The branch keeps the red_zone_framer_execution MOVBE guard (9ccd140) for the SIGILL that main shows under qemu-x86_64 -cpu SandyBridge, in case anyone needs it sooner.

This comment was written by Claude Code, an AI assistant, at my request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants