Skip to content

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

Open
huangzhuoyue wants to merge 1 commit into
ProjectLeonida:mainfrom
huangzhuoyue:feat/cpu-emulation-msvc-seh
Open

huangzhuoyue wants to merge 1 commit into
ProjectLeonida:mainfrom
huangzhuoyue:feat/cpu-emulation-msvc-seh

Conversation

@huangzhuoyue

Copy link
Copy Markdown

Claimed in the coordination issue as: #14 (comment)

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)

@ytsodacan

ytsodacan commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

[KEEP]

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…h commit (#1707), MSVC SEH helpers (#1499)

Wave 9A (GPL-3.0 direct adaptations from ubiquitarianism/leonida):
- ProjectLeonida/mandy#742 (madhyn): coalesce contiguous physical-page runs into single MapViewOfFile3 views in XMemMapPhysicalPages and copy_page_mappings, splitting multi-page views on partial unmap/remap, coalescing split placeholders on remap, and avoiding O(N^2) splits during TitleVirtualFree window teardown. Preserves GPU-only <-> CPU-view pair mirroring, PageTrack write tracking, and pre/post unmap remap_epoch bumps.
- ProjectLeonida/mandy#1707 (Tortoche): 64-page-aligned (4 MiB) fault-ahead batch commit in commit_mapped_page with view-end clamping, single-page VirtualAlloc fallback, PageTrack committed/change propagation across every newly committed page in the batch, and PAGE_READONLY/PAGE_GUARD preservation (keeping tiny 1..3-page views demand-committed per page so sparse shadow/stamp tests do not over-commit neighbors).
- ProjectLeonida/mandy#1499 (huangzhuoyue): replace per-#UD VirtualQuery kernel transitions in rt::cpu_emulation::handler and apply(Op::clzero) with MSVC SEH helpers (read_code_seh / zero_line_seh) while retaining writable()/readable() for try_clzero_loop and non-MSVC/clang-cl builds.
- Tests: added test_coalesced_run_partial_unmap, test_fault_batch_commit, and test_page_edge_faults. Verified xmem (500 checks), cpu_emulation, d3d12_xmem_placed, and d3d12_xmem_shadow (4/4 CTest passed) + gta6rt build + check_no_game_data.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants