1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "subtype_check.h"
#include "gtest/gtest.h"
#include "android-base/logging.h"
namespace art {
constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
constexpr size_t BitString::kCapacity;
struct MockClass {
explicit MockClass(MockClass* parent, size_t x = 0, size_t y = 0) {
parent_ = parent;
memset(&subtype_check_info_and_status_, 0u, sizeof(subtype_check_info_and_status_));
// Start the numbering at '1' to match the bitstring numbering.
// A bitstring numbering never starts at '0' which just means 'no value'.
x_ = 1;
if (parent_ != nullptr) {
if (parent_->GetMaxChild() != nullptr) {
x_ = parent_->GetMaxChild()->x_ + 1u;
}
parent_->children_.push_back(this);
if (parent_->path_to_root_ != "") {
path_to_root_ = parent->path_to_root_ + ",";
}
path_to_root_ += std::to_string(x_);
} else {
path_to_root_ = ""; // root has no path.
}
y_ = y;
UNUSED(x);
}
MockClass() : MockClass(nullptr) {
}
///////////////////////////////////////////////////////////////
// Implementation of the SubtypeCheck::KlassT static interface.
///////////////////////////////////////////////////////////////
MockClass* GetSuperClass() const {
return parent_;
}
bool HasSuperClass() const {
return GetSuperClass() != nullptr;
}
size_t Depth() const {
if (parent_ == nullptr) {
return 0u;
} else {
return parent_->Depth() + 1u;
}
}
std::string PrettyClass() const
REQUIRES_SHARED(Locks::mutator_lock_) {
return path_to_root_;
}
int32_t GetField32Volatile(art::MemberOffset offset = art::MemberOffset(0u)) const
REQUIRES_SHARED(Locks::mutator_lock_) {
UNUSED(offset);
int32_t field_32 = 0;
memcpy(&field_32, &subtype_check_info_and_status_, sizeof(int32_t));
return field_32;
}
template <bool kTransactionActive>
bool CasField32(art::MemberOffset offset,
int32_t old_value,
int32_t new_value,
CASMode mode ATTRIBUTE_UNUSED,
std::memory_order memory_order ATTRIBUTE_UNUSED)
REQUIRES_SHARED(Locks::mutator_lock_) {
UNUSED(offset);
if (old_value == GetField32Volatile(offset)) {
memcpy(&subtype_check_info_and_status_, &new_value, sizeof(int32_t));
return true;
}
return false;
}
MemberOffset StatusOffset() const {
return MemberOffset(0); // Doesn't matter. We ignore offset.
}
///////////////////////////////////////////////////////////////
// Convenience functions to make the testing easier
///////////////////////////////////////////////////////////////
size_t GetNumberOfChildren() const {
return children_.size();
}
MockClass* GetParent() const {
return parent_;
}
MockClass* GetMaxChild() const {
if (GetNumberOfChildren() > 0u) {
return GetChild(GetNumberOfChildren() - 1);
}
return nullptr;
}
MockClass* GetChild(size_t idx) const {
if (idx >= GetNumberOfChildren()) {
return nullptr;
}
return children_[idx];
}
// Traverse the sibling at "X" at each level.
// Once we get to level==depth, return yourself.
MockClass* FindChildAt(size_t x, size_t depth) {
if (Depth() == depth) {
return this;
} else if (GetNumberOfChildren() > 0) {
return GetChild(x)->FindChildAt(x, depth);
}
return nullptr;
}
template <typename T>
MockClass* Visit(T visitor, bool recursive = true) {
if (!visitor(this)) {
return this;
}
if (!recursive) {
return this;
}
for (MockClass* child : children_) {
MockClass* visit_res = child->Visit(visitor);
if (visit_res != nullptr) {
return visit_res;
}
}
return nullptr;
}
size_t GetX() const {
return x_;
}
bool SlowIsSubtypeOf(const MockClass* target) const {
DCHECK(target != nullptr);
const MockClass* kls = this;
while (kls != nullptr) {
if (kls == target) {
return true;
}
kls = kls->GetSuperClass();
}
return false;
}
std::string ToDotGraph() const {
std::stringstream ss;
ss << std::endl;
ss << "digraph MockClass {" << std::endl;
ss << " node [fontname=\"Arial\"];" << std::endl;
ToDotGraphImpl(ss);
ss << "}" << std::endl;
return ss.str();
}
void ToDotGraphImpl(std::ostream& os) const {
for (MockClass* child : children_) {
os << " '" << path_to_root_ << "' -> '" << child->path_to_root_ << "';" << std::endl;
child->ToDotGraphImpl(os);
}
}
std::vector<MockClass*> children_;
MockClass* parent_;
SubtypeCheckBitsAndStatus subtype_check_info_and_status_;
size_t x_;
size_t y_;
std::string path_to_root_;
};
std::ostream& operator<<(std::ostream& os, const MockClass& kls) {
SubtypeCheckBits iod = kls.subtype_check_info_and_status_.subtype_check_info_;
os << "MClass{D:" << kls.Depth() << ",W:" << kls.x_
<< ", OF:"
<< (iod.overflow_ ? "true" : "false")
<< ", bitstring: " << iod.bitstring_
<< ", mock_path: " << kls.path_to_root_
<< "}";
return os;
}
struct MockSubtypeCheck {
using SC = SubtypeCheck<MockClass*>;
static MockSubtypeCheck Lookup(MockClass* klass) {
MockSubtypeCheck mock;
mock.klass_ = klass;
return mock;
}
// Convenience functions to avoid using statics everywhere.
// static(class, args...) -> instance.method(args...)
SubtypeCheckInfo::State EnsureInitialized()
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::EnsureInitialized(klass_);
}
SubtypeCheckInfo::State EnsureAssigned()
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::EnsureAssigned(klass_);
}
SubtypeCheckInfo::State ForceUninitialize()
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::ForceUninitialize(klass_);
}
BitString::StorageType GetEncodedPathToRootForSource() const
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::GetEncodedPathToRootForSource(klass_);
}
BitString::StorageType GetEncodedPathToRootForTarget() const
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::GetEncodedPathToRootForTarget(klass_);
}
BitString::StorageType GetEncodedPathToRootMask() const
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::GetEncodedPathToRootMask(klass_);
}
SubtypeCheckInfo::Result IsSubtypeOf(const MockSubtypeCheck& target)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::IsSubtypeOf(klass_, target.klass_);
}
friend std::ostream& operator<<(std::ostream& os, const MockSubtypeCheck& tree)
NO_THREAD_SAFETY_ANALYSIS {
os << "(MockSubtypeCheck io:";
SC::Dump(tree.klass_, os);
os << ", class: " << tree.klass_->PrettyClass() << ")";
return os;
}
// Additional convenience functions.
SubtypeCheckInfo::State GetState() const
REQUIRES(Locks::subtype_check_lock_)
REQUIRES_SHARED(Locks::mutator_lock_) {
return SC::GetSubtypeCheckInfo(klass_).GetState();
}
MockClass& GetClass() const {
return *klass_;
}
private:
MockClass* klass_;
};
struct MockScopedLockSubtypeCheck {
MockScopedLockSubtypeCheck() ACQUIRE(*Locks::subtype_check_lock_) {}
~MockScopedLockSubtypeCheck() RELEASE(*Locks::subtype_check_lock_) {}
};
struct MockScopedLockMutator {
MockScopedLockMutator() ACQUIRE_SHARED(*Locks::mutator_lock_) {}
~MockScopedLockMutator() RELEASE_SHARED(*Locks::mutator_lock_) {}
};
struct SubtypeCheckTest : public ::testing::Test {
protected:
void SetUp() override {
android::base::InitLogging(/*argv=*/nullptr);
CreateRootedTree(BitString::kCapacity + 2u, BitString::kCapacity + 2u);
}
void TearDown() override {
}
void CreateRootedTree(size_t width, size_t height) {
all_classes_.clear();
root_ = CreateClassFor(/*parent=*/nullptr, /*x=*/0, /*y=*/0);
CreateTreeFor(root_, /*width=*/width, /*levels=*/height);
}
MockClass* CreateClassFor(MockClass* parent, size_t x, size_t y) {
MockClass* kls = new MockClass(parent, x, y);
all_classes_.push_back(std::unique_ptr<MockClass>(kls));
return kls;
}
void CreateTreeFor(MockClass* parent, size_t width, size_t levels) {
DCHECK(parent != nullptr);
if (levels == 0) {
return;
}
for (size_t i = 0; i < width; ++i) {
MockClass* child = CreateClassFor(parent, i, parent->y_ + 1);
CreateTreeFor(child, width, levels - 1);
}
}
MockClass* root_ = nullptr;
std::vector<std::unique_ptr<MockClass>> all_classes_;
};
TEST_F(SubtypeCheckTest, LookupAllChildren) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
root_->Visit([&](MockClass* kls) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, SCTree::Lookup(kls).GetState());
return true; // Keep visiting.
});
}
TEST_F(SubtypeCheckTest, LookupRoot) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree root = SCTree::Lookup(root_);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, root.EnsureInitialized());
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, root.IsSubtypeOf(root)) << root;
}
TEST_F(SubtypeCheckTest, EnsureInitializedFirstLevel) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree root = SCTree::Lookup(root_);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, root.EnsureInitialized());
ASSERT_LT(0u, root_->GetNumberOfChildren());
// Initialize root's children only.
for (size_t i = 0; i < root_->GetNumberOfChildren(); ++i) {
MockClass* child = root_->GetChild(i);
SCTree child_tree = SCTree::Lookup(child);
// Before: all unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child_tree)) << child_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(root)) << child_tree;
// Transition.
EXPECT_EQ(SubtypeCheckInfo::kInitialized, child_tree.EnsureInitialized());
// After: "src instanceof target" known, but "target instanceof src" unknown.
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child_tree.IsSubtypeOf(root)) << child_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child_tree)) << child_tree;
}
}
TEST_F(SubtypeCheckTest, EnsureAssignedFirstLevel) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree root = SCTree::Lookup(root_);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, root.EnsureInitialized());
ASSERT_LT(0u, root_->GetNumberOfChildren());
// Initialize root's children only.
for (size_t i = 0; i < root_->GetNumberOfChildren(); ++i) {
MockClass* child = root_->GetChild(i);
SCTree child_tree = SCTree::Lookup(child);
// Before: all unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child_tree)) << child_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(root)) << child_tree;
// Transition.
EXPECT_EQ(SubtypeCheckInfo::kAssigned, child_tree.EnsureAssigned());
// After: "src instanceof target" known, and "target instanceof src" known.
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child_tree.IsSubtypeOf(root)) << child_tree;
EXPECT_EQ(SubtypeCheckInfo::kNotSubtypeOf, root.IsSubtypeOf(child_tree)) << child_tree;
}
}
TEST_F(SubtypeCheckTest, EnsureInitializedSecondLevelWithPreassign) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree root = SCTree::Lookup(root_);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, root.EnsureInitialized());
ASSERT_LT(0u, root_->GetNumberOfChildren());
// Initialize root's children.
for (size_t i = 0; i < root_->GetNumberOfChildren(); ++i) {
MockClass* child = root_->GetChild(i);
SCTree child_tree = SCTree::Lookup(child);
ASSERT_EQ(1u, child->Depth());
EXPECT_EQ(SubtypeCheckInfo::kInitialized, child_tree.EnsureInitialized()) << *child;
EXPECT_EQ(SubtypeCheckInfo::kAssigned, child_tree.EnsureAssigned())
<< *child << ", root:" << *root_;
for (size_t j = 0; j < child->GetNumberOfChildren(); ++j) {
MockClass* child2 = child->GetChild(j);
ASSERT_EQ(2u, child2->Depth());
SCTree child2_tree = SCTree::Lookup(child2);
// Before: all unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child2_tree)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(child2_tree))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child2_tree.IsSubtypeOf(root))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child2_tree.IsSubtypeOf(child_tree))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, child2_tree.GetState()) << *child2;
EXPECT_EQ(SubtypeCheckInfo::kInitialized, child2_tree.EnsureInitialized()) << *child2;
// After: src=child2_tree is known, otherwise unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child2_tree)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(child2_tree))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child2_tree.IsSubtypeOf(root)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child2_tree.IsSubtypeOf(child_tree)) << child2_tree;
}
// The child is "assigned" as a side-effect of initializing sub-children.
EXPECT_EQ(SubtypeCheckInfo::kAssigned, child_tree.GetState());
}
}
TEST_F(SubtypeCheckTest, EnsureInitializedSecondLevelDontPreassign) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree root = SCTree::Lookup(root_);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, root.EnsureInitialized());
ASSERT_LT(0u, root_->GetNumberOfChildren());
// Initialize root's children only.
for (size_t i = 0; i < root_->GetNumberOfChildren(); ++i) {
MockClass* child = root_->GetChild(i);
SCTree child_tree = SCTree::Lookup(child);
ASSERT_EQ(1u, child->Depth());
for (size_t j = 0; j < child->GetNumberOfChildren(); ++j) {
MockClass* child2 = child->GetChild(j);
ASSERT_EQ(2u, child2->Depth());
SCTree child2_tree = SCTree::Lookup(child2);
// Before: all unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child2_tree)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(child2_tree))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child2_tree.IsSubtypeOf(root)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child2_tree.IsSubtypeOf(child_tree))
<< child2_tree;
// Transition.
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, child2_tree.GetState()) << *child2;
EXPECT_EQ(SubtypeCheckInfo::kInitialized, child2_tree.EnsureInitialized()) << *child2;
// After: src=child2_tree is known, otherwise unknown.
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, root.IsSubtypeOf(child2_tree)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, child_tree.IsSubtypeOf(child2_tree))
<< child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child2_tree.IsSubtypeOf(root)) << child2_tree;
EXPECT_EQ(SubtypeCheckInfo::kSubtypeOf, child2_tree.IsSubtypeOf(child_tree)) << child2_tree;
}
// The child is "assigned" as a side-effect of initializing sub-children.
EXPECT_EQ(SubtypeCheckInfo::kAssigned, child_tree.GetState());
}
}
void ApplyTransition(MockSubtypeCheck sc_tree,
SubtypeCheckInfo::State transition,
SubtypeCheckInfo::State expected) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, sc_tree.GetState()) << sc_tree.GetClass();
if (transition == SubtypeCheckInfo::kUninitialized) {
EXPECT_EQ(expected, sc_tree.ForceUninitialize()) << sc_tree.GetClass();
} else if (transition == SubtypeCheckInfo::kInitialized) {
EXPECT_EQ(expected, sc_tree.EnsureInitialized()) << sc_tree.GetClass();
} else if (transition == SubtypeCheckInfo::kAssigned) {
EXPECT_EQ(expected, sc_tree.EnsureAssigned()) << sc_tree.GetClass();
}
}
enum MockSubtypeOfTransition {
kNone,
kUninitialized,
kInitialized,
kAssigned,
};
std::ostream& operator<<(std::ostream& os, const MockSubtypeOfTransition& transition) {
if (transition == MockSubtypeOfTransition::kUninitialized) {
os << "kUninitialized";
} else if (transition == MockSubtypeOfTransition::kInitialized) {
os << "kInitialized";
} else if (transition == MockSubtypeOfTransition::kAssigned) {
os << "kAssigned";
} else {
os << "kNone";
}
return os;
}
SubtypeCheckInfo::State ApplyTransition(MockSubtypeCheck sc_tree,
MockSubtypeOfTransition transition) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
if (transition == MockSubtypeOfTransition::kUninitialized) {
return sc_tree.ForceUninitialize();
} else if (transition == MockSubtypeOfTransition::kInitialized) {
return sc_tree.EnsureInitialized();
} else if (transition == MockSubtypeOfTransition::kAssigned) {
return sc_tree.EnsureAssigned();
}
return sc_tree.GetState();
}
enum {
kBeforeTransition = 0,
kAfterTransition = 1,
kAfterChildren = 2,
};
const char* StringifyTransition(int x) {
if (x == kBeforeTransition) {
return "kBeforeTransition";
} else if (x == kAfterTransition) {
return "kAfterTransition";
} else if (x == kAfterChildren) {
return "kAfterChildren";
}
return "<<Unknown>>";
}
struct TransitionHistory {
void Record(int transition_label, MockClass* kls) {
ss_ << "<<<" << StringifyTransition(transition_label) << ">>>";
ss_ << "{Self}: " << *kls;
if (kls->HasSuperClass()) {
ss_ << "{Parent}: " << *(kls->GetSuperClass());
}
ss_ << "================== ";
}
friend std::ostream& operator<<(std::ostream& os, const TransitionHistory& t) {
os << t.ss_.str();
return os;
}
std::stringstream ss_;
};
template <typename T, typename T2>
void EnsureStateChangedTestRecursiveGeneric(MockClass* klass,
size_t cur_depth,
size_t total_depth,
T2 transition_func,
T expect_checks) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
SCTree sc_tree = SCTree::Lookup(klass);
MockSubtypeOfTransition requested_transition = transition_func(klass);
// FIXME: need to print before(self, parent) and after(self, parent)
// to make any sense of what's going on.
auto do_expect_checks = [&](int transition_label, TransitionHistory& transition_details) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
transition_details.Record(transition_label, klass);
SCOPED_TRACE(transition_details);
ASSERT_EQ(cur_depth, klass->Depth());
ASSERT_NO_FATAL_FAILURE(expect_checks(klass,
transition_label,
sc_tree.GetState(),
requested_transition));
};
TransitionHistory transition_history;
do_expect_checks(kBeforeTransition, transition_history);
SubtypeCheckInfo::State state = ApplyTransition(sc_tree, requested_transition);
UNUSED(state);
do_expect_checks(kAfterTransition, transition_history);
if (total_depth == cur_depth) {
return;
}
// Initialize root's children only.
for (size_t i = 0; i < klass->GetNumberOfChildren(); ++i) {
MockClass* child = klass->GetChild(i);
EnsureStateChangedTestRecursiveGeneric(child,
cur_depth + 1u,
total_depth,
transition_func,
expect_checks);
}
do_expect_checks(kAfterChildren, transition_history);
}
void EnsureStateChangedTestRecursive(
MockClass* klass,
size_t cur_depth,
size_t total_depth,
const std::vector<std::pair<SubtypeCheckInfo::State, SubtypeCheckInfo::State>>& transitions) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
ASSERT_EQ(cur_depth, klass->Depth());
ApplyTransition(SCTree::Lookup(klass),
transitions[cur_depth].first,
transitions[cur_depth].second);
if (total_depth == cur_depth + 1) {
return;
}
// Initialize root's children only.
for (size_t i = 0; i < klass->GetNumberOfChildren(); ++i) {
MockClass* child = klass->GetChild(i);
EnsureStateChangedTestRecursive(child, cur_depth + 1u, total_depth, transitions);
}
}
void EnsureStateChangedTest(
MockClass* root,
size_t depth,
const std::vector<std::pair<SubtypeCheckInfo::State, SubtypeCheckInfo::State>>& transitions) {
ASSERT_EQ(depth, transitions.size());
EnsureStateChangedTestRecursive(root, /*cur_depth=*/0u, depth, transitions);
}
TEST_F(SubtypeCheckTest, EnsureInitialized_NoOverflow) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kInitialized;
};
constexpr size_t kMaxDepthForThisTest = BitString::kCapacity;
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
if (expect_when == kBeforeTransition) {
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, actual_state);
return;
}
if (expect_when == kAfterTransition) {
// After explicit transition has been completed.
switch (kls->Depth()) {
case 0:
if (transition >= MockSubtypeOfTransition::kInitialized) {
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
break;
default:
if (transition >= MockSubtypeOfTransition::kInitialized) {
if (transition == MockSubtypeOfTransition::kInitialized) {
EXPECT_EQ(SubtypeCheckInfo::kInitialized, actual_state);
} else if (transition == MockSubtypeOfTransition::kAssigned) {
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
}
break;
}
}
if (expect_when == kAfterChildren) {
if (transition >= MockSubtypeOfTransition::kInitialized) {
ASSERT_NE(kls->Depth(), kMaxDepthForThisTest);
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
}
};
// Initialize every level 0-3.
// Intermediate levels become "assigned", max levels become initialized.
EnsureStateChangedTestRecursiveGeneric(root_, 0u, kMaxDepthForThisTest, transitions, expected);
auto transitions_uninitialize = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kUninitialized;
};
auto expected_uninitialize = [](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(kls);
UNUSED(transition);
if (expect_when >= kAfterTransition) {
EXPECT_EQ(SubtypeCheckInfo::kUninitialized, actual_state);
}
};
// Uninitialize the entire tree after it was assigned.
EnsureStateChangedTestRecursiveGeneric(root_,
0u,
kMaxDepthForThisTest,
transitions_uninitialize,
expected_uninitialize);
}
TEST_F(SubtypeCheckTest, EnsureAssigned_TooDeep) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kAssigned;
};
constexpr size_t kMaxDepthForThisTest = BitString::kCapacity + 1u;
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(transition);
if (expect_when == kAfterTransition) {
if (kls->Depth() > BitString::kCapacity) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
}
}
};
// Assign every level 0-4.
// We cannot assign 4th level, so it will overflow instead.
EnsureStateChangedTestRecursiveGeneric(root_, 0u, kMaxDepthForThisTest, transitions, expected);
}
TEST_F(SubtypeCheckTest, EnsureAssigned_TooDeep_OfTooDeep) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kAssigned;
};
constexpr size_t kMaxDepthForThisTest = BitString::kCapacity + 2u;
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(transition);
if (expect_when == kAfterTransition) {
if (kls->Depth() > BitString::kCapacity) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
}
}
};
// Assign every level 0-5.
// We cannot assign 4th level, so it will overflow instead.
// In addition, level 5th cannot be assigned (parent is overflowed), so it will also fail.
EnsureStateChangedTestRecursiveGeneric(root_, 0u, kMaxDepthForThisTest, transitions, expected);
}
constexpr size_t MaxWidthCutOff(size_t depth) {
if (depth == 0) {
return 1;
}
if (depth > BitString::kCapacity) {
return std::numeric_limits<size_t>::max();
}
return MaxInt<size_t>(BitString::kBitSizeAtPosition[depth - 1]);
}
// Either itself is too wide, or any of the parents were too wide.
bool IsTooWide(MockClass* kls) {
if (kls == nullptr || kls->Depth() == 0u) {
// Root is never too wide.
return false;
} else {
if (kls->GetX() >= MaxWidthCutOff(kls->Depth())) {
return true;
}
}
return IsTooWide(kls->GetParent());
}
// Either itself is too deep, or any of the parents were too deep.
bool IsTooDeep(MockClass* kls) {
if (kls == nullptr || kls->Depth() == 0u) {
// Root is never too deep.
return false;
} else {
if (kls->Depth() > BitString::kCapacity) {
return true;
}
}
return false;
}
TEST_F(SubtypeCheckTest, EnsureInitialized_TooWide) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kAssigned;
};
// Pick the 2nd level because has the most narrow # of bits.
constexpr size_t kTargetDepth = 2;
constexpr size_t kMaxWidthCutOff = MaxWidthCutOff(kTargetDepth);
constexpr size_t kMaxDepthForThisTest = std::numeric_limits<size_t>::max();
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(transition);
// Note: purposefuly ignore the too-deep children in the premade tree.
if (expect_when == kAfterTransition && kls->Depth() <= BitString::kCapacity) {
if (IsTooWide(kls)) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
} else {
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
}
};
{
// Create too-wide siblings at the kTargetDepth level.
MockClass* child = root_->FindChildAt(/*x=*/0, kTargetDepth - 1u);
CreateTreeFor(child, kMaxWidthCutOff*2, /*levels=*/1);
ASSERT_LE(kMaxWidthCutOff*2, child->GetNumberOfChildren());
ASSERT_TRUE(IsTooWide(child->GetMaxChild())) << *(child->GetMaxChild());
// Leave the rest of the tree as the default.
}
// Try to assign every level
// It will fail once it gets to the "too wide" siblings and cause overflows.
EnsureStateChangedTestRecursiveGeneric(root_,
0u,
kMaxDepthForThisTest,
transitions,
expected);
}
TEST_F(SubtypeCheckTest, EnsureInitialized_TooWide_TooWide) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kAssigned;
};
// Pick the 2nd level because has the most narrow # of bits.
constexpr size_t kTargetDepth = 2;
constexpr size_t kMaxWidthCutOff = MaxWidthCutOff(kTargetDepth);
constexpr size_t kMaxWidthCutOffSub = MaxWidthCutOff(kTargetDepth+1u);
constexpr size_t kMaxDepthForThisTest = std::numeric_limits<size_t>::max();
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(transition);
// Note: purposefuly ignore the too-deep children in the premade tree.
if (expect_when == kAfterTransition && kls->Depth() <= BitString::kCapacity) {
if (IsTooWide(kls)) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
} else {
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
}
};
{
// Create too-wide siblings at the kTargetDepth level.
MockClass* child = root_->FindChildAt(/*x=*/0, kTargetDepth - 1);
CreateTreeFor(child, kMaxWidthCutOff*2, /*levels=*/1);
ASSERT_LE(kMaxWidthCutOff*2, child->GetNumberOfChildren()) << *child;
ASSERT_TRUE(IsTooWide(child->GetMaxChild())) << *(child->GetMaxChild());
// Leave the rest of the tree as the default.
// Create too-wide children for a too-wide parent.
MockClass* child_subchild = child->FindChildAt(/*x=*/0, kTargetDepth);
CreateTreeFor(child_subchild, kMaxWidthCutOffSub*2, /*levels=*/1);
ASSERT_LE(kMaxWidthCutOffSub*2, child_subchild->GetNumberOfChildren()) << *child_subchild;
ASSERT_TRUE(IsTooWide(child_subchild->GetMaxChild())) << *(child_subchild->GetMaxChild());
}
// Try to assign every level
// It will fail once it gets to the "too wide" siblings and cause overflows.
// Furthermore, assigning any subtree whose ancestor is too wide will also fail.
EnsureStateChangedTestRecursiveGeneric(root_, 0u, kMaxDepthForThisTest, transitions, expected);
}
void EnsureSubtypeOfCorrect(MockClass* a, MockClass* b) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
using SCTree = MockSubtypeCheck;
auto IsAssigned = [](SCTree& tree) {
MockScopedLockSubtypeCheck lock_a;
MockScopedLockMutator lock_b;
// This assumes that MockClass is always called with EnsureAssigned.
EXPECT_NE(SubtypeCheckInfo::kInitialized, tree.GetState());
EXPECT_NE(SubtypeCheckInfo::kUninitialized, tree.GetState());
// Use our own test checks, so we are actually testing different logic than the impl.
return !(IsTooDeep(&tree.GetClass()) || IsTooWide(&tree.GetClass()));
};
SCTree src_tree = SCTree::Lookup(a);
SCTree target_tree = SCTree::Lookup(b);
SCOPED_TRACE("class A");
SCOPED_TRACE(*a);
SCOPED_TRACE("class B");
SCOPED_TRACE(*b);
SubtypeCheckInfo::Result slow_result =
a->SlowIsSubtypeOf(b) ? SubtypeCheckInfo::kSubtypeOf : SubtypeCheckInfo::kNotSubtypeOf;
SubtypeCheckInfo::Result fast_result = src_tree.IsSubtypeOf(target_tree);
// Target must be Assigned for this check to succeed.
// Source is either Overflowed | Assigned (in this case).
if (IsAssigned(src_tree) && IsAssigned(target_tree)) {
ASSERT_EQ(slow_result, fast_result);
} else if (IsAssigned(src_tree)) {
// A is assigned. B is >= initialized.
ASSERT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, fast_result);
} else if (IsAssigned(target_tree)) {
// B is assigned. A is >= initialized.
ASSERT_EQ(slow_result, fast_result);
} else {
// Neither A,B are assigned.
ASSERT_EQ(SubtypeCheckInfo::kUnknownSubtypeOf, fast_result);
}
// Use asserts, not expects to immediately fail.
// Otherwise the entire tree (very large) could potentially be broken.
}
void EnsureSubtypeOfRecursive(MockClass* kls_root) {
MockScopedLockMutator mutator_lock_fake_;
auto visit_func = [&](MockClass* kls) {
kls->Visit([&](MockClass* inner_class) {
EnsureSubtypeOfCorrect(kls, inner_class);
EnsureSubtypeOfCorrect(inner_class, kls);
if (::testing::Test::HasFatalFailure()) {
return false;
}
return true; // Keep visiting.
});
if (::testing::Test::HasFatalFailure()) {
return false;
}
return true; // Keep visiting.
};
ASSERT_NO_FATAL_FAILURE(kls_root->Visit(visit_func));
}
TEST_F(SubtypeCheckTest, EnsureInitialized_TooWide_TooDeep) {
auto transitions = [](MockClass* kls) {
UNUSED(kls);
return MockSubtypeOfTransition::kAssigned;
};
// Pick the 2nd level because has the most narrow # of bits.
constexpr size_t kTargetDepth = 2;
constexpr size_t kTooDeepTargetDepth = BitString::kCapacity + 1;
constexpr size_t kMaxWidthCutOff = MaxWidthCutOff(kTargetDepth);
constexpr size_t kMaxDepthForThisTest = std::numeric_limits<size_t>::max();
auto expected = [=](MockClass* kls,
int expect_when,
SubtypeCheckInfo::State actual_state,
MockSubtypeOfTransition transition) {
UNUSED(transition);
if (expect_when == kAfterTransition) {
if (IsTooDeep(kls)) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
} else if (IsTooWide(kls)) {
EXPECT_EQ(SubtypeCheckInfo::kOverflowed, actual_state);
} else {
EXPECT_EQ(SubtypeCheckInfo::kAssigned, actual_state);
}
}
};
{
// Create too-wide siblings at the kTargetDepth level.
MockClass* child = root_->FindChildAt(/*x=*/0, kTargetDepth - 1u);
CreateTreeFor(child, kMaxWidthCutOff*2, /*levels=*/1);
ASSERT_LE(kMaxWidthCutOff*2, child->GetNumberOfChildren());
ASSERT_TRUE(IsTooWide(child->GetMaxChild())) << *(child->GetMaxChild());
// Leave the rest of the tree as the default.
// Create too-deep children for a too-wide parent.
MockClass* child_subchild = child->GetMaxChild();
ASSERT_TRUE(child_subchild != nullptr);
ASSERT_EQ(0u, child_subchild->GetNumberOfChildren()) << *child_subchild;
CreateTreeFor(child_subchild, /*width=*/1, /*levels=*/kTooDeepTargetDepth);
MockClass* too_deep_child = child_subchild->FindChildAt(0, kTooDeepTargetDepth + 2);
ASSERT_TRUE(too_deep_child != nullptr) << child_subchild->ToDotGraph();
ASSERT_TRUE(IsTooWide(too_deep_child)) << *(too_deep_child);
ASSERT_TRUE(IsTooDeep(too_deep_child)) << *(too_deep_child);
}
// Try to assign every level
// It will fail once it gets to the "too wide" siblings and cause overflows.
EnsureStateChangedTestRecursiveGeneric(root_, 0u, kMaxDepthForThisTest, transitions, expected);
// Check every class against every class for "x instanceof y".
EnsureSubtypeOfRecursive(root_);
}
// TODO: add dcheck for child-parent invariants (e.g. child < parent.next) and death tests
} // namespace art
|