client/tests/rtti.c (9979B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #define WIN32_LEAN_AND_MEAN 7 #include <windows.h> 8 #define MEM32(a) (*(uint32_t *)(uintptr_t)(a)) 9 #define ADDR(a) ((void *)(uintptr_t)(a)) 10 static void *(__cdecl *native_cast)(void *, int, void *, void *, int); 11 static void *(__cdecl *native_typeid)(void *); 12 #else 13 #include "guest_rtti.h" 14 #endif 15 16 static void type(unsigned index, const char *name) 17 { 18 strcpy((char *)ADDR(0x50001008 + index * 64), name); 19 } 20 21 static void base(unsigned index, unsigned type_index, unsigned children, int offset, int vbptr, int vbslot) 22 { 23 uint32_t descriptor = 0x50002000 + index * 32; 24 MEM32(0x50003000 + index * 4) = descriptor; 25 MEM32(descriptor) = 0x50001000 + type_index * 64; 26 MEM32(descriptor + 4) = children; 27 MEM32(descriptor + 8) = offset; 28 MEM32(descriptor + 12) = vbptr; 29 MEM32(descriptor + 16) = vbslot; 30 MEM32(descriptor + 20) = 0; 31 } 32 33 static void layout(unsigned count, unsigned attributes, unsigned source_offset) 34 { 35 memset((void *)ADDR(0x50002000), 0, 0x6000); 36 MEM32(0x50004004) = attributes; 37 MEM32(0x50004008) = count; 38 MEM32(0x5000400c) = 0x50003000; 39 MEM32(0x50005004) = source_offset; 40 MEM32(0x5000500c) = 0x50001000; 41 MEM32(0x50005010) = 0x50004000; 42 MEM32(0x50006000) = 0x50005000; 43 MEM32(0x50007000 + source_offset) = 0x50006004; 44 } 45 46 static uint32_t cast(uint32_t input, int delta, unsigned source, unsigned target, int reference) 47 { 48 #ifdef _WIN32 49 return (uintptr_t)native_cast((void *)(uintptr_t)input, delta, 50 (void *)(uintptr_t)(0x50001000 + source * 64), 51 (void *)(uintptr_t)(0x50001000 + target * 64), reference); 52 #else 53 g_esp = 0x5000ff00; 54 MEM32(g_esp + 4) = input; 55 MEM32(g_esp + 8) = delta; 56 MEM32(g_esp + 12) = 0x50001000 + source * 64; 57 MEM32(g_esp + 16) = 0x50001000 + target * 64; 58 MEM32(g_esp + 20) = reference; 59 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 60 guest_rtti_lookup("__RTDynamicCast")(); 61 assert(g_esp == 0x5000ff04); 62 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 63 return g_eax; 64 #endif 65 } 66 67 static void multiple_inheritance_adjusts_secondary_base(void) 68 { 69 layout(4, 1, 24); 70 base(0, 0, 3, 0, -1, 0); 71 base(1, 1, 2, 0, -1, 0); 72 base(2, 2, 0, 0, -1, 0); 73 base(3, 3, 0, 24, -1, 0); 74 75 uint32_t result = cast(0x50007018, 0, 3, 1, 0); 76 77 assert(result == 0x50007000); 78 assert(cast(0x50007018, 0, 3, 0, 1) == 0x50007000); 79 assert(cast(0x50007018, 0, 3, 2, 0) == 0x50007000); 80 assert(cast(0x50007018, 0, 3, 3, 0) == 0x50007018); 81 assert(cast(0x50007018, 0, 3, 4, 0) == 0); 82 assert(cast(0, 0, 3, 1, 0) == 0); 83 assert(cast(0x50007018, 0, 5, 6, 0) == 0x50007000); 84 } 85 86 static void repeated_bases_select_the_source_branch(void) 87 { 88 layout(5, 5, 32); 89 base(0, 0, 4, 0, -1, 0); 90 base(1, 1, 1, 0, -1, 0); 91 base(2, 2, 0, 0, -1, 0); 92 base(3, 1, 1, 32, -1, 0); 93 base(4, 2, 0, 32, -1, 0); 94 for (unsigned i = 1; i < 5; ++i) MEM32(0x50002000 + i * 32 + 20) = 2; 95 96 uint32_t result = cast(0x50007020, 0, 2, 1, 0); 97 98 assert(result == 0x50007020); 99 assert(cast(0x50007020, 0, 2, 0, 0) == 0x50007000); 100 MEM32(0x50007000) = 0x50006004; 101 MEM32(0x50005004) = 0; 102 assert(cast(0x50007000, 0, 0, 1, 0) == 0); 103 } 104 105 static void virtual_base_displacement_locates_the_shared_base(void) 106 { 107 layout(3, 3, 32); 108 base(0, 0, 2, 0, -1, 0); 109 base(1, 1, 1, 0, -1, 0); 110 base(2, 2, 0, 0, 8, 4); 111 MEM32(0x50007008) = 0x50008000; 112 MEM32(0x50008004) = 24; 113 114 uint32_t result = cast(0x50007020, 0, 2, 1, 0); 115 116 assert(result == 0x50007000); 117 assert(cast(0x50007020, 0, 2, 2, 0) == 0x50007020); 118 } 119 120 static void vfptr_and_construction_displacements_adjust_the_complete_object(void) 121 { 122 layout(2, 1, 32); 123 base(0, 0, 1, 0, -1, 0); 124 base(1, 1, 0, 24, -1, 0); 125 126 uint32_t result = cast(0x50007020, 8, 1, 0, 0); 127 128 assert(result == 0x50007000); 129 layout(2, 1, 40); 130 base(0, 0, 1, 0, -1, 0); 131 base(1, 1, 0, 40, -1, 0); 132 MEM32(0x50005004) = 32; 133 MEM32(0x50005008) = 4; 134 MEM32(0x50007024) = 8; 135 assert(cast(0x50007028, 0, 1, 0, 0) == 0x50007000); 136 } 137 138 static void shared_virtual_source_matches_both_target_branches(void) 139 { 140 layout(5, 3, 32); 141 base(0, 0, 4, 0, -1, 0); 142 base(1, 1, 1, 0, -1, 0); 143 base(2, 2, 0, 0, 8, 4); 144 base(3, 3, 1, 16, -1, 0); 145 base(4, 2, 0, 0, 8, 4); 146 MEM32(0x50007008) = 0x50008000; 147 MEM32(0x50008004) = 24; 148 149 uint32_t left = cast(0x50007020, 0, 2, 1, 0); 150 uint32_t right = cast(0x50007020, 0, 2, 3, 0); 151 152 assert(left == 0x50007000); 153 assert(right == 0x50007010); 154 } 155 156 static void type_info_equality_uses_names(void) 157 { 158 type(7, "!?AVRDBObject_t@@"); 159 type(8, ".?AVrDBObject_t@@"); 160 const unsigned pairs[][3] = {{1, 1, 1}, {1, 6, 1}, {6, 1, 1}, {1, 7, 1}, {1, 8, 0}, {1, 2, 0}}; 161 #ifdef _WIN32 162 int (__thiscall *equal)(void *, void *) = 163 (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "??8type_info@@QBEHABV0@@Z"); 164 assert(equal); 165 #endif 166 for (unsigned i = 0; i < sizeof(pairs) / sizeof(pairs[0]); ++i) { 167 uint32_t left = 0x50001000 + pairs[i][0] * 64; 168 uint32_t right = 0x50001000 + pairs[i][1] * 64; 169 MEM32(left) = 0x12345678; MEM32(left + 4) = 0x87654321; 170 MEM32(right) = 0x23456789; MEM32(right + 4) = 0x98765432; 171 172 #ifdef _WIN32 173 int result = equal((void *)(uintptr_t)left, (void *)(uintptr_t)right); 174 #else 175 g_esp = 0x5000ff00; 176 MEM32(g_esp + 4) = right; 177 g_ecx = left; 178 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 179 guest_rtti_lookup("??8type_info@@QBEHABV0@@Z")(); 180 int result = g_eax; 181 assert(g_esp == 0x5000ff08); 182 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 183 #endif 184 185 assert(result == (int)pairs[i][2]); 186 assert(MEM32(right) == 0x23456789 && MEM32(right + 4) == 0x98765432); 187 } 188 } 189 190 static void typeid_returns_the_complete_object_type(void) 191 { 192 const unsigned offsets[] = {0, 24, 40}; 193 for (unsigned i = 0; i < 3; ++i) { 194 layout(0, 0, offsets[i]); 195 MEM32(0x50005010) = 0; 196 if (i == 2) { 197 MEM32(0x50005004) = 32; 198 MEM32(0x50005008) = 4; 199 MEM32(0x50007024) = 8; 200 } 201 uint32_t input = 0x50007000 + offsets[i]; 202 #ifndef _WIN32 203 g_esp = 0x5000ff00; 204 MEM32(g_esp + 4) = input; 205 g_ecx = 567; g_edx = 678; 206 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 207 recomp_func_t function = guest_rtti_lookup("__RTtypeid"); 208 assert(function); 209 #endif 210 211 #ifdef _WIN32 212 uint32_t result = (uintptr_t)native_typeid((void *)(uintptr_t)input); 213 #else 214 function(); 215 uint32_t result = g_eax; 216 #endif 217 218 assert(result == 0x50001000); 219 #ifndef _WIN32 220 assert(g_esp == 0x5000ff04 && MEM32(g_esp) == input); 221 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 222 #endif 223 } 224 } 225 226 static void unrelated_private_base_allows_public_vehicle_casts(void) 227 { 228 type(9, ".?AVNPCVehicle_t@@"); 229 type(10, ".?AVCharVehicle_t@@"); 230 type(11, ".?AVDummyVehicle_t@@"); 231 type(12, ".?AVVehicle_t@@"); 232 type(13, ".?AVLocalitySource_t@@"); 233 type(14, ".?AVSpaceCellAble_i@@"); 234 type(15, ".?AVLocalityListener_i@@"); 235 type(16, ".?AVn3DynelEventListener_i@@"); 236 layout(8, 1, 0); 237 base(0, 9, 7, 0, -1, 0); 238 base(1, 10, 6, 0, -1, 0); 239 base(2, 11, 4, 0, -1, 0); 240 base(3, 12, 3, 0, -1, 0); 241 base(4, 13, 1, 0, -1, 0); 242 base(5, 14, 0, 0, -1, 0); 243 base(6, 15, 0, 32, -1, 0); 244 base(7, 16, 0, 316, -1, 0); 245 MEM32(0x50002000 + 7 * 32 + 20) = 9; 246 247 uint32_t result = cast(0x50007000, 0, 10, 9, 0); 248 249 assert(result == 0x50007000); 250 assert(cast(0x50007000, 0, 10, 15, 0) == 0x50007020); 251 assert(cast(0x50007000, 0, 10, 4, 0) == 0); 252 } 253 254 static void metadata_records_span_guest_pages(void) 255 { 256 for (unsigned record = 0; record < 5; ++record) { 257 layout(3, 3, 32); 258 base(0, 0, 2, 0, -1, 0); 259 base(1, 1, 1, 0, -1, 0); 260 base(2, 2, 0, 0, 8, 4); 261 MEM32(0x50007008) = 0x50008000; 262 MEM32(0x50008004) = 24; 263 const uint32_t source[] = {0x50005000, 0x50004000, 0x50003000, 0x50002000, 0x50002040}; 264 const uint32_t size[] = {20, 16, 12, 24, 24}; 265 const uint32_t pointer[] = {0x50006000, 0x50005010, 0x5000400c, 0x50003000, 0x50003008}; 266 memcpy((void *)ADDR(0x5001fff8), (void *)ADDR(source[record]), size[record]); 267 MEM32(pointer[record]) = 0x5001fff8; 268 269 uint32_t result = cast(0x50007020, 0, 2, 1, 0); 270 271 assert(result == 0x50007000); 272 } 273 } 274 275 int main(void) 276 { 277 #ifdef _WIN32 278 assert(VirtualAlloc((void *)0x50000000, 0x30000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)0x50000000); 279 native_cast = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "__RTDynamicCast"); 280 assert(native_cast); 281 native_typeid = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "__RTtypeid"); 282 assert(native_typeid); 283 #else 284 assert(guest_map("RTTI fixture", 0x50000000, 0x30000)); 285 #endif 286 type(0, ".?AVRDBCATMesh_t@@"); 287 type(1, ".?AVRDBObject_t@@"); 288 type(2, ".?AVDbObject_t@@"); 289 type(3, ".?AVResourceUser_t@@"); 290 type(4, ".?AVUnrelated@@"); 291 type(5, ".?AVResourceUser_t@@"); 292 type(6, ".?AVRDBObject_t@@"); 293 multiple_inheritance_adjusts_secondary_base(); 294 repeated_bases_select_the_source_branch(); 295 virtual_base_displacement_locates_the_shared_base(); 296 vfptr_and_construction_displacements_adjust_the_complete_object(); 297 shared_virtual_source_matches_both_target_branches(); 298 type_info_equality_uses_names(); 299 typeid_returns_the_complete_object_type(); 300 unrelated_private_base_allows_public_vehicle_casts(); 301 metadata_records_span_guest_pages(); 302 puts("RTTI: public multiple/virtual inheritance, unrelated private bases, repeated bases, cross-DLL names, type_info equality, typeid, null/failure and ABI passed"); 303 return 0; 304 }