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