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