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