• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------- private_typeinfo.cpp -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "private_typeinfo.h"
11 
12 // The flag _LIBCXX_DYNAMIC_FALLBACK is used to make dynamic_cast more
13 // forgiving when type_info's mistakenly have hidden visibility and thus
14 // multiple type_infos can exist for a single type.
15 //
16 // When _LIBCXX_DYNAMIC_FALLBACK is defined, and only in the case where
17 // there is a detected inconsistency in the type_info hierarchy during a
18 // dynamic_cast, then the equality operation will fall back to using strcmp
19 // on type_info names to determine type_info equality.
20 //
21 // This change happens *only* under dynamic_cast, and only when
22 // dynamic_cast is faced with the choice:  abort, or possibly give back the
23 // wrong answer.  If when the dynamic_cast is done with this fallback
24 // algorithm and an inconsistency is still detected, dynamic_cast will call
25 // abort with an appropriate message.
26 //
27 // The current implementation of _LIBCXX_DYNAMIC_FALLBACK requires a
28 // printf-like function called syslog:
29 //
30 //     void syslog(int facility_priority, const char* format, ...);
31 //
32 // If you want this functionality but your platform doesn't have syslog,
33 // just implement it in terms of fprintf(stderr, ...).
34 //
35 // _LIBCXX_DYNAMIC_FALLBACK is currently off by default.
36 
37 
38 #include <string.h>
39 
40 
41 #ifdef _LIBCXX_DYNAMIC_FALLBACK
42 #include "abort_message.h"
43 #include <sys/syslog.h>
44 #endif
45 
46 // On Windows, typeids are different between DLLs and EXEs, so comparing
47 // type_info* will work for typeids from the same compiled file but fail
48 // for typeids from a DLL and an executable. Among other things, exceptions
49 // are not caught by handlers since can_catch() returns false.
50 //
51 // Defining _LIBCXX_DYNAMIC_FALLBACK does not help since can_catch() calls
52 // is_equal() with use_strcmp=false so the string names are not compared.
53 
54 #ifdef _WIN32
55 #include <string.h>
56 #endif
57 
58 static inline
59 bool
is_equal(const std::type_info * x,const std::type_info * y,bool use_strcmp)60 is_equal(const std::type_info* x, const std::type_info* y, bool use_strcmp)
61 {
62 #ifndef _WIN32
63     if (!use_strcmp)
64         return x == y;
65     return strcmp(x->name(), y->name()) == 0;
66 #else
67     (void) use_strcmp;
68     return (x == y) || (strcmp(x->name(), y->name()) == 0);
69 #endif
70 }
71 
72 namespace __cxxabiv1
73 {
74 
75 // __shim_type_info
76 
~__shim_type_info()77 __shim_type_info::~__shim_type_info()
78 {
79 }
80 
noop1() const81 void __shim_type_info::noop1() const {}
noop2() const82 void __shim_type_info::noop2() const {}
83 
84 // __fundamental_type_info
85 
86 // This miraculously (compiler magic) emits the type_info's for:
87 //   1. all of the fundamental types
88 //   2. pointers to all of the fundamental types
89 //   3. pointers to all of the const fundamental types
~__fundamental_type_info()90 __fundamental_type_info::~__fundamental_type_info()
91 {
92 }
93 
94 // __array_type_info
95 
~__array_type_info()96 __array_type_info::~__array_type_info()
97 {
98 }
99 
100 // __function_type_info
101 
~__function_type_info()102 __function_type_info::~__function_type_info()
103 {
104 }
105 
106 // __enum_type_info
107 
~__enum_type_info()108 __enum_type_info::~__enum_type_info()
109 {
110 }
111 
112 // __class_type_info
113 
~__class_type_info()114 __class_type_info::~__class_type_info()
115 {
116 }
117 
118 // __si_class_type_info
119 
~__si_class_type_info()120 __si_class_type_info::~__si_class_type_info()
121 {
122 }
123 
124 // __vmi_class_type_info
125 
~__vmi_class_type_info()126 __vmi_class_type_info::~__vmi_class_type_info()
127 {
128 }
129 
130 // __pbase_type_info
131 
~__pbase_type_info()132 __pbase_type_info::~__pbase_type_info()
133 {
134 }
135 
136 // __pointer_type_info
137 
~__pointer_type_info()138 __pointer_type_info::~__pointer_type_info()
139 {
140 }
141 
142 // __pointer_to_member_type_info
143 
~__pointer_to_member_type_info()144 __pointer_to_member_type_info::~__pointer_to_member_type_info()
145 {
146 }
147 
148 // can_catch
149 
150 // A handler is a match for an exception object of type E if
151 //   1. The handler is of type cv T or cv T& and E and T are the same type
152 //      (ignoring the top-level cv-qualifiers), or
153 //   2. the handler is of type cv T or cv T& and T is an unambiguous public
154 //       base class of E, or
155 //   3. the handler is of type cv1 T* cv2 and E is a pointer type that can be
156 //      converted to the type of the handler by either or both of
157 //      A. a standard pointer conversion (4.10) not involving conversions to
158 //         pointers to private or protected or ambiguous classes
159 //      B. a qualification conversion
160 //   4. the handler is a pointer or pointer to member type and E is
161 //      std::nullptr_t.
162 
163 // adjustedPtr:
164 //
165 // catch (A& a) : adjustedPtr == &a
166 // catch (A* a) : adjustedPtr == a
167 // catch (A** a) : adjustedPtr == a
168 //
169 // catch (D2& d2) : adjustedPtr == &d2  (d2 is base class of thrown object)
170 // catch (D2* d2) : adjustedPtr == d2
171 // catch (D2*& d2) : adjustedPtr == d2
172 //
173 // catch (...) : adjustedPtr == & of the exception
174 //
175 // If the thrown type is nullptr_t and the caught type is a pointer to
176 // member type, adjustedPtr points to a statically-allocated null pointer
177 // representation of that type.
178 
179 // Handles bullet 1
180 bool
can_catch(const __shim_type_info * thrown_type,void * &) const181 __fundamental_type_info::can_catch(const __shim_type_info* thrown_type,
182                                    void*&) const
183 {
184     return is_equal(this, thrown_type, false);
185 }
186 
187 bool
can_catch(const __shim_type_info *,void * &) const188 __array_type_info::can_catch(const __shim_type_info*, void*&) const
189 {
190     // We can get here if someone tries to catch an array by reference.
191     //   However if someone tries to throw an array, it immediately gets
192     //   converted to a pointer, which will not convert back to an array
193     //   at the catch clause.  So this can never catch anything.
194     return false;
195 }
196 
197 bool
can_catch(const __shim_type_info *,void * &) const198 __function_type_info::can_catch(const __shim_type_info*, void*&) const
199 {
200     // We can get here if someone tries to catch a function by reference.
201     //   However if someone tries to throw a function, it immediately gets
202     //   converted to a pointer, which will not convert back to a function
203     //   at the catch clause.  So this can never catch anything.
204     return false;
205 }
206 
207 // Handles bullet 1
208 bool
can_catch(const __shim_type_info * thrown_type,void * &) const209 __enum_type_info::can_catch(const __shim_type_info* thrown_type,
210                             void*&) const
211 {
212     return is_equal(this, thrown_type, false);
213 }
214 
215 #ifdef __clang__
216 #pragma clang diagnostic push
217 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
218 #endif
219 
220 // Handles bullets 1 and 2
221 bool
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const222 __class_type_info::can_catch(const __shim_type_info* thrown_type,
223                              void*& adjustedPtr) const
224 {
225     // bullet 1
226     if (is_equal(this, thrown_type, false))
227         return true;
228     const __class_type_info* thrown_class_type =
229         dynamic_cast<const __class_type_info*>(thrown_type);
230     if (thrown_class_type == 0)
231         return false;
232     // bullet 2
233     __dynamic_cast_info info = {thrown_class_type, 0, this, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
234     info.number_of_dst_type = 1;
235     thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
236     if (info.path_dst_ptr_to_static_ptr == public_path)
237     {
238         adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
239         return true;
240     }
241     return false;
242 }
243 
244 #ifdef __clang__
245 #pragma clang diagnostic pop
246 #endif
247 
248 void
process_found_base_class(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const249 __class_type_info::process_found_base_class(__dynamic_cast_info* info,
250                                                void* adjustedPtr,
251                                                int path_below) const
252 {
253     if (info->dst_ptr_leading_to_static_ptr == 0)
254     {
255         // First time here
256         info->dst_ptr_leading_to_static_ptr = adjustedPtr;
257         info->path_dst_ptr_to_static_ptr = path_below;
258         info->number_to_static_ptr = 1;
259     }
260     else if (info->dst_ptr_leading_to_static_ptr == adjustedPtr)
261     {
262         // We've been here before.  Update path to "most public"
263         if (info->path_dst_ptr_to_static_ptr == not_public_path)
264             info->path_dst_ptr_to_static_ptr = path_below;
265     }
266     else
267     {
268         // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
269         //   to a static_type
270         info->number_to_static_ptr += 1;
271         info->path_dst_ptr_to_static_ptr = not_public_path;
272         info->search_done = true;
273     }
274 }
275 
276 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const277 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
278                                                void* adjustedPtr,
279                                                int path_below) const
280 {
281     if (is_equal(this, info->static_type, false))
282         process_found_base_class(info, adjustedPtr, path_below);
283 }
284 
285 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const286 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
287                                                   void* adjustedPtr,
288                                                   int path_below) const
289 {
290     if (is_equal(this, info->static_type, false))
291         process_found_base_class(info, adjustedPtr, path_below);
292     else
293         __base_type->has_unambiguous_public_base(info, adjustedPtr, path_below);
294 }
295 
296 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const297 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
298                                                     void* adjustedPtr,
299                                                     int path_below) const
300 {
301     ptrdiff_t offset_to_base = 0;
302     if (adjustedPtr != nullptr)
303     {
304         offset_to_base = __offset_flags >> __offset_shift;
305         if (__offset_flags & __virtual_mask)
306         {
307             const char* vtable = *static_cast<const char*const*>(adjustedPtr);
308             offset_to_base = *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
309         }
310     }
311     __base_type->has_unambiguous_public_base(
312             info,
313             static_cast<char*>(adjustedPtr) + offset_to_base,
314             (__offset_flags & __public_mask) ? path_below : not_public_path);
315 }
316 
317 void
has_unambiguous_public_base(__dynamic_cast_info * info,void * adjustedPtr,int path_below) const318 __vmi_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
319                                                    void* adjustedPtr,
320                                                    int path_below) const
321 {
322     if (is_equal(this, info->static_type, false))
323         process_found_base_class(info, adjustedPtr, path_below);
324     else
325     {
326         typedef const __base_class_type_info* Iter;
327         const Iter e = __base_info + __base_count;
328         Iter p = __base_info;
329         p->has_unambiguous_public_base(info, adjustedPtr, path_below);
330         if (++p < e)
331         {
332             do
333             {
334                 p->has_unambiguous_public_base(info, adjustedPtr, path_below);
335                 if (info->search_done)
336                     break;
337             } while (++p < e);
338         }
339     }
340 }
341 
342 // Handles bullet 1 for both pointers and member pointers
343 bool
can_catch(const __shim_type_info * thrown_type,void * &) const344 __pbase_type_info::can_catch(const __shim_type_info* thrown_type,
345                              void*&) const
346 {
347     bool use_strcmp = this->__flags & (__incomplete_class_mask |
348                                        __incomplete_mask);
349     if (!use_strcmp) {
350         const __pbase_type_info* thrown_pbase = dynamic_cast<const __pbase_type_info*>(
351                 thrown_type);
352         if (!thrown_pbase) return false;
353         use_strcmp = thrown_pbase->__flags & (__incomplete_class_mask |
354                                               __incomplete_mask);
355     }
356     return is_equal(this, thrown_type, use_strcmp);
357 }
358 
359 #ifdef __clang__
360 #pragma clang diagnostic push
361 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
362 #endif
363 
364 // Handles bullets 1, 3 and 4
365 // NOTE: It might not be safe to adjust the pointer if it is not not a pointer
366 // type. Only adjust the pointer after we know it is safe to do so.
367 bool
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const368 __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
369                                void*& adjustedPtr) const
370 {
371     // bullet 4
372     if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
373       adjustedPtr = nullptr;
374       return true;
375     }
376 
377     // bullet 1
378     if (__pbase_type_info::can_catch(thrown_type, adjustedPtr)) {
379         if (adjustedPtr != NULL)
380             adjustedPtr = *static_cast<void**>(adjustedPtr);
381         return true;
382     }
383     // bullet 3
384     const __pointer_type_info* thrown_pointer_type =
385         dynamic_cast<const __pointer_type_info*>(thrown_type);
386     if (thrown_pointer_type == 0)
387         return false;
388     // Do the dereference adjustment
389     if (adjustedPtr != NULL)
390         adjustedPtr = *static_cast<void**>(adjustedPtr);
391     // bullet 3B and 3C
392     if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
393         return false;
394     if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
395         return false;
396     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
397         return true;
398     // bullet 3A
399     if (is_equal(__pointee, &typeid(void), false)) {
400         // pointers to functions cannot be converted to void*.
401         // pointers to member functions are not handled here.
402         const __function_type_info* thrown_function =
403             dynamic_cast<const __function_type_info*>(thrown_pointer_type->__pointee);
404         return (thrown_function == nullptr);
405     }
406     // Handle pointer to pointer
407     const __pointer_type_info* nested_pointer_type =
408         dynamic_cast<const __pointer_type_info*>(__pointee);
409     if (nested_pointer_type) {
410         if (~__flags & __const_mask) return false;
411         return nested_pointer_type->can_catch_nested(thrown_pointer_type->__pointee);
412     }
413 
414     // Handle pointer to pointer to member
415     const __pointer_to_member_type_info* member_ptr_type =
416         dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
417     if (member_ptr_type) {
418         if (~__flags & __const_mask) return false;
419         return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
420     }
421 
422     // Handle pointer to class type
423     const __class_type_info* catch_class_type =
424         dynamic_cast<const __class_type_info*>(__pointee);
425     if (catch_class_type == 0)
426         return false;
427     const __class_type_info* thrown_class_type =
428         dynamic_cast<const __class_type_info*>(thrown_pointer_type->__pointee);
429     if (thrown_class_type == 0)
430         return false;
431     __dynamic_cast_info info = {thrown_class_type, 0, catch_class_type, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
432     info.number_of_dst_type = 1;
433     thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
434     if (info.path_dst_ptr_to_static_ptr == public_path)
435     {
436         if (adjustedPtr != NULL)
437             adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
438         return true;
439     }
440     return false;
441 }
442 
can_catch_nested(const __shim_type_info * thrown_type) const443 bool __pointer_type_info::can_catch_nested(
444     const __shim_type_info* thrown_type) const
445 {
446   const __pointer_type_info* thrown_pointer_type =
447         dynamic_cast<const __pointer_type_info*>(thrown_type);
448     if (thrown_pointer_type == 0)
449         return false;
450     // bullet 3B
451     if (thrown_pointer_type->__flags & ~__flags)
452         return false;
453     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
454         return true;
455     // If the pointed to types differ then the catch type must be const
456     // qualified.
457     if (~__flags & __const_mask)
458         return false;
459 
460     // Handle pointer to pointer
461     const __pointer_type_info* nested_pointer_type =
462         dynamic_cast<const __pointer_type_info*>(__pointee);
463     if (nested_pointer_type) {
464         return nested_pointer_type->can_catch_nested(
465             thrown_pointer_type->__pointee);
466     }
467 
468     // Handle pointer to pointer to member
469     const __pointer_to_member_type_info* member_ptr_type =
470         dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
471     if (member_ptr_type) {
472         return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
473     }
474 
475     return false;
476 }
477 
can_catch(const __shim_type_info * thrown_type,void * & adjustedPtr) const478 bool __pointer_to_member_type_info::can_catch(
479     const __shim_type_info* thrown_type, void*& adjustedPtr) const {
480     // bullet 4
481     if (is_equal(thrown_type, &typeid(std::nullptr_t), false)) {
482       // We assume that the pointer to member representation is the same for
483       // all pointers to data members and for all pointers to member functions.
484       struct X {};
485       if (dynamic_cast<const __function_type_info*>(__pointee)) {
486         static int (X::*const null_ptr_rep)() = nullptr;
487         adjustedPtr = const_cast<int (X::**)()>(&null_ptr_rep);
488       } else {
489         static int X::*const null_ptr_rep = nullptr;
490         adjustedPtr = const_cast<int X::**>(&null_ptr_rep);
491       }
492       return true;
493     }
494 
495     // bullet 1
496     if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
497         return true;
498 
499     const __pointer_to_member_type_info* thrown_pointer_type =
500         dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
501     if (thrown_pointer_type == 0)
502         return false;
503     if (thrown_pointer_type->__flags & ~__flags & __no_remove_flags_mask)
504         return false;
505     if (__flags & ~thrown_pointer_type->__flags & __no_add_flags_mask)
506         return false;
507     if (!is_equal(__pointee, thrown_pointer_type->__pointee, false))
508         return false;
509     if (is_equal(__context, thrown_pointer_type->__context, false))
510         return true;
511 
512     // [except.handle] does not allow the pointer-to-member conversions mentioned
513     // in [mem.conv] to take place. For this reason we don't check Derived->Base
514     // for Derived->Base conversions.
515 
516     return false;
517 }
518 
can_catch_nested(const __shim_type_info * thrown_type) const519 bool __pointer_to_member_type_info::can_catch_nested(
520     const __shim_type_info* thrown_type) const
521 {
522     const __pointer_to_member_type_info* thrown_member_ptr_type =
523         dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
524     if (thrown_member_ptr_type == 0)
525         return false;
526     if (~__flags & thrown_member_ptr_type->__flags)
527         return false;
528     if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false))
529         return false;
530     if (!is_equal(__context, thrown_member_ptr_type->__context, false))
531         return false;
532     return true;
533 }
534 
535 #ifdef __clang__
536 #pragma clang diagnostic pop
537 #endif
538 
539 #ifdef __clang__
540 #pragma clang diagnostic push
541 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
542 #endif
543 
544 // __dynamic_cast
545 
546 // static_ptr: pointer to an object of type static_type; nonnull, and since the
547 //   object is polymorphic, *(void**)static_ptr is a virtual table pointer.
548 //   static_ptr is &v in the expression dynamic_cast<T>(v).
549 // static_type: static type of the object pointed to by static_ptr.
550 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
551 // src2dst_offset: a static hint about the location of the
552 //                 source subobject with respect to the complete object;
553 //                 special negative values are:
554 //                     -1: no hint
555 //                     -2: static_type is not a public base of dst_type
556 //                     -3: static_type is a multiple public base type but never a
557 //                         virtual base type
558 //                 otherwise, the static_type type is a unique public nonvirtual
559 //                 base type of dst_type at offset src2dst_offset from the
560 //                 origin of dst_type.
561 //
562 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
563 // referred to by static_ptr and a pointer to it.  These can be found from
564 // static_ptr for polymorphic types.
565 // static_type is guaranteed to be a polymorphic type.
566 //
567 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward.  Each
568 // node of the tree represents a base class/object of its parent (or parents) below.
569 // Each node is uniquely represented by a pointer to the object, and a pointer
570 // to a type_info - its type.  Different nodes may have the same pointer and
571 // different nodes may have the same type.  But only one node has a specific
572 // (pointer-value, type) pair.  In C++ two objects of the same type can not
573 // share the same address.
574 //
575 // There are two flavors of nodes which have the type dst_type:
576 //    1.  Those that are derived from (below) (static_ptr, static_type).
577 //    2.  Those that are not derived from (below) (static_ptr, static_type).
578 //
579 // Invariants of the DAG:
580 //
581 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
582 // the node (static_ptr, static_type).  This path may or may not be public.
583 // There may be more than one such path (some public some not).  Such a path may
584 // or may not go through a node having type dst_type.
585 //
586 // No node of type T appears above a node of the same type.  That means that
587 // there is only one node with dynamic_type.  And if dynamic_type == dst_type,
588 // then there is only one dst_type in the DAG.
589 //
590 // No node of type dst_type appears above a node of type static_type.  Such
591 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
592 // compile time, and only calls __dynamic_cast when dst_type lies below
593 // static_type in the DAG.
594 //
595 // dst_type != static_type:  The compiler computes the dynamic_cast in this case too.
596 // dynamic_type != static_type:  The compiler computes the dynamic_cast in this case too.
597 //
598 // Returns:
599 //
600 // If there is exactly one dst_type of flavor 1, and
601 //    If there is a public path from that dst_type to (static_ptr, static_type), or
602 //    If there are 0 dst_types of flavor 2, and there is a public path from
603 //        (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
604 //        path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
605 //        a pointer to that dst_type.
606 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
607 //    if there is a public path from (dynamic_ptr, dynamic_type) to
608 //    (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
609 //    to the one dst_type, then return a pointer to that one dst_type.
610 // Else return nullptr.
611 //
612 // If dynamic_type == dst_type, then the above algorithm collapses to the
613 // following cheaper algorithm:
614 //
615 // If there is a public path from (dynamic_ptr, dynamic_type) to
616 //    (static_ptr, static_type), then return dynamic_ptr.
617 // Else return nullptr.
618 
619 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)620 __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
621                const __class_type_info *dst_type,
622                std::ptrdiff_t src2dst_offset) {
623     // Possible future optimization:  Take advantage of src2dst_offset
624     // Currently clang always sets src2dst_offset to -1 (no hint).
625 
626     // Get (dynamic_ptr, dynamic_type) from static_ptr
627     void **vtable = *static_cast<void ** const *>(static_ptr);
628     ptrdiff_t offset_to_derived = reinterpret_cast<ptrdiff_t>(vtable[-2]);
629     const void* dynamic_ptr = static_cast<const char*>(static_ptr) + offset_to_derived;
630     const __class_type_info* dynamic_type = static_cast<const __class_type_info*>(vtable[-1]);
631 
632     // Initialize answer to nullptr.  This will be changed from the search
633     //    results if a non-null answer is found.  Regardless, this is what will
634     //    be returned.
635     const void* dst_ptr = 0;
636     // Initialize info struct for this search.
637     __dynamic_cast_info info = {dst_type, static_ptr, static_type, src2dst_offset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
638 
639     // Find out if we can use a giant short cut in the search
640     if (is_equal(dynamic_type, dst_type, false))
641     {
642         // Using giant short cut.  Add that information to info.
643         info.number_of_dst_type = 1;
644         // Do the  search
645         dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, false);
646 #ifdef _LIBCXX_DYNAMIC_FALLBACK
647         // The following if should always be false because we should definitely
648         //   find (static_ptr, static_type), either on a public or private path
649         if (info.path_dst_ptr_to_static_ptr == unknown)
650         {
651             // We get here only if there is some kind of visibility problem
652             //   in client code.
653             syslog(LOG_ERR, "dynamic_cast error 1: Both of the following type_info's "
654                     "should have public visibility. At least one of them is hidden. %s"
655                     ", %s.\n", static_type->name(), dynamic_type->name());
656             // Redo the search comparing type_info's using strcmp
657             info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
658             info.number_of_dst_type = 1;
659             dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, true);
660         }
661 #endif  // _LIBCXX_DYNAMIC_FALLBACK
662         // Query the search.
663         if (info.path_dst_ptr_to_static_ptr == public_path)
664             dst_ptr = dynamic_ptr;
665     }
666     else
667     {
668         // Not using giant short cut.  Do the search
669         dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, false);
670  #ifdef _LIBCXX_DYNAMIC_FALLBACK
671         // The following if should always be false because we should definitely
672         //   find (static_ptr, static_type), either on a public or private path
673         if (info.path_dst_ptr_to_static_ptr == unknown &&
674             info.path_dynamic_ptr_to_static_ptr == unknown)
675         {
676             syslog(LOG_ERR, "dynamic_cast error 2: One or more of the following type_info's "
677                             "has hidden visibility or is defined in more than one translation "
678                             "unit. They should all have public visibility. "
679                             "%s, %s, %s.\n", static_type->name(), dynamic_type->name(),
680                     dst_type->name());
681             // Redo the search comparing type_info's using strcmp
682             info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
683             dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, true);
684         }
685 #endif  // _LIBCXX_DYNAMIC_FALLBACK
686         // Query the search.
687         switch (info.number_to_static_ptr)
688         {
689         case 0:
690             if (info.number_to_dst_ptr == 1 &&
691                     info.path_dynamic_ptr_to_static_ptr == public_path &&
692                     info.path_dynamic_ptr_to_dst_ptr == public_path)
693                 dst_ptr = info.dst_ptr_not_leading_to_static_ptr;
694             break;
695         case 1:
696             if (info.path_dst_ptr_to_static_ptr == public_path ||
697                    (
698                        info.number_to_dst_ptr == 0 &&
699                        info.path_dynamic_ptr_to_static_ptr == public_path &&
700                        info.path_dynamic_ptr_to_dst_ptr == public_path
701                    )
702                )
703                 dst_ptr = info.dst_ptr_leading_to_static_ptr;
704             break;
705         }
706     }
707     return const_cast<void*>(dst_ptr);
708 }
709 
710 #ifdef __clang__
711 #pragma clang diagnostic pop
712 #endif
713 
714 // Call this function when you hit a static_type which is a base (above) a dst_type.
715 // Let caller know you hit a static_type.  But only start recording details if
716 // this is (static_ptr, static_type) -- the node we are casting from.
717 // If this is (static_ptr, static_type)
718 //   Record the path (public or not) from the dst_type to here.  There may be
719 //   multiple paths from the same dst_type to here, record the "most public" one.
720 //   Record the dst_ptr as pointing to (static_ptr, static_type).
721 //   If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
722 //   then mark this dyanmic_cast as ambiguous and stop the search.
723 void
process_static_type_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below) const724 __class_type_info::process_static_type_above_dst(__dynamic_cast_info* info,
725                                                  const void* dst_ptr,
726                                                  const void* current_ptr,
727                                                  int path_below) const
728 {
729     // Record that we found a static_type
730     info->found_any_static_type = true;
731     if (current_ptr == info->static_ptr)
732     {
733         // Record that we found (static_ptr, static_type)
734         info->found_our_static_ptr = true;
735         if (info->dst_ptr_leading_to_static_ptr == 0)
736         {
737             // First time here
738             info->dst_ptr_leading_to_static_ptr = dst_ptr;
739             info->path_dst_ptr_to_static_ptr = path_below;
740             info->number_to_static_ptr = 1;
741             // If there is only one dst_type in the entire tree and the path from
742             //    there to here is public then we are done!
743             if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
744                 info->search_done = true;
745         }
746         else if (info->dst_ptr_leading_to_static_ptr == dst_ptr)
747         {
748             // We've been here before.  Update path to "most public"
749             if (info->path_dst_ptr_to_static_ptr == not_public_path)
750                 info->path_dst_ptr_to_static_ptr = path_below;
751             // If there is only one dst_type in the entire tree and the path from
752             //    there to here is public then we are done!
753             if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
754                 info->search_done = true;
755         }
756         else
757         {
758             // We've detected an ambiguous cast from (static_ptr, static_type)
759             //   to a dst_type
760             info->number_to_static_ptr += 1;
761             info->search_done = true;
762         }
763     }
764 }
765 
766 // Call this function when you hit a static_type which is not a base (above) a dst_type.
767 // If this is (static_ptr, static_type)
768 //   Record the path (public or not) from (dynamic_ptr, dynamic_type) to here.  There may be
769 //   multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
770 void
process_static_type_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below) const771 __class_type_info::process_static_type_below_dst(__dynamic_cast_info* info,
772                                                  const void* current_ptr,
773                                                  int path_below) const
774 {
775     if (current_ptr == info->static_ptr)
776     {
777         // Record the most public path from (dynamic_ptr, dynamic_type) to
778         //                                  (static_ptr, static_type)
779         if (info->path_dynamic_ptr_to_static_ptr != public_path)
780             info->path_dynamic_ptr_to_static_ptr = path_below;
781     }
782 }
783 
784 // Call this function when searching below a dst_type node.  This function searches
785 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
786 // If it finds a static_type node, there is no need to further search base classes
787 // above.
788 // If it finds a dst_type node it should search base classes using search_above_dst
789 // to find out if this dst_type points to (static_ptr, static_type) or not.
790 // Either way, the dst_type is recorded as one of two "flavors":  one that does
791 // or does not point to (static_ptr, static_type).
792 // If this is neither a static_type nor a dst_type node, continue searching
793 // base classes above.
794 // All the hoopla surrounding the search code is doing nothing but looking for
795 // excuses to stop the search prematurely (break out of the for-loop).  That is,
796 // the algorithm below is simply an optimization of this:
797 // void
798 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
799 //                                         const void* current_ptr,
800 //                                         int path_below) const
801 // {
802 //     typedef const __base_class_type_info* Iter;
803 //     if (this == info->static_type)
804 //         process_static_type_below_dst(info, current_ptr, path_below);
805 //     else if (this == info->dst_type)
806 //     {
807 //         // Record the most public access path that got us here
808 //         if (info->path_dynamic_ptr_to_dst_ptr != public_path)
809 //             info->path_dynamic_ptr_to_dst_ptr = path_below;
810 //         bool does_dst_type_point_to_our_static_type = false;
811 //         for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
812 //         {
813 //             p->search_above_dst(info, current_ptr, current_ptr, public_path);
814 //             if (info->found_our_static_ptr)
815 //                 does_dst_type_point_to_our_static_type = true;
816 //             // break out early here if you can detect it doesn't matter if you do
817 //         }
818 //         if (!does_dst_type_point_to_our_static_type)
819 //         {
820 //             // We found a dst_type that doesn't point to (static_ptr, static_type)
821 //             // So record the address of this dst_ptr and increment the
822 //             // count of the number of such dst_types found in the tree.
823 //             info->dst_ptr_not_leading_to_static_ptr = current_ptr;
824 //             info->number_to_dst_ptr += 1;
825 //         }
826 //     }
827 //     else
828 //     {
829 //         // This is not a static_type and not a dst_type.
830 //         for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
831 //         {
832 //             p->search_below_dst(info, current_ptr, public_path);
833 //             // break out early here if you can detect it doesn't matter if you do
834 //         }
835 //     }
836 // }
837 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const838 __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
839                                         const void* current_ptr,
840                                         int path_below,
841                                         bool use_strcmp) const
842 {
843     typedef const __base_class_type_info* Iter;
844     if (is_equal(this, info->static_type, use_strcmp))
845         process_static_type_below_dst(info, current_ptr, path_below);
846     else if (is_equal(this, info->dst_type, use_strcmp))
847     {
848         // We've been here before if we've recorded current_ptr in one of these
849         //   two places:
850         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
851             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
852         {
853             // We've seen this node before, and therefore have already searched
854             // its base classes above.
855             //  Update path to here that is "most public".
856             if (path_below == public_path)
857                 info->path_dynamic_ptr_to_dst_ptr = public_path;
858         }
859         else  // We have haven't been here before
860         {
861             // Record the access path that got us here
862             //   If there is more than one dst_type this path doesn't matter.
863             info->path_dynamic_ptr_to_dst_ptr = path_below;
864             bool does_dst_type_point_to_our_static_type = false;
865             // Only search above here if dst_type derives from static_type, or
866             //    if it is unknown if dst_type derives from static_type.
867             if (info->is_dst_type_derived_from_static_type != no)
868             {
869                 // Set up flags to record results from all base classes
870                 bool is_dst_type_derived_from_static_type = false;
871 
872                 // We've found a dst_type with a potentially public path to here.
873                 // We have to assume the path is public because it may become
874                 //   public later (if we get back to here with a public path).
875                 // We can stop looking above if:
876                 //    1.  We've found a public path to (static_ptr, static_type).
877                 //    2.  We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
878                 //        This is detected at the (static_ptr, static_type).
879                 //    3.  We can prove that there is no public path to (static_ptr, static_type)
880                 //        above here.
881                 const Iter e = __base_info + __base_count;
882                 for (Iter p = __base_info; p < e; ++p)
883                 {
884                     // Zero out found flags
885                     info->found_our_static_ptr = false;
886                     info->found_any_static_type = false;
887                     p->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
888                     if (info->search_done)
889                         break;
890                     if (info->found_any_static_type)
891                     {
892                         is_dst_type_derived_from_static_type = true;
893                         if (info->found_our_static_ptr)
894                         {
895                             does_dst_type_point_to_our_static_type = true;
896                             // If we found what we're looking for, stop looking above.
897                             if (info->path_dst_ptr_to_static_ptr == public_path)
898                                 break;
899                             // We found a private path to (static_ptr, static_type)
900                             //   If there is no diamond then there is only one path
901                             //   to (static_ptr, static_type) and we just found it.
902                             if (!(__flags & __diamond_shaped_mask))
903                                 break;
904                         }
905                         else
906                         {
907                             // If we found a static_type that isn't the one we're looking
908                             //    for, and if there are no repeated types above here,
909                             //    then stop looking.
910                             if (!(__flags & __non_diamond_repeat_mask))
911                                 break;
912                         }
913                     }
914                 }
915                 // If we found no static_type,s then dst_type doesn't derive
916                 //   from static_type, else it does.  Record this result so that
917                 //   next time we hit a dst_type we will know not to search above
918                 //   it if it doesn't derive from static_type.
919                 if (is_dst_type_derived_from_static_type)
920                     info->is_dst_type_derived_from_static_type = yes;
921                 else
922                     info->is_dst_type_derived_from_static_type = no;
923               }
924               if (!does_dst_type_point_to_our_static_type)
925               {
926                   // We found a dst_type that doesn't point to (static_ptr, static_type)
927                   // So record the address of this dst_ptr and increment the
928                   // count of the number of such dst_types found in the tree.
929                   info->dst_ptr_not_leading_to_static_ptr = current_ptr;
930                   info->number_to_dst_ptr += 1;
931                   // If there exists another dst with a private path to
932                   //    (static_ptr, static_type), then the cast from
933                   //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
934                   //      so stop search.
935                   if (info->number_to_static_ptr == 1 &&
936                           info->path_dst_ptr_to_static_ptr == not_public_path)
937                       info->search_done = true;
938               }
939         }
940     }
941     else
942     {
943         // This is not a static_type and not a dst_type.
944         const Iter e = __base_info + __base_count;
945         Iter p = __base_info;
946         p->search_below_dst(info, current_ptr, path_below, use_strcmp);
947         if (++p < e)
948         {
949             if ((__flags & __diamond_shaped_mask) || info->number_to_static_ptr == 1)
950             {
951                 // If there are multiple paths to a base above from here, or if
952                 //    a dst_type pointing to (static_ptr, static_type) has been found,
953                 //    then there is no way to break out of this loop early unless
954                 //    something below detects the search is done.
955                 do
956                 {
957                     if (info->search_done)
958                         break;
959                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
960                 } while (++p < e);
961             }
962             else if (__flags & __non_diamond_repeat_mask)
963             {
964                 // There are not multiple paths to any base class from here and a
965                 //   dst_type pointing to (static_ptr, static_type) has not yet been
966                 //   found.
967                 do
968                 {
969                     if (info->search_done)
970                         break;
971                     // If we just found a dst_type with a public path to (static_ptr, static_type),
972                     //    then the only reason to continue the search is to make sure
973                     //    no other dst_type points to (static_ptr, static_type).
974                     //    If !diamond, then we don't need to search here.
975                     if (info->number_to_static_ptr == 1 &&
976                               info->path_dst_ptr_to_static_ptr == public_path)
977                         break;
978                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
979                 } while (++p < e);
980             }
981             else
982             {
983                 // There are no repeated types above this node.
984                 // There are no nodes with multiple parents above this node.
985                 // no dst_type has been found to (static_ptr, static_type)
986                 do
987                 {
988                     if (info->search_done)
989                         break;
990                     // If we just found a dst_type with a public path to (static_ptr, static_type),
991                     //    then the only reason to continue the search is to make sure sure
992                     //    no other dst_type points to (static_ptr, static_type).
993                     //    If !diamond, then we don't need to search here.
994                     // if we just found a dst_type with a private path to (static_ptr, static_type),
995                     //    then we're only looking for a public path to (static_ptr, static_type)
996                     //    and to check for other dst_types.
997                     //    If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
998                     //    and not a dst_type under here.
999                     if (info->number_to_static_ptr == 1)
1000                         break;
1001                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
1002                 } while (++p < e);
1003             }
1004         }
1005     }
1006 }
1007 
1008 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1009 //   simplified to the case that there is only a single base class.
1010 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1011 __si_class_type_info::search_below_dst(__dynamic_cast_info* info,
1012                                        const void* current_ptr,
1013                                        int path_below,
1014                                        bool use_strcmp) const
1015 {
1016     if (is_equal(this, info->static_type, use_strcmp))
1017         process_static_type_below_dst(info, current_ptr, path_below);
1018     else if (is_equal(this, info->dst_type, use_strcmp))
1019     {
1020         // We've been here before if we've recorded current_ptr in one of these
1021         //   two places:
1022         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1023             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1024         {
1025             // We've seen this node before, and therefore have already searched
1026             // its base classes above.
1027             //  Update path to here that is "most public".
1028             if (path_below == public_path)
1029                 info->path_dynamic_ptr_to_dst_ptr = public_path;
1030         }
1031         else  // We have haven't been here before
1032         {
1033             // Record the access path that got us here
1034             //   If there is more than one dst_type this path doesn't matter.
1035             info->path_dynamic_ptr_to_dst_ptr = path_below;
1036             bool does_dst_type_point_to_our_static_type = false;
1037             // Only search above here if dst_type derives from static_type, or
1038             //    if it is unknown if dst_type derives from static_type.
1039             if (info->is_dst_type_derived_from_static_type != no)
1040             {
1041                 // Set up flags to record results from all base classes
1042                 bool is_dst_type_derived_from_static_type = false;
1043                 // Zero out found flags
1044                 info->found_our_static_ptr = false;
1045                 info->found_any_static_type = false;
1046                 __base_type->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
1047                 if (info->found_any_static_type)
1048                 {
1049                     is_dst_type_derived_from_static_type = true;
1050                     if (info->found_our_static_ptr)
1051                         does_dst_type_point_to_our_static_type = true;
1052                 }
1053                 // If we found no static_type,s then dst_type doesn't derive
1054                 //   from static_type, else it does.  Record this result so that
1055                 //   next time we hit a dst_type we will know not to search above
1056                 //   it if it doesn't derive from static_type.
1057                 if (is_dst_type_derived_from_static_type)
1058                     info->is_dst_type_derived_from_static_type = yes;
1059                 else
1060                     info->is_dst_type_derived_from_static_type = no;
1061             }
1062             if (!does_dst_type_point_to_our_static_type)
1063             {
1064                 // We found a dst_type that doesn't point to (static_ptr, static_type)
1065                 // So record the address of this dst_ptr and increment the
1066                 // count of the number of such dst_types found in the tree.
1067                 info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1068                 info->number_to_dst_ptr += 1;
1069                 // If there exists another dst with a private path to
1070                 //    (static_ptr, static_type), then the cast from
1071                 //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1072                 if (info->number_to_static_ptr == 1 &&
1073                         info->path_dst_ptr_to_static_ptr == not_public_path)
1074                     info->search_done = true;
1075             }
1076         }
1077     }
1078     else
1079     {
1080         // This is not a static_type and not a dst_type
1081         __base_type->search_below_dst(info, current_ptr, path_below, use_strcmp);
1082     }
1083 }
1084 
1085 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
1086 //   simplified to the case that there is no base class.
1087 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1088 __class_type_info::search_below_dst(__dynamic_cast_info* info,
1089                                     const void* current_ptr,
1090                                     int path_below,
1091                                     bool use_strcmp) const
1092 {
1093     if (is_equal(this, info->static_type, use_strcmp))
1094         process_static_type_below_dst(info, current_ptr, path_below);
1095     else if (is_equal(this, info->dst_type, use_strcmp))
1096     {
1097         // We've been here before if we've recorded current_ptr in one of these
1098         //   two places:
1099         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
1100             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
1101         {
1102             // We've seen this node before, and therefore have already searched
1103             // its base classes above.
1104             //  Update path to here that is "most public".
1105             if (path_below == public_path)
1106                 info->path_dynamic_ptr_to_dst_ptr = public_path;
1107         }
1108         else  // We have haven't been here before
1109         {
1110             // Record the access path that got us here
1111             //   If there is more than one dst_type this path doesn't matter.
1112             info->path_dynamic_ptr_to_dst_ptr = path_below;
1113             // We found a dst_type that doesn't point to (static_ptr, static_type)
1114             // So record the address of this dst_ptr and increment the
1115             // count of the number of such dst_types found in the tree.
1116             info->dst_ptr_not_leading_to_static_ptr = current_ptr;
1117             info->number_to_dst_ptr += 1;
1118             // If there exists another dst with a private path to
1119             //    (static_ptr, static_type), then the cast from
1120             //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
1121             if (info->number_to_static_ptr == 1 &&
1122                     info->path_dst_ptr_to_static_ptr == not_public_path)
1123                 info->search_done = true;
1124             // We found that dst_type does not derive from static_type
1125             info->is_dst_type_derived_from_static_type = no;
1126         }
1127     }
1128 }
1129 
1130 // Call this function when searching above a dst_type node.  This function searches
1131 // for a public path to (static_ptr, static_type).
1132 // This function is guaranteed not to find a node of type dst_type.
1133 // Theoretically this is a very simple function which just stops if it finds a
1134 // static_type node:  All the hoopla surrounding the search code is doing
1135 // nothing but looking for excuses to stop the search prematurely (break out of
1136 // the for-loop).  That is, the algorithm below is simply an optimization of this:
1137 // void
1138 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1139 //                                         const void* dst_ptr,
1140 //                                         const void* current_ptr,
1141 //                                         int path_below) const
1142 // {
1143 //     if (this == info->static_type)
1144 //         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1145 //     else
1146 //     {
1147 //         typedef const __base_class_type_info* Iter;
1148 //         // This is not a static_type and not a dst_type
1149 //         for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1150 //         {
1151 //             p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1152 //             // break out early here if you can detect it doesn't matter if you do
1153 //         }
1154 //     }
1155 // }
1156 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1157 __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1158                                         const void* dst_ptr,
1159                                         const void* current_ptr,
1160                                         int path_below,
1161                                         bool use_strcmp) const
1162 {
1163     if (is_equal(this, info->static_type, use_strcmp))
1164         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1165     else
1166     {
1167         typedef const __base_class_type_info* Iter;
1168         // This is not a static_type and not a dst_type
1169         // Save flags so they can be restored when returning to nodes below.
1170         bool found_our_static_ptr = info->found_our_static_ptr;
1171         bool found_any_static_type = info->found_any_static_type;
1172         // We've found a dst_type below with a path to here.  If the path
1173         //    to here is not public, there may be another path to here that
1174         //    is public.  So we have to assume that the path to here is public.
1175         //  We can stop looking above if:
1176         //    1.  We've found a public path to (static_ptr, static_type).
1177         //    2.  We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1178         //        This is detected at the (static_ptr, static_type).
1179         //    3.  We can prove that there is no public path to (static_ptr, static_type)
1180         //        above here.
1181         const Iter e = __base_info + __base_count;
1182         Iter p = __base_info;
1183         // Zero out found flags
1184         info->found_our_static_ptr = false;
1185         info->found_any_static_type = false;
1186         p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1187         found_our_static_ptr |= info->found_our_static_ptr;
1188         found_any_static_type |= info->found_any_static_type;
1189         if (++p < e)
1190         {
1191             do
1192             {
1193                 if (info->search_done)
1194                     break;
1195                 if (info->found_our_static_ptr)
1196                 {
1197                     // If we found what we're looking for, stop looking above.
1198                     if (info->path_dst_ptr_to_static_ptr == public_path)
1199                         break;
1200                     // We found a private path to (static_ptr, static_type)
1201                     //   If there is no diamond then there is only one path
1202                     //   to (static_ptr, static_type) from here and we just found it.
1203                     if (!(__flags & __diamond_shaped_mask))
1204                         break;
1205                 }
1206                 else if (info->found_any_static_type)
1207                 {
1208                     // If we found a static_type that isn't the one we're looking
1209                     //    for, and if there are no repeated types above here,
1210                     //    then stop looking.
1211                     if (!(__flags & __non_diamond_repeat_mask))
1212                         break;
1213                 }
1214                 // Zero out found flags
1215                 info->found_our_static_ptr = false;
1216                 info->found_any_static_type = false;
1217                 p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1218                 found_our_static_ptr |= info->found_our_static_ptr;
1219                 found_any_static_type |= info->found_any_static_type;
1220             } while (++p < e);
1221         }
1222         // Restore flags
1223         info->found_our_static_ptr = found_our_static_ptr;
1224         info->found_any_static_type = found_any_static_type;
1225     }
1226 }
1227 
1228 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1229 //   simplified to the case that there is only a single base class.
1230 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1231 __si_class_type_info::search_above_dst(__dynamic_cast_info* info,
1232                                        const void* dst_ptr,
1233                                        const void* current_ptr,
1234                                        int path_below,
1235                                        bool use_strcmp) const
1236 {
1237     if (is_equal(this, info->static_type, use_strcmp))
1238         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1239     else
1240         __base_type->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1241 }
1242 
1243 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1244 //   simplified to the case that there is no base class.
1245 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1246 __class_type_info::search_above_dst(__dynamic_cast_info* info,
1247                                     const void* dst_ptr,
1248                                     const void* current_ptr,
1249                                     int path_below,
1250                                     bool use_strcmp) const
1251 {
1252     if (is_equal(this, info->static_type, use_strcmp))
1253         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1254 }
1255 
1256 // The search functions for __base_class_type_info are simply convenience
1257 //   functions for adjusting the current_ptr and path_below as the search is
1258 //   passed up to the base class node.
1259 
1260 void
search_above_dst(__dynamic_cast_info * info,const void * dst_ptr,const void * current_ptr,int path_below,bool use_strcmp) const1261 __base_class_type_info::search_above_dst(__dynamic_cast_info* info,
1262                                          const void* dst_ptr,
1263                                          const void* current_ptr,
1264                                          int path_below,
1265                                          bool use_strcmp) const
1266 {
1267     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1268     if (__offset_flags & __virtual_mask)
1269     {
1270         const char* vtable = *static_cast<const char*const*>(current_ptr);
1271         offset_to_base = *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
1272     }
1273     __base_type->search_above_dst(info, dst_ptr,
1274                                   static_cast<const char*>(current_ptr) + offset_to_base,
1275                                   (__offset_flags & __public_mask) ?
1276                                       path_below :
1277                                       not_public_path,
1278                                   use_strcmp);
1279 }
1280 
1281 void
search_below_dst(__dynamic_cast_info * info,const void * current_ptr,int path_below,bool use_strcmp) const1282 __base_class_type_info::search_below_dst(__dynamic_cast_info* info,
1283                                          const void* current_ptr,
1284                                          int path_below,
1285                                          bool use_strcmp) const
1286 {
1287     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1288     if (__offset_flags & __virtual_mask)
1289     {
1290         const char* vtable = *static_cast<const char*const*>(current_ptr);
1291         offset_to_base = *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
1292     }
1293     __base_type->search_below_dst(info,
1294                                   static_cast<const char*>(current_ptr) + offset_to_base,
1295                                   (__offset_flags & __public_mask) ?
1296                                       path_below :
1297                                       not_public_path,
1298                                   use_strcmp);
1299 }
1300 
1301 }  // __cxxabiv1
1302