1 /* 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 */ 16 17 #include "reference_table.h" 18 19 #include <regex> 20 21 #include "android-base/stringprintf.h" 22 23 #include "art_method-inl.h" 24 #include "class_linker.h" 25 #include "common_runtime_test.h" 26 #include "dex/primitive.h" 27 #include "handle_scope-inl.h" 28 #include "mirror/array-inl.h" 29 #include "mirror/class-inl.h" 30 #include "mirror/class_loader.h" 31 #include "mirror/string.h" 32 #include "runtime.h" 33 #include "scoped_thread_state_change-inl.h" 34 #include "thread-current-inl.h" 35 #include "well_known_classes.h" 36 37 namespace art { 38 39 using android::base::StringPrintf; 40 41 class ReferenceTableTest : public CommonRuntimeTest {}; 42 43 static 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>()))); 55 CHECK(h_ref_class != nullptr); 56 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))); 60 CHECK(h_ref_instance != nullptr); 61 62 ArtMethod* constructor = h_ref_class->FindConstructor( 63 "(Ljava/lang/Object;)V", class_linker->GetImagePointerSize()); 64 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 76 TEST_F(ReferenceTableTest, Basics) { 77 ScopedObjectAccess soa(Thread::Current()); 78 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"); 79 80 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 90 // Check removal of all nullss in a empty table is a no-op. 91 rt.Remove(nullptr); 92 EXPECT_EQ(0U, rt.Size()); 93 94 // Check removal of all o1 in a empty table is a no-op. 95 rt.Remove(o1); 96 EXPECT_EQ(0U, rt.Size()); 97 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. 109 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0); 110 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 } 152 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(); 162 rt.Remove(empty_reference); 163 } 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(); 175 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 } 255 } 256 } 257 258 static 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 272 TEST_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 331 } // namespace art 332