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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 23 | #include "common_runtime_test.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 24 | #include "dex_file.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 26 | #include "field_helper.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "gc/heap.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "mirror/art_field-inl.h" |
| 29 | #include "mirror/art_method.h" |
| 30 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/class-inl.h" |
| 32 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "mirror/object-inl.h" |
| 34 | #include "mirror/object_array-inl.h" |
| 35 | #include "mirror/proxy.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 36 | #include "mirror/reference.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 38 | #include "mirror/string-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 39 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 40 | #include "scoped_thread_state_change.h" |
| 41 | #include "thread-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 45 | class ClassLinkerTest : public CommonRuntimeTest { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 46 | protected: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | void AssertNonExistentClass(const std::string& descriptor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 48 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 49 | Thread* self = Thread::Current(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 50 | EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 51 | EXPECT_TRUE(self->IsExceptionPending()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 52 | mirror::Object* exception = self->GetException(NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 53 | self->ClearException(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 54 | mirror::Class* exception_class = |
| 55 | class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;"); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 56 | EXPECT_TRUE(exception->InstanceOf(exception_class)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | void AssertPrimitiveClass(const std::string& descriptor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 60 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 61 | Thread* self = Thread::Current(); |
| 62 | AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str())); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 66 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(primitive != NULL); |
| 68 | ASSERT_TRUE(primitive->GetClass() != NULL); |
| 69 | ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass()); |
| 70 | EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 71 | std::string temp; |
| 72 | ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(primitive->GetSuperClass() == NULL); |
| 74 | EXPECT_FALSE(primitive->HasSuperClass()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(primitive->GetClassLoader() == NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 76 | EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 77 | EXPECT_FALSE(primitive->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(primitive->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(primitive->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(primitive->IsVerified()); |
| 81 | EXPECT_TRUE(primitive->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 82 | EXPECT_FALSE(primitive->IsArrayInstance()); |
| 83 | EXPECT_FALSE(primitive->IsArrayClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(primitive->GetComponentType() == NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 85 | EXPECT_FALSE(primitive->IsInterface()); |
| 86 | EXPECT_TRUE(primitive->IsPublic()); |
| 87 | EXPECT_TRUE(primitive->IsFinal()); |
| 88 | EXPECT_TRUE(primitive->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 89 | EXPECT_FALSE(primitive->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 90 | EXPECT_EQ(0U, primitive->NumDirectMethods()); |
| 91 | EXPECT_EQ(0U, primitive->NumVirtualMethods()); |
| 92 | EXPECT_EQ(0U, primitive->NumInstanceFields()); |
| 93 | EXPECT_EQ(0U, primitive->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 94 | EXPECT_EQ(0U, primitive->NumDirectInterfaces()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 95 | EXPECT_FALSE(primitive->HasVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 96 | EXPECT_EQ(0, primitive->GetIfTableCount()); |
| 97 | EXPECT_TRUE(primitive->GetIfTable() == NULL); |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 98 | EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 101 | void AssertArrayClass(const std::string& array_descriptor, |
| 102 | const std::string& component_type, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 103 | mirror::ClassLoader* class_loader) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 105 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 106 | StackHandleScope<2> hs(self); |
| 107 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
| 108 | Handle<mirror::Class> array( |
| 109 | hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader))); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 110 | std::string temp; |
| 111 | EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp)); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 112 | EXPECT_EQ(class_loader, array->GetClassLoader()); |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 113 | EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract))); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 114 | AssertArrayClass(array_descriptor, array); |
| 115 | } |
| 116 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 117 | void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(array.Get() != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 120 | ASSERT_TRUE(array->GetClass() != NULL); |
| 121 | ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass()); |
| 122 | EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 123 | std::string temp; |
| 124 | ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(array->GetSuperClass() != NULL); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 126 | Thread* self = Thread::Current(); |
| 127 | EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(array->HasSuperClass()); |
| 129 | ASSERT_TRUE(array->GetComponentType() != NULL); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 130 | ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 131 | EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 132 | EXPECT_FALSE(array->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(array->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(array->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(array->IsVerified()); |
| 136 | EXPECT_TRUE(array->IsInitialized()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 137 | EXPECT_FALSE(array->IsArrayInstance()); |
| 138 | EXPECT_TRUE(array->IsArrayClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 139 | EXPECT_FALSE(array->IsInterface()); |
| 140 | EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic()); |
| 141 | EXPECT_TRUE(array->IsFinal()); |
| 142 | EXPECT_FALSE(array->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 143 | EXPECT_FALSE(array->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 144 | EXPECT_EQ(0U, array->NumDirectMethods()); |
| 145 | EXPECT_EQ(0U, array->NumVirtualMethods()); |
| 146 | EXPECT_EQ(0U, array->NumInstanceFields()); |
| 147 | EXPECT_EQ(0U, array->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 148 | EXPECT_EQ(2U, array->NumDirectInterfaces()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 149 | EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 150 | EXPECT_EQ(2, array->GetIfTableCount()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 151 | ASSERT_TRUE(array->GetIfTable() != NULL); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 152 | mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0); |
| 153 | EXPECT_TRUE(direct_interface0 != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 154 | EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;"); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 155 | mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 156 | EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;"); |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 157 | mirror::Class* array_ptr = array->GetComponentType(); |
| 158 | EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 159 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 160 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 161 | void AssertMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 162 | EXPECT_TRUE(method != nullptr); |
| 163 | EXPECT_TRUE(method->GetClass() != nullptr); |
| 164 | EXPECT_TRUE(method->GetName() != nullptr); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(method->GetSignature() != Signature::NoSignature()); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 166 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 167 | EXPECT_TRUE(method->GetDexCacheStrings() != nullptr); |
| 168 | EXPECT_TRUE(method->HasDexCacheResolvedMethods()); |
| 169 | EXPECT_TRUE(method->HasDexCacheResolvedTypes()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 170 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(), |
| 171 | method->GetDexCacheStrings()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 172 | EXPECT_TRUE(method->HasSameDexCacheResolvedMethods( |
| 173 | method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods())); |
| 174 | EXPECT_TRUE(method->HasSameDexCacheResolvedTypes( |
| 175 | method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes())); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 178 | void AssertField(mirror::Class* klass, mirror::ArtField* field) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 180 | EXPECT_TRUE(field != NULL); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 181 | EXPECT_TRUE(field->GetClass() != NULL); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 182 | EXPECT_EQ(klass, field->GetDeclaringClass()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 183 | EXPECT_TRUE(field->GetName() != NULL); |
| 184 | StackHandleScope<1> hs(Thread::Current()); |
| 185 | FieldHelper fh(hs.NewHandle(field)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 186 | EXPECT_TRUE(fh.GetType() != NULL); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 189 | void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 190 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 191 | std::string temp; |
| 192 | EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 193 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 194 | EXPECT_FALSE(klass->HasSuperClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 195 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 196 | EXPECT_TRUE(klass->HasSuperClass()); |
| 197 | EXPECT_TRUE(klass->GetSuperClass() != NULL); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 198 | } |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 199 | EXPECT_TRUE(klass->GetClass() != NULL); |
| 200 | EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 201 | EXPECT_TRUE(klass->GetDexCache() != NULL); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 202 | EXPECT_TRUE(klass->IsLoaded()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(klass->IsResolved()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 204 | EXPECT_FALSE(klass->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 205 | EXPECT_FALSE(klass->IsArrayClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 206 | EXPECT_TRUE(klass->GetComponentType() == NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 207 | EXPECT_TRUE(klass->IsInSamePackage(klass.Get())); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 208 | std::string temp2; |
| 209 | EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp), |
| 210 | klass->GetDescriptor(&temp2))); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 211 | if (klass->IsInterface()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 212 | EXPECT_TRUE(klass->IsAbstract()); |
| 213 | if (klass->NumDirectMethods() == 1) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 214 | EXPECT_TRUE(klass->GetDirectMethod(0)->IsClassInitializer()); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 215 | EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 216 | } else { |
| 217 | EXPECT_EQ(0U, klass->NumDirectMethods()); |
| 218 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 219 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 220 | if (!klass->IsSynthetic()) { |
| 221 | EXPECT_NE(0U, klass->NumDirectMethods()); |
| 222 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 223 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 224 | EXPECT_EQ(klass->IsInterface(), !klass->HasVTable()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 225 | mirror::IfTable* iftable = klass->GetIfTable(); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 226 | for (int i = 0; i < klass->GetIfTableCount(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 227 | mirror::Class* interface = iftable->GetInterface(i); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 228 | ASSERT_TRUE(interface != NULL); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 229 | if (klass->IsInterface()) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 230 | EXPECT_EQ(0U, iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 231 | } else { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 232 | EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i)); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 235 | if (klass->IsAbstract()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 236 | EXPECT_FALSE(klass->IsFinal()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 237 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 238 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 239 | } |
| 240 | if (klass->IsFinal()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 241 | EXPECT_FALSE(klass->IsAbstract()); |
| 242 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 243 | } |
| 244 | if (klass->IsAnnotation()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 245 | EXPECT_FALSE(klass->IsFinal()); |
| 246 | EXPECT_TRUE(klass->IsAbstract()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | EXPECT_FALSE(klass->IsPrimitive()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 250 | EXPECT_TRUE(klass->CanAccess(klass.Get())); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 251 | |
| 252 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 253 | mirror::ArtMethod* method = klass->GetDirectMethod(i); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 254 | AssertMethod(method); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 255 | EXPECT_TRUE(method->IsDirect()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 256 | EXPECT_EQ(klass.Get(), method->GetDeclaringClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 260 | mirror::ArtMethod* method = klass->GetVirtualMethod(i); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 261 | AssertMethod(method); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 262 | EXPECT_FALSE(method->IsDirect()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 263 | EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass.Get())); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 267 | mirror::ArtField* field = klass->GetInstanceField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 268 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 269 | EXPECT_FALSE(field->IsStatic()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | for (size_t i = 0; i < klass->NumStaticFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 273 | mirror::ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 274 | AssertField(klass.Get(), field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 275 | EXPECT_TRUE(field->IsStatic()); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 276 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 277 | |
| 278 | // Confirm that all instances fields are packed together at the start |
| 279 | EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 280 | StackHandleScope<1> hs(Thread::Current()); |
| 281 | FieldHelper fh(hs.NewHandle<mirror::ArtField>(nullptr)); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 282 | for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 283 | mirror::ArtField* field = klass->GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 284 | fh.ChangeField(field); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 285 | ASSERT_TRUE(!field->IsPrimitiveType()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 286 | mirror::Class* field_type = fh.GetType(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 287 | ASSERT_TRUE(field_type != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(!field_type->IsPrimitive()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 289 | } |
| 290 | for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 291 | mirror::ArtField* field = klass->GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 292 | fh.ChangeField(field); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 293 | mirror::Class* field_type = fh.GetType(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 294 | ASSERT_TRUE(field_type != NULL); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 295 | if (!fh.GetField()->IsPrimitiveType() || !field_type->IsPrimitive()) { |
Brian Carlstrom | fbfdce6 | 2011-10-05 17:33:32 -0700 | [diff] [blame] | 296 | // While Reference.referent is not primitive, the ClassLinker |
| 297 | // treats it as such so that the garbage collector won't scan it. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 298 | EXPECT_EQ(PrettyField(fh.GetField()), "java.lang.Object java.lang.ref.Reference.referent"); |
Brian Carlstrom | fbfdce6 | 2011-10-05 17:33:32 -0700 | [diff] [blame] | 299 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | size_t total_num_reference_instance_fields = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 303 | mirror::Class* k = klass.Get(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 304 | while (k != NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 305 | total_num_reference_instance_fields += k->NumReferenceInstanceFields(); |
| 306 | k = k->GetSuperClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 307 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 308 | EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0, total_num_reference_instance_fields == 0); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 311 | void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 312 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 313 | ASSERT_TRUE(descriptor != NULL); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 314 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 315 | StackHandleScope<1> hs(self); |
| 316 | Handle<mirror::Class> klass( |
| 317 | hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str()))); |
| 318 | ASSERT_TRUE(klass.Get() != nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 319 | std::string temp; |
| 320 | EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp)); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 321 | EXPECT_EQ(class_loader, klass->GetClassLoader()); |
| 322 | if (klass->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 323 | AssertPrimitiveClass(descriptor, klass.Get()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 324 | } else if (klass->IsArrayClass()) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 325 | AssertArrayClass(descriptor, klass); |
| 326 | } else { |
| 327 | AssertClass(descriptor, klass); |
| 328 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 331 | void AssertDexFile(const DexFile* dex, mirror::ClassLoader* class_loader) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 332 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 333 | ASSERT_TRUE(dex != NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 334 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 335 | // Verify all the classes defined in this file |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 336 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 337 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 338 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 339 | AssertDexFileClass(class_loader, descriptor); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 340 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 341 | // Verify all the types referenced by this file |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 342 | for (size_t i = 0; i < dex->NumTypeIds(); i++) { |
| 343 | const DexFile::TypeId& type_id = dex->GetTypeId(i); |
| 344 | const char* descriptor = dex->GetTypeDescriptor(type_id); |
| 345 | AssertDexFileClass(class_loader, descriptor); |
| 346 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 347 | class_linker_->VisitRoots(TestRootVisitor, NULL, kVisitRootFlagAllRoots); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 348 | // Verify the dex cache has resolution methods in all resolved method slots |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 349 | mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex); |
| 350 | mirror::ObjectArray<mirror::ArtMethod>* resolved_methods = dex_cache->GetResolvedMethods(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 351 | for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 352 | EXPECT_TRUE(resolved_methods->Get(i) != NULL) << dex->GetLocation() << " i=" << i; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 353 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 354 | } |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 355 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 356 | static void TestRootVisitor(mirror::Object** root, void*, uint32_t, RootType) { |
| 357 | EXPECT_TRUE(*root != NULL); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 358 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 359 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 360 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 361 | struct CheckOffset { |
| 362 | size_t cpp_offset; |
| 363 | const char* java_name; |
| 364 | CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {} |
| 365 | }; |
| 366 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 367 | template <typename T> |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 368 | struct CheckOffsets { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 369 | CheckOffsets(bool is_static, const char* class_descriptor) |
| 370 | : is_static(is_static), class_descriptor(class_descriptor) {} |
| 371 | bool is_static; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 372 | std::string class_descriptor; |
| 373 | std::vector<CheckOffset> offsets; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 374 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 375 | bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 376 | Thread* self = Thread::Current(); |
| 377 | mirror::Class* klass = |
| 378 | Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 379 | CHECK(klass != NULL) << class_descriptor; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 380 | |
| 381 | bool error = false; |
| 382 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 383 | if (!klass->IsClassClass() && !is_static) { |
| 384 | size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 385 | if (sizeof(T) != expected_size) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 386 | LOG(ERROR) << "Class size mismatch:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 387 | << " class=" << class_descriptor |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 388 | << " Java=" << expected_size |
| 389 | << " C++=" << sizeof(T); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 390 | error = true; |
| 391 | } |
| 392 | } |
| 393 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 394 | size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 395 | if (offsets.size() != num_fields) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 396 | LOG(ERROR) << "Field count mismatch:" |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 397 | << " class=" << class_descriptor |
| 398 | << " Java=" << num_fields |
| 399 | << " C++=" << offsets.size(); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 400 | error = true; |
| 401 | } |
| 402 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 403 | for (size_t i = 0; i < offsets.size(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 404 | mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 405 | StringPiece field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 406 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 407 | error = true; |
| 408 | } |
| 409 | } |
| 410 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 411 | for (size_t i = 0; i < offsets.size(); i++) { |
| 412 | CheckOffset& offset = offsets[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 413 | mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 414 | StringPiece field_name(field->GetName()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 415 | if (field_name != offsets[i].java_name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 416 | LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 417 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 418 | LOG(ERROR) << "Java field order:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 419 | << " i=" << i << " class=" << class_descriptor |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 420 | << " Java=" << field_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 421 | << " CheckOffsets=" << offset.java_name; |
| 422 | } |
| 423 | } |
| 424 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 425 | for (size_t i = 0; i < offsets.size(); i++) { |
| 426 | CheckOffset& offset = offsets[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 427 | mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 428 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
| 429 | error = true; |
| 430 | } |
| 431 | } |
| 432 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 433 | for (size_t i = 0; i < offsets.size(); i++) { |
| 434 | CheckOffset& offset = offsets[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 435 | mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 436 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 437 | LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:"; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 438 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 439 | LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 440 | << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return !error; |
| 445 | }; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 446 | |
| 447 | private: |
| 448 | DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 449 | }; |
| 450 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 451 | // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses |
| 452 | // is first since if it is failing, others are unlikely to succeed. |
| 453 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 454 | struct ObjectOffsets : public CheckOffsets<mirror::Object> { |
| 455 | ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 456 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 457 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 458 | |
| 459 | // alphabetical 32-bit |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 460 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_")); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 461 | #ifdef USE_BAKER_OR_BROOKS_READ_BARRIER |
| 462 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_")); |
| 463 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_")); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 464 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 465 | }; |
| 466 | }; |
| 467 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 468 | struct ArtFieldOffsets : public CheckOffsets<mirror::ArtField> { |
| 469 | ArtFieldOffsets() : CheckOffsets<mirror::ArtField>(false, "Ljava/lang/reflect/ArtField;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 470 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 471 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, declaring_class_), "declaringClass")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 472 | |
| 473 | // alphabetical 32-bit |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 474 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, access_flags_), "accessFlags")); |
| 475 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, field_dex_idx_), "fieldDexIndex")); |
| 476 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, offset_), "offset")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 477 | }; |
| 478 | }; |
| 479 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 480 | struct ArtMethodOffsets : public CheckOffsets<mirror::ArtMethod> { |
| 481 | ArtMethodOffsets() : CheckOffsets<mirror::ArtMethod>(false, "Ljava/lang/reflect/ArtMethod;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 482 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 483 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, declaring_class_), "declaringClass")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 484 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_methods_), "dexCacheResolvedMethods")); |
| 485 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_types_), "dexCacheResolvedTypes")); |
| 486 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_strings_), "dexCacheStrings")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 487 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 488 | // alphabetical 64-bit |
| 489 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_interpreter_), "entryPointFromInterpreter")); |
| 490 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_jni_), "entryPointFromJni")); |
| 491 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_portable_compiled_code_), "entryPointFromPortableCompiledCode")); |
| 492 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_quick_compiled_code_), "entryPointFromQuickCompiledCode")); |
| 493 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, gc_map_), "gcMap")); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 494 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 495 | // alphabetical 32-bit |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 496 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, access_flags_), "accessFlags")); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 497 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_code_item_offset_), "dexCodeItemOffset")); |
| 498 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_method_index_), "dexMethodIndex")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 499 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_index_), "methodIndex")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 500 | }; |
| 501 | }; |
| 502 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 503 | struct ClassOffsets : public CheckOffsets<mirror::Class> { |
| 504 | ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 505 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 506 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader")); |
| 507 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType")); |
| 508 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache")); |
| 509 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods")); |
| 510 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields")); |
| 511 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable")); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 512 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, imtable_), "imTable")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 513 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name")); |
| 514 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields")); |
| 515 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass")); |
| 516 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_), "verifyErrorClass")); |
| 517 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods")); |
| 518 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 519 | |
| 520 | // alphabetical 32-bit |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 521 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags")); |
| 522 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize")); |
| 523 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId")); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 524 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 525 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex")); |
| 526 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), "numReferenceInstanceFields")); |
| 527 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), "numReferenceStaticFields")); |
| 528 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize")); |
| 529 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType")); |
| 530 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), "referenceInstanceOffsets")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 531 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 532 | }; |
| 533 | }; |
| 534 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 535 | struct StringOffsets : public CheckOffsets<mirror::String> { |
| 536 | StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 537 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 538 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, array_), "value")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 539 | |
| 540 | // alphabetical 32-bit |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 541 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, count_), "count")); |
| 542 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode")); |
| 543 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, offset_), "offset")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 544 | }; |
| 545 | }; |
| 546 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 547 | struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> { |
| 548 | ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 549 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 550 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause")); |
| 551 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage")); |
| 552 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState")); |
| 553 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace")); |
| 554 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 555 | }; |
| 556 | }; |
| 557 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 558 | struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> { |
| 559 | StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(false, "Ljava/lang/StackTraceElement;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 560 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 561 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass")); |
| 562 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName")); |
| 563 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName")); |
| 564 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 565 | }; |
| 566 | }; |
| 567 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 568 | struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> { |
| 569 | ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 570 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 571 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages")); |
| 572 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent")); |
| 573 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 574 | }; |
| 575 | }; |
| 576 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 577 | struct ProxyOffsets : public CheckOffsets<mirror::Proxy> { |
| 578 | ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") { |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 579 | // alphabetical references |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 580 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h")); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 581 | }; |
| 582 | }; |
| 583 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 584 | struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> { |
| 585 | DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 586 | // alphabetical references |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 587 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex")); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 588 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location")); |
| 589 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields")); |
| 590 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods")); |
| 591 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes")); |
| 592 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings")); |
| 593 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile")); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 594 | }; |
| 595 | }; |
| 596 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 597 | struct ReferenceOffsets : public CheckOffsets<mirror::Reference> { |
| 598 | ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") { |
| 599 | // alphabetical references |
| 600 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext")); |
| 601 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue")); |
| 602 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext")); |
| 603 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent")); |
| 604 | }; |
| 605 | }; |
| 606 | |
| 607 | struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> { |
| 608 | FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(false, "Ljava/lang/ref/FinalizerReference;") { |
| 609 | // alphabetical references |
| 610 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next")); |
| 611 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev")); |
| 612 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie")); |
| 613 | }; |
| 614 | }; |
| 615 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 616 | // 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] | 617 | // 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] | 618 | // ClassLinker::LinkFields. |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 619 | TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 620 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 621 | EXPECT_TRUE(ObjectOffsets().Check()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 622 | EXPECT_TRUE(ArtFieldOffsets().Check()); |
| 623 | EXPECT_TRUE(ArtMethodOffsets().Check()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 624 | EXPECT_TRUE(ClassOffsets().Check()); |
| 625 | EXPECT_TRUE(StringOffsets().Check()); |
| 626 | EXPECT_TRUE(ThrowableOffsets().Check()); |
| 627 | EXPECT_TRUE(StackTraceElementOffsets().Check()); |
| 628 | EXPECT_TRUE(ClassLoaderOffsets().Check()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 629 | EXPECT_TRUE(ProxyOffsets().Check()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 630 | EXPECT_TRUE(DexCacheOffsets().Check()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 631 | EXPECT_TRUE(ReferenceOffsets().Check()); |
| 632 | EXPECT_TRUE(FinalizerReferenceOffsets().Check()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 635 | TEST_F(ClassLinkerTest, FindClassNonexistent) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 636 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 637 | AssertNonExistentClass("NoSuchClass;"); |
| 638 | AssertNonExistentClass("LNoSuchClass;"); |
| 639 | } |
| 640 | |
| 641 | TEST_F(ClassLinkerTest, FindClassNested) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 642 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 643 | StackHandleScope<1> hs(soa.Self()); |
| 644 | Handle<mirror::ClassLoader> class_loader( |
| 645 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 646 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 647 | mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 648 | ASSERT_TRUE(outer != NULL); |
| 649 | EXPECT_EQ(0U, outer->NumVirtualMethods()); |
| 650 | EXPECT_EQ(1U, outer->NumDirectMethods()); |
| 651 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 652 | mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 653 | ASSERT_TRUE(inner != NULL); |
| 654 | EXPECT_EQ(0U, inner->NumVirtualMethods()); |
| 655 | EXPECT_EQ(1U, inner->NumDirectMethods()); |
| 656 | } |
| 657 | |
| 658 | TEST_F(ClassLinkerTest, FindClass_Primitives) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 659 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 660 | const std::string expected("BCDFIJSZV"); |
Elliott Hughes | db7d5e9 | 2011-12-16 18:47:37 -0800 | [diff] [blame] | 661 | for (int ch = 1; ch < 256; ++ch) { |
| 662 | std::string descriptor; |
| 663 | descriptor.push_back(ch); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 664 | if (expected.find(ch) == std::string::npos) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 665 | AssertNonExistentClass(descriptor); |
| 666 | } else { |
| 667 | AssertPrimitiveClass(descriptor); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | TEST_F(ClassLinkerTest, FindClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 673 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 674 | mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 675 | ASSERT_TRUE(JavaLangObject != NULL); |
| 676 | ASSERT_TRUE(JavaLangObject->GetClass() != NULL); |
| 677 | ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass()); |
| 678 | EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 679 | std::string temp; |
| 680 | ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 681 | EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL); |
| 682 | EXPECT_FALSE(JavaLangObject->HasSuperClass()); |
| 683 | EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 684 | EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 685 | EXPECT_FALSE(JavaLangObject->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 686 | EXPECT_TRUE(JavaLangObject->IsLoaded()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 687 | EXPECT_TRUE(JavaLangObject->IsResolved()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 688 | EXPECT_TRUE(JavaLangObject->IsVerified()); |
| 689 | EXPECT_TRUE(JavaLangObject->IsInitialized()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 690 | EXPECT_FALSE(JavaLangObject->IsArrayInstance()); |
| 691 | EXPECT_FALSE(JavaLangObject->IsArrayClass()); |
| 692 | EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL); |
| 693 | EXPECT_FALSE(JavaLangObject->IsInterface()); |
| 694 | EXPECT_TRUE(JavaLangObject->IsPublic()); |
| 695 | EXPECT_FALSE(JavaLangObject->IsFinal()); |
| 696 | EXPECT_FALSE(JavaLangObject->IsPrimitive()); |
| 697 | EXPECT_FALSE(JavaLangObject->IsSynthetic()); |
| 698 | EXPECT_EQ(2U, JavaLangObject->NumDirectMethods()); |
| 699 | EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 700 | if (!kUseBakerOrBrooksReadBarrier) { |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 701 | EXPECT_EQ(2U, JavaLangObject->NumInstanceFields()); |
| 702 | } else { |
| 703 | EXPECT_EQ(4U, JavaLangObject->NumInstanceFields()); |
| 704 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 705 | EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_"); |
| 706 | EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_"); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 707 | if (kUseBakerOrBrooksReadBarrier) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 708 | EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_"); |
| 709 | EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_"); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 710 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 711 | |
| 712 | EXPECT_EQ(0U, JavaLangObject->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 713 | EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 714 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 715 | StackHandleScope<1> hs(soa.Self()); |
| 716 | Handle<mirror::ClassLoader> class_loader( |
| 717 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 718 | AssertNonExistentClass("LMyClass;"); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 719 | mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 720 | ASSERT_TRUE(MyClass != NULL); |
| 721 | ASSERT_TRUE(MyClass->GetClass() != NULL); |
| 722 | ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass()); |
| 723 | EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 724 | ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 725 | EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject); |
| 726 | EXPECT_TRUE(MyClass->HasSuperClass()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 727 | EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 728 | EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 729 | EXPECT_FALSE(MyClass->IsErroneous()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 730 | EXPECT_TRUE(MyClass->IsLoaded()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 731 | EXPECT_TRUE(MyClass->IsResolved()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 732 | EXPECT_FALSE(MyClass->IsVerified()); |
| 733 | EXPECT_FALSE(MyClass->IsInitialized()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 734 | EXPECT_FALSE(MyClass->IsArrayInstance()); |
| 735 | EXPECT_FALSE(MyClass->IsArrayClass()); |
| 736 | EXPECT_TRUE(MyClass->GetComponentType() == NULL); |
| 737 | EXPECT_FALSE(MyClass->IsInterface()); |
| 738 | EXPECT_FALSE(MyClass->IsPublic()); |
| 739 | EXPECT_FALSE(MyClass->IsFinal()); |
| 740 | EXPECT_FALSE(MyClass->IsPrimitive()); |
| 741 | EXPECT_FALSE(MyClass->IsSynthetic()); |
| 742 | EXPECT_EQ(1U, MyClass->NumDirectMethods()); |
| 743 | EXPECT_EQ(0U, MyClass->NumVirtualMethods()); |
| 744 | EXPECT_EQ(0U, MyClass->NumInstanceFields()); |
| 745 | EXPECT_EQ(0U, MyClass->NumStaticFields()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 746 | EXPECT_EQ(0U, MyClass->NumDirectInterfaces()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 747 | |
| 748 | EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass()); |
| 749 | |
| 750 | // created by class_linker |
| 751 | AssertArrayClass("[C", "C", NULL); |
| 752 | AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL); |
| 753 | // synthesized on the fly |
| 754 | AssertArrayClass("[[C", "[C", NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 755 | AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 756 | // or not available at all |
| 757 | AssertNonExistentClass("[[[[LNonExistentClass;"); |
| 758 | } |
| 759 | |
| 760 | TEST_F(ClassLinkerTest, LibCore) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 761 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 762 | AssertDexFile(java_lang_dex_file_, NULL); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 765 | // 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] | 766 | // start of the object |
| 767 | TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 768 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 769 | mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 770 | mirror::ObjectArray<mirror::String>* array = |
| 771 | mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 772 | uintptr_t data_offset = |
| 773 | reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>), |
| 774 | 0)); |
| 775 | if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 776 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment. |
| 777 | } else { |
| 778 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment. |
| 779 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 783 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 784 | StackHandleScope<5> hs(soa.Self()); |
| 785 | 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] | 786 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 787 | uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData()); |
| 788 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment |
| 789 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 790 | 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] | 791 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 792 | data_offset = reinterpret_cast<uintptr_t>(double_array->GetData()); |
| 793 | EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment |
| 794 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 795 | 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] | 796 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 797 | data_offset = reinterpret_cast<uintptr_t>(int_array->GetData()); |
| 798 | EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment |
| 799 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 800 | 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] | 801 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 802 | data_offset = reinterpret_cast<uintptr_t>(char_array->GetData()); |
| 803 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment |
| 804 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 805 | 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] | 806 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass()); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 807 | data_offset = reinterpret_cast<uintptr_t>(short_array->GetData()); |
| 808 | EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment |
| 809 | |
| 810 | // Take it as given that bytes and booleans have byte alignment |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 811 | } |
| 812 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 813 | TEST_F(ClassLinkerTest, ValidateBoxedTypes) { |
| 814 | // Validate that the "value" field is always the 0th field in each of java.lang's box classes. |
| 815 | // This lets UnboxPrimitive avoid searching for the field by name at runtime. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 816 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 817 | NullHandle<mirror::ClassLoader> class_loader; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 818 | mirror::Class* c; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 819 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 820 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 821 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 822 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 823 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 824 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 825 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 826 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 827 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 828 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 829 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 830 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 831 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 832 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 833 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 834 | EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 837 | TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 838 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 839 | StackHandleScope<2> hs(soa.Self()); |
| 840 | Handle<mirror::ClassLoader> class_loader_1( |
| 841 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")))); |
| 842 | Handle<mirror::ClassLoader> class_loader_2( |
| 843 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 844 | mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1); |
| 845 | 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] | 846 | EXPECT_TRUE(MyClass_1 != nullptr); |
| 847 | EXPECT_TRUE(MyClass_2 != nullptr); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 848 | EXPECT_NE(MyClass_1, MyClass_2); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 849 | } |
| 850 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 851 | TEST_F(ClassLinkerTest, StaticFields) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 852 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 853 | StackHandleScope<2> hs(soa.Self()); |
| 854 | Handle<mirror::ClassLoader> class_loader( |
| 855 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics")))); |
| 856 | Handle<mirror::Class> statics( |
| 857 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 858 | class_linker_->EnsureInitialized(statics, true, true); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 859 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 860 | // Static final primitives that are initialized by a compile-time constant |
| 861 | // expression resolve to a copy of a constant value from the constant pool. |
| 862 | // So <clinit> should be null. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 863 | mirror::ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V"); |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 864 | EXPECT_TRUE(clinit == NULL); |
| 865 | |
| 866 | EXPECT_EQ(9U, statics->NumStaticFields()); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 867 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 868 | mirror::ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z"); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 869 | std::string temp; |
| 870 | EXPECT_STREQ(s0->GetClass()->GetDescriptor(&temp), "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 871 | EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 872 | EXPECT_EQ(true, s0->GetBoolean(statics.Get())); |
| 873 | s0->SetBoolean<false>(statics.Get(), false); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 874 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 875 | mirror::ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 876 | EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 877 | EXPECT_EQ(5, s1->GetByte(statics.Get())); |
| 878 | s1->SetByte<false>(statics.Get(), 6); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 879 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 880 | mirror::ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 881 | EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 882 | EXPECT_EQ('a', s2->GetChar(statics.Get())); |
| 883 | s2->SetChar<false>(statics.Get(), 'b'); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 884 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 885 | mirror::ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 886 | EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 887 | EXPECT_EQ(-536, s3->GetShort(statics.Get())); |
| 888 | s3->SetShort<false>(statics.Get(), -535); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 889 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 890 | mirror::ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 891 | EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 892 | EXPECT_EQ(2000000000, s4->GetInt(statics.Get())); |
| 893 | s4->SetInt<false>(statics.Get(), 2000000001); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 894 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 895 | mirror::ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 896 | EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 897 | EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get())); |
| 898 | s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12)); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 899 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 900 | mirror::ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 901 | EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 902 | EXPECT_EQ(0.5, s6->GetFloat(statics.Get())); |
| 903 | s6->SetFloat<false>(statics.Get(), 0.75); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 904 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 905 | mirror::ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 906 | EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 907 | EXPECT_EQ(16777217, s7->GetDouble(statics.Get())); |
| 908 | s7->SetDouble<false>(statics.Get(), 16777219); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 909 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 910 | mirror::ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8", |
| 911 | "Ljava/lang/String;"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 912 | EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 913 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android")); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 914 | s8->SetObject<false>(s8->GetDeclaringClass(), |
| 915 | mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 916 | |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 917 | // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ |
| 918 | // http://code.google.com/p/googletest/issues/detail?id=322 |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 919 | EXPECT_FALSE(s0->GetBoolean(statics.Get())); |
| 920 | EXPECT_EQ(6, s1->GetByte(statics.Get())); |
| 921 | EXPECT_EQ('b', s2->GetChar(statics.Get())); |
| 922 | EXPECT_EQ(-535, s3->GetShort(statics.Get())); |
| 923 | EXPECT_EQ(2000000001, s4->GetInt(statics.Get())); |
| 924 | EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get())); |
| 925 | EXPECT_EQ(0.75, s6->GetFloat(statics.Get())); |
| 926 | EXPECT_EQ(16777219, s7->GetDouble(statics.Get())); |
| 927 | EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 928 | } |
| 929 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 930 | TEST_F(ClassLinkerTest, Interfaces) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 931 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 932 | StackHandleScope<6> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 933 | Handle<mirror::ClassLoader> class_loader( |
| 934 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces")))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 935 | Handle<mirror::Class> I( |
| 936 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 937 | Handle<mirror::Class> J( |
| 938 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
| 939 | Handle<mirror::Class> K( |
| 940 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader))); |
| 941 | Handle<mirror::Class> A( |
| 942 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader))); |
| 943 | Handle<mirror::Class> B( |
| 944 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader))); |
| 945 | EXPECT_TRUE(I->IsAssignableFrom(A.Get())); |
| 946 | EXPECT_TRUE(J->IsAssignableFrom(A.Get())); |
| 947 | EXPECT_TRUE(J->IsAssignableFrom(K.Get())); |
| 948 | EXPECT_TRUE(K->IsAssignableFrom(B.Get())); |
| 949 | EXPECT_TRUE(J->IsAssignableFrom(B.Get())); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 950 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 951 | const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V"); |
| 952 | mirror::ArtMethod* Ii = I->FindVirtualMethod("i", void_sig); |
| 953 | mirror::ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig); |
| 954 | mirror::ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig); |
| 955 | mirror::ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig); |
| 956 | mirror::ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig); |
| 957 | mirror::ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig); |
| 958 | mirror::ArtMethod* Ai = A->FindVirtualMethod("i", void_sig); |
| 959 | mirror::ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig); |
| 960 | mirror::ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 961 | ASSERT_TRUE(Ii != NULL); |
| 962 | ASSERT_TRUE(Jj1 != NULL); |
| 963 | ASSERT_TRUE(Jj2 != NULL); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 964 | ASSERT_TRUE(Kj1 != NULL); |
| 965 | ASSERT_TRUE(Kj2 != NULL); |
| 966 | ASSERT_TRUE(Kk != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 967 | ASSERT_TRUE(Ai != NULL); |
| 968 | ASSERT_TRUE(Aj1 != NULL); |
| 969 | ASSERT_TRUE(Aj2 != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 970 | EXPECT_NE(Ii, Ai); |
| 971 | EXPECT_NE(Jj1, Aj1); |
| 972 | EXPECT_NE(Jj2, Aj2); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 973 | EXPECT_EQ(Kj1, Jj1); |
| 974 | EXPECT_EQ(Kj2, Jj2); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 975 | EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii)); |
| 976 | EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1)); |
| 977 | EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2)); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 978 | EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii)); |
| 979 | EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1)); |
| 980 | EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2)); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 981 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 982 | mirror::ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", |
| 983 | "Ljava/lang/String;"); |
| 984 | mirror::ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", |
| 985 | "Ljava/lang/String;"); |
| 986 | mirror::ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", |
| 987 | "Ljava/lang/String;"); |
| 988 | mirror::ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", |
| 989 | "Ljava/lang/String;"); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 990 | ASSERT_TRUE(Afoo != NULL); |
| 991 | EXPECT_EQ(Afoo, Bfoo); |
| 992 | EXPECT_EQ(Afoo, Jfoo); |
| 993 | EXPECT_EQ(Afoo, Kfoo); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 996 | TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) { |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 997 | // 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] | 998 | |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 999 | // case 1, get the uninitialized storage from StaticsFromCode.<clinit> |
| 1000 | // case 2, get the initialized storage from StaticsFromCode.getS0 |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1001 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1002 | ScopedObjectAccess soa(Thread::Current()); |
| 1003 | jobject jclass_loader = LoadDex("StaticsFromCode"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1004 | StackHandleScope<1> hs(soa.Self()); |
| 1005 | Handle<mirror::ClassLoader> class_loader( |
| 1006 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1007 | const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0]; |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1008 | CHECK(dex_file != NULL); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1009 | mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1010 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1011 | mirror::ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;"); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 1012 | const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;"); |
| 1013 | ASSERT_TRUE(string_id != NULL); |
| 1014 | const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
| 1015 | ASSERT_TRUE(type_id != NULL); |
| 1016 | uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1017 | mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1018 | EXPECT_TRUE(uninit != NULL); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1019 | EXPECT_FALSE(uninit->IsInitialized()); |
| 1020 | mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1021 | EXPECT_TRUE(init != NULL); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 1022 | EXPECT_TRUE(init->IsInitialized()); |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1025 | TEST_F(ClassLinkerTest, FinalizableBit) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1026 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1027 | mirror::Class* c; |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1028 | |
| 1029 | // Object has a finalize method, but we know it's empty. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1030 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1031 | EXPECT_FALSE(c->IsFinalizable()); |
| 1032 | |
| 1033 | // Enum has a finalize method to prevent its subclasses from implementing one. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1034 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1035 | EXPECT_FALSE(c->IsFinalizable()); |
| 1036 | |
| 1037 | // RoundingMode is an enum. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1038 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1039 | EXPECT_FALSE(c->IsFinalizable()); |
| 1040 | |
| 1041 | // RandomAccessFile extends Object and overrides finalize. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1042 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1043 | EXPECT_TRUE(c->IsFinalizable()); |
| 1044 | |
| 1045 | // FileInputStream is finalizable and extends InputStream which isn't. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1046 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1047 | EXPECT_FALSE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1048 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1049 | EXPECT_TRUE(c->IsFinalizable()); |
| 1050 | |
| 1051 | // ScheduledThreadPoolExecutor doesn't have a finalize method but |
| 1052 | // extends ThreadPoolExecutor which does. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1053 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1054 | EXPECT_TRUE(c->IsFinalizable()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1055 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;"); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1056 | EXPECT_TRUE(c->IsFinalizable()); |
| 1057 | } |
| 1058 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1059 | TEST_F(ClassLinkerTest, ClassRootDescriptors) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1060 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1061 | std::string temp; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1062 | for (int i = 0; i < ClassLinker::kClassRootsMax; i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1063 | mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i)); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1064 | EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U); |
| 1065 | EXPECT_STREQ(klass->GetDescriptor(&temp), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1066 | class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i; |
| 1067 | } |
| 1068 | } |
| 1069 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1070 | TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) { |
| 1071 | ScopedObjectAccess soa(Thread::Current()); |
| 1072 | NullHandle<mirror::ClassLoader> class_loader; |
| 1073 | mirror::Class* c; |
| 1074 | |
| 1075 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader); |
| 1076 | EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize()); |
| 1077 | |
| 1078 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader); |
| 1079 | EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize()); |
| 1080 | |
| 1081 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader); |
| 1082 | EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize()); |
| 1083 | |
| 1084 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader); |
| 1085 | EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize()); |
| 1086 | |
| 1087 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtField;", class_loader); |
| 1088 | EXPECT_EQ(c->GetClassSize(), mirror::ArtField::ClassSize()); |
| 1089 | |
| 1090 | c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtMethod;", class_loader); |
| 1091 | EXPECT_EQ(c->GetClassSize(), mirror::ArtMethod::ClassSize()); |
| 1092 | } |
| 1093 | |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame^] | 1094 | static void CheckMethod(mirror::ArtMethod* method, bool verified) |
| 1095 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1096 | if (!method->IsNative() && !method->IsAbstract()) { |
| 1097 | EXPECT_EQ((method->GetAccessFlags() & kAccPreverified) != 0U, verified) |
| 1098 | << PrettyMethod(method, true); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | static void CheckPreverified(mirror::Class* c, bool preverified) |
| 1103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1104 | EXPECT_EQ((c->GetAccessFlags() & kAccPreverified) != 0U, preverified) |
| 1105 | << "Class " << PrettyClass(c) << " not as expected"; |
| 1106 | for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 1107 | CheckMethod(c->GetDirectMethod(i), preverified); |
| 1108 | } |
| 1109 | for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 1110 | CheckMethod(c->GetVirtualMethod(i), preverified); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | TEST_F(ClassLinkerTest, Preverified_InitializedBoot) { |
| 1115 | ScopedObjectAccess soa(Thread::Current()); |
| 1116 | |
| 1117 | mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 1118 | ASSERT_TRUE(JavaLangObject != NULL); |
| 1119 | EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the " |
| 1120 | "core"; |
| 1121 | CheckPreverified(JavaLangObject, true); |
| 1122 | } |
| 1123 | |
| 1124 | TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) { |
| 1125 | ScopedObjectAccess soa(Thread::Current()); |
| 1126 | |
| 1127 | StackHandleScope<1> hs(soa.Self()); |
| 1128 | |
| 1129 | Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass( |
| 1130 | soa.Self(), "Ljava/lang/SecurityManager;"))); |
| 1131 | EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the " |
| 1132 | "core"; |
| 1133 | |
| 1134 | CheckPreverified(security_manager.Get(), false); |
| 1135 | |
| 1136 | class_linker_->EnsureInitialized(security_manager, true, true); |
| 1137 | CheckPreverified(security_manager.Get(), true); |
| 1138 | } |
| 1139 | |
| 1140 | TEST_F(ClassLinkerTest, Preverified_App) { |
| 1141 | ScopedObjectAccess soa(Thread::Current()); |
| 1142 | |
| 1143 | StackHandleScope<2> hs(soa.Self()); |
| 1144 | Handle<mirror::ClassLoader> class_loader( |
| 1145 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics")))); |
| 1146 | Handle<mirror::Class> statics( |
| 1147 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader))); |
| 1148 | |
| 1149 | CheckPreverified(statics.Get(), false); |
| 1150 | |
| 1151 | class_linker_->EnsureInitialized(statics, true, true); |
| 1152 | CheckPreverified(statics.Get(), true); |
| 1153 | } |
| 1154 | |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1155 | } // namespace art |