Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions runtime/src/cpu_emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,10 +188,12 @@ inline std::atomic<uint64_t>& 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<void*>(addr), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT)
return DWORD(STATUS_ACCESS_VIOLATION);
if (mbi.Protect & PAGE_GUARD) {
Expand All @@ -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<uint64_t>(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<void*>(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<void*>(cur), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT ||
(mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) || !mbi.Protect)
break;
const uint64_t end = reinterpret_cast<uint64_t>(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<uint64_t>(mbi.BaseAddress);
if (cur < base || cur - base >= mbi.RegionSize) break;
const uint64_t avail = mbi.RegionSize - (cur - base);
got += avail < want - got ? static_cast<std::size_t>(avail) : want - got;
}
return got;
}
Expand Down Expand Up @@ -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<void*>(start), 0, std::size_t(cur - start));
Expand All @@ -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<void*>(start), 0, std::size_t(total));
Expand Down
23 changes: 23 additions & 0 deletions runtime/test/cpu_emulation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 ----
Expand Down
Loading