Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "class_linker.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 4 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "UniquePtr.h" |
| 8 | #include "common_test.h" |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 9 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "dex_file.h" |
| 11 | #include "heap.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 12 | |
| 13 | namespace art { |
| 14 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 15 | class ClassLinkerTest : public CommonTest { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | protected: |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 17 | void AssertNonExistentClass(const StringPiece& descriptor) { |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 18 | EXPECT_TRUE(class_linker_->FindSystemClass(descriptor) == NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 19 | Thread* self = Thread::Current(); |
| 20 | EXPECT_TRUE(self->IsExceptionPending()); |
| 21 | Object* exception = self->GetException(); |
| 22 | self->ClearException(); |
| 23 | Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;"); |
| 24 | EXPECT_TRUE(exception->InstanceOf(exception_class)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void AssertPrimitiveClass(const StringPiece& descriptor) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 28 | AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor)); |
| 29 | } |
| 30 | |
| 31 | void AssertPrimitiveClass(const StringPiece& descriptor, const Class* primitive) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | ASSERT_TRUE(primitive != NULL); |
| 33 | ASSERT_TRUE(primitive->GetClass() != NULL); |
| 34 | ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass()); |
| 35 | EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 36 | ASSERT_TRUE(primitive->GetDescriptor()->Equals(descriptor)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | EXPECT_TRUE(primitive->GetSuperClass() == NULL); |
| 38 | EXPECT_FALSE(primitive->HasSuperClass()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 39 | EXPECT_TRUE(primitive->GetClassLoader() == NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 40 | EXPECT_TRUE(primitive->GetStatus() == Class::kStatusInitialized); |
| 41 | EXPECT_FALSE(primitive->IsErroneous()); |
| 42 | EXPECT_TRUE(primitive->IsVerified()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(primitive->IsResolved()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 44 | EXPECT_FALSE(primitive->IsArrayInstance()); |
| 45 | EXPECT_FALSE(primitive->IsArrayClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 46 | EXPECT_TRUE(primitive->GetComponentType() == NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 47 | EXPECT_FALSE(primitive->IsInterface()); |
| 48 | EXPECT_TRUE(primitive->IsPublic()); |
| 49 | EXPECT_TRUE(primitive->IsFinal()); |
| 50 | EXPECT_TRUE(primitive->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 51 | EXPECT_FALSE(primitive->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 52 | EXPECT_EQ(0U, primitive->NumDirectMethods()); |
| 53 | EXPECT_EQ(0U, primitive->NumVirtualMethods()); |
| 54 | EXPECT_EQ(0U, primitive->NumInstanceFields()); |
| 55 | EXPECT_EQ(0U, primitive->NumStaticFields()); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 56 | EXPECT_EQ(0U, primitive->NumInterfaces()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(primitive->GetVTable() == NULL); |
| 58 | EXPECT_EQ(0, primitive->GetIfTableCount()); |
| 59 | EXPECT_TRUE(primitive->GetIfTable() == NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void AssertArrayClass(const StringPiece& array_descriptor, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 63 | const StringPiece& component_type, |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 64 | const ClassLoader* class_loader) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 65 | Class* array = class_linker_->FindClass(array_descriptor, class_loader); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 66 | EXPECT_TRUE(array->GetComponentType()->GetDescriptor()->Equals(component_type)); |
| 67 | EXPECT_EQ(class_loader, array->GetClassLoader()); |
| 68 | AssertArrayClass(array_descriptor, array); |
| 69 | } |
| 70 | |
| 71 | void AssertArrayClass(const StringPiece& array_descriptor, Class* array) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(array != NULL); |
| 73 | ASSERT_TRUE(array->GetClass() != NULL); |
| 74 | ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass()); |
| 75 | EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(array->GetDescriptor()->Equals(array_descriptor)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(array->GetSuperClass() != NULL); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 78 | EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(array->HasSuperClass()); |
| 80 | ASSERT_TRUE(array->GetComponentType() != NULL); |
| 81 | ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(array->GetStatus() == Class::kStatusInitialized); |
| 83 | EXPECT_FALSE(array->IsErroneous()); |
| 84 | EXPECT_TRUE(array->IsVerified()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(array->IsResolved()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 86 | EXPECT_FALSE(array->IsArrayInstance()); |
| 87 | EXPECT_TRUE(array->IsArrayClass()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 88 | EXPECT_FALSE(array->IsInterface()); |
| 89 | EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic()); |
| 90 | EXPECT_TRUE(array->IsFinal()); |
| 91 | EXPECT_FALSE(array->IsPrimitive()); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 92 | EXPECT_FALSE(array->IsSynthetic()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 93 | EXPECT_EQ(0U, array->NumDirectMethods()); |
| 94 | EXPECT_EQ(0U, array->NumVirtualMethods()); |
| 95 | EXPECT_EQ(0U, array->NumInstanceFields()); |
| 96 | EXPECT_EQ(0U, array->NumStaticFields()); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 97 | EXPECT_EQ(2U, array->NumInterfaces()); |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 98 | EXPECT_TRUE(array->GetVTable() != NULL); |
| 99 | EXPECT_EQ(2, array->GetIfTableCount()); |
| 100 | ObjectArray<InterfaceEntry>* iftable = array->GetIfTable(); |
| 101 | ASSERT_TRUE(iftable != NULL); |
| 102 | EXPECT_TRUE(iftable->Get(0)->GetInterface()->GetDescriptor()->Equals("Ljava/lang/Cloneable;")); |
| 103 | EXPECT_TRUE(iftable->Get(1)->GetInterface()->GetDescriptor()->Equals("Ljava/io/Serializable;")); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 104 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 105 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 106 | void AssertMethod(Class* klass, Method* method) { |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(method != NULL); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 108 | EXPECT_TRUE(method->GetClass() != NULL); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(method->GetName() != NULL); |
| 110 | EXPECT_TRUE(method->GetSignature() != NULL); |
| 111 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 112 | EXPECT_TRUE(method->GetDexCacheStrings() != NULL); |
| 113 | EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL); |
| 114 | EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL); |
| 115 | EXPECT_TRUE(method->GetDexCacheResolvedFields() != NULL); |
| 116 | EXPECT_TRUE(method->GetDexCacheCodeAndDirectMethods() != NULL); |
| 117 | EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL); |
| 118 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(), |
| 119 | method->GetDexCacheStrings()); |
| 120 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(), |
| 121 | method->GetDexCacheResolvedTypes()); |
| 122 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(), |
| 123 | method->GetDexCacheResolvedMethods()); |
| 124 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedFields(), |
| 125 | method->GetDexCacheResolvedFields()); |
| 126 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetCodeAndDirectMethods(), |
| 127 | method->GetDexCacheCodeAndDirectMethods()); |
| 128 | EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(), |
| 129 | method->GetDexCacheInitializedStaticStorage()); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 132 | void AssertField(Class* klass, Field* field) { |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(field != NULL); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(field->GetClass() != NULL); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 135 | EXPECT_EQ(klass, field->GetDeclaringClass()); |
| 136 | EXPECT_TRUE(field->GetName() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(field->GetType() != NULL); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 140 | void AssertClass(const StringPiece& descriptor, Class* klass) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 142 | if (klass->GetDescriptor()->Equals(String::AllocFromModifiedUtf8("Ljava/lang/Object;"))) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 143 | EXPECT_FALSE(klass->HasSuperClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 144 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(klass->HasSuperClass()); |
| 146 | EXPECT_TRUE(klass->GetSuperClass() != NULL); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 147 | } |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 148 | EXPECT_TRUE(klass->GetClass() != NULL); |
| 149 | EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 150 | EXPECT_TRUE(klass->GetDexCache() != NULL); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 151 | EXPECT_TRUE(klass->IsResolved()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 152 | EXPECT_FALSE(klass->IsErroneous()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 153 | EXPECT_TRUE(klass->IsResolved()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 154 | EXPECT_TRUE(klass->IsLoaded()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 155 | EXPECT_FALSE(klass->IsArrayClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 156 | EXPECT_TRUE(klass->GetComponentType() == NULL); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(klass->IsInSamePackage(klass)); |
| 158 | EXPECT_TRUE(Class::IsInSamePackage(klass->GetDescriptor(), klass->GetDescriptor())); |
| 159 | if (klass->IsInterface()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 160 | EXPECT_TRUE(klass->IsAbstract()); |
| 161 | if (klass->NumDirectMethods() == 1) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 162 | EXPECT_TRUE(klass->GetDirectMethod(0)->GetName()->Equals("<clinit>")); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 164 | } else { |
| 165 | EXPECT_EQ(0U, klass->NumDirectMethods()); |
| 166 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 167 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 168 | if (!klass->IsSynthetic()) { |
| 169 | EXPECT_NE(0U, klass->NumDirectMethods()); |
| 170 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 171 | } |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 172 | EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL); |
| 173 | for (int i = 0; i < klass->GetIfTableCount(); i++) { |
| 174 | const InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i); |
| 175 | ASSERT_TRUE(interface_entry != NULL); |
| 176 | Class* interface = interface_entry->GetInterface(); |
| 177 | ASSERT_TRUE(interface != NULL); |
| 178 | EXPECT_TRUE(interface_entry->GetInterface() != NULL); |
| 179 | if (klass->IsInterface()) { |
| 180 | EXPECT_EQ(0U, interface_entry->GetMethodArrayCount()); |
| 181 | } else { |
| 182 | CHECK_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount()); |
| 183 | EXPECT_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount()); |
| 184 | } |
| 185 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 186 | if (klass->IsAbstract()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 187 | EXPECT_FALSE(klass->IsFinal()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 188 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 189 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 190 | } |
| 191 | if (klass->IsFinal()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 192 | EXPECT_FALSE(klass->IsAbstract()); |
| 193 | EXPECT_FALSE(klass->IsAnnotation()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 194 | } |
| 195 | if (klass->IsAnnotation()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 196 | EXPECT_FALSE(klass->IsFinal()); |
| 197 | EXPECT_TRUE(klass->IsAbstract()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | EXPECT_FALSE(klass->IsPrimitive()); |
| 201 | EXPECT_TRUE(klass->CanAccess(klass)); |
| 202 | |
| 203 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 204 | Method* method = klass->GetDirectMethod(i); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 205 | AssertMethod(klass, method); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 206 | EXPECT_TRUE(method->IsDirect()); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 207 | EXPECT_EQ(klass, method->GetDeclaringClass()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 211 | Method* method = klass->GetVirtualMethod(i); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 212 | AssertMethod(klass, method); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 213 | EXPECT_FALSE(method->IsDirect()); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 214 | EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass)); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | for (size_t i = 0; i < klass->NumInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 218 | Field* field = klass->GetInstanceField(i); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 219 | AssertField(klass, field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 220 | EXPECT_FALSE(field->IsStatic()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | for (size_t i = 0; i < klass->NumStaticFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 224 | Field* field = klass->GetStaticField(i); |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 225 | AssertField(klass, field); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 226 | EXPECT_TRUE(field->IsStatic()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 227 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 228 | |
| 229 | // Confirm that all instances fields are packed together at the start |
| 230 | EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
| 231 | for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 232 | Field* field = klass->GetInstanceField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 233 | Class* field_type = field->GetType(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 234 | ASSERT_TRUE(field_type != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 235 | ASSERT_TRUE(!field_type->IsPrimitive()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 236 | } |
| 237 | for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 238 | Field* field = klass->GetInstanceField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 239 | Class* field_type = field->GetType(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 240 | ASSERT_TRUE(field_type != NULL); |
| 241 | EXPECT_TRUE(field_type->IsPrimitive()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | size_t total_num_reference_instance_fields = 0; |
| 245 | Class* k = klass; |
| 246 | while (k != NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 247 | total_num_reference_instance_fields += k->NumReferenceInstanceFields(); |
| 248 | k = k->GetSuperClass(); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 249 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 250 | EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0, |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 251 | total_num_reference_instance_fields == 0); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 254 | void AssertDexFileClass(ClassLoader* class_loader, const StringPiece& descriptor) { |
| 255 | ASSERT_TRUE(descriptor != NULL); |
| 256 | Class* klass = class_linker_->FindSystemClass(descriptor); |
| 257 | ASSERT_TRUE(klass != NULL); |
| 258 | EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor)); |
| 259 | EXPECT_EQ(class_loader, klass->GetClassLoader()); |
| 260 | if (klass->IsPrimitive()) { |
| 261 | AssertPrimitiveClass(descriptor, klass); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 262 | } else if (klass->IsArrayClass()) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 263 | AssertArrayClass(descriptor, klass); |
| 264 | } else { |
| 265 | AssertClass(descriptor, klass); |
| 266 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 269 | void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 270 | ASSERT_TRUE(dex != NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 271 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 272 | // Verify all the classes defined in this file |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 273 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 274 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 275 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 276 | AssertDexFileClass(class_loader, descriptor); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 277 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 278 | // Verify all the types referenced by this file |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 279 | for (size_t i = 0; i < dex->NumTypeIds(); i++) { |
| 280 | const DexFile::TypeId& type_id = dex->GetTypeId(i); |
| 281 | const char* descriptor = dex->GetTypeDescriptor(type_id); |
| 282 | AssertDexFileClass(class_loader, descriptor); |
| 283 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 284 | class_linker_->VisitRoots(TestRootVisitor, NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 285 | } |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 286 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 287 | static void TestRootVisitor(const Object* root, void* arg) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 288 | EXPECT_TRUE(root != NULL); |
| 289 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 290 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 291 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 292 | struct CheckOffset { |
| 293 | size_t cpp_offset; |
| 294 | const char* java_name; |
| 295 | CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {} |
| 296 | }; |
| 297 | |
| 298 | struct CheckOffsets { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 299 | bool instance; |
| 300 | size_t size; |
| 301 | std::string class_descriptor; |
| 302 | std::vector<CheckOffset> offsets; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 303 | |
| 304 | bool Check() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 305 | Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor); |
| 306 | CHECK(klass != NULL) << class_descriptor; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 307 | |
| 308 | bool error = false; |
| 309 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 310 | if (!klass->IsClassClass() && instance) { |
| 311 | size_t expected_size = instance ? klass->GetObjectSize() : klass->GetClassSize(); |
| 312 | if (size != expected_size) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 313 | LG << "Class size mismatch:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 314 | << " class=" << class_descriptor |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 315 | << " Java=" << klass->GetObjectSize() |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 316 | << " C++=" << expected_size; |
| 317 | error = true; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | size_t num_fields = instance ? klass->NumInstanceFields() : klass->NumStaticFields(); |
| 322 | if (offsets.size() != num_fields) { |
| 323 | LG << "Field count mismatch:" |
| 324 | << " class=" << class_descriptor |
| 325 | << " Java=" << num_fields |
| 326 | << " C++=" << offsets.size(); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 327 | error = true; |
| 328 | } |
| 329 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 330 | for (size_t i = 0; i < offsets.size(); i++) { |
| 331 | Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i); |
| 332 | if (!field->GetName()->Equals(offsets[i].java_name)) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 333 | error = true; |
| 334 | } |
| 335 | } |
| 336 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 337 | for (size_t i = 0; i < offsets.size(); i++) { |
| 338 | CheckOffset& offset = offsets[i]; |
| 339 | Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i); |
| 340 | if (!field->GetName()->Equals(offsets[i].java_name)) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 341 | LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:"; |
| 342 | } |
| 343 | LG << "Java field order:" |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 344 | << " i=" << i << " class=" << class_descriptor |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 345 | << " Java=" << field->GetName()->ToModifiedUtf8() |
| 346 | << " CheckOffsets=" << offset.java_name; |
| 347 | } |
| 348 | } |
| 349 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 350 | for (size_t i = 0; i < offsets.size(); i++) { |
| 351 | CheckOffset& offset = offsets[i]; |
| 352 | Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 353 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
| 354 | error = true; |
| 355 | } |
| 356 | } |
| 357 | if (error) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 358 | for (size_t i = 0; i < offsets.size(); i++) { |
| 359 | CheckOffset& offset = offsets[i]; |
| 360 | Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 361 | if (field->GetOffset().Uint32Value() != offset.cpp_offset) { |
| 362 | LG << "OFFSET MISMATCH NEXT LINE:"; |
| 363 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 364 | LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 365 | << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return !error; |
| 370 | }; |
| 371 | }; |
| 372 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 373 | // Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses |
| 374 | // is first since if it is failing, others are unlikely to succeed. |
| 375 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 376 | struct ObjectOffsets : public CheckOffsets { |
| 377 | ObjectOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 378 | instance = true; |
| 379 | size = sizeof(Object); |
| 380 | class_descriptor = "Ljava/lang/Object;"; |
| 381 | |
| 382 | // alphabetical references |
| 383 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_")); |
| 384 | |
| 385 | // alphabetical 32-bit |
| 386 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 387 | }; |
| 388 | }; |
| 389 | |
| 390 | struct AccessibleObjectOffsets : public CheckOffsets { |
| 391 | AccessibleObjectOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 392 | instance = true; |
| 393 | size = sizeof(AccessibleObject); |
| 394 | class_descriptor = "Ljava/lang/reflect/AccessibleObject;"; |
| 395 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AccessibleObject, java_flag_), "flag")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 396 | }; |
| 397 | }; |
| 398 | |
| 399 | struct FieldOffsets : public CheckOffsets { |
| 400 | FieldOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 401 | instance = true; |
| 402 | size = sizeof(Field); |
| 403 | class_descriptor = "Ljava/lang/reflect/Field;"; |
| 404 | |
| 405 | // alphabetical references |
| 406 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass")); |
| 407 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_type_), "genericType")); |
| 408 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, name_), "name")); |
| 409 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_), "type")); |
| 410 | |
| 411 | // alphabetical 32-bit |
| 412 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_types_are_initialized_), "genericTypesAreInitialized")); |
| 413 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "shadow$_access_flags_")); |
| 414 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "shadow$_offset_")); |
| 415 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_idx_), "shadow$_type_idx_")); |
| 416 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, slot_), "slot")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 417 | }; |
| 418 | }; |
| 419 | |
| 420 | struct MethodOffsets : public CheckOffsets { |
| 421 | MethodOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 422 | instance = true; |
| 423 | size = sizeof(Method); |
| 424 | class_descriptor = "Ljava/lang/reflect/Method;"; |
| 425 | |
| 426 | // alphabetical references |
| 427 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, declaring_class_), "declaringClass")); |
| 428 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_exception_types_), "exceptionTypes")); |
| 429 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_formal_type_parameters_), "formalTypeParameters")); |
| 430 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_exception_types_), "genericExceptionTypes")); |
| 431 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_parameter_types_), "genericParameterTypes")); |
| 432 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_return_type_), "genericReturnType")); |
| 433 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, name_), "name")); |
| 434 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_parameter_types_), "parameterTypes")); |
| 435 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_), "returnType")); |
| 436 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_array_), "shadow$_code_array_")); |
| 437 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_code_and_direct_methods_), "shadow$_dex_cache_code_and_direct_methods_")); |
| 438 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_initialized_static_storage_), "shadow$_dex_cache_initialized_static_storage_")); |
| 439 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_fields_), "shadow$_dex_cache_resolved_fields_")); |
| 440 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_methods_), "shadow$_dex_cache_resolved_methods_")); |
| 441 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_types_), "shadow$_dex_cache_resolved_types_")); |
| 442 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_strings_), "shadow$_dex_cache_strings_")); |
| 443 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_array_), "shadow$_invoke_stub_array_")); |
| 444 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "shadow$_mapping_table_")); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 445 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_data_), "shadow$_register_map_data_")); |
| 446 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_header_), "shadow$_register_map_header_")); |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 447 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, shorty_), "shadow$_shorty_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 448 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, signature_), "shadow$_signature_")); |
| 449 | |
| 450 | // alphabetical 32-bit |
| 451 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_types_are_initialized_), "genericTypesAreInitialized")); |
| 452 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "shadow$_access_flags_")); |
| 453 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "shadow$_code_")); |
| 454 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "shadow$_code_item_offset_")); |
| 455 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "shadow$_core_spill_mask_")); |
| 456 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "shadow$_fp_spill_mask_")); |
| 457 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "shadow$_frame_size_in_bytes_")); |
| 458 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "shadow$_invoke_stub_")); |
| 459 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_idx_), "shadow$_java_return_type_idx_")); |
| 460 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "shadow$_method_index_")); |
| 461 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "shadow$_native_method_")); |
| 462 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_ins_), "shadow$_num_ins_")); |
| 463 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_outs_), "shadow$_num_outs_")); |
| 464 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_registers_), "shadow$_num_registers_")); |
| 465 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, proto_idx_), "shadow$_proto_idx_")); |
| 466 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, return_pc_offset_in_bytes_), "shadow$_return_pc_offset_in_bytes_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 467 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_slot_), "slot")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 468 | }; |
| 469 | }; |
| 470 | |
| 471 | struct ClassOffsets : public CheckOffsets { |
| 472 | ClassOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 473 | instance = true; |
| 474 | size = sizeof(Class); |
| 475 | class_descriptor = "Ljava/lang/Class;"; |
| 476 | |
| 477 | // alphabetical references |
| 478 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name")); |
| 479 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "shadow$_class_loader_")); |
| 480 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "shadow$_component_type_")); |
| 481 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, descriptor_), "shadow$_descriptor_")); |
| 482 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "shadow$_dex_cache_")); |
| 483 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "shadow$_direct_methods_")); |
| 484 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "shadow$_ifields_")); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 485 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "shadow$_iftable_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 486 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_), "shadow$_interfaces_")); |
| 487 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_type_idx_), "shadow$_interfaces_type_idx_")); |
| 488 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "shadow$_sfields_")); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 489 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, source_file_), "shadow$_source_file_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 490 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "shadow$_super_class_")); |
| 491 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "shadow$_verify_error_class_")); |
| 492 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "shadow$_virtual_methods_")); |
| 493 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "shadow$_vtable_")); |
| 494 | |
| 495 | // alphabetical 32-bit |
| 496 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "shadow$_access_flags_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 497 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "shadow$_class_size_")); |
| 498 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "shadow$_clinit_thread_id_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 499 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "shadow$_num_reference_instance_fields_")); |
| 500 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "shadow$_num_reference_static_fields_")); |
| 501 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "shadow$_object_size_")); |
| 502 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "shadow$_primitive_type_")); |
| 503 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "shadow$_reference_instance_offsets_")); |
| 504 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "shadow$_reference_static_offsets_")); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 505 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "shadow$_status_")); |
| 506 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_type_idx_), "shadow$_super_class_type_idx_")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 507 | }; |
| 508 | }; |
| 509 | |
| 510 | struct StringOffsets : public CheckOffsets { |
| 511 | StringOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 512 | instance = true; |
| 513 | size = sizeof(String); |
| 514 | class_descriptor = "Ljava/lang/String;"; |
| 515 | |
| 516 | // alphabetical references |
| 517 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value")); |
| 518 | |
| 519 | // alphabetical 32-bit |
| 520 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count")); |
| 521 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode")); |
| 522 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 523 | }; |
| 524 | }; |
| 525 | |
| 526 | struct ThrowableOffsets : public CheckOffsets { |
| 527 | ThrowableOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 528 | instance = true; |
| 529 | size = sizeof(Throwable); |
| 530 | class_descriptor = "Ljava/lang/Throwable;"; |
| 531 | |
| 532 | // alphabetical references |
| 533 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause")); |
| 534 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage")); |
| 535 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState")); |
| 536 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace")); |
| 537 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 538 | }; |
| 539 | }; |
| 540 | |
| 541 | struct StackTraceElementOffsets : public CheckOffsets { |
| 542 | StackTraceElementOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 543 | instance = true; |
| 544 | size = sizeof(StackTraceElement); |
| 545 | class_descriptor = "Ljava/lang/StackTraceElement;"; |
| 546 | |
| 547 | // alphabetical references |
| 548 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass")); |
| 549 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName")); |
| 550 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName")); |
| 551 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 552 | }; |
| 553 | }; |
| 554 | |
| 555 | struct ClassLoaderOffsets : public CheckOffsets { |
| 556 | ClassLoaderOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 557 | instance = true; |
| 558 | size = sizeof(ClassLoader); |
| 559 | class_descriptor = "Ljava/lang/ClassLoader;"; |
| 560 | |
| 561 | // alphabetical references |
| 562 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages")); |
| 563 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 564 | }; |
| 565 | }; |
| 566 | |
| 567 | struct BaseDexClassLoaderOffsets : public CheckOffsets { |
| 568 | BaseDexClassLoaderOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 569 | instance = true; |
| 570 | size = sizeof(BaseDexClassLoader); |
| 571 | class_descriptor = "Ldalvik/system/BaseDexClassLoader;"; |
| 572 | |
| 573 | // alphabetical references |
| 574 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_path_), "originalPath")); |
| 575 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, path_list_), "pathList")); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 576 | }; |
| 577 | }; |
| 578 | |
| 579 | struct PathClassLoaderOffsets : public CheckOffsets { |
| 580 | PathClassLoaderOffsets() { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 581 | instance = true; |
| 582 | size = sizeof(PathClassLoader); |
| 583 | class_descriptor = "Ldalvik/system/PathClassLoader;"; |
| 584 | }; |
| 585 | }; |
| 586 | |
| 587 | struct ClassClassOffsets : public CheckOffsets { |
| 588 | ClassClassOffsets() { |
| 589 | instance = false; |
| 590 | size = sizeof(ClassClass); |
| 591 | class_descriptor = "Ljava/lang/Class;"; |
| 592 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 593 | // padding 32-bit |
| 594 | CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4, |
| 595 | OFFSETOF_MEMBER(ClassClass, serialVersionUID_)); |
| 596 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 597 | // alphabetical 64-bit |
| 598 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID")); |
| 599 | }; |
| 600 | }; |
| 601 | |
| 602 | struct StringClassOffsets : public CheckOffsets { |
| 603 | StringClassOffsets() { |
| 604 | instance = false; |
| 605 | size = sizeof(StringClass); |
| 606 | class_descriptor = "Ljava/lang/String;"; |
| 607 | |
| 608 | // alphabetical references |
| 609 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII")); |
| 610 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER")); |
| 611 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 612 | // padding 32-bit |
| 613 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR")); |
| 614 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 615 | // alphabetical 64-bit |
| 616 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID")); |
| 617 | }; |
| 618 | }; |
| 619 | |
| 620 | struct FieldClassOffsets : public CheckOffsets { |
| 621 | FieldClassOffsets() { |
| 622 | instance = false; |
| 623 | size = sizeof(FieldClass); |
| 624 | class_descriptor = "Ljava/lang/reflect/Field;"; |
| 625 | |
| 626 | // alphabetical references |
| 627 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, ORDER_BY_NAME_AND_DECLARING_CLASS_), "ORDER_BY_NAME_AND_DECLARING_CLASS")); |
| 628 | |
| 629 | // alphabetical 32-bit |
| 630 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BOOLEAN_), "TYPE_BOOLEAN")); |
| 631 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BYTE_), "TYPE_BYTE")); |
| 632 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_CHAR_), "TYPE_CHAR")); |
| 633 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_DOUBLE_), "TYPE_DOUBLE")); |
| 634 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_FLOAT_), "TYPE_FLOAT")); |
| 635 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_INTEGER_), "TYPE_INTEGER")); |
| 636 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_LONG_), "TYPE_LONG")); |
| 637 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_SHORT_), "TYPE_SHORT")); |
| 638 | }; |
| 639 | }; |
| 640 | |
| 641 | struct MethodClassOffsets : public CheckOffsets { |
| 642 | MethodClassOffsets() { |
| 643 | instance = false; |
| 644 | size = sizeof(MethodClass); |
| 645 | class_descriptor = "Ljava/lang/reflect/Method;"; |
| 646 | |
| 647 | // alphabetical references |
| 648 | offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, NO_ANNOTATIONS_), "NO_ANNOTATIONS")); |
| 649 | 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] | 650 | }; |
| 651 | }; |
| 652 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 653 | // 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] | 654 | // 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] | 655 | // ClassLinker::LinkFields. |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 656 | TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 657 | EXPECT_TRUE(ObjectOffsets().Check()); |
| 658 | EXPECT_TRUE(AccessibleObjectOffsets().Check()); |
| 659 | EXPECT_TRUE(FieldOffsets().Check()); |
| 660 | EXPECT_TRUE(MethodOffsets().Check()); |
| 661 | EXPECT_TRUE(ClassOffsets().Check()); |
| 662 | EXPECT_TRUE(StringOffsets().Check()); |
| 663 | EXPECT_TRUE(ThrowableOffsets().Check()); |
| 664 | EXPECT_TRUE(StackTraceElementOffsets().Check()); |
| 665 | EXPECT_TRUE(ClassLoaderOffsets().Check()); |
| 666 | EXPECT_TRUE(BaseDexClassLoaderOffsets().Check()); |
| 667 | EXPECT_TRUE(PathClassLoaderOffsets().Check()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 668 | |
| 669 | EXPECT_TRUE(ClassClassOffsets().Check()); |
| 670 | EXPECT_TRUE(StringClassOffsets().Check()); |
| 671 | EXPECT_TRUE(FieldClassOffsets().Check()); |
| 672 | EXPECT_TRUE(MethodClassOffsets().Check()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame^] | 675 | TEST_F(ClassLinkerTest, FindClassNonexistent) { |
| 676 | AssertNonExistentClass("NoSuchClass;"); |
| 677 | AssertNonExistentClass("LNoSuchClass;"); |
| 678 | } |
| 679 | |
| 680 | TEST_F(ClassLinkerTest, FindClassNested) { |
| 681 | const ClassLoader* class_loader = LoadDex("Nested"); |
| 682 | |
| 683 | Class* outer = class_linker_->FindClass("LNested;", class_loader); |
| 684 | ASSERT_TRUE(outer != NULL); |
| 685 | EXPECT_EQ(0U, outer->NumVirtualMethods()); |
| 686 | EXPECT_EQ(1U, outer->NumDirectMethods()); |
| 687 | |
| 688 | Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader); |
| 689 | ASSERT_TRUE(inner != NULL); |
| 690 | EXPECT_EQ(0U, inner->NumVirtualMethods()); |
| 691 | EXPECT_EQ(1U, inner->NumDirectMethods()); |
| 692 | } |
| 693 | |
| 694 | TEST_F(ClassLinkerTest, FindClass_Primitives) { |
| 695 | StringPiece expected = "BCDFIJSZV"; |
| 696 | for (int ch = 0; ch < 255; ch++) { |
| 697 | char* s = reinterpret_cast<char*>(&ch); |
| 698 | StringPiece descriptor(s, 1); |
| 699 | if (expected.find(ch) == StringPiece::npos) { |
| 700 | AssertNonExistentClass(descriptor); |
| 701 | } else { |
| 702 | AssertPrimitiveClass(descriptor); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | TEST_F(ClassLinkerTest, FindClass) { |
| 708 | Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;"); |
| 709 | ASSERT_TRUE(JavaLangObject != NULL); |
| 710 | ASSERT_TRUE(JavaLangObject->GetClass() != NULL); |
| 711 | ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass()); |
| 712 | EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass()); |
| 713 | ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;")); |
| 714 | EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL); |
| 715 | EXPECT_FALSE(JavaLangObject->HasSuperClass()); |
| 716 | EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL); |
| 717 | EXPECT_FALSE(JavaLangObject->IsErroneous()); |
| 718 | EXPECT_TRUE(JavaLangObject->IsVerified()); |
| 719 | EXPECT_TRUE(JavaLangObject->IsResolved()); |
| 720 | EXPECT_FALSE(JavaLangObject->IsArrayInstance()); |
| 721 | EXPECT_FALSE(JavaLangObject->IsArrayClass()); |
| 722 | EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL); |
| 723 | EXPECT_FALSE(JavaLangObject->IsInterface()); |
| 724 | EXPECT_TRUE(JavaLangObject->IsPublic()); |
| 725 | EXPECT_FALSE(JavaLangObject->IsFinal()); |
| 726 | EXPECT_FALSE(JavaLangObject->IsPrimitive()); |
| 727 | EXPECT_FALSE(JavaLangObject->IsSynthetic()); |
| 728 | EXPECT_EQ(2U, JavaLangObject->NumDirectMethods()); |
| 729 | EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods()); |
| 730 | EXPECT_EQ(2U, JavaLangObject->NumInstanceFields()); |
| 731 | EXPECT_TRUE(JavaLangObject->GetInstanceField(0)->GetName()->Equals("shadow$_klass_")); |
| 732 | EXPECT_TRUE(JavaLangObject->GetInstanceField(1)->GetName()->Equals("shadow$_monitor_")); |
| 733 | |
| 734 | EXPECT_EQ(0U, JavaLangObject->NumStaticFields()); |
| 735 | EXPECT_EQ(0U, JavaLangObject->NumInterfaces()); |
| 736 | |
| 737 | const ClassLoader* class_loader = LoadDex("MyClass"); |
| 738 | AssertNonExistentClass("LMyClass;"); |
| 739 | Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader); |
| 740 | ASSERT_TRUE(MyClass != NULL); |
| 741 | ASSERT_TRUE(MyClass->GetClass() != NULL); |
| 742 | ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass()); |
| 743 | EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass()); |
| 744 | ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;")); |
| 745 | EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject); |
| 746 | EXPECT_TRUE(MyClass->HasSuperClass()); |
| 747 | EXPECT_EQ(class_loader, MyClass->GetClassLoader()); |
| 748 | EXPECT_TRUE(MyClass->GetStatus() == Class::kStatusResolved); |
| 749 | EXPECT_FALSE(MyClass->IsErroneous()); |
| 750 | EXPECT_FALSE(MyClass->IsVerified()); |
| 751 | EXPECT_TRUE(MyClass->IsResolved()); |
| 752 | EXPECT_FALSE(MyClass->IsArrayInstance()); |
| 753 | EXPECT_FALSE(MyClass->IsArrayClass()); |
| 754 | EXPECT_TRUE(MyClass->GetComponentType() == NULL); |
| 755 | EXPECT_FALSE(MyClass->IsInterface()); |
| 756 | EXPECT_FALSE(MyClass->IsPublic()); |
| 757 | EXPECT_FALSE(MyClass->IsFinal()); |
| 758 | EXPECT_FALSE(MyClass->IsPrimitive()); |
| 759 | EXPECT_FALSE(MyClass->IsSynthetic()); |
| 760 | EXPECT_EQ(1U, MyClass->NumDirectMethods()); |
| 761 | EXPECT_EQ(0U, MyClass->NumVirtualMethods()); |
| 762 | EXPECT_EQ(0U, MyClass->NumInstanceFields()); |
| 763 | EXPECT_EQ(0U, MyClass->NumStaticFields()); |
| 764 | EXPECT_EQ(0U, MyClass->NumInterfaces()); |
| 765 | |
| 766 | EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass()); |
| 767 | |
| 768 | // created by class_linker |
| 769 | AssertArrayClass("[C", "C", NULL); |
| 770 | AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL); |
| 771 | // synthesized on the fly |
| 772 | AssertArrayClass("[[C", "[C", NULL); |
| 773 | AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader); |
| 774 | // or not available at all |
| 775 | AssertNonExistentClass("[[[[LNonExistentClass;"); |
| 776 | } |
| 777 | |
| 778 | TEST_F(ClassLinkerTest, LibCore) { |
| 779 | AssertDexFile(java_lang_dex_file_.get(), NULL); |
| 780 | } |
| 781 | |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 782 | // The first reference array element must be a multiple of 8 bytes from the |
| 783 | // start of the object |
| 784 | TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) { |
| 785 | Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 786 | ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0); |
| 787 | uint32_t array_offset = reinterpret_cast<uint32_t>(array); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 788 | uint32_t data_offset = |
| 789 | array_offset + ObjectArray<String>::DataOffset().Uint32Value(); |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 790 | EXPECT_EQ(16U, data_offset - array_offset); |
| 791 | } |
| 792 | |
| 793 | TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 794 | LongArray* array = LongArray::Alloc(0); |
| 795 | EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass()); |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 796 | uint32_t array_offset = reinterpret_cast<uint32_t>(array); |
| 797 | uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData()); |
| 798 | EXPECT_EQ(16U, data_offset - array_offset); |
| 799 | } |
| 800 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 801 | TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 802 | const ClassLoader* class_loader_1 = LoadDex("MyClass"); |
| 803 | const ClassLoader* class_loader_2 = LoadDex("MyClass"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 804 | Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1); |
| 805 | Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2); |
| 806 | EXPECT_TRUE(MyClass_1 != NULL); |
| 807 | EXPECT_TRUE(MyClass_2 != NULL); |
| 808 | EXPECT_NE(MyClass_1, MyClass_2); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 811 | TEST_F(ClassLinkerTest, StaticFields) { |
| 812 | // TODO: uncomment expectations of initial values when InitializeClass works |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 813 | const ClassLoader* class_loader = LoadDex("Statics"); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 814 | Class* statics = class_linker_->FindClass("LStatics;", class_loader); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 815 | class_linker_->EnsureInitialized(statics); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 816 | |
| 817 | EXPECT_EQ(10U, statics->NumStaticFields()); |
| 818 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 819 | Field* s0 = statics->FindStaticField("s0", class_linker_->FindClass("Z", class_loader)); |
| 820 | EXPECT_TRUE(s0->GetClass()->GetDescriptor()->Equals("Ljava/lang/reflect/Field;")); |
| 821 | EXPECT_TRUE(s0->GetType()->IsPrimitiveBoolean()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 822 | // EXPECT_EQ(true, s0->GetBoolean(NULL)); // TODO: needs clinit to be run? |
| 823 | s0->SetBoolean(NULL, false); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 824 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 825 | Field* s1 = statics->FindStaticField("s1", class_linker_->FindClass("B", class_loader)); |
| 826 | EXPECT_TRUE(s1->GetType()->IsPrimitiveByte()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 827 | // EXPECT_EQ(5, s1->GetByte(NULL)); // TODO: needs clinit to be run? |
| 828 | s1->SetByte(NULL, 6); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 829 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 830 | Field* s2 = statics->FindStaticField("s2", class_linker_->FindClass("C", class_loader)); |
| 831 | EXPECT_TRUE(s2->GetType()->IsPrimitiveChar()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 832 | // EXPECT_EQ('a', s2->GetChar(NULL)); // TODO: needs clinit to be run? |
| 833 | s2->SetChar(NULL, 'b'); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 834 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 835 | Field* s3 = statics->FindStaticField("s3", class_linker_->FindClass("S", class_loader)); |
| 836 | EXPECT_TRUE(s3->GetType()->IsPrimitiveShort()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 837 | // EXPECT_EQ(65000, s3->GetShort(NULL)); // TODO: needs clinit to be run? |
| 838 | s3->SetShort(NULL, 65001); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 839 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 840 | Field* s4 = statics->FindStaticField("s4", class_linker_->FindClass("I", class_loader)); |
| 841 | EXPECT_TRUE(s4->GetType()->IsPrimitiveInt()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 842 | // EXPECT_EQ(2000000000, s4->GetInt(NULL)); // TODO: needs clinit to be run? |
| 843 | s4->SetInt(NULL, 2000000001); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 844 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 845 | Field* s5 = statics->FindStaticField("s5", class_linker_->FindClass("J", class_loader)); |
| 846 | EXPECT_TRUE(s5->GetType()->IsPrimitiveLong()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 847 | // EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL)); // TODO: needs clinit to be run? |
| 848 | s5->SetLong(NULL, 0x34567890abcdef12LL); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 849 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 850 | Field* s6 = statics->FindStaticField("s6", class_linker_->FindClass("F", class_loader)); |
| 851 | EXPECT_TRUE(s6->GetType()->IsPrimitiveFloat()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 852 | // EXPECT_EQ(0.5, s6->GetFloat(NULL)); // TODO: needs clinit to be run? |
| 853 | s6->SetFloat(NULL, 0.75); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 854 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 855 | Field* s7 = statics->FindStaticField("s7", class_linker_->FindClass("D", class_loader)); |
| 856 | EXPECT_TRUE(s7->GetType()->IsPrimitiveDouble()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 857 | // EXPECT_EQ(16777217, s7->GetDouble(NULL)); // TODO: needs clinit to be run? |
| 858 | s7->SetDouble(NULL, 16777219); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 859 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 860 | Field* s8 = statics->FindStaticField("s8", class_linker_->FindClass("Ljava/lang/Object;", class_loader)); |
| 861 | EXPECT_FALSE(s8->GetType()->IsPrimitive()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 862 | // EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android")); // TODO: needs clinit to be run? |
| 863 | s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 864 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 865 | Field* s9 = statics->FindStaticField("s9", class_linker_->FindClass("[Ljava/lang/Object;", class_loader)); |
| 866 | EXPECT_TRUE(s9->GetType()->IsArrayClass()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 867 | // EXPECT_EQ(NULL, s9->GetObject(NULL)); // TODO: needs clinit to be run? |
| 868 | s9->SetObject(NULL, NULL); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 869 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 870 | EXPECT_EQ(false, s0->GetBoolean(NULL)); |
| 871 | EXPECT_EQ(6, s1->GetByte(NULL)); |
| 872 | EXPECT_EQ('b', s2->GetChar(NULL)); |
| 873 | EXPECT_EQ(65001, s3->GetShort(NULL)); |
| 874 | EXPECT_EQ(2000000001, s4->GetInt(NULL)); |
| 875 | EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL)); |
| 876 | EXPECT_EQ(0.75, s6->GetFloat(NULL)); |
| 877 | EXPECT_EQ(16777219, s7->GetDouble(NULL)); |
| 878 | EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot")); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 881 | TEST_F(ClassLinkerTest, Interfaces) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 882 | const ClassLoader* class_loader = LoadDex("Interfaces"); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 883 | Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader); |
| 884 | Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 885 | Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 886 | Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 887 | Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 888 | EXPECT_TRUE(I->IsAssignableFrom(A)); |
| 889 | EXPECT_TRUE(J->IsAssignableFrom(A)); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 890 | EXPECT_TRUE(J->IsAssignableFrom(K)); |
| 891 | EXPECT_TRUE(K->IsAssignableFrom(B)); |
| 892 | EXPECT_TRUE(J->IsAssignableFrom(B)); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 893 | |
| 894 | Method* Ii = I->FindVirtualMethod("i", "()V"); |
| 895 | Method* Jj1 = J->FindVirtualMethod("j1", "()V"); |
| 896 | Method* Jj2 = J->FindVirtualMethod("j2", "()V"); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 897 | Method* Kj1 = K->FindInterfaceMethod("j1", "()V"); |
| 898 | Method* Kj2 = K->FindInterfaceMethod("j2", "()V"); |
| 899 | Method* Kk = K->FindInterfaceMethod("k", "()V"); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 900 | Method* Ai = A->FindVirtualMethod("i", "()V"); |
| 901 | Method* Aj1 = A->FindVirtualMethod("j1", "()V"); |
| 902 | Method* Aj2 = A->FindVirtualMethod("j2", "()V"); |
| 903 | ASSERT_TRUE(Ii != NULL); |
| 904 | ASSERT_TRUE(Jj1 != NULL); |
| 905 | ASSERT_TRUE(Jj2 != NULL); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 906 | ASSERT_TRUE(Kj1 != NULL); |
| 907 | ASSERT_TRUE(Kj2 != NULL); |
| 908 | ASSERT_TRUE(Kk != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 909 | ASSERT_TRUE(Ai != NULL); |
| 910 | ASSERT_TRUE(Aj1 != NULL); |
| 911 | ASSERT_TRUE(Aj2 != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 912 | EXPECT_NE(Ii, Ai); |
| 913 | EXPECT_NE(Jj1, Aj1); |
| 914 | EXPECT_NE(Jj2, Aj2); |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 915 | EXPECT_EQ(Kj1, Jj1); |
| 916 | EXPECT_EQ(Kj2, Jj2); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 917 | EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii)); |
| 918 | EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1)); |
| 919 | EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2)); |
| 920 | EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii)); |
| 921 | EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1)); |
| 922 | EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2)); |
| 923 | } |
| 924 | |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 925 | TEST_F(ClassLinkerTest, InitializeStaticStorageFromCode) { |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 926 | // pretend we are trying to get the static storage for the Statics class. |
| 927 | |
| 928 | // case 1, get the uninitialized storage from Statics.<clinit> |
| 929 | // case 2, get the initialized storage from Statics.getS8 |
| 930 | |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 931 | const ClassLoader* class_loader = LoadDex("Statics"); |
| 932 | const DexFile* dex_file = ClassLoader::GetClassPath(class_loader)[0]; |
| 933 | CHECK(dex_file != NULL); |
| 934 | |
| 935 | Class* Statics = class_linker_->FindClass("LStatics;", class_loader); |
| 936 | Method* clinit = Statics->FindDirectMethod("<clinit>", "()V"); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 937 | Method* getS8 = Statics->FindDirectMethod("getS8", "()Ljava/lang/Object;"); |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 938 | uint32_t type_idx = FindTypeIdxByDescriptor(*dex_file, "LStatics;"); |
| 939 | |
| 940 | EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 941 | StaticStorageBase* uninit = class_linker_->InitializeStaticStorageFromCode(type_idx, clinit); |
| 942 | EXPECT_TRUE(uninit != NULL); |
| 943 | EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL); |
| 944 | StaticStorageBase* init = class_linker_->InitializeStaticStorageFromCode(type_idx, getS8); |
| 945 | EXPECT_TRUE(init != NULL); |
| 946 | EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx)); |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 949 | } // namespace art |