mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-12-02 09:32:27 +00:00
A title that zeroes memory with
clzero ; add rax, 0x40 ; cmp rax, <reg64> ; jb <the clzero>
raised one illegal-instruction exception per 64 bytes under Rosetta 2.
A project-authorized target build spent tens of seconds of its frame
loop there (more than two million exceptions in a run).
When the trapped CLZERO is followed by exactly that form, the handler
zeroes every remaining line below the limit and leaves RAX at the last
one, so the title's own add/cmp/jb leaves the loop. The range is zeroed
region by region and stops at memory that is not committed and writable;
the title's next CLZERO then traps and faults as before. Any other loop
form keeps the per-instruction path.
test_veh_clzero_loop runs its own machine-code loops, checks the memory
result line by line with sentinels, and counts illegal-instruction
exceptions with a handler registered ahead of BetterXen's: one per
recognised loop, one per line otherwise, three for a range that runs
into a read-only page. On the old handler five of its sixteen checks
fail.
Signed-off-by: ates <ahmetrizaates@hotmail.com>
200 lines
9.3 KiB
C++
200 lines
9.3 KiB
C++
// Synthetic Windows x64 fixture for CLZERO loops under the Rosetta exception path (#665).
|
|
//
|
|
// CLZERO (AMD APM vol. 3, 24594) zeroes the 64-byte cache line that contains the address in RAX
|
|
// and changes no register. Rosetta 2 does not run it, so bx_runtime_veh.dll emulates it from the
|
|
// illegal-instruction exception. A loop of the form
|
|
//
|
|
// clzero ; add rax, 0x40 ; cmp rax, <reg64> ; jb <the clzero>
|
|
//
|
|
// would raise one exception per line. The handler completes such a loop in the first exception.
|
|
//
|
|
// This fixture runs its own machine-code loops (bytes written here, no title code) and checks:
|
|
// * the memory result against the instruction's definition, line by line, with sentinels;
|
|
// * the number of illegal-instruction exceptions, counted by a handler of its own that runs
|
|
// before BetterXen's: one per loop that matches the form, one per line otherwise;
|
|
// * a range that runs into a read-only page: the loop stops there with a write fault at that
|
|
// page, and finishes once the page is writable.
|
|
// On a host that runs CLZERO natively no exception is raised and the fixture skips (exit 77).
|
|
#include <windows.h>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
extern "C" uint64_t bx_veh_get_clwb_trap_count(void); // imports bx_runtime_veh.dll, which installs the handler
|
|
|
|
namespace {
|
|
|
|
unsigned failures = 0;
|
|
unsigned checks = 0;
|
|
|
|
void expect(bool passed, const char* name) {
|
|
++checks;
|
|
if (passed) {
|
|
std::printf("ok: %s\n", name);
|
|
} else {
|
|
std::fprintf(stderr, "FAIL: %s\n", name);
|
|
++failures;
|
|
}
|
|
}
|
|
|
|
volatile LONG g_illegal = 0; // illegal-instruction exceptions seen first-chance
|
|
volatile LONG g_write_faults = 0; // write faults that reached the last handler
|
|
volatile ULONG_PTR g_fault_address = 0;
|
|
void* g_unprotect = nullptr; // page the last handler makes writable when it faults
|
|
SIZE_T g_unprotect_size = 0;
|
|
|
|
// Registered first: sees every illegal instruction before BetterXen's handler emulates it.
|
|
LONG CALLBACK count_first(PEXCEPTION_POINTERS ep) {
|
|
if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) InterlockedIncrement(&g_illegal);
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
|
|
// Registered last: sees the write fault BetterXen passes on when a line is not writable.
|
|
LONG CALLBACK fix_last(PEXCEPTION_POINTERS ep) {
|
|
const EXCEPTION_RECORD& r = *ep->ExceptionRecord;
|
|
if (r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION || r.NumberParameters < 2 || r.ExceptionInformation[0] != 1) {
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
InterlockedIncrement(&g_write_faults);
|
|
g_fault_address = r.ExceptionInformation[1];
|
|
DWORD old = 0;
|
|
if (!g_unprotect || !VirtualProtect(g_unprotect, g_unprotect_size, PAGE_READWRITE, &old)) {
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
g_unprotect = nullptr;
|
|
return EXCEPTION_CONTINUE_EXECUTION; // the CLZERO runs again
|
|
}
|
|
|
|
using Loop = void (*)(uint8_t* start, uint8_t* limit); // rcx = start, rdx = limit
|
|
|
|
// mov rax, rcx ; L: clzero ; add rax, 0x40 ; cmp rax, rdx ; jb L ; ret
|
|
const uint8_t kLoopRdx[] = {0x48, 0x89, 0xC8, 0x0F, 0x01, 0xFC, 0x48, 0x83, 0xC0, 0x40, 0x48, 0x39, 0xD0, 0x72, 0xF4, 0xC3};
|
|
// mov rax, rcx ; mov r10, rdx ; L: clzero ; add rax, 0x40 ; cmp rax, r10 ; jb L ; ret
|
|
const uint8_t kLoopR10[] = {0x48, 0x89, 0xC8, 0x49, 0x89, 0xD2, 0x0F, 0x01, 0xFC, 0x48, 0x83,
|
|
0xC0, 0x40, 0x4C, 0x39, 0xD0, 0x72, 0xF4, 0xC3};
|
|
// The same loop with a NOP before the add, which is not the recognised form:
|
|
// mov rax, rcx ; L: clzero ; nop ; add rax, 0x40 ; cmp rax, rdx ; jb L ; ret
|
|
const uint8_t kLoopOther[] = {0x48, 0x89, 0xC8, 0x0F, 0x01, 0xFC, 0x90, 0x48, 0x83, 0xC0, 0x40, 0x48, 0x39, 0xD0, 0x72, 0xF3, 0xC3};
|
|
|
|
Loop make(const uint8_t* code, size_t size) {
|
|
void* page = VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
|
if (!page) return nullptr;
|
|
std::memcpy(page, code, size);
|
|
FlushInstructionCache(GetCurrentProcess(), page, size);
|
|
return reinterpret_cast<Loop>(page);
|
|
}
|
|
|
|
constexpr size_t kLine = 64;
|
|
|
|
// Lines [first, first + count) of `base` are zero and every other line of the `lines`-line buffer
|
|
// still holds the fill byte.
|
|
bool only_zeroed(const uint8_t* base, size_t lines, size_t first, size_t count) {
|
|
for (size_t line = 0; line < lines; ++line) {
|
|
const uint8_t want = (line >= first && line < first + count) ? 0x00 : 0xAA;
|
|
for (size_t i = 0; i < kLine; ++i) {
|
|
if (base[line * kLine + i] != want) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
LONG run(Loop loop, uint8_t* start, uint8_t* limit) {
|
|
InterlockedExchange(&g_illegal, 0);
|
|
loop(start, limit);
|
|
return g_illegal;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
(void)bx_veh_get_clwb_trap_count();
|
|
void* first = AddVectoredExceptionHandler(1, count_first);
|
|
void* last = AddVectoredExceptionHandler(0, fix_last);
|
|
Loop loop_rdx = make(kLoopRdx, sizeof(kLoopRdx));
|
|
Loop loop_r10 = make(kLoopR10, sizeof(kLoopR10));
|
|
Loop loop_other = make(kLoopOther, sizeof(kLoopOther));
|
|
if (!first || !last || !loop_rdx || !loop_r10 || !loop_other) {
|
|
std::fprintf(stderr, "FAIL: setup\n");
|
|
return 1;
|
|
}
|
|
|
|
// 1,026 lines: a sentinel line, 1,024 lines to work in, a sentinel line.
|
|
constexpr size_t kLines = 1026;
|
|
auto* buffer = static_cast<uint8_t*>(VirtualAlloc(nullptr, kLines * kLine, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
|
|
if (!buffer) {
|
|
std::fprintf(stderr, "FAIL: buffer\n");
|
|
return 1;
|
|
}
|
|
uint8_t* work = buffer + kLine;
|
|
|
|
// One line: the loop body runs once.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
LONG illegal = run(loop_rdx, work, work + kLine);
|
|
if (illegal == 0) {
|
|
std::printf("SKIP: test_veh_clzero_loop: CLZERO raised no exception on this host (it runs natively)\n");
|
|
return 77;
|
|
}
|
|
expect(only_zeroed(buffer, kLines, 1, 1), "one line: exactly that line is zero");
|
|
expect(illegal == 1, "one line: one exception");
|
|
|
|
// 1,000 lines through the recognised form.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
illegal = run(loop_rdx, work, work + 1000 * kLine);
|
|
expect(only_zeroed(buffer, kLines, 1, 1000), "1000 lines (cmp rax, rdx): exactly those lines are zero");
|
|
expect(illegal == 1, "1000 lines (cmp rax, rdx): one exception for the whole loop");
|
|
|
|
// The limit register is r10.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
illegal = run(loop_r10, work, work + 1000 * kLine);
|
|
expect(only_zeroed(buffer, kLines, 1, 1000), "1000 lines (cmp rax, r10): exactly those lines are zero");
|
|
expect(illegal == 1, "1000 lines (cmp rax, r10): one exception for the whole loop");
|
|
|
|
// A limit one byte into an eleventh line: the loop runs while rax < limit, so eleven lines.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
illegal = run(loop_rdx, work, work + 10 * kLine + 1);
|
|
expect(only_zeroed(buffer, kLines, 1, 11), "limit inside a line: that line is zeroed too");
|
|
expect(illegal == 1, "limit inside a line: one exception");
|
|
|
|
// A start 8 bytes into a line: each CLZERO zeroes the whole line containing rax. With the limit
|
|
// at the end of the tenth line, rax takes ten values below it.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
illegal = run(loop_rdx, work + 8, work + 10 * kLine);
|
|
expect(only_zeroed(buffer, kLines, 1, 10), "unaligned start: ten whole lines are zero");
|
|
expect(illegal == 1, "unaligned start: one exception");
|
|
|
|
// A loop that is not the recognised form keeps the per-instruction path: same memory result,
|
|
// one exception per line.
|
|
std::memset(buffer, 0xAA, kLines * kLine);
|
|
illegal = run(loop_other, work, work + 8 * kLine);
|
|
expect(only_zeroed(buffer, kLines, 1, 8), "other loop form: exactly eight lines are zero");
|
|
expect(illegal == 8, "other loop form: one exception per line");
|
|
|
|
// A range that runs from a writable page into a read-only one. The lines of the first page are
|
|
// zeroed; the first line of the second page faults as a write to that page; once the last
|
|
// handler makes it writable the loop finishes.
|
|
auto* pages = static_cast<uint8_t*>(VirtualAlloc(nullptr, 2 * 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
|
|
if (!pages) {
|
|
std::fprintf(stderr, "FAIL: pages\n");
|
|
return 1;
|
|
}
|
|
std::memset(pages, 0xAA, 2 * 4096);
|
|
DWORD old = 0;
|
|
VirtualProtect(pages + 4096, 4096, PAGE_READONLY, &old);
|
|
g_unprotect = pages + 4096;
|
|
g_unprotect_size = 4096;
|
|
InterlockedExchange(&g_write_faults, 0);
|
|
g_fault_address = 0;
|
|
illegal = run(loop_rdx, pages + 4096 - 4 * kLine, pages + 4096 + 4 * kLine);
|
|
expect(g_write_faults == 1, "read-only page: one write fault is passed on");
|
|
expect(g_fault_address == reinterpret_cast<ULONG_PTR>(pages + 4096), "read-only page: the fault names the first line of that page");
|
|
expect(only_zeroed(pages, 2 * 4096 / kLine, 4096 / kLine - 4, 8), "read-only page: all eight lines are zero after it is made writable");
|
|
// First exception zeroes the four writable lines; the fifth line traps and faults; its retry
|
|
// traps again and finishes the range.
|
|
expect(illegal == 3, "read-only page: three exceptions (range start, the fault, the retry)");
|
|
|
|
RemoveVectoredExceptionHandler(last);
|
|
RemoveVectoredExceptionHandler(first);
|
|
std::printf("%s: test_veh_clzero_loop (%u of %u checks passed)\n", failures ? "FAIL" : "PASS", checks - failures, checks);
|
|
return failures ? 1 : 0;
|
|
}
|