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