blob: 1156cf5996a68d3ea4c45e07354cd91a8255e37a [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) {
Andreas Gampe369810a2015-01-14 19:53:31 -080046 // This will lead to error messages in the log.
47 ScopedLogSeverity sls(LogSeverity::FATAL);
48
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070050 static const size_t kTableInitial = 10;
51 static const size_t kTableMax = 20;
52 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
53
Ian Rogers98379392014-02-24 16:53:16 -080054 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogersc0542af2014-09-03 16:16:56 -070055 ASSERT_TRUE(c != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 mirror::Object* obj0 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070057 ASSERT_TRUE(obj0 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::Object* obj1 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070059 ASSERT_TRUE(obj1 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 mirror::Object* obj2 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070061 ASSERT_TRUE(obj2 != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 mirror::Object* obj3 = c->AllocObject(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070063 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064
65 const uint32_t cookie = IRT_FIRST_SEGMENT;
66
Ian Rogers63818dc2012-09-26 12:23:04 -070067 CheckDump(&irt, 0, 0);
68
Elliott Hughes6c1a3942011-08-17 15:00:06 -070069 IndirectRef iref0 = (IndirectRef) 0x11110;
70 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
71
72 // Add three, check, remove in the order in which they were added.
73 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -070074 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070075 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076 IndirectRef iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -070077 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079 IndirectRef iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -070080 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070081 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070082
83 EXPECT_EQ(obj0, irt.Get(iref0));
84 EXPECT_EQ(obj1, irt.Get(iref1));
85 EXPECT_EQ(obj2, irt.Get(iref2));
86
87 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070088 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070090 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070091 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070092 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070093
94 // Table should be empty now.
95 EXPECT_EQ(0U, irt.Capacity());
96
97 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -070098 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070099
100 // Add three, remove in the opposite order.
101 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700102 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700104 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700106 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700107 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700108
109 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700110 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700112 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700114 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700115
116 // Table should be empty now.
117 ASSERT_EQ(0U, irt.Capacity());
118
119 // Add three, remove middle / middle / bottom / top. (Second attempt
120 // to remove middle should fail.)
121 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700122 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700123 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700124 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700126 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700127 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700128
129 ASSERT_EQ(3U, irt.Capacity());
130
131 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700132 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700133 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700134 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700135
136 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700137 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138
139 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700140 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700142 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700143
144 // Table should be empty now.
145 ASSERT_EQ(0U, irt.Capacity());
146
147 // Add four entries. Remove #1, add new entry, verify that table size
148 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
149 // that we delete one and don't hole-compact the other.
150 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700151 EXPECT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700153 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700154 iref2 = irt.Add(cookie, obj2);
Ian Rogersc0542af2014-09-03 16:16:56 -0700155 EXPECT_TRUE(iref2 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156 IndirectRef iref3 = irt.Add(cookie, obj3);
Ian Rogersc0542af2014-09-03 16:16:56 -0700157 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700158 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700159
160 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700161 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700162
163 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700164 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165
166 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700167 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168
169 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700170 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700171 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700172 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700173
174 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
175
176 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700177 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700179 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700180
181 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
182
183 // Add an entry, remove it, add a new entry, and try to use the original
184 // iref. They have the same slot number but are for different objects.
185 // With the extended checks in place, this should fail.
186 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700187 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700188 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700189 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700190 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 iref1 = irt.Add(cookie, obj1);
Ian Rogersc0542af2014-09-03 16:16:56 -0700192 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700193 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700194 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700195 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700196 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
197 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700198 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700199
200 // Same as above, but with the same object. A more rigorous checker
201 // (e.g. with slot serialization) will catch this.
202 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700203 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700204 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700205 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700206 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 iref1 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700208 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700209 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210 if (iref0 != iref1) {
211 // Try 0, should not work.
212 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
213 }
214 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
215 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700216 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217
Ian Rogersc0542af2014-09-03 16:16:56 -0700218 // nullptr isn't a valid iref.
219 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220
221 // Stale lookup.
222 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700223 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700224 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700225 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700226 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700227 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228
229 // Test table resizing.
230 // These ones fit...
231 IndirectRef manyRefs[kTableInitial];
232 for (size_t i = 0; i < kTableInitial; i++) {
233 manyRefs[i] = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700234 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700235 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 }
237 // ...this one causes overflow.
238 iref0 = irt.Add(cookie, obj0);
Ian Rogersc0542af2014-09-03 16:16:56 -0700239 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700241 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242
243 for (size_t i = 0; i < kTableInitial; i++) {
244 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700245 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246 }
247 // Because of removal order, should have 11 entries, 10 of them holes.
248 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
249
250 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
251
252 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700253 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700254}
255
256} // namespace art