883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
104 lines
4.2 KiB
C
104 lines
4.2 KiB
C
/* Owned Win64 fixture for the lazily protected Realm seam page. A private
|
|
* MEM_MAPPED view stands in for the client page; a VEH plays the client's
|
|
* first-execution handler (RX on an in-page fault). No game code runs. */
|
|
#include <windows.h>
|
|
#include "../native/nohelper/realm_seam_page.c"
|
|
|
|
static unsigned char *view;
|
|
static volatile LONG handler_hits, decrypt_enabled = 1;
|
|
|
|
static void check(int value, unsigned code) { if (!value) ExitProcess(code); }
|
|
|
|
static LONG WINAPI lazy_handler(EXCEPTION_POINTERS *info)
|
|
{
|
|
uintptr_t at = (uintptr_t)info->ExceptionRecord->ExceptionAddress;
|
|
DWORD old;
|
|
if (info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION ||
|
|
at < (uintptr_t)view || at >= (uintptr_t)view + 4096 || !decrypt_enabled)
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
InterlockedIncrement(&handler_hits);
|
|
VirtualProtect(view, 4096, PAGE_EXECUTE_READ, &old);
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
}
|
|
|
|
static void probe(uintptr_t address, size_t bytes, struct GwRealmInstallDiagnostic *out)
|
|
{
|
|
MEMORY_BASIC_INFORMATION m;
|
|
SIZE_T q = VirtualQuery((const void *)address, &m, sizeof(m));
|
|
out->page_query_bytes = (uint32_t)q;
|
|
if (!q) return;
|
|
out->page_state = m.State;
|
|
out->page_protect = m.Protect;
|
|
out->page_type = m.Type;
|
|
if (m.AllocationBase == view) out->page_identity_bits |= 3u;
|
|
if (address >= (uintptr_t)m.BaseAddress &&
|
|
address + bytes <= (uintptr_t)m.BaseAddress + m.RegionSize)
|
|
out->page_identity_bits |= 4u;
|
|
}
|
|
|
|
static void protect(DWORD value)
|
|
{
|
|
DWORD old;
|
|
check(VirtualProtect(view, 4096, value, &old), 90);
|
|
}
|
|
|
|
void fixture_start(void)
|
|
{
|
|
struct GwRealmInstallDiagnostic page;
|
|
volatile LONG once = 0;
|
|
uintptr_t site, stub;
|
|
int touched;
|
|
HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
|
|
PAGE_EXECUTE_READWRITE, 0, 4096, NULL);
|
|
check(section != NULL, 2);
|
|
view = MapViewOfFile(section, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 4096);
|
|
check(view != NULL, 3);
|
|
site = (uintptr_t)view + 0x8ad;
|
|
stub = (uintptr_t)view + 0x8b2;
|
|
/* Pinned seam epilogue: add rsp,0x40; pop rdi; ret. */
|
|
{
|
|
static const unsigned char epilogue[6] = {0x48,0x83,0xc4,0x40,0x5f,0xc3};
|
|
unsigned i;
|
|
for (i = 0; i < sizeof(epilogue); ++i) view[0x8ad + i] = epilogue[i];
|
|
}
|
|
check(AddVectoredExceptionHandler(1, lazy_handler) != NULL, 4);
|
|
|
|
/* 1: already executable page needs no touch. */
|
|
protect(PAGE_EXECUTE_READ);
|
|
check(gw_realm_seam_page_prepare(site, 16, stub, probe, &once, &page, &touched) == 1, 10);
|
|
check(!touched && !once && !handler_hits && page.page_protect == PAGE_EXECUTE_READ, 11);
|
|
|
|
/* 2: lazily protected page: one in-page ret lets the handler admit it. */
|
|
protect(PAGE_NOACCESS);
|
|
check(gw_realm_seam_page_prepare(site, 16, stub, probe, &once, &page, &touched) == 1, 20);
|
|
check(touched && once == 1 && handler_hits == 1, 21);
|
|
check(page.page_protect == PAGE_EXECUTE_READ && page.page_type == MEM_MAPPED &&
|
|
page.page_identity_bits == 7u, 22);
|
|
check(view[0x8b2] == 0xc3 && view[0x8ad] == 0x48, 23);
|
|
|
|
/* 3: re-protected in the same generation: no second touch, defer. */
|
|
protect(PAGE_NOACCESS);
|
|
check(gw_realm_seam_page_prepare(site, 16, stub, probe, &once, &page, &touched) == 2, 30);
|
|
check(!touched && handler_hits == 1 && page.page_protect == PAGE_NOACCESS, 31);
|
|
|
|
/* 4: new generation re-arms exactly one touch. */
|
|
once = 0;
|
|
check(gw_realm_seam_page_prepare(site, 16, stub, probe, &once, &page, &touched) == 1, 40);
|
|
check(touched && handler_hits == 2, 41);
|
|
|
|
/* 5: stub outside the seam page is never executed. */
|
|
protect(PAGE_NOACCESS);
|
|
once = 0;
|
|
check(gw_realm_seam_page_prepare(site, 16, stub + 4096, probe, &once, &page, &touched) == 2, 50);
|
|
check(!touched && !once && handler_hits == 2, 51);
|
|
|
|
/* 6: no probe or output fails closed (deferred), nothing executed. */
|
|
check(gw_realm_seam_page_prepare(site, 16, stub, NULL, &once, &page, &touched) == 2, 60);
|
|
check(!touched && !once && handler_hits == 2, 61);
|
|
|
|
protect(PAGE_EXECUTE_READ);
|
|
UnmapViewOfFile(view);
|
|
CloseHandle(section);
|
|
ExitProcess(0);
|
|
}
|