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