blob: 99ee597bb89db24ba906f9824c488887adc84726 [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
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 Chartierc56057e2014-05-04 13:18:58 -070017#include "indirect_reference_table-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080018
19#include "common_runtime_test.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070020#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "scoped_thread_state_change.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070022
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023namespace art {
24
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026
Ian Rogers63818dc2012-09-26 12:23:04 -070027static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
28 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29 std::ostringstream oss;
30 irt->Dump(oss);
31 if (num_objects == 0) {
32 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
33 } else if (num_objects == 1) {
34 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
35 } else {
36 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
37 num_objects, num_unique)),
38 std::string::npos)
39 << "\n Expected number of objects: " << num_objects
40 << "\n Expected unique objects: " << num_unique << "\n"
41 << oss.str();
42 }
43}
44
Elliott Hughes6c1a3942011-08-17 15:00:06 -070045TEST_F(IndirectReferenceTableTest, BasicTest) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070047 static const size_t kTableInitial = 10;
48 static const size_t kTableMax = 20;
49 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
50
Ian Rogers98379392014-02-24 16:53:16 -080051 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogersc0542af2014-09-03 16:16:56 -070052 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 mirror::Object* obj0 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070054 ASSERT_TRUE(obj0 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 mirror::Object* obj1 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070056 ASSERT_TRUE(obj1 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::Object* obj2 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070058 ASSERT_TRUE(obj2 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Object* obj3 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070060 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070061
62 const uint32_t cookie = IRT_FIRST_SEGMENT;
63
Ian Rogers63818dc2012-09-26 12:23:04 -070064 CheckDump(&irt, 0, 0);
65
Elliott Hughes6c1a3942011-08-17 15:00:06 -070066 IndirectRef iref0 = (IndirectRef) 0x11110;
67 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
68
69 // Add three, check, remove in the order in which they were added.
70 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -070071 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070072 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070073 IndirectRef iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -070074 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070075 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076 IndirectRef iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -070077 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079
80 EXPECT_EQ(obj0, irt.Get(iref0));
81 EXPECT_EQ(obj1, irt.Get(iref1));
82 EXPECT_EQ(obj2, irt.Get(iref2));
83
84 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070085 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070086 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070087 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070089 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090
91 // Table should be empty now.
92 EXPECT_EQ(0U, irt.Capacity());
93
94 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -070095 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096
97 // Add three, remove in the opposite order.
98 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -070099 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700100 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700101 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700103 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700104 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105
106 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700107 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700108 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700109 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700111 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112
113 // Table should be empty now.
114 ASSERT_EQ(0U, irt.Capacity());
115
116 // Add three, remove middle / middle / bottom / top. (Second attempt
117 // to remove middle should fail.)
118 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700119 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700120 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700121 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700123 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700124 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125
126 ASSERT_EQ(3U, irt.Capacity());
127
128 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700129 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700131 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700132
133 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700134 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700135
136 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700137 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700139 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140
141 // Table should be empty now.
142 ASSERT_EQ(0U, irt.Capacity());
143
144 // Add four entries. Remove #1, add new entry, verify that table size
145 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
146 // that we delete one and don't hole-compact the other.
147 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700148 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700150 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700151 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700152 EXPECT_TRUE(iref2 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153 IndirectRef iref3 = irt.Add(cookie, obj3);
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700155 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156
157 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700158 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700159
160 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700161 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700162
163 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700164 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165
166 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700167 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700169 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170
171 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
172
173 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700174 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700176 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700177
178 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
179
180 // Add an entry, remove it, add a new entry, and try to use the original
181 // iref. They have the same slot number but are for different objects.
182 // With the extended checks in place, this should fail.
183 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700184 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700185 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700186 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700187 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700188 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700189 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700190 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700192 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
194 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700195 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700196
197 // Same as above, but with the same object. A more rigorous checker
198 // (e.g. with slot serialization) will catch this.
199 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700200 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700203 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700204 iref1 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700205 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700206 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 if (iref0 != iref1) {
208 // Try 0, should not work.
209 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
210 }
211 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
212 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700213 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700214
Ian Rogersc0542af2014-09-03 16:16:56 -0700215 // nullptr isn't a valid iref.
216 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217
218 // Stale lookup.
219 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700220 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700221 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700223 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700224 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700225
226 // Test table resizing.
227 // These ones fit...
228 IndirectRef manyRefs[kTableInitial];
229 for (size_t i = 0; i < kTableInitial; i++) {
230 manyRefs[i] = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700231 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700232 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233 }
234 // ...this one causes overflow.
235 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700236 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700238 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700239
240 for (size_t i = 0; i < kTableInitial; i++) {
241 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700242 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243 }
244 // Because of removal order, should have 11 entries, 10 of them holes.
245 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
246
247 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
248
249 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700250 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251}
252
253} // namespace art