C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::any::base

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 16 Apr 2024 13:40:51 -0700
On Tuesday 16 April 2024 07:51:34 GMT-7 Frederick Virchanza Gotham via Std-
Proposals wrote:
> So there must be a link from the type_info
> to either:
> (a) RTTICompleteObjectLocator
> or
> (b) ThrowInfo


https://msvc.godbolt.org/z/75rjKzGoh

The function f() returns a pointer to ??_R0?AUDerived@@@8 ("Derived `RTTI Type
Descriptor'"). That is an object of type "type_info" as seen by the fact that
its first member is a pointer to type_info's vftable. Function g() loads the
_R0 for both Base and derived as parameters to __RTDynamicCast, so we must
conclude the typeinfos are sufficient to dynamic cast.

How, I have no clue, because both _R0 objects don't link to anything else.
Both objects are adjacent to each other in the assembly output, so it can't be
following in memory or preceding in memory either.

Hmm... it occurs to me that there's a better way to debug MSVC assembly
output: don't use MSVC. Here's the Clang-cl version:

https://msvc.godbolt.org/z/YK76eYavj

But same thing: the g() function loads the two ?_R0 as parameters and neither
_R0 has a link to anything else aside from the type_info vftable. The two
objects are also in separate output sections.

And unlike MSVC, it didn't emit ?_R1, ?_R2, ?_R3 and ?_R4 because it didn't
need to.

Ok, so maybe the hint is in the vftable instead of the typeinfo. If I force it
to emit the vftable for Derived (https://msvc.godbolt.org/z/P4cPjE1rq), then
we can follow:
* the constructor (?0) loads the vftable (?_7)
* the vftable has a link to ?_R4 ('RTTI Complete Object Locator')
* the ?_R4 has a couple of flags and/or integers, followed by a pointer to the
  ?_R0 (the type_info), the ?_R3 and back to itself
* the ?_R3 ('RTTI Class Hierarchy Descriptor') has a couple of integers and
  then a pointer to the ?_R2
* the ?_R2 (`RTTI Base Class Array') is a null-terminated list of pointers,
  starting with itself, then the base class, to their ?_R1
* the ?_R1 (`RTTI Base Class Descriptor`) links again to the type info (the
  ?_R0), a couple of flags, then back to the ?_R3

PS: Are those pointers 32-bit on a 64-bit system??

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Principal Engineer - Intel DCAI Cloud Engineering

Received on 2024-04-16 20:40:55