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