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