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