147 lines
7.3 KiB
C
147 lines
7.3 KiB
C
/* Q92 actual-C diagnostic boundary, synthetic buffers and Win32 calls only. */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
#include <tlhelp32.h>
|
|
#include "direct_two_site_transaction.h"
|
|
|
|
static _Alignas(4096) unsigned char page[4096];
|
|
static unsigned failures, checks;
|
|
#define CHECK(x) do { ++checks; if (!(x)) { ++failures; fprintf(stderr,"check failed line %u\n",(unsigned)__LINE__); } } while (0)
|
|
enum Mode { OK, SNAPSHOT, THREAD_OPEN, SUSPEND, CONTEXT_FAIL, PC, PREIMAGE,
|
|
PROTECT, FLUSH, RESTORE, NOACCESS, QUERY };
|
|
static enum Mode mode;
|
|
static unsigned locks, snapshots, handles, suspends, resumes, protects, flushes;
|
|
static DWORD protection, last_error;
|
|
static uintptr_t site;
|
|
|
|
void AcquireSRWLockExclusive(SRWLOCK *p) { (void)p; CHECK(locks == 0); ++locks; }
|
|
void ReleaseSRWLockExclusive(SRWLOCK *p) { (void)p; CHECK(locks == 1); --locks; }
|
|
void GetSystemInfo(SYSTEM_INFO *p) { p->dwPageSize = sizeof(page); }
|
|
SIZE_T VirtualQuery(const void *p, MEMORY_BASIC_INFORMATION *info, SIZE_T n) {
|
|
CHECK((uintptr_t)p == (uintptr_t)page); CHECK(n == sizeof(*info));
|
|
if (mode == QUERY) { last_error = 123; return 0; }
|
|
info->State = MEM_COMMIT; info->Protect = mode == NOACCESS ? PAGE_NOACCESS : protection;
|
|
return sizeof(*info);
|
|
}
|
|
DWORD GetLastError(void) { return last_error; }
|
|
DWORD GetCurrentProcessId(void) { return 11; }
|
|
DWORD GetCurrentThreadId(void) { return 22; }
|
|
HANDLE GetCurrentProcess(void) { return (HANDLE)(uintptr_t)1; }
|
|
HANDLE CreateToolhelp32Snapshot(DWORD flags, DWORD pid) {
|
|
CHECK(flags == TH32CS_SNAPTHREAD && pid == 0);
|
|
if (mode == SNAPSHOT) return INVALID_HANDLE_VALUE;
|
|
++snapshots; return (HANDLE)(uintptr_t)2;
|
|
}
|
|
BOOL Thread32First(HANDLE h, THREADENTRY32 *e) {
|
|
CHECK(h == (HANDLE)(uintptr_t)2 && e->dwSize == sizeof(*e));
|
|
e->th32OwnerProcessID = 11; e->th32ThreadID = 33; return 1;
|
|
}
|
|
BOOL Thread32Next(HANDLE h, THREADENTRY32 *e) {
|
|
(void)e; CHECK(h == (HANDLE)(uintptr_t)2); last_error = ERROR_NO_MORE_FILES; return 0;
|
|
}
|
|
HANDLE OpenThread(DWORD flags, BOOL inherit, DWORD id) {
|
|
CHECK(flags == (THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION));
|
|
CHECK(!inherit && id == 33);
|
|
if (mode == THREAD_OPEN) return NULL;
|
|
++handles; return (HANDLE)(uintptr_t)3;
|
|
}
|
|
DWORD SuspendThread(HANDLE h) {
|
|
CHECK(h == (HANDLE)(uintptr_t)3);
|
|
if (mode == SUSPEND) return (DWORD)-1;
|
|
++suspends; return 2; /* prior suspend count is not assumed zero */
|
|
}
|
|
DWORD ResumeThread(HANDLE h) { CHECK(h == (HANDLE)(uintptr_t)3); ++resumes; return 3; }
|
|
BOOL GetThreadContext(HANDLE h, CONTEXT *context) {
|
|
CHECK(h == (HANDLE)(uintptr_t)3 && context->ContextFlags == CONTEXT_CONTROL);
|
|
if (mode == CONTEXT_FAIL) return 0;
|
|
context->Rip = mode == PC ? site + 2 : site + 5; return 1;
|
|
}
|
|
BOOL GetExitCodeThread(HANDLE h, DWORD *code) { (void)h; *code = STILL_ACTIVE; return 1; }
|
|
BOOL CloseHandle(HANDLE h) {
|
|
if (h == (HANDLE)(uintptr_t)2) { CHECK(snapshots == 1); --snapshots; }
|
|
else { CHECK(h == (HANDLE)(uintptr_t)3 && handles == 1); --handles; }
|
|
return 1;
|
|
}
|
|
BOOL VirtualProtect(void *p, SIZE_T n, DWORD wanted, DWORD *old) {
|
|
CHECK(p == page && n == sizeof(page)); ++protects;
|
|
if ((mode == PROTECT && protects == 1) || (mode == RESTORE && protects == 2)) return 0;
|
|
*old = protection; protection = wanted; return 1;
|
|
}
|
|
BOOL FlushInstructionCache(HANDLE h, const void *p, SIZE_T n) {
|
|
CHECK(h == (HANDLE)(uintptr_t)1 && !p && !n); ++flushes;
|
|
return !(mode == FLUSH && flushes == 1);
|
|
}
|
|
BOOL TerminateProcess(HANDLE h, unsigned code) { (void)h; (void)code; exit(90); }
|
|
void Sleep(DWORD delay) { (void)delay; exit(91); }
|
|
|
|
#include "retirement.inc"
|
|
BOOL ReadProcessMemory(HANDLE process, const void *address, void *out, SIZE_T size, SIZE_T *got)
|
|
{ (void)process; memcpy(out, address, size); if (got) *got = size; return 1; }
|
|
|
|
static void reset(enum Mode which) {
|
|
mode = which; locks = snapshots = handles = suspends = resumes = protects = flushes = 0;
|
|
protection = PAGE_EXECUTE_READ; last_error = 0; site = (uintptr_t)(page + 64);
|
|
memset(page, 0, sizeof(page)); login_retired = 0;
|
|
memset(&gw_direct_retire_diagnostic, 0, sizeof(gw_direct_retire_diagnostic));
|
|
}
|
|
static struct GwStartupPatch patch(void) {
|
|
struct GwStartupPatch p = {0};
|
|
p.address = site; p.size = 5;
|
|
memcpy(p.before, "ABCDE", 5); memcpy(p.after, "FGHIJ", 5);
|
|
return p;
|
|
}
|
|
static void balanced(void) {
|
|
CHECK(!locks && !snapshots && !handles); CHECK(suspends == resumes);
|
|
CHECK(protection == PAGE_EXECUTE_READ);
|
|
}
|
|
static void transaction_case(enum Mode which, unsigned expected, unsigned reason) {
|
|
reset(which); struct GwStartupPatch p = patch();
|
|
memcpy((void *)site, p.before, 5); if (which == PREIMAGE) page[64] = 'X';
|
|
unsigned char original[5]; memcpy(original, (void *)site, 5);
|
|
int got = gw_direct_apply_two_patches(&p, 1);
|
|
CHECK(got == (int)expected); CHECK(gw_direct_admission.reason == reason);
|
|
CHECK(memcmp((void *)site, expected ? original : p.after, 5) == 0);
|
|
balanced();
|
|
printf("transaction mode=%u result=%d reason=%u\n", which, got, gw_direct_admission.reason);
|
|
}
|
|
static void retirement_case(enum Mode which, unsigned expected, unsigned reason) {
|
|
reset(which); struct GwDirectInstallation install = {0};
|
|
void *owner = page + 128;
|
|
install.abi = 1; install.size = sizeof(install); install.state = 1;
|
|
install.primary_thread = 22; install.owner = owner; install.original_owner_slot = &owner;
|
|
install.patches[0] = patch();
|
|
memcpy((void *)site, install.patches[0].after, 5);
|
|
if (which == PREIMAGE) page[64] = 'X';
|
|
unsigned char original[5]; memcpy(original, (void *)site, 5);
|
|
int got = gw_direct_retire_login_site_after_capture(&install, 9, owner);
|
|
CHECK(got == (expected ? (gw_direct_patch_retryable((int)expected, reason) ? -5 : -2) : 0));
|
|
CHECK(gw_direct_retire_diagnostic.transaction_result == expected);
|
|
CHECK(gw_direct_retire_diagnostic.reason == reason);
|
|
CHECK(gw_direct_retire_diagnostic.index == gw_direct_admission.index);
|
|
CHECK(gw_direct_retire_diagnostic.winerror == gw_direct_admission.winerror);
|
|
CHECK(gw_direct_retire_diagnostic.state == gw_direct_admission.state);
|
|
CHECK(gw_direct_retire_diagnostic.protect == gw_direct_admission.protect);
|
|
CHECK(login_retired == (expected == 0));
|
|
CHECK(memcmp((void *)site, expected ? original : install.patches[0].before, 5) == 0);
|
|
balanced();
|
|
printf("retirement mode=%u result=%d transaction=%u reason=%u\n", which, got,
|
|
gw_direct_retire_diagnostic.transaction_result, gw_direct_retire_diagnostic.reason);
|
|
/* Guard refusal must not leak a previous attempt's diagnostic. */
|
|
memset(&gw_direct_retire_diagnostic, 0xa5, sizeof(gw_direct_retire_diagnostic));
|
|
CHECK(gw_direct_retire_login_site_after_capture(NULL, 9, owner) == -1);
|
|
const unsigned char *bytes = (const unsigned char *)&gw_direct_retire_diagnostic;
|
|
for (size_t i = 0; i < sizeof(gw_direct_retire_diagnostic); ++i) CHECK(bytes[i] == 0);
|
|
}
|
|
int main(void) {
|
|
const unsigned results[] = {0,2,3,4,5,6,7,8,9,10,1,1};
|
|
for (unsigned i = 0; i < sizeof(results)/sizeof(results[0]); ++i) {
|
|
unsigned reason = i == NOACCESS ? 8 : i == QUERY ? 6 : 0;
|
|
transaction_case((enum Mode)i, results[i], reason);
|
|
retirement_case((enum Mode)i, results[i], reason);
|
|
}
|
|
printf("checks=%u failures=%u\n", checks, failures);
|
|
return failures ? 1 : 0;
|
|
}
|