1 //===----------------------- private_typeinfo.cpp -------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "private_typeinfo.h"
10 
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.
14 //
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.
19 //
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.
25 //
26 // The current implementation of _LIBCXXABI_FORGIVING_DYNAMIC_CAST requires a
27 // printf-like function called syslog:
28 //
29 //     void syslog(int facility_priority, const char* format, ...);
30 //
31 // If you want this functionality but your platform doesn't have syslog,
32 // just implement it in terms of fprintf(stderr, ...).
33 //
34 // _LIBCXXABI_FORGIVING_DYNAMIC_CAST is currently off by default.
35 
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.
40 //
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.
43 
44 #include <string.h>
45 
46 #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
47 #include "abort_message.h"
48 #include <sys/syslog.h>
49 #include <atomic>
50 #endif
51 
52 static inline
53 bool
is_equal(const std::type_info * x,const std::type_info * y,bool use_strcmp)54 is_equal(const std::type_info* x, const std::type_info* y, bool use_strcmp)
55 {
56     // Use std::type_info's default comparison unless we've explicitly asked
57     // for strcmp.
58     if (!use_strcmp)
59         return *x == *y;
60     // Still allow pointer equality to short circut.
61     return x == y || strcmp(x->name(), y->name()) == 0;
62 }
63 
update_offset_to_base(const char * vtable,ptrdiff_t offset_to_base)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);
69 #else
70   return *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
71 #endif
72 }
73 
74 namespace __cxxabiv1
75 {
76 
77 // __shim_type_info
78 
~__shim_type_info()79 __shim_type_info::~__shim_type_info()
80 {
81 }
82 
noop1() const83 void __shim_type_info::noop1() const {}
noop2() const84 void __shim_type_info::noop2() const {}
85 
86 // __fundamental_type_info
87 
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
~__fundamental_type_info()92 __fundamental_type_info::~__fundamental_type_info()
93 {
94 }
95 
96 // __array_type_info
97 
~__array_type_info()98 __array_type_info::~__array_type_info()
99 {
100 }
101 
102 // __function_type_info
103 
~__function_type_info()104 __function_type_info::~__function_type_info()
105 {
106 }
107 
108 // __enum_type_info
109 
~__enum_type_info()110 __enum_type_info::~__enum_type_info()
111 {
112 }
113 
114 // __class_type_info
115 
~__class_type_info()116 __class_type_info::~__class_type_info()
117 {
118 }
119 
120 // __si_class_type_info
121 
~__si_class_type_info()122 __si_class_type_info::~__si_class_type_info()
123 {
124 }
125 
126 // __vmi_class_type_info
127 
~__vmi_class_type_info()128 __vmi_class_type_info::~__vmi_class_type_info()
129 {
130 }
131 
132 // __pbase_type_info
133 
~__pbase_type_info()134 __pbase_type_info::~__pbase_type_info()
135 {
136 }
137 
138 // __pointer_type_info
139 
~__pointer_type_info()140 __pointer_type_info::~__pointer_type_info()
141 {
142 }
143 
144 // __pointer_to_member_type_info
145 
~__pointer_to_member_type_info()146 __pointer_to_member_type_info::~__pointer_to_member_type_info()
147 {
148 }
149 
150 // can_catch
151 
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
163 //      std::nullptr_t.
164 
165 // adjustedPtr:
166 //
167 // catch (A& a) : adjustedPtr == &a
168 // catch (A* a) : adjustedPtr == a
169 // catch (A** a) : adjustedPtr == a
170 //
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
174 //
175 // catch (...) : adjustedPtr == & of the exception
176 //
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.
180 
181 // Handles bullet 1
182 bool
can_catch(const __shim_type_info * thrown_type,void * &) const183 __fundamental_type_info::can_catch(const __shim_type_info* thrown_type,
184                                    void*&) const
185 {
186     return is_equal(this, thrown_type, false);
187 }
188 
189 bool
can_catch(const __shim_type_info *,void * &) const190 __array_type_info::can_catch(const __shim_type_info*, void*&) const
191 {
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.
196     return false;
197 }
198 
199 bool
can_catch(const __shim_type_info *,void * &) const200 __function_type_info::can_catch(const __shim_type_info*, void*&) const
201 {
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.
206     return false;
207 }
208 
209 // Handles bullet 1
210 bool
can_catch(const __shim_type_info * thrown_type,void * &) const211 __enum_type_info::can_catch(const __shim_type_info* thrown_type,
212                             void*&) const
213 {
214     return is_equal(this, thrown_type, false);
215 }
216 
217 #ifdef __clang__
218 #pragma clang diagnostic push
219 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
220 #endif
221 
222 // Handles bullets 1 and 2
223 bool
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const224 __class_type_info::can_catch(const __shim_type_info* thrown_type,
225                              void*& adjustedPtr) const
226 {
227     // bullet 1
228     if (is_equal(this, thrown_type, false))
229         return true;
230     const __class_type_info* thrown_class_type =
231         dynamic_cast<const __class_type_info*>(thrown_type);
232     if (thrown_class_type == 0)
233         return false;
234     // bullet 2
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)
239     {
240         adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
241         return true;
242     }
243     return false;
244 }
245 
246 #ifdef __clang__
247 #pragma clang diagnostic pop
248 #endif
249 
250 void
process_found_base_class(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const251 __class_type_info::process_found_base_class(__dynamic_cast_info* info,
252                                                void* adjustedPtr,
253                                                int path_below) const
254 {
255     if (info->dst_ptr_leading_to_static_ptr == 0)
256     {
257         // First time here
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;
261     }
262     else if (info->dst_ptr_leading_to_static_ptr == adjustedPtr)
263     {
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;
267     }
268     else
269     {
270         // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
271         //   to a static_type
272         info->number_to_static_ptr += 1;
273         info->path_dst_ptr_to_static_ptr = not_public_path;
274         info->search_done = true;
275     }
276 }
277 
278 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const279 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
280                                                void* adjustedPtr,
281                                                int path_below) const
282 {
283     if (is_equal(this, info->static_type, false))
284         process_found_base_class(info, adjustedPtr, path_below);
285 }
286 
287 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const288 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
289                                                   void* adjustedPtr,
290                                                   int path_below) const
291 {
292     if (is_equal(this, info->static_type, false))
293         process_found_base_class(info, adjustedPtr, path_below);
294     else
295         __base_type->has_unambiguous_public_base(info, adjustedPtr, path_below);
296 }
297 
298 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const299 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
300                                                     void* adjustedPtr,
301                                                     int path_below) const
302 {
303     ptrdiff_t offset_to_base = 0;
304     if (adjustedPtr != nullptr)
305     {
306         offset_to_base = __offset_flags >> __offset_shift;
307         if (__offset_flags & __virtual_mask)
308         {
309             const char* vtable = *static_cast<const char*const*>(adjustedPtr);
310             offset_to_base = update_offset_to_base(vtable, offset_to_base);
311         }
312     }
313     __base_type->has_unambiguous_public_base(
314             info,
315             static_cast<char*>(adjustedPtr) + offset_to_base,
316             (__offset_flags & __public_mask) ? path_below : not_public_path);
317 }
318 
319 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const320 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
321                                                    void* adjustedPtr,
322                                                    int path_below) const
323 {
324     if (is_equal(this, info->static_type, false))
325         process_found_base_class(info, adjustedPtr, path_below);
326     else
327     {
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);
332         if (++p < e)
333         {
334             do
335             {
336                 p->has_unambiguous_public_base(info, adjustedPtr, path_below);
337                 if (info->search_done)
338                     break;
339             } while (++p < e);
340         }
341     }
342 }
343 
344 // Handles bullet 1 for both pointers and member pointers
345 bool
can_catch(const __shim_type_info * thrown_type,void * &) const346 __pbase_type_info::can_catch(const __shim_type_info* thrown_type,
347                              void*&) const
348 {
349     bool use_strcmp = this->__flags & (__incomplete_class_mask |
350                                        __incomplete_mask);
351     if (!use_strcmp) {
352         const __pbase_type_info* thrown_pbase = dynamic_cast<const __pbase_type_info*>(
353                 thrown_type);
354         if (!thrown_pbase) return false;
355         use_strcmp = thrown_pbase->__flags & (__incomplete_class_mask |
356                                               __incomplete_mask);
357     }
358     return is_equal(this, thrown_type, use_strcmp);
359 }
360 
361 #ifdef __clang__
362 #pragma clang diagnostic push
363 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
364 #endif
365 
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.
369 bool
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const370 __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
371                                void*& adjustedPtr) const
372 {
373     // bullet 4
374     if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
375       adjustedPtr = nullptr;
376       return true;
377     }
378 
379     // bullet 1
380     if (__pbase_type_info::can_catch(thrown_type, adjustedPtr)) {
381         if (adjustedPtr != NULL)
382             adjustedPtr = *static_cast<void**>(adjustedPtr);
383         return true;
384     }
385     // bullet 3
386     const __pointer_type_info* thrown_pointer_type =
387         dynamic_cast<const __pointer_type_info*>(thrown_type);
388     if (thrown_pointer_type == 0)
389         return false;
390     // Do the dereference adjustment
391     if (adjustedPtr != NULL)
392         adjustedPtr = *static_cast<void**>(adjustedPtr);
393     // bullet 3B and 3C
394     if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
395         return false;
396     if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
397         return false;
398     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
399         return true;
400     // bullet 3A
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);
407     }
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);
414     }
415 
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);
422     }
423 
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)
428         return false;
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)
432         return false;
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)
437     {
438         if (adjustedPtr != NULL)
439             adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
440         return true;
441     }
442     return false;
443 }
444 
can_catch_nested(const __shim_type_info * thrown_type) const445 bool __pointer_type_info::can_catch_nested(
446     const __shim_type_info* thrown_type) const
447 {
448   const __pointer_type_info* thrown_pointer_type =
449         dynamic_cast<const __pointer_type_info*>(thrown_type);
450     if (thrown_pointer_type == 0)
451         return false;
452     // bullet 3B
453     if (thrown_pointer_type->__flags & ~__flags)
454         return false;
455     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
456         return true;
457     // If the pointed to types differ then the catch type must be const
458     // qualified.
459     if (~__flags & __const_mask)
460         return false;
461 
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);
468     }
469 
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);
475     }
476 
477     return false;
478 }
479 
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const480 bool __pointer_to_member_type_info::can_catch(
481     const __shim_type_info* thrown_type, void*& adjustedPtr) const {
482     // bullet 4
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.
486       struct X {};
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);
490       } else {
491         static int X::*const null_ptr_rep = nullptr;
492         adjustedPtr = const_cast<int X::**>(&null_ptr_rep);
493       }
494       return true;
495     }
496 
497     // bullet 1
498     if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
499         return true;
500 
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)
504         return false;
505     if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
506         return false;
507     if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
508         return false;
509     if (!is_equal(__pointee, thrown_pointer_type->__pointee, false))
510         return false;
511     if (is_equal(__context, thrown_pointer_type->__context, false))
512         return true;
513 
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.
517 
518     return false;
519 }
520 
can_catch_nested(const __shim_type_info * thrown_type) const521 bool __pointer_to_member_type_info::can_catch_nested(
522     const __shim_type_info* thrown_type) const
523 {
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)
527         return false;
528     if (~__flags & thrown_member_ptr_type->__flags)
529         return false;
530     if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false))
531         return false;
532     if (!is_equal(__context, thrown_member_ptr_type->__context, false))
533         return false;
534     return true;
535 }
536 
537 #ifdef __clang__
538 #pragma clang diagnostic pop
539 #endif
540 
541 #ifdef __clang__
542 #pragma clang diagnostic push
543 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
544 #endif
545 
546 // __dynamic_cast
547 
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:
556 //                     -1: no hint
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
559 //                         virtual base type
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.
563 //
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.
568 //
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.
576 //
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).
580 //
581 // Invariants of the DAG:
582 //
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.
587 //
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.
591 //
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.
596 //
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.
599 //
600 // Returns:
601 //
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.
613 //
614 // If dynamic_type == dst_type, then the above algorithm collapses to the
615 // following cheaper algorithm:
616 //
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.
620 
621 extern "C" _LIBCXXABI_FUNC_VIS void *
__dynamic_cast(const void * static_ptr,const __class_type_info * static_type,const __class_type_info * dst_type,std::ptrdiff_t src2dst_offset)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
626 
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;
635 
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));
642 #else
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]);
647 #endif
648 
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
651     //    be returned.
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,};
655 
656     // Find out if we can use a giant short cut in the search
657     if (is_equal(dynamic_type, dst_type, false))
658     {
659         // Using giant short cut.  Add that information to info.
660         info.number_of_dst_type = 1;
661         // Do the  search
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)
667         {
668             // We get here only if there is some kind of visibility problem
669             //   in client code.
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);
681         }
682 #endif  // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
683         // Query the search.
684         if (info.path_dst_ptr_to_static_ptr == public_path)
685             dst_ptr = dynamic_ptr;
686     }
687     else
688     {
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)
696         {
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(),
705                         dst_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);
709         }
710 #endif  // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
711         // Query the search.
712         switch (info.number_to_static_ptr)
713         {
714         case 0:
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;
719             break;
720         case 1:
721             if (info.path_dst_ptr_to_static_ptr == public_path ||
722                    (
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
726                    )
727                )
728                 dst_ptr = info.dst_ptr_leading_to_static_ptr;
729             break;
730         }
731     }
732     return const_cast<void*>(dst_ptr);
733 }
734 
735 #ifdef __clang__
736 #pragma clang diagnostic pop
737 #endif
738 
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.
748 void
process_static_type_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below) const749 __class_type_info::process_static_type_above_dst(__dynamic_cast_info* info,
750                                                  const void* dst_ptr,
751                                                  const void* current_ptr,
752                                                  int path_below) const
753 {
754     // Record that we found a static_type
755     info->found_any_static_type = true;
756     if (current_ptr == info->static_ptr)
757     {
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)
761         {
762             // First time here
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;
770         }
771         else if (info->dst_ptr_leading_to_static_ptr == dst_ptr)
772         {
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;
780         }
781         else
782         {
783             // We've detected an ambiguous cast from (static_ptr, static_type)
784             //   to a dst_type
785             info->number_to_static_ptr += 1;
786             info->search_done = true;
787         }
788     }
789 }
790 
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.
795 void
process_static_type_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below) const796 __class_type_info::process_static_type_below_dst(__dynamic_cast_info* info,
797                                                  const void* current_ptr,
798                                                  int path_below) const
799 {
800     if (current_ptr == info->static_ptr)
801     {
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;
806     }
807 }
808 
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
812 // above.
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:
822 // void
823 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
824 //                                         const void* current_ptr,
825 //                                         int path_below) const
826 // {
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)
831 //     {
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)
837 //         {
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
842 //         }
843 //         if (!does_dst_type_point_to_our_static_type)
844 //         {
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;
850 //         }
851 //     }
852 //     else
853 //     {
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)
856 //         {
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
859 //         }
860 //     }
861 // }
862 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const863 __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
864                                         const void* current_ptr,
865                                         int path_below,
866                                         bool use_strcmp) const
867 {
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))
872     {
873         // We've been here before if we've recorded current_ptr in one of these
874         //   two places:
875         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
876             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
877         {
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;
883         }
884         else  // We have haven't been here before
885         {
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)
893             {
894                 // Set up flags to record results from all base classes
895                 bool is_dst_type_derived_from_static_type = false;
896 
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)
905                 //        above here.
906                 const Iter e = __base_info + __base_count;
907                 for (Iter p = __base_info; p < e; ++p)
908                 {
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)
914                         break;
915                     if (info->found_any_static_type)
916                     {
917                         is_dst_type_derived_from_static_type = true;
918                         if (info->found_our_static_ptr)
919                         {
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)
923                                 break;
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))
928                                 break;
929                         }
930                         else
931                         {
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))
936                                 break;
937                         }
938                     }
939                 }
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;
946                 else
947                     info->is_dst_type_derived_from_static_type = no;
948               }
949               if (!does_dst_type_point_to_our_static_type)
950               {
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,
959                   //      so stop search.
960                   if (info->number_to_static_ptr == 1 &&
961                           info->path_dst_ptr_to_static_ptr == not_public_path)
962                       info->search_done = true;
963               }
964         }
965     }
966     else
967     {
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);
972         if (++p < e)
973         {
974             if ((__flags & __diamond_shaped_mask) || info->number_to_static_ptr == 1)
975             {
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.
980                 do
981                 {
982                     if (info->search_done)
983                         break;
984                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
985                 } while (++p < e);
986             }
987             else if (__flags & __non_diamond_repeat_mask)
988             {
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
991                 //   found.
992                 do
993                 {
994                     if (info->search_done)
995                         break;
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)
1002                         break;
1003                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1004                 } while (++p < e);
1005             }
1006             else
1007             {
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)
1011                 do
1012                 {
1013                     if (info->search_done)
1014                         break;
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)
1025                         break;
1026                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1027                 } while (++p < e);
1028             }
1029         }
1030     }
1031 }
1032 
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.
1035 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1036 __si_class_type_info::search_below_dst(__dynamic_cast_info* info,
1037                                        const void* current_ptr,
1038                                        int path_below,
1039                                        bool use_strcmp) const
1040 {
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))
1044     {
1045         // We've been here before if we've recorded current_ptr in one of these
1046         //   two places:
1047         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1048             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1049         {
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;
1055         }
1056         else  // We have haven't been here before
1057         {
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)
1065             {
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)
1073                 {
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;
1077                 }
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;
1084                 else
1085                     info->is_dst_type_derived_from_static_type = no;
1086             }
1087             if (!does_dst_type_point_to_our_static_type)
1088             {
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;
1100             }
1101         }
1102     }
1103     else
1104     {
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);
1107     }
1108 }
1109 
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.
1112 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1113 __class_type_info::search_below_dst(__dynamic_cast_info* info,
1114                                     const void* current_ptr,
1115                                     int path_below,
1116                                     bool use_strcmp) const
1117 {
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))
1121     {
1122         // We've been here before if we've recorded current_ptr in one of these
1123         //   two places:
1124         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1125             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1126         {
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;
1132         }
1133         else  // We have haven't been here before
1134         {
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;
1151         }
1152     }
1153 }
1154 
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:
1162 // void
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
1167 // {
1168 //     if (this == info->static_type)
1169 //         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1170 //     else
1171 //     {
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)
1175 //         {
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
1178 //         }
1179 //     }
1180 // }
1181 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1182 __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1183                                         const void* dst_ptr,
1184                                         const void* current_ptr,
1185                                         int path_below,
1186                                         bool use_strcmp) const
1187 {
1188     if (is_equal(this, info->static_type, use_strcmp))
1189         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1190     else
1191     {
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)
1205         //        above here.
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;
1214         if (++p < e)
1215         {
1216             do
1217             {
1218                 if (info->search_done)
1219                     break;
1220                 if (info->found_our_static_ptr)
1221                 {
1222                     // If we found what we're looking for, stop looking above.
1223                     if (info->path_dst_ptr_to_static_ptr == public_path)
1224                         break;
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))
1229                         break;
1230                 }
1231                 else if (info->found_any_static_type)
1232                 {
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))
1237                         break;
1238                 }
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;
1245             } while (++p < e);
1246         }
1247         // Restore flags
1248         info->found_our_static_ptr = found_our_static_ptr;
1249         info->found_any_static_type = found_any_static_type;
1250     }
1251 }
1252 
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.
1255 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1256 __si_class_type_info::search_above_dst(__dynamic_cast_info* info,
1257                                        const void* dst_ptr,
1258                                        const void* current_ptr,
1259                                        int path_below,
1260                                        bool use_strcmp) const
1261 {
1262     if (is_equal(this, info->static_type, use_strcmp))
1263         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1264     else
1265         __base_type->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1266 }
1267 
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.
1270 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1271 __class_type_info::search_above_dst(__dynamic_cast_info* info,
1272                                     const void* dst_ptr,
1273                                     const void* current_ptr,
1274                                     int path_below,
1275                                     bool use_strcmp) const
1276 {
1277     if (is_equal(this, info->static_type, use_strcmp))
1278         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1279 }
1280 
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.
1284 
1285 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1286 __base_class_type_info::search_above_dst(__dynamic_cast_info* info,
1287                                          const void* dst_ptr,
1288                                          const void* current_ptr,
1289                                          int path_below,
1290                                          bool use_strcmp) const
1291 {
1292     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1293     if (__offset_flags & __virtual_mask)
1294     {
1295         const char* vtable = *static_cast<const char*const*>(current_ptr);
1296         offset_to_base = update_offset_to_base(vtable, offset_to_base);
1297     }
1298     __base_type->search_above_dst(info, dst_ptr,
1299                                   static_cast<const char*>(current_ptr) + offset_to_base,
1300                                   (__offset_flags & __public_mask) ?
1301                                       path_below :
1302                                       not_public_path,
1303                                   use_strcmp);
1304 }
1305 
1306 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1307 __base_class_type_info::search_below_dst(__dynamic_cast_info* info,
1308                                          const void* current_ptr,
1309                                          int path_below,
1310                                          bool use_strcmp) const
1311 {
1312     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1313     if (__offset_flags & __virtual_mask)
1314     {
1315         const char* vtable = *static_cast<const char*const*>(current_ptr);
1316         offset_to_base = update_offset_to_base(vtable, offset_to_base);
1317     }
1318     __base_type->search_below_dst(info,
1319                                   static_cast<const char*>(current_ptr) + offset_to_base,
1320                                   (__offset_flags & __public_mask) ?
1321                                       path_below :
1322                                       not_public_path,
1323                                   use_strcmp);
1324 }
1325 
1326 }  // __cxxabiv1
1327