blob: d7026de5593bb9ef865bed607f2d04317526c158 [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;
Richard Uhlerda0a69e2016-10-11 15:06:38 +010052 std::string error_msg;
53 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
54 ASSERT_TRUE(irt.IsValid()) << error_msg;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070055
Ian Rogers98379392014-02-24 16:53:16 -080056 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070057 StackHandleScope<4> hs(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070058 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070059 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
60 ASSERT_TRUE(obj0.Get() != nullptr);
61 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
62 ASSERT_TRUE(obj1.Get() != nullptr);
63 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
64 ASSERT_TRUE(obj2.Get() != nullptr);
65 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
66 ASSERT_TRUE(obj3.Get() != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067
68 const uint32_t cookie = IRT_FIRST_SEGMENT;
69
Ian Rogers63818dc2012-09-26 12:23:04 -070070 CheckDump(&irt, 0, 0);
71
Elliott Hughes6c1a3942011-08-17 15:00:06 -070072 IndirectRef iref0 = (IndirectRef) 0x11110;
73 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
74
75 // Add three, check, remove in the order in which they were added.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070076 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070077 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 1, 1);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070079 IndirectRef iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070080 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070081 CheckDump(&irt, 2, 2);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070082 IndirectRef iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070083 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070084 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070086 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
87 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
88 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089
90 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070091 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070093 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070095 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096
97 // Table should be empty now.
98 EXPECT_EQ(0U, irt.Capacity());
99
100 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -0700101 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102
103 // Add three, remove in the opposite order.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700104 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700105 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700106 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700107 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700108 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700109 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700110 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111
112 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700113 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700114 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700115 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700117 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700118
119 // Table should be empty now.
120 ASSERT_EQ(0U, irt.Capacity());
121
122 // Add three, remove middle / middle / bottom / top. (Second attempt
123 // to remove middle should fail.)
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700124 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700125 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700126 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700127 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700128 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700129 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700130 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131
132 ASSERT_EQ(3U, irt.Capacity());
133
134 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700135 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700137 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138
139 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700140 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141
142 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700143 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700145 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146
147 // Table should be empty now.
148 ASSERT_EQ(0U, irt.Capacity());
149
150 // Add four entries. Remove #1, add new entry, verify that table size
151 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
152 // that we delete one and don't hole-compact the other.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700153 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700155 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700156 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700157 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700158 EXPECT_TRUE(iref2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700159 IndirectRef iref3 = irt.Add(cookie, obj3.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700160 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700161 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700162
163 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700164 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700166 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700167 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168
169 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700170 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700171
172 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700173 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700174 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700175 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700176
177 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
178
179 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700180 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700182 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183
184 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
185
186 // Add an entry, remove it, add a new entry, and try to use the original
187 // iref. They have the same slot number but are for different objects.
188 // With the extended checks in place, this should fail.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700189 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700190 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700191 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700193 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700194 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700195 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700196 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700198 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700199 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
200 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202
203 // Same as above, but with the same object. A more rigorous checker
204 // (e.g. with slot serialization) will catch this.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700205 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700206 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700207 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700209 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700210 iref1 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700211 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700212 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700213 if (iref0 != iref1) {
214 // Try 0, should not work.
215 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
216 }
217 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
218 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700219 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700221 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700222 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700223
224 // Stale lookup.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700225 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700226 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700227 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700229 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700230 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231
232 // Test table resizing.
233 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700234 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235 IndirectRef manyRefs[kTableInitial];
236 for (size_t i = 0; i < kTableInitial; i++) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700237 manyRefs[i] = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700238 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700239 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 }
241 // ...this one causes overflow.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700242 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700243 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700244 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700245 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246
247 for (size_t i = 0; i < kTableInitial; i++) {
248 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700249 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700250 }
251 // Because of removal order, should have 11 entries, 10 of them holes.
252 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
253
254 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
255
256 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700257 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700258}
259
260} // namespace art