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 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 22 | #include "android-base/strings.h" |
| 23 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 26 | #include "base/enums.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 30 | #include "dex_file_types.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 31 | #include "experimental_flags.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 32 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "gc/heap.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 34 | #include "mirror/accessible_object.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 36 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 38 | #include "mirror/emulated_stack_frame.h" |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 39 | #include "mirror/executable.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 40 | #include "mirror/field.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 41 | #include "mirror/method_type.h" |
| 42 | #include "mirror/method_handle_impl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/object-inl.h" |
| 44 | #include "mirror/object_array-inl.h" |
| 45 | #include "mirror/proxy.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 46 | #include "mirror/reference.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 48 | #include "mirror/string-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 49 | #include "handle_scope-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 50 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 51 | #include "thread-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 55 | class ClassLinkerTest : public CommonRuntimeTest { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 56 | protected: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | void AssertNonExistentClass(const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 58 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 59 | Thread* self = Thread::Current(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 60 | EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 61 | EXPECT_TRUE(self->IsExceptionPending()); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 62 | mirror::Object* exception = self->GetException(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 63 | self->ClearException(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 64 | mirror::Class* exception_class = |
| 65 | class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;"); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 66 | EXPECT_TRUE(exception->InstanceOf(exception_class)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | void AssertPrimitiveClass(const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 71 | Thread* self = Thread::Current(); |
| 72 | AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str())); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 75 | void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 76 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(primitive != nullptr); |
| 78 | ASSERT_TRUE(primitive->GetClass() != nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 79 | ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 81 | std::string temp; |
| 82 | ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp)); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 83 | EXPECT_TRUE(primitive->GetSuperClass() == nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 84 | EXPECT_FALSE(primitive->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(primitive->GetClassLoader() == nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 86 | EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(primitive->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 88 | EXPECT_TRUE(primitive->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(primitive->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(primitive->IsVerified()); |
| 91 | EXPECT_TRUE(primitive->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 92 | EXPECT_FALSE(primitive->IsArrayInstance()); |
| 93 | EXPECT_FALSE(primitive->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(primitive->GetComponentType() == nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 95 | EXPECT_FALSE(primitive->IsInterface()); |
| 96 | EXPECT_TRUE(primitive->IsPublic()); |
| 97 | EXPECT_TRUE(primitive->IsFinal()); |
| 98 | EXPECT_TRUE(primitive->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 99 | EXPECT_FALSE(primitive->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 100 | EXPECT_EQ(0U, primitive->NumDirectMethods()); |
| 101 | EXPECT_EQ(0U, primitive->NumVirtualMethods()); |
| 102 | EXPECT_EQ(0U, primitive->NumInstanceFields()); |
| 103 | EXPECT_EQ(0U, primitive->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 104 | EXPECT_EQ(0U, primitive->NumDirectInterfaces()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 105 | EXPECT_FALSE(primitive->HasVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 106 | EXPECT_EQ(0, primitive->GetIfTableCount()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 107 | EXPECT_TRUE(primitive->GetIfTable() != nullptr); |
| 108 | EXPECT_EQ(primitive->GetIfTable()->Count(), 0u); |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 109 | EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 112 | void AssertObjectClass(mirror::Class* JavaLangObject) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 113 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 114 | ASSERT_TRUE(JavaLangObject != nullptr); |
| 115 | ASSERT_TRUE(JavaLangObject->GetClass() != nullptr); |
| 116 | ASSERT_EQ(JavaLangObject->GetClass(), |
| 117 | JavaLangObject->GetClass()->GetClass()); |
| 118 | EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass()); |
| 119 | std::string temp; |
| 120 | ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;"); |
| 121 | EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr); |
| 122 | EXPECT_FALSE(JavaLangObject->HasSuperClass()); |
| 123 | EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr); |
| 124 | EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus()); |
| 125 | EXPECT_FALSE(JavaLangObject->IsErroneous()); |
| 126 | EXPECT_TRUE(JavaLangObject->IsLoaded()); |
| 127 | EXPECT_TRUE(JavaLangObject->IsResolved()); |
| 128 | EXPECT_TRUE(JavaLangObject->IsVerified()); |
| 129 | EXPECT_TRUE(JavaLangObject->IsInitialized()); |
| 130 | EXPECT_FALSE(JavaLangObject->IsArrayInstance()); |
| 131 | EXPECT_FALSE(JavaLangObject->IsArrayClass()); |
| 132 | EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr); |
| 133 | EXPECT_FALSE(JavaLangObject->IsInterface()); |
| 134 | EXPECT_TRUE(JavaLangObject->IsPublic()); |
| 135 | EXPECT_FALSE(JavaLangObject->IsFinal()); |
| 136 | EXPECT_FALSE(JavaLangObject->IsPrimitive()); |
| 137 | EXPECT_FALSE(JavaLangObject->IsSynthetic()); |
| 138 | EXPECT_EQ(2U, JavaLangObject->NumDirectMethods()); |
| 139 | EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods()); |
| 140 | if (!kUseBrooksReadBarrier) { |
| 141 | EXPECT_EQ(2U, JavaLangObject->NumInstanceFields()); |
| 142 | } else { |
| 143 | EXPECT_EQ(4U, JavaLangObject->NumInstanceFields()); |
| 144 | } |
| 145 | EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), |
| 146 | "shadow$_klass_"); |
| 147 | EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), |
| 148 | "shadow$_monitor_"); |
| 149 | if (kUseBrooksReadBarrier) { |
| 150 | EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), |
| 151 | "shadow$_x_rb_ptr_"); |
| 152 | EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), |
| 153 | "shadow$_x_xpadding_"); |
| 154 | } |
| 155 | |
| 156 | EXPECT_EQ(0U, JavaLangObject->NumStaticFields()); |
| 157 | EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces()); |
| 158 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 159 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 160 | ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod(); |
| 161 | ImTable* imt = JavaLangObject->GetImt(pointer_size); |
| 162 | ASSERT_NE(nullptr, imt); |
| 163 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 164 | ASSERT_EQ(unimplemented, imt->Get(i, pointer_size)); |
| 165 | } |
| 166 | } |
| 167 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 168 | void AssertArrayClass(const std::string& array_descriptor, |
| 169 | const std::string& component_type, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 170 | mirror::ClassLoader* class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 171 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 172 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 173 | StackHandleScope<2> hs(self); |
| 174 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
| 175 | Handle<mirror::Class> array( |
| 176 | hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader))); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 177 | std::string temp; |
| 178 | EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp)); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 179 | EXPECT_EQ(class_loader, array->GetClassLoader()); |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 180 | EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract))); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 181 | AssertArrayClass(array_descriptor, array); |
| 182 | } |
| 183 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 184 | void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 185 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 186 | ASSERT_TRUE(array.Get() != nullptr); |
| 187 | ASSERT_TRUE(array->GetClass() != nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 188 | ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 189 | EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 190 | std::string temp; |
| 191 | ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp)); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(array->GetSuperClass() != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 193 | Thread* self = Thread::Current(); |
| 194 | EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 195 | EXPECT_TRUE(array->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(array->GetComponentType() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 197 | ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 198 | EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 199 | EXPECT_FALSE(array->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 200 | EXPECT_TRUE(array->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 201 | EXPECT_TRUE(array->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 202 | EXPECT_TRUE(array->IsVerified()); |
| 203 | EXPECT_TRUE(array->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 204 | EXPECT_FALSE(array->IsArrayInstance()); |
| 205 | EXPECT_TRUE(array->IsArrayClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 206 | EXPECT_FALSE(array->IsInterface()); |
| 207 | EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic()); |
| 208 | EXPECT_TRUE(array->IsFinal()); |
| 209 | EXPECT_FALSE(array->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 210 | EXPECT_FALSE(array->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 211 | EXPECT_EQ(0U, array->NumDirectMethods()); |
| 212 | EXPECT_EQ(0U, array->NumVirtualMethods()); |
| 213 | EXPECT_EQ(0U, array->NumInstanceFields()); |
| 214 | EXPECT_EQ(0U, array->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 215 | EXPECT_EQ(2U, array->NumDirectInterfaces()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 216 | EXPECT_TRUE(array->ShouldHaveImt()); |
| 217 | EXPECT_TRUE(array->ShouldHaveEmbeddedVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 218 | EXPECT_EQ(2, array->GetIfTableCount()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(array->GetIfTable() != nullptr); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 220 | ObjPtr<mirror::Class> direct_interface0 = |
| 221 | mirror::Class::GetDirectInterface(self, array.Get(), 0); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 222 | EXPECT_TRUE(direct_interface0 != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 223 | EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;"); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 224 | ObjPtr<mirror::Class> direct_interface1 = |
| 225 | mirror::Class::GetDirectInterface(self, array.Get(), 1); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 226 | EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;"); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 227 | ObjPtr<mirror::Class> array_ptr = array->GetComponentType(); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 228 | EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 229 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 230 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 231 | mirror::Class* JavaLangObject = |
| 232 | class_linker_->FindSystemClass(self, "Ljava/lang/Object;"); |
| 233 | ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size); |
| 234 | // IMT of a array class should be shared with the IMT of the java.lag.Object |
| 235 | ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 236 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 237 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 238 | void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 239 | EXPECT_TRUE(method != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 240 | EXPECT_TRUE(method->GetDeclaringClass() != nullptr); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 241 | EXPECT_TRUE(method->GetName() != nullptr); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 242 | EXPECT_TRUE(method->GetSignature() != Signature::NoSignature()); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 243 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 244 | EXPECT_TRUE(method->HasDexCacheResolvedMethods(kRuntimePointerSize)); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 245 | EXPECT_TRUE(method->HasSameDexCacheResolvedMethods( |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 246 | method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 247 | kRuntimePointerSize)); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 250 | void AssertField(ObjPtr<mirror::Class> klass, ArtField* field) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 251 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 252 | EXPECT_TRUE(field != nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 253 | EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 254 | EXPECT_TRUE(field->GetName() != nullptr); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 255 | EXPECT_TRUE(field->GetType<true>() != nullptr); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 258 | void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 259 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 260 | std::string temp; |
| 261 | EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 262 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 263 | EXPECT_FALSE(klass->HasSuperClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 264 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 265 | EXPECT_TRUE(klass->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 266 | EXPECT_TRUE(klass->GetSuperClass() != nullptr); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 267 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 268 | EXPECT_TRUE(klass->GetClass() != nullptr); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 269 | EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 270 | EXPECT_TRUE(klass->GetDexCache() != nullptr); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 271 | EXPECT_TRUE(klass->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 272 | EXPECT_TRUE(klass->IsResolved()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 273 | EXPECT_FALSE(klass->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 274 | EXPECT_FALSE(klass->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 275 | EXPECT_TRUE(klass->GetComponentType() == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(klass->IsInSamePackage(klass.Get())); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 277 | std::string temp2; |
| 278 | EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp), |
| 279 | klass->GetDescriptor(&temp2))); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 280 | if (klass->IsInterface()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 281 | EXPECT_TRUE(klass->IsAbstract()); |
Alex Light | 42d511d | 2016-02-03 13:45:41 -0800 | [diff] [blame] | 282 | // Check that all direct methods are static (either <clinit> or a regular static method). |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 283 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
Alex Light | 42d511d | 2016-02-03 13:45:41 -0800 | [diff] [blame] | 284 | EXPECT_TRUE(m.IsStatic()); |
| 285 | EXPECT_TRUE(m.IsDirect()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 286 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 287 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 288 | if (!klass->IsSynthetic()) { |
| 289 | EXPECT_NE(0U, klass->NumDirectMethods()); |
| 290 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 291 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 292 | EXPECT_EQ(klass->IsInterface(), !klass->HasVTable()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 293 | mirror::IfTable* iftable = klass->GetIfTable(); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 294 | for (int i = 0; i < klass->GetIfTableCount(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 295 | mirror::Class* interface = iftable->GetInterface(i); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 296 | ASSERT_TRUE(interface != nullptr); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 297 | if (klass->IsInterface()) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 298 | EXPECT_EQ(0U, iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 299 | } else { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 300 | EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 303 | if (klass->IsAbstract()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 304 | EXPECT_FALSE(klass->IsFinal()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 305 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 306 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 307 | } |
| 308 | if (klass->IsFinal()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 309 | EXPECT_FALSE(klass->IsAbstract()); |
| 310 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 311 | } |
| 312 | if (klass->IsAnnotation()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 313 | EXPECT_FALSE(klass->IsFinal()); |
| 314 | EXPECT_TRUE(klass->IsAbstract()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | EXPECT_FALSE(klass->IsPrimitive()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 318 | EXPECT_TRUE(klass->CanAccess(klass.Get())); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 319 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 320 | for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 321 | AssertMethod(&method); |
| 322 | EXPECT_TRUE(method.IsDirect()); |
| 323 | EXPECT_EQ(klass.Get(), method.GetDeclaringClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 326 | for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 327 | AssertMethod(&method); |
| 328 | EXPECT_FALSE(method.IsDirect()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 329 | EXPECT_EQ(klass.Get(), method.GetDeclaringClass()); |
| 330 | } |
| 331 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 332 | for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 333 | AssertMethod(&method); |
| 334 | EXPECT_FALSE(method.IsDirect()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 335 | EXPECT_TRUE(method.IsCopied()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 336 | EXPECT_TRUE(method.GetDeclaringClass()->IsInterface()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 337 | << "declaring class: " << method.GetDeclaringClass()->PrettyClass(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 338 | EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get())) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 339 | << "declaring class: " << method.GetDeclaringClass()->PrettyClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 343 | ArtField* field = klass->GetInstanceField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 344 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 345 | EXPECT_FALSE(field->IsStatic()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | for (size_t i = 0; i < klass->NumStaticFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 349 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 350 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 351 | EXPECT_TRUE(field->IsStatic()); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 352 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 353 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 354 | // Confirm that all instances field offsets are packed together at the start. |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 355 | EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 356 | MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset(); |
| 357 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 358 | klass->NumReferenceInstanceFields() * |
| 359 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 360 | MemberOffset current_ref_offset = start_ref_offset; |
| 361 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 362 | ArtField* field = klass->GetInstanceField(i); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 363 | ObjPtr<mirror::Class> field_type = field->GetType<true>(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 364 | ASSERT_TRUE(field_type != nullptr); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 365 | if (!field->IsPrimitiveType()) { |
| 366 | ASSERT_TRUE(!field_type->IsPrimitive()); |
| 367 | ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value()); |
| 368 | if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) { |
| 369 | // While Reference.referent is not primitive, the ClassLinker |
| 370 | // treats it as such so that the garbage collector won't scan it. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 371 | EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent"); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 372 | } else { |
| 373 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 374 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 375 | } |
| 376 | } else { |
| 377 | if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) { |
| 378 | // Shuffled before references. |
| 379 | ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value()); |
| 380 | CHECK(!IsAligned<4>(field->GetOffset().Uint32Value())); |
| 381 | } |
Brian Carlstrom | fbfdce6 | 2011-10-05 17:33:32 -0700 | [diff] [blame] | 382 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 383 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 384 | ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 385 | |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 386 | uint32_t total_num_reference_instance_fields = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 387 | mirror::Class* k = klass.Get(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 388 | while (k != nullptr) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 389 | total_num_reference_instance_fields += k->NumReferenceInstanceFields(); |
| 390 | k = k->GetSuperClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 391 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 392 | EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class. |
| 393 | if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) { |
| 394 | // The reference instance offsets have a bit set for each reference offset. |
| 395 | // +1 for Object's class. |
| 396 | EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1, |
| 397 | total_num_reference_instance_fields); |
| 398 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 401 | void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 402 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 403 | ASSERT_TRUE(descriptor != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 404 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 405 | StackHandleScope<1> hs(self); |
| 406 | Handle<mirror::Class> klass( |
| 407 | hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str()))); |
| 408 | ASSERT_TRUE(klass.Get() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 409 | std::string temp; |
| 410 | EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp)); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 411 | EXPECT_EQ(class_loader, klass->GetClassLoader()); |
| 412 | if (klass->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 413 | AssertPrimitiveClass(descriptor, klass.Get()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 414 | } else if (klass->IsArrayClass()) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 415 | AssertArrayClass(descriptor, klass); |
| 416 | } else { |
| 417 | AssertClass(descriptor, klass); |
| 418 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 421 | void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 422 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 423 | // Verify all the classes defined in this file |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 424 | for (size_t i = 0; i < dex.NumClassDefs(); i++) { |
| 425 | const DexFile::ClassDef& class_def = dex.GetClassDef(i); |
| 426 | const char* descriptor = dex.GetClassDescriptor(class_def); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 427 | AssertDexFileClass(class_loader, descriptor); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 428 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 429 | // Verify all the types referenced by this file |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 430 | for (size_t i = 0; i < dex.NumTypeIds(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 431 | const DexFile::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i)); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 432 | const char* descriptor = dex.GetTypeDescriptor(type_id); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 433 | AssertDexFileClass(class_loader, descriptor); |
| 434 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 435 | TestRootVisitor visitor; |
| 436 | class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 437 | // Verify the dex cache has resolution methods in all resolved method slots |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 438 | mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 439 | auto* resolved_methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 440 | for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) { |
| 441 | EXPECT_TRUE( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 442 | mirror::DexCache::GetElementPtrSize(resolved_methods, i, kRuntimePointerSize) != nullptr) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 443 | << dex.GetLocation() << " i=" << i; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 444 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 445 | } |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 446 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 447 | class TestRootVisitor : public SingleRootVisitor { |
| 448 | public: |
| 449 | void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE { |
| 450 | EXPECT_TRUE(root != nullptr); |
| 451 | } |
| 452 | }; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 453 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 454 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 455 | class ClassLinkerMethodHandlesTest : public ClassLinkerTest { |
| 456 | protected: |
| 457 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE { |
| 458 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 459 | } |
| 460 | }; |
| 461 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 462 | struct CheckOffset { |
| 463 | size_t cpp_offset; |
| 464 | const char* java_name; |
| 465 | CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {} |
| 466 | }; |
| 467 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 468 | template <typename T> |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 469 | struct CheckOffsets { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 470 | CheckOffsets(bool is_static_in, const char* class_descriptor_in) |
| 471 | : is_static(is_static_in), class_descriptor(class_descriptor_in) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 472 | bool is_static; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 473 | std::string class_descriptor; |
| 474 | std::vector<CheckOffset> offsets; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 475 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 476 | bool Check() REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 477 | Thread* self = Thread::Current(); |
| 478 | mirror::Class* klass = |
| 479 | Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 480 | CHECK(klass != nullptr) << class_descriptor; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 481 | |
| 482 | bool error = false; |
| 483 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 484 | // Classes have a different size due to padding field. Strings are variable length. |
| 485 | if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 486 | // Currently only required for AccessibleObject since of the padding fields. The class linker |
| 487 | // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding. |
| 488 | // The RoundUp is to get around this case. |
| 489 | static constexpr size_t kPackAlignment = 4; |
Roland Levillain | 5e8d5f0 | 2016-10-18 18:03:43 +0100 | [diff] [blame] | 490 | size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(), |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 491 | kPackAlignment); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 492 | if (sizeof(T) != expected_size) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 493 | LOG(ERROR) << "Class size mismatch:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 494 | << " class=" << class_descriptor |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 495 | << " Java=" << expected_size |
| 496 | << " C++=" << sizeof(T); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 497 | error = true; |
| 498 | } |
| 499 | } |
| 500 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 501 | size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 502 | if (offsets.size() != num_fields) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 503 | LOG(ERROR) << "Field count mismatch:" |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 504 | << " class=" << class_descriptor |
| 505 | << " Java=" << num_fields |
| 506 | << " C++=" << offsets.size(); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 507 | error = true; |
| 508 | } |
| 509 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 510 | for (size_t i = 0; i < offsets.size(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 511 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 512 | StringPiece field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 513 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 514 | error = true; |
| 515 | } |
| 516 | } |
| 517 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 518 | for (size_t i = 0; i < offsets.size(); i++) { |
| 519 | CheckOffset& offset = offsets[i]; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 520 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 521 | StringPiece field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 522 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 523 | LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 524 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 525 | LOG(ERROR) << "Java field order:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 526 | << " i=" << i << " class=" << class_descriptor |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 527 | << " Java=" << field_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 528 | << " CheckOffsets=" << offset.java_name; |
| 529 | } |
| 530 | } |
| 531 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 532 | for (size_t i = 0; i < offsets.size(); i++) { |
| 533 | CheckOffset& offset = offsets[i]; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 534 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 535 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
| 536 | error = true; |
| 537 | } |
| 538 | } |
| 539 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 540 | for (size_t i = 0; i < offsets.size(); i++) { |
| 541 | CheckOffset& offset = offsets[i]; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 542 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 543 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 544 | LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 545 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 546 | LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 547 | << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return !error; |
| 552 | }; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 553 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 554 | void addOffset(size_t offset, const char* name) { |
| 555 | offsets.push_back(CheckOffset(offset, name)); |
| 556 | } |
| 557 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 558 | private: |
| 559 | DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 560 | }; |
| 561 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 562 | // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses |
| 563 | // is first since if it is failing, others are unlikely to succeed. |
| 564 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 565 | struct ObjectOffsets : public CheckOffsets<mirror::Object> { |
| 566 | ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 567 | addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_"); |
| 568 | addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 569 | #ifdef USE_BROOKS_READ_BARRIER |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 570 | addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_"); |
| 571 | addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_"); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 572 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 573 | }; |
| 574 | }; |
| 575 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 576 | struct ClassOffsets : public CheckOffsets<mirror::Class> { |
| 577 | ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 578 | addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags"); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 579 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 580 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader"); |
| 581 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize"); |
| 582 | addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId"); |
| 583 | addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 584 | addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 585 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 586 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex"); |
| 587 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex"); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 588 | addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 589 | addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields"); |
| 590 | addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 591 | addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 592 | addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 593 | addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), |
| 594 | "numReferenceInstanceFields"); |
| 595 | addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), |
| 596 | "numReferenceStaticFields"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 597 | addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize"); |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 598 | addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_), |
| 599 | "objectSizeAllocFastPath"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 600 | addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType"); |
| 601 | addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), |
| 602 | "referenceInstanceOffsets"); |
| 603 | addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields"); |
| 604 | addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status"); |
| 605 | addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 606 | addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 607 | addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable"); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 608 | }; |
| 609 | }; |
| 610 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 611 | struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> { |
| 612 | ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 613 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches"); |
| 614 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods"); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 615 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_bytes_), "originalDexFile"); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 616 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError"); |
| 617 | } |
| 618 | }; |
| 619 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 620 | struct StringOffsets : public CheckOffsets<mirror::String> { |
| 621 | StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 622 | addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count"); |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 623 | addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash"); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 624 | }; |
| 625 | }; |
| 626 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 627 | struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> { |
| 628 | ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") { |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 629 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 630 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause"); |
| 631 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 632 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace"); |
| 633 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 634 | }; |
| 635 | }; |
| 636 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 637 | struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 638 | StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>( |
| 639 | false, "Ljava/lang/StackTraceElement;") { |
| 640 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"); |
| 641 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName"); |
| 642 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber"); |
| 643 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName"); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 644 | }; |
| 645 | }; |
| 646 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 647 | struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> { |
| 648 | ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 649 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator"); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 650 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 651 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages"); |
| 652 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent"); |
| 653 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 654 | }; |
| 655 | }; |
| 656 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 657 | struct ProxyOffsets : public CheckOffsets<mirror::Proxy> { |
| 658 | ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 659 | addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 660 | }; |
| 661 | }; |
| 662 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 663 | struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> { |
| 664 | DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 665 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex"); |
| 666 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile"); |
| 667 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location"); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 668 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields"); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 669 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes"); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 670 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods"); |
| 671 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes"); |
| 672 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 673 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields"); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 674 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 675 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods"); |
| 676 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes"); |
| 677 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 678 | }; |
| 679 | }; |
| 680 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 681 | struct ReferenceOffsets : public CheckOffsets<mirror::Reference> { |
| 682 | ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 683 | addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext"); |
| 684 | addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue"); |
| 685 | addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext"); |
| 686 | addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent"); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 687 | }; |
| 688 | }; |
| 689 | |
| 690 | struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 691 | FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>( |
| 692 | false, "Ljava/lang/ref/FinalizerReference;") { |
| 693 | addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next"); |
| 694 | addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev"); |
| 695 | addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie"); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 696 | }; |
| 697 | }; |
| 698 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 699 | struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 700 | AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>( |
| 701 | false, "Ljava/lang/reflect/AccessibleObject;") { |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 702 | addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override"); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 703 | }; |
| 704 | }; |
| 705 | |
| 706 | struct FieldOffsets : public CheckOffsets<mirror::Field> { |
| 707 | FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 708 | addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags"); |
| 709 | addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass"); |
| 710 | addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex"); |
| 711 | addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset"); |
| 712 | addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type"); |
| 713 | }; |
| 714 | }; |
| 715 | |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 716 | struct ExecutableOffsets : public CheckOffsets<mirror::Executable> { |
| 717 | ExecutableOffsets() : CheckOffsets<mirror::Executable>( |
| 718 | false, "Ljava/lang/reflect/Executable;") { |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 719 | addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags"); |
| 720 | addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod"); |
| 721 | addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass"); |
| 722 | addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_), |
| 723 | "declaringClassOfOverriddenMethod"); |
| 724 | addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex"); |
Neil Fuller | 60458a0 | 2016-09-01 15:32:44 +0100 | [diff] [blame] | 725 | addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_), |
| 726 | "hasRealParameterData"); |
| 727 | addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters"); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 728 | }; |
| 729 | }; |
| 730 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 731 | struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> { |
| 732 | MethodTypeOffsets() : CheckOffsets<mirror::MethodType>( |
| 733 | false, "Ljava/lang/invoke/MethodType;") { |
| 734 | addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form"); |
| 735 | addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor"); |
| 736 | addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes"); |
| 737 | addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype"); |
| 738 | addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt"); |
| 739 | } |
| 740 | }; |
| 741 | |
| 742 | struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> { |
| 743 | MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>( |
| 744 | false, "Ljava/lang/invoke/MethodHandle;") { |
| 745 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, art_field_or_method_), "artFieldOrMethod"); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 746 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, handle_kind_), "handleKind"); |
Narayan Kamath | 0a8485e | 2016-11-02 18:47:11 +0000 | [diff] [blame] | 747 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, nominal_type_), "nominalType"); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 748 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, method_type_), "type"); |
| 749 | } |
| 750 | }; |
| 751 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 752 | struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> { |
| 753 | EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>( |
| 754 | false, "Ldalvik/system/EmulatedStackFrame;") { |
Narayan Kamath | b79bbd8 | 2017-01-16 17:48:28 +0000 | [diff] [blame] | 755 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType"); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 756 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references"); |
| 757 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame"); |
| 758 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type"); |
| 759 | } |
| 760 | }; |
| 761 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 762 | // C++ fields must exactly match the fields in the Java classes. If this fails, |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 763 | // reorder the fields in the C++ class. Managed class fields are ordered by |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 764 | // ClassLinker::LinkFields. |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 765 | TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 766 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 767 | EXPECT_TRUE(ObjectOffsets().Check()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 768 | EXPECT_TRUE(ClassOffsets().Check()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 769 | EXPECT_TRUE(ClassExtOffsets().Check()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 770 | EXPECT_TRUE(StringOffsets().Check()); |
| 771 | EXPECT_TRUE(ThrowableOffsets().Check()); |
| 772 | EXPECT_TRUE(StackTraceElementOffsets().Check()); |
| 773 | EXPECT_TRUE(ClassLoaderOffsets().Check()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 774 | EXPECT_TRUE(ProxyOffsets().Check()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 775 | EXPECT_TRUE(DexCacheOffsets().Check()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 776 | EXPECT_TRUE(ReferenceOffsets().Check()); |
| 777 | EXPECT_TRUE(FinalizerReferenceOffsets().Check()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 778 | EXPECT_TRUE(AccessibleObjectOffsets().Check()); |
| 779 | EXPECT_TRUE(FieldOffsets().Check()); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 780 | EXPECT_TRUE(ExecutableOffsets().Check()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 781 | EXPECT_TRUE(MethodTypeOffsets().Check()); |
| 782 | EXPECT_TRUE(MethodHandleImplOffsets().Check()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 783 | EXPECT_TRUE(EmulatedStackFrameOffsets().Check()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 786 | TEST_F(ClassLinkerTest, FindClassNonexistent) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 787 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 788 | AssertNonExistentClass("NoSuchClass;"); |
| 789 | AssertNonExistentClass("LNoSuchClass;"); |
| 790 | } |
| 791 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 792 | TEST_F(ClassLinkerTest, GetDexFiles) { |
| 793 | ScopedObjectAccess soa(Thread::Current()); |
| 794 | |
| 795 | jobject jclass_loader = LoadDex("Nested"); |
| 796 | std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader)); |
| 797 | ASSERT_EQ(dex_files.size(), 1U); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 798 | EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar")); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 799 | |
| 800 | jobject jclass_loader2 = LoadDex("MultiDex"); |
| 801 | std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2)); |
| 802 | ASSERT_EQ(dex_files2.size(), 2U); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 803 | EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar")); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 806 | TEST_F(ClassLinkerTest, FindClassNested) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 807 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 808 | StackHandleScope<1> hs(soa.Self()); |
| 809 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 810 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 811 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 812 | mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 813 | ASSERT_TRUE(outer != nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 814 | EXPECT_EQ(0U, outer->NumVirtualMethods()); |
| 815 | EXPECT_EQ(1U, outer->NumDirectMethods()); |
| 816 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 817 | mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 818 | ASSERT_TRUE(inner != nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 819 | EXPECT_EQ(0U, inner->NumVirtualMethods()); |
| 820 | EXPECT_EQ(1U, inner->NumDirectMethods()); |
| 821 | } |
| 822 | |
| 823 | TEST_F(ClassLinkerTest, FindClass_Primitives) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 824 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 825 | const std::string expected("BCDFIJSZV"); |
Elliott Hughes | db7d5e9 | 2011-12-16 18:47:37 -0800 | [diff] [blame] | 826 | for (int ch = 1; ch < 256; ++ch) { |
| 827 | std::string descriptor; |
| 828 | descriptor.push_back(ch); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 829 | if (expected.find(ch) == std::string::npos) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 830 | AssertNonExistentClass(descriptor); |
| 831 | } else { |
| 832 | AssertPrimitiveClass(descriptor); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | TEST_F(ClassLinkerTest, FindClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 838 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 839 | mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 840 | AssertObjectClass(JavaLangObject); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 841 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 842 | StackHandleScope<1> hs(soa.Self()); |
| 843 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 844 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 845 | AssertNonExistentClass("LMyClass;"); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 846 | mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 847 | ASSERT_TRUE(MyClass != nullptr); |
| 848 | ASSERT_TRUE(MyClass->GetClass() != nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 849 | ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass()); |
| 850 | EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 851 | std::string temp; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 852 | ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 853 | EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject); |
| 854 | EXPECT_TRUE(MyClass->HasSuperClass()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 855 | EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 856 | EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 857 | EXPECT_FALSE(MyClass->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 858 | EXPECT_TRUE(MyClass->IsLoaded()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 859 | EXPECT_TRUE(MyClass->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 860 | EXPECT_FALSE(MyClass->IsVerified()); |
| 861 | EXPECT_FALSE(MyClass->IsInitialized()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 862 | EXPECT_FALSE(MyClass->IsArrayInstance()); |
| 863 | EXPECT_FALSE(MyClass->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 864 | EXPECT_TRUE(MyClass->GetComponentType() == nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 865 | EXPECT_FALSE(MyClass->IsInterface()); |
| 866 | EXPECT_FALSE(MyClass->IsPublic()); |
| 867 | EXPECT_FALSE(MyClass->IsFinal()); |
| 868 | EXPECT_FALSE(MyClass->IsPrimitive()); |
| 869 | EXPECT_FALSE(MyClass->IsSynthetic()); |
| 870 | EXPECT_EQ(1U, MyClass->NumDirectMethods()); |
| 871 | EXPECT_EQ(0U, MyClass->NumVirtualMethods()); |
| 872 | EXPECT_EQ(0U, MyClass->NumInstanceFields()); |
| 873 | EXPECT_EQ(0U, MyClass->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 874 | EXPECT_EQ(0U, MyClass->NumDirectInterfaces()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 875 | |
| 876 | EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass()); |
| 877 | |
| 878 | // created by class_linker |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 879 | AssertArrayClass("[C", "C", nullptr); |
| 880 | AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 881 | // synthesized on the fly |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 882 | AssertArrayClass("[[C", "[C", nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 883 | AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 884 | // or not available at all |
| 885 | AssertNonExistentClass("[[[[LNonExistentClass;"); |
| 886 | } |
| 887 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 888 | TEST_F(ClassLinkerTest, LookupResolvedType) { |
| 889 | ScopedObjectAccess soa(Thread::Current()); |
| 890 | StackHandleScope<1> hs(soa.Self()); |
| 891 | Handle<mirror::ClassLoader> class_loader( |
| 892 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
| 893 | AssertNonExistentClass("LMyClass;"); |
| 894 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 895 | dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 896 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
| 897 | const DexFile& dex_file = klass->GetDexFile(); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 898 | EXPECT_OBJ_PTR_EQ( |
| 899 | class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()), |
| 900 | klass); |
| 901 | // Zero out the resolved type and make sure LookupResolvedType still finds it. |
| 902 | dex_cache->SetResolvedType(type_idx, nullptr); |
| 903 | EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr); |
| 904 | EXPECT_OBJ_PTR_EQ( |
| 905 | class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()), |
| 906 | klass); |
| 907 | } |
| 908 | |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 909 | TEST_F(ClassLinkerTest, LookupResolvedTypeArray) { |
| 910 | ScopedObjectAccess soa(Thread::Current()); |
| 911 | StackHandleScope<2> hs(soa.Self()); |
| 912 | Handle<mirror::ClassLoader> class_loader( |
| 913 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields")))); |
| 914 | // Get the AllFields class for the dex cache and dex file. |
| 915 | ObjPtr<mirror::Class> all_fields_klass |
| 916 | = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader); |
| 917 | ASSERT_OBJ_PTR_NE(all_fields_klass, ObjPtr<mirror::Class>(nullptr)); |
| 918 | Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache()); |
| 919 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 920 | // Get the index of the array class we want to test. |
| 921 | const DexFile::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;"); |
| 922 | ASSERT_TRUE(array_id != nullptr); |
| 923 | dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id); |
| 924 | // Check that the array class wasn't resolved yet. |
| 925 | EXPECT_OBJ_PTR_EQ( |
| 926 | class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()), |
| 927 | ObjPtr<mirror::Class>(nullptr)); |
| 928 | // Resolve the array class we want to test. |
| 929 | ObjPtr<mirror::Class> array_klass |
| 930 | = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader); |
| 931 | ASSERT_OBJ_PTR_NE(array_klass, ObjPtr<mirror::Class>(nullptr)); |
| 932 | // Test that LookupResolvedType() finds the array class. |
| 933 | EXPECT_OBJ_PTR_EQ( |
| 934 | class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()), |
| 935 | array_klass); |
| 936 | // Zero out the resolved type and make sure LookupResolvedType() still finds it. |
| 937 | dex_cache->SetResolvedType(array_idx, nullptr); |
| 938 | EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr); |
| 939 | EXPECT_OBJ_PTR_EQ( |
| 940 | class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()), |
| 941 | array_klass); |
| 942 | } |
| 943 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 944 | TEST_F(ClassLinkerTest, LibCore) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 945 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 946 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 947 | AssertDexFile(*java_lang_dex_file_, nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 950 | // The first reference array element must be a multiple of 4 bytes from the |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 951 | // start of the object |
| 952 | TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 953 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 954 | mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 955 | mirror::ObjectArray<mirror::String>* array = |
| 956 | mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 957 | uintptr_t data_offset = |
| 958 | reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>), |
| 959 | 0)); |
| 960 | if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 961 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment. |
| 962 | } else { |
| 963 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment. |
| 964 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 968 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 969 | StackHandleScope<5> hs(soa.Self()); |
| 970 | Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 971 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 972 | uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData()); |
| 973 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment |
| 974 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 975 | Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 976 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 977 | data_offset = reinterpret_cast<uintptr_t>(double_array->GetData()); |
| 978 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment |
| 979 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 980 | Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 981 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 982 | data_offset = reinterpret_cast<uintptr_t>(int_array->GetData()); |
| 983 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment |
| 984 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 985 | Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 986 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 987 | data_offset = reinterpret_cast<uintptr_t>(char_array->GetData()); |
| 988 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment |
| 989 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 990 | Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 991 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 992 | data_offset = reinterpret_cast<uintptr_t>(short_array->GetData()); |
| 993 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment |
| 994 | |
| 995 | // Take it as given that bytes and booleans have byte alignment |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 996 | } |
| 997 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 998 | TEST_F(ClassLinkerTest, ValidateBoxedTypes) { |
| 999 | // Validate that the "value" field is always the 0th field in each of java.lang's box classes. |
| 1000 | // This lets UnboxPrimitive avoid searching for the field by name at runtime. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1001 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1002 | ScopedNullHandle<mirror::ClassLoader> class_loader; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1003 | mirror::Class* c; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1004 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1005 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1006 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1007 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1008 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1009 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1010 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1011 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1012 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1013 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1014 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1015 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1016 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1017 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1018 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1019 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1022 | TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1023 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1024 | StackHandleScope<2> hs(soa.Self()); |
| 1025 | Handle<mirror::ClassLoader> class_loader_1( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1026 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1027 | Handle<mirror::ClassLoader> class_loader_2( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1028 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1029 | mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1); |
| 1030 | mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1031 | EXPECT_TRUE(MyClass_1 != nullptr); |
| 1032 | EXPECT_TRUE(MyClass_2 != nullptr); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1033 | EXPECT_NE(MyClass_1, MyClass_2); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 1034 | } |
| 1035 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1036 | TEST_F(ClassLinkerTest, StaticFields) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1037 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1038 | StackHandleScope<2> hs(soa.Self()); |
| 1039 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1040 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1041 | Handle<mirror::Class> statics( |
| 1042 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1043 | class_linker_->EnsureInitialized(soa.Self(), statics, true, true); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1044 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1045 | // Static final primitives that are initialized by a compile-time constant |
| 1046 | // expression resolve to a copy of a constant value from the constant pool. |
| 1047 | // So <clinit> should be null. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1048 | ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", kRuntimePointerSize); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1049 | EXPECT_TRUE(clinit == nullptr); |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1050 | |
| 1051 | EXPECT_EQ(9U, statics->NumStaticFields()); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1052 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1053 | ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1054 | EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1055 | EXPECT_EQ(true, s0->GetBoolean(statics.Get())); |
| 1056 | s0->SetBoolean<false>(statics.Get(), false); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1057 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1058 | ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1059 | EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1060 | EXPECT_EQ(5, s1->GetByte(statics.Get())); |
| 1061 | s1->SetByte<false>(statics.Get(), 6); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1062 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1063 | ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1064 | EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1065 | EXPECT_EQ('a', s2->GetChar(statics.Get())); |
| 1066 | s2->SetChar<false>(statics.Get(), 'b'); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1067 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1068 | ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1069 | EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1070 | EXPECT_EQ(-536, s3->GetShort(statics.Get())); |
| 1071 | s3->SetShort<false>(statics.Get(), -535); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1072 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1073 | ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1074 | EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1075 | EXPECT_EQ(2000000000, s4->GetInt(statics.Get())); |
| 1076 | s4->SetInt<false>(statics.Get(), 2000000001); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1077 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1078 | ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1079 | EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1080 | EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get())); |
| 1081 | s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12)); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1082 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1083 | ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1084 | EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1085 | EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1086 | s6->SetFloat<false>(statics.Get(), 0.75); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1087 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1088 | ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1089 | EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1090 | EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1091 | s7->SetDouble<false>(statics.Get(), 16777219); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1092 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1093 | ArtField* s8 = mirror::Class::FindStaticField( |
| 1094 | soa.Self(), statics.Get(), "s8", "Ljava/lang/String;"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1095 | EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1096 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android")); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1097 | mirror::String* str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"); |
| 1098 | s8->SetObject<false>(s8->GetDeclaringClass(), str_value); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1099 | |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1100 | // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ |
| 1101 | // http://code.google.com/p/googletest/issues/detail?id=322 |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1102 | EXPECT_FALSE(s0->GetBoolean(statics.Get())); |
| 1103 | EXPECT_EQ(6, s1->GetByte(statics.Get())); |
| 1104 | EXPECT_EQ('b', s2->GetChar(statics.Get())); |
| 1105 | EXPECT_EQ(-535, s3->GetShort(statics.Get())); |
| 1106 | EXPECT_EQ(2000000001, s4->GetInt(statics.Get())); |
| 1107 | EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get())); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1108 | EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get())); |
| 1109 | EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1110 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1113 | TEST_F(ClassLinkerTest, Interfaces) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1114 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1115 | StackHandleScope<6> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1116 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1117 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces")))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1118 | Handle<mirror::Class> I( |
| 1119 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 1120 | Handle<mirror::Class> J( |
| 1121 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
| 1122 | Handle<mirror::Class> K( |
| 1123 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader))); |
| 1124 | Handle<mirror::Class> A( |
| 1125 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader))); |
| 1126 | Handle<mirror::Class> B( |
| 1127 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader))); |
| 1128 | EXPECT_TRUE(I->IsAssignableFrom(A.Get())); |
| 1129 | EXPECT_TRUE(J->IsAssignableFrom(A.Get())); |
| 1130 | EXPECT_TRUE(J->IsAssignableFrom(K.Get())); |
| 1131 | EXPECT_TRUE(K->IsAssignableFrom(B.Get())); |
| 1132 | EXPECT_TRUE(J->IsAssignableFrom(B.Get())); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1133 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1134 | const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V"); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1135 | ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, kRuntimePointerSize); |
| 1136 | ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, kRuntimePointerSize); |
| 1137 | ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, kRuntimePointerSize); |
| 1138 | ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize); |
| 1139 | ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize); |
| 1140 | ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize); |
| 1141 | ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, kRuntimePointerSize); |
| 1142 | ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, kRuntimePointerSize); |
| 1143 | ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, kRuntimePointerSize); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1144 | ASSERT_TRUE(Ii != nullptr); |
| 1145 | ASSERT_TRUE(Jj1 != nullptr); |
| 1146 | ASSERT_TRUE(Jj2 != nullptr); |
| 1147 | ASSERT_TRUE(Kj1 != nullptr); |
| 1148 | ASSERT_TRUE(Kj2 != nullptr); |
| 1149 | ASSERT_TRUE(Kk != nullptr); |
| 1150 | ASSERT_TRUE(Ai != nullptr); |
| 1151 | ASSERT_TRUE(Aj1 != nullptr); |
| 1152 | ASSERT_TRUE(Aj2 != nullptr); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1153 | EXPECT_NE(Ii, Ai); |
| 1154 | EXPECT_NE(Jj1, Aj1); |
| 1155 | EXPECT_NE(Jj2, Aj2); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 1156 | EXPECT_EQ(Kj1, Jj1); |
| 1157 | EXPECT_EQ(Kj2, Jj2); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1158 | EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize)); |
| 1159 | EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize)); |
| 1160 | EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize)); |
| 1161 | EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize)); |
| 1162 | EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize)); |
| 1163 | EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize)); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1164 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1165 | ArtField* Afoo = |
| 1166 | mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;"); |
| 1167 | ArtField* Bfoo = |
| 1168 | mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;"); |
| 1169 | ArtField* Jfoo = |
| 1170 | mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;"); |
| 1171 | ArtField* Kfoo = |
| 1172 | mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1173 | ASSERT_TRUE(Afoo != nullptr); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1174 | EXPECT_EQ(Afoo, Bfoo); |
| 1175 | EXPECT_EQ(Afoo, Jfoo); |
| 1176 | EXPECT_EQ(Afoo, Kfoo); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 1179 | TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) { |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1180 | // pretend we are trying to get the static storage for the StaticsFromCode class. |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1181 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1182 | // case 1, get the uninitialized storage from StaticsFromCode.<clinit> |
| 1183 | // case 2, get the initialized storage from StaticsFromCode.getS0 |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1184 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1185 | ScopedObjectAccess soa(Thread::Current()); |
| 1186 | jobject jclass_loader = LoadDex("StaticsFromCode"); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1187 | const DexFile* dex_file = GetFirstDexFile(jclass_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1188 | StackHandleScope<1> hs(soa.Self()); |
| 1189 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1190 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1191 | mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1192 | ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize); |
| 1193 | ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 1194 | const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1195 | ASSERT_TRUE(type_id != nullptr); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1196 | dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1197 | mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1198 | EXPECT_TRUE(uninit != nullptr); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1199 | EXPECT_FALSE(uninit->IsInitialized()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1200 | mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1201 | EXPECT_TRUE(init != nullptr); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1202 | EXPECT_TRUE(init->IsInitialized()); |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 1205 | TEST_F(ClassLinkerTest, ErroneousClass) { |
| 1206 | ScopedObjectAccess soa(Thread::Current()); |
| 1207 | jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB"); |
| 1208 | StackHandleScope<1> hs(soa.Self()); |
| 1209 | Handle<mirror::ClassLoader> class_loader( |
| 1210 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
| 1211 | hs.Self()->AssertNoPendingException(); |
| 1212 | const char* descriptor = "LErroneous;"; |
| 1213 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader); |
| 1214 | // Erronenous since we are extending final class. |
| 1215 | hs.Self()->AssertPendingException(); |
| 1216 | EXPECT_TRUE(klass == nullptr); |
| 1217 | klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get()); |
| 1218 | EXPECT_FALSE(klass == nullptr); |
| 1219 | EXPECT_TRUE(klass->IsErroneous()); |
| 1220 | EXPECT_TRUE(klass->GetIfTable() != nullptr); |
| 1221 | } |
| 1222 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1223 | TEST_F(ClassLinkerTest, FinalizableBit) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1224 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1225 | mirror::Class* c; |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1226 | |
| 1227 | // Object has a finalize method, but we know it's empty. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1228 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1229 | EXPECT_FALSE(c->IsFinalizable()); |
| 1230 | |
| 1231 | // Enum has a finalize method to prevent its subclasses from implementing one. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1232 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1233 | EXPECT_FALSE(c->IsFinalizable()); |
| 1234 | |
| 1235 | // RoundingMode is an enum. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1236 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1237 | EXPECT_FALSE(c->IsFinalizable()); |
| 1238 | |
| 1239 | // RandomAccessFile extends Object and overrides finalize. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1240 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1241 | EXPECT_TRUE(c->IsFinalizable()); |
| 1242 | |
| 1243 | // FileInputStream is finalizable and extends InputStream which isn't. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1244 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1245 | EXPECT_FALSE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1246 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1247 | EXPECT_TRUE(c->IsFinalizable()); |
| 1248 | |
| 1249 | // ScheduledThreadPoolExecutor doesn't have a finalize method but |
| 1250 | // extends ThreadPoolExecutor which does. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1251 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1252 | EXPECT_TRUE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1253 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1254 | EXPECT_TRUE(c->IsFinalizable()); |
| 1255 | } |
| 1256 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1257 | TEST_F(ClassLinkerTest, ClassRootDescriptors) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1258 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1259 | std::string temp; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1260 | for (int i = 0; i < ClassLinker::kClassRootsMax; i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1261 | mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i)); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1262 | EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U); |
| 1263 | EXPECT_STREQ(klass->GetDescriptor(&temp), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1264 | class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i; |
| 1265 | } |
| 1266 | } |
| 1267 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1268 | TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) { |
| 1269 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1270 | ScopedNullHandle<mirror::ClassLoader> class_loader; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1271 | mirror::Class* c; |
| 1272 | |
| 1273 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1274 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1275 | EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1276 | |
| 1277 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1278 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1279 | EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1280 | |
| 1281 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1282 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1283 | EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1284 | |
| 1285 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1286 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1287 | EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1290 | static void CheckMethod(ArtMethod* method, bool verified) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1291 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1292 | if (!method->IsNative() && !method->IsAbstract()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1293 | EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1294 | << method->PrettyMethod(true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1298 | static void CheckVerificationAttempted(mirror::Class* c, bool preverified) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1299 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1300 | EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1301 | << "Class " << mirror::Class::PrettyClass(c) << " not as expected"; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1302 | for (auto& m : c->GetMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1303 | CheckMethod(&m, preverified); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | TEST_F(ClassLinkerTest, Preverified_InitializedBoot) { |
| 1308 | ScopedObjectAccess soa(Thread::Current()); |
| 1309 | |
| 1310 | mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1311 | ASSERT_TRUE(JavaLangObject != nullptr); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1312 | EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the " |
| 1313 | "core"; |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1314 | CheckVerificationAttempted(JavaLangObject, true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) { |
| 1318 | ScopedObjectAccess soa(Thread::Current()); |
| 1319 | |
| 1320 | StackHandleScope<1> hs(soa.Self()); |
| 1321 | |
| 1322 | Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass( |
| 1323 | soa.Self(), "Ljava/lang/SecurityManager;"))); |
| 1324 | EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the " |
| 1325 | "core"; |
| 1326 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1327 | CheckVerificationAttempted(security_manager.Get(), false); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1328 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1329 | class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1330 | CheckVerificationAttempted(security_manager.Get(), true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | TEST_F(ClassLinkerTest, Preverified_App) { |
| 1334 | ScopedObjectAccess soa(Thread::Current()); |
| 1335 | |
| 1336 | StackHandleScope<2> hs(soa.Self()); |
| 1337 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1338 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1339 | Handle<mirror::Class> statics( |
| 1340 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
| 1341 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1342 | CheckVerificationAttempted(statics.Get(), false); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1343 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1344 | class_linker_->EnsureInitialized(soa.Self(), statics, true, true); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1345 | CheckVerificationAttempted(statics.Get(), true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1348 | TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) { |
| 1349 | ScopedObjectAccess soa(Thread::Current()); |
| 1350 | |
| 1351 | StackHandleScope<3> hs(soa.Self()); |
| 1352 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1353 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1354 | |
| 1355 | // java.lang.Object is a bootstrap class. |
| 1356 | Handle<mirror::Class> jlo_class( |
| 1357 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
| 1358 | ASSERT_TRUE(jlo_class.Get() != nullptr); |
| 1359 | EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded()); |
| 1360 | |
| 1361 | // Statics is not a bootstrap class. |
| 1362 | Handle<mirror::Class> statics( |
| 1363 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
| 1364 | ASSERT_TRUE(statics.Get() != nullptr); |
| 1365 | EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded()); |
| 1366 | } |
| 1367 | |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1368 | // Regression test for b/26799552. |
| 1369 | TEST_F(ClassLinkerTest, RegisterDexFileName) { |
| 1370 | ScopedObjectAccess soa(Thread::Current()); |
| 1371 | StackHandleScope<2> hs(soa.Self()); |
| 1372 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1373 | MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr)); |
| 1374 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1375 | ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1376 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1377 | dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache()); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1378 | if (dex_cache.Get() != nullptr) { |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | ASSERT_TRUE(dex_cache.Get() != nullptr); |
| 1383 | } |
| 1384 | // Make a copy of the dex cache and change the name. |
| 1385 | dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache()); |
| 1386 | const uint16_t data[] = { 0x20AC, 0x20A1 }; |
| 1387 | Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(), |
| 1388 | arraysize(data), |
| 1389 | data))); |
| 1390 | dex_cache->SetLocation(location.Get()); |
| 1391 | const DexFile* old_dex_file = dex_cache->GetDexFile(); |
| 1392 | |
Hiroshi Yamauchi | 906ae5d | 2016-02-22 13:30:35 -0800 | [diff] [blame] | 1393 | std::unique_ptr<DexFile> dex_file(new DexFile(old_dex_file->Begin(), |
| 1394 | old_dex_file->Size(), |
| 1395 | location->ToModifiedUtf8(), |
| 1396 | 0u, |
Hiroshi Yamauchi | 906ae5d | 2016-02-22 13:30:35 -0800 | [diff] [blame] | 1397 | nullptr)); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1398 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1399 | WriterMutexLock mu(soa.Self(), *Locks::dex_lock_); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1400 | // Check that inserting with a UTF16 name works. |
| 1401 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1405 | TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) { |
| 1406 | ScopedObjectAccess soa(Thread::Current()); |
| 1407 | |
| 1408 | StackHandleScope<7> hs(soa.Self()); |
| 1409 | |
| 1410 | Handle<mirror::ClassLoader> class_loader( |
| 1411 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes")))); |
| 1412 | Handle<mirror::Class> method_types( |
| 1413 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader))); |
| 1414 | class_linker_->EnsureInitialized(soa.Self(), method_types, true, true); |
| 1415 | |
| 1416 | ArtMethod* method1 = method_types->FindVirtualMethod("method1", |
| 1417 | "(Ljava/lang/String;)Ljava/lang/String;", |
| 1418 | kRuntimePointerSize); |
| 1419 | |
| 1420 | const DexFile& dex_file = *(method1->GetDexFile()); |
| 1421 | Handle<mirror::DexCache> dex_cache = hs.NewHandle( |
| 1422 | class_linker_->FindDexCache(Thread::Current(), dex_file)); |
| 1423 | |
| 1424 | const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex()); |
| 1425 | |
| 1426 | // This is the MethodType corresponding to the prototype of |
| 1427 | // String MethodTypes# method1(String). |
| 1428 | // Its RType = Ljava/lang/String; |
Narayan Kamath | 42b3dd0 | 2016-10-03 11:33:01 +0100 | [diff] [blame] | 1429 | // Its PTypes = { Ljava/lang/String; } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1430 | Handle<mirror::MethodType> method1_type = hs.NewHandle( |
| 1431 | class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader)); |
| 1432 | |
| 1433 | // Assert that the method type was resolved successfully. |
| 1434 | ASSERT_TRUE(method1_type.Get() != nullptr); |
| 1435 | |
| 1436 | // Assert that the return type and the method arguments are as we expect. |
| 1437 | Handle<mirror::Class> string_class( |
| 1438 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader))); |
| 1439 | ASSERT_EQ(string_class.Get(), method1_type->GetRType()); |
| 1440 | ASSERT_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0)); |
| 1441 | |
| 1442 | // Resolve the method type again and assert that we get back the same value. |
| 1443 | Handle<mirror::MethodType> method1_type2 = hs.NewHandle( |
| 1444 | class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader)); |
| 1445 | ASSERT_EQ(method1_type.Get(), method1_type2.Get()); |
| 1446 | |
| 1447 | // Resolve the MethodType associated with a different method signature |
| 1448 | // and assert it's different. |
| 1449 | ArtMethod* method2 = method_types->FindVirtualMethod( |
| 1450 | "method2", |
| 1451 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 1452 | kRuntimePointerSize); |
| 1453 | const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex()); |
| 1454 | Handle<mirror::MethodType> method2_type = hs.NewHandle( |
| 1455 | class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader)); |
| 1456 | |
| 1457 | ASSERT_TRUE(method1_type.Get() != method2_type.Get()); |
| 1458 | } |
| 1459 | |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1460 | } // namespace art |