client/runtime/guest_rtti.c (4743B)
1 #include "guest_rtti.h" 2 3 /* MSVC x86 RTTI records contain guest pointers, never host pointers. */ 4 struct rtti_locator { 5 uint32_t signature, offset, construction_displacement, type, hierarchy; 6 }; 7 struct rtti_hierarchy { 8 uint32_t signature, attributes, count, bases; 9 }; 10 struct rtti_base { 11 uint32_t type, descendants, offset; 12 int32_t vbptr; 13 uint32_t vbslot, attributes; 14 }; 15 16 static int same_type(uint32_t left, uint32_t right) 17 { 18 return left == right || !strcmp((const char *)ADDR(left + 8), (const char *)ADDR(right + 8)); 19 } 20 21 static void type_info_equal(void) 22 { 23 uint32_t other = MEM32(g_esp + 4); 24 g_eax = g_ecx == other || !strcmp((const char *)ADDR(g_ecx + 9), (const char *)ADDR(other + 9)); 25 g_esp += 8; /* type_info::operator== is thiscall. */ 26 } 27 28 static void runtime_typeid(void) 29 { 30 uint32_t input = MEM32(g_esp + 4); 31 if (!input) { 32 fprintf(stderr, "Guest RTTI bad_typeid exception is not implemented\n"); 33 abort(); 34 } 35 36 /* Every polymorphic subobject's locator identifies its complete object type. */ 37 const struct rtti_locator *locator = (const void *)ADDR(MEM32(MEM32(input) - 4)); 38 if (locator->signature) { 39 fprintf(stderr, "Unsupported guest RTTI metadata version\n"); 40 abort(); 41 } 42 g_eax = locator->type; 43 g_esp += 4; /* __RTtypeid is cdecl. */ 44 } 45 46 static uint32_t base_address(uint32_t object, const struct rtti_base *descriptor) 47 { 48 uint32_t result = object + descriptor->offset; 49 int32_t vbptr = descriptor->vbptr; 50 if (vbptr != -1) 51 result += vbptr + MEM32(MEM32(object + vbptr) + descriptor->vbslot); 52 return result; 53 } 54 55 static void dynamic_cast(void) 56 { 57 uint32_t input = MEM32(g_esp + 4), vf_delta = MEM32(g_esp + 8); 58 uint32_t source = MEM32(g_esp + 12), target = MEM32(g_esp + 16); 59 uint32_t reference = MEM32(g_esp + 20); 60 g_eax = 0; 61 g_esp += 4; /* __RTDynamicCast is cdecl. */ 62 if (!input) return; 63 64 /* The x86 locator precedes the vtable and contains guest absolute pointers. */ 65 const struct rtti_locator *locator = (const void *)ADDR(MEM32(MEM32(input) - 4)); 66 uint32_t object = input - locator->offset; 67 if (locator->construction_displacement) object -= MEM32(input - locator->construction_displacement); 68 const struct rtti_hierarchy *hierarchy = (const void *)ADDR(locator->hierarchy); 69 uint32_t count = hierarchy->count; 70 const uint32_t *bases = (const void *)ADDR(hierarchy->bases); 71 if (locator->signature || hierarchy->signature) { 72 fprintf(stderr, "Unsupported guest RTTI metadata version\n"); 73 abort(); 74 } 75 76 /* Match names across DLLs. Public base trees carry preorder subtree sizes; 77 * a downcast must contain this particular source subobject, not just its type. */ 78 uint32_t downcast = 0, crosscast = 0; 79 uint32_t source_index = count; 80 int down_ambiguous = 0, cross_ambiguous = 0; 81 for (uint32_t i = count; i-- > 0;) { 82 const struct rtti_base *descriptor = (const void *)ADDR(bases[i]); 83 if (descriptor->attributes & ~2u) { 84 /* An unrelated private base does not invalidate a public cast. */ 85 if (!(descriptor->attributes & ~0x1fu) && 86 !same_type(descriptor->type, source) && !same_type(descriptor->type, target)) continue; 87 fprintf(stderr, "Non-public guest RTTI bases are not implemented (attributes %x)\n", 88 descriptor->attributes); 89 abort(); 90 } 91 if (same_type(descriptor->type, source) && base_address(object, descriptor) == input - vf_delta) 92 source_index = i; 93 if (!same_type(descriptor->type, target)) continue; 94 uint32_t address = base_address(object, descriptor); 95 if (crosscast && crosscast != address) cross_ambiguous = 1; 96 crosscast = address; 97 /* In reverse preorder, the nearest matching source is enough to 98 * decide whether this target's subtree contains the source subobject. */ 99 if (source_index < count && source_index <= i + descriptor->descendants) { 100 if (downcast && downcast != address) down_ambiguous = 1; 101 downcast = address; 102 } 103 } 104 if (source_index < count) { 105 if (downcast && !down_ambiguous) g_eax = downcast; 106 else if (!downcast && !cross_ambiguous) g_eax = crosscast; 107 } 108 if (!g_eax && reference) { 109 fprintf(stderr, "Guest RTTI bad_cast exception is not implemented\n"); 110 abort(); 111 } 112 } 113 114 recomp_func_t guest_rtti_lookup(const char *name) 115 { 116 if (!strcmp(name, "__RTDynamicCast")) return dynamic_cast; 117 if (!strcmp(name, "__RTtypeid")) return runtime_typeid; 118 if (!strcmp(name, "??8type_info@@QBEHABV0@@Z")) return type_info_equal; 119 return NULL; 120 }