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