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