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