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