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 #if _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 #if _LIBCXX_DYNAMIC_FALLBACK
61 
62 inline
63 bool
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 
88 __shim_type_info::~__shim_type_info()
89 {
90 }
91 
92 void __shim_type_info::noop1() const {}
93 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
101 __fundamental_type_info::~__fundamental_type_info()
102 {
103 }
104 
105 // __array_type_info
106 
107 __array_type_info::~__array_type_info()
108 {
109 }
110 
111 // __function_type_info
112 
113 __function_type_info::~__function_type_info()
114 {
115 }
116 
117 // __enum_type_info
118 
119 __enum_type_info::~__enum_type_info()
120 {
121 }
122 
123 // __class_type_info
124 
125 __class_type_info::~__class_type_info()
126 {
127 }
128 
129 // __si_class_type_info
130 
131 __si_class_type_info::~__si_class_type_info()
132 {
133 }
134 
135 // __vmi_class_type_info
136 
137 __vmi_class_type_info::~__vmi_class_type_info()
138 {
139 }
140 
141 // __pbase_type_info
142 
143 __pbase_type_info::~__pbase_type_info()
144 {
145 }
146 
147 // __pointer_type_info
148 
149 __pointer_type_info::~__pointer_type_info()
150 {
151 }
152 
153 // __pointer_to_member_type_info
154 
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
188 __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
195 __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
205 __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
216 __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 #pragma clang diagnostic push
223 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
224 
225 // Handles bullets 1 and 2
226 bool
227 __class_type_info::can_catch(const __shim_type_info* thrown_type,
228                              void*& adjustedPtr) const
229 {
230     // bullet 1
231     if (is_equal(this, thrown_type, false))
232         return true;
233     const __class_type_info* thrown_class_type =
234         dynamic_cast<const __class_type_info*>(thrown_type);
235     if (thrown_class_type == 0)
236         return false;
237     // bullet 2
238     __dynamic_cast_info info = {thrown_class_type, 0, this, -1, 0};
239     info.number_of_dst_type = 1;
240     thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
241     if (info.path_dst_ptr_to_static_ptr == public_path)
242     {
243         adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
244         return true;
245     }
246     return false;
247 }
248 
249 #pragma clang diagnostic pop
250 
251 void
252 __class_type_info::process_found_base_class(__dynamic_cast_info* info,
253                                                void* adjustedPtr,
254                                                int path_below) const
255 {
256     if (info->dst_ptr_leading_to_static_ptr == 0)
257     {
258         // First time here
259         info->dst_ptr_leading_to_static_ptr = adjustedPtr;
260         info->path_dst_ptr_to_static_ptr = path_below;
261         info->number_to_static_ptr = 1;
262     }
263     else if (info->dst_ptr_leading_to_static_ptr == adjustedPtr)
264     {
265         // We've been here before.  Update path to "most public"
266         if (info->path_dst_ptr_to_static_ptr == not_public_path)
267             info->path_dst_ptr_to_static_ptr = path_below;
268     }
269     else
270     {
271         // We've detected an ambiguous cast from (thrown_class_type, adjustedPtr)
272         //   to a static_type
273         info->number_to_static_ptr += 1;
274         info->path_dst_ptr_to_static_ptr = not_public_path;
275         info->search_done = true;
276     }
277 }
278 
279 void
280 __class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
281                                                void* adjustedPtr,
282                                                int path_below) const
283 {
284     if (is_equal(this, info->static_type, false))
285         process_found_base_class(info, adjustedPtr, path_below);
286 }
287 
288 void
289 __si_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
290                                                   void* adjustedPtr,
291                                                   int path_below) const
292 {
293     if (is_equal(this, info->static_type, false))
294         process_found_base_class(info, adjustedPtr, path_below);
295     else
296         __base_type->has_unambiguous_public_base(info, adjustedPtr, path_below);
297 }
298 
299 void
300 __base_class_type_info::has_unambiguous_public_base(__dynamic_cast_info* info,
301                                                     void* adjustedPtr,
302                                                     int path_below) const
303 {
304     ptrdiff_t 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     __base_type->has_unambiguous_public_base(info,
311                                              static_cast<char*>(adjustedPtr) + offset_to_base,
312                                              (__offset_flags & __public_mask) ?
313                                                  path_below :
314                                                  not_public_path);
315 }
316 
317 void
318 __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 bullets 1 and 4 for both pointers and member pointers
343 bool
344 __pbase_type_info::can_catch(const __shim_type_info* thrown_type,
345                              void*&) const
346 {
347     if (is_equal(this, thrown_type, false))
348         return true;
349     return is_equal(thrown_type, &typeid(std::nullptr_t), false);
350 }
351 
352 #pragma clang diagnostic push
353 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
354 
355 // Handles bullets 1, 3 and 4
356 bool
357 __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
358                                void*& adjustedPtr) const
359 {
360     // Do the dereference adjustment
361     adjustedPtr = *static_cast<void**>(adjustedPtr);
362     // bullets 1 and 4
363     if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
364         return true;
365     // bullet 3
366     const __pointer_type_info* thrown_pointer_type =
367         dynamic_cast<const __pointer_type_info*>(thrown_type);
368     if (thrown_pointer_type == 0)
369         return false;
370     // bullet 3B
371     if (thrown_pointer_type->__flags & ~__flags)
372         return false;
373     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
374         return true;
375     // bullet 3A
376     if (is_equal(__pointee, &typeid(void), false))
377         return true;
378     const __class_type_info* catch_class_type =
379         dynamic_cast<const __class_type_info*>(__pointee);
380     if (catch_class_type == 0)
381         return false;
382     const __class_type_info* thrown_class_type =
383         dynamic_cast<const __class_type_info*>(thrown_pointer_type->__pointee);
384     if (thrown_class_type == 0)
385         return false;
386     __dynamic_cast_info info = {thrown_class_type, 0, catch_class_type, -1, 0};
387     info.number_of_dst_type = 1;
388     thrown_class_type->has_unambiguous_public_base(&info, adjustedPtr, public_path);
389     if (info.path_dst_ptr_to_static_ptr == public_path)
390     {
391         adjustedPtr = const_cast<void*>(info.dst_ptr_leading_to_static_ptr);
392         return true;
393     }
394     return false;
395 }
396 
397 #pragma clang diagnostic pop
398 
399 #pragma GCC visibility pop
400 #pragma GCC visibility push(default)
401 
402 #pragma clang diagnostic push
403 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
404 
405 // __dynamic_cast
406 
407 // static_ptr: pointer to an object of type static_type; nonnull, and since the
408 //   object is polymorphic, *(void**)static_ptr is a virtual table pointer.
409 //   static_ptr is &v in the expression dynamic_cast<T>(v).
410 // static_type: static type of the object pointed to by static_ptr.
411 // dst_type: destination type of the cast (the "T" in "dynamic_cast<T>(v)").
412 // src2dst_offset: a static hint about the location of the
413 //                 source subobject with respect to the complete object;
414 //                 special negative values are:
415 //                     -1: no hint
416 //                     -2: static_type is not a public base of dst_type
417 //                     -3: static_type is a multiple public base type but never a
418 //                         virtual base type
419 //                 otherwise, the static_type type is a unique public nonvirtual
420 //                 base type of dst_type at offset src2dst_offset from the
421 //                 origin of dst_type.
422 //
423 // (dynamic_ptr, dynamic_type) are the run time type of the complete object
424 // referred to by static_ptr and a pointer to it.  These can be found from
425 // static_ptr for polymorphic types.
426 // static_type is guaranteed to be a polymorphic type.
427 //
428 // (dynamic_ptr, dynamic_type) is the root of a DAG that grows upward.  Each
429 // node of the tree represents a base class/object of its parent (or parents) below.
430 // Each node is uniquely represented by a pointer to the object, and a pointer
431 // to a type_info - its type.  Different nodes may have the same pointer and
432 // different nodes may have the same type.  But only one node has a specific
433 // (pointer-value, type) pair.  In C++ two objects of the same type can not
434 // share the same address.
435 //
436 // There are two flavors of nodes which have the type dst_type:
437 //    1.  Those that are derived from (below) (static_ptr, static_type).
438 //    2.  Those that are not derived from (below) (static_ptr, static_type).
439 //
440 // Invariants of the DAG:
441 //
442 // There is at least one path from the root (dynamic_ptr, dynamic_type) to
443 // the node (static_ptr, static_type).  This path may or may not be public.
444 // There may be more than one such path (some public some not).  Such a path may
445 // or may not go through a node having type dst_type.
446 //
447 // No node of type T appears above a node of the same type.  That means that
448 // there is only one node with dynamic_type.  And if dynamic_type == dst_type,
449 // then there is only one dst_type in the DAG.
450 //
451 // No node of type dst_type appears above a node of type static_type.  Such
452 // DAG's are possible in C++, but the compiler computes those dynamic_casts at
453 // compile time, and only calls __dynamic_cast when dst_type lies below
454 // static_type in the DAG.
455 //
456 // dst_type != static_type:  The compiler computes the dynamic_cast in this case too.
457 // dynamic_type != static_type:  The compiler computes the dynamic_cast in this case too.
458 //
459 // Returns:
460 //
461 // If there is exactly one dst_type of flavor 1, and
462 //    If there is a public path from that dst_type to (static_ptr, static_type), or
463 //    If there are 0 dst_types of flavor 2, and there is a public path from
464 //        (dynamic_ptr, dynamic_type) to (static_ptr, static_type) and a public
465 //        path from (dynamic_ptr, dynamic_type) to the one dst_type, then return
466 //        a pointer to that dst_type.
467 // Else if there are 0 dst_types of flavor 1 and exactly 1 dst_type of flavor 2, and
468 //    if there is a public path from (dynamic_ptr, dynamic_type) to
469 //    (static_ptr, static_type) and a public path from (dynamic_ptr, dynamic_type)
470 //    to the one dst_type, then return a pointer to that one dst_type.
471 // Else return nullptr.
472 //
473 // If dynamic_type == dst_type, then the above algorithm collapses to the
474 // following cheaper algorithm:
475 //
476 // If there is a public path from (dynamic_ptr, dynamic_type) to
477 //    (static_ptr, static_type), then return dynamic_ptr.
478 // Else return nullptr.
479 extern "C"
480 void*
481 __dynamic_cast(const void* static_ptr,
482                const __class_type_info* static_type,
483                const __class_type_info* dst_type,
484                std::ptrdiff_t src2dst_offset)
485 {
486     // Possible future optimization:  Take advantage of src2dst_offset
487     // Currently clang always sets src2dst_offset to -1 (no hint).
488 
489     // Get (dynamic_ptr, dynamic_type) from static_ptr
490     void** vtable = *(void***)static_ptr;
491     ptrdiff_t offset_to_derived = reinterpret_cast<ptrdiff_t>(vtable[-2]);
492     const void* dynamic_ptr = static_cast<const char*>(static_ptr) + offset_to_derived;
493     const __class_type_info* dynamic_type = static_cast<const __class_type_info*>(vtable[-1]);
494 
495     // Initialize answer to nullptr.  This will be changed from the search
496     //    results if a non-null answer is found.  Regardless, this is what will
497     //    be returned.
498     const void* dst_ptr = 0;
499     // Initialize info struct for this search.
500     __dynamic_cast_info info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
501 
502     // Find out if we can use a giant short cut in the search
503     if (is_equal(dynamic_type, dst_type, false))
504     {
505         // Using giant short cut.  Add that information to info.
506         info.number_of_dst_type = 1;
507         // Do the  search
508         dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, false);
509 #if _LIBCXX_DYNAMIC_FALLBACK
510         // The following if should always be false because we should definitely
511         //   find (static_ptr, static_type), either on a public or private path
512         if (info.path_dst_ptr_to_static_ptr == unknown)
513         {
514             // We get here only if there is some kind of visibility problem
515             //   in client code.
516             syslog(LOG_ERR, "dynamic_cast error 1: Both of the following type_info's "
517                     "should have public visibility.  At least one of them is hidden. %s"
518                     ", %s.\n", static_type->name(), dynamic_type->name());
519             // Redo the search comparing type_info's using strcmp
520             info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
521             info.number_of_dst_type = 1;
522             dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, true);
523         }
524 #endif  // _LIBCXX_DYNAMIC_FALLBACK
525         // Query the search.
526         if (info.path_dst_ptr_to_static_ptr == public_path)
527             dst_ptr = dynamic_ptr;
528     }
529     else
530     {
531         // Not using giant short cut.  Do the search
532         dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, false);
533  #if _LIBCXX_DYNAMIC_FALLBACK
534         // The following if should always be false because we should definitely
535         //   find (static_ptr, static_type), either on a public or private path
536         if (info.path_dst_ptr_to_static_ptr == unknown &&
537             info.path_dynamic_ptr_to_static_ptr == unknown)
538         {
539             syslog(LOG_ERR, "dynamic_cast error 2: One or more of the following type_info's "
540                             " has hidden visibility.  They should all have public visibility.  "
541                             " %s, %s, %s.\n", static_type->name(), dynamic_type->name(),
542                     dst_type->name());
543             // Redo the search comparing type_info's using strcmp
544             info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
545             dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, true);
546         }
547 #endif  // _LIBCXX_DYNAMIC_FALLBACK
548         // Query the search.
549         switch (info.number_to_static_ptr)
550         {
551         case 0:
552             if (info.number_to_dst_ptr == 1 &&
553                     info.path_dynamic_ptr_to_static_ptr == public_path &&
554                     info.path_dynamic_ptr_to_dst_ptr == public_path)
555                 dst_ptr = info.dst_ptr_not_leading_to_static_ptr;
556             break;
557         case 1:
558             if (info.path_dst_ptr_to_static_ptr == public_path ||
559                    (
560                        info.number_to_dst_ptr == 0 &&
561                        info.path_dynamic_ptr_to_static_ptr == public_path &&
562                        info.path_dynamic_ptr_to_dst_ptr == public_path
563                    )
564                )
565                 dst_ptr = info.dst_ptr_leading_to_static_ptr;
566             break;
567         }
568     }
569     return const_cast<void*>(dst_ptr);
570 }
571 
572 #pragma clang diagnostic pop
573 
574 #pragma GCC visibility pop
575 #pragma GCC visibility push(hidden)
576 
577 // Call this function when you hit a static_type which is a base (above) a dst_type.
578 // Let caller know you hit a static_type.  But only start recording details if
579 // this is (static_ptr, static_type) -- the node we are casting from.
580 // If this is (static_ptr, static_type)
581 //   Record the path (public or not) from the dst_type to here.  There may be
582 //   multiple paths from the same dst_type to here, record the "most public" one.
583 //   Record the dst_ptr as pointing to (static_ptr, static_type).
584 //   If more than one (dst_ptr, dst_type) points to (static_ptr, static_type),
585 //   then mark this dyanmic_cast as ambiguous and stop the search.
586 void
587 __class_type_info::process_static_type_above_dst(__dynamic_cast_info* info,
588                                                  const void* dst_ptr,
589                                                  const void* current_ptr,
590                                                  int path_below) const
591 {
592     // Record that we found a static_type
593     info->found_any_static_type = true;
594     if (current_ptr == info->static_ptr)
595     {
596         // Record that we found (static_ptr, static_type)
597         info->found_our_static_ptr = true;
598         if (info->dst_ptr_leading_to_static_ptr == 0)
599         {
600             // First time here
601             info->dst_ptr_leading_to_static_ptr = dst_ptr;
602             info->path_dst_ptr_to_static_ptr = path_below;
603             info->number_to_static_ptr = 1;
604             // If there is only one dst_type in the entire tree and the path from
605             //    there to here is public then we are done!
606             if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
607                 info->search_done = true;
608         }
609         else if (info->dst_ptr_leading_to_static_ptr == dst_ptr)
610         {
611             // We've been here before.  Update path to "most public"
612             if (info->path_dst_ptr_to_static_ptr == not_public_path)
613                 info->path_dst_ptr_to_static_ptr = path_below;
614             // If there is only one dst_type in the entire tree and the path from
615             //    there to here is public then we are done!
616             if (info->number_of_dst_type == 1 && info->path_dst_ptr_to_static_ptr == public_path)
617                 info->search_done = true;
618         }
619         else
620         {
621             // We've detected an ambiguous cast from (static_ptr, static_type)
622             //   to a dst_type
623             info->number_to_static_ptr += 1;
624             info->search_done = true;
625         }
626     }
627 }
628 
629 // Call this function when you hit a static_type which is not a base (above) a dst_type.
630 // If this is (static_ptr, static_type)
631 //   Record the path (public or not) from (dynamic_ptr, dynamic_type) to here.  There may be
632 //   multiple paths from (dynamic_ptr, dynamic_type) to here, record the "most public" one.
633 void
634 __class_type_info::process_static_type_below_dst(__dynamic_cast_info* info,
635                                                  const void* current_ptr,
636                                                  int path_below) const
637 {
638     if (current_ptr == info->static_ptr)
639     {
640         // Record the most public path from (dynamic_ptr, dynamic_type) to
641         //                                  (static_ptr, static_type)
642         if (info->path_dynamic_ptr_to_static_ptr != public_path)
643             info->path_dynamic_ptr_to_static_ptr = path_below;
644     }
645 }
646 
647 // Call this function when searching below a dst_type node.  This function searches
648 // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes.
649 // If it finds a static_type node, there is no need to further search base classes
650 // above.
651 // If it finds a dst_type node it should search base classes using search_above_dst
652 // to find out if this dst_type points to (static_ptr, static_type) or not.
653 // Either way, the dst_type is recorded as one of two "flavors":  one that does
654 // or does not point to (static_ptr, static_type).
655 // If this is neither a static_type nor a dst_type node, continue searching
656 // base classes above.
657 // All the hoopla surrounding the search code is doing nothing but looking for
658 // excuses to stop the search prematurely (break out of the for-loop).  That is,
659 // the algorithm below is simply an optimization of this:
660 // void
661 // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
662 //                                         const void* current_ptr,
663 //                                         int path_below) const
664 // {
665 //     typedef const __base_class_type_info* Iter;
666 //     if (this == info->static_type)
667 //         process_static_type_below_dst(info, current_ptr, path_below);
668 //     else if (this == info->dst_type)
669 //     {
670 //         // Record the most public access path that got us here
671 //         if (info->path_dynamic_ptr_to_dst_ptr != public_path)
672 //             info->path_dynamic_ptr_to_dst_ptr = path_below;
673 //         bool does_dst_type_point_to_our_static_type = false;
674 //         for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p)
675 //         {
676 //             p->search_above_dst(info, current_ptr, current_ptr, public_path);
677 //             if (info->found_our_static_ptr)
678 //                 does_dst_type_point_to_our_static_type = true;
679 //             // break out early here if you can detect it doesn't matter if you do
680 //         }
681 //         if (!does_dst_type_point_to_our_static_type)
682 //         {
683 //             // We found a dst_type that doesn't point to (static_ptr, static_type)
684 //             // So record the address of this dst_ptr and increment the
685 //             // count of the number of such dst_types found in the tree.
686 //             info->dst_ptr_not_leading_to_static_ptr = current_ptr;
687 //             info->number_to_dst_ptr += 1;
688 //         }
689 //     }
690 //     else
691 //     {
692 //         // This is not a static_type and not a dst_type.
693 //         for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
694 //         {
695 //             p->search_below_dst(info, current_ptr, public_path);
696 //             // break out early here if you can detect it doesn't matter if you do
697 //         }
698 //     }
699 // }
700 void
701 __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info,
702                                         const void* current_ptr,
703                                         int path_below,
704                                         bool use_strcmp) const
705 {
706     typedef const __base_class_type_info* Iter;
707     if (is_equal(this, info->static_type, use_strcmp))
708         process_static_type_below_dst(info, current_ptr, path_below);
709     else if (is_equal(this, info->dst_type, use_strcmp))
710     {
711         // We've been here before if we've recorded current_ptr in one of these
712         //   two places:
713         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
714             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
715         {
716             // We've seen this node before, and therefore have already searched
717             // its base classes above.
718             //  Update path to here that is "most public".
719             if (path_below == public_path)
720                 info->path_dynamic_ptr_to_dst_ptr = public_path;
721         }
722         else  // We have haven't been here before
723         {
724             // Record the access path that got us here
725             //   If there is more than one dst_type this path doesn't matter.
726             info->path_dynamic_ptr_to_dst_ptr = path_below;
727             // Only search above here if dst_type derives from static_type, or
728             //    if it is unknown if dst_type derives from static_type.
729             if (info->is_dst_type_derived_from_static_type != no)
730             {
731                 // Set up flags to record results from all base classes
732                 bool is_dst_type_derived_from_static_type = false;
733                 bool does_dst_type_point_to_our_static_type = false;
734                 // We've found a dst_type with a potentially public path to here.
735                 // We have to assume the path is public because it may become
736                 //   public later (if we get back to here with a public path).
737                 // We can stop looking above if:
738                 //    1.  We've found a public path to (static_ptr, static_type).
739                 //    2.  We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
740                 //        This is detected at the (static_ptr, static_type).
741                 //    3.  We can prove that there is no public path to (static_ptr, static_type)
742                 //        above here.
743                 const Iter e = __base_info + __base_count;
744                 for (Iter p = __base_info; p < e; ++p)
745                 {
746                     // Zero out found flags
747                     info->found_our_static_ptr = false;
748                     info->found_any_static_type = false;
749                     p->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
750                     if (info->search_done)
751                         break;
752                     if (info->found_any_static_type)
753                     {
754                         is_dst_type_derived_from_static_type = true;
755                         if (info->found_our_static_ptr)
756                         {
757                             does_dst_type_point_to_our_static_type = true;
758                             // If we found what we're looking for, stop looking above.
759                             if (info->path_dst_ptr_to_static_ptr == public_path)
760                                 break;
761                             // We found a private path to (static_ptr, static_type)
762                             //   If there is no diamond then there is only one path
763                             //   to (static_ptr, static_type) and we just found it.
764                             if (!(__flags & __diamond_shaped_mask))
765                                 break;
766                         }
767                         else
768                         {
769                             // If we found a static_type that isn't the one we're looking
770                             //    for, and if there are no repeated types above here,
771                             //    then stop looking.
772                             if (!(__flags & __non_diamond_repeat_mask))
773                                 break;
774                         }
775                     }
776                 }
777                 if (!does_dst_type_point_to_our_static_type)
778                 {
779                     // We found a dst_type that doesn't point to (static_ptr, static_type)
780                     // So record the address of this dst_ptr and increment the
781                     // count of the number of such dst_types found in the tree.
782                     info->dst_ptr_not_leading_to_static_ptr = current_ptr;
783                     info->number_to_dst_ptr += 1;
784                     // If there exists another dst with a private path to
785                     //    (static_ptr, static_type), then the cast from
786                     //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous,
787                     //      so stop search.
788                     if (info->number_to_static_ptr == 1 &&
789                             info->path_dst_ptr_to_static_ptr == not_public_path)
790                         info->search_done = true;
791                 }
792                 // If we found no static_type,s then dst_type doesn't derive
793                 //   from static_type, else it does.  Record this result so that
794                 //   next time we hit a dst_type we will know not to search above
795                 //   it if it doesn't derive from static_type.
796                 if (is_dst_type_derived_from_static_type)
797                     info->is_dst_type_derived_from_static_type = yes;
798                 else
799                     info->is_dst_type_derived_from_static_type = no;
800             }
801         }
802     }
803     else
804     {
805         // This is not a static_type and not a dst_type.
806         const Iter e = __base_info + __base_count;
807         Iter p = __base_info;
808         p->search_below_dst(info, current_ptr, path_below, use_strcmp);
809         if (++p < e)
810         {
811             if ((__flags & __diamond_shaped_mask) || info->number_to_static_ptr == 1)
812             {
813                 // If there are multiple paths to a base above from here, or if
814                 //    a dst_type pointing to (static_ptr, static_type) has been found,
815                 //    then there is no way to break out of this loop early unless
816                 //    something below detects the search is done.
817                 do
818                 {
819                     if (info->search_done)
820                         break;
821                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
822                 } while (++p < e);
823             }
824             else if (__flags & __non_diamond_repeat_mask)
825             {
826                 // There are not multiple paths to any base class from here and a
827                 //   dst_type pointing to (static_ptr, static_type) has not yet been
828                 //   found.
829                 do
830                 {
831                     if (info->search_done)
832                         break;
833                     // If we just found a dst_type with a public path to (static_ptr, static_type),
834                     //    then the only reason to continue the search is to make sure
835                     //    no other dst_type points to (static_ptr, static_type).
836                     //    If !diamond, then we don't need to search here.
837                     if (info->number_to_static_ptr == 1 &&
838                               info->path_dst_ptr_to_static_ptr == public_path)
839                         break;
840                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
841                 } while (++p < e);
842             }
843             else
844             {
845                 // There are no repeated types above this node.
846                 // There are no nodes with multiple parents above this node.
847                 // no dst_type has been found to (static_ptr, static_type)
848                 do
849                 {
850                     if (info->search_done)
851                         break;
852                     // If we just found a dst_type with a public path to (static_ptr, static_type),
853                     //    then the only reason to continue the search is to make sure sure
854                     //    no other dst_type points to (static_ptr, static_type).
855                     //    If !diamond, then we don't need to search here.
856                     // if we just found a dst_type with a private path to (static_ptr, static_type),
857                     //    then we're only looking for a public path to (static_ptr, static_type)
858                     //    and to check for other dst_types.
859                     //    If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type)
860                     //    and not a dst_type under here.
861                     if (info->number_to_static_ptr == 1)
862                         break;
863                     p->search_below_dst(info, current_ptr, path_below, use_strcmp);
864                 } while (++p < e);
865             }
866         }
867     }
868 }
869 
870 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
871 //   simplified to the case that there is only a single base class.
872 void
873 __si_class_type_info::search_below_dst(__dynamic_cast_info* info,
874                                        const void* current_ptr,
875                                        int path_below,
876                                        bool use_strcmp) const
877 {
878     if (is_equal(this, info->static_type, use_strcmp))
879         process_static_type_below_dst(info, current_ptr, path_below);
880     else if (is_equal(this, info->dst_type, use_strcmp))
881     {
882         // We've been here before if we've recorded current_ptr in one of these
883         //   two places:
884         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
885             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
886         {
887             // We've seen this node before, and therefore have already searched
888             // its base classes above.
889             //  Update path to here that is "most public".
890             if (path_below == public_path)
891                 info->path_dynamic_ptr_to_dst_ptr = public_path;
892         }
893         else  // We have haven't been here before
894         {
895             // Record the access path that got us here
896             //   If there is more than one dst_type this path doesn't matter.
897             info->path_dynamic_ptr_to_dst_ptr = path_below;
898             // Only search above here if dst_type derives from static_type, or
899             //    if it is unknown if dst_type derives from static_type.
900             if (info->is_dst_type_derived_from_static_type != no)
901             {
902                 // Set up flags to record results from all base classes
903                 bool is_dst_type_derived_from_static_type = false;
904                 bool does_dst_type_point_to_our_static_type = false;
905                 // Zero out found flags
906                 info->found_our_static_ptr = false;
907                 info->found_any_static_type = false;
908                 __base_type->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp);
909                 if (info->found_any_static_type)
910                 {
911                     is_dst_type_derived_from_static_type = true;
912                     if (info->found_our_static_ptr)
913                         does_dst_type_point_to_our_static_type = true;
914                 }
915                 if (!does_dst_type_point_to_our_static_type)
916                 {
917                     // We found a dst_type that doesn't point to (static_ptr, static_type)
918                     // So record the address of this dst_ptr and increment the
919                     // count of the number of such dst_types found in the tree.
920                     info->dst_ptr_not_leading_to_static_ptr = current_ptr;
921                     info->number_to_dst_ptr += 1;
922                     // If there exists another dst with a private path to
923                     //    (static_ptr, static_type), then the cast from
924                     //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
925                     if (info->number_to_static_ptr == 1 &&
926                             info->path_dst_ptr_to_static_ptr == not_public_path)
927                         info->search_done = true;
928                 }
929                 // If we found no static_type,s then dst_type doesn't derive
930                 //   from static_type, else it does.  Record this result so that
931                 //   next time we hit a dst_type we will know not to search above
932                 //   it if it doesn't derive from static_type.
933                 if (is_dst_type_derived_from_static_type)
934                     info->is_dst_type_derived_from_static_type = yes;
935                 else
936                     info->is_dst_type_derived_from_static_type = no;
937             }
938         }
939     }
940     else
941     {
942         // This is not a static_type and not a dst_type
943         __base_type->search_below_dst(info, current_ptr, path_below, use_strcmp);
944     }
945 }
946 
947 // This is the same algorithm as __vmi_class_type_info::search_below_dst but
948 //   simplified to the case that there is no base class.
949 void
950 __class_type_info::search_below_dst(__dynamic_cast_info* info,
951                                     const void* current_ptr,
952                                     int path_below,
953                                     bool use_strcmp) const
954 {
955     typedef const __base_class_type_info* Iter;
956     if (is_equal(this, info->static_type, use_strcmp))
957         process_static_type_below_dst(info, current_ptr, path_below);
958     else if (is_equal(this, info->dst_type, use_strcmp))
959     {
960         // We've been here before if we've recorded current_ptr in one of these
961         //   two places:
962         if (current_ptr == info->dst_ptr_leading_to_static_ptr ||
963             current_ptr == info->dst_ptr_not_leading_to_static_ptr)
964         {
965             // We've seen this node before, and therefore have already searched
966             // its base classes above.
967             //  Update path to here that is "most public".
968             if (path_below == public_path)
969                 info->path_dynamic_ptr_to_dst_ptr = public_path;
970         }
971         else  // We have haven't been here before
972         {
973             // Record the access path that got us here
974             //   If there is more than one dst_type this path doesn't matter.
975             info->path_dynamic_ptr_to_dst_ptr = path_below;
976             // We found a dst_type that doesn't point to (static_ptr, static_type)
977             // So record the address of this dst_ptr and increment the
978             // count of the number of such dst_types found in the tree.
979             info->dst_ptr_not_leading_to_static_ptr = current_ptr;
980             info->number_to_dst_ptr += 1;
981             // If there exists another dst with a private path to
982             //    (static_ptr, static_type), then the cast from
983             //     (dynamic_ptr, dynamic_type) to dst_type is now ambiguous.
984             if (info->number_to_static_ptr == 1 &&
985                     info->path_dst_ptr_to_static_ptr == not_public_path)
986                 info->search_done = true;
987             // We found that dst_type does not derive from static_type
988             info->is_dst_type_derived_from_static_type = no;
989         }
990     }
991 }
992 
993 // Call this function when searching above a dst_type node.  This function searches
994 // for a public path to (static_ptr, static_type).
995 // This function is guaranteed not to find a node of type dst_type.
996 // Theoretically this is a very simple function which just stops if it finds a
997 // static_type node:  All the hoopla surrounding the search code is doing
998 // nothing but looking for excuses to stop the search prematurely (break out of
999 // the for-loop).  That is, the algorithm below is simply an optimization of this:
1000 // void
1001 // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1002 //                                         const void* dst_ptr,
1003 //                                         const void* current_ptr,
1004 //                                         int path_below) const
1005 // {
1006 //     if (this == info->static_type)
1007 //         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1008 //     else
1009 //     {
1010 //         typedef const __base_class_type_info* Iter;
1011 //         // This is not a static_type and not a dst_type
1012 //         for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
1013 //         {
1014 //             p->search_above_dst(info, dst_ptr, current_ptr, public_path);
1015 //             // break out early here if you can detect it doesn't matter if you do
1016 //         }
1017 //     }
1018 // }
1019 void
1020 __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info,
1021                                         const void* dst_ptr,
1022                                         const void* current_ptr,
1023                                         int path_below,
1024                                         bool use_strcmp) const
1025 {
1026     if (is_equal(this, info->static_type, use_strcmp))
1027         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1028     else
1029     {
1030         typedef const __base_class_type_info* Iter;
1031         // This is not a static_type and not a dst_type
1032         // Save flags so they can be restored when returning to nodes below.
1033         bool found_our_static_ptr = info->found_our_static_ptr;
1034         bool found_any_static_type = info->found_any_static_type;
1035         // We've found a dst_type below with a path to here.  If the path
1036         //    to here is not public, there may be another path to here that
1037         //    is public.  So we have to assume that the path to here is public.
1038         //  We can stop looking above if:
1039         //    1.  We've found a public path to (static_ptr, static_type).
1040         //    2.  We've found an ambiguous cast from (static_ptr, static_type) to a dst_type.
1041         //        This is detected at the (static_ptr, static_type).
1042         //    3.  We can prove that there is no public path to (static_ptr, static_type)
1043         //        above here.
1044         const Iter e = __base_info + __base_count;
1045         Iter p = __base_info;
1046         // Zero out found flags
1047         info->found_our_static_ptr = false;
1048         info->found_any_static_type = false;
1049         p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1050         if (++p < e)
1051         {
1052             do
1053             {
1054                 if (info->search_done)
1055                     break;
1056                 if (info->found_our_static_ptr)
1057                 {
1058                     // If we found what we're looking for, stop looking above.
1059                     if (info->path_dst_ptr_to_static_ptr == public_path)
1060                         break;
1061                     // We found a private path to (static_ptr, static_type)
1062                     //   If there is no diamond then there is only one path
1063                     //   to (static_ptr, static_type) from here and we just found it.
1064                     if (!(__flags & __diamond_shaped_mask))
1065                         break;
1066                 }
1067                 else if (info->found_any_static_type)
1068                 {
1069                     // If we found a static_type that isn't the one we're looking
1070                     //    for, and if there are no repeated types above here,
1071                     //    then stop looking.
1072                     if (!(__flags & __non_diamond_repeat_mask))
1073                         break;
1074                 }
1075                 // Zero out found flags
1076                 info->found_our_static_ptr = false;
1077                 info->found_any_static_type = false;
1078                 p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1079             } while (++p < e);
1080         }
1081         // Restore flags
1082         info->found_our_static_ptr = found_our_static_ptr;
1083         info->found_any_static_type = found_any_static_type;
1084     }
1085 }
1086 
1087 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1088 //   simplified to the case that there is only a single base class.
1089 void
1090 __si_class_type_info::search_above_dst(__dynamic_cast_info* info,
1091                                        const void* dst_ptr,
1092                                        const void* current_ptr,
1093                                        int path_below,
1094                                        bool use_strcmp) const
1095 {
1096     if (is_equal(this, info->static_type, use_strcmp))
1097         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1098     else
1099         __base_type->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp);
1100 }
1101 
1102 // This is the same algorithm as __vmi_class_type_info::search_above_dst but
1103 //   simplified to the case that there is no base class.
1104 void
1105 __class_type_info::search_above_dst(__dynamic_cast_info* info,
1106                                     const void* dst_ptr,
1107                                     const void* current_ptr,
1108                                     int path_below,
1109                                     bool use_strcmp) const
1110 {
1111     if (is_equal(this, info->static_type, use_strcmp))
1112         process_static_type_above_dst(info, dst_ptr, current_ptr, path_below);
1113 }
1114 
1115 // The search functions for __base_class_type_info are simply convenience
1116 //   functions for adjusting the current_ptr and path_below as the search is
1117 //   passed up to the base class node.
1118 
1119 void
1120 __base_class_type_info::search_above_dst(__dynamic_cast_info* info,
1121                                          const void* dst_ptr,
1122                                          const void* current_ptr,
1123                                          int path_below,
1124                                          bool use_strcmp) const
1125 {
1126     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1127     if (__offset_flags & __virtual_mask)
1128     {
1129         const char* vtable = *static_cast<const char*const*>(current_ptr);
1130         offset_to_base = *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
1131     }
1132     __base_type->search_above_dst(info, dst_ptr,
1133                                   static_cast<const char*>(current_ptr) + offset_to_base,
1134                                   (__offset_flags & __public_mask) ?
1135                                       path_below :
1136                                       not_public_path,
1137                                   use_strcmp);
1138 }
1139 
1140 void
1141 __base_class_type_info::search_below_dst(__dynamic_cast_info* info,
1142                                          const void* current_ptr,
1143                                          int path_below,
1144                                          bool use_strcmp) const
1145 {
1146     ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
1147     if (__offset_flags & __virtual_mask)
1148     {
1149         const char* vtable = *static_cast<const char*const*>(current_ptr);
1150         offset_to_base = *reinterpret_cast<const ptrdiff_t*>(vtable + offset_to_base);
1151     }
1152     __base_type->search_below_dst(info,
1153                                   static_cast<const char*>(current_ptr) + offset_to_base,
1154                                   (__offset_flags & __public_mask) ?
1155                                       path_below :
1156                                       not_public_path,
1157                                   use_strcmp);
1158 }
1159 
1160 #pragma GCC visibility pop
1161 
1162 }  // __cxxabiv1
1163