Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 17 | #include "reference_table.h" |
| 18 | |
Andreas Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 19 | #include "class_linker.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
Andreas Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 21 | #include "handle_scope-inl.h" |
Andreas Gampe | 8db4c88 | 2014-07-17 14:52:06 -0700 | [diff] [blame] | 22 | #include "mirror/array-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Andreas Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 24 | #include "mirror/class_loader.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "mirror/string.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 26 | #include "primitive.h" |
Andreas Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 28 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 29 | #include "thread-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 33 | class ReferenceTableTest : public CommonRuntimeTest {}; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 34 | |
Andreas Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 35 | static 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 68 | TEST_F(ReferenceTableTest, Basics) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 71 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 72 | 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | // Check removal of all nullss in a empty table is a no-op. |
| 83 | rt.Remove(nullptr); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 84 | EXPECT_EQ(0U, rt.Size()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 85 | |
| 86 | // Check removal of all o1 in a empty table is a no-op. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 87 | rt.Remove(o1); |
| 88 | EXPECT_EQ(0U, rt.Size()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 89 | |
| 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 102 | 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 Gampe | a3bbf8b | 2016-09-27 18:45:02 -0700 | [diff] [blame] | 144 | |
| 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace art |