client/tests/guest_exception.c (6230B)
1 #include "recomp_types.h" 2 #include "guest_memory.h" 3 #include <assert.h> 4 #ifdef _WIN32 5 #include <windows.h> 6 static HMODULE crt; 7 ptrdiff_t g_mem_base; 8 #else 9 #include "guest_exception.h" 10 #include "guest_heap.h" 11 #endif 12 13 static uint32_t call(const char *name, uint32_t function_address, uint32_t object, 14 unsigned count, uint32_t argument) 15 { 16 #ifdef _WIN32 17 void *function = name ? (void *)GetProcAddress(crt, name) : (void *)(uintptr_t)function_address; 18 assert(function); 19 if (count) return ((uint32_t (__thiscall *)(void *, uint32_t))function)((void *)(uintptr_t)object, argument); 20 return ((uint32_t (__thiscall *)(void *))function)((void *)(uintptr_t)object); 21 #else 22 recomp_func_t function = name ? guest_exception_lookup(name) : guest_exception_virtual(function_address); 23 assert(function); 24 g_esp = 0x5000ff00u; 25 g_ecx = object; 26 g_ebx = 0x12345678; 27 g_esi = 0x23456789; 28 g_edi = 0x3456789a; 29 g_ebp = 0x456789ab; 30 MEM32(g_esp) = RECOMP_RETADDR; 31 MEM32(g_esp + 4) = argument; 32 function(); 33 assert(g_esp == 0x5000ff04u + count * 4); 34 assert(g_ebx == 0x12345678 && g_esi == 0x23456789); 35 assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); 36 return g_eax; 37 #endif 38 } 39 40 static uint32_t allocate(unsigned size) 41 { 42 #ifdef _WIN32 43 return ((uint32_t (__cdecl *)(unsigned))GetProcAddress(crt, "malloc"))(size); 44 #else 45 return guest_malloc(size); 46 #endif 47 } 48 49 static void release(uint32_t address) 50 { 51 #ifdef _WIN32 52 ((void (__cdecl *)(uint32_t))GetProcAddress(crt, "free"))(address); 53 #else 54 guest_free(address); 55 #endif 56 } 57 58 static uint32_t vtable; 59 static uint32_t digest = 2166136261u; 60 static unsigned snapshots; 61 62 static void check(uint32_t object, const char *expected, unsigned owned) 63 { 64 assert(MEM32(object) == vtable); 65 assert(MEM32(object + 8) == owned); 66 assert(MEM32(object - 4) == 0xa5a5a5a5u && MEM32(object + 12) == 0xa5a5a5a5u); 67 uint32_t text = call("?what@exception@@UBEPBDXZ", 0, object, 0, 0); 68 assert(call(NULL, MEM32(vtable + 4), object, 0, 0) == text); 69 assert(!strcmp((const char *)ADDR(text), expected)); 70 digest = (digest ^ owned) * 16777619u; 71 for (unsigned i = 0; expected[i]; ++i) digest = (digest ^ (unsigned char)expected[i]) * 16777619u; 72 ++snapshots; 73 } 74 75 static void default_and_null_message(void) 76 { 77 memset((void *)ADDR(0x50001000u), 0xa5, 64); 78 MEM32(0x50002000u) = 0; 79 80 assert(call("??0exception@@QAE@XZ", 0, 0x50001004u, 0, 0) == 0x50001004u); 81 vtable = MEM32(0x50001004u); 82 assert(vtable); 83 assert(call("??0exception@@QAE@ABQBD@Z", 0, 0x50001024u, 1, 0x50002000u) == 0x50001024u); 84 85 assert(MEM32(0x50001008u) == 0 && MEM32(0x50001028u) == 0); 86 check(0x50001004u, "Unknown exception", 0); 87 check(0x50001024u, "Unknown exception", 0); 88 call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0); 89 call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0); 90 } 91 92 static void owned_messages_copy_independently(void) 93 { 94 for (unsigned length = 0; length < 256; ++length) { 95 char expected[256]; 96 for (unsigned i = 0; i < length; ++i) expected[i] = '!' + (i * 17 + length) % 90; 97 expected[length] = 0; 98 memcpy((void *)ADDR(0x50003000u), expected, length + 1); 99 MEM32(0x50002000u) = 0x50003000u; 100 memset((void *)ADDR(0x50001000u), 0xa5, 64); 101 102 assert(call("??0exception@@QAE@ABQBD@Z", 0, 0x50001004u, 1, 0x50002000u) == 0x50001004u); 103 assert(call("??0exception@@QAE@ABV0@@Z", 0, 0x50001024u, 1, 0x50001004u) == 0x50001024u); 104 memset((void *)ADDR(0x50003000u), 0x7f, length); 105 106 assert(MEM32(0x50001008u) != 0x50003000u); 107 assert(MEM32(0x50001008u) != MEM32(0x50001028u)); 108 check(0x50001004u, expected, 1); 109 check(0x50001024u, expected, 1); 110 call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0); 111 check(0x50001024u, expected, 1); 112 call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0); 113 } 114 } 115 116 static void borrowed_message_keeps_alias(void) 117 { 118 memset((void *)ADDR(0x50001000u), 0xa5, 64); 119 call("??0exception@@QAE@XZ", 0, 0x50001004u, 0, 0); 120 memcpy((void *)ADDR(0x50003000u), "borrowed", 9); 121 MEM32(0x50001008u) = 0x50003000u; 122 123 call("??0exception@@QAE@ABV0@@Z", 0, 0x50001024u, 1, 0x50001004u); 124 MEM8(0x50003000u) = 'B'; 125 126 assert(MEM32(0x50001028u) == 0x50003000u); 127 check(0x50001004u, "Borrowed", 0); 128 check(0x50001024u, "Borrowed", 0); 129 MEM32(0x50001004u) = 0x12345678u; /* A derived class installs its own vtable. */ 130 call("??1exception@@UAE@XZ", 0, 0x50001004u, 0, 0); 131 assert(MEM32(0x50001004u) == vtable); 132 check(0x50001024u, "Borrowed", 0); 133 call("??1exception@@UAE@XZ", 0, 0x50001024u, 0, 0); 134 assert(!strcmp((const char *)ADDR(0x50003000u), "Borrowed")); 135 } 136 137 static void virtual_destruction(void) 138 { 139 for (unsigned flags = 0; flags < 4; ++flags) { 140 unsigned count = flags & 2 ? 3 : 1; 141 uint32_t allocation = allocate(4 + count * 12); 142 assert(allocation); 143 uint32_t object = allocation + (flags & 2 ? 4 : 0); 144 if (flags & 2) MEM32(allocation) = count; 145 MEM32(0x50002000u) = 0x50003000u; 146 for (unsigned i = 0; i < count; ++i) 147 call("??0exception@@QAE@ABQBD@Z", 0, object + i * 12, 1, 0x50002000u); 148 149 uint32_t result = call(NULL, MEM32(vtable), object, 1, flags); 150 151 assert(result == allocation); 152 if (!(flags & 1)) { 153 for (unsigned i = 0; i < count; ++i) { 154 assert(MEM32(object + i * 12) == vtable); 155 assert(MEM32(object + i * 12 + 8) == 1); 156 } 157 release(allocation); 158 } 159 } 160 } 161 162 int main(void) 163 { 164 #ifdef _WIN32 165 crt = LoadLibraryA("msvcrt.dll"); 166 assert(crt); 167 assert(VirtualAlloc((void *)0x50000000u, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); 168 #else 169 assert(guest_map("exception fixture", 0x50000000u, 0x10000)); 170 #endif 171 default_and_null_message(); 172 owned_messages_copy_independently(); 173 borrowed_message_keeps_alias(); 174 virtual_destruction(); 175 assert(snapshots == 773 && digest == 0x421cabd1u); 176 printf("CRT exception: %u snapshots, digest %08x, message ownership, virtual calls and destruction passed\n", snapshots, digest); 177 return 0; 178 }