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> |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 21 | #include <string_view> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 23 | #include "android-base/strings.h" |
| 24 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 25 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 26 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 29 | #include "class_root-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | #include "common_runtime_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 31 | #include "dex/dex_file_types.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 32 | #include "dex/signature-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "dex/standard_dex_file.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 34 | #include "entrypoints/entrypoint_utils-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 35 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/heap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include "handle_scope-inl.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 38 | #include "mirror/array-alloc-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 39 | #include "mirror/accessible_object.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 40 | #include "mirror/call_site.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mirror/class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 42 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 44 | #include "mirror/emulated_stack_frame.h" |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 45 | #include "mirror/executable.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 46 | #include "mirror/field.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 47 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 48 | #include "mirror/method_handles_lookup.h" |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 49 | #include "mirror/method_type-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | #include "mirror/object-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 51 | #include "mirror/object_array-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/object_array-inl.h" |
| 53 | #include "mirror/proxy.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 54 | #include "mirror/reference.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 56 | #include "mirror/string-inl.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 57 | #include "mirror/var_handle.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 58 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 59 | #include "thread-current-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 63 | class ClassLinkerTest : public CommonRuntimeTest { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 64 | protected: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | void AssertNonExistentClass(const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 66 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 67 | Thread* self = Thread::Current(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 68 | EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 69 | EXPECT_TRUE(self->IsExceptionPending()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 70 | StackHandleScope<1> hs(self); |
| 71 | Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException()); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 72 | self->ClearException(); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 73 | ObjPtr<mirror::Class> exception_class = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 74 | class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;"); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(exception->InstanceOf(exception_class)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | void AssertPrimitiveClass(const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 79 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 80 | Thread* self = Thread::Current(); |
| 81 | AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str())); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 84 | void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 85 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(primitive != nullptr); |
| 87 | ASSERT_TRUE(primitive->GetClass() != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 88 | ASSERT_OBJ_PTR_EQ(primitive->GetClass(), primitive->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 90 | std::string temp; |
| 91 | ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp)); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(primitive->GetSuperClass() == nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 93 | EXPECT_FALSE(primitive->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(primitive->GetClassLoader() == nullptr); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 95 | EXPECT_EQ(ClassStatus::kVisiblyInitialized, primitive->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 96 | EXPECT_FALSE(primitive->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(primitive->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 98 | EXPECT_TRUE(primitive->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 99 | EXPECT_FALSE(primitive->IsErroneousResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 100 | EXPECT_TRUE(primitive->IsVerified()); |
| 101 | EXPECT_TRUE(primitive->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 102 | EXPECT_FALSE(primitive->IsArrayInstance()); |
| 103 | EXPECT_FALSE(primitive->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(primitive->GetComponentType() == nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 105 | EXPECT_FALSE(primitive->IsInterface()); |
| 106 | EXPECT_TRUE(primitive->IsPublic()); |
| 107 | EXPECT_TRUE(primitive->IsFinal()); |
| 108 | EXPECT_TRUE(primitive->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 109 | EXPECT_FALSE(primitive->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 110 | EXPECT_EQ(0U, primitive->NumDirectMethods()); |
| 111 | EXPECT_EQ(0U, primitive->NumVirtualMethods()); |
| 112 | EXPECT_EQ(0U, primitive->NumInstanceFields()); |
| 113 | EXPECT_EQ(0U, primitive->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 114 | EXPECT_EQ(0U, primitive->NumDirectInterfaces()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 115 | EXPECT_FALSE(primitive->HasVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 116 | EXPECT_EQ(0, primitive->GetIfTableCount()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 117 | EXPECT_TRUE(primitive->GetIfTable() != nullptr); |
| 118 | EXPECT_EQ(primitive->GetIfTable()->Count(), 0u); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 119 | EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 122 | void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 123 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 124 | ASSERT_TRUE(JavaLangObject != nullptr); |
| 125 | ASSERT_TRUE(JavaLangObject->GetClass() != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 126 | ASSERT_OBJ_PTR_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 127 | EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 128 | std::string temp; |
| 129 | ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;"); |
| 130 | EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr); |
| 131 | EXPECT_FALSE(JavaLangObject->HasSuperClass()); |
| 132 | EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 133 | class_linker_->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true); |
| 134 | EXPECT_EQ(ClassStatus::kVisiblyInitialized, JavaLangObject->GetStatus()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 135 | EXPECT_FALSE(JavaLangObject->IsErroneous()); |
| 136 | EXPECT_TRUE(JavaLangObject->IsLoaded()); |
| 137 | EXPECT_TRUE(JavaLangObject->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 138 | EXPECT_FALSE(JavaLangObject->IsErroneousResolved()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 139 | EXPECT_TRUE(JavaLangObject->IsVerified()); |
| 140 | EXPECT_TRUE(JavaLangObject->IsInitialized()); |
| 141 | EXPECT_FALSE(JavaLangObject->IsArrayInstance()); |
| 142 | EXPECT_FALSE(JavaLangObject->IsArrayClass()); |
| 143 | EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr); |
| 144 | EXPECT_FALSE(JavaLangObject->IsInterface()); |
| 145 | EXPECT_TRUE(JavaLangObject->IsPublic()); |
| 146 | EXPECT_FALSE(JavaLangObject->IsFinal()); |
| 147 | EXPECT_FALSE(JavaLangObject->IsPrimitive()); |
| 148 | EXPECT_FALSE(JavaLangObject->IsSynthetic()); |
Nicolas Geoffray | 6e311ac | 2017-02-27 11:46:03 +0000 | [diff] [blame] | 149 | EXPECT_EQ(4U, JavaLangObject->NumDirectMethods()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 150 | EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods()); |
Vladimir Marko | fd846c5 | 2021-10-19 07:54:13 +0100 | [diff] [blame] | 151 | EXPECT_EQ(2U, JavaLangObject->NumInstanceFields()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 152 | EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), |
| 153 | "shadow$_klass_"); |
| 154 | EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), |
| 155 | "shadow$_monitor_"); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 156 | |
| 157 | EXPECT_EQ(0U, JavaLangObject->NumStaticFields()); |
| 158 | EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces()); |
| 159 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 160 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 161 | ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod(); |
| 162 | ImTable* imt = JavaLangObject->GetImt(pointer_size); |
| 163 | ASSERT_NE(nullptr, imt); |
| 164 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 165 | ASSERT_EQ(unimplemented, imt->Get(i, pointer_size)); |
| 166 | } |
| 167 | } |
| 168 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 169 | void AssertArrayClass(const std::string& array_descriptor, |
| 170 | const std::string& component_type, |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 171 | ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 172 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 173 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 174 | StackHandleScope<2> hs(self); |
| 175 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
| 176 | Handle<mirror::Class> array( |
| 177 | hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader))); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 178 | std::string temp; |
| 179 | EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 180 | EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader()); |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 181 | EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract))); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 182 | AssertArrayClass(array_descriptor, array); |
| 183 | } |
| 184 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 185 | void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 186 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 187 | ASSERT_TRUE(array != nullptr); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(array->GetClass() != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 189 | ASSERT_OBJ_PTR_EQ(array->GetClass(), array->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 190 | EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 191 | std::string temp; |
| 192 | ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp)); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 193 | EXPECT_TRUE(array->GetSuperClass() != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 194 | Thread* self = Thread::Current(); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 195 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), |
| 196 | array->GetSuperClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 197 | EXPECT_TRUE(array->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 198 | ASSERT_TRUE(array->GetComponentType() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 199 | ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 200 | EXPECT_EQ(ClassStatus::kVisiblyInitialized, array->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 201 | EXPECT_FALSE(array->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 202 | EXPECT_TRUE(array->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(array->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 204 | EXPECT_FALSE(array->IsErroneousResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 205 | EXPECT_TRUE(array->IsVerified()); |
| 206 | EXPECT_TRUE(array->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 207 | EXPECT_FALSE(array->IsArrayInstance()); |
| 208 | EXPECT_TRUE(array->IsArrayClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 209 | EXPECT_FALSE(array->IsInterface()); |
| 210 | EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic()); |
| 211 | EXPECT_TRUE(array->IsFinal()); |
| 212 | EXPECT_FALSE(array->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 213 | EXPECT_FALSE(array->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 214 | EXPECT_EQ(0U, array->NumDirectMethods()); |
| 215 | EXPECT_EQ(0U, array->NumVirtualMethods()); |
| 216 | EXPECT_EQ(0U, array->NumInstanceFields()); |
| 217 | EXPECT_EQ(0U, array->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 218 | EXPECT_EQ(2U, array->NumDirectInterfaces()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 219 | EXPECT_TRUE(array->ShouldHaveImt()); |
| 220 | EXPECT_TRUE(array->ShouldHaveEmbeddedVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 221 | EXPECT_EQ(2, array->GetIfTableCount()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 222 | ASSERT_TRUE(array->GetIfTable() != nullptr); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 223 | ObjPtr<mirror::Class> direct_interface0 = array->GetDirectInterface(0); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(direct_interface0 != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 225 | EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;"); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 226 | ObjPtr<mirror::Class> direct_interface1 = array->GetDirectInterface(1); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 227 | EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;"); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 228 | EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), 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(); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 231 | ObjPtr<mirror::Class> JavaLangObject = |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 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 | } |
| 244 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 245 | void AssertField(ObjPtr<mirror::Class> klass, ArtField* field) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 246 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 247 | EXPECT_TRUE(field != nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 248 | EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(field->GetName() != nullptr); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 250 | EXPECT_TRUE(field->ResolveType() != nullptr); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 253 | void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 254 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 255 | std::string temp; |
| 256 | EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 257 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 258 | EXPECT_FALSE(klass->HasSuperClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 259 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 260 | EXPECT_TRUE(klass->HasSuperClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 261 | EXPECT_TRUE(klass->GetSuperClass() != nullptr); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 262 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 263 | EXPECT_TRUE(klass->GetClass() != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 264 | EXPECT_OBJ_PTR_EQ(klass->GetClass(), klass->GetClass()->GetClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 265 | EXPECT_TRUE(klass->GetDexCache() != nullptr); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 266 | EXPECT_TRUE(klass->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 268 | EXPECT_FALSE(klass->IsErroneousResolved()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 269 | EXPECT_FALSE(klass->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 270 | EXPECT_FALSE(klass->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 271 | EXPECT_TRUE(klass->GetComponentType() == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 272 | EXPECT_TRUE(klass->IsInSamePackage(klass.Get())); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 273 | std::string temp2; |
| 274 | EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp), |
| 275 | klass->GetDescriptor(&temp2))); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 276 | if (klass->IsInterface()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 277 | EXPECT_TRUE(klass->IsAbstract()); |
Alex Light | 42d511d | 2016-02-03 13:45:41 -0800 | [diff] [blame] | 278 | // 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] | 279 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
Alex Light | 42d511d | 2016-02-03 13:45:41 -0800 | [diff] [blame] | 280 | EXPECT_TRUE(m.IsStatic()); |
| 281 | EXPECT_TRUE(m.IsDirect()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 282 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 283 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 284 | if (!klass->IsSynthetic()) { |
| 285 | EXPECT_NE(0U, klass->NumDirectMethods()); |
| 286 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 287 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 288 | EXPECT_EQ(klass->IsInterface(), !klass->HasVTable()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 289 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 290 | for (int i = 0; i < klass->GetIfTableCount(); i++) { |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 291 | ObjPtr<mirror::Class> interface = iftable->GetInterface(i); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 292 | ASSERT_TRUE(interface != nullptr); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 293 | if (klass->IsInterface()) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 294 | EXPECT_EQ(0U, iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 295 | } else { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 296 | EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 299 | if (klass->IsAbstract()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 300 | EXPECT_FALSE(klass->IsFinal()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 301 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 302 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 303 | } |
| 304 | if (klass->IsFinal()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 305 | EXPECT_FALSE(klass->IsAbstract()); |
| 306 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 307 | } |
| 308 | if (klass->IsAnnotation()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 309 | EXPECT_FALSE(klass->IsFinal()); |
| 310 | EXPECT_TRUE(klass->IsAbstract()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | EXPECT_FALSE(klass->IsPrimitive()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 314 | EXPECT_TRUE(klass->CanAccess(klass.Get())); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 315 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 316 | for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 317 | AssertMethod(&method); |
| 318 | EXPECT_TRUE(method.IsDirect()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 319 | EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 322 | for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 323 | AssertMethod(&method); |
| 324 | EXPECT_FALSE(method.IsDirect()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 325 | EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 328 | for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 329 | AssertMethod(&method); |
| 330 | EXPECT_FALSE(method.IsDirect()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 331 | EXPECT_TRUE(method.IsCopied()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 332 | EXPECT_TRUE(method.GetDeclaringClass()->IsInterface()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 333 | << "declaring class: " << method.GetDeclaringClass()->PrettyClass(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 334 | EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get())) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 335 | << "declaring class: " << method.GetDeclaringClass()->PrettyClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 339 | ArtField* field = klass->GetInstanceField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 340 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 341 | EXPECT_FALSE(field->IsStatic()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | for (size_t i = 0; i < klass->NumStaticFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 345 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 346 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 347 | EXPECT_TRUE(field->IsStatic()); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 348 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 349 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 350 | // Confirm that all instances field offsets are packed together at the start. |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 351 | EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 352 | MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset(); |
| 353 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 354 | klass->NumReferenceInstanceFields() * |
| 355 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 356 | MemberOffset current_ref_offset = start_ref_offset; |
| 357 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 358 | ArtField* field = klass->GetInstanceField(i); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 359 | ObjPtr<mirror::Class> field_type = field->ResolveType(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 360 | ASSERT_TRUE(field_type != nullptr); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 361 | if (!field->IsPrimitiveType()) { |
| 362 | ASSERT_TRUE(!field_type->IsPrimitive()); |
| 363 | ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value()); |
| 364 | if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) { |
| 365 | // While Reference.referent is not primitive, the ClassLinker |
| 366 | // 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] | 367 | EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent"); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 368 | } else { |
| 369 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 370 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 371 | } |
| 372 | } else { |
| 373 | if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) { |
| 374 | // Shuffled before references. |
| 375 | ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value()); |
| 376 | CHECK(!IsAligned<4>(field->GetOffset().Uint32Value())); |
| 377 | } |
Brian Carlstrom | fbfdce6 | 2011-10-05 17:33:32 -0700 | [diff] [blame] | 378 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 379 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 380 | ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 381 | |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 382 | uint32_t total_num_reference_instance_fields = 0; |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 383 | ObjPtr<mirror::Class> k = klass.Get(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 384 | while (k != nullptr) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 385 | total_num_reference_instance_fields += k->NumReferenceInstanceFields(); |
| 386 | k = k->GetSuperClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 387 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 388 | EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class. |
| 389 | if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) { |
| 390 | // The reference instance offsets have a bit set for each reference offset. |
| 391 | // +1 for Object's class. |
| 392 | EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1, |
| 393 | total_num_reference_instance_fields); |
| 394 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 397 | void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 398 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | e512556 | 2019-02-06 17:38:26 +0000 | [diff] [blame] | 399 | ASSERT_FALSE(descriptor.empty()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 400 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 401 | StackHandleScope<1> hs(self); |
| 402 | Handle<mirror::Class> klass( |
| 403 | hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 404 | ASSERT_TRUE(klass != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 405 | std::string temp; |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 406 | EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp)); |
| 407 | EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader()); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 408 | if (klass->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 409 | AssertPrimitiveClass(descriptor, klass.Get()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 410 | } else if (klass->IsArrayClass()) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 411 | AssertArrayClass(descriptor, klass); |
| 412 | } else { |
| 413 | AssertClass(descriptor, klass); |
| 414 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 417 | void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 418 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 419 | // Verify all the classes defined in this file |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 420 | for (size_t i = 0; i < dex.NumClassDefs(); i++) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 421 | const dex::ClassDef& class_def = dex.GetClassDef(i); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 422 | const char* descriptor = dex.GetClassDescriptor(class_def); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 423 | AssertDexFileClass(class_loader, descriptor); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 424 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 425 | // Verify all the types referenced by this file |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 426 | for (size_t i = 0; i < dex.NumTypeIds(); i++) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 427 | const dex::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i)); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 428 | const char* descriptor = dex.GetTypeDescriptor(type_id); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 429 | AssertDexFileClass(class_loader, descriptor); |
| 430 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 431 | TestRootVisitor visitor; |
| 432 | class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 433 | } |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 434 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 435 | class TestRootVisitor : public SingleRootVisitor { |
| 436 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 437 | void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 438 | EXPECT_TRUE(root != nullptr); |
| 439 | } |
| 440 | }; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 441 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 442 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 443 | class ClassLinkerMethodHandlesTest : public ClassLinkerTest { |
| 444 | protected: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 445 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 446 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 447 | } |
| 448 | }; |
| 449 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 450 | struct CheckOffset { |
| 451 | size_t cpp_offset; |
| 452 | const char* java_name; |
| 453 | CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {} |
| 454 | }; |
| 455 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 456 | template <typename T> |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 457 | struct CheckOffsets { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 458 | CheckOffsets(bool is_static_in, const char* class_descriptor_in) |
| 459 | : is_static(is_static_in), class_descriptor(class_descriptor_in) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 460 | bool is_static; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 461 | std::string class_descriptor; |
| 462 | std::vector<CheckOffset> offsets; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 463 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 464 | bool Check() REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 465 | Thread* self = Thread::Current(); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 466 | ObjPtr<mirror::Class> klass = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 467 | Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 468 | CHECK(klass != nullptr) << class_descriptor; |
Alex Light | f1b18fa | 2019-11-11 14:22:00 -0800 | [diff] [blame] | 469 | CHECK(klass->IsMirrored()) << class_descriptor; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 470 | |
| 471 | bool error = false; |
| 472 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 473 | // Classes have a different size due to padding field. Strings are variable length. |
| 474 | if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 475 | // The RoundUp is required for some mirror classes that have a gap at the end, |
| 476 | // such as AccessibleObject, ByteArrayViewVarHandle and ByteBufferViewVarHandle. |
| 477 | // For example, the AccessibleObject has size 9 according to the class linker. |
| 478 | // However, the C++ sizeof(AccessibleObject) is 12 bytes due to alignment, even |
| 479 | // though members in C++ subclasses are actually starting at offset 9. |
| 480 | // |
| 481 | // TODO: We could define a subclass with a `uint8_t` member and check its offset instead. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 482 | static constexpr size_t kPackAlignment = 4; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 483 | size_t expected_size = RoundUp(klass->GetObjectSize(), kPackAlignment); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 484 | if (sizeof(T) != expected_size) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 485 | LOG(ERROR) << "Class size mismatch:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 486 | << " class=" << class_descriptor |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 487 | << " Java=" << expected_size |
| 488 | << " C++=" << sizeof(T); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 489 | error = true; |
| 490 | } |
| 491 | } |
| 492 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 493 | size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 494 | if (offsets.size() != num_fields) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 495 | LOG(ERROR) << "Field count mismatch:" |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 496 | << " class=" << class_descriptor |
| 497 | << " Java=" << num_fields |
| 498 | << " C++=" << offsets.size(); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 499 | error = true; |
| 500 | } |
| 501 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 502 | for (size_t i = 0; i < offsets.size(); i++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 503 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 504 | std::string_view field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 505 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 506 | error = true; |
| 507 | } |
| 508 | } |
| 509 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 510 | for (size_t i = 0; i < offsets.size(); i++) { |
| 511 | CheckOffset& offset = offsets[i]; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 512 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 513 | std::string_view field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 514 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 515 | LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 516 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 517 | LOG(ERROR) << "Java field order:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 518 | << " i=" << i << " class=" << class_descriptor |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 519 | << " Java=" << field_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 520 | << " CheckOffsets=" << offset.java_name; |
| 521 | } |
| 522 | } |
| 523 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 524 | for (size_t i = 0; i < offsets.size(); i++) { |
| 525 | CheckOffset& offset = offsets[i]; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 526 | ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 527 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
| 528 | error = true; |
| 529 | } |
| 530 | } |
| 531 | if (error) { |
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) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 536 | LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 537 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 538 | LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 539 | << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return !error; |
| 544 | }; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 545 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 546 | void addOffset(size_t offset, const char* name) { |
| 547 | offsets.push_back(CheckOffset(offset, name)); |
| 548 | } |
| 549 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 550 | private: |
| 551 | DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 552 | }; |
| 553 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 554 | // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses |
| 555 | // is first since if it is failing, others are unlikely to succeed. |
| 556 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 557 | struct ObjectOffsets : public CheckOffsets<mirror::Object> { |
| 558 | ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 559 | addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_"); |
| 560 | addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 561 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 564 | struct ClassOffsets : public CheckOffsets<mirror::Class> { |
| 565 | ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 566 | addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags"); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 567 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 568 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader"); |
| 569 | addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize"); |
| 570 | addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId"); |
| 571 | addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 572 | addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 573 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 574 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex"); |
| 575 | addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex"); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 576 | addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 577 | addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields"); |
| 578 | addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 579 | addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 580 | addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 581 | addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), |
| 582 | "numReferenceInstanceFields"); |
| 583 | addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), |
| 584 | "numReferenceStaticFields"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 585 | addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize"); |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 586 | addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_), |
| 587 | "objectSizeAllocFastPath"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 588 | addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType"); |
| 589 | addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), |
| 590 | "referenceInstanceOffsets"); |
| 591 | addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields"); |
| 592 | addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status"); |
| 593 | addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass"); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 594 | addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 595 | addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 596 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 597 | }; |
| 598 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 599 | struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> { |
| 600 | ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 601 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, erroneous_state_error_), "erroneousStateError"); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 602 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, instance_jfield_ids_), "instanceJfieldIDs"); |
| 603 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, jmethod_ids_), "jmethodIDs"); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 604 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_class_), "obsoleteClass"); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 605 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches"); |
| 606 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods"); |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 607 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile"); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 608 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_), |
| 609 | "preRedefineClassDefIndex"); |
| 610 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_), |
| 611 | "preRedefineDexFilePtr"); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 612 | addOffset(OFFSETOF_MEMBER(mirror::ClassExt, static_jfield_ids_), "staticJfieldIDs"); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 613 | } |
| 614 | }; |
| 615 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 616 | struct StringOffsets : public CheckOffsets<mirror::String> { |
| 617 | StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 618 | addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count"); |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 619 | addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 620 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 623 | struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> { |
| 624 | ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") { |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 625 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 626 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause"); |
| 627 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 628 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace"); |
| 629 | addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 630 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 631 | }; |
| 632 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 633 | struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 634 | StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>( |
| 635 | false, "Ljava/lang/StackTraceElement;") { |
| 636 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"); |
| 637 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName"); |
| 638 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber"); |
| 639 | addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 640 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 641 | }; |
| 642 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 643 | struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> { |
| 644 | ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 645 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator"); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 646 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 647 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages"); |
| 648 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent"); |
| 649 | addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 650 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 651 | }; |
| 652 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 653 | struct ProxyOffsets : public CheckOffsets<mirror::Proxy> { |
| 654 | ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 655 | addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 656 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 657 | }; |
| 658 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 659 | struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> { |
| 660 | DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") { |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 661 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, class_loader_), "classLoader"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 662 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile"); |
| 663 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location"); |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 664 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 665 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites"); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 666 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields"); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 667 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes"); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 668 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods"); |
| 669 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes"); |
| 670 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings"); |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 671 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 672 | addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites"); |
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"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 678 | } |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 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"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 687 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 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"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 696 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 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"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 703 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 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"); |
Alex Light | 5e251ea | 2020-01-09 10:55:49 -0800 | [diff] [blame] | 709 | addOffset(OFFSETOF_MEMBER(mirror::Field, art_field_index_), "artFieldIndex"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 710 | addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 711 | addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset"); |
| 712 | addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 713 | } |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 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"); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 728 | } |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 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 | |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 742 | struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> { |
| 743 | MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>( |
| 744 | false, "Ljava/lang/invoke/MethodHandle;") { |
| 745 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod"); |
Orion Hodson | b08e115 | 2022-01-05 14:29:05 +0000 | [diff] [blame] | 746 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, as_type_cache_), "asTypeCache"); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 747 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_), |
| 748 | "cachedSpreadInvoker"); |
| 749 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind"); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 750 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type"); |
| 751 | } |
| 752 | }; |
| 753 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 754 | struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> { |
| 755 | MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>( |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 756 | false, "Ljava/lang/invoke/MethodHandleImpl;") { |
| 757 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info"); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 758 | } |
| 759 | }; |
| 760 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 761 | struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> { |
| 762 | MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>( |
| 763 | false, "Ljava/lang/invoke/MethodHandles$Lookup;") { |
| 764 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes"); |
| 765 | addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass"); |
| 766 | } |
| 767 | }; |
| 768 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 769 | struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> { |
| 770 | EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>( |
| 771 | false, "Ldalvik/system/EmulatedStackFrame;") { |
| 772 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references"); |
| 773 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame"); |
| 774 | addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type"); |
| 775 | } |
| 776 | }; |
| 777 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 778 | struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> { |
| 779 | CallSiteOffsets() : CheckOffsets<mirror::CallSite>( |
| 780 | false, "Ljava/lang/invoke/CallSite;") { |
| 781 | addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target"); |
| 782 | } |
| 783 | }; |
| 784 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 785 | struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> { |
| 786 | VarHandleOffsets() : CheckOffsets<mirror::VarHandle>( |
| 787 | false, "Ljava/lang/invoke/VarHandle;") { |
| 788 | addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask"); |
| 789 | addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0"); |
| 790 | addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1"); |
| 791 | addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType"); |
| 792 | } |
| 793 | }; |
| 794 | |
| 795 | struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> { |
| 796 | FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>( |
| 797 | false, "Ljava/lang/invoke/FieldVarHandle;") { |
| 798 | addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField"); |
| 799 | } |
| 800 | }; |
| 801 | |
Orion Hodson | dd41196 | 2021-06-25 08:55:22 +0100 | [diff] [blame] | 802 | struct StaticFieldVarHandleOffsets : public CheckOffsets<mirror::StaticFieldVarHandle> { |
| 803 | StaticFieldVarHandleOffsets() : CheckOffsets<mirror::StaticFieldVarHandle>( |
| 804 | false, "Ljava/lang/invoke/StaticFieldVarHandle;") { |
| 805 | addOffset(OFFSETOF_MEMBER(mirror::StaticFieldVarHandle, declaring_class_), "declaringClass"); |
| 806 | } |
| 807 | }; |
| 808 | |
Orion Hodson | ffad668 | 2018-01-02 10:18:58 +0000 | [diff] [blame] | 809 | struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> { |
| 810 | ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>( |
| 811 | false, "Ljava/lang/invoke/ArrayElementVarHandle;") { |
| 812 | } |
| 813 | }; |
| 814 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 815 | struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> { |
| 816 | ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>( |
| 817 | false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") { |
| 818 | addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_), |
| 819 | "nativeByteOrder"); |
| 820 | } |
| 821 | }; |
| 822 | |
| 823 | struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> { |
| 824 | ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>( |
| 825 | false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") { |
| 826 | addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_), |
| 827 | "nativeByteOrder"); |
| 828 | } |
| 829 | }; |
| 830 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 831 | // 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] | 832 | // 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] | 833 | // ClassLinker::LinkFields. |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 834 | TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 835 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 836 | EXPECT_TRUE(ObjectOffsets().Check()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 837 | EXPECT_TRUE(ClassOffsets().Check()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 838 | EXPECT_TRUE(ClassExtOffsets().Check()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 839 | EXPECT_TRUE(StringOffsets().Check()); |
| 840 | EXPECT_TRUE(ThrowableOffsets().Check()); |
| 841 | EXPECT_TRUE(StackTraceElementOffsets().Check()); |
| 842 | EXPECT_TRUE(ClassLoaderOffsets().Check()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 843 | EXPECT_TRUE(ProxyOffsets().Check()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 844 | EXPECT_TRUE(DexCacheOffsets().Check()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 845 | EXPECT_TRUE(ReferenceOffsets().Check()); |
| 846 | EXPECT_TRUE(FinalizerReferenceOffsets().Check()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 847 | EXPECT_TRUE(AccessibleObjectOffsets().Check()); |
| 848 | EXPECT_TRUE(FieldOffsets().Check()); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 849 | EXPECT_TRUE(ExecutableOffsets().Check()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 850 | EXPECT_TRUE(MethodTypeOffsets().Check()); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 851 | EXPECT_TRUE(MethodHandleOffsets().Check()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 852 | EXPECT_TRUE(MethodHandleImplOffsets().Check()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 853 | EXPECT_TRUE(MethodHandlesLookupOffsets().Check()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 854 | EXPECT_TRUE(EmulatedStackFrameOffsets().Check()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 855 | EXPECT_TRUE(CallSiteOffsets().Check()); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 856 | EXPECT_TRUE(VarHandleOffsets().Check()); |
| 857 | EXPECT_TRUE(FieldVarHandleOffsets().Check()); |
Orion Hodson | ffad668 | 2018-01-02 10:18:58 +0000 | [diff] [blame] | 858 | EXPECT_TRUE(ArrayElementVarHandleOffsets().Check()); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 859 | EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check()); |
| 860 | EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 863 | TEST_F(ClassLinkerTest, FindClassNonexistent) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 864 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 865 | AssertNonExistentClass("NoSuchClass;"); |
| 866 | AssertNonExistentClass("LNoSuchClass;"); |
| 867 | } |
| 868 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 869 | TEST_F(ClassLinkerTest, GetDexFiles) { |
| 870 | ScopedObjectAccess soa(Thread::Current()); |
| 871 | |
| 872 | jobject jclass_loader = LoadDex("Nested"); |
| 873 | std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader)); |
| 874 | ASSERT_EQ(dex_files.size(), 1U); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 875 | EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar")); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 876 | |
| 877 | jobject jclass_loader2 = LoadDex("MultiDex"); |
| 878 | std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2)); |
| 879 | ASSERT_EQ(dex_files2.size(), 2U); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 880 | EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar")); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 883 | TEST_F(ClassLinkerTest, FindClassNested) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 884 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 885 | StackHandleScope<1> hs(soa.Self()); |
| 886 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 887 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 888 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 889 | ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 890 | ASSERT_TRUE(outer != nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 891 | EXPECT_EQ(0U, outer->NumVirtualMethods()); |
| 892 | EXPECT_EQ(1U, outer->NumDirectMethods()); |
| 893 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 894 | ObjPtr<mirror::Class> inner = |
| 895 | class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 896 | ASSERT_TRUE(inner != nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 897 | EXPECT_EQ(0U, inner->NumVirtualMethods()); |
| 898 | EXPECT_EQ(1U, inner->NumDirectMethods()); |
| 899 | } |
| 900 | |
| 901 | TEST_F(ClassLinkerTest, FindClass_Primitives) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 902 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 903 | const std::string expected("BCDFIJSZV"); |
Elliott Hughes | db7d5e9 | 2011-12-16 18:47:37 -0800 | [diff] [blame] | 904 | for (int ch = 1; ch < 256; ++ch) { |
| 905 | std::string descriptor; |
| 906 | descriptor.push_back(ch); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 907 | if (expected.find(ch) == std::string::npos) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 908 | AssertNonExistentClass(descriptor); |
| 909 | } else { |
| 910 | AssertPrimitiveClass(descriptor); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | TEST_F(ClassLinkerTest, FindClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 916 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 917 | StackHandleScope<2> hs(soa.Self()); |
| 918 | Handle<mirror::Class> JavaLangObject = hs.NewHandle( |
| 919 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
| 920 | AssertObjectClass(JavaLangObject.Get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 921 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 922 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 923 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 924 | AssertNonExistentClass("LMyClass;"); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 925 | ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 926 | ASSERT_TRUE(MyClass != nullptr); |
| 927 | ASSERT_TRUE(MyClass->GetClass() != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 928 | ASSERT_OBJ_PTR_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 929 | EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 930 | std::string temp; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 931 | ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;"); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 932 | EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 933 | EXPECT_TRUE(MyClass->HasSuperClass()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 934 | EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 935 | EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 936 | EXPECT_FALSE(MyClass->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 937 | EXPECT_TRUE(MyClass->IsLoaded()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 938 | EXPECT_TRUE(MyClass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 939 | EXPECT_FALSE(MyClass->IsErroneousResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 940 | EXPECT_FALSE(MyClass->IsVerified()); |
| 941 | EXPECT_FALSE(MyClass->IsInitialized()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 942 | EXPECT_FALSE(MyClass->IsArrayInstance()); |
| 943 | EXPECT_FALSE(MyClass->IsArrayClass()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 944 | EXPECT_TRUE(MyClass->GetComponentType() == nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 945 | EXPECT_FALSE(MyClass->IsInterface()); |
| 946 | EXPECT_FALSE(MyClass->IsPublic()); |
| 947 | EXPECT_FALSE(MyClass->IsFinal()); |
| 948 | EXPECT_FALSE(MyClass->IsPrimitive()); |
| 949 | EXPECT_FALSE(MyClass->IsSynthetic()); |
| 950 | EXPECT_EQ(1U, MyClass->NumDirectMethods()); |
| 951 | EXPECT_EQ(0U, MyClass->NumVirtualMethods()); |
| 952 | EXPECT_EQ(0U, MyClass->NumInstanceFields()); |
| 953 | EXPECT_EQ(0U, MyClass->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 954 | EXPECT_EQ(0U, MyClass->NumDirectInterfaces()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 955 | |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 956 | EXPECT_OBJ_PTR_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 957 | |
| 958 | // created by class_linker |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 959 | AssertArrayClass("[C", "C", nullptr); |
| 960 | AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 961 | // synthesized on the fly |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 962 | AssertArrayClass("[[C", "[C", nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 963 | AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 964 | // or not available at all |
| 965 | AssertNonExistentClass("[[[[LNonExistentClass;"); |
| 966 | } |
| 967 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 968 | TEST_F(ClassLinkerTest, LookupResolvedType) { |
| 969 | ScopedObjectAccess soa(Thread::Current()); |
| 970 | StackHandleScope<1> hs(soa.Self()); |
| 971 | Handle<mirror::ClassLoader> class_loader( |
| 972 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
| 973 | AssertNonExistentClass("LMyClass;"); |
| 974 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 975 | dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 976 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 977 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 978 | class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()), |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 979 | klass); |
| 980 | // Zero out the resolved type and make sure LookupResolvedType still finds it. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 981 | dex_cache->ClearResolvedType(type_idx); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 982 | EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr); |
| 983 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 984 | class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()), |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 985 | klass); |
| 986 | } |
| 987 | |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 988 | TEST_F(ClassLinkerTest, LookupResolvedTypeArray) { |
| 989 | ScopedObjectAccess soa(Thread::Current()); |
| 990 | StackHandleScope<2> hs(soa.Self()); |
| 991 | Handle<mirror::ClassLoader> class_loader( |
| 992 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields")))); |
| 993 | // Get the AllFields class for the dex cache and dex file. |
| 994 | ObjPtr<mirror::Class> all_fields_klass |
| 995 | = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader); |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 996 | ASSERT_TRUE(all_fields_klass != nullptr); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 997 | Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache()); |
| 998 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 999 | // Get the index of the array class we want to test. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1000 | const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;"); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1001 | ASSERT_TRUE(array_id != nullptr); |
| 1002 | dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id); |
| 1003 | // Check that the array class wasn't resolved yet. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1004 | EXPECT_TRUE( |
| 1005 | class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1006 | // Resolve the array class we want to test. |
| 1007 | ObjPtr<mirror::Class> array_klass |
| 1008 | = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader); |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1009 | ASSERT_TRUE(array_klass != nullptr); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1010 | // Test that LookupResolvedType() finds the array class. |
| 1011 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1012 | class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1013 | array_klass); |
| 1014 | // Zero out the resolved type and make sure LookupResolvedType() still finds it. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1015 | dex_cache->ClearResolvedType(array_idx); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1016 | EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr); |
| 1017 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1018 | class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1019 | array_klass); |
| 1020 | } |
| 1021 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1022 | TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) { |
| 1023 | ScopedObjectAccess soa(Thread::Current()); |
| 1024 | StackHandleScope<3> hs(soa.Self()); |
| 1025 | Handle<mirror::ClassLoader> class_loader( |
| 1026 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit")))); |
| 1027 | AssertNonExistentClass("LErroneousInit;"); |
| 1028 | Handle<mirror::Class> klass = |
| 1029 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader)); |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1030 | ASSERT_TRUE(klass != nullptr); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1031 | dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_; |
| 1032 | Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1033 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1034 | class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1035 | klass.Get()); |
| 1036 | // Zero out the resolved type and make sure LookupResolvedType still finds it. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1037 | dex_cache->ClearResolvedType(type_idx); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1038 | EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr); |
| 1039 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1040 | class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1041 | klass.Get()); |
| 1042 | // Force initialization to turn the class erroneous. |
| 1043 | bool initialized = class_linker_->EnsureInitialized(soa.Self(), |
| 1044 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1045 | /* can_init_fields= */ true, |
| 1046 | /* can_init_parents= */ true); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1047 | EXPECT_FALSE(initialized); |
| 1048 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
| 1049 | soa.Self()->ClearException(); |
| 1050 | // Check that the LookupResolvedType() can still find the resolved type. |
| 1051 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1052 | class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1053 | klass.Get()); |
| 1054 | // Zero out the resolved type and make sure LookupResolvedType() still finds it. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1055 | dex_cache->ClearResolvedType(type_idx); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1056 | EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr); |
| 1057 | EXPECT_OBJ_PTR_EQ( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1058 | class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()), |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1059 | klass.Get()); |
| 1060 | } |
| 1061 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1062 | TEST_F(ClassLinkerTest, LibCore) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1063 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 1064 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 1065 | AssertDexFile(*java_lang_dex_file_, nullptr); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1068 | // 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] | 1069 | // start of the object |
| 1070 | TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1071 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1072 | ObjPtr<mirror::Class> array_class = |
| 1073 | class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
| 1074 | ObjPtr<mirror::ObjectArray<mirror::String>> array = |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1075 | mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1076 | uintptr_t data_offset = |
| 1077 | reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>), |
| 1078 | 0)); |
| 1079 | if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1080 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment. |
| 1081 | } else { |
| 1082 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment. |
| 1083 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1087 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1088 | StackHandleScope<5> hs(soa.Self()); |
| 1089 | Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1090 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1091 | uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData()); |
| 1092 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment |
| 1093 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1094 | Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1095 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1096 | data_offset = reinterpret_cast<uintptr_t>(double_array->GetData()); |
| 1097 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment |
| 1098 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1099 | Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1100 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1101 | data_offset = reinterpret_cast<uintptr_t>(int_array->GetData()); |
| 1102 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment |
| 1103 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1104 | Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1105 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1106 | data_offset = reinterpret_cast<uintptr_t>(char_array->GetData()); |
| 1107 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment |
| 1108 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1109 | Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1110 | EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1111 | data_offset = reinterpret_cast<uintptr_t>(short_array->GetData()); |
| 1112 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment |
| 1113 | |
| 1114 | // Take it as given that bytes and booleans have byte alignment |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 1117 | TEST_F(ClassLinkerTest, ValidateBoxedTypes) { |
| 1118 | // Validate that the "value" field is always the 0th field in each of java.lang's box classes. |
| 1119 | // This lets UnboxPrimitive avoid searching for the field by name at runtime. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1120 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1121 | ScopedNullHandle<mirror::ClassLoader> class_loader; |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1122 | ObjPtr<mirror::Class> c; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1123 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1124 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1125 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1126 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1127 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1128 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1129 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1130 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1131 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1132 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1133 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1134 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1135 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1136 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1137 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1138 | EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1141 | TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1142 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1143 | StackHandleScope<3> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1144 | Handle<mirror::ClassLoader> class_loader_1( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1145 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1146 | Handle<mirror::ClassLoader> class_loader_2( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1147 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass")))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1148 | Handle<mirror::Class> MyClass_1 = hs.NewHandle( |
| 1149 | class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1)); |
| 1150 | ObjPtr<mirror::Class> MyClass_2 = |
| 1151 | class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1152 | EXPECT_TRUE(MyClass_1 != nullptr); |
| 1153 | EXPECT_TRUE(MyClass_2 != nullptr); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1154 | EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 1155 | } |
| 1156 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1157 | TEST_F(ClassLinkerTest, StaticFields) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1158 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1159 | StackHandleScope<2> hs(soa.Self()); |
| 1160 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1161 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1162 | Handle<mirror::Class> statics( |
| 1163 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1164 | class_linker_->EnsureInitialized(soa.Self(), statics, true, true); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1165 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1166 | // Static final primitives that are initialized by a compile-time constant |
| 1167 | // expression resolve to a copy of a constant value from the constant pool. |
| 1168 | // So <clinit> should be null. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1169 | ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1170 | EXPECT_TRUE(clinit == nullptr); |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1171 | |
| 1172 | EXPECT_EQ(9U, statics->NumStaticFields()); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1173 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1174 | ArtField* s0 = statics->FindStaticField("s0", "Z"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1175 | EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1176 | EXPECT_EQ(true, s0->GetBoolean(statics.Get())); |
| 1177 | s0->SetBoolean<false>(statics.Get(), false); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1178 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1179 | ArtField* s1 = statics->FindStaticField("s1", "B"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1180 | EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1181 | EXPECT_EQ(5, s1->GetByte(statics.Get())); |
| 1182 | s1->SetByte<false>(statics.Get(), 6); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1183 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1184 | ArtField* s2 = statics->FindStaticField("s2", "C"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1185 | EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1186 | EXPECT_EQ('a', s2->GetChar(statics.Get())); |
| 1187 | s2->SetChar<false>(statics.Get(), 'b'); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1188 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1189 | ArtField* s3 = statics->FindStaticField("s3", "S"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1190 | EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1191 | EXPECT_EQ(-536, s3->GetShort(statics.Get())); |
| 1192 | s3->SetShort<false>(statics.Get(), -535); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1193 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1194 | ArtField* s4 = statics->FindStaticField("s4", "I"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1195 | EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1196 | EXPECT_EQ(2000000000, s4->GetInt(statics.Get())); |
| 1197 | s4->SetInt<false>(statics.Get(), 2000000001); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1198 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1199 | ArtField* s5 = statics->FindStaticField("s5", "J"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1200 | EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1201 | EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get())); |
| 1202 | s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12)); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1203 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1204 | ArtField* s6 = statics->FindStaticField("s6", "F"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1205 | EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1206 | EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1207 | s6->SetFloat<false>(statics.Get(), 0.75); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1208 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1209 | ArtField* s7 = statics->FindStaticField("s7", "D"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1210 | EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1211 | EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1212 | s7->SetDouble<false>(statics.Get(), 16777219); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1213 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1214 | ArtField* s8 = statics->FindStaticField("s8", "Ljava/lang/String;"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1215 | EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1216 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android")); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1217 | ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1218 | s8->SetObject<false>(s8->GetDeclaringClass(), str_value); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1219 | |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1220 | // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ |
| 1221 | // http://code.google.com/p/googletest/issues/detail?id=322 |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1222 | EXPECT_FALSE(s0->GetBoolean(statics.Get())); |
| 1223 | EXPECT_EQ(6, s1->GetByte(statics.Get())); |
| 1224 | EXPECT_EQ('b', s2->GetChar(statics.Get())); |
| 1225 | EXPECT_EQ(-535, s3->GetShort(statics.Get())); |
| 1226 | EXPECT_EQ(2000000001, s4->GetInt(statics.Get())); |
| 1227 | EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get())); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1228 | EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get())); |
| 1229 | EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1230 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1233 | TEST_F(ClassLinkerTest, Interfaces) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1234 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1235 | StackHandleScope<6> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1236 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1237 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces")))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1238 | Handle<mirror::Class> I( |
| 1239 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 1240 | Handle<mirror::Class> J( |
| 1241 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
| 1242 | Handle<mirror::Class> K( |
| 1243 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader))); |
| 1244 | Handle<mirror::Class> A( |
| 1245 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader))); |
| 1246 | Handle<mirror::Class> B( |
| 1247 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader))); |
| 1248 | EXPECT_TRUE(I->IsAssignableFrom(A.Get())); |
| 1249 | EXPECT_TRUE(J->IsAssignableFrom(A.Get())); |
| 1250 | EXPECT_TRUE(J->IsAssignableFrom(K.Get())); |
| 1251 | EXPECT_TRUE(K->IsAssignableFrom(B.Get())); |
| 1252 | EXPECT_TRUE(J->IsAssignableFrom(B.Get())); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1253 | |
Vladimir Marko | eb37ba5 | 2019-02-05 14:10:38 +0000 | [diff] [blame] | 1254 | const std::string_view void_sig("()V"); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1255 | ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize); |
| 1256 | ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize); |
| 1257 | ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1258 | ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize); |
| 1259 | ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize); |
| 1260 | ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1261 | ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize); |
| 1262 | ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize); |
| 1263 | ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1264 | ASSERT_TRUE(Ii != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1265 | ASSERT_FALSE(Ii->IsDirect()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1266 | ASSERT_TRUE(Jj1 != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1267 | ASSERT_FALSE(Jj1->IsDirect()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1268 | ASSERT_TRUE(Jj2 != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1269 | ASSERT_FALSE(Jj2->IsDirect()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1270 | ASSERT_TRUE(Kj1 != nullptr); |
| 1271 | ASSERT_TRUE(Kj2 != nullptr); |
| 1272 | ASSERT_TRUE(Kk != nullptr); |
| 1273 | ASSERT_TRUE(Ai != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1274 | ASSERT_FALSE(Ai->IsDirect()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1275 | ASSERT_TRUE(Aj1 != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1276 | ASSERT_FALSE(Aj1->IsDirect()); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1277 | ASSERT_TRUE(Aj2 != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1278 | ASSERT_FALSE(Aj2->IsDirect()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1279 | EXPECT_NE(Ii, Ai); |
| 1280 | EXPECT_NE(Jj1, Aj1); |
| 1281 | EXPECT_NE(Jj2, Aj2); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 1282 | EXPECT_EQ(Kj1, Jj1); |
| 1283 | EXPECT_EQ(Kj2, Jj2); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1284 | EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize)); |
| 1285 | EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize)); |
| 1286 | EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize)); |
| 1287 | EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize)); |
| 1288 | EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize)); |
| 1289 | EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize)); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1290 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1291 | ArtField* Afoo = A->FindStaticField("foo", "Ljava/lang/String;"); |
| 1292 | ArtField* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;"); |
| 1293 | ArtField* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;"); |
| 1294 | ArtField* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1295 | ASSERT_TRUE(Afoo != nullptr); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 1296 | EXPECT_EQ(Afoo, Bfoo); |
| 1297 | EXPECT_EQ(Afoo, Jfoo); |
| 1298 | EXPECT_EQ(Afoo, Kfoo); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 1301 | TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) { |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1302 | // 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] | 1303 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 1304 | // case 1, get the uninitialized storage from StaticsFromCode.<clinit> |
| 1305 | // case 2, get the initialized storage from StaticsFromCode.getS0 |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1306 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1307 | ScopedObjectAccess soa(Thread::Current()); |
| 1308 | jobject jclass_loader = LoadDex("StaticsFromCode"); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 1309 | const DexFile* dex_file = GetFirstDexFile(jclass_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1310 | StackHandleScope<1> hs(soa.Self()); |
| 1311 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1312 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1313 | ObjPtr<mirror::Class> klass = |
| 1314 | class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1315 | ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1316 | ArtMethod* getS0 = |
| 1317 | klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize); |
| 1318 | ASSERT_TRUE(getS0 != nullptr); |
| 1319 | ASSERT_TRUE(getS0->IsStatic()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1320 | const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1321 | ASSERT_TRUE(type_id != nullptr); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1322 | dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 1323 | ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx, |
| 1324 | clinit, |
| 1325 | soa.Self(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1326 | /* can_run_clinit= */ true, |
| 1327 | /* verify_access= */ false); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1328 | EXPECT_TRUE(uninit != nullptr); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1329 | EXPECT_FALSE(uninit->IsInitialized()); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 1330 | ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx, |
| 1331 | getS0, |
| 1332 | soa.Self(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1333 | /* can_run_clinit= */ true, |
| 1334 | /* verify_access= */ false); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1335 | EXPECT_TRUE(init != nullptr); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1336 | EXPECT_TRUE(init->IsInitialized()); |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 1339 | TEST_F(ClassLinkerTest, ErroneousClass) { |
| 1340 | ScopedObjectAccess soa(Thread::Current()); |
| 1341 | jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB"); |
| 1342 | StackHandleScope<1> hs(soa.Self()); |
| 1343 | Handle<mirror::ClassLoader> class_loader( |
| 1344 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
| 1345 | hs.Self()->AssertNoPendingException(); |
| 1346 | const char* descriptor = "LErroneous;"; |
| 1347 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader); |
| 1348 | // Erronenous since we are extending final class. |
| 1349 | hs.Self()->AssertPendingException(); |
| 1350 | EXPECT_TRUE(klass == nullptr); |
| 1351 | klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get()); |
| 1352 | EXPECT_FALSE(klass == nullptr); |
| 1353 | EXPECT_TRUE(klass->IsErroneous()); |
| 1354 | EXPECT_TRUE(klass->GetIfTable() != nullptr); |
| 1355 | } |
| 1356 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1357 | TEST_F(ClassLinkerTest, FinalizableBit) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1358 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1359 | ObjPtr<mirror::Class> c; |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1360 | |
| 1361 | // Object has a finalize method, but we know it's empty. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1362 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1363 | EXPECT_FALSE(c->IsFinalizable()); |
| 1364 | |
| 1365 | // Enum has a finalize method to prevent its subclasses from implementing one. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1366 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1367 | EXPECT_FALSE(c->IsFinalizable()); |
| 1368 | |
| 1369 | // RoundingMode is an enum. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1370 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1371 | EXPECT_FALSE(c->IsFinalizable()); |
| 1372 | |
| 1373 | // RandomAccessFile extends Object and overrides finalize. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1374 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1375 | EXPECT_TRUE(c->IsFinalizable()); |
| 1376 | |
| 1377 | // FileInputStream is finalizable and extends InputStream which isn't. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1378 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1379 | EXPECT_FALSE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1380 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1381 | EXPECT_TRUE(c->IsFinalizable()); |
| 1382 | |
| 1383 | // ScheduledThreadPoolExecutor doesn't have a finalize method but |
| 1384 | // extends ThreadPoolExecutor which does. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1385 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1386 | EXPECT_TRUE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1387 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1388 | EXPECT_TRUE(c->IsFinalizable()); |
| 1389 | } |
| 1390 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1391 | TEST_F(ClassLinkerTest, ClassRootDescriptors) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1392 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1393 | std::string temp; |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1394 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1395 | ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1396 | EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1397 | EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1401 | TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) { |
| 1402 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1403 | ScopedNullHandle<mirror::ClassLoader> class_loader; |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1404 | ObjPtr<mirror::Class> c; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1405 | |
| 1406 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1407 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1408 | EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1409 | |
| 1410 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1411 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1412 | EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1413 | |
| 1414 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1415 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1416 | EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1417 | |
| 1418 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1419 | ASSERT_TRUE(c != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1420 | EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1423 | static void CheckMethod(ArtMethod* method, bool verified) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1424 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1425 | if (!method->IsNative() && !method->IsAbstract()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1426 | EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1427 | << method->PrettyMethod(true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1431 | static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1432 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1433 | for (auto& m : c->GetMethods(kRuntimePointerSize)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1434 | CheckMethod(&m, preverified); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | TEST_F(ClassLinkerTest, Preverified_InitializedBoot) { |
| 1439 | ScopedObjectAccess soa(Thread::Current()); |
| 1440 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1441 | ObjPtr<mirror::Class> JavaLangObject = |
| 1442 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 1443 | ASSERT_TRUE(JavaLangObject != nullptr); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1444 | EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the " |
| 1445 | "core"; |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1446 | CheckVerificationAttempted(JavaLangObject, true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) { |
| 1450 | ScopedObjectAccess soa(Thread::Current()); |
| 1451 | |
| 1452 | StackHandleScope<1> hs(soa.Self()); |
| 1453 | |
| 1454 | Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass( |
| 1455 | soa.Self(), "Ljava/lang/SecurityManager;"))); |
| 1456 | EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the " |
| 1457 | "core"; |
| 1458 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1459 | CheckVerificationAttempted(security_manager.Get(), false); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1460 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1461 | class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1462 | CheckVerificationAttempted(security_manager.Get(), true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | TEST_F(ClassLinkerTest, Preverified_App) { |
| 1466 | ScopedObjectAccess soa(Thread::Current()); |
| 1467 | |
| 1468 | StackHandleScope<2> hs(soa.Self()); |
| 1469 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1470 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1471 | Handle<mirror::Class> statics( |
| 1472 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
| 1473 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1474 | CheckVerificationAttempted(statics.Get(), false); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1475 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1476 | class_linker_->EnsureInitialized(soa.Self(), statics, true, true); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1477 | CheckVerificationAttempted(statics.Get(), true); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1480 | TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) { |
| 1481 | ScopedObjectAccess soa(Thread::Current()); |
| 1482 | |
| 1483 | StackHandleScope<3> hs(soa.Self()); |
| 1484 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1485 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics")))); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1486 | |
| 1487 | // java.lang.Object is a bootstrap class. |
| 1488 | Handle<mirror::Class> jlo_class( |
| 1489 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1490 | ASSERT_TRUE(jlo_class != nullptr); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1491 | EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded()); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1492 | |
| 1493 | // Statics is not a bootstrap class. |
| 1494 | Handle<mirror::Class> statics( |
| 1495 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1496 | ASSERT_TRUE(statics != nullptr); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1497 | EXPECT_FALSE(statics->IsBootStrapClassLoaded()); |
Sebastien Hertz | 6963e44 | 2014-11-26 22:11:27 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1500 | // Regression test for b/26799552. |
| 1501 | TEST_F(ClassLinkerTest, RegisterDexFileName) { |
| 1502 | ScopedObjectAccess soa(Thread::Current()); |
| 1503 | StackHandleScope<2> hs(soa.Self()); |
| 1504 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1505 | MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr)); |
| 1506 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1507 | ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 1508 | for (const auto& entry : class_linker->GetDexCachesData()) { |
| 1509 | const ClassLinker::DexCacheData& data = entry.second; |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1510 | dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1511 | if (dex_cache != nullptr) { |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1512 | break; |
| 1513 | } |
| 1514 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1515 | ASSERT_TRUE(dex_cache != nullptr); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1516 | } |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1517 | const uint16_t data[] = { 0x20AC, 0x20A1 }; |
| 1518 | Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(), |
| 1519 | arraysize(data), |
| 1520 | data))); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1521 | const DexFile* old_dex_file = dex_cache->GetDexFile(); |
| 1522 | |
David Sehr | 0b42677 | 2018-07-03 23:03:42 +0000 | [diff] [blame] | 1523 | std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(), |
| 1524 | old_dex_file->Size(), |
| 1525 | location->ToModifiedUtf8(), |
| 1526 | 0u, |
| 1527 | nullptr, |
| 1528 | nullptr)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1529 | // Make a copy of the dex cache with changed name. |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 1530 | dex_cache.Assign(class_linker->AllocAndInitializeDexCache(Thread::Current(), |
| 1531 | *dex_file, |
| 1532 | /* class_loader= */ nullptr)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1533 | DCHECK_EQ(dex_cache->GetLocation()->CompareTo(location.Get()), 0); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1534 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1535 | WriterMutexLock mu(soa.Self(), *Locks::dex_lock_); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1536 | // Check that inserting with a UTF16 name works. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1537 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr); |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1541 | TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) { |
| 1542 | ScopedObjectAccess soa(Thread::Current()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1543 | StackHandleScope<7> hs(soa.Self()); |
| 1544 | |
| 1545 | Handle<mirror::ClassLoader> class_loader( |
| 1546 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes")))); |
| 1547 | Handle<mirror::Class> method_types( |
| 1548 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader))); |
| 1549 | class_linker_->EnsureInitialized(soa.Self(), method_types, true, true); |
| 1550 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1551 | ArtMethod* method1 = method_types->FindClassMethod( |
| 1552 | "method1", |
| 1553 | "(Ljava/lang/String;)Ljava/lang/String;", |
| 1554 | kRuntimePointerSize); |
| 1555 | ASSERT_TRUE(method1 != nullptr); |
| 1556 | ASSERT_FALSE(method1->IsDirect()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1557 | |
| 1558 | const DexFile& dex_file = *(method1->GetDexFile()); |
| 1559 | Handle<mirror::DexCache> dex_cache = hs.NewHandle( |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 1560 | class_linker_->FindDexCache(soa.Self(), dex_file)); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1561 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1562 | const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1563 | |
| 1564 | // This is the MethodType corresponding to the prototype of |
| 1565 | // String MethodTypes# method1(String). |
| 1566 | // Its RType = Ljava/lang/String; |
Narayan Kamath | 42b3dd0 | 2016-10-03 11:33:01 +0100 | [diff] [blame] | 1567 | // Its PTypes = { Ljava/lang/String; } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1568 | Handle<mirror::MethodType> method1_type = hs.NewHandle( |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 1569 | class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader)); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1570 | |
| 1571 | // Assert that the method type was resolved successfully. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1572 | ASSERT_TRUE(method1_type != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1573 | |
| 1574 | // Assert that the return type and the method arguments are as we expect. |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 1575 | Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(), |
| 1576 | "Ljava/lang/String;", |
| 1577 | class_loader))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1578 | ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType()); |
| 1579 | ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0)); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1580 | |
| 1581 | // Resolve the method type again and assert that we get back the same value. |
| 1582 | Handle<mirror::MethodType> method1_type2 = hs.NewHandle( |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 1583 | class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1584 | ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1585 | |
| 1586 | // Resolve the MethodType associated with a different method signature |
| 1587 | // and assert it's different. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1588 | ArtMethod* method2 = method_types->FindClassMethod( |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1589 | "method2", |
| 1590 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 1591 | kRuntimePointerSize); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1592 | ASSERT_TRUE(method2 != nullptr); |
| 1593 | ASSERT_FALSE(method2->IsDirect()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1594 | const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1595 | Handle<mirror::MethodType> method2_type = hs.NewHandle( |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 1596 | class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1597 | ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1598 | } |
| 1599 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 1600 | // Verify that ClassLinker's CreateWellknownClassLoader works as expected |
| 1601 | // by creating a chain of class loaders with various dex files. |
| 1602 | TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) { |
| 1603 | // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one. |
| 1604 | // No need to check again. |
| 1605 | jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr); |
| 1606 | jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a); |
| 1607 | jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b); |
| 1608 | LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c); |
| 1609 | } |
| 1610 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1611 | TEST_F(ClassLinkerTest, PrettyClass) { |
| 1612 | ScopedObjectAccess soa(Thread::Current()); |
| 1613 | EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1614 | ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1615 | ASSERT_TRUE(c != nullptr); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1616 | ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1617 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass())); |
| 1618 | } |
| 1619 | |
| 1620 | TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) { |
| 1621 | ScopedObjectAccess soa(Thread::Current()); |
| 1622 | EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1623 | ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1624 | ASSERT_TRUE(c != nullptr); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1625 | ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1626 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", |
| 1627 | mirror::Class::PrettyClassAndClassLoader(o->GetClass())); |
| 1628 | } |
| 1629 | |
| 1630 | TEST_F(ClassLinkerTest, PrettyField) { |
| 1631 | ScopedObjectAccess soa(Thread::Current()); |
| 1632 | EXPECT_EQ("null", ArtField::PrettyField(nullptr)); |
| 1633 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1634 | ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(), |
| 1635 | "Ljava/lang/String;"); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1636 | |
| 1637 | ArtField* f; |
| 1638 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
| 1639 | EXPECT_EQ("int java.lang.String.count", f->PrettyField()); |
| 1640 | EXPECT_EQ("java.lang.String.count", f->PrettyField(false)); |
| 1641 | } |
| 1642 | |
| 1643 | TEST_F(ClassLinkerTest, JniShortName_JniLongName) { |
| 1644 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1645 | ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1646 | ASSERT_TRUE(c != nullptr); |
| 1647 | ArtMethod* m; |
| 1648 | |
| 1649 | m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize); |
| 1650 | ASSERT_TRUE(m != nullptr); |
| 1651 | ASSERT_FALSE(m->IsDirect()); |
| 1652 | EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName()); |
| 1653 | EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName()); |
| 1654 | |
| 1655 | m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize); |
| 1656 | ASSERT_TRUE(m != nullptr); |
| 1657 | ASSERT_FALSE(m->IsDirect()); |
| 1658 | EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName()); |
| 1659 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName()); |
| 1660 | |
| 1661 | m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize); |
| 1662 | ASSERT_TRUE(m != nullptr); |
| 1663 | ASSERT_TRUE(m->IsStatic()); |
| 1664 | EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName()); |
| 1665 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName()); |
| 1666 | } |
| 1667 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1668 | class ClassLinkerClassLoaderTest : public ClassLinkerTest { |
| 1669 | protected: |
| 1670 | // Verifies that the class identified by the given descriptor is loaded with |
| 1671 | // the expected_class_loader_obj when search from class_loader_to_search_obj. |
| 1672 | // When expected_class_loader_obj is null the check will be done against BootClassLoader. |
| 1673 | void VerifyClassResolution(const std::string& descriptor, |
| 1674 | jobject class_loader_to_search_obj, |
| 1675 | jobject expected_class_loader_obj, |
| 1676 | bool should_find = true) { |
| 1677 | Thread* self = Thread::Current(); |
| 1678 | ScopedObjectAccess soa(self); |
| 1679 | StackHandleScope<3> hs(self); |
| 1680 | Handle<mirror::ClassLoader> class_loader_to_search( |
| 1681 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj))); |
| 1682 | |
| 1683 | Handle<mirror::Class> klass = hs.NewHandle( |
| 1684 | class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search)); |
| 1685 | |
| 1686 | if (!should_find) { |
| 1687 | if (self->IsExceptionPending()) { |
| 1688 | self->ClearException(); |
| 1689 | } |
| 1690 | ASSERT_TRUE(klass == nullptr); |
| 1691 | } else if (expected_class_loader_obj == nullptr) { |
| 1692 | ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader())); |
| 1693 | } else { |
| 1694 | ASSERT_TRUE(klass != nullptr) << descriptor; |
| 1695 | Handle<mirror::ClassLoader> expected_class_loader( |
| 1696 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj))); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 1697 | ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get()); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | }; |
| 1701 | |
| 1702 | TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) { |
| 1703 | jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr); |
| 1704 | VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a); |
| 1705 | VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1706 | VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) { |
| 1710 | jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr); |
| 1711 | VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a); |
| 1712 | VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1713 | VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) { |
| 1717 | // The chain is |
| 1718 | // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD) |
| 1719 | // ^ |
| 1720 | // | |
| 1721 | // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD) |
| 1722 | // ^ |
| 1723 | // | |
| 1724 | // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD) |
| 1725 | // ^ |
| 1726 | // | |
Calin Juravle | e64ed9e | 2017-07-06 22:01:00 -0700 | [diff] [blame] | 1727 | // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD) |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1728 | |
| 1729 | jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr); |
| 1730 | jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a); |
| 1731 | jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b); |
| 1732 | jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c); |
| 1733 | |
| 1734 | // Verify exclusive classes (present in only one class loader). |
| 1735 | VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d); |
| 1736 | VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c); |
| 1737 | VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b); |
| 1738 | VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a); |
| 1739 | |
| 1740 | // Verify classes that are defined in multiple classloader. |
| 1741 | |
| 1742 | // Classes defined in B should be found in B even if they are defined in A or C because |
| 1743 | // B is a DelegateLastClassLoader. |
| 1744 | VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b); |
| 1745 | VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b); |
| 1746 | VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b); |
| 1747 | |
| 1748 | // Classes defined in D should be found in D even if they are defined in parent class loaders |
| 1749 | // as well because D is a DelegateLastClassLoader. |
| 1750 | VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d); |
| 1751 | VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d); |
| 1752 | VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d); |
| 1753 | |
| 1754 | |
| 1755 | // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found |
| 1756 | // in the top parent. |
| 1757 | VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a); |
| 1758 | |
Ian Pedowitz | 2d53643 | 2020-07-22 14:33:00 -0700 | [diff] [blame] | 1759 | // Check that we don't find an undefined class. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1760 | VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1763 | } // namespace art |