client/runtime/guest_exception.c (2971B)
1 #include "guest_exception.h" 2 #include "guest_heap.h" 3 #include <pthread.h> 4 5 static uint32_t exception_data; 6 static pthread_once_t exception_once = PTHREAD_ONCE_INIT; 7 8 static void initialize_exception_data(void) 9 { 10 exception_data = guest_malloc(32); 11 if (!exception_data) abort(); 12 /* RTTI is not implemented; these two slots dispatch to native C adapters. */ 13 MEM32(exception_data) = 0; 14 MEM32(exception_data + 4) = 0xf1000000u; 15 MEM32(exception_data + 8) = 0xf1000004u; 16 memcpy((void *)ADDR(exception_data + 12), "Unknown exception", 18); 17 } 18 19 static uint32_t exception_vtable(void) 20 { 21 if (pthread_once(&exception_once, initialize_exception_data)) abort(); 22 return exception_data + 4; 23 } 24 25 static void construct(uint32_t object, uint32_t message, int owned) 26 { 27 MEM32(object) = exception_vtable(); 28 MEM32(object + 8) = 0; 29 if (message && owned) { 30 uint32_t size = strlen((const char *)ADDR(message)) + 1; 31 uint32_t copy = guest_malloc(size); 32 if (copy) { 33 memcpy((void *)ADDR(copy), (const void *)ADDR(message), size); 34 MEM32(object + 8) = 1; 35 } 36 message = copy; 37 } 38 MEM32(object + 4) = message; 39 } 40 41 static void exception_ctor(void) 42 { 43 construct(g_ecx, 0, 0); 44 g_eax = g_ecx; 45 g_esp += 4; 46 } 47 48 static void exception_message_ctor(void) 49 { 50 construct(g_ecx, MEM32(MEM32(g_esp + 4)), 1); 51 g_eax = g_ecx; 52 g_esp += 8; 53 } 54 55 static void exception_copy_ctor(void) 56 { 57 uint32_t source = MEM32(g_esp + 4); 58 construct(g_ecx, MEM32(source + 4), MEM32(source + 8) != 0); 59 g_eax = g_ecx; 60 g_esp += 8; 61 } 62 63 static void destroy(uint32_t object) 64 { 65 MEM32(object) = exception_vtable(); 66 if (MEM32(object + 8)) guest_free(MEM32(object + 4)); 67 } 68 69 static void exception_dtor(void) 70 { 71 destroy(g_ecx); 72 g_esp += 4; 73 } 74 75 static void exception_deleting_dtor(void) 76 { 77 uint32_t object = g_ecx, flags = MEM32(g_esp + 4); 78 if (flags & 2) { 79 uint32_t count = MEM32(object - 4); 80 while (count) destroy(object + --count * 12); 81 object -= 4; 82 } else destroy(object); 83 if (flags & 1) guest_free(object); 84 g_eax = object; 85 g_esp += 8; 86 } 87 88 static void exception_what(void) 89 { 90 exception_vtable(); 91 g_eax = MEM32(g_ecx + 4); 92 if (!g_eax) g_eax = exception_data + 12; 93 g_esp += 4; 94 } 95 96 recomp_func_t guest_exception_virtual(uint32_t address) 97 { 98 if (address == 0xf1000000u) return exception_deleting_dtor; 99 if (address == 0xf1000004u) return exception_what; 100 return NULL; 101 } 102 103 recomp_func_t guest_exception_lookup(const char *name) 104 { 105 if (!strcmp(name, "??0exception@@QAE@XZ")) return exception_ctor; 106 if (!strcmp(name, "??0exception@@QAE@ABQBD@Z")) return exception_message_ctor; 107 if (!strcmp(name, "??0exception@@QAE@ABV0@@Z")) return exception_copy_ctor; 108 if (!strcmp(name, "??1exception@@UAE@XZ")) return exception_dtor; 109 if (!strcmp(name, "?what@exception@@UBEPBDXZ")) return exception_what; 110 return NULL; 111 }