Repository navigation
Conversation
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.
…EH; fix the startup message quoting
…n any emulated instruction is missing
|
Tested |
|
Tested e658716 on RTX 5090, 621.24, Windows 11 (CPU-only tests): pass. |
|
Tested e658716 on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass
AI-assisted (Claude Code). |
|
Tested AMD Ryzen 7 5700X (Zen 3; has CLZERO, MONITORX/MWAITX, CLWB and SSE4a natively), CachyOS, Linux 7.3.5; runtime cross-compiled with clang-cl 23.1.5 + xwin (SDK 10.0.26100), tests run under Wine 11.22.
AI-assisted (Claude Code). |
throny
left a comment
There was a problem hiding this comment.
Tested 63d51ed on Windows 11, MSVC 14.44, i9-12900K (no SSE4a/CLZERO/MONITORX, has CLWB): pass, one fidelity gap below
This CPU has CLWB (CPUID 7.0 EBX[24] = 1 on all 24 logical CPUs). The real-#UD path for CLWB therefore cannot run here, and no negative control is possible for it. Instead I used the CPU as an oracle for the emulator's length decoding (below).
Commands (CMake + Ninja, RelWithDebInfo, VS 2022 BuildTools vcvars64, Vulkan SDK 1.4.363.0, -j 6):
- The build at 63d51ed succeeds.
- The PR's tests:
- Full
ctest -j 6with the Vulkan ICD hidden (no GPU on this run): 3 of 268 failed. They ared3d12_sparse_queue_family,vk_bindless_ps_texandvk_bindless_ps_imgatomic, atvkCreateInstance(-9) because I hid the driver. They fail the same way at the other heads tested here and are unrelated to this PR.
Length decoding against the CPU (scratch program linking this head's cpu_ud_emulate.cpp, not pushed). The setup is 20,000 random encodings 66 [REX 40-4F] 0F AE /6 with mod 0-2, random rm/SIB/displacement bytes and 15-byte windows. For each one, emulate() gives a length L. The CPU then executes the bytes placed so they end at a page boundary in front of a no-access page: with L bytes it must not fault fetching at the instruction start, and with L-1 bytes it must. Result: 20,000 of 20,000 lengths agree with the hardware, 0 declined. modrm_length (RIP-relative, SIB, SIB base 101 with mod 00, disp8/disp32) is right.
Finding: an invalid address does not fault (runtime/src/cpu_ud_emulate.cpp:105, emulate_clwb)
- Native CLWB here raises
STATUS_ACCESS_VIOLATIONforclwb [rcx]with RCX on aPAGE_NOACCESSpage, and also with RCX = 0x10. - The emulator, given the same instruction and RCX as a synthetic #UD, returns
EXCEPTION_CONTINUE_EXECUTIONand skips it. - A CPU without CLWB therefore runs past a CLWB that would crash on hardware. #7055's CLZERO path in the same file turns that case into the hardware's access violation.
- Only buggy code would hit this, so I'm not requesting changes. Matching it would need the effective address (the ModRM/SIB decode is already there) plus a
VirtualQueryreadability check that returnswrite_fault()-style AV information.
Nits
runtime/CMakeLists.txt:2952:cpu_ud_emulate_clwb_testis registered insideif(TARGET Vulkan::Headers)next toarena_size_test. A Windows build without Vulkan headers therefore drops this CPU-only test, whilecpu_ud_emulate_testandcpu_ud_monitorx_testsit in the plainif(BUILD_TESTING)Windows block.- Stack: this head merges #7096, and #7100 (5881905) merges this branch, so merge 7055 → 7096 → 7098 → 7100. #7111 covers CLWB with its own handler and conflicts with this branch in
runtime/src/main.cpp.
AI-assisted.
|
Thanks @throny for the 20,000-length oracle run: that is the check the decoder needed. Two points taken: the CMake registration of the CLWB test is being moved next to the other two CPU-only tests, and the merge order you list (7055 → 7096 → 7098 → 7100) is the intended one. The missing access violation for an invalid CLWB address stays a known gap: it needs the effective address from the ModRM/SIB decode plus a readability check, and CLWB on a bad address is only reachable by a title that would already crash on hardware. |
|
Tested
CLWB has no result that a program can see, thus a native comparison of its effect is not possible. I did not repeat the instruction-length oracle of @throny. |
Stacked on the CLZERO dispatcher PR (c2-ud-clzero).
What
Adds a CLWB emulator to the #UD dispatcher in
runtime/src/cpu_ud_emulate.cpp:66 [REX] 0F AE /6with a memory operand. CLWB writes a cache line back and changes nothing a program can see, so the emulation is a no-op that resumes after the instruction. One function, one line inkTable.The real work is the exact instruction length: prefix 66, optional REX,
0F AE, ModRM, optional SIB, disp8/disp32, RIP-relative disp32, and a SIB with no base (disp32). An instruction cut off by unreadable memory is not taken.Also:
host_has_clwb()(CPUID 7.0 EBX bit 24), andinstall()now installs the handler when the host lacks CLWB as well as CLZERO. New testcpu_ud_emulate_clwb_test(separate file and CMake block, to stay clear of the other stacked PRs), and a paragraph inruntime/README.md.Why
The executable can contain CLWB, which some desktop CPUs lack. On them it raises #UD and the game stops. Not emulated on purpose:
0F AE /7(CLFLUSH),66 0F AE /7(CLFLUSHOPT),0F AE /6without 66 (XSAVEOPT), and the register form (mod 11).@ItsNotRexed: an exe PR, as you asked.
Claim: #6910 (comment)
Tested
On the RTX 4090 pod (AMD EPYC, so CLWB runs natively there), cross-built and run under Wine:
cpu_ud_emulate_clwb_test.The test calls the emulation directly with a synthetic CONTEXT and EXCEPTION_RECORD. Expected lengths are written by hand from the Intel SDM encoding rules, not computed by the code.
kTableline disabled: 35 checks, 17 failed (all 16 CLWB addressing forms and the end-of-page case report "passed on").[rcx]4,[rax+rdx*4]5,[rax+rdx*4+8]6,[rax+disp32]8,[rip+disp32]8,[rsp]5,[rsp+0x20]6,[rbp+0]5,[rdx*4+disp32]9,[r13+0]6,[r12]6,[r12+0x10]7,[r8+r9*8+disp32]10, and REX.W / bare REX variants.66 0F AF, a NOP.cpu_ud_emulate_test(CLZERO): 19 checks, 0 failed.Built on #7055 (the CLZERO dispatcher): GitHub shows its commits too until it merges; this PR is the last commit. Known limits (from the review):
67and segment-override prefixes are not decoded, and unmapped or non-canonical operands are not faulted; the test target sits in the Vulkan-headers block of CMakeLists.txt.