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