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 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 17 | #include "indirect_reference_table.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | #include "common_runtime_test.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 20 | #include "mirror/object-inl.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 21 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 24 | class IndirectReferenceTableTest : public CommonRuntimeTest {}; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 25 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 26 | static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique) |
| 27 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 28 | std::ostringstream oss; |
| 29 | irt->Dump(oss); |
| 30 | if (num_objects == 0) { |
| 31 | EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str(); |
| 32 | } else if (num_objects == 1) { |
| 33 | EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str(); |
| 34 | } else { |
| 35 | EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)", |
| 36 | num_objects, num_unique)), |
| 37 | std::string::npos) |
| 38 | << "\n Expected number of objects: " << num_objects |
| 39 | << "\n Expected unique objects: " << num_unique << "\n" |
| 40 | << oss.str(); |
| 41 | } |
| 42 | } |
| 43 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 44 | TEST_F(IndirectReferenceTableTest, BasicTest) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 46 | static const size_t kTableInitial = 10; |
| 47 | static const size_t kTableMax = 20; |
| 48 | IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal); |
| 49 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 50 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | mirror::Object* obj0 = c->AllocObject(soa.Self()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(obj0 != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | mirror::Object* obj1 = c->AllocObject(soa.Self()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 55 | ASSERT_TRUE(obj1 != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | mirror::Object* obj2 = c->AllocObject(soa.Self()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(obj2 != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | mirror::Object* obj3 = c->AllocObject(soa.Self()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 59 | ASSERT_TRUE(obj3 != NULL); |
| 60 | |
| 61 | const uint32_t cookie = IRT_FIRST_SEGMENT; |
| 62 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 63 | CheckDump(&irt, 0, 0); |
| 64 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 65 | IndirectRef iref0 = (IndirectRef) 0x11110; |
| 66 | EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal"; |
| 67 | |
| 68 | // Add three, check, remove in the order in which they were added. |
| 69 | iref0 = irt.Add(cookie, obj0); |
| 70 | EXPECT_TRUE(iref0 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 71 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 72 | IndirectRef iref1 = irt.Add(cookie, obj1); |
| 73 | EXPECT_TRUE(iref1 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 74 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 75 | IndirectRef iref2 = irt.Add(cookie, obj2); |
| 76 | EXPECT_TRUE(iref2 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 77 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 78 | |
| 79 | EXPECT_EQ(obj0, irt.Get(iref0)); |
| 80 | EXPECT_EQ(obj1, irt.Get(iref1)); |
| 81 | EXPECT_EQ(obj2, irt.Get(iref2)); |
| 82 | |
| 83 | EXPECT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 84 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 86 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 88 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 89 | |
| 90 | // Table should be empty now. |
| 91 | EXPECT_EQ(0U, irt.Capacity()); |
| 92 | |
| 93 | // Get invalid entry (off the end of the list). |
| 94 | EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)); |
| 95 | |
| 96 | // Add three, remove in the opposite order. |
| 97 | iref0 = irt.Add(cookie, obj0); |
| 98 | EXPECT_TRUE(iref0 != NULL); |
| 99 | iref1 = irt.Add(cookie, obj1); |
| 100 | EXPECT_TRUE(iref1 != NULL); |
| 101 | iref2 = irt.Add(cookie, obj2); |
| 102 | EXPECT_TRUE(iref2 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 103 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 104 | |
| 105 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 106 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 107 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 108 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 110 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 111 | |
| 112 | // Table should be empty now. |
| 113 | ASSERT_EQ(0U, irt.Capacity()); |
| 114 | |
| 115 | // Add three, remove middle / middle / bottom / top. (Second attempt |
| 116 | // to remove middle should fail.) |
| 117 | iref0 = irt.Add(cookie, obj0); |
| 118 | EXPECT_TRUE(iref0 != NULL); |
| 119 | iref1 = irt.Add(cookie, obj1); |
| 120 | EXPECT_TRUE(iref1 != NULL); |
| 121 | iref2 = irt.Add(cookie, obj2); |
| 122 | EXPECT_TRUE(iref2 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 123 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 124 | |
| 125 | ASSERT_EQ(3U, irt.Capacity()); |
| 126 | |
| 127 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 128 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 129 | ASSERT_FALSE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 130 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 131 | |
| 132 | // Get invalid entry (from hole). |
| 133 | EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1)); |
| 134 | |
| 135 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 136 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 137 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 138 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 139 | |
| 140 | // Table should be empty now. |
| 141 | ASSERT_EQ(0U, irt.Capacity()); |
| 142 | |
| 143 | // Add four entries. Remove #1, add new entry, verify that table size |
| 144 | // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 145 | // that we delete one and don't hole-compact the other. |
| 146 | iref0 = irt.Add(cookie, obj0); |
| 147 | EXPECT_TRUE(iref0 != NULL); |
| 148 | iref1 = irt.Add(cookie, obj1); |
| 149 | EXPECT_TRUE(iref1 != NULL); |
| 150 | iref2 = irt.Add(cookie, obj2); |
| 151 | EXPECT_TRUE(iref2 != NULL); |
| 152 | IndirectRef iref3 = irt.Add(cookie, obj3); |
| 153 | EXPECT_TRUE(iref3 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 154 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 155 | |
| 156 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 157 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 158 | |
| 159 | iref1 = irt.Add(cookie, obj1); |
| 160 | EXPECT_TRUE(iref1 != NULL); |
| 161 | |
| 162 | ASSERT_EQ(4U, irt.Capacity()) << "hole not filled"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 163 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 164 | |
| 165 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 166 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 167 | ASSERT_TRUE(irt.Remove(cookie, iref3)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 168 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 169 | |
| 170 | ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions"; |
| 171 | |
| 172 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 173 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 174 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 175 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 176 | |
| 177 | ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove"; |
| 178 | |
| 179 | // Add an entry, remove it, add a new entry, and try to use the original |
| 180 | // iref. They have the same slot number but are for different objects. |
| 181 | // With the extended checks in place, this should fail. |
| 182 | iref0 = irt.Add(cookie, obj0); |
| 183 | EXPECT_TRUE(iref0 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 184 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 185 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 186 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 187 | iref1 = irt.Add(cookie, obj1); |
| 188 | EXPECT_TRUE(iref1 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 189 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 190 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 191 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 192 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed"; |
| 193 | ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 194 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 195 | |
| 196 | // Same as above, but with the same object. A more rigorous checker |
| 197 | // (e.g. with slot serialization) will catch this. |
| 198 | iref0 = irt.Add(cookie, obj0); |
| 199 | EXPECT_TRUE(iref0 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 200 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 201 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 202 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 203 | iref1 = irt.Add(cookie, obj0); |
| 204 | EXPECT_TRUE(iref1 != NULL); |
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 | if (iref0 != iref1) { |
| 207 | // Try 0, should not work. |
| 208 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded"; |
| 209 | } |
| 210 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed"; |
| 211 | ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 212 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 213 | |
| 214 | // NULL isn't a valid iref. |
| 215 | ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL)); |
| 216 | |
| 217 | // Stale lookup. |
| 218 | iref0 = irt.Add(cookie, obj0); |
| 219 | EXPECT_TRUE(iref0 != NULL); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 220 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 221 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
| 222 | EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 223 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 224 | |
| 225 | // Test table resizing. |
| 226 | // These ones fit... |
| 227 | IndirectRef manyRefs[kTableInitial]; |
| 228 | for (size_t i = 0; i < kTableInitial; i++) { |
| 229 | manyRefs[i] = irt.Add(cookie, obj0); |
| 230 | ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 231 | CheckDump(&irt, i + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 232 | } |
| 233 | // ...this one causes overflow. |
| 234 | iref0 = irt.Add(cookie, obj0); |
| 235 | ASSERT_TRUE(iref0 != NULL); |
| 236 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 237 | CheckDump(&irt, kTableInitial + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 238 | |
| 239 | for (size_t i = 0; i < kTableInitial; i++) { |
| 240 | ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 241 | CheckDump(&irt, kTableInitial - i, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 242 | } |
| 243 | // Because of removal order, should have 11 entries, 10 of them holes. |
| 244 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
| 245 | |
| 246 | ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed"; |
| 247 | |
| 248 | ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 249 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace art |