1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "private_typeinfo.h"
11 // The flag _LIBCXXABI_FORGIVING_DYNAMIC_CAST is used to make dynamic_cast
12 // more forgiving when type_info's mistakenly have hidden visibility and
13 // thus multiple type_infos can exist for a single type.
15 // When _LIBCXXABI_FORGIVING_DYNAMIC_CAST is defined, and only in the case where
16 // there is a detected inconsistency in the type_info hierarchy during a
17 // dynamic_cast, then the equality operation will fall back to using strcmp
18 // on type_info names to determine type_info equality.
20 // This change happens *only* under dynamic_cast, and only when
21 // dynamic_cast is faced with the choice: abort, or possibly give back the
22 // wrong answer. If when the dynamic_cast is done with this fallback
23 // algorithm and an inconsistency is still detected, dynamic_cast will call
24 // abort with an appropriate message.
26 // The current implementation of _LIBCXXABI_FORGIVING_DYNAMIC_CAST requires a
27 // printf-like function called syslog:
29 // void syslog(int facility_priority, const char* format, ...);
31 // If you want this functionality but your platform doesn't have syslog,
32 // just implement it in terms of fprintf(stderr, ...).
34 // _LIBCXXABI_FORGIVING_DYNAMIC_CAST is currently off by default.
36 // On Windows, typeids are different between DLLs and EXEs, so comparing
37 // type_info* will work for typeids from the same compiled file but fail
38 // for typeids from a DLL and an executable. Among other things, exceptions
39 // are not caught by handlers since can_catch() returns false.
41 // Defining _LIBCXXABI_FORGIVING_DYNAMIC_CAST does not help since can_catch() calls
42 // is_equal() with use_strcmp=false so the string names are not compared.
46 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
47 #include "abort_message.h"
48 #include <sys/syslog.h>
54 is_equal(const std::type_info
* x
, const std::type_info
* y
, bool use_strcmp
)
56 // Use std::type_info's default comparison unless we've explicitly asked
60 // Still allow pointer equality to short circut.
61 return x
== y
|| strcmp(x
->name(), y
->name()) == 0;
64 static inline ptrdiff_t update_offset_to_base(const char* vtable
,
65 ptrdiff_t offset_to_base
) {
66 #if __has_feature(cxx_abi_relative_vtable)
67 // VTable components are 32 bits in the relative vtables ABI.
68 return *reinterpret_cast<const int32_t*>(vtable
+ offset_to_base
);
70 return *reinterpret_cast<const ptrdiff_t*>(vtable
+ offset_to_base
);
79 __shim_type_info::~__shim_type_info()
83 void __shim_type_info::noop1() const {}
84 void __shim_type_info::noop2() const {}
86 // __fundamental_type_info
88 // This miraculously (compiler magic) emits the type_info's for:
89 // 1. all of the fundamental types
90 // 2. pointers to all of the fundamental types
91 // 3. pointers to all of the const fundamental types
92 __fundamental_type_info::~__fundamental_type_info()
98 __array_type_info::~__array_type_info()
102 // __function_type_info
104 __function_type_info::~__function_type_info()
110 __enum_type_info::~__enum_type_info()
116 __class_type_info::~__class_type_info()
120 // __si_class_type_info
122 __si_class_type_info::~__si_class_type_info()
126 // __vmi_class_type_info
128 __vmi_class_type_info::~__vmi_class_type_info()
134 __pbase_type_info::~__pbase_type_info()
138 // __pointer_type_info
140 __pointer_type_info::~__pointer_type_info()
144 // __pointer_to_member_type_info
146 __pointer_to_member_type_info::~__pointer_to_member_type_info()
152 // A handler is a match for an exception object of type E if
153 // 1. The handler is of type cv T or cv T& and E and T are the same type
154 // (ignoring the top-level cv-qualifiers), or
155 // 2. the handler is of type cv T or cv T& and T is an unambiguous public
156 // base class of E, or
157 // 3. the handler is of type cv1 T* cv2 and E is a pointer type that can be
158 // converted to the type of the handler by either or both of
159 // A. a standard pointer conversion (4.10) not involving conversions to
160 // pointers to private or protected or ambiguous classes
161 // B. a qualification conversion
162 // 4. the handler is a pointer or pointer to member type and E is
167 // catch (A& a) : adjustedPtr == &a
168 // catch (A* a) : adjustedPtr == a
169 // catch (A** a) : adjustedPtr == a
171 // catch (D2& d2) : adjustedPtr == &d2 (d2 is base class of thrown object)
172 // catch (D2* d2) : adjustedPtr == d2
173 // catch (D2*& d2) : adjustedPtr == d2
175 // catch (...) : adjustedPtr == & of the exception
177 // If the thrown type is nullptr_t and the caught type is a pointer to
178 // member type, adjustedPtr points to a statically-allocated null pointer
179 // representation of that type.
183 __fundamental_type_info::can_catch(const __shim_type_info
* thrown_type
,
186 return is_equal(this, thrown_type
, false);
190 __array_type_info::can_catch(const __shim_type_info
*, void*&) const
192 // We can get here if someone tries to catch an array by reference.
193 // However if someone tries to throw an array, it immediately gets
194 // converted to a pointer, which will not convert back to an array
195 // at the catch clause. So this can never catch anything.
200 __function_type_info::can_catch(const __shim_type_info
*, void*&) const
202 // We can get here if someone tries to catch a function by reference.
203 // However if someone tries to throw a function, it immediately gets
204 // converted to a pointer, which will not convert back to a function
205 // at the catch clause. So this can never catch anything.
211 __enum_type_info::can_catch(const __shim_type_info
* thrown_type
,
214 return is_equal(this, thrown_type
, false);
218 #pragma clang diagnostic push
219 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
222 // Handles bullets 1 and 2
224 __class_type_info::can_catch(const __shim_type_info
* thrown_type
,
225 void*& adjustedPtr
) const
228 if (is_equal(this, thrown_type
, false))
230 const __class_type_info
* thrown_class_type
=
231 dynamic_cast<const __class_type_info
*>(thrown_type
);
232 if (thrown_class_type
== 0)
235 __dynamic_cast_info info
= {thrown_class_type
, 0, this, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
236 info
.number_of_dst_type
= 1;
237 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
238 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
240 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
247 #pragma clang diagnostic pop
251 __class_type_info::process_found_base_class(__dynamic_cast_info
* info
,
253 int path_below
) const
255 if (info
->dst_ptr_leading_to_static_ptr
== 0)
258 info
->dst_ptr_leading_to_static_ptr
= adjustedPtr
;
259 info
->path_dst_ptr_to_static_ptr
= path_below
;
260 info
->number_to_static_ptr
= 1;
262 else if (info
->dst_ptr_leading_to_static_ptr
== adjustedPtr
)
264 // We've been here before. Update path to "most public"
265 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
266 info
->path_dst_ptr_to_static_ptr
= path_below
;
270 // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
272 info
->number_to_static_ptr
+= 1;
273 info
->path_dst_ptr_to_static_ptr
= not_public_path
;
274 info
->search_done
= true;
279 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
281 int path_below
) const
283 if (is_equal(this, info
->static_type
, false))
284 process_found_base_class(info
, adjustedPtr
, path_below
);
288 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
290 int path_below
) const
292 if (is_equal(this, info
->static_type
, false))
293 process_found_base_class(info
, adjustedPtr
, path_below
);
295 __base_type
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
299 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
301 int path_below
) const
303 ptrdiff_t offset_to_base
= 0;
304 if (adjustedPtr
!= nullptr)
306 offset_to_base
= __offset_flags
>> __offset_shift
;
307 if (__offset_flags
& __virtual_mask
)
309 const char* vtable
= *static_cast<const char*const*>(adjustedPtr
);
310 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
313 __base_type
->has_unambiguous_public_base(
315 static_cast<char*>(adjustedPtr
) + offset_to_base
,
316 (__offset_flags
& __public_mask
) ? path_below
: not_public_path
);
320 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info
* info
,
322 int path_below
) const
324 if (is_equal(this, info
->static_type
, false))
325 process_found_base_class(info
, adjustedPtr
, path_below
);
328 typedef const __base_class_type_info
* Iter
;
329 const Iter e
= __base_info
+ __base_count
;
330 Iter p
= __base_info
;
331 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
336 p
->has_unambiguous_public_base(info
, adjustedPtr
, path_below
);
337 if (info
->search_done
)
344 // Handles bullet 1 for both pointers and member pointers
346 __pbase_type_info::can_catch(const __shim_type_info
* thrown_type
,
349 bool use_strcmp
= this->__flags
& (__incomplete_class_mask
|
352 const __pbase_type_info
* thrown_pbase
= dynamic_cast<const __pbase_type_info
*>(
354 if (!thrown_pbase
) return false;
355 use_strcmp
= thrown_pbase
->__flags
& (__incomplete_class_mask
|
358 return is_equal(this, thrown_type
, use_strcmp
);
362 #pragma clang diagnostic push
363 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
366 // Handles bullets 1, 3 and 4
367 // NOTE: It might not be safe to adjust the pointer if it is not not a pointer
368 // type. Only adjust the pointer after we know it is safe to do so.
370 __pointer_type_info::can_catch(const __shim_type_info
* thrown_type
,
371 void*& adjustedPtr
) const
374 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
375 adjustedPtr
= nullptr;
380 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
)) {
381 if (adjustedPtr
!= NULL
)
382 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
386 const __pointer_type_info
* thrown_pointer_type
=
387 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
388 if (thrown_pointer_type
== 0)
390 // Do the dereference adjustment
391 if (adjustedPtr
!= NULL
)
392 adjustedPtr
= *static_cast<void**>(adjustedPtr
);
394 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
396 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
398 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
401 if (is_equal(__pointee
, &typeid(void), false)) {
402 // pointers to functions cannot be converted to void*.
403 // pointers to member functions are not handled here.
404 const __function_type_info
* thrown_function
=
405 dynamic_cast<const __function_type_info
*>(thrown_pointer_type
->__pointee
);
406 return (thrown_function
== nullptr);
408 // Handle pointer to pointer
409 const __pointer_type_info
* nested_pointer_type
=
410 dynamic_cast<const __pointer_type_info
*>(__pointee
);
411 if (nested_pointer_type
) {
412 if (~__flags
& __const_mask
) return false;
413 return nested_pointer_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
416 // Handle pointer to pointer to member
417 const __pointer_to_member_type_info
* member_ptr_type
=
418 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
419 if (member_ptr_type
) {
420 if (~__flags
& __const_mask
) return false;
421 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
424 // Handle pointer to class type
425 const __class_type_info
* catch_class_type
=
426 dynamic_cast<const __class_type_info
*>(__pointee
);
427 if (catch_class_type
== 0)
429 const __class_type_info
* thrown_class_type
=
430 dynamic_cast<const __class_type_info
*>(thrown_pointer_type
->__pointee
);
431 if (thrown_class_type
== 0)
433 __dynamic_cast_info info
= {thrown_class_type
, 0, catch_class_type
, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
434 info
.number_of_dst_type
= 1;
435 thrown_class_type
->has_unambiguous_public_base(&info
, adjustedPtr
, public_path
);
436 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
438 if (adjustedPtr
!= NULL
)
439 adjustedPtr
= const_cast<void*>(info
.dst_ptr_leading_to_static_ptr
);
445 bool __pointer_type_info::can_catch_nested(
446 const __shim_type_info
* thrown_type
) const
448 const __pointer_type_info
* thrown_pointer_type
=
449 dynamic_cast<const __pointer_type_info
*>(thrown_type
);
450 if (thrown_pointer_type
== 0)
453 if (thrown_pointer_type
->__flags
& ~__flags
)
455 if (is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
457 // If the pointed to types differ then the catch type must be const
459 if (~__flags
& __const_mask
)
462 // Handle pointer to pointer
463 const __pointer_type_info
* nested_pointer_type
=
464 dynamic_cast<const __pointer_type_info
*>(__pointee
);
465 if (nested_pointer_type
) {
466 return nested_pointer_type
->can_catch_nested(
467 thrown_pointer_type
->__pointee
);
470 // Handle pointer to pointer to member
471 const __pointer_to_member_type_info
* member_ptr_type
=
472 dynamic_cast<const __pointer_to_member_type_info
*>(__pointee
);
473 if (member_ptr_type
) {
474 return member_ptr_type
->can_catch_nested(thrown_pointer_type
->__pointee
);
480 bool __pointer_to_member_type_info::can_catch(
481 const __shim_type_info
* thrown_type
, void*& adjustedPtr
) const {
483 if (is_equal(thrown_type
, &typeid(std::nullptr_t
), false)) {
484 // We assume that the pointer to member representation is the same for
485 // all pointers to data members and for all pointers to member functions.
487 if (dynamic_cast<const __function_type_info
*>(__pointee
)) {
488 static int (X::*const null_ptr_rep
)() = nullptr;
489 adjustedPtr
= const_cast<int (X::**)()>(&null_ptr_rep
);
491 static int X::*const null_ptr_rep
= nullptr;
492 adjustedPtr
= const_cast<int X::**>(&null_ptr_rep
);
498 if (__pbase_type_info::can_catch(thrown_type
, adjustedPtr
))
501 const __pointer_to_member_type_info
* thrown_pointer_type
=
502 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
503 if (thrown_pointer_type
== 0)
505 if (thrown_pointer_type
->__flags
& ~__flags
& __no_remove_flags_mask
)
507 if (__flags
& ~thrown_pointer_type
->__flags
& __no_add_flags_mask
)
509 if (!is_equal(__pointee
, thrown_pointer_type
->__pointee
, false))
511 if (is_equal(__context
, thrown_pointer_type
->__context
, false))
514 // [except.handle] does not allow the pointer-to-member conversions mentioned
515 // in [mem.conv] to take place. For this reason we don't check Derived->Base
516 // for Derived->Base conversions.
521 bool __pointer_to_member_type_info::can_catch_nested(
522 const __shim_type_info
* thrown_type
) const
524 const __pointer_to_member_type_info
* thrown_member_ptr_type
=
525 dynamic_cast<const __pointer_to_member_type_info
*>(thrown_type
);
526 if (thrown_member_ptr_type
== 0)
528 if (~__flags
& thrown_member_ptr_type
->__flags
)
530 if (!is_equal(__pointee
, thrown_member_ptr_type
->__pointee
, false))
532 if (!is_equal(__context
, thrown_member_ptr_type
->__context
, false))
538 #pragma clang diagnostic pop
542 #pragma clang diagnostic push
543 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
548 // static_ptr: pointer to an object of type static_type; nonnull, and since the
549 // object is polymorphic, *(void**)static_ptr is a virtual table pointer.
550 // static_ptr is &v in the expression dynamic_cast<T>(v).
551 // static_type: static type of the object pointed to by static_ptr.
552 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
553 // src2dst_offset: a static hint about the location of the
554 // source subobject with respect to the complete object;
555 // special negative values are:
557 // -2: static_type is not a public base of dst_type
558 // -3: static_type is a multiple public base type but never a
560 // otherwise, the static_type type is a unique public nonvirtual
561 // base type of dst_type at offset src2dst_offset from the
562 // origin of dst_type.
564 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
565 // referred to by static_ptr and a pointer to it. These can be found from
566 // static_ptr for polymorphic types.
567 // static_type is guaranteed to be a polymorphic type.
569 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward. Each
570 // node of the tree represents a base class/object of its parent (or parents) below.
571 // Each node is uniquely represented by a pointer to the object, and a pointer
572 // to a type_info - its type. Different nodes may have the same pointer and
573 // different nodes may have the same type. But only one node has a specific
574 // (pointer-value, type) pair. In C++ two objects of the same type can not
575 // share the same address.
577 // There are two flavors of nodes which have the type dst_type:
578 // 1. Those that are derived from (below) (static_ptr, static_type).
579 // 2. Those that are not derived from (below) (static_ptr, static_type).
581 // Invariants of the DAG:
583 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
584 // the node (static_ptr, static_type). This path may or may not be public.
585 // There may be more than one such path (some public some not). Such a path may
586 // or may not go through a node having type dst_type.
588 // No node of type T appears above a node of the same type. That means that
589 // there is only one node with dynamic_type. And if dynamic_type == dst_type,
590 // then there is only one dst_type in the DAG.
592 // No node of type dst_type appears above a node of type static_type. Such
593 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
594 // compile time, and only calls __dynamic_cast when dst_type lies below
595 // static_type in the DAG.
597 // dst_type != static_type: The compiler computes the dynamic_cast in this case too.
598 // dynamic_type != static_type: The compiler computes the dynamic_cast in this case too.
602 // If there is exactly one dst_type of flavor 1, and
603 // If there is a public path from that dst_type to (static_ptr, static_type), or
604 // If there are 0 dst_types of flavor 2, and there is a public path from
605 // (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
606 // path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
607 // a pointer to that dst_type.
608 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
609 // if there is a public path from (dynamic_ptr, dynamic_type) to
610 // (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
611 // to the one dst_type, then return a pointer to that one dst_type.
612 // Else return nullptr.
614 // If dynamic_type == dst_type, then the above algorithm collapses to the
615 // following cheaper algorithm:
617 // If there is a public path from (dynamic_ptr, dynamic_type) to
618 // (static_ptr, static_type), then return dynamic_ptr.
619 // Else return nullptr.
621 extern "C" _LIBCXXABI_FUNC_VIS
void *
622 __dynamic_cast(const void *static_ptr
, const __class_type_info
*static_type
,
623 const __class_type_info
*dst_type
,
624 std::ptrdiff_t src2dst_offset
) {
625 // Possible future optimization: Take advantage of src2dst_offset
627 // Get (dynamic_ptr, dynamic_type) from static_ptr
628 #if __has_feature(cxx_abi_relative_vtable)
629 // The vtable address will point to the first virtual function, which is 8
630 // bytes after the start of the vtable (4 for the offset from top + 4 for the typeinfo component).
631 const int32_t* vtable
=
632 *reinterpret_cast<const int32_t* const*>(static_ptr
);
633 int32_t offset_to_derived
= vtable
[-2];
634 const void* dynamic_ptr
= static_cast<const char*>(static_ptr
) + offset_to_derived
;
636 // The typeinfo component is now a relative offset to a proxy.
637 int32_t offset_to_ti_proxy
= vtable
[-1];
638 const uint8_t* ptr_to_ti_proxy
=
639 reinterpret_cast<const uint8_t*>(vtable
) + offset_to_ti_proxy
;
640 const __class_type_info
* dynamic_type
=
641 *(reinterpret_cast<const __class_type_info
* const*>(ptr_to_ti_proxy
));
643 void **vtable
= *static_cast<void ** const *>(static_ptr
);
644 ptrdiff_t offset_to_derived
= reinterpret_cast<ptrdiff_t>(vtable
[-2]);
645 const void* dynamic_ptr
= static_cast<const char*>(static_ptr
) + offset_to_derived
;
646 const __class_type_info
* dynamic_type
= static_cast<const __class_type_info
*>(vtable
[-1]);
649 // Initialize answer to nullptr. This will be changed from the search
650 // results if a non-null answer is found. Regardless, this is what will
652 const void* dst_ptr
= 0;
653 // Initialize info struct for this search.
654 __dynamic_cast_info info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
656 // Find out if we can use a giant short cut in the search
657 if (is_equal(dynamic_type
, dst_type
, false))
659 // Using giant short cut. Add that information to info.
660 info
.number_of_dst_type
= 1;
662 dynamic_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, false);
663 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
664 // The following if should always be false because we should definitely
665 // find (static_ptr, static_type), either on a public or private path
666 if (info
.path_dst_ptr_to_static_ptr
== unknown
)
668 // We get here only if there is some kind of visibility problem
670 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
671 static std::atomic
<size_t> error_count(0);
672 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
673 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
674 syslog(LOG_ERR
, "dynamic_cast error 1: Both of the following type_info's "
675 "should have public visibility. At least one of them is hidden. %s"
676 ", %s.\n", static_type
->name(), dynamic_type
->name());
677 // Redo the search comparing type_info's using strcmp
678 info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0};
679 info
.number_of_dst_type
= 1;
680 dynamic_type
->search_above_dst(&info
, dynamic_ptr
, dynamic_ptr
, public_path
, true);
682 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
684 if (info
.path_dst_ptr_to_static_ptr
== public_path
)
685 dst_ptr
= dynamic_ptr
;
689 // Not using giant short cut. Do the search
690 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, false);
691 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
692 // The following if should always be false because we should definitely
693 // find (static_ptr, static_type), either on a public or private path
694 if (info
.path_dst_ptr_to_static_ptr
== unknown
&&
695 info
.path_dynamic_ptr_to_static_ptr
== unknown
)
697 static_assert(std::atomic
<size_t>::is_always_lock_free
, "");
698 static std::atomic
<size_t> error_count(0);
699 size_t error_count_snapshot
= error_count
.fetch_add(1, std::memory_order_relaxed
);
700 if ((error_count_snapshot
& (error_count_snapshot
-1)) == 0)
701 syslog(LOG_ERR
, "dynamic_cast error 2: One or more of the following type_info's "
702 "has hidden visibility or is defined in more than one translation "
703 "unit. They should all have public visibility. "
704 "%s, %s, %s.\n", static_type
->name(), dynamic_type
->name(),
706 // Redo the search comparing type_info's using strcmp
707 info
= {dst_type
, static_ptr
, static_type
, src2dst_offset
, 0};
708 dynamic_type
->search_below_dst(&info
, dynamic_ptr
, public_path
, true);
710 #endif // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
712 switch (info
.number_to_static_ptr
)
715 if (info
.number_to_dst_ptr
== 1 &&
716 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
717 info
.path_dynamic_ptr_to_dst_ptr
== public_path
)
718 dst_ptr
= info
.dst_ptr_not_leading_to_static_ptr
;
721 if (info
.path_dst_ptr_to_static_ptr
== public_path
||
723 info
.number_to_dst_ptr
== 0 &&
724 info
.path_dynamic_ptr_to_static_ptr
== public_path
&&
725 info
.path_dynamic_ptr_to_dst_ptr
== public_path
728 dst_ptr
= info
.dst_ptr_leading_to_static_ptr
;
732 return const_cast<void*>(dst_ptr
);
736 #pragma clang diagnostic pop
739 // Call this function when you hit a static_type which is a base (above) a dst_type.
740 // Let caller know you hit a static_type. But only start recording details if
741 // this is (static_ptr, static_type) -- the node we are casting from.
742 // If this is (static_ptr, static_type)
743 // Record the path (public or not) from the dst_type to here. There may be
744 // multiple paths from the same dst_type to here, record the "most public" one.
745 // Record the dst_ptr as pointing to (static_ptr, static_type).
746 // If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
747 // then mark this dyanmic_cast as ambiguous and stop the search.
749 __class_type_info::process_static_type_above_dst(__dynamic_cast_info
* info
,
751 const void* current_ptr
,
752 int path_below
) const
754 // Record that we found a static_type
755 info
->found_any_static_type
= true;
756 if (current_ptr
== info
->static_ptr
)
758 // Record that we found (static_ptr, static_type)
759 info
->found_our_static_ptr
= true;
760 if (info
->dst_ptr_leading_to_static_ptr
== 0)
763 info
->dst_ptr_leading_to_static_ptr
= dst_ptr
;
764 info
->path_dst_ptr_to_static_ptr
= path_below
;
765 info
->number_to_static_ptr
= 1;
766 // If there is only one dst_type in the entire tree and the path from
767 // there to here is public then we are done!
768 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
769 info
->search_done
= true;
771 else if (info
->dst_ptr_leading_to_static_ptr
== dst_ptr
)
773 // We've been here before. Update path to "most public"
774 if (info
->path_dst_ptr_to_static_ptr
== not_public_path
)
775 info
->path_dst_ptr_to_static_ptr
= path_below
;
776 // If there is only one dst_type in the entire tree and the path from
777 // there to here is public then we are done!
778 if (info
->number_of_dst_type
== 1 && info
->path_dst_ptr_to_static_ptr
== public_path
)
779 info
->search_done
= true;
783 // We've detected an ambiguous cast from (static_ptr, static_type)
785 info
->number_to_static_ptr
+= 1;
786 info
->search_done
= true;
791 // Call this function when you hit a static_type which is not a base (above) a dst_type.
792 // If this is (static_ptr, static_type)
793 // Record the path (public or not) from (dynamic_ptr, dynamic_type) to here. There may be
794 // multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
796 __class_type_info::process_static_type_below_dst(__dynamic_cast_info
* info
,
797 const void* current_ptr
,
798 int path_below
) const
800 if (current_ptr
== info
->static_ptr
)
802 // Record the most public path from (dynamic_ptr, dynamic_type) to
803 // (static_ptr, static_type)
804 if (info
->path_dynamic_ptr_to_static_ptr
!= public_path
)
805 info
->path_dynamic_ptr_to_static_ptr
= path_below
;
809 // Call this function when searching below a dst_type node. This function searches
810 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
811 // If it finds a static_type node, there is no need to further search base classes
813 // If it finds a dst_type node it should search base classes using search_above_dst
814 // to find out if this dst_type points to (static_ptr, static_type) or not.
815 // Either way, the dst_type is recorded as one of two "flavors": one that does
816 // or does not point to (static_ptr, static_type).
817 // If this is neither a static_type nor a dst_type node, continue searching
818 // base classes above.
819 // All the hoopla surrounding the search code is doing nothing but looking for
820 // excuses to stop the search prematurely (break out of the for-loop). That is,
821 // the algorithm below is simply an optimization of this:
823 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
824 // const void* current_ptr,
825 // int path_below) const
827 // typedef const __base_class_type_info* Iter;
828 // if (this == info->static_type)
829 // process_static_type_below_dst(info, current_ptr, path_below);
830 // else if (this == info->dst_type)
832 // // Record the most public access path that got us here
833 // if (info->path_dynamic_ptr_to_dst_ptr != public_path)
834 // info->path_dynamic_ptr_to_dst_ptr = path_below;
835 // bool does_dst_type_point_to_our_static_type = false;
836 // for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
838 // p->search_above_dst(info, current_ptr, current_ptr, public_path);
839 // if (info->found_our_static_ptr)
840 // does_dst_type_point_to_our_static_type = true;
841 // // break out early here if you can detect it doesn't matter if you do
843 // if (!does_dst_type_point_to_our_static_type)
845 // // We found a dst_type that doesn't point to (static_ptr, static_type)
846 // // So record the address of this dst_ptr and increment the
847 // // count of the number of such dst_types found in the tree.
848 // info->dst_ptr_not_leading_to_static_ptr = current_ptr;
849 // info->number_to_dst_ptr += 1;
854 // // This is not a static_type and not a dst_type.
855 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
857 // p->search_below_dst(info, current_ptr, public_path);
858 // // break out early here if you can detect it doesn't matter if you do
863 __vmi_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
864 const void* current_ptr
,
866 bool use_strcmp
) const
868 typedef const __base_class_type_info
* Iter
;
869 if (is_equal(this, info
->static_type
, use_strcmp
))
870 process_static_type_below_dst(info
, current_ptr
, path_below
);
871 else if (is_equal(this, info
->dst_type
, use_strcmp
))
873 // We've been here before if we've recorded current_ptr in one of these
875 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
876 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
878 // We've seen this node before, and therefore have already searched
879 // its base classes above.
880 // Update path to here that is "most public".
881 if (path_below
== public_path
)
882 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
884 else // We have haven't been here before
886 // Record the access path that got us here
887 // If there is more than one dst_type this path doesn't matter.
888 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
889 bool does_dst_type_point_to_our_static_type
= false;
890 // Only search above here if dst_type derives from static_type, or
891 // if it is unknown if dst_type derives from static_type.
892 if (info
->is_dst_type_derived_from_static_type
!= no
)
894 // Set up flags to record results from all base classes
895 bool is_dst_type_derived_from_static_type
= false;
897 // We've found a dst_type with a potentially public path to here.
898 // We have to assume the path is public because it may become
899 // public later (if we get back to here with a public path).
900 // We can stop looking above if:
901 // 1. We've found a public path to (static_ptr, static_type).
902 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
903 // This is detected at the (static_ptr, static_type).
904 // 3. We can prove that there is no public path to (static_ptr, static_type)
906 const Iter e
= __base_info
+ __base_count
;
907 for (Iter p
= __base_info
; p
< e
; ++p
)
909 // Zero out found flags
910 info
->found_our_static_ptr
= false;
911 info
->found_any_static_type
= false;
912 p
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
913 if (info
->search_done
)
915 if (info
->found_any_static_type
)
917 is_dst_type_derived_from_static_type
= true;
918 if (info
->found_our_static_ptr
)
920 does_dst_type_point_to_our_static_type
= true;
921 // If we found what we're looking for, stop looking above.
922 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
924 // We found a private path to (static_ptr, static_type)
925 // If there is no diamond then there is only one path
926 // to (static_ptr, static_type) and we just found it.
927 if (!(__flags
& __diamond_shaped_mask
))
932 // If we found a static_type that isn't the one we're looking
933 // for, and if there are no repeated types above here,
934 // then stop looking.
935 if (!(__flags
& __non_diamond_repeat_mask
))
940 // If we found no static_type,s then dst_type doesn't derive
941 // from static_type, else it does. Record this result so that
942 // next time we hit a dst_type we will know not to search above
943 // it if it doesn't derive from static_type.
944 if (is_dst_type_derived_from_static_type
)
945 info
->is_dst_type_derived_from_static_type
= yes
;
947 info
->is_dst_type_derived_from_static_type
= no
;
949 if (!does_dst_type_point_to_our_static_type
)
951 // We found a dst_type that doesn't point to (static_ptr, static_type)
952 // So record the address of this dst_ptr and increment the
953 // count of the number of such dst_types found in the tree.
954 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
955 info
->number_to_dst_ptr
+= 1;
956 // If there exists another dst with a private path to
957 // (static_ptr, static_type), then the cast from
958 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
960 if (info
->number_to_static_ptr
== 1 &&
961 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
962 info
->search_done
= true;
968 // This is not a static_type and not a dst_type.
969 const Iter e
= __base_info
+ __base_count
;
970 Iter p
= __base_info
;
971 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
974 if ((__flags
& __diamond_shaped_mask
) || info
->number_to_static_ptr
== 1)
976 // If there are multiple paths to a base above from here, or if
977 // a dst_type pointing to (static_ptr, static_type) has been found,
978 // then there is no way to break out of this loop early unless
979 // something below detects the search is done.
982 if (info
->search_done
)
984 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
987 else if (__flags
& __non_diamond_repeat_mask
)
989 // There are not multiple paths to any base class from here and a
990 // dst_type pointing to (static_ptr, static_type) has not yet been
994 if (info
->search_done
)
996 // If we just found a dst_type with a public path to (static_ptr, static_type),
997 // then the only reason to continue the search is to make sure
998 // no other dst_type points to (static_ptr, static_type).
999 // If !diamond, then we don't need to search here.
1000 if (info
->number_to_static_ptr
== 1 &&
1001 info
->path_dst_ptr_to_static_ptr
== public_path
)
1003 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1008 // There are no repeated types above this node.
1009 // There are no nodes with multiple parents above this node.
1010 // no dst_type has been found to (static_ptr, static_type)
1013 if (info
->search_done
)
1015 // If we just found a dst_type with a public path to (static_ptr, static_type),
1016 // then the only reason to continue the search is to make sure sure
1017 // no other dst_type points to (static_ptr, static_type).
1018 // If !diamond, then we don't need to search here.
1019 // if we just found a dst_type with a private path to (static_ptr, static_type),
1020 // then we're only looking for a public path to (static_ptr, static_type)
1021 // and to check for other dst_types.
1022 // If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
1023 // and not a dst_type under here.
1024 if (info
->number_to_static_ptr
== 1)
1026 p
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1033 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1034 // simplified to the case that there is only a single base class.
1036 __si_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1037 const void* current_ptr
,
1039 bool use_strcmp
) const
1041 if (is_equal(this, info
->static_type
, use_strcmp
))
1042 process_static_type_below_dst(info
, current_ptr
, path_below
);
1043 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1045 // We've been here before if we've recorded current_ptr in one of these
1047 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1048 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1050 // We've seen this node before, and therefore have already searched
1051 // its base classes above.
1052 // Update path to here that is "most public".
1053 if (path_below
== public_path
)
1054 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1056 else // We have haven't been here before
1058 // Record the access path that got us here
1059 // If there is more than one dst_type this path doesn't matter.
1060 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1061 bool does_dst_type_point_to_our_static_type
= false;
1062 // Only search above here if dst_type derives from static_type, or
1063 // if it is unknown if dst_type derives from static_type.
1064 if (info
->is_dst_type_derived_from_static_type
!= no
)
1066 // Set up flags to record results from all base classes
1067 bool is_dst_type_derived_from_static_type
= false;
1068 // Zero out found flags
1069 info
->found_our_static_ptr
= false;
1070 info
->found_any_static_type
= false;
1071 __base_type
->search_above_dst(info
, current_ptr
, current_ptr
, public_path
, use_strcmp
);
1072 if (info
->found_any_static_type
)
1074 is_dst_type_derived_from_static_type
= true;
1075 if (info
->found_our_static_ptr
)
1076 does_dst_type_point_to_our_static_type
= true;
1078 // If we found no static_type,s then dst_type doesn't derive
1079 // from static_type, else it does. Record this result so that
1080 // next time we hit a dst_type we will know not to search above
1081 // it if it doesn't derive from static_type.
1082 if (is_dst_type_derived_from_static_type
)
1083 info
->is_dst_type_derived_from_static_type
= yes
;
1085 info
->is_dst_type_derived_from_static_type
= no
;
1087 if (!does_dst_type_point_to_our_static_type
)
1089 // We found a dst_type that doesn't point to (static_ptr, static_type)
1090 // So record the address of this dst_ptr and increment the
1091 // count of the number of such dst_types found in the tree.
1092 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1093 info
->number_to_dst_ptr
+= 1;
1094 // If there exists another dst with a private path to
1095 // (static_ptr, static_type), then the cast from
1096 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1097 if (info
->number_to_static_ptr
== 1 &&
1098 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1099 info
->search_done
= true;
1105 // This is not a static_type and not a dst_type
1106 __base_type
->search_below_dst(info
, current_ptr
, path_below
, use_strcmp
);
1110 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1111 // simplified to the case that there is no base class.
1113 __class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1114 const void* current_ptr
,
1116 bool use_strcmp
) const
1118 if (is_equal(this, info
->static_type
, use_strcmp
))
1119 process_static_type_below_dst(info
, current_ptr
, path_below
);
1120 else if (is_equal(this, info
->dst_type
, use_strcmp
))
1122 // We've been here before if we've recorded current_ptr in one of these
1124 if (current_ptr
== info
->dst_ptr_leading_to_static_ptr
||
1125 current_ptr
== info
->dst_ptr_not_leading_to_static_ptr
)
1127 // We've seen this node before, and therefore have already searched
1128 // its base classes above.
1129 // Update path to here that is "most public".
1130 if (path_below
== public_path
)
1131 info
->path_dynamic_ptr_to_dst_ptr
= public_path
;
1133 else // We have haven't been here before
1135 // Record the access path that got us here
1136 // If there is more than one dst_type this path doesn't matter.
1137 info
->path_dynamic_ptr_to_dst_ptr
= path_below
;
1138 // We found a dst_type that doesn't point to (static_ptr, static_type)
1139 // So record the address of this dst_ptr and increment the
1140 // count of the number of such dst_types found in the tree.
1141 info
->dst_ptr_not_leading_to_static_ptr
= current_ptr
;
1142 info
->number_to_dst_ptr
+= 1;
1143 // If there exists another dst with a private path to
1144 // (static_ptr, static_type), then the cast from
1145 // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1146 if (info
->number_to_static_ptr
== 1 &&
1147 info
->path_dst_ptr_to_static_ptr
== not_public_path
)
1148 info
->search_done
= true;
1149 // We found that dst_type does not derive from static_type
1150 info
->is_dst_type_derived_from_static_type
= no
;
1155 // Call this function when searching above a dst_type node. This function searches
1156 // for a public path to (static_ptr, static_type).
1157 // This function is guaranteed not to find a node of type dst_type.
1158 // Theoretically this is a very simple function which just stops if it finds a
1159 // static_type node: All the hoopla surrounding the search code is doing
1160 // nothing but looking for excuses to stop the search prematurely (break out of
1161 // the for-loop). That is, the algorithm below is simply an optimization of this:
1163 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1164 // const void* dst_ptr,
1165 // const void* current_ptr,
1166 // int path_below) const
1168 // if (this == info->static_type)
1169 // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1172 // typedef const __base_class_type_info* Iter;
1173 // // This is not a static_type and not a dst_type
1174 // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1176 // p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1177 // // break out early here if you can detect it doesn't matter if you do
1182 __vmi_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1183 const void* dst_ptr
,
1184 const void* current_ptr
,
1186 bool use_strcmp
) const
1188 if (is_equal(this, info
->static_type
, use_strcmp
))
1189 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1192 typedef const __base_class_type_info
* Iter
;
1193 // This is not a static_type and not a dst_type
1194 // Save flags so they can be restored when returning to nodes below.
1195 bool found_our_static_ptr
= info
->found_our_static_ptr
;
1196 bool found_any_static_type
= info
->found_any_static_type
;
1197 // We've found a dst_type below with a path to here. If the path
1198 // to here is not public, there may be another path to here that
1199 // is public. So we have to assume that the path to here is public.
1200 // We can stop looking above if:
1201 // 1. We've found a public path to (static_ptr, static_type).
1202 // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1203 // This is detected at the (static_ptr, static_type).
1204 // 3. We can prove that there is no public path to (static_ptr, static_type)
1206 const Iter e
= __base_info
+ __base_count
;
1207 Iter p
= __base_info
;
1208 // Zero out found flags
1209 info
->found_our_static_ptr
= false;
1210 info
->found_any_static_type
= false;
1211 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1212 found_our_static_ptr
|= info
->found_our_static_ptr
;
1213 found_any_static_type
|= info
->found_any_static_type
;
1218 if (info
->search_done
)
1220 if (info
->found_our_static_ptr
)
1222 // If we found what we're looking for, stop looking above.
1223 if (info
->path_dst_ptr_to_static_ptr
== public_path
)
1225 // We found a private path to (static_ptr, static_type)
1226 // If there is no diamond then there is only one path
1227 // to (static_ptr, static_type) from here and we just found it.
1228 if (!(__flags
& __diamond_shaped_mask
))
1231 else if (info
->found_any_static_type
)
1233 // If we found a static_type that isn't the one we're looking
1234 // for, and if there are no repeated types above here,
1235 // then stop looking.
1236 if (!(__flags
& __non_diamond_repeat_mask
))
1239 // Zero out found flags
1240 info
->found_our_static_ptr
= false;
1241 info
->found_any_static_type
= false;
1242 p
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1243 found_our_static_ptr
|= info
->found_our_static_ptr
;
1244 found_any_static_type
|= info
->found_any_static_type
;
1248 info
->found_our_static_ptr
= found_our_static_ptr
;
1249 info
->found_any_static_type
= found_any_static_type
;
1253 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1254 // simplified to the case that there is only a single base class.
1256 __si_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1257 const void* dst_ptr
,
1258 const void* current_ptr
,
1260 bool use_strcmp
) const
1262 if (is_equal(this, info
->static_type
, use_strcmp
))
1263 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1265 __base_type
->search_above_dst(info
, dst_ptr
, current_ptr
, path_below
, use_strcmp
);
1268 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1269 // simplified to the case that there is no base class.
1271 __class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1272 const void* dst_ptr
,
1273 const void* current_ptr
,
1275 bool use_strcmp
) const
1277 if (is_equal(this, info
->static_type
, use_strcmp
))
1278 process_static_type_above_dst(info
, dst_ptr
, current_ptr
, path_below
);
1281 // The search functions for __base_class_type_info are simply convenience
1282 // functions for adjusting the current_ptr and path_below as the search is
1283 // passed up to the base class node.
1286 __base_class_type_info::search_above_dst(__dynamic_cast_info
* info
,
1287 const void* dst_ptr
,
1288 const void* current_ptr
,
1290 bool use_strcmp
) const
1292 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1293 if (__offset_flags
& __virtual_mask
)
1295 const char* vtable
= *static_cast<const char*const*>(current_ptr
);
1296 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1298 __base_type
->search_above_dst(info
, dst_ptr
,
1299 static_cast<const char*>(current_ptr
) + offset_to_base
,
1300 (__offset_flags
& __public_mask
) ?
1307 __base_class_type_info::search_below_dst(__dynamic_cast_info
* info
,
1308 const void* current_ptr
,
1310 bool use_strcmp
) const
1312 ptrdiff_t offset_to_base
= __offset_flags
>> __offset_shift
;
1313 if (__offset_flags
& __virtual_mask
)
1315 const char* vtable
= *static_cast<const char*const*>(current_ptr
);
1316 offset_to_base
= update_offset_to_base(vtable
, offset_to_base
);
1318 __base_type
->search_below_dst(info
,
1319 static_cast<const char*>(current_ptr
) + offset_to_base
,
1320 (__offset_flags
& __public_mask
) ?