Files
WoW-Hybrid-gateway/native/nohelper/direct_two_site_transaction.c

278 lines
11 KiB
C

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <stdint.h>
#include "direct_login_patch_plan.h"
#include "direct_two_site_transaction.h"
/* Native Win32 publication adapter; never called from DllMain.
* The snapshot excludes threads born later. This bounded quiescence is not
* a proof of process-wide thread-creation exclusion. The disposable acceptance
* must establish natural registration ordering, separately from this adapter.
*/
#define GW_MAX_THREADS 512u
#define GW_MAX_PAGES 12u
enum {
GW_PATCH_ARGUMENT = 1, GW_PATCH_SNAPSHOT = 2, GW_PATCH_THREAD_OPEN = 3,
GW_PATCH_SUSPEND = 4, GW_PATCH_CONTEXT = 5, GW_PATCH_PC = 6,
GW_PATCH_PREIMAGE = 7, GW_PATCH_PROTECT = 8, GW_PATCH_FLUSH = 9,
GW_PATCH_RESTORE = 10
};
struct GwPausedThread { HANDLE handle; DWORD id; int suspended; };
struct GwPatchPage { uintptr_t address; DWORD protection; int changed; };
static SRWLOCK gw_patch_lock = SRWLOCK_INIT;
/* Serialized scratch lives in owned module data, avoiding a large stack probe
* or heap use while other process threads are suspended. */
static struct GwPausedThread gw_patch_threads[GW_MAX_THREADS];
static struct GwPatchPage gw_patch_pages[GW_MAX_PAGES];
struct GwStartupAdmission gw_direct_admission;
static int gw_admission_refuse(uint32_t reason, uint32_t index,
uintptr_t address, uint32_t winerror,
uint32_t state, uint32_t protect)
{
gw_direct_admission.index = index;
gw_direct_admission.address = address;
gw_direct_admission.winerror = winerror;
gw_direct_admission.state = state;
gw_direct_admission.protect = protect;
MemoryBarrier();
gw_direct_admission.reason = reason;
return GW_PATCH_ARGUMENT;
}
static int gw_bytes_equal(uintptr_t address, const uint8_t *bytes, uint32_t n)
{
const volatile uint8_t *p = (const volatile uint8_t *)address;
uint32_t i;
for (i = 0; i < n; ++i) if (p[i] != bytes[i]) return 0;
return 1;
}
static void gw_bytes_write(uintptr_t address, const uint8_t *bytes, uint32_t n)
{
volatile uint8_t *p = (volatile uint8_t *)address;
uint32_t i;
for (i = 0; i < n; ++i) p[i] = bytes[i];
MemoryBarrier();
}
static void gw_patch_failstop(unsigned code)
{
/* Returning with partially published executable bytes is unsafe. */
TerminateProcess(GetCurrentProcess(), 0xe0475000u | code);
for (;;) Sleep(INFINITE);
}
static int gw_restore_pages(struct GwPatchPage *pages, uint32_t n, SIZE_T size)
{
uint32_t i;
int ok = 1;
for (i = n; i != 0; --i) {
DWORD ignored;
struct GwPatchPage *p = &pages[i - 1];
if (p->changed && !VirtualProtect((void *)p->address, size,
p->protection, &ignored)) ok = 0;
else p->changed = 0;
}
return ok;
}
static void gw_rollback(const struct GwStartupPatch *patches, uint32_t written,
struct GwPatchPage *pages, uint32_t page_count,
SIZE_T page_size)
{
uint32_t i;
/* Some protections may already have been restored: reopen all pages. */
for (i = 0; i < page_count; ++i) {
DWORD ignored;
if (!VirtualProtect((void *)pages[i].address, page_size,
PAGE_EXECUTE_READWRITE, &ignored))
gw_patch_failstop(1);
pages[i].changed = 1;
}
/* Restore installed sites in reverse publication order. */
while (written) {
const struct GwStartupPatch *p = &patches[--written];
gw_bytes_write(p->address, p->before, p->size);
}
if (!FlushInstructionCache(GetCurrentProcess(), NULL, 0))
gw_patch_failstop(2);
if (!gw_restore_pages(pages, page_count, page_size)) gw_patch_failstop(3);
}
int gw_direct_apply_two_patches(const struct GwStartupPatch *patches, uint32_t count)
{
struct GwPausedThread *threads = gw_patch_threads;
struct GwPatchPage *pages = gw_patch_pages;
uint32_t thread_count = 0, page_count = 0, written = 0, i, j;
DWORD process_id, own_id;
SYSTEM_INFO system_info;
SIZE_T page_size;
HANDLE snapshot = INVALID_HANDLE_VALUE;
THREADENTRY32 entry;
int result = 0;
gw_direct_admission.abi = 1;
gw_direct_admission.size = sizeof(gw_direct_admission);
gw_direct_admission.reason = 0;
gw_direct_admission.index = UINT32_MAX;
gw_direct_admission.address = 0;
gw_direct_admission.winerror = 0;
gw_direct_admission.state = 0;
gw_direct_admission.protect = 0;
gw_direct_admission.page_size = 0;
if (!patches || count < 1 || count > 2)
return gw_admission_refuse(1, count, 0, 0, 0, 0);
for (i = 0; i < count; ++i) {
if (!patches[i].address || patches[i].size != 5u ||
patches[i].address > UINTPTR_MAX - patches[i].size)
return gw_admission_refuse(2, i, patches[i].address, 0, 0, 0);
for (j = 0; j < i; ++j)
if (patches[i].address < patches[j].address + patches[j].size &&
patches[j].address < patches[i].address + patches[i].size)
return gw_admission_refuse(3, i, patches[i].address, 0, 0, 0);
}
AcquireSRWLockExclusive(&gw_patch_lock);
GetSystemInfo(&system_info);
page_size = system_info.dwPageSize;
gw_direct_admission.page_size = (uint32_t)page_size;
if (!page_size || (page_size & (page_size - 1))) {
result = gw_admission_refuse(4, 0, 0, 0, 0, 0); goto done;
}
for (i = 0; i < count; ++i) {
uintptr_t start = patches[i].address & ~((uintptr_t)page_size - 1);
uintptr_t end = (patches[i].address + patches[i].size - 1) &
~((uintptr_t)page_size - 1);
uintptr_t address;
for (address = start;; address += page_size) {
MEMORY_BASIC_INFORMATION info;
for (j = 0; j < page_count; ++j)
if (pages[j].address == address) break;
if (j == page_count) {
if (page_count == GW_MAX_PAGES) {
result = gw_admission_refuse(5, i, address, 0, 0, 0); goto done;
}
info.State = 0; info.Protect = 0;
if (VirtualQuery((const void *)address, &info, sizeof(info)) != sizeof(info)) {
result = gw_admission_refuse(6, i, address, GetLastError(),
info.State, info.Protect); goto done;
}
if (info.State != MEM_COMMIT) {
result = gw_admission_refuse(7, i, address, 0,
info.State, info.Protect); goto done;
}
if (info.Protect & PAGE_NOACCESS) {
result = gw_admission_refuse(8, i, address, 0,
info.State, info.Protect); goto done;
}
if (info.Protect & PAGE_GUARD) {
result = gw_admission_refuse(9, i, address, 0,
info.State, info.Protect); goto done;
}
pages[page_count].address = address;
pages[page_count].protection = info.Protect;
pages[page_count++].changed = 0;
}
if (address == end) break;
}
}
process_id = GetCurrentProcessId(); own_id = GetCurrentThreadId();
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshot == INVALID_HANDLE_VALUE) { result = GW_PATCH_SNAPSHOT; goto done; }
entry.dwSize = sizeof(entry);
if (!Thread32First(snapshot, &entry)) { result = GW_PATCH_SNAPSHOT; goto done; }
do {
if (entry.th32OwnerProcessID == process_id && entry.th32ThreadID != own_id) {
HANDLE h;
if (thread_count == GW_MAX_THREADS) { result = GW_PATCH_SNAPSHOT; goto done; }
h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT |
THREAD_QUERY_INFORMATION, FALSE, entry.th32ThreadID);
if (!h) { result = GW_PATCH_THREAD_OPEN; goto done; }
threads[thread_count].handle = h;
threads[thread_count].id = entry.th32ThreadID;
threads[thread_count++].suspended = 0;
}
entry.dwSize = sizeof(entry);
} while (Thread32Next(snapshot, &entry));
if (GetLastError() != ERROR_NO_MORE_FILES) { result = GW_PATCH_SNAPSHOT; goto done; }
CloseHandle(snapshot); snapshot = INVALID_HANDLE_VALUE;
/* From the first suspension until release: fixed stack data and resolved
* Win32 calls only; no heap allocation, module lookup, or loader operation. */
for (i = 0; i < thread_count; ++i) {
if (SuspendThread(threads[i].handle) == (DWORD)-1) {
result = GW_PATCH_SUSPEND; goto done;
}
threads[i].suspended = 1;
}
for (i = 0; i < thread_count; ++i) {
CONTEXT context;
uintptr_t pc;
context.ContextFlags = CONTEXT_CONTROL;
if (!GetThreadContext(threads[i].handle, &context)) {
result = GW_PATCH_CONTEXT; goto done;
}
#if defined(_WIN64)
pc = (uintptr_t)context.Rip;
#else
pc = (uintptr_t)context.Eip;
#endif
for (j = 0; j < count; ++j)
if (pc >= patches[j].address && pc < patches[j].address + patches[j].size) {
result = GW_PATCH_PC; goto done;
}
}
for (i = 0; i < count; ++i)
if (!gw_bytes_equal(patches[i].address, patches[i].before, patches[i].size)) {
result = GW_PATCH_PREIMAGE; goto done;
}
for (i = 0; i < page_count; ++i) {
DWORD old;
if (!VirtualProtect((void *)pages[i].address, page_size,
PAGE_EXECUTE_READWRITE, &old)) {
result = GW_PATCH_PROTECT;
if (!gw_restore_pages(pages, page_count, page_size)) gw_patch_failstop(4);
goto done;
}
pages[i].protection = old; pages[i].changed = 1;
}
for (i = 0; i < count; ++i) {
gw_bytes_write(patches[i].address, patches[i].after, patches[i].size);
++written;
}
if (!FlushInstructionCache(GetCurrentProcess(), NULL, 0)) {
result = GW_PATCH_FLUSH;
gw_rollback(patches, written, pages, page_count, page_size);
goto done;
}
if (!gw_restore_pages(pages, page_count, page_size)) {
result = GW_PATCH_RESTORE;
gw_rollback(patches, written, pages, page_count, page_size);
}
done:
/* One resume per successful suspension preserves previous suspend counts. */
for (i = thread_count; i != 0; --i) {
struct GwPausedThread *t = &threads[i - 1];
if (t->suspended && ResumeThread(t->handle) == (DWORD)-1) {
DWORD exit_code = STILL_ACTIVE;
if (!GetExitCodeThread(t->handle, &exit_code) || exit_code == STILL_ACTIVE)
gw_patch_failstop(5);
}
CloseHandle(t->handle);
}
if (snapshot != INVALID_HANDLE_VALUE) CloseHandle(snapshot);
ReleaseSRWLockExclusive(&gw_patch_lock);
return result;
}
int gw_direct_patch_retryable(int result, uint32_t admission_reason)
{
if (result == GW_PATCH_ARGUMENT)
return admission_reason >= 6 && admission_reason <= 9;
return result == GW_PATCH_SNAPSHOT || result == GW_PATCH_THREAD_OPEN ||
result == GW_PATCH_SUSPEND || result == GW_PATCH_CONTEXT ||
result == GW_PATCH_PC || result == GW_PATCH_PROTECT ||
result == GW_PATCH_FLUSH || result == GW_PATCH_RESTORE;
}