blob: 3ea1714ee9ad8920c1949b8d45d9d81b66e0b32b [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
Igor Murashkin5573c372017-11-16 13:34:30 -080019#include <regex>
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "art_method-inl.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070024#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_runtime_test.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "dex/primitive.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070027#include "handle_scope-inl.h"
Andreas Gampe8db4c882014-07-17 14:52:06 -070028#include "mirror/array-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070029#include "mirror/array-alloc-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070031#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "mirror/string.h"
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070033#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070036#include "well_known_classes.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080037
Elliott Hughes11e45072011-08-16 17:40:46 -070038namespace art {
39
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040using android::base::StringPrintf;
41
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080042class ReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070043
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070044static mirror::Object* CreateWeakReference(mirror::Object* referent)
45 REQUIRES_SHARED(Locks::mutator_lock_) {
46 Thread* self = Thread::Current();
47 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
48
49 StackHandleScope<3> scope(self);
50 Handle<mirror::Object> h_referent(scope.NewHandle<mirror::Object>(referent));
51
52 Handle<mirror::Class> h_ref_class(scope.NewHandle<mirror::Class>(
53 class_linker->FindClass(self,
54 "Ljava/lang/ref/WeakReference;",
55 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -080056 CHECK(h_ref_class != nullptr);
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070057 CHECK(class_linker->EnsureInitialized(self, h_ref_class, true, true));
58
59 Handle<mirror::Object> h_ref_instance(scope.NewHandle<mirror::Object>(
60 h_ref_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080061 CHECK(h_ref_instance != nullptr);
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070062
Vladimir Markoba118822017-06-12 15:41:56 +010063 ArtMethod* constructor = h_ref_class->FindConstructor(
64 "(Ljava/lang/Object;)V", class_linker->GetImagePointerSize());
Andreas Gampea3bbf8b2016-09-27 18:45:02 -070065 CHECK(constructor != nullptr);
66
67 uint32_t args[2];
68 args[0] = PointerToLowMemUInt32(h_ref_instance.Get());
69 args[1] = PointerToLowMemUInt32(h_referent.Get());
70 JValue result;
71 constructor->Invoke(self, args, sizeof(uint32_t), &result, constructor->GetShorty());
72 CHECK(!self->IsExceptionPending());
73
74 return h_ref_instance.Get();
75}
76
Elliott Hughes11e45072011-08-16 17:40:46 -070077TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070080
Ian Rogers63818dc2012-09-26 12:23:04 -070081 ReferenceTable rt("test", 0, 11);
82
83 // Check dumping the empty table.
84 {
85 std::ostringstream oss;
86 rt.Dump(oss);
87 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
88 EXPECT_EQ(0U, rt.Size());
89 }
90
Mathieu Chartier2cebb242015-04-21 16:50:40 -070091 // Check removal of all nullss in a empty table is a no-op.
92 rt.Remove(nullptr);
Elliott Hughes11e45072011-08-16 17:40:46 -070093 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070094
95 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070096 rt.Remove(o1);
97 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070098
99 // Add o1 and check we have 1 element and can dump.
100 {
101 rt.Add(o1);
102 EXPECT_EQ(1U, rt.Size());
103 std::ostringstream oss;
104 rt.Dump(oss);
105 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
106 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
107 }
108
109 // Add a second object 10 times and check dumping is sane.
Vladimir Markobcf17522018-06-01 13:14:32 +0100110 ObjPtr<mirror::Object> o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -0700111 for (size_t i = 0; i < 10; ++i) {
112 rt.Add(o2);
113 EXPECT_EQ(i + 2, rt.Size());
114 std::ostringstream oss;
115 rt.Dump(oss);
116 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
117 i + 2 > 10 ? 10 : i + 2,
118 i + 2)),
119 std::string::npos) << oss.str();
120 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
121 if (i == 0) {
122 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
123 } else {
124 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
125 std::string::npos) << oss.str();
126 }
127 }
128
129 // Remove o1 (first element).
130 {
131 rt.Remove(o1);
132 EXPECT_EQ(10U, rt.Size());
133 std::ostringstream oss;
134 rt.Dump(oss);
135 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
136 }
137
138 // Remove o2 ten times.
139 for (size_t i = 0; i < 10; ++i) {
140 rt.Remove(o2);
141 EXPECT_EQ(9 - i, rt.Size());
142 std::ostringstream oss;
143 rt.Dump(oss);
144 if (i == 9) {
145 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
146 } else if (i == 8) {
147 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
148 } else {
149 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
150 std::string::npos) << oss.str();
151 }
152 }
Andreas Gampea3bbf8b2016-09-27 18:45:02 -0700153
154 // Add a reference and check that the type of the referent is dumped.
155 {
156 mirror::Object* empty_reference = CreateWeakReference(nullptr);
157 ASSERT_TRUE(empty_reference->IsReferenceInstance());
158 rt.Add(empty_reference);
159 std::ostringstream oss;
160 rt.Dump(oss);
161 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is null)"), std::string::npos)
162 << oss.str();
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -0700163 rt.Remove(empty_reference);
Andreas Gampea3bbf8b2016-09-27 18:45:02 -0700164 }
165
166 {
167 mirror::Object* string_referent = mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A");
168 mirror::Object* non_empty_reference = CreateWeakReference(string_referent);
169 ASSERT_TRUE(non_empty_reference->IsReferenceInstance());
170 rt.Add(non_empty_reference);
171 std::ostringstream oss;
172 rt.Dump(oss);
173 EXPECT_NE(oss.str().find("java.lang.ref.WeakReference (referent is a java.lang.String)"),
174 std::string::npos)
175 << oss.str();
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -0700176 rt.Remove(non_empty_reference);
177 }
178
179 // Add two objects. Enable allocation tracking for the latter.
180 {
181 StackHandleScope<3> hs(soa.Self());
182 Handle<mirror::String> h_without_trace(hs.NewHandle(
183 mirror::String::AllocFromModifiedUtf8(soa.Self(), "Without")));
184
185 {
186 ScopedThreadSuspension sts(soa.Self(), ThreadState::kSuspended);
187 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(true);
188 }
189
190 // To get a stack, actually make a call. Use substring, that's simple. Calling through JNI
191 // avoids having to create the low-level args array ourselves.
192 Handle<mirror::Object> h_with_trace;
193 {
194 jmethodID substr = soa.Env()->GetMethodID(WellKnownClasses::java_lang_String,
195 "substring",
196 "(II)Ljava/lang/String;");
197 ASSERT_TRUE(substr != nullptr);
198 jobject jobj = soa.Env()->AddLocalReference<jobject>(h_without_trace.Get());
199 ASSERT_TRUE(jobj != nullptr);
200 jobject result = soa.Env()->CallObjectMethod(jobj,
201 substr,
202 static_cast<jint>(0),
203 static_cast<jint>(4));
204 ASSERT_TRUE(result != nullptr);
205 h_with_trace = hs.NewHandle(soa.Self()->DecodeJObject(result));
206 }
207
208 Handle<mirror::Object> h_ref;
209 {
210 jclass weak_ref_class = soa.Env()->FindClass("java/lang/ref/WeakReference");
211 ASSERT_TRUE(weak_ref_class != nullptr);
212 jmethodID init = soa.Env()->GetMethodID(weak_ref_class,
213 "<init>",
214 "(Ljava/lang/Object;)V");
215 ASSERT_TRUE(init != nullptr);
216 jobject referent = soa.Env()->AddLocalReference<jobject>(h_with_trace.Get());
217 jobject result = soa.Env()->NewObject(weak_ref_class, init, referent);
218 ASSERT_TRUE(result != nullptr);
219 h_ref = hs.NewHandle(soa.Self()->DecodeJObject(result));
220 }
221
222 rt.Add(h_without_trace.Get());
223 rt.Add(h_with_trace.Get());
224 rt.Add(h_ref.Get());
225
226 std::ostringstream oss;
227 rt.Dump(oss);
228
229 constexpr const char* kStackTracePattern =
230 R"(test reference table dump:\n)"
231 R"( Last 3 entries \(of 3\):\n)" // NOLINT
232 R"( 2: 0x[0-9a-f]* java.lang.ref.WeakReference \(referent is a java.lang.String\)\n)" // NOLINT
233 R"( Allocated at:\n)"
234 R"( \(No managed frames\)\n)" // NOLINT
235 R"( Referent allocated at:\n)"
236 R"( java.lang.String java.lang.String.fastSubstring\(int, int\):-2\n)" // NOLINT
237 R"( java.lang.String java.lang.String.substring\(int, int\):[0-9]*\n)" // NOLINT
238 R"( 1: 0x[0-9a-f]* java.lang.String "With"\n)"
239 R"( Allocated at:\n)"
240 R"( java.lang.String java.lang.String.fastSubstring\(int, int\):-2\n)" // NOLINT
241 R"( java.lang.String java.lang.String.substring\(int, int\):[0-9]*\n)" // NOLINT
242 R"( 0: 0x[0-9a-f]* java.lang.String "Without"\n)"
243 R"( Summary:\n)"
244 R"( 2 of java.lang.String \(2 unique instances\)\n)" // NOLINT
245 R"( 1 of java.lang.ref.WeakReference\n)";
246 std::regex stack_trace_regex(kStackTracePattern);
247 std::smatch stack_trace_match;
248 std::string str = oss.str();
249 bool found = std::regex_search(str, stack_trace_match, stack_trace_regex);
250 EXPECT_TRUE(found) << str;
251
252 {
253 ScopedThreadSuspension sts(soa.Self(), ThreadState::kSuspended);
254 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(false);
255 }
Andreas Gampea3bbf8b2016-09-27 18:45:02 -0700256 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700257}
258
Andreas Gampe6e970e72016-11-14 17:00:28 -0800259static std::vector<size_t> FindAll(const std::string& haystack, const char* needle) {
260 std::vector<size_t> res;
261 size_t start = 0;
262 do {
263 size_t pos = haystack.find(needle, start);
264 if (pos == std::string::npos) {
265 break;
266 }
267 res.push_back(pos);
268 start = pos + 1;
269 } while (start < haystack.size());
270 return res;
271}
272
273TEST_F(ReferenceTableTest, SummaryOrder) {
274 // Check that the summary statistics are sorted.
275 ScopedObjectAccess soa(Thread::Current());
276
277 ReferenceTable rt("test", 0, 20);
278
279 {
Vladimir Markobcf17522018-06-01 13:14:32 +0100280 StackHandleScope<1> hs(soa.Self());
281 Handle<mirror::String> s1 =
282 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"));
283 ObjPtr<mirror::String> s2 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "world");
Andreas Gampe6e970e72016-11-14 17:00:28 -0800284
285 // 3 copies of s1, 2 copies of s2, interleaved.
286 for (size_t i = 0; i != 2; ++i) {
Vladimir Markobcf17522018-06-01 13:14:32 +0100287 rt.Add(s1.Get());
Andreas Gampe6e970e72016-11-14 17:00:28 -0800288 rt.Add(s2);
289 }
Vladimir Markobcf17522018-06-01 13:14:32 +0100290 rt.Add(s1.Get());
Andreas Gampe6e970e72016-11-14 17:00:28 -0800291 }
292
293 {
Vladimir Marko18090d12018-06-01 16:53:12 +0100294 // Differently sized byte arrays. Should be sorted by identical (non-unique count).
Vladimir Markobcf17522018-06-01 13:14:32 +0100295 StackHandleScope<1> hs(soa.Self());
296 Handle<mirror::ByteArray> b1_1 = hs.NewHandle(mirror::ByteArray::Alloc(soa.Self(), 1));
297 rt.Add(b1_1.Get());
Andreas Gampe6e970e72016-11-14 17:00:28 -0800298 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
Vladimir Markobcf17522018-06-01 13:14:32 +0100299 rt.Add(b1_1.Get());
Andreas Gampe6e970e72016-11-14 17:00:28 -0800300 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
301 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 1));
302 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
303 }
304
305 rt.Add(mirror::CharArray::Alloc(soa.Self(), 0));
306
307 // Now dump, and ensure order.
308 std::ostringstream oss;
309 rt.Dump(oss);
310
311 // Only do this on the part after Summary.
312 std::string base = oss.str();
313 size_t summary_pos = base.find("Summary:");
314 ASSERT_NE(summary_pos, std::string::npos);
315
316 std::string haystack = base.substr(summary_pos);
317
318 std::vector<size_t> strCounts = FindAll(haystack, "java.lang.String");
319 std::vector<size_t> b1Counts = FindAll(haystack, "byte[] (1 elements)");
320 std::vector<size_t> b2Counts = FindAll(haystack, "byte[] (2 elements)");
321 std::vector<size_t> cCounts = FindAll(haystack, "char[]");
322
323 // Only one each.
324 EXPECT_EQ(1u, strCounts.size());
325 EXPECT_EQ(1u, b1Counts.size());
326 EXPECT_EQ(1u, b2Counts.size());
327 EXPECT_EQ(1u, cCounts.size());
328
329 // Expect them to be in order.
330 EXPECT_LT(strCounts[0], b1Counts[0]);
331 EXPECT_LT(b1Counts[0], b2Counts[0]);
332 EXPECT_LT(b2Counts[0], cCounts[0]);
333}
334
Elliott Hughes11e45072011-08-16 17:40:46 -0700335} // namespace art