From afdbcbf33c7144a45023e8b277c5587ea54dd5ce Mon Sep 17 00:00:00 2001 From: HZY <63896554+huangzhuoyue@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:51:41 +0800 Subject: [PATCH] cpu_emulation: replace per-#UD VirtualQuery with MSVC SEH helpers --- runtime/src/cpu_emulation.h | 50 ++++++++++++++++++++++ runtime/test/cpu_emulation_test.cpp | 66 +++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index 8df3b55d0a..7da844749d 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h @@ -217,6 +217,48 @@ inline std::size_t readable(uint64_t addr, std::size_t want) { return got; } +#if defined(_MSC_VER) && !defined(__clang__) +inline LONG capture_exception_record(EXCEPTION_POINTERS* ep, EXCEPTION_RECORD* out) noexcept { + if (out && ep && ep->ExceptionRecord) *out = *ep->ExceptionRecord; + return EXCEPTION_EXECUTE_HANDLER; +} + +__declspec(noinline) inline void zero_64_volatile(volatile uint64_t* p) noexcept { + p[0] = 0; + p[1] = 0; + p[2] = 0; + p[3] = 0; + p[4] = 0; + p[5] = 0; + p[6] = 0; + p[7] = 0; +} + +inline bool zero_line_seh(uint64_t addr, EXCEPTION_RECORD* fault) noexcept { + __try { + zero_64_volatile(reinterpret_cast(addr)); + return true; + } __except (capture_exception_record(GetExceptionInformation(), fault)) { + return false; + } +} + +__declspec(noinline) inline uint8_t read_u8_volatile(const volatile uint8_t* p) noexcept { return *p; } + +inline std::size_t read_code_seh(uint64_t rip, uint8_t* out, std::size_t max_len) noexcept { + const auto* vp = reinterpret_cast(rip); + volatile std::size_t got = 0; + __try { + for (std::size_t i = 0; i < max_len; ++i) { + out[i] = read_u8_volatile(vp + i); + got = i + 1; + } + } __except (EXCEPTION_EXECUTE_HANDLER) { + } + return got; +} +#endif + inline uint64_t& lo(CONTEXT* c, int r) { return c->FltSave.XmmRegisters[r].Low; } inline uint64_t hi(const CONTEXT* c, int r) { return uint64_t(c->FltSave.XmmRegisters[r].High); } @@ -247,6 +289,9 @@ inline bool apply(const Decoded& d, CONTEXT* c, EXCEPTION_RECORD* fault = nullpt } case Op::clzero: { const uint64_t addr = (d.addr32 ? (c->Rax & 0xFFFFFFFFu) : c->Rax) & ~uint64_t(63); +#if defined(_MSC_VER) && !defined(__clang__) + if (!zero_line_seh(addr, fault)) return false; +#else // The line's page is checked first instead of catching a fault from the write: a compiler may turn the // write into inline stores that a local __try does not cover (clang-cl without /EHa). A 64-byte aligned // line never spans two pages, so one query answers for all of it. @@ -262,6 +307,7 @@ inline bool apply(const Decoded& d, CONTEXT* c, EXCEPTION_RECORD* fault = nullpt return false; } std::memset(reinterpret_cast(addr), 0, 64); +#endif break; } case Op::mwaitx: YieldProcessor(); break; @@ -354,8 +400,12 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; CONTEXT* c = info->ContextRecord; uint8_t code[15] = {}; +#if defined(_MSC_VER) && !defined(__clang__) + const std::size_t avail = read_code_seh(c->Rip, code, sizeof code); +#else const std::size_t avail = readable(c->Rip, sizeof code); // the instruction may end at a page boundary std::memcpy(code, reinterpret_cast(c->Rip), avail); +#endif const Decoded d = decode(code, avail); if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; EXCEPTION_RECORD fault; diff --git a/runtime/test/cpu_emulation_test.cpp b/runtime/test/cpu_emulation_test.cpp index f94f080443..5aaab08105 100644 --- a/runtime/test/cpu_emulation_test.cpp +++ b/runtime/test/cpu_emulation_test.cpp @@ -339,6 +339,71 @@ void test_live(const ce::Host& host) { Code ud({0x0F, 0x0B, 0xC3}); check(!run(ud, nullptr, nullptr, &ex) && ex == STATUS_ILLEGAL_INSTRUCTION, "ud2 still faults"); } + +void test_page_edge_faults() { + // Guard-page and read-only faults through apply(Op::clzero), plus code ending at an unmapped page boundary. + if (auto* page = static_cast(VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))) { + std::memset(page, 0xCC, 256); + DWORD old = 0; + if (VirtualProtect(page, 4096, PAGE_READONLY, &old)) { + CONTEXT c{}; + c.Rax = reinterpret_cast(page + 64 + 9); + const uint64_t rip = 0x1000; + c.Rip = rip; + EXCEPTION_RECORD fault{}; + check(!ce::apply(dec({0x0F, 0x01, 0xFC}), &c, &fault) && c.Rip == rip, + "clzero on read-only VirtualAlloc page is refused without advancing rip"); + check(fault.ExceptionCode == STATUS_ACCESS_VIOLATION && fault.NumberParameters == 2 && + fault.ExceptionInformation[0] == 1 && + fault.ExceptionInformation[1] == reinterpret_cast(page + 64), + "clzero on read-only page reports write fault at aligned line"); + } + if (VirtualProtect(page, 4096, PAGE_READWRITE | PAGE_GUARD, &old)) { + CONTEXT c{}; + c.Rax = reinterpret_cast(page + 128 + 5); + const uint64_t rip = 0x2000; + c.Rip = rip; + EXCEPTION_RECORD fault{}; + check(!ce::apply(dec({0x0F, 0x01, 0xFC}), &c, &fault) && c.Rip == rip, + "clzero on guard page raises fault without advancing rip"); + check(fault.ExceptionCode == STATUS_GUARD_PAGE_VIOLATION, + "clzero on guard page reports STATUS_GUARD_PAGE_VIOLATION"); + check(ce::apply(dec({0x0F, 0x01, 0xFC}), &c, &fault) && c.Rip == rip + 3, + "second clzero after guard clears succeeds and advances rip"); + bool line_zeroed = true; + for (int i = 128; i < 192; ++i) line_zeroed &= page[i] == 0; + check(line_zeroed && page[127] == 0xCC && page[192] == 0xCC, + "second clzero zeroes only [128, 192)"); + } + VirtualFree(page, 0, MEM_RELEASE); + } + + // Reserve two pages and commit only the first, so byte 4096 marks an unreadable boundary. + if (auto* span = static_cast(VirtualAlloc(nullptr, 8192, MEM_RESERVE, PAGE_NOACCESS))) { + if (VirtualAlloc(span, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE)) { + // mov rax, rcx; clzero; ret (7 bytes) ending right at span + 4096 + const uint8_t stub[] = {0x48, 0x89, 0xC8, 0x0F, 0x01, 0xFC, 0xC3}; + uint8_t* const entry = span + 4096 - sizeof stub; + std::memcpy(entry, stub, sizeof stub); + FlushInstructionCache(GetCurrentProcess(), entry, sizeof stub); +#if defined(_MSC_VER) && !defined(__clang__) + uint8_t fetched[15]{}; + check(ce::read_code_seh(reinterpret_cast(entry + 3), fetched, sizeof fetched) == 4 && + fetched[0] == 0x0F && fetched[1] == 0x01 && fetched[2] == 0xFC && fetched[3] == 0xC3, + "read_code_seh stops cleanly at unmapped page boundary"); + check(ce::read_code_seh(reinterpret_cast(span + 4096), fetched, sizeof fetched) == 0, + "read_code_seh returns 0 on unmapped page"); +#endif + alignas(64) uint8_t target[64]; + std::memset(target, 0x77, sizeof target); + reinterpret_cast(entry)(target, nullptr); + bool zeroed = true; + for (uint8_t b : target) zeroed &= b == 0; + check(zeroed, "emulated clzero at executable page boundary runs cleanly"); + } + VirtualFree(span, 0, MEM_RELEASE); + } +} } // namespace int main() { @@ -350,6 +415,7 @@ int main() { host.clwb); check(ce::install(), "install the handler"); test_live(host); + test_page_edge_faults(); if (failures) { std::fprintf(stderr, "%d failure(s)\n", failures); return 1;