C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Get base class from std::any

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Wed, 29 Mar 2023 12:59:42 -0500
On Wed, 29 Mar 2023 at 11:35, <language.lawyer_at_[hidden]> wrote:

> > So the question is whether it would be possible in all implementations to
> > add any_base_cast / any_dynamic_cast without ABI break. We know that it
> is
> > in libstdc++ and libc++. Do we have an answer for MSVC-STL?
>
> I was playing with it smth. like 5-6 years ago and have lost the code, but
> I'll try to write what I remember.
>
> Unlike Itanium ABI, in MSVC, exception handling is not based (only) on
> type_info, and the corresponding structures are not easily obtainable.
>

Right, and that wouldn't be attainable from what's currently in the MS-STL
std::any, a std::type_info pointer with flags in the low bits - the manual
vtable isn't extensible, and the trivial representation doesn't even have a
vtable so as to make more efficient use of space - a nice trick.

So if we want Microsoft to implement this we'd need them either to add it
to std::type_info itself (not sure how feasible this is) or wait for an ABI
break in std::any (at the next toolset major).

AFAIR, I had a template function throwing an exception of type T*:
>
> template<typename T>
> void throwing() { throw T*{}; }
>
> I was obtaining a pointer to it (`&throwing<T>`) and then scanned the
> function body binary code to get the address of the corresponding exception
> type info (it is dumped to the object file, but there is no symbol
> referring to it). AFAIR, it is a structure of ThrowInfo type [0], which has
> pCatchableTypeArray member [1], which enumerates the types of the handlers
> which can capture the exception.
>
> So, when storing an object of type D, one should save its ThrowInfo, and
> then search for the B's CatchableType [2] when trying to cast to B, and, if
> present, it contains the offset to the B subobject [3].
> AFAIR, pType [4] is just std::type_info.
>
> So this method is ISA-dependent, because one will have to find the
> instruction containing the address (offset to) the ThrowInfo in
> `throwing<T>` binary code. Maybe ThrowInfo is more easily obtainable (now).
>
> [0]
> https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L369-L384
> [1]
> https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L382
> [2]
> https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L286-L298
> [3]
> https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L293
> [4]
> https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L288-L292
>

Received on 2023-03-29 17:59:55