Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 17 | #include "indirect_reference_table-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "common_runtime_test.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 23 | #include "mirror/object-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | class IndirectReferenceTableTest : public CommonRuntimeTest {}; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 31 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 32 | static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 33 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 34 | std::ostringstream oss; |
| 35 | irt->Dump(oss); |
| 36 | if (num_objects == 0) { |
| 37 | EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str(); |
| 38 | } else if (num_objects == 1) { |
| 39 | EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str(); |
| 40 | } else { |
| 41 | EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)", |
| 42 | num_objects, num_unique)), |
| 43 | std::string::npos) |
| 44 | << "\n Expected number of objects: " << num_objects |
| 45 | << "\n Expected unique objects: " << num_unique << "\n" |
| 46 | << oss.str(); |
| 47 | } |
| 48 | } |
| 49 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 50 | TEST_F(IndirectReferenceTableTest, BasicTest) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 51 | // This will lead to error messages in the log. |
| 52 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 53 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 54 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 55 | static const size_t kTableMax = 20; |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 56 | std::string error_msg; |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 57 | IndirectReferenceTable irt(kTableMax, |
| 58 | kGlobal, |
| 59 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 60 | &error_msg); |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 61 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 62 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 63 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 64 | StackHandleScope<4> hs(soa.Self()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(c != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 66 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 67 | ASSERT_TRUE(obj0.Get() != nullptr); |
| 68 | Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 69 | ASSERT_TRUE(obj1.Get() != nullptr); |
| 70 | Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 71 | ASSERT_TRUE(obj2.Get() != nullptr); |
| 72 | Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 73 | ASSERT_TRUE(obj3.Get() != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 74 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 75 | const IRTSegmentState cookie = kIRTFirstSegment; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 76 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 77 | CheckDump(&irt, 0, 0); |
| 78 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 79 | IndirectRef iref0 = (IndirectRef) 0x11110; |
| 80 | EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal"; |
| 81 | |
| 82 | // Add three, check, remove in the order in which they were added. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 83 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 85 | CheckDump(&irt, 1, 1); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 86 | IndirectRef iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 88 | CheckDump(&irt, 2, 2); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 89 | IndirectRef iref2 = irt.Add(cookie, obj2.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 91 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 92 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 93 | EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0)); |
| 94 | EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1)); |
| 95 | EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 96 | |
| 97 | EXPECT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 98 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 99 | EXPECT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 100 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 102 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 103 | |
| 104 | // Table should be empty now. |
| 105 | EXPECT_EQ(0U, irt.Capacity()); |
| 106 | |
| 107 | // Get invalid entry (off the end of the list). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 108 | EXPECT_TRUE(irt.Get(iref0) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 109 | |
| 110 | // Add three, remove in the opposite order. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 111 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 112 | EXPECT_TRUE(iref0 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 113 | iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(iref1 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 115 | iref2 = irt.Add(cookie, obj2.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 117 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 118 | |
| 119 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 120 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 122 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 124 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 125 | |
| 126 | // Table should be empty now. |
| 127 | ASSERT_EQ(0U, irt.Capacity()); |
| 128 | |
| 129 | // Add three, remove middle / middle / bottom / top. (Second attempt |
| 130 | // to remove middle should fail.) |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 131 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 132 | EXPECT_TRUE(iref0 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 133 | iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(iref1 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 135 | iref2 = irt.Add(cookie, obj2.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 137 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 138 | |
| 139 | ASSERT_EQ(3U, irt.Capacity()); |
| 140 | |
| 141 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 142 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 143 | ASSERT_FALSE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 144 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 145 | |
| 146 | // Get invalid entry (from hole). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 147 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 148 | |
| 149 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 150 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 151 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 152 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 153 | |
| 154 | // Table should be empty now. |
| 155 | ASSERT_EQ(0U, irt.Capacity()); |
| 156 | |
| 157 | // Add four entries. Remove #1, add new entry, verify that table size |
| 158 | // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 159 | // that we delete one and don't hole-compact the other. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 160 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 161 | EXPECT_TRUE(iref0 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 162 | iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(iref1 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 164 | iref2 = irt.Add(cookie, obj2.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(iref2 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 166 | IndirectRef iref3 = irt.Add(cookie, obj3.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 167 | EXPECT_TRUE(iref3 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 168 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 169 | |
| 170 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 171 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 173 | iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 174 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 175 | |
| 176 | ASSERT_EQ(4U, irt.Capacity()) << "hole not filled"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 177 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 178 | |
| 179 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 180 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(irt.Remove(cookie, iref3)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 182 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 183 | |
| 184 | ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions"; |
| 185 | |
| 186 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 187 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 189 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 190 | |
| 191 | ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove"; |
| 192 | |
| 193 | // Add an entry, remove it, add a new entry, and try to use the original |
| 194 | // iref. They have the same slot number but are for different objects. |
| 195 | // With the extended checks in place, this should fail. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 196 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 197 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 198 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 199 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 200 | CheckDump(&irt, 0, 0); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 201 | iref1 = irt.Add(cookie, obj1.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 202 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 203 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 204 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 205 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed"; |
| 207 | ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 208 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 209 | |
| 210 | // Same as above, but with the same object. A more rigorous checker |
| 211 | // (e.g. with slot serialization) will catch this. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 212 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 213 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 214 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 216 | CheckDump(&irt, 0, 0); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 217 | iref1 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 218 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 219 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 220 | if (iref0 != iref1) { |
| 221 | // Try 0, should not work. |
| 222 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded"; |
| 223 | } |
| 224 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed"; |
| 225 | ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 226 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 228 | // null isn't a valid iref. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(irt.Get(nullptr) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 230 | |
| 231 | // Stale lookup. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 232 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 233 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 234 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 235 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 237 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 238 | |
| 239 | // Test table resizing. |
| 240 | // These ones fit... |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 241 | static const size_t kTableInitial = kTableMax / 2; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 242 | IndirectRef manyRefs[kTableInitial]; |
| 243 | for (size_t i = 0; i < kTableInitial; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 244 | manyRefs[i] = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 246 | CheckDump(&irt, i + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 247 | } |
| 248 | // ...this one causes overflow. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 249 | iref0 = irt.Add(cookie, obj0.Get()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 250 | ASSERT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 251 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 252 | CheckDump(&irt, kTableInitial + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 253 | |
| 254 | for (size_t i = 0; i < kTableInitial; i++) { |
| 255 | ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 256 | CheckDump(&irt, kTableInitial - i, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 257 | } |
| 258 | // Because of removal order, should have 11 entries, 10 of them holes. |
| 259 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
| 260 | |
| 261 | ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed"; |
| 262 | |
| 263 | ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 264 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 267 | TEST_F(IndirectReferenceTableTest, Holes) { |
| 268 | // Test the explicitly named cases from the IRT implementation: |
| 269 | // |
| 270 | // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference |
| 271 | // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference |
| 272 | // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove |
| 273 | // reference |
| 274 | // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference |
| 275 | // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove |
| 276 | // reference |
| 277 | |
| 278 | ScopedObjectAccess soa(Thread::Current()); |
| 279 | static const size_t kTableMax = 10; |
| 280 | |
| 281 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 282 | StackHandleScope<5> hs(soa.Self()); |
| 283 | ASSERT_TRUE(c != nullptr); |
| 284 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 285 | ASSERT_TRUE(obj0.Get() != nullptr); |
| 286 | Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 287 | ASSERT_TRUE(obj1.Get() != nullptr); |
| 288 | Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 289 | ASSERT_TRUE(obj2.Get() != nullptr); |
| 290 | Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 291 | ASSERT_TRUE(obj3.Get() != nullptr); |
| 292 | Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 293 | ASSERT_TRUE(obj4.Get() != nullptr); |
| 294 | |
| 295 | std::string error_msg; |
| 296 | |
| 297 | // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference. |
| 298 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 299 | IndirectReferenceTable irt(kTableMax, |
| 300 | kGlobal, |
| 301 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 302 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 303 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 304 | |
| 305 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 306 | |
| 307 | CheckDump(&irt, 0, 0); |
| 308 | |
| 309 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get()); |
| 310 | IndirectRef iref1 = irt.Add(cookie0, obj1.Get()); |
| 311 | IndirectRef iref2 = irt.Add(cookie0, obj2.Get()); |
| 312 | |
| 313 | EXPECT_TRUE(irt.Remove(cookie0, iref1)); |
| 314 | |
| 315 | // New segment. |
| 316 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 317 | |
| 318 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get()); |
| 319 | |
| 320 | // Must not have filled the previous hole. |
| 321 | EXPECT_EQ(irt.Capacity(), 4u); |
| 322 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
| 323 | CheckDump(&irt, 3, 3); |
| 324 | |
| 325 | UNUSED(iref0, iref1, iref2, iref3); |
| 326 | } |
| 327 | |
| 328 | // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference |
| 329 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 330 | IndirectReferenceTable irt(kTableMax, |
| 331 | kGlobal, |
| 332 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 333 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 334 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 335 | |
| 336 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 337 | |
| 338 | CheckDump(&irt, 0, 0); |
| 339 | |
| 340 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get()); |
| 341 | |
| 342 | // New segment. |
| 343 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 344 | |
| 345 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get()); |
| 346 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get()); |
| 347 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get()); |
| 348 | |
| 349 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 350 | |
| 351 | // Pop segment. |
| 352 | irt.SetSegmentState(cookie1); |
| 353 | |
| 354 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get()); |
| 355 | |
| 356 | EXPECT_EQ(irt.Capacity(), 2u); |
| 357 | EXPECT_TRUE(irt.Get(iref2) == nullptr); |
| 358 | CheckDump(&irt, 2, 2); |
| 359 | |
| 360 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 361 | } |
| 362 | |
| 363 | // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove |
| 364 | // reference. |
| 365 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 366 | IndirectReferenceTable irt(kTableMax, |
| 367 | kGlobal, |
| 368 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 369 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 370 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 371 | |
| 372 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 373 | |
| 374 | CheckDump(&irt, 0, 0); |
| 375 | |
| 376 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get()); |
| 377 | |
| 378 | // New segment. |
| 379 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 380 | |
| 381 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get()); |
| 382 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get()); |
| 383 | |
| 384 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 385 | |
| 386 | // New segment. |
| 387 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 388 | |
| 389 | IndirectRef iref3 = irt.Add(cookie2, obj3.Get()); |
| 390 | |
| 391 | // Pop segment. |
| 392 | irt.SetSegmentState(cookie2); |
| 393 | |
| 394 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get()); |
| 395 | |
| 396 | EXPECT_EQ(irt.Capacity(), 3u); |
| 397 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
| 398 | CheckDump(&irt, 3, 3); |
| 399 | |
| 400 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 401 | } |
| 402 | |
| 403 | // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference. |
| 404 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 405 | IndirectReferenceTable irt(kTableMax, |
| 406 | kGlobal, |
| 407 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 408 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 409 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 410 | |
| 411 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 412 | |
| 413 | CheckDump(&irt, 0, 0); |
| 414 | |
| 415 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get()); |
| 416 | |
| 417 | // New segment. |
| 418 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 419 | |
| 420 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get()); |
| 421 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 422 | |
| 423 | // Emptied segment, push new one. |
| 424 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 425 | |
| 426 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get()); |
| 427 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get()); |
| 428 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get()); |
| 429 | |
| 430 | EXPECT_TRUE(irt.Remove(cookie1, iref3)); |
| 431 | |
| 432 | // Pop segment. |
| 433 | UNUSED(cookie2); |
| 434 | irt.SetSegmentState(cookie1); |
| 435 | |
| 436 | IndirectRef iref5 = irt.Add(cookie1, obj4.Get()); |
| 437 | |
| 438 | EXPECT_EQ(irt.Capacity(), 2u); |
| 439 | EXPECT_TRUE(irt.Get(iref3) == nullptr); |
| 440 | CheckDump(&irt, 2, 2); |
| 441 | |
| 442 | UNUSED(iref0, iref1, iref2, iref3, iref4, iref5); |
| 443 | } |
| 444 | |
| 445 | // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove |
| 446 | // reference |
| 447 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 448 | IndirectReferenceTable irt(kTableMax, |
| 449 | kGlobal, |
| 450 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 451 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 452 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 453 | |
| 454 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 455 | |
| 456 | CheckDump(&irt, 0, 0); |
| 457 | |
| 458 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get()); |
| 459 | |
| 460 | // New segment. |
| 461 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 462 | |
| 463 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get()); |
| 464 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get()); |
| 465 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get()); |
| 466 | |
| 467 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 468 | |
| 469 | // Pop segment. |
| 470 | irt.SetSegmentState(cookie1); |
| 471 | |
| 472 | // Push segment. |
| 473 | const IRTSegmentState cookie1_second = irt.GetSegmentState(); |
| 474 | UNUSED(cookie1_second); |
| 475 | |
| 476 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get()); |
| 477 | |
| 478 | EXPECT_EQ(irt.Capacity(), 2u); |
| 479 | EXPECT_TRUE(irt.Get(iref3) == nullptr); |
| 480 | CheckDump(&irt, 2, 2); |
| 481 | |
| 482 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 483 | } |
| 484 | } |
| 485 | |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 486 | TEST_F(IndirectReferenceTableTest, Resize) { |
| 487 | ScopedObjectAccess soa(Thread::Current()); |
| 488 | static const size_t kTableMax = 512; |
| 489 | |
| 490 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 491 | StackHandleScope<1> hs(soa.Self()); |
| 492 | ASSERT_TRUE(c != nullptr); |
| 493 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
| 494 | ASSERT_TRUE(obj0.Get() != nullptr); |
| 495 | |
| 496 | std::string error_msg; |
| 497 | IndirectReferenceTable irt(kTableMax, |
| 498 | kLocal, |
| 499 | IndirectReferenceTable::ResizableCapacity::kYes, |
| 500 | &error_msg); |
| 501 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 502 | |
| 503 | CheckDump(&irt, 0, 0); |
| 504 | const IRTSegmentState cookie = kIRTFirstSegment; |
| 505 | |
| 506 | for (size_t i = 0; i != kTableMax + 1; ++i) { |
| 507 | irt.Add(cookie, obj0.Get()); |
| 508 | } |
| 509 | |
| 510 | EXPECT_EQ(irt.Capacity(), kTableMax + 1); |
| 511 | } |
| 512 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 513 | } // namespace art |