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