blob: 819e17a61992a10b334d3ed46290f6d6f88ae4bf [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes11e45072011-08-16 17:40:46 -070017#include "reference_table.h"
18
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070019#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_runtime_test.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070021#include "handle_scope-inl.h"
Andreas Gampe8db4c882014-07-17 14:52:06 -070022#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "mirror/class-inl.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070024#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "mirror/string.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "primitive.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070027#include "runtime.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "scoped_thread_state_change.h"
29#include "thread-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030
Elliott Hughes11e45072011-08-16 17:40:46 -070031namespace art {
32
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080033class ReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070034
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070035static mirror::Object* CreateWeakReference(mirror::Object* referent)
36 REQUIRES_SHARED(Locks::mutator_lock_) {
37 Thread* self = Thread::Current();
38 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
39
40 StackHandleScope<3> scope(self);
41 Handle<mirror::Object> h_referent(scope.NewHandle<mirror::Object>(referent));
42
43 Handle<mirror::Class> h_ref_class(scope.NewHandle<mirror::Class>(
44 class_linker->FindClass(self,
45 "Ljava/lang/ref/WeakReference;",
46 ScopedNullHandle<mirror::ClassLoader>())));
47 CHECK(h_ref_class.Get() != nullptr);
48 CHECK(class_linker->EnsureInitialized(self, h_ref_class, true, true));
49
50 Handle<mirror::Object> h_ref_instance(scope.NewHandle<mirror::Object>(
51 h_ref_class->AllocObject(self)));
52 CHECK(h_ref_instance.Get() != nullptr);
53
54 ArtMethod* constructor = h_ref_class->FindDeclaredDirectMethod(
55 "<init>", "(Ljava/lang/Object;)V", class_linker->GetImagePointerSize());
56 CHECK(constructor != nullptr);
57
58 uint32_t args[2];
59 args[0] = PointerToLowMemUInt32(h_ref_instance.Get());
60 args[1] = PointerToLowMemUInt32(h_referent.Get());
61 JValue result;
62 constructor->Invoke(self, args, sizeof(uint32_t), &result, constructor->GetShorty());
63 CHECK(!self->IsExceptionPending());
64
65 return h_ref_instance.Get();
66}
67
Elliott Hughes11e45072011-08-16 17:40:46 -070068TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070071
Ian Rogers63818dc2012-09-26 12:23:04 -070072 ReferenceTable rt("test", 0, 11);
73
74 // Check dumping the empty table.
75 {
76 std::ostringstream oss;
77 rt.Dump(oss);
78 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
79 EXPECT_EQ(0U, rt.Size());
80 }
81
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 // Check removal of all nullss in a empty table is a no-op.
83 rt.Remove(nullptr);
Elliott Hughes11e45072011-08-16 17:40:46 -070084 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070085
86 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070087 rt.Remove(o1);
88 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070089
90 // Add o1 and check we have 1 element and can dump.
91 {
92 rt.Add(o1);
93 EXPECT_EQ(1U, rt.Size());
94 std::ostringstream oss;
95 rt.Dump(oss);
96 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
97 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
98 }
99
100 // Add a second object 10 times and check dumping is sane.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -0700102 for (size_t i = 0; i < 10; ++i) {
103 rt.Add(o2);
104 EXPECT_EQ(i + 2, rt.Size());
105 std::ostringstream oss;
106 rt.Dump(oss);
107 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
108 i + 2 > 10 ? 10 : i + 2,
109 i + 2)),
110 std::string::npos) << oss.str();
111 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
112 if (i == 0) {
113 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
114 } else {
115 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
116 std::string::npos) << oss.str();
117 }
118 }
119
120 // Remove o1 (first element).
121 {
122 rt.Remove(o1);
123 EXPECT_EQ(10U, rt.Size());
124 std::ostringstream oss;
125 rt.Dump(oss);
126 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
127 }
128
129 // Remove o2 ten times.
130 for (size_t i = 0; i < 10; ++i) {
131 rt.Remove(o2);
132 EXPECT_EQ(9 - i, rt.Size());
133 std::ostringstream oss;
134 rt.Dump(oss);
135 if (i == 9) {
136 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
137 } else if (i == 8) {
138 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
139 } else {
140 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
141 std::string::npos) << oss.str();
142 }
143 }
Andreas Gampea3bbf8b2016-09-27 18:45:02 -0700144
145 // Add a reference and check that the type of the referent is dumped.
146 {
147 mirror::Object* empty_reference = CreateWeakReference(nullptr);
148 ASSERT_TRUE(empty_reference->IsReferenceInstance());
149 rt.Add(empty_reference);
150 std::ostringstream oss;
151 rt.Dump(oss);
152 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is null)"), std::string::npos)
153 << oss.str();
154 }
155
156 {
157 mirror::Object* string_referent = mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A");
158 mirror::Object* non_empty_reference = CreateWeakReference(string_referent);
159 ASSERT_TRUE(non_empty_reference->IsReferenceInstance());
160 rt.Add(non_empty_reference);
161 std::ostringstream oss;
162 rt.Dump(oss);
163 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is a java.lang.String)"),
164 std::string::npos)
165 << oss.str();
166 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700167}
168
169} // namespace art