blob: 5da7a30ecea2dcaf77462055ffec4770a1dde3a9 [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
Vladimir Marko17491ac2020-12-01 12:02:29 +0000109 // Check that the entry off the end of the list is not valid.
110 // (CheckJNI shall abort for such entries.)
111 EXPECT_FALSE(irt.IsValidReference(iref0, &error_msg));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112
113 // Add three, remove in the opposite order.
Andreas Gampe25651122017-09-25 14:50:23 -0700114 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700115 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700116 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700117 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700118 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700119 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700120 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700121
122 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700123 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700125 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700127 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700128
129 // Table should be empty now.
130 ASSERT_EQ(0U, irt.Capacity());
131
132 // Add three, remove middle / middle / bottom / top. (Second attempt
133 // to remove middle should fail.)
Andreas Gampe25651122017-09-25 14:50:23 -0700134 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700135 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700136 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700137 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700138 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700139 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700140 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141
142 ASSERT_EQ(3U, irt.Capacity());
143
144 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700145 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700147 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148
Vladimir Marko17491ac2020-12-01 12:02:29 +0000149 // Check that the reference to the hole is not valid.
150 EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg));
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700151
152 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700153 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700154 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700155 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156
157 // Table should be empty now.
158 ASSERT_EQ(0U, irt.Capacity());
159
160 // Add four entries. Remove #1, add new entry, verify that table size
161 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
162 // that we delete one and don't hole-compact the other.
Andreas Gampe25651122017-09-25 14:50:23 -0700163 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700164 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700165 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700166 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700167 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700168 EXPECT_TRUE(iref2 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700169 IndirectRef iref3 = irt.Add(cookie, obj3.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700170 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700171 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700172
173 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700174 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175
Andreas Gampe25651122017-09-25 14:50:23 -0700176 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700177 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178
179 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700180 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181
182 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700183 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700184 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700185 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700186
187 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
188
189 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700190 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700192 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193
194 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
195
196 // Add an entry, remove it, add a new entry, and try to use the original
197 // iref. They have the same slot number but are for different objects.
198 // With the extended checks in place, this should fail.
Andreas Gampe25651122017-09-25 14:50:23 -0700199 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700200 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700203 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700204 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700205 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700206 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700208 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
210 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700211 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700212
213 // Same as above, but with the same object. A more rigorous checker
214 // (e.g. with slot serialization) will catch this.
Andreas Gampe25651122017-09-25 14:50:23 -0700215 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700216 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700217 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700218 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700219 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700220 iref1 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700221 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700222 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700223 if (iref0 != iref1) {
224 // Try 0, should not work.
225 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
226 }
227 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
228 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700229 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700230
Vladimir Marko17491ac2020-12-01 12:02:29 +0000231 // Stale reference is not valid.
Andreas Gampe25651122017-09-25 14:50:23 -0700232 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700233 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700234 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235 ASSERT_TRUE(irt.Remove(cookie, iref0));
Vladimir Marko17491ac2020-12-01 12:02:29 +0000236 EXPECT_FALSE(irt.IsValidReference(iref0, &error_msg)) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700237 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700238
239 // Test table resizing.
240 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700241 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 IndirectRef manyRefs[kTableInitial];
243 for (size_t i = 0; i < kTableInitial; i++) {
Andreas Gampe25651122017-09-25 14:50:23 -0700244 manyRefs[i] = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700245 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700246 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 }
248 // ...this one causes overflow.
Andreas Gampe25651122017-09-25 14:50:23 -0700249 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700250 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700252 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253
254 for (size_t i = 0; i < kTableInitial; i++) {
255 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700256 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700257 }
258 // Because of removal order, should have 11 entries, 10 of them holes.
259 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
260
261 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
262
263 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700264 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700265}
266
Andreas Gampee03662b2016-10-13 17:12:56 -0700267TEST_F(IndirectReferenceTableTest, Holes) {
268 // Test the explicitly named cases from the IRT implementation:
269 //
270 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
271 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
272 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
273 // reference
274 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
275 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
276 // reference
277
278 ScopedObjectAccess soa(Thread::Current());
279 static const size_t kTableMax = 10;
280
Vladimir Markoe9987b02018-05-22 16:26:43 +0100281 StackHandleScope<6> hs(soa.Self());
282 Handle<mirror::Class> c = hs.NewHandle(
283 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampee03662b2016-10-13 17:12:56 -0700284 ASSERT_TRUE(c != nullptr);
285 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800286 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700287 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800288 ASSERT_TRUE(obj1 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700289 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800290 ASSERT_TRUE(obj2 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700291 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800292 ASSERT_TRUE(obj3 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700293 Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800294 ASSERT_TRUE(obj4 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700295
296 std::string error_msg;
297
298 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference.
299 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700300 IndirectReferenceTable irt(kTableMax,
301 kGlobal,
302 IndirectReferenceTable::ResizableCapacity::kNo,
303 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700304 ASSERT_TRUE(irt.IsValid()) << error_msg;
305
306 const IRTSegmentState cookie0 = kIRTFirstSegment;
307
308 CheckDump(&irt, 0, 0);
309
Andreas Gampe25651122017-09-25 14:50:23 -0700310 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
311 IndirectRef iref1 = irt.Add(cookie0, obj1.Get(), &error_msg);
312 IndirectRef iref2 = irt.Add(cookie0, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700313
314 EXPECT_TRUE(irt.Remove(cookie0, iref1));
315
316 // New segment.
317 const IRTSegmentState cookie1 = irt.GetSegmentState();
318
Andreas Gampe25651122017-09-25 14:50:23 -0700319 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700320
321 // Must not have filled the previous hole.
322 EXPECT_EQ(irt.Capacity(), 4u);
Vladimir Marko17491ac2020-12-01 12:02:29 +0000323 EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg));
Andreas Gampee03662b2016-10-13 17:12:56 -0700324 CheckDump(&irt, 3, 3);
325
326 UNUSED(iref0, iref1, iref2, iref3);
327 }
328
329 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
330 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700331 IndirectReferenceTable irt(kTableMax,
332 kGlobal,
333 IndirectReferenceTable::ResizableCapacity::kNo,
334 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700335 ASSERT_TRUE(irt.IsValid()) << error_msg;
336
337 const IRTSegmentState cookie0 = kIRTFirstSegment;
338
339 CheckDump(&irt, 0, 0);
340
Andreas Gampe25651122017-09-25 14:50:23 -0700341 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700342
343 // New segment.
344 const IRTSegmentState cookie1 = irt.GetSegmentState();
345
Andreas Gampe25651122017-09-25 14:50:23 -0700346 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
347 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
348 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700349
350 EXPECT_TRUE(irt.Remove(cookie1, iref2));
351
352 // Pop segment.
353 irt.SetSegmentState(cookie1);
354
Andreas Gampe25651122017-09-25 14:50:23 -0700355 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700356
357 EXPECT_EQ(irt.Capacity(), 2u);
Vladimir Marko17491ac2020-12-01 12:02:29 +0000358 EXPECT_FALSE(irt.IsValidReference(iref2, &error_msg));
Andreas Gampee03662b2016-10-13 17:12:56 -0700359 CheckDump(&irt, 2, 2);
360
361 UNUSED(iref0, iref1, iref2, iref3, iref4);
362 }
363
364 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
365 // reference.
366 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700367 IndirectReferenceTable irt(kTableMax,
368 kGlobal,
369 IndirectReferenceTable::ResizableCapacity::kNo,
370 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700371 ASSERT_TRUE(irt.IsValid()) << error_msg;
372
373 const IRTSegmentState cookie0 = kIRTFirstSegment;
374
375 CheckDump(&irt, 0, 0);
376
Andreas Gampe25651122017-09-25 14:50:23 -0700377 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700378
379 // New segment.
380 const IRTSegmentState cookie1 = irt.GetSegmentState();
381
Andreas Gampe25651122017-09-25 14:50:23 -0700382 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
383 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700384
385 EXPECT_TRUE(irt.Remove(cookie1, iref1));
386
387 // New segment.
388 const IRTSegmentState cookie2 = irt.GetSegmentState();
389
Andreas Gampe25651122017-09-25 14:50:23 -0700390 IndirectRef iref3 = irt.Add(cookie2, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700391
392 // Pop segment.
393 irt.SetSegmentState(cookie2);
394
Andreas Gampe25651122017-09-25 14:50:23 -0700395 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700396
397 EXPECT_EQ(irt.Capacity(), 3u);
Vladimir Marko17491ac2020-12-01 12:02:29 +0000398 EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg));
Andreas Gampee03662b2016-10-13 17:12:56 -0700399 CheckDump(&irt, 3, 3);
400
401 UNUSED(iref0, iref1, iref2, iref3, iref4);
402 }
403
404 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference.
405 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700406 IndirectReferenceTable irt(kTableMax,
407 kGlobal,
408 IndirectReferenceTable::ResizableCapacity::kNo,
409 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700410 ASSERT_TRUE(irt.IsValid()) << error_msg;
411
412 const IRTSegmentState cookie0 = kIRTFirstSegment;
413
414 CheckDump(&irt, 0, 0);
415
Andreas Gampe25651122017-09-25 14:50:23 -0700416 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700417
418 // New segment.
419 const IRTSegmentState cookie1 = irt.GetSegmentState();
420
Andreas Gampe25651122017-09-25 14:50:23 -0700421 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700422 EXPECT_TRUE(irt.Remove(cookie1, iref1));
423
424 // Emptied segment, push new one.
425 const IRTSegmentState cookie2 = irt.GetSegmentState();
426
Andreas Gampe25651122017-09-25 14:50:23 -0700427 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
428 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
429 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700430
431 EXPECT_TRUE(irt.Remove(cookie1, iref3));
432
433 // Pop segment.
434 UNUSED(cookie2);
435 irt.SetSegmentState(cookie1);
436
Andreas Gampe25651122017-09-25 14:50:23 -0700437 IndirectRef iref5 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700438
439 EXPECT_EQ(irt.Capacity(), 2u);
Vladimir Marko17491ac2020-12-01 12:02:29 +0000440 EXPECT_FALSE(irt.IsValidReference(iref3, &error_msg));
Andreas Gampee03662b2016-10-13 17:12:56 -0700441 CheckDump(&irt, 2, 2);
442
443 UNUSED(iref0, iref1, iref2, iref3, iref4, iref5);
444 }
445
446 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
447 // reference
448 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700449 IndirectReferenceTable irt(kTableMax,
450 kGlobal,
451 IndirectReferenceTable::ResizableCapacity::kNo,
452 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700453 ASSERT_TRUE(irt.IsValid()) << error_msg;
454
455 const IRTSegmentState cookie0 = kIRTFirstSegment;
456
457 CheckDump(&irt, 0, 0);
458
Andreas Gampe25651122017-09-25 14:50:23 -0700459 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700460
461 // New segment.
462 const IRTSegmentState cookie1 = irt.GetSegmentState();
463
Andreas Gampe25651122017-09-25 14:50:23 -0700464 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
465 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
466 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700467
468 EXPECT_TRUE(irt.Remove(cookie1, iref2));
469
470 // Pop segment.
471 irt.SetSegmentState(cookie1);
472
473 // Push segment.
474 const IRTSegmentState cookie1_second = irt.GetSegmentState();
475 UNUSED(cookie1_second);
476
Andreas Gampe25651122017-09-25 14:50:23 -0700477 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700478
479 EXPECT_EQ(irt.Capacity(), 2u);
Vladimir Marko17491ac2020-12-01 12:02:29 +0000480 EXPECT_FALSE(irt.IsValidReference(iref3, &error_msg));
Andreas Gampee03662b2016-10-13 17:12:56 -0700481 CheckDump(&irt, 2, 2);
482
483 UNUSED(iref0, iref1, iref2, iref3, iref4);
484 }
485}
486
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700487TEST_F(IndirectReferenceTableTest, Resize) {
488 ScopedObjectAccess soa(Thread::Current());
489 static const size_t kTableMax = 512;
490
Vladimir Markoe9987b02018-05-22 16:26:43 +0100491 StackHandleScope<2> hs(soa.Self());
492 Handle<mirror::Class> c = hs.NewHandle(
493 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700494 ASSERT_TRUE(c != nullptr);
495 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800496 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700497
498 std::string error_msg;
499 IndirectReferenceTable irt(kTableMax,
500 kLocal,
501 IndirectReferenceTable::ResizableCapacity::kYes,
502 &error_msg);
503 ASSERT_TRUE(irt.IsValid()) << error_msg;
504
505 CheckDump(&irt, 0, 0);
506 const IRTSegmentState cookie = kIRTFirstSegment;
507
508 for (size_t i = 0; i != kTableMax + 1; ++i) {
Andreas Gampe25651122017-09-25 14:50:23 -0700509 irt.Add(cookie, obj0.Get(), &error_msg);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700510 }
511
512 EXPECT_EQ(irt.Capacity(), kTableMax + 1);
513}
514
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700515} // namespace art