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