blob: 141feb434fc565201c35be5593afe869f8192588 [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"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070023#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026namespace art {
27
Andreas Gampe46ee31b2016-12-14 10:11:49 -080028using android::base::StringPrintf;
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070031
Ian Rogers63818dc2012-09-26 12:23:04 -070032static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070033 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers63818dc2012-09-26 12:23:04 -070034 std::ostringstream oss;
35 irt->Dump(oss);
36 if (num_objects == 0) {
37 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
38 } else if (num_objects == 1) {
39 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
40 } else {
41 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
42 num_objects, num_unique)),
43 std::string::npos)
44 << "\n Expected number of objects: " << num_objects
45 << "\n Expected unique objects: " << num_unique << "\n"
46 << oss.str();
47 }
48}
49
Elliott Hughes6c1a3942011-08-17 15:00:06 -070050TEST_F(IndirectReferenceTableTest, BasicTest) {
Andreas Gampe369810a2015-01-14 19:53:31 -080051 // This will lead to error messages in the log.
52 ScopedLogSeverity sls(LogSeverity::FATAL);
53
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070055 static const size_t kTableMax = 20;
Richard Uhlerda0a69e2016-10-11 15:06:38 +010056 std::string error_msg;
Andreas Gampe9d7ef622016-10-24 19:35:19 -070057 IndirectReferenceTable irt(kTableMax,
58 kGlobal,
59 IndirectReferenceTable::ResizableCapacity::kNo,
60 &error_msg);
Richard Uhlerda0a69e2016-10-11 15:06:38 +010061 ASSERT_TRUE(irt.IsValid()) << error_msg;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070062
Vladimir Markoe9987b02018-05-22 16:26:43 +010063 StackHandleScope<5> hs(soa.Self());
64 Handle<mirror::Class> c =
65 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Ian Rogersc0542af2014-09-03 16:16:56 -070066 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070067 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080068 ASSERT_TRUE(obj0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070069 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080070 ASSERT_TRUE(obj1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070071 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080072 ASSERT_TRUE(obj2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070073 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080074 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070075
Andreas Gampee03662b2016-10-13 17:12:56 -070076 const IRTSegmentState cookie = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070077
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 0, 0);
79
Elliott Hughes6c1a3942011-08-17 15:00:06 -070080 IndirectRef iref0 = (IndirectRef) 0x11110;
81 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
82
83 // Add three, check, remove in the order in which they were added.
Andreas Gampe25651122017-09-25 14:50:23 -070084 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070085 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070086 CheckDump(&irt, 1, 1);
Andreas Gampe25651122017-09-25 14:50:23 -070087 IndirectRef iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070088 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070089 CheckDump(&irt, 2, 2);
Andreas Gampe25651122017-09-25 14:50:23 -070090 IndirectRef iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -070091 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070092 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070093
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070094 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
95 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
96 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070097
98 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070099 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700100 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700101 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700103 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700104
105 // Table should be empty now.
106 EXPECT_EQ(0U, irt.Capacity());
107
108 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -0700109 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110
111 // Add three, remove in the opposite order.
Andreas Gampe25651122017-09-25 14:50:23 -0700112 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700113 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700114 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700115 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700116 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700117 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700118 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700119
120 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700121 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700122 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700123 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700125 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126
127 // Table should be empty now.
128 ASSERT_EQ(0U, irt.Capacity());
129
130 // Add three, remove middle / middle / bottom / top. (Second attempt
131 // to remove middle should fail.)
Andreas Gampe25651122017-09-25 14:50:23 -0700132 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700133 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700134 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700135 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700136 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700137 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700138 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700139
140 ASSERT_EQ(3U, irt.Capacity());
141
142 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700143 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700145 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146
147 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700148 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700149
150 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700151 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700152 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700153 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700154
155 // Table should be empty now.
156 ASSERT_EQ(0U, irt.Capacity());
157
158 // Add four entries. Remove #1, add new entry, verify that table size
159 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
160 // that we delete one and don't hole-compact the other.
Andreas Gampe25651122017-09-25 14:50:23 -0700161 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700162 EXPECT_TRUE(iref0 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700163 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700164 EXPECT_TRUE(iref1 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700165 iref2 = irt.Add(cookie, obj2.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700166 EXPECT_TRUE(iref2 != nullptr);
Andreas Gampe25651122017-09-25 14:50:23 -0700167 IndirectRef iref3 = irt.Add(cookie, obj3.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700168 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700169 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170
171 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700172 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700173
Andreas Gampe25651122017-09-25 14:50:23 -0700174 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700175 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700176
177 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700178 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700179
180 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700181 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700182 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700183 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700184
185 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
186
187 ASSERT_TRUE(irt.Remove(cookie, iref2));
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
192 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
193
194 // Add an entry, remove it, add a new entry, and try to use the original
195 // iref. They have the same slot number but are for different objects.
196 // With the extended checks in place, this should fail.
Andreas Gampe25651122017-09-25 14:50:23 -0700197 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700198 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700199 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700200 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700202 iref1 = irt.Add(cookie, obj1.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700203 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700204 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700205 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700206 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
208 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700209 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700210
211 // Same as above, but with the same object. A more rigorous checker
212 // (e.g. with slot serialization) will catch this.
Andreas Gampe25651122017-09-25 14:50:23 -0700213 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700214 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700215 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700216 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700217 CheckDump(&irt, 0, 0);
Andreas Gampe25651122017-09-25 14:50:23 -0700218 iref1 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700219 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700220 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700221 if (iref0 != iref1) {
222 // Try 0, should not work.
223 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
224 }
225 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
226 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700227 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700229 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700230 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231
232 // Stale lookup.
Andreas Gampe25651122017-09-25 14:50:23 -0700233 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700234 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700235 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700236 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700237 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700238 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700239
240 // Test table resizing.
241 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700242 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243 IndirectRef manyRefs[kTableInitial];
244 for (size_t i = 0; i < kTableInitial; i++) {
Andreas Gampe25651122017-09-25 14:50:23 -0700245 manyRefs[i] = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700246 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700247 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700248 }
249 // ...this one causes overflow.
Andreas Gampe25651122017-09-25 14:50:23 -0700250 iref0 = irt.Add(cookie, obj0.Get(), &error_msg);
Ian Rogersc0542af2014-09-03 16:16:56 -0700251 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700252 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700253 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700254
255 for (size_t i = 0; i < kTableInitial; i++) {
256 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700257 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700258 }
259 // Because of removal order, should have 11 entries, 10 of them holes.
260 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
261
262 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
263
264 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700265 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700266}
267
Andreas Gampee03662b2016-10-13 17:12:56 -0700268TEST_F(IndirectReferenceTableTest, Holes) {
269 // Test the explicitly named cases from the IRT implementation:
270 //
271 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
272 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
273 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
274 // reference
275 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
276 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
277 // reference
278
279 ScopedObjectAccess soa(Thread::Current());
280 static const size_t kTableMax = 10;
281
Vladimir Markoe9987b02018-05-22 16:26:43 +0100282 StackHandleScope<6> hs(soa.Self());
283 Handle<mirror::Class> c = hs.NewHandle(
284 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampee03662b2016-10-13 17:12:56 -0700285 ASSERT_TRUE(c != nullptr);
286 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800287 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700288 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800289 ASSERT_TRUE(obj1 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700290 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800291 ASSERT_TRUE(obj2 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700292 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800293 ASSERT_TRUE(obj3 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700294 Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800295 ASSERT_TRUE(obj4 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700296
297 std::string error_msg;
298
299 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference.
300 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700301 IndirectReferenceTable irt(kTableMax,
302 kGlobal,
303 IndirectReferenceTable::ResizableCapacity::kNo,
304 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700305 ASSERT_TRUE(irt.IsValid()) << error_msg;
306
307 const IRTSegmentState cookie0 = kIRTFirstSegment;
308
309 CheckDump(&irt, 0, 0);
310
Andreas Gampe25651122017-09-25 14:50:23 -0700311 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
312 IndirectRef iref1 = irt.Add(cookie0, obj1.Get(), &error_msg);
313 IndirectRef iref2 = irt.Add(cookie0, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700314
315 EXPECT_TRUE(irt.Remove(cookie0, iref1));
316
317 // New segment.
318 const IRTSegmentState cookie1 = irt.GetSegmentState();
319
Andreas Gampe25651122017-09-25 14:50:23 -0700320 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700321
322 // Must not have filled the previous hole.
323 EXPECT_EQ(irt.Capacity(), 4u);
324 EXPECT_TRUE(irt.Get(iref1) == nullptr);
325 CheckDump(&irt, 3, 3);
326
327 UNUSED(iref0, iref1, iref2, iref3);
328 }
329
330 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
331 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700332 IndirectReferenceTable irt(kTableMax,
333 kGlobal,
334 IndirectReferenceTable::ResizableCapacity::kNo,
335 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700336 ASSERT_TRUE(irt.IsValid()) << error_msg;
337
338 const IRTSegmentState cookie0 = kIRTFirstSegment;
339
340 CheckDump(&irt, 0, 0);
341
Andreas Gampe25651122017-09-25 14:50:23 -0700342 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700343
344 // New segment.
345 const IRTSegmentState cookie1 = irt.GetSegmentState();
346
Andreas Gampe25651122017-09-25 14:50:23 -0700347 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
348 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
349 IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700350
351 EXPECT_TRUE(irt.Remove(cookie1, iref2));
352
353 // Pop segment.
354 irt.SetSegmentState(cookie1);
355
Andreas Gampe25651122017-09-25 14:50:23 -0700356 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700357
358 EXPECT_EQ(irt.Capacity(), 2u);
359 EXPECT_TRUE(irt.Get(iref2) == nullptr);
360 CheckDump(&irt, 2, 2);
361
362 UNUSED(iref0, iref1, iref2, iref3, iref4);
363 }
364
365 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
366 // reference.
367 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700368 IndirectReferenceTable irt(kTableMax,
369 kGlobal,
370 IndirectReferenceTable::ResizableCapacity::kNo,
371 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700372 ASSERT_TRUE(irt.IsValid()) << error_msg;
373
374 const IRTSegmentState cookie0 = kIRTFirstSegment;
375
376 CheckDump(&irt, 0, 0);
377
Andreas Gampe25651122017-09-25 14:50:23 -0700378 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700379
380 // New segment.
381 const IRTSegmentState cookie1 = irt.GetSegmentState();
382
Andreas Gampe25651122017-09-25 14:50:23 -0700383 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
384 IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700385
386 EXPECT_TRUE(irt.Remove(cookie1, iref1));
387
388 // New segment.
389 const IRTSegmentState cookie2 = irt.GetSegmentState();
390
Andreas Gampe25651122017-09-25 14:50:23 -0700391 IndirectRef iref3 = irt.Add(cookie2, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700392
393 // Pop segment.
394 irt.SetSegmentState(cookie2);
395
Andreas Gampe25651122017-09-25 14:50:23 -0700396 IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700397
398 EXPECT_EQ(irt.Capacity(), 3u);
399 EXPECT_TRUE(irt.Get(iref1) == nullptr);
400 CheckDump(&irt, 3, 3);
401
402 UNUSED(iref0, iref1, iref2, iref3, iref4);
403 }
404
405 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference.
406 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700407 IndirectReferenceTable irt(kTableMax,
408 kGlobal,
409 IndirectReferenceTable::ResizableCapacity::kNo,
410 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700411 ASSERT_TRUE(irt.IsValid()) << error_msg;
412
413 const IRTSegmentState cookie0 = kIRTFirstSegment;
414
415 CheckDump(&irt, 0, 0);
416
Andreas Gampe25651122017-09-25 14:50:23 -0700417 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700418
419 // New segment.
420 const IRTSegmentState cookie1 = irt.GetSegmentState();
421
Andreas Gampe25651122017-09-25 14:50:23 -0700422 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700423 EXPECT_TRUE(irt.Remove(cookie1, iref1));
424
425 // Emptied segment, push new one.
426 const IRTSegmentState cookie2 = irt.GetSegmentState();
427
Andreas Gampe25651122017-09-25 14:50:23 -0700428 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
429 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
430 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700431
432 EXPECT_TRUE(irt.Remove(cookie1, iref3));
433
434 // Pop segment.
435 UNUSED(cookie2);
436 irt.SetSegmentState(cookie1);
437
Andreas Gampe25651122017-09-25 14:50:23 -0700438 IndirectRef iref5 = irt.Add(cookie1, obj4.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700439
440 EXPECT_EQ(irt.Capacity(), 2u);
441 EXPECT_TRUE(irt.Get(iref3) == nullptr);
442 CheckDump(&irt, 2, 2);
443
444 UNUSED(iref0, iref1, iref2, iref3, iref4, iref5);
445 }
446
447 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
448 // reference
449 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700450 IndirectReferenceTable irt(kTableMax,
451 kGlobal,
452 IndirectReferenceTable::ResizableCapacity::kNo,
453 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700454 ASSERT_TRUE(irt.IsValid()) << error_msg;
455
456 const IRTSegmentState cookie0 = kIRTFirstSegment;
457
458 CheckDump(&irt, 0, 0);
459
Andreas Gampe25651122017-09-25 14:50:23 -0700460 IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700461
462 // New segment.
463 const IRTSegmentState cookie1 = irt.GetSegmentState();
464
Andreas Gampe25651122017-09-25 14:50:23 -0700465 IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg);
466 IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg);
467 IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700468
469 EXPECT_TRUE(irt.Remove(cookie1, iref2));
470
471 // Pop segment.
472 irt.SetSegmentState(cookie1);
473
474 // Push segment.
475 const IRTSegmentState cookie1_second = irt.GetSegmentState();
476 UNUSED(cookie1_second);
477
Andreas Gampe25651122017-09-25 14:50:23 -0700478 IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700479
480 EXPECT_EQ(irt.Capacity(), 2u);
481 EXPECT_TRUE(irt.Get(iref3) == nullptr);
482 CheckDump(&irt, 2, 2);
483
484 UNUSED(iref0, iref1, iref2, iref3, iref4);
485 }
486}
487
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700488TEST_F(IndirectReferenceTableTest, Resize) {
489 ScopedObjectAccess soa(Thread::Current());
490 static const size_t kTableMax = 512;
491
Vladimir Markoe9987b02018-05-22 16:26:43 +0100492 StackHandleScope<2> hs(soa.Self());
493 Handle<mirror::Class> c = hs.NewHandle(
494 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700495 ASSERT_TRUE(c != nullptr);
496 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800497 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700498
499 std::string error_msg;
500 IndirectReferenceTable irt(kTableMax,
501 kLocal,
502 IndirectReferenceTable::ResizableCapacity::kYes,
503 &error_msg);
504 ASSERT_TRUE(irt.IsValid()) << error_msg;
505
506 CheckDump(&irt, 0, 0);
507 const IRTSegmentState cookie = kIRTFirstSegment;
508
509 for (size_t i = 0; i != kTableMax + 1; ++i) {
Andreas Gampe25651122017-09-25 14:50:23 -0700510 irt.Add(cookie, obj0.Get(), &error_msg);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700511 }
512
513 EXPECT_EQ(irt.Capacity(), kTableMax + 1);
514}
515
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700516} // namespace art