From 7f56e6a50b45ea8d760d93df855b6acc04594f0c Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 19:39:50 +0100 Subject: [PATCH] runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation --- runtime/src/cpu_emulation.h | 47 ++++++++++++++++++++--------- runtime/test/cpu_emulation_test.cpp | 23 ++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index 8df3b55d0a..8e26eb1746 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h @@ -45,6 +45,8 @@ struct Decoded { // One of the encodings above at `code` (`avail` readable bytes), or Op::none. inline Decoded decode(const uint8_t* code, std::size_t avail) { + if (!code) return {}; + if (avail > 15) avail = 15; // architectural x86-64 maximum instruction length Decoded d; std::size_t i = 0; bool p66 = false, pf2 = false, pf3 = false; @@ -186,10 +188,12 @@ inline std::atomic& count(Op op) { return counts[int(op)]; } -// 0 when the page holding `addr` can be written; otherwise the exception a write raises there. A guard page loses +// 0 when the page holding `addr` can be written (and sets `region_end` to the end of the contiguous committed +// writable region returned by VirtualQuery); otherwise the exception a write raises there. A guard page loses // its guard on that first touch and reports STATUS_GUARD_PAGE_VIOLATION, as the system does for a real access. -inline DWORD writable(uint64_t addr) { - MEMORY_BASIC_INFORMATION mbi; +inline DWORD writable_region(uint64_t addr, uint64_t& region_end) { + region_end = addr; + MEMORY_BASIC_INFORMATION mbi{}; if (!VirtualQuery(reinterpret_cast(addr), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT) return DWORD(STATUS_ACCESS_VIOLATION); if (mbi.Protect & PAGE_GUARD) { @@ -200,19 +204,33 @@ inline DWORD writable(uint64_t addr) { const DWORD p = mbi.Protect & 0xFF; const bool ok = p == PAGE_READWRITE || p == PAGE_WRITECOPY || p == PAGE_EXECUTE_READWRITE || p == PAGE_EXECUTE_WRITECOPY; - return ok ? 0 : DWORD(STATUS_ACCESS_VIOLATION); + if (!ok) return DWORD(STATUS_ACCESS_VIOLATION); + const uint64_t base = reinterpret_cast(mbi.BaseAddress); + if (addr < base || addr - base >= mbi.RegionSize) return DWORD(STATUS_ACCESS_VIOLATION); + const uint64_t avail = mbi.RegionSize - (addr - base); + region_end = avail > UINT64_MAX - addr ? UINT64_MAX : addr + avail; + return 0; +} + +inline DWORD writable(uint64_t addr) { + uint64_t region_end = 0; + return writable_region(addr, region_end); } // How many of the `want` bytes at `addr` can be read, by page protection (no fault is taken to find out). inline std::size_t readable(uint64_t addr, std::size_t want) { std::size_t got = 0; while (got < want) { - MEMORY_BASIC_INFORMATION mbi; - if (!VirtualQuery(reinterpret_cast(addr + got), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT || + if (got > UINT64_MAX - addr) break; + const uint64_t cur = addr + got; + MEMORY_BASIC_INFORMATION mbi{}; + if (!VirtualQuery(reinterpret_cast(cur), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT || (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) || !mbi.Protect) break; - const uint64_t end = reinterpret_cast(mbi.BaseAddress) + mbi.RegionSize; - got += std::size_t(end - (addr + got)) < want - got ? std::size_t(end - (addr + got)) : want - got; + const uint64_t base = reinterpret_cast(mbi.BaseAddress); + if (cur < base || cur - base >= mbi.RegionSize) break; + const uint64_t avail = mbi.RegionSize - (cur - base); + got += avail < want - got ? static_cast(avail) : want - got; } return got; } @@ -315,15 +333,17 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, const uint64_t start = c->Rax; const uint64_t limit = gpr_read(c, limit_reg); - if ((start & 63) != 0 || limit <= start + 64) return ClzeroLoopResult::no_match; + if ((start & 63) != 0 || limit <= start || limit - start <= 64) return ClzeroLoopResult::no_match; - uint64_t total = (limit - start + 63) & ~uint64_t(63); + const uint64_t span = limit - start; constexpr uint64_t kMaxBatch = uint64_t(64) << 20; - if (total > kMaxBatch) total = kMaxBatch; + const uint64_t total = span >= kMaxBatch ? kMaxBatch : ((span + 63) & ~uint64_t(63)); + if (total > UINT64_MAX - start) return ClzeroLoopResult::no_match; const uint64_t end = start + total; for (uint64_t cur = start; cur < end;) { - const DWORD err = writable(cur); + uint64_t region_end = cur; + const DWORD err = writable_region(cur, region_end); if (err) { if (cur > start) { std::memset(reinterpret_cast(start), 0, std::size_t(cur - start)); @@ -339,8 +359,7 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, } return ClzeroLoopResult::faulted; } - const uint64_t next_page = (cur & ~uint64_t(0xFFF)) + 0x1000; - cur = next_page < end ? next_page : end; + cur = region_end < end ? region_end : end; } std::memset(reinterpret_cast(start), 0, std::size_t(total)); diff --git a/runtime/test/cpu_emulation_test.cpp b/runtime/test/cpu_emulation_test.cpp index f94f080443..cf9e756bfd 100644 --- a/runtime/test/cpu_emulation_test.cpp +++ b/runtime/test/cpu_emulation_test.cpp @@ -87,6 +87,11 @@ void test_decode() { check(dec({0x66, 0x0F, 0xAE, 0x39}).op == ce::Op::none, "66 0F AE /7 is clflushopt"); check(dec({0x66, 0x0F, 0xAE, 0x74, 0x01}).op == ce::Op::none, "a cut-off clwb is not decoded"); check(dec({0x0F, 0x0B}).op == ce::Op::none, "ud2 is not handled"); + check(ce::decode(nullptr, 15).op == ce::Op::none, "null code buffer is refused"); + // 14 prefixes + 3-byte CLZERO = 17 bytes (> 15-byte architectural limit) must not decode. + check(dec({0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, + 0x0F, 0x01, 0xFC}).op == ce::Op::none, + "overlong >15-byte clzero is refused"); } void test_bits() { @@ -195,6 +200,24 @@ void test_apply() { "clzero loop >64 MiB second batch completes with CF=0, ZF=1, SF=0"); VirtualFree(large, 0, MEM_RELEASE); } + + // Wraparound guards: start near UINT64_MAX and full-span [0, UINT64_MAX) must not wrap total to 0 or loop. + { + c = {}; + c.Rax = UINT64_MAX - 63; // 0xFFFFFFFFFFFFFFC0 (64-byte aligned); start + 64 would wrap to 0 + c.Rdx = UINT64_MAX; + c.Rip = rip; + check(ce::try_clzero_loop(loop12, sizeof loop12, &c, &fault) == ce::ClzeroLoopResult::no_match, + "clzero loop rejects wraparound start near UINT64_MAX"); + + c = {}; + c.Rax = 0; + c.Rdx = UINT64_MAX; // limit - start + 63 would wrap to 0 without kMaxBatch clamp first + c.Rip = rip; + check(ce::try_clzero_loop(loop12, sizeof loop12, &c, &fault) == ce::ClzeroLoopResult::faulted && + fault.ExceptionCode == STATUS_ACCESS_VIOLATION && fault.ExceptionInformation[1] == 0, + "clzero loop with UINT64_MAX limit faults on null page instead of wrapping total to 0"); + } } // ---- Running the real encodings ----