blob: 7b28f0bda898c5635209bac5120f2143d76f409a [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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070022#include "scoped_thread_state_change-inl.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)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070029 REQUIRES_SHARED(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 kTableMax = 20;
Andreas Gampea8e3b862016-10-17 20:12:52 -070052 IndirectReferenceTable irt(kTableMax, kGlobal);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070053
Ian Rogers98379392014-02-24 16:53:16 -080054 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070055 StackHandleScope<4> hs(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070056 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070057 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
58 ASSERT_TRUE(obj0.Get() != nullptr);
59 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
60 ASSERT_TRUE(obj1.Get() != nullptr);
61 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
62 ASSERT_TRUE(obj2.Get() != nullptr);
63 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
64 ASSERT_TRUE(obj3.Get() != 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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070074 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070075 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070076 CheckDump(&irt, 1, 1);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070077 IndirectRef iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070078 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070079 CheckDump(&irt, 2, 2);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070080 IndirectRef iref2 = irt.Add(cookie, obj2.Get());
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
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070084 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
85 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
86 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070087
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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700102 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700103 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700104 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700105 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700106 iref2 = irt.Add(cookie, obj2.Get());
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.)
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700122 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700123 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700124 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700125 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700126 iref2 = irt.Add(cookie, obj2.Get());
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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700151 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700152 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700153 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700155 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700156 EXPECT_TRUE(iref2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700157 IndirectRef iref3 = irt.Add(cookie, obj3.Get());
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
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700164 iref1 = irt.Add(cookie, obj1.Get());
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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700187 iref0 = irt.Add(cookie, obj0.Get());
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);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700192 iref1 = irt.Add(cookie, obj1.Get());
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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700203 iref0 = irt.Add(cookie, obj0.Get());
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);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700208 iref1 = irt.Add(cookie, obj0.Get());
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.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700223 iref0 = irt.Add(cookie, obj0.Get());
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...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700232 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233 IndirectRef manyRefs[kTableInitial];
234 for (size_t i = 0; i < kTableInitial; i++) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700235 manyRefs[i] = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700236 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700237 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700238 }
239 // ...this one causes overflow.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700240 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700241 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700243 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700244
245 for (size_t i = 0; i < kTableInitial; i++) {
246 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700247 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700248 }
249 // Because of removal order, should have 11 entries, 10 of them holes.
250 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
251
252 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
253
254 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700255 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700256}
257
258} // namespace art