cpu_emulation: replace per-#UD VirtualQuery with MSVC SEH helpers #1499

Closed
huangzhuoyue wants to merge 1 commit from upstream-pr/1499 into main
huangzhuoyue commented 2026-11-25 02:17:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by huangzhuoyue on 2026-11-25T02:17:33Z.


Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150481062

What and why

Replaces per-#UD VirtualQuery kernel transitions in the rt::cpu_emulation exception handler and single-line CLZERO emulation with structured MSVC SEH helpers when built with MSVC (keeping clang-cl and non-MSVC builds on the existing writable() / readable() path):

  • Adds read_code_seh() + __declspec(noinline) read_u8_volatile() under #if defined(_MSC_VER) && !defined(__clang__) to fetch up to 15 instruction bytes from c->Rip and stop cleanly at unreadable/unmapped page boundaries without calling VirtualQuery per instruction fault.
  • Adds zero_line_seh() + __declspec(noinline) zero_64_volatile() + capture_exception_record() so apply(Op::clzero) writes the aligned 64-byte line directly under __try/__except, preserving STATUS_ACCESS_VIOLATION / STATUS_GUARD_PAGE_VIOLATION reporting while letting XMem's VEH handle first-touch physical backing without a redundant VirtualQuery.
  • Keeps writable() and readable() defined unconditionally for try_clzero_loop() and non-MSVC/clang-cl builds, avoiding conflicts with open PRs #826 and #1472.
  • Adds targeted tests in runtime/test/cpu_emulation_test.cpp (test_page_edge_faults()) exercising apply(Op::clzero) on read-only and PAGE_GUARD pages as well as instruction fetches ending right at an unmapped page boundary (MEM_RESERVE).

Tested

  • Built cpu_emulation_test and gta6rt (RelWithDebInfo, MSVC 19.44.35216.0 / Ninja / Windows SDK 10.0.26100) cleanly on feat/cpu-emulation-msvc-seh (rebased cleanly onto origin/main 3ff2a2adc as afdbcbf33):
    • ctest --test-dir ... -C RelWithDebInfo --output-on-failure -R "^cpu_emulation$" -> 1/1 passed (0.10 s; Intel host SSE4a 0, MONITORX 0, CLZERO 0, CLWB 1, all checks passed).
    • PE .tls size on gta6rt.exe: 1681 bytes (<= 16384), 0 new thread_local, python -I scripts/check_no_game_data.py passed (exit 0).
  • Earlier verified the same SEH helper logic inside the full PR #984 integration runtime build (100b2952e + 034391e10):
    • Standalone cpu_emulation CTest passed and 181 s offline retail boot (run18, LEONIDA_DEVICE_TYPE=6, LEONIDA_ARENA_MB=2048) ran cleanly to game=3 session=3 with PresentX ok 1642, failed 0, 28/28 non-black captures, faults: none, and 0 guest exceptions.

GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU, driver 616.92, Windows 11 (Intel Core i7-14650HX, non-SSE4a/CLZERO host exercising emulated paths)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (git merge-tree --write-tree origin/main HEAD exits 0).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — runtime-only change)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5483861592 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1499). Originally posted by [huangzhuoyue](https://github.com/huangzhuoyue) on 2026-11-25T02:17:33Z. --- Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150481062 ### What and why Replaces per-`#UD` `VirtualQuery` kernel transitions in the `rt::cpu_emulation` exception handler and single-line `CLZERO` emulation with structured MSVC SEH helpers when built with MSVC (keeping clang-cl and non-MSVC builds on the existing `writable()` / `readable()` path): - Adds `read_code_seh()` + `__declspec(noinline)` `read_u8_volatile()` under `#if defined(_MSC_VER) && !defined(__clang__)` to fetch up to 15 instruction bytes from `c->Rip` and stop cleanly at unreadable/unmapped page boundaries without calling `VirtualQuery` per instruction fault. - Adds `zero_line_seh()` + `__declspec(noinline)` `zero_64_volatile()` + `capture_exception_record()` so `apply(Op::clzero)` writes the aligned 64-byte line directly under `__try`/`__except`, preserving `STATUS_ACCESS_VIOLATION` / `STATUS_GUARD_PAGE_VIOLATION` reporting while letting XMem's VEH handle first-touch physical backing without a redundant `VirtualQuery`. - Keeps `writable()` and `readable()` defined unconditionally for `try_clzero_loop()` and non-MSVC/clang-cl builds, avoiding conflicts with open PRs `#826` and `#1472`. - Adds targeted tests in `runtime/test/cpu_emulation_test.cpp` (`test_page_edge_faults()`) exercising `apply(Op::clzero)` on read-only and `PAGE_GUARD` pages as well as instruction fetches ending right at an unmapped page boundary (`MEM_RESERVE`). ### Tested - Built `cpu_emulation_test` and `gta6rt` (`RelWithDebInfo`, MSVC 19.44.35216.0 / Ninja / Windows SDK 10.0.26100) cleanly on `feat/cpu-emulation-msvc-seh` (rebased cleanly onto `origin/main` `3ff2a2adc` as `afdbcbf33`): - `ctest --test-dir ... -C RelWithDebInfo --output-on-failure -R "^cpu_emulation$"` -> `1/1 passed` (0.10 s; Intel host `SSE4a 0, MONITORX 0, CLZERO 0, CLWB 1`, all checks passed). - PE `.tls` size on `gta6rt.exe`: `1681` bytes (`<= 16384`), `0` new `thread_local`, `python -I scripts/check_no_game_data.py` passed (`exit 0`). - Earlier verified the same SEH helper logic inside the full PR `#984` integration runtime build (`100b2952e` + `034391e10`): - Standalone `cpu_emulation` CTest passed and `181 s` offline retail boot (`run18`, `LEONIDA_DEVICE_TYPE=6`, `LEONIDA_ARENA_MB=2048`) ran cleanly to `game=3 session=3` with `PresentX ok 1642, failed 0`, `28/28` non-black captures, `faults: none`, and `0` guest exceptions. GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU, driver 616.92, Windows 11 (Intel Core i7-14650HX, non-SSE4a/CLZERO host exercising emulated paths) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (`git merge-tree --write-tree origin/main HEAD` exits 0). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. *(n/a — runtime-only change)*
ytsodacan commented 2026-11-25 03:36:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ytsodacan on 2026-11-25T03:36:34Z.


[KEEP]

  • Scoped and plausible: runtime/src/cpu_emulation.h (MSVC-only SEH helpers, clang-cl/non-MSVC path unchanged) plus test_page_edge_faults() in runtime/test/cpu_emulation_test.cpp.
  • No CI checks have run on head afdbcbf3 (gh pr checks reports none), so the Windows build and cpu_emulation ctest are only verified by the author's local run.
  • Overlaps open #826 (CLZERO reserved XMem backing) and #1472 (CLWB rewrite at first #UD), which edit the same apply(Op::clzero) / handler code in the same two files. The description says conflicts are avoided, but the mergeable state is UNSTABLE and a maintainer should pick the merge order.
  • Needs CI green and a decision on ordering with #826/#1472 before merging.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7152074213 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1499#issuecomment-7152074213). Originally posted by [ytsodacan](https://github.com/ytsodacan) on 2026-11-25T03:36:34Z. --- **[KEEP]** - Scoped and plausible: `runtime/src/cpu_emulation.h` (MSVC-only SEH helpers, clang-cl/non-MSVC path unchanged) plus `test_page_edge_faults()` in `runtime/test/cpu_emulation_test.cpp`. - No CI checks have run on head `afdbcbf3` (`gh pr checks` reports none), so the Windows build and `cpu_emulation` ctest are only verified by the author's local run. - Overlaps open #826 (CLZERO reserved XMem backing) and #1472 (CLWB rewrite at first #UD), which edit the same `apply(Op::clzero)` / handler code in the same two files. The description says conflicts are avoided, but the mergeable state is `UNSTABLE` and a maintainer should pick the merge order. - Needs CI green and a decision on ordering with #826/#1472 before merging.
MoonEyes commented 2026-11-28 17:59:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:59:35Z.


Hi @huangzhuoyue, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/test/cpu_emulation_test.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243444846 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1499#issuecomment-7243444846). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:59:35Z. --- Hi @huangzhuoyue, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/test/cpu_emulation_test.cpp`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
huangzhuoyue commented 2026-11-28 18:07:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by huangzhuoyue on 2026-11-28T18:07:15Z.


Thanks for checking in! I don't plan to continue with this PR and will close it.

Now that main has clzero_patch.h, region-stepping writable_region() in try_clzero_loop, and in-place CLWB rewriting (#1472), the hot #UD paths this originally targeted are already addressed cleanly. Keeping a separate MSVC-only SEH path in cpu_emulation.h no longer justifies the extra maintenance complexity and nested-VEH / guard-page edge cases.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243545785 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1499#issuecomment-7243545785). Originally posted by [huangzhuoyue](https://github.com/huangzhuoyue) on 2026-11-28T18:07:15Z. --- Thanks for checking in! I don't plan to continue with this PR and will close it. Now that `main` has `clzero_patch.h`, region-stepping `writable_region()` in `try_clzero_loop`, and in-place CLWB rewriting (#1472), the hot `#UD` paths this originally targeted are already addressed cleanly. Keeping a separate MSVC-only SEH path in `cpu_emulation.h` no longer justifies the extra maintenance complexity and nested-VEH / guard-page edge cases.

Pull request closed

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
ProjectLeonida/mandy!1499
No description provided.