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