aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/game_host_test.c (9389B)


      1 #define WIN32_LEAN_AND_MEAN
      2 #include <windows.h>
      3 #include <shellapi.h>
      4 static HINSTANCE WINAPI fixture_shell(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT);
      5 static BOOL WINAPI fixture_shell_ex(SHELLEXECUTEINFOA *);
      6 static void WINAPI fixture_debug(LPCSTR);
      7 #define ShellExecuteA fixture_shell
      8 #define ShellExecuteExA fixture_shell_ex
      9 #define OutputDebugStringA fixture_debug
     10 #define main game_host_main
     11 #include "game_host.c"
     12 #undef main
     13 #undef ShellExecuteA
     14 #undef ShellExecuteExA
     15 #undef OutputDebugStringA
     16 
     17 static const char *debug_message;
     18 static DWORD debug_last_error;
     19 static void WINAPI fixture_debug(LPCSTR message)
     20 {
     21     debug_message = message;
     22     debug_last_error = GetLastError();
     23     SetLastError(987);
     24 }
     25 
     26 static char shell_file[MAX_PATH];
     27 static const char *shell_operation, *shell_parameters, *shell_directory;
     28 static HWND shell_window;
     29 static int shell_show;
     30 static HINSTANCE WINAPI fixture_shell(HWND window, LPCSTR operation, LPCSTR file,
     31                                      LPCSTR parameters, LPCSTR directory, INT show)
     32 {
     33     snprintf(shell_file, sizeof(shell_file), "%s", file);
     34     shell_window = window;
     35     shell_operation = operation;
     36     shell_parameters = parameters;
     37     shell_directory = directory;
     38     shell_show = show;
     39     return (HINSTANCE)77;
     40 }
     41 
     42 static BOOL WINAPI fixture_shell_ex(SHELLEXECUTEINFOA *information)
     43 {
     44     fixture_shell(information->hwnd, information->lpVerb, information->lpFile,
     45                   information->lpParameters, information->lpDirectory, information->nShow);
     46     information->hProcess = (HANDLE)0x1234;
     47     return TRUE;
     48 }
     49 
     50 static unsigned attached;
     51 static uint32_t reserved;
     52 static void fixture_dllmain(void)
     53 {
     54     attached++;
     55     reserved = MEM32(g_esp + 12);
     56     g_ecx = 0xbad;
     57     g_cur_func = 0x100012e1;
     58     g_st[0] = 99.0;
     59     g_eax = 1;
     60     g_esp += 16;
     61 }
     62 static void fixture_factory(void)
     63 {
     64     g_eax = 0xfeed1234;
     65     g_esp += 4;
     66 }
     67 const recomp_dispatch_entry_t recomp_dispatch_table[] = {
     68     {0x100012e1, fixture_dllmain}, {0x200c7e80, fixture_factory}
     69 };
     70 const uint32_t recomp_dispatch_count = 2;
     71 
     72 int main(int argc, char **argv)
     73 {
     74     /* Refusal cases run in separate processes because the guard terminates. */
     75     if (argc == 2) {
     76         if (!strcmp(argv[1], "module-factory")) {
     77             InitializeCriticalSection(&loader_lock);
     78             native32_init();
     79             int bbr = module_named("bbr.dll"), dr = module_named("dr.dll");
     80             module_span[bbr] = native32_map("client-target\\bbr.dll", 0x20000000);
     81             module_span[dr] = native32_map("client-target\\dr.dll", 0x21000000);
     82             if (!module_span[bbr] || !module_span[dr]) return 10;
     83             module_state[bbr] = 2;
     84             HANDLE first = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
     85             HANDLE second = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
     86             MODULEENTRY32 left = {0}, right = {0};
     87             left.dwSize = right.dwSize = sizeof(left);
     88 
     89             BOOL left_found = host_Module32First(first, &left);
     90             BOOL right_found = host_Module32First(second, &right);
     91             FARPROC address = host_GetProcAddress(left.hModule,
     92                 "?Instantiate@RTriMesh_t@@SAPAV1@PAVObjectArchive_c@fun@@@Z");
     93             void *(__cdecl *factory)(void *) = (void *)address;
     94             void *object = factory(NULL);
     95 
     96             if (!left_found || !right_found || left.hModule != (HMODULE)0x20000000 ||
     97                     right.hModule != left.hModule || strcmp(left.szModule, "bbr.dll") ||
     98                     left.modBaseSize != module_span[bbr] || (uintptr_t)address != 0x200c7e80 ||
     99                     object != (void *)0xfeed1234) return 11;
    100             if (GetModuleHandleA("bbr.dll") || GetModuleHandleA("dr.dll")) return 12;
    101             if (!host_Module32Next(first, &left) || !host_Module32Next(second, &right)) return 13;
    102             do {
    103                 if (left.hModule != right.hModule || left.hModule == (HMODULE)0x21000000) return 14;
    104                 left_found = host_Module32Next(first, &left);
    105                 right_found = Module32Next(second, &right);
    106                 if (left_found != right_found) return 15;
    107             } while (left_found);
    108             CloseHandle(first);
    109             CloseHandle(second);
    110             MEMORY_BASIC_INFORMATION protection;
    111             VirtualQuery((void *)0x200c7e80, &protection, sizeof(protection));
    112             if (protection.Protect != PAGE_READWRITE) return 16;
    113             puts("PASS: attached guest factory found through independent snapshots; native entries preserved; inactive guest excluded; original code NX");
    114             return 0;
    115         }
    116         if (!strcmp(argv[1], "debug-output")) {
    117             const char *message = "resource fixture diagnostic";
    118             SetLastError(777);
    119 
    120             host_OutputDebugStringA(message);
    121 
    122             if (debug_message != message || debug_last_error != 777 || GetLastError() != 987) return 9;
    123 
    124             EXCEPTION_RECORD record = {0};
    125             CONTEXT context = {0};
    126             EXCEPTION_POINTERS exception = {&record, &context};
    127             record.ExceptionCode = DBG_PRINTEXCEPTION_C;
    128 
    129             LONG disposition = host_fault(&exception);
    130 
    131             if (disposition != EXCEPTION_CONTINUE_SEARCH) return 10;
    132             puts("PASS: debug message and Windows last-error state forwarded");
    133             return 0;
    134         }
    135         if (!strcmp(argv[1], "fault-diagnostic")) {
    136             EXCEPTION_RECORD record = {0};
    137             CONTEXT context = {0};
    138             EXCEPTION_POINTERS exception = {&record, &context};
    139             record.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
    140             record.ExceptionAddress = (void *)host_fault;
    141             record.NumberParameters = 2;
    142             record.ExceptionInformation[0] = 1;
    143             record.ExceptionInformation[1] = 0xdeadbeef;
    144             context.Eip = (DWORD)(uintptr_t)host_fault;
    145             context.Eax = 0x55667788;
    146             g_cur_func = 0x11223344;
    147             g_esp = 0xdeaddead;
    148 
    149             LONG result = host_fault(&exception);
    150 
    151             if (result != EXCEPTION_CONTINUE_SEARCH || g_esp != 0xdeaddead) return 8;
    152             puts("PASS: diagnostic preserves exception dispatch and does not read guest stack");
    153             return 0;
    154         }
    155         if (!strcmp(argv[1], "vendor") || !strcmp(argv[1], "vendor-other-path")) {
    156             GetCurrentDirectoryA(sizeof(guest_directory), guest_directory);
    157             strcat(guest_directory, "\\client-target");
    158             char expected[MAX_PATH];
    159             snprintf(expected, sizeof(expected), "%s\\dxdiag.exe", guest_directory);
    160             const char *file = !strcmp(argv[1], "vendor") ? "dxdiag.exe" : "..\\dxdiag.exe";
    161 
    162             HINSTANCE result = host_ShellExecuteA((HWND)7, "open", file, "/t report.txt", guest_directory, SW_HIDE);
    163 
    164             if (result != (HINSTANCE)77 || strcmp(shell_file, expected) || shell_window != (HWND)7 ||
    165                     strcmp(shell_operation, "open") || strcmp(shell_parameters, "/t report.txt") ||
    166                     shell_directory != guest_directory || shell_show != SW_HIDE) return 6;
    167 
    168             SHELLEXECUTEINFOA information = {0};
    169             information.cbSize = sizeof(information);
    170             information.lpFile = file;
    171             information.lpDirectory = guest_directory;
    172             information.lpParameters = "/t other.txt";
    173             information.nShow = SW_SHOW;
    174 
    175             BOOL extended_result = host_ShellExecuteExA(&information);
    176 
    177             if (!extended_result || information.lpFile != file || information.hProcess != (HANDLE)0x1234 ||
    178                     strcmp(shell_file, expected) || strcmp(shell_parameters, "/t other.txt") || shell_show != SW_SHOW)
    179                 return 7;
    180             puts("PASS: local vendor path and native shell arguments preserved; no child launched");
    181             return 0;
    182         }
    183         if (!strcmp(argv[1], "create"))
    184             host_CreateProcessA("Clientr.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, NULL);
    185         if (!strcmp(argv[1], "shell"))
    186             host_ShellExecuteA(NULL, "open", "Clientr.exe", NULL, NULL, 0);
    187         if (!strcmp(argv[1], "spawn")) host__spawnv(0, "Clientr.exe", NULL);
    188         if (!strcmp(argv[1], "exec")) host__execv("Clientr.exe", NULL);
    189         if (!strcmp(argv[1], "system")) host_system("Clientr.exe");
    190         return 5;
    191     }
    192 
    193     InitializeCriticalSection(&loader_lock);
    194     native32_init();
    195     AddVectoredExceptionHandler(0, host_fault);
    196     int index = module_named("hr.dll");
    197     module_span[index] = native32_map("client-target\\hr.dll", 0x10000000);
    198     if (!module_span[index]) return 2;
    199     mach_enter();
    200     g_ecx = 0x12345678;
    201     g_cur_func = 0x11223344;
    202     g_st[0] = 3.5;
    203     g_fp_top = 1;
    204     uint32_t stack = g_esp;
    205     PUSH32(g_esp, (uintptr_t)"hr.dll");
    206     PUSH32(g_esp, RECOMP_RETADDR);
    207 
    208     shim_LoadLibraryA();
    209 
    210     int passed = attached == 1 && reserved == 0 && g_eax == 0x10000000 && g_ecx == 0x12345678 &&
    211                  g_cur_func == 0x11223344 && g_st[0] == 3.5 && g_fp_top == 1 && g_esp == stack;
    212     MEMORY_BASIC_INFORMATION protection;
    213     VirtualQuery((void *)0x100012e1, &protection, sizeof(protection));
    214     passed = passed && protection.Protect == PAGE_READWRITE;
    215     fprintf(stderr, "dynamic attach state: %s ecx=%08x cur=%08x fp=%g\n",
    216             passed ? "PASS" : "FAIL", g_ecx, g_cur_func, g_st[0]);
    217     mach_leave();
    218     return passed ? 0 : 4;
    219 }
    220