tools/client/eh_prolog_test.c (3334B)
1 #define WIN32_LEAN_AND_MEAN 2 #include <windows.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include "native32.h" 6 7 void native32_shim_EH_prolog(void); 8 const recomp_dispatch_entry_t recomp_dispatch_table[] = {{0, NULL}}; 9 const uint32_t recomp_dispatch_count = 0; 10 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 11 12 struct observation { 13 uint32_t before_sp, before_fs, after_sp, after_bp, after_ax; 14 uint32_t after_cx, after_dx, after_fs, words[4], below_sp; 15 }; 16 17 /* Call the actual vendor helper with its register ABI and restore the native 18 * exception chain and stack before returning to C. No game code executes. */ 19 __declspec(naked) static void native_observe(void *helper, uint32_t handler, 20 uint32_t old_bp, struct observation *result) 21 { 22 __asm { 23 push ebp 24 push ebx 25 push esi 26 push edi 27 mov esi, [esp + 20] 28 mov eax, [esp + 24] 29 mov ebp, [esp + 28] 30 mov edi, [esp + 32] 31 mov ebx, esp 32 mov [edi], esp 33 mov edx, fs:[0] 34 mov [edi + 4], edx 35 mov ecx, 0x55667788 36 mov edx, 0x99aabbcc 37 call esi 38 mov [edi + 8], esp 39 mov [edi + 12], ebp 40 mov [edi + 16], eax 41 mov [edi + 20], ecx 42 mov [edi + 24], edx 43 mov eax, fs:[0] 44 mov [edi + 28], eax 45 mov eax, [esp] 46 mov [edi + 32], eax 47 mov eax, [esp + 4] 48 mov [edi + 36], eax 49 mov eax, [esp + 8] 50 mov [edi + 40], eax 51 mov eax, [esp + 12] 52 mov [edi + 44], eax 53 mov eax, [esp - 4] 54 mov [edi + 48], eax 55 mov eax, [edi + 4] 56 mov fs:[0], eax 57 mov esp, ebx 58 pop edi 59 pop esi 60 pop ebx 61 pop ebp 62 ret 63 } 64 } 65 66 int main(void) 67 { 68 HMODULE crt = LoadLibraryA("msvcrt.dll"); 69 void *helper = (void *)GetProcAddress(crt, "_EH_prolog"); 70 uint32_t handler = (uintptr_t)GetProcAddress(crt, "_except_handler3"); 71 if (!helper || !handler) return 2; 72 native32_init(); 73 mach_enter(); 74 uint32_t stack = g_esp, previous_fs = MEM32(g_fs_base); 75 uint32_t old_frames[] = {0, 0x12345678, 0xfffffff0}; 76 for (unsigned i = 0; i < sizeof(old_frames) / sizeof(old_frames[0]); i++) { 77 struct observation native; 78 native_observe(helper, handler, old_frames[i], &native); 79 g_esp = stack; 80 g_ebp = old_frames[i]; 81 g_eax = handler; 82 g_ecx = 0x55667788; 83 g_edx = 0x99aabbcc; 84 MEM32(g_fs_base) = native.before_fs; 85 PUSH32(g_esp, native.after_ax); 86 87 native32_shim_EH_prolog(); 88 89 if (g_esp - stack != native.after_sp - native.before_sp || 90 g_ebp - stack != native.after_bp - native.before_sp || 91 g_eax != native.after_ax || g_ecx != native.after_cx || g_edx != native.after_dx || 92 MEM32(g_fs_base) != g_esp || native.after_fs != native.after_sp || 93 MEM32(g_esp - 4) != native.below_sp) return 3; 94 for (unsigned word = 0; word < 4; word++) 95 if (MEM32(g_esp + word * 4) != native.words[word]) return 4; 96 } 97 g_esp = stack; 98 MEM32(g_fs_base) = previous_fs; 99 mach_leave(); 100 puts("PASS: _EH_prolog matches native MSVCRT registers, stack frame and SEH chain (3 cases)"); 101 return 0; 102 }