commit 80a1b6eacf4feb97e08c106a0f343b3787415e76
parent 6248eb5d9d9e1c27b32a7fff59c69160df83ae03
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 13:22:52 +0000
Resolve runtime type casts in one inheritance traversal
Diffstat:
3 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -105,3 +105,7 @@ build/wasm/shadow-contact.js: tests/shadow_contact.c game/kkr.dll/lifted-0081.c
build/wasm/animation-initialization.js: tests/animation_initialization.c game/bbr.dll/lifted-0032.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h
@mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@
+
+build/wasm/rtti.js: tests/rtti.c runtime/guest_rtti.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_rtti.h runtime/guest_memory.h
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
diff --git a/src/runtime/guest_rtti.c b/src/runtime/guest_rtti.c
@@ -44,8 +44,9 @@ static void dynamic_cast(void)
/* Match names across DLLs. Public base trees carry preorder subtree sizes;
* a downcast must contain this particular source subobject, not just its type. */
uint32_t downcast = 0, crosscast = 0;
- int source_found = 0, down_ambiguous = 0, cross_ambiguous = 0;
- for (uint32_t i = 0; i < count; ++i) {
+ uint32_t source_index = count;
+ int down_ambiguous = 0, cross_ambiguous = 0;
+ for (uint32_t i = count; i-- > 0;) {
uint32_t descriptor = MEM32(bases + i * 4);
if (MEM32(descriptor + 20) & ~2u) {
/* An unrelated private base does not invalidate a public cast. */
@@ -56,20 +57,19 @@ static void dynamic_cast(void)
abort();
}
if (same_type(MEM32(descriptor), source) && base_address(object, descriptor) == input - vf_delta)
- source_found = 1;
+ source_index = i;
if (!same_type(MEM32(descriptor), target)) continue;
uint32_t address = base_address(object, descriptor);
if (crosscast && crosscast != address) cross_ambiguous = 1;
crosscast = address;
- for (uint32_t j = i; j <= i + MEM32(descriptor + 4); ++j) {
- uint32_t child = MEM32(bases + j * 4);
- if (same_type(MEM32(child), source) && base_address(object, child) == input - vf_delta) {
- if (downcast && downcast != address) down_ambiguous = 1;
- downcast = address;
- }
+ /* In reverse preorder, the nearest matching source is enough to
+ * decide whether this target's subtree contains the source subobject. */
+ if (source_index < count && source_index <= i + MEM32(descriptor + 4)) {
+ if (downcast && downcast != address) down_ambiguous = 1;
+ downcast = address;
}
}
- if (source_found) {
+ if (source_index < count) {
if (downcast && !down_ambiguous) g_eax = downcast;
else if (!downcast && !cross_ambiguous) g_eax = crosscast;
}
diff --git a/src/tests/rtti.c b/src/tests/rtti.c
@@ -134,6 +134,24 @@ static void vfptr_and_construction_displacements_adjust_the_complete_object(void
assert(cast(0x50007028, 0, 1, 0, 0) == 0x50007000);
}
+static void shared_virtual_source_matches_both_target_branches(void)
+{
+ layout(5, 3, 32);
+ base(0, 0, 4, 0, -1, 0);
+ base(1, 1, 1, 0, -1, 0);
+ base(2, 2, 0, 0, 8, 4);
+ base(3, 3, 1, 16, -1, 0);
+ base(4, 2, 0, 0, 8, 4);
+ MEM32(0x50007008) = 0x50008000;
+ MEM32(0x50008004) = 24;
+
+ uint32_t left = cast(0x50007020, 0, 2, 1, 0);
+ uint32_t right = cast(0x50007020, 0, 2, 3, 0);
+
+ assert(left == 0x50007000);
+ assert(right == 0x50007010);
+}
+
static void type_info_equality_uses_names(void)
{
type(7, "!?AVRDBObject_t@@");
@@ -216,6 +234,7 @@ int main(void)
repeated_bases_select_the_source_branch();
virtual_base_displacement_locates_the_shared_base();
vfptr_and_construction_displacements_adjust_the_complete_object();
+ shared_virtual_source_matches_both_target_branches();
type_info_equality_uses_names();
unrelated_private_base_allows_public_vehicle_casts();
puts("RTTI: public multiple/virtual inheritance, unrelated private bases, repeated bases, cross-DLL names, type_info equality, null/failure and ABI passed");