blob: c5ae4c63f72795be52bc53bb2f52c696573b95fe [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_runtime_test.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070023#include "mirror/class-alloc-inl.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026
Elliott Hughes6c1a3942011-08-17 15:00:06 -070027namespace art {
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029using android::base::StringPrintf;
30
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070032
Ian Rogers63818dc2012-09-26 12:23:04 -070033static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070034 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers63818dc2012-09-26 12:23:04 -070035 std::ostringstream oss;
36 irt->Dump(oss);
37 if (num_objects == 0) {
38 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
39 } else if (num_objects == 1) {
40 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
41 } else {
42 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
43 num_objects, num_unique)),
44 std::string::npos)
45 << "\n Expected number of objects: " << num_objects
46 << "\n Expected unique objects: " << num_unique << "\n"
47 << oss.str();
48 }
49}
50
Elliott Hughes6c1a3942011-08-17 15:00:06 -070051TEST_F(IndirectReferenceTableTest, BasicTest) {
Andreas Gampe369810a2015-01-14 19:53:31 -080052 // This will lead to error messages in the log.
53 ScopedLogSeverity sls(LogSeverity::FATAL);
54
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070056 static const size_t kTableMax = 20;
Richard Uhlerda0a69e2016-10-11 15:06:38 +010057 std::string error_msg;
Andreas Gampe9d7ef622016-10-24 19:35:19 -070058 IndirectReferenceTable irt(kTableMax,
59 kGlobal,
60 IndirectReferenceTable::ResizableCapacity::kNo,
61 &error_msg);
Richard Uhlerda0a69e2016-10-11 15:06:38 +010062 ASSERT_TRUE(irt.IsValid()) << error_msg;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070063
Vladimir Markoe9987b02018-05-22 16:26:43 +010064 StackHandleScope<5> hs(soa.Self());
65 Handle<mirror::Class> c =
66 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Ian Rogersc0542af2014-09-03 16:16:56 -070067 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070068 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080069 ASSERT_TRUE(obj0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070070 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080071 ASSERT_TRUE(obj1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070072 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080073 ASSERT_TRUE(obj2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070074 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080075 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076
Andreas Gampee03662b2016-10-13 17:12:56 -070077 const IRTSegmentState cookie = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070078
Ian Rogers63818dc2012-09-26 12:23:04 -070079 CheckDump(&irt, 0, 0);
80
Elliott Hughes6c1a3942011-08-17 15:00:06 -070081 IndirectRef iref0 = (IndirectRef) 0x11110;
82 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
83
84 // Add three, check, remove in the order in which they were added.
Andreas Gampe25651122017-09-25 14:50:23 -070085 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070086 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070087 CheckDump(&irt, 1, 1);
Andreas Gampe25651122017-09-25 14:50:23 -070088 IndirectRef iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070089 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070090 CheckDump(&irt, 2, 2);
Andreas Gampe25651122017-09-25 14:50:23 -070091 IndirectRef iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070092 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070093 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070095 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
96 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
97 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070098
99 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700100 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700101 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700102 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700104 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105
106 // Table should be empty now.
107 EXPECT_EQ(0U, irt.Capacity());
108
109 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -0700110 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111
112 // Add three, remove in the opposite order.
Andreas Gampe25651122017-09-25 14:50:23 -0700113 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700114 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700115 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700116 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700117 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700118 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700119 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700120
121 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700122 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700123 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700124 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700126 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700127
128 // Table should be empty now.
129 ASSERT_EQ(0U, irt.Capacity());
130
131 // Add three, remove middle / middle / bottom / top. (Second attempt
132 // to remove middle should fail.)
Andreas Gampe25651122017-09-25 14:50:23 -0700133 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700134 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700135 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700136 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700137 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700138 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700139 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140
141 ASSERT_EQ(3U, irt.Capacity());
142
143 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700144 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700145 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700146 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700147
148 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700149 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700150
151 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700152 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700154 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700155
156 // Table should be empty now.
157 ASSERT_EQ(0U, irt.Capacity());
158
159 // Add four entries. Remove #1, add new entry, verify that table size
160 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
161 // that we delete one and don't hole-compact the other.
Andreas Gampe25651122017-09-25 14:50:23 -0700162 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700163 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700164 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700165 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700166 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700167 EXPECT_TRUE(iref2 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700168 IndirectRef iref3 = irt.Add(cookie, obj3.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700169 EXPECT_TRUE(iref3 != nullptr);
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
Andreas Gampe25651122017-09-25 14:50:23 -0700175 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700176 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700177
178 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700179 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700180
181 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700182 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700184 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700185
186 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
187
188 ASSERT_TRUE(irt.Remove(cookie, iref2));
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
193 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
194
195 // Add an entry, remove it, add a new entry, and try to use the original
196 // iref. They have the same slot number but are for different objects.
197 // With the extended checks in place, this should fail.
Andreas Gampe25651122017-09-25 14:50:23 -0700198 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700199 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700200 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700201 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700202 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700203 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700204 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700205 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
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, iref1)) << "switched del failed";
209 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700210 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700211
212 // Same as above, but with the same object. A more rigorous checker
213 // (e.g. with slot serialization) will catch this.
Andreas Gampe25651122017-09-25 14:50:23 -0700214 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700215 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700216 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700217 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700218 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700219 iref1 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700220 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700221 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 if (iref0 != iref1) {
223 // Try 0, should not work.
224 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
225 }
226 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
227 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700228 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700229
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700230 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700231 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700232
233 // Stale lookup.
Andreas Gampe25651122017-09-25 14:50:23 -0700234 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700235 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700236 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700238 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700239 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240
241 // Test table resizing.
242 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700243 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700244 IndirectRef manyRefs[kTableInitial];
245 for (size_t i = 0; i < kTableInitial; i++) {
Andreas Gampe25651122017-09-25 14:50:23 -0700246 manyRefs[i] = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700247 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700248 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700249 }
250 // ...this one causes overflow.
Andreas Gampe25651122017-09-25 14:50:23 -0700251 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700252 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700254 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700255
256 for (size_t i = 0; i < kTableInitial; i++) {
257 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700258 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700259 }
260 // Because of removal order, should have 11 entries, 10 of them holes.
261 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
262
263 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
264
265 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700266 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700267}
268
Andreas Gampee03662b2016-10-13 17:12:56 -0700269TEST_F(IndirectReferenceTableTest, Holes) {
270 // Test the explicitly named cases from the IRT implementation:
271 //
272 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
273 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
274 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
275 // reference
276 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
277 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
278 // reference
279
280 ScopedObjectAccess soa(Thread::Current());
281 static const size_t kTableMax = 10;
282
Vladimir Markoe9987b02018-05-22 16:26:43 +0100283 StackHandleScope<6> hs(soa.Self());
284 Handle<mirror::Class> c = hs.NewHandle(
285 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampee03662b2016-10-13 17:12:56 -0700286 ASSERT_TRUE(c != nullptr);
287 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800288 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700289 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800290 ASSERT_TRUE(obj1 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700291 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800292 ASSERT_TRUE(obj2 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700293 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800294 ASSERT_TRUE(obj3 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700295 Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800296 ASSERT_TRUE(obj4 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700297
298 std::string error_msg;
299
300 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference.
301 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700302 IndirectReferenceTable irt(kTableMax,
303 kGlobal,
304 IndirectReferenceTable::ResizableCapacity::kNo,
305 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700306 ASSERT_TRUE(irt.IsValid()) << error_msg;
307
308 const IRTSegmentState cookie0 = kIRTFirstSegment;
309
310 CheckDump(&irt, 0, 0);
311
Andreas Gampe25651122017-09-25 14:50:23 -0700312 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
313 IndirectRef iref1 = irt.Add(cookie0, obj1.Get(), &error_msg);
314 IndirectRef iref2 = irt.Add(cookie0, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700315
316 EXPECT_TRUE(irt.Remove(cookie0, iref1));
317
318 // New segment.
319 const IRTSegmentState cookie1 = irt.GetSegmentState();
320
Andreas Gampe25651122017-09-25 14:50:23 -0700321 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700322
323 // Must not have filled the previous hole.
324 EXPECT_EQ(irt.Capacity(), 4u);
325 EXPECT_TRUE(irt.Get(iref1) == nullptr);
326 CheckDump(&irt, 3, 3);
327
328 UNUSED(iref0, iref1, iref2, iref3);
329 }
330
331 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
332 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700333 IndirectReferenceTable irt(kTableMax,
334 kGlobal,
335 IndirectReferenceTable::ResizableCapacity::kNo,
336 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700337 ASSERT_TRUE(irt.IsValid()) << error_msg;
338
339 const IRTSegmentState cookie0 = kIRTFirstSegment;
340
341 CheckDump(&irt, 0, 0);
342
Andreas Gampe25651122017-09-25 14:50:23 -0700343 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700344
345 // New segment.
346 const IRTSegmentState cookie1 = irt.GetSegmentState();
347
Andreas Gampe25651122017-09-25 14:50:23 -0700348 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
349 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
350 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700351
352 EXPECT_TRUE(irt.Remove(cookie1, iref2));
353
354 // Pop segment.
355 irt.SetSegmentState(cookie1);
356
Andreas Gampe25651122017-09-25 14:50:23 -0700357 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700358
359 EXPECT_EQ(irt.Capacity(), 2u);
360 EXPECT_TRUE(irt.Get(iref2) == nullptr);
361 CheckDump(&irt, 2, 2);
362
363 UNUSED(iref0, iref1, iref2, iref3, iref4);
364 }
365
366 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
367 // reference.
368 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700369 IndirectReferenceTable irt(kTableMax,
370 kGlobal,
371 IndirectReferenceTable::ResizableCapacity::kNo,
372 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700373 ASSERT_TRUE(irt.IsValid()) << error_msg;
374
375 const IRTSegmentState cookie0 = kIRTFirstSegment;
376
377 CheckDump(&irt, 0, 0);
378
Andreas Gampe25651122017-09-25 14:50:23 -0700379 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700380
381 // New segment.
382 const IRTSegmentState cookie1 = irt.GetSegmentState();
383
Andreas Gampe25651122017-09-25 14:50:23 -0700384 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
385 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700386
387 EXPECT_TRUE(irt.Remove(cookie1, iref1));
388
389 // New segment.
390 const IRTSegmentState cookie2 = irt.GetSegmentState();
391
Andreas Gampe25651122017-09-25 14:50:23 -0700392 IndirectRef iref3 = irt.Add(cookie2, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700393
394 // Pop segment.
395 irt.SetSegmentState(cookie2);
396
Andreas Gampe25651122017-09-25 14:50:23 -0700397 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700398
399 EXPECT_EQ(irt.Capacity(), 3u);
400 EXPECT_TRUE(irt.Get(iref1) == nullptr);
401 CheckDump(&irt, 3, 3);
402
403 UNUSED(iref0, iref1, iref2, iref3, iref4);
404 }
405
406 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference.
407 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700408 IndirectReferenceTable irt(kTableMax,
409 kGlobal,
410 IndirectReferenceTable::ResizableCapacity::kNo,
411 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700412 ASSERT_TRUE(irt.IsValid()) << error_msg;
413
414 const IRTSegmentState cookie0 = kIRTFirstSegment;
415
416 CheckDump(&irt, 0, 0);
417
Andreas Gampe25651122017-09-25 14:50:23 -0700418 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700419
420 // New segment.
421 const IRTSegmentState cookie1 = irt.GetSegmentState();
422
Andreas Gampe25651122017-09-25 14:50:23 -0700423 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700424 EXPECT_TRUE(irt.Remove(cookie1, iref1));
425
426 // Emptied segment, push new one.
427 const IRTSegmentState cookie2 = irt.GetSegmentState();
428
Andreas Gampe25651122017-09-25 14:50:23 -0700429 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
430 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
431 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700432
433 EXPECT_TRUE(irt.Remove(cookie1, iref3));
434
435 // Pop segment.
436 UNUSED(cookie2);
437 irt.SetSegmentState(cookie1);
438
Andreas Gampe25651122017-09-25 14:50:23 -0700439 IndirectRef iref5 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700440
441 EXPECT_EQ(irt.Capacity(), 2u);
442 EXPECT_TRUE(irt.Get(iref3) == nullptr);
443 CheckDump(&irt, 2, 2);
444
445 UNUSED(iref0, iref1, iref2, iref3, iref4, iref5);
446 }
447
448 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
449 // reference
450 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700451 IndirectReferenceTable irt(kTableMax,
452 kGlobal,
453 IndirectReferenceTable::ResizableCapacity::kNo,
454 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700455 ASSERT_TRUE(irt.IsValid()) << error_msg;
456
457 const IRTSegmentState cookie0 = kIRTFirstSegment;
458
459 CheckDump(&irt, 0, 0);
460
Andreas Gampe25651122017-09-25 14:50:23 -0700461 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700462
463 // New segment.
464 const IRTSegmentState cookie1 = irt.GetSegmentState();
465
Andreas Gampe25651122017-09-25 14:50:23 -0700466 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
467 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
468 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700469
470 EXPECT_TRUE(irt.Remove(cookie1, iref2));
471
472 // Pop segment.
473 irt.SetSegmentState(cookie1);
474
475 // Push segment.
476 const IRTSegmentState cookie1_second = irt.GetSegmentState();
477 UNUSED(cookie1_second);
478
Andreas Gampe25651122017-09-25 14:50:23 -0700479 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700480
481 EXPECT_EQ(irt.Capacity(), 2u);
482 EXPECT_TRUE(irt.Get(iref3) == nullptr);
483 CheckDump(&irt, 2, 2);
484
485 UNUSED(iref0, iref1, iref2, iref3, iref4);
486 }
487}
488
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700489TEST_F(IndirectReferenceTableTest, Resize) {
490 ScopedObjectAccess soa(Thread::Current());
491 static const size_t kTableMax = 512;
492
Vladimir Markoe9987b02018-05-22 16:26:43 +0100493 StackHandleScope<2> hs(soa.Self());
494 Handle<mirror::Class> c = hs.NewHandle(
495 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700496 ASSERT_TRUE(c != nullptr);
497 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800498 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700499
500 std::string error_msg;
501 IndirectReferenceTable irt(kTableMax,
502 kLocal,
503 IndirectReferenceTable::ResizableCapacity::kYes,
504 &error_msg);
505 ASSERT_TRUE(irt.IsValid()) << error_msg;
506
507 CheckDump(&irt, 0, 0);
508 const IRTSegmentState cookie = kIRTFirstSegment;
509
510 for (size_t i = 0; i != kTableMax + 1; ++i) {
Andreas Gampe25651122017-09-25 14:50:23 -0700511 irt.Add(cookie, obj0.Get(), &error_msg);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700512 }
513
514 EXPECT_EQ(irt.Capacity(), kTableMax + 1);
515}
516
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700517} // namespace art