Skip to content

cpu_ud_emulate: CLWB on an invalid address raises the access violation the hardware raises - #7692

Closed
tedin7 wants to merge 25 commits into
ProjectLeonida:mainfrom
tedin7:c2-clwb-fault
Closed

tedin7 wants to merge 25 commits into
ProjectLeonida:mainfrom
tedin7:c2-clwb-fault

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Compute the CLWB memory operand and report an access violation for inaccessible memory, preserving CLZERO write faults. Segment overrides are ignored.

Stacked on #7055, #7096 and #7098.

Why

The emulator previously skipped invalid CLWB operands; native Windows hardware raised an access violation in the #7098 review. The read access classification follows the requested contract; that review did not report its access-type field.

Tested

  • p2, clang-cl with Wine: cpu_ud_emulate_clwb_test: 43 checks, 0 failed; negative control before the fix: 38 checks, 8 failed.
  • p2, same environment: cpu_ud_emulate_test: 47 checks, 0 failed.
  • Wine skipped two page-boundary checks; the CPU supports CLWB and CLZERO, so their native #UD handler paths were skipped. No GPU test; this is CPU-only.

AI-assisted.

@ItsNotRexed: an exe PR, as you asked.

Claim: #6910 (comment)

Known limits (from the review): a CLWB with an FS/GS override is skipped without the readability check (the segment base is not known to the emulator); a guard page is reported as a plain access violation (as for CLZERO in #7055, the CPU would raise STATUS_GUARD_PAGE_VIOLATION); the 67 address-size prefix is not decoded. Stacked on #7055, #7096 and #7098.

A table-driven dispatcher for STATUS_ILLEGAL_INSTRUCTION (cpu_ud_emulate), installed at startup only when the host CPU has no CLZERO. Unknown instructions and unwritable targets pass on untouched.
# Conflicts:
#	runtime/README.md

@svds12343 svds12343 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Independent native Windows review of 0604a58367c41e65b0b26727f140bebfd86aa1e4: all four CPU suites pass; one low-priority exception-metadata discrepancy found by comparison with native CLWB.

Windows 11 Pro N build 26200, Intel Core i7-12700K, MSVC 19.44.35230, Windows SDK 10.0.26100, CMake 4.4.5 / Ninja 1.13.2, RelWithDebInfo. CPU-only; GPU/driver not involved. CPUID reports CLWB present and CLZERO/MONITORX/SSE4a absent.

Validation:

  • CMake build of cpu_ud_emulate_test, cpu_ud_emulate_clwb_test, cpu_ud_monitorx_test, cpu_ud_insertq_test; ctest --test-dir runtime/build-review -R '^cpu_ud' --output-on-failure -V: 4/4 pass. Direct suite totals: CLWB 50/50, CLZERO 47/47, MONITORX 10/10, INSERTQ 67/67.
  • The four truncated-instruction checks and the complete-at-page-end check run and pass on Windows. CLWB's real-#UD path is skipped because this CPU executes it natively; INSERTQ's comparison with native SSE4a is skipped because this CPU lacks SSE4a.
  • Negative control: the current CLWB test linked against the pre-fix implementation/header at 3b60f08c fails 14 of 50 checks. The PR implementation passes all 50.
  • A separate scratch program links the production cpu_ud_emulate.cpp, executes hand-built CLWB instructions natively, captures Windows' exception record and CONTEXT, then gives the same context to the emulator as a synthetic #UD. Low unmapped addresses, PAGE_NOACCESS and decommitted pages agree on read AV classification, exact fault address and unchanged CONTEXT. READWRITE, READONLY, EXECUTE and EXECUTE_READ pages succeed in both paths.
  • A deterministic 20,000-encoding sweep (seed 7692; ModRM/SIB, disp8/disp32, RIP-relative and optional REX) produced 16,467 canonical-address AVs whose code, parameter count, access kind, fault address and preserved CONTEXT all matched. Another 243 noncanonical-address AVs differ only in the metadata described below; the remaining 3,290 executions did not fault and are not counted as emulator comparisons. Counts depend on the process's actual register values/address layout; the minimal reproductions below are fixed.

Finding, low priority: runtime/src/cpu_ud_emulate.cpp:187-188 funnels noncanonical addresses through the same fault_at(address, false) path as ordinary inaccessible memory. On this native Windows/Intel host:

Hand-written instruction and operand Native ExceptionInformation[0:2] Emulator
66 0F AE 31 (clwb [rcx]), RCX=0x0000800000000000 {0, 0xffffffffffffffff} {0, 0x0000800000000000}
Same instruction, RCX=0xffff7fffffffffff {0, 0xffffffffffffffff} {0, 0xffff7fffffffffff}
66 0F AE 34 0C (clwb [rsp+rcx]), RCX=0x0000800000000000 {3, 0} {0, effective_address}

All raise STATUS_ACCESS_VIOLATION, with two parameters and RIP unchanged. The canonical control RCX=0xffffffffffffffff agrees in both paths. The random sweep's differences were 204 of the first kind and 39 of the RSP/RBP-based kind, with no other mismatches. Thus ordinary invalid accesses are fixed, but handlers inspecting exception parameters can still distinguish these malformed operands from native execution. This is a fidelity follow-up, not evidence of a valid-address regression; no retail occurrence was tested. The inline note marks the new branch.

Commands for the standard suites:

cmake -S runtime -B runtime/build-review -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build runtime/build-review --target cpu_ud_emulate_test cpu_ud_emulate_clwb_test cpu_ud_monitorx_test cpu_ud_insertq_test -j 8
ctest --test-dir runtime/build-review -R '^cpu_ud' --output-on-failure -V

The scratch oracle and negative control were built with cl /std:c++17 /W4 /EHsc /O2 /DWIN32_LEAN_AND_MEAN /DNOMINMAX, linking the respective production source. They compile without warnings. The CMake build has one C4244 warning in the unchanged CLZERO test at line 215.

Scope: synthetic code and memory only; no game files, GPU tests, full-runtime suite or translator changes. FS/GS, guard-page and address-size-prefix limitations already documented by the author were not re-reported. Rechecked the coordination board and unchanged PR head before publishing. AI-assisted (Codex).

Claim: #7694 (comment)

address += displacement;
}
MEMORY_BASIC_INFORMATION mbi{};
if (!canonical(address) || VirtualQuery(reinterpret_cast<const void*>(address), &mbi, sizeof mbi) != sizeof mbi ||

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Preserve native fault metadata for noncanonical CLWB operands

This branch gives noncanonical operands the same {0, address} parameters as an ordinary read page fault. In a native Windows 11 / i7-12700K comparison, clwb [rcx] with RCX=0x0000800000000000 instead reports STATUS_ACCESS_VIOLATION with {0, UINT64_MAX}; clwb [rsp+rcx] with the same RCX reports {3, 0}. The emulator preserves the exception code and RIP but changes the parameters visible to subsequent handlers. Consider separating noncanonical faults from the VirtualQuery failures (and retaining the addressing form needed for the stack-based case), with native-oracle regressions. This affects already-invalid operands only; all canonical fault comparisons passed. See the review body for exact encodings and controls.

@Ekabusto

Copy link
Copy Markdown

Tested 0604a5836 natively on Windows 11 10.0.26300, AMD Ryzen 9 5900X (Zen 3), MSVC 19.44.35230, RelWithDebInfo:
pass.

ctest -R cpu_ud: 4/4 passed (cpu_ud_emulate, cpu_ud_insertq, cpu_ud_monitorx, cpu_ud_emulate_clwb).

  • cpu_ud_emulate_clwb_test: 50 checks, 0 failed (43 under Wine in the PR). The page-boundary cases Wine
    skipped ran here: "CLWB cut off after 3/4/5/6 bytes by unreadable memory" and "ending at the last readable byte"
    all ok. One SKIP: "CLWB through the vectored handler: this CPU has CLWB, it ran natively".
  • cpu_ud_emulate_test: 47 checks, 0 failed. One SKIP: "CLZERO through the vectored handler: this CPU has CLZERO".
  • Build note: cpu_ud_emulate_test.cpp(215,42) gives MSVC warning C4244 (int → uint8_t), non-fatal at /W4.

So the native #UD handler path is still untested on real hardware: Zen 3 has both CLWB and CLZERO. It needs a CPU
without CLWB.

AI-assisted: yes (Claude ran the build and tests; results checked by me).

rndrmu added a commit to rndrmu/Leonida that referenced this pull request Nov 23, 2026
@memorypressure

Copy link
Copy Markdown

PASS with notes — but stack-bound. Tested head 0604a58 against main dde82f2: merges clean (tree be2dd8736). However, the heads of your three open bases — #7055 (e1a4764), #7096 (c96c285), #7098 (c011e26) — are all ancestors of this head (re-verified with merge-base at current heads), and none are in main (runtime/src/cpu_ud_emulate.cpp is absent; no CLWB/INSERTQ code anywhere on main). Merging this PR alone would land all three other PRs' contents at once — it should merge last, or the CLWB-delta commits (6b4ba67, 0604a58) rebase onto main once the bases land. The CLWB fix itself reviews well: fault-on-inaccessible-operand with CLZERO write faults preserved, Wine negative control 38/8 → 43/0, CI pending. C++ — not runnable here. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@bvtterbroot

Copy link
Copy Markdown

Tested 0604a58 on AMD Radeon RX 7900 XTX (RDNA3, Navi 31; native wave32 and wave64), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11, CPU AMD Ryzen 7 7800X3D (Zen 4): pass.

  • ctest --test-dir runtime/build -R cpu_ud: 4/4 passed (cpu_ud_emulate Passed, cpu_ud_insertq Passed, cpu_ud_monitorx Passed, cpu_ud_emulate_clwb Passed).

Tools: llvm-mc 21.1.8 (WSL), Vulkan SDK 1.4.363.0, MSVC 14.44.

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.

6 participants