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