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