C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 16 Apr 2024 16:07:25 -0700
On Tuesday 16 April 2024 15:43:41 GMT-7 Frederick Virchanza Gotham via Std-
Proposals wrote:
> On Tue, Apr 16, 2024 at 9:40 PM Thiago Macieira wrote:
> > 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')
>
> I think the secret is to be found in disassembling the implementation
> of "__RTDynamicCast" which is to be found inside "vcruntime140.dll".
> You can see my initial attempt to convert it from x86_64 assembler
> into C here:
>
> https://lists.isocpp.org/std-proposals/2024/04/9546.php

Which is of no interest to me and may be a violation of Microsoft's terms of
use. So, no thanks.

Anyway, the point is that given a pointer to the object and the typeinfo of
the source and destination, __RTDynamicCast can perform the cast. std::any has
the pointer to the object and the source typeinfo; the any_cast call provides
the typeinfo to the destination. Therefore, it should be possible.

The questions are:
a) what's that extra 0 parameter?
b) does the pointer passed to __RTDynamicCast need to be adjusted in some way
  by the compiler before the call, based on how the source and destination
  relate to each other?

> > PS: Are those pointers 32-bit on a 64-bit system??
>
> If you use Visual Studio 2022 to build your program as x86_32, then
> they are just normal 32-Bit pointers. However if you build your
> program as x86_64, then they are 32-Bit offsets which you add to the
> return address from "GetModuleHandleA(nullptr)".

I see. So can they point to structures found in dllimport'ed libraries? Well,
they can't, so how the hell can dynamic_cast cast across DLLs?

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

Received on 2024-04-16 23:07:30