blob: f376ec0c6d52b83b44b69ef4a6373befcde49735 [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
Vladimir Marko3481ba22015-04-13 12:22:36 +010019#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_runtime_test.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070021#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "scoped_thread_state_change.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024namespace art {
25
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070027
Ian Rogers63818dc2012-09-26 12:23:04 -070028static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
Mathieu Chartier90443472015-07-16 20:32:27 -070029 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers63818dc2012-09-26 12:23:04 -070030 std::ostringstream oss;
31 irt->Dump(oss);
32 if (num_objects == 0) {
33 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
34 } else if (num_objects == 1) {
35 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
36 } else {
37 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
38 num_objects, num_unique)),
39 std::string::npos)
40 << "\n Expected number of objects: " << num_objects
41 << "\n Expected unique objects: " << num_unique << "\n"
42 << oss.str();
43 }
44}
45
Elliott Hughes6c1a3942011-08-17 15:00:06 -070046TEST_F(IndirectReferenceTableTest, BasicTest) {
Andreas Gampe369810a2015-01-14 19:53:31 -080047 // This will lead to error messages in the log.
48 ScopedLogSeverity sls(LogSeverity::FATAL);
49
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070051 static const size_t kTableInitial = 10;
52 static const size_t kTableMax = 20;
53 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
54
Ian Rogers98379392014-02-24 16:53:16 -080055 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogersc0542af2014-09-03 16:16:56 -070056 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::Object* obj0 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070058 ASSERT_TRUE(obj0 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Object* obj1 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070060 ASSERT_TRUE(obj1 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::Object* obj2 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070062 ASSERT_TRUE(obj2 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Object* obj3 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070064 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070065
66 const uint32_t cookie = IRT_FIRST_SEGMENT;
67
Ian Rogers63818dc2012-09-26 12:23:04 -070068 CheckDump(&irt, 0, 0);
69
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070 IndirectRef iref0 = (IndirectRef) 0x11110;
71 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
72
73 // Add three, check, remove in the order in which they were added.
74 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -070075 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070076 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070077 IndirectRef iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -070078 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070079 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070080 IndirectRef iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -070081 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070082 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070083
84 EXPECT_EQ(obj0, irt.Get(iref0));
85 EXPECT_EQ(obj1, irt.Get(iref1));
86 EXPECT_EQ(obj2, irt.Get(iref2));
87
88 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070089 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070091 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070093 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094
95 // Table should be empty now.
96 EXPECT_EQ(0U, irt.Capacity());
97
98 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -070099 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700100
101 // Add three, remove in the opposite order.
102 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700103 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700104 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700105 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700107 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700108 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
110 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700111 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700113 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700114 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700115 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116
117 // Table should be empty now.
118 ASSERT_EQ(0U, irt.Capacity());
119
120 // Add three, remove middle / middle / bottom / top. (Second attempt
121 // to remove middle should fail.)
122 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700123 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700125 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700127 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700128 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700129
130 ASSERT_EQ(3U, irt.Capacity());
131
132 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700133 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700134 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700135 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136
137 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700138 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139
140 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700141 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700142 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700143 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144
145 // Table should be empty now.
146 ASSERT_EQ(0U, irt.Capacity());
147
148 // Add four entries. Remove #1, add new entry, verify that table size
149 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
150 // that we delete one and don't hole-compact the other.
151 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700152 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700155 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700156 EXPECT_TRUE(iref2 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700157 IndirectRef iref3 = irt.Add(cookie, obj3);
Ian Rogersc0542af2014-09-03 16:16:56 -0700158 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700159 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700160
161 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700162 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700163
164 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700165 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700166
167 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700168 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700169
170 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700171 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700172 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700173 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700174
175 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
176
177 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700178 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700179 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700180 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181
182 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
183
184 // Add an entry, remove it, add a new entry, and try to use the original
185 // iref. They have the same slot number but are for different objects.
186 // With the extended checks in place, this should fail.
187 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700188 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700189 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700191 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700193 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700194 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700195 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700196 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
198 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700199 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700200
201 // Same as above, but with the same object. A more rigorous checker
202 // (e.g. with slot serialization) will catch this.
203 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700204 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700205 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700207 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208 iref1 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700209 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700210 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700211 if (iref0 != iref1) {
212 // Try 0, should not work.
213 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
214 }
215 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
216 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700217 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700218
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700219 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700220 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700221
222 // Stale lookup.
223 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700224 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700225 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700226 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700227 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700228 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700229
230 // Test table resizing.
231 // These ones fit...
232 IndirectRef manyRefs[kTableInitial];
233 for (size_t i = 0; i < kTableInitial; i++) {
234 manyRefs[i] = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700235 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700236 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 }
238 // ...this one causes overflow.
239 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700240 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700241 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700242 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243
244 for (size_t i = 0; i < kTableInitial; i++) {
245 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700246 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 }
248 // Because of removal order, should have 11 entries, 10 of them holes.
249 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
250
251 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
252
253 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700254 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700255}
256
257} // namespace art