blob: 3362fdc34cdedaf92670ed5ec498a1f8c4c32a71 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07004
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07005#include <string>
6
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
8#include "common_test.h"
Brian Carlstromc4fa2c02011-08-21 03:00:12 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_file.h"
11#include "heap.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012
13namespace art {
14
Brian Carlstromf734cf52011-08-17 16:28:14 -070015class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016 protected:
Elliott Hughesbd935992011-08-22 11:59:34 -070017 void AssertNonExistentClass(const StringPiece& descriptor) {
Carl Shapiro7a909592011-07-24 19:21:59 -070018 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070019 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 Carlstrom578bbdc2011-07-21 14:07:47 -070025 }
26
27 void AssertPrimitiveClass(const StringPiece& descriptor) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070028 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor));
29 }
30
31 void AssertPrimitiveClass(const StringPiece& descriptor, const Class* primitive) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032 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 Carlstrom9cff8e12011-08-18 16:47:29 -070036 ASSERT_TRUE(primitive->GetDescriptor()->Equals(descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070037 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
38 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070039 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040 EXPECT_TRUE(primitive->GetStatus() == Class::kStatusInitialized);
41 EXPECT_FALSE(primitive->IsErroneous());
42 EXPECT_TRUE(primitive->IsVerified());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070043 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -070044 EXPECT_FALSE(primitive->IsArrayInstance());
45 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070046 EXPECT_EQ(0, primitive->GetArrayRank());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047 EXPECT_FALSE(primitive->IsInterface());
48 EXPECT_TRUE(primitive->IsPublic());
49 EXPECT_TRUE(primitive->IsFinal());
50 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070051 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 EXPECT_EQ(0U, primitive->NumDirectMethods());
53 EXPECT_EQ(0U, primitive->NumVirtualMethods());
54 EXPECT_EQ(0U, primitive->NumInstanceFields());
55 EXPECT_EQ(0U, primitive->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070056 EXPECT_EQ(0U, primitive->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 }
58
59 void AssertArrayClass(const StringPiece& array_descriptor,
60 int32_t array_rank,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070061 const StringPiece& component_type,
Brian Carlstrom8a487412011-08-29 20:08:52 -070062 const ClassLoader* class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070063 Class* array = class_linker_->FindClass(array_descriptor, class_loader);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070064 EXPECT_EQ(array_rank, array->GetArrayRank());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070065 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 Carlstrom578bbdc2011-07-21 14:07:47 -070071 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 Carlstrom9cff8e12011-08-18 16:47:29 -070075 ASSERT_TRUE(array->GetDescriptor()->Equals(array_descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070077 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 EXPECT_TRUE(array->HasSuperClass());
79 ASSERT_TRUE(array->GetComponentType() != NULL);
80 ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081 EXPECT_TRUE(array->GetStatus() == Class::kStatusInitialized);
82 EXPECT_FALSE(array->IsErroneous());
83 EXPECT_TRUE(array->IsVerified());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070084 EXPECT_TRUE(array->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -070085 EXPECT_FALSE(array->IsArrayInstance());
86 EXPECT_TRUE(array->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070087 EXPECT_LE(1, array->GetArrayRank());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 EXPECT_FALSE(array->IsInterface());
89 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
90 EXPECT_TRUE(array->IsFinal());
91 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070092 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 EXPECT_EQ(0U, array->NumDirectMethods());
94 EXPECT_EQ(0U, array->NumVirtualMethods());
95 EXPECT_EQ(0U, array->NumInstanceFields());
96 EXPECT_EQ(0U, array->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070097 EXPECT_EQ(2U, array->NumInterfaces());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070098 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -070099
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700100 void AssertMethod(Class* klass, Method* method) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700101 EXPECT_TRUE(method != NULL);
102 EXPECT_TRUE(method->GetName() != NULL);
103 EXPECT_TRUE(method->GetSignature() != NULL);
104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105 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 Carlstromc4fa2c02011-08-21 03:00:12 -0700123 }
124
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700125 void AssertField(Class* klass, Field* field) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700126 EXPECT_TRUE(field != NULL);
127 EXPECT_EQ(klass, field->GetDeclaringClass());
128 EXPECT_TRUE(field->GetName() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129 EXPECT_TRUE(field->GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700130 }
131
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700132 void AssertClass(const StringPiece& descriptor, Class* klass) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700133 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700134 if (klass->GetDescriptor()->Equals(String::AllocFromModifiedUtf8("Ljava/lang/Object;"))) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700135 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700136 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700137 EXPECT_TRUE(klass->HasSuperClass());
138 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700139 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700140 EXPECT_TRUE(klass->GetDexCache() != NULL);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700141 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700142 EXPECT_FALSE(klass->IsErroneous());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700143 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700144 EXPECT_TRUE(klass->IsLoaded());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700145 EXPECT_FALSE(klass->IsArrayClass());
146 EXPECT_EQ(0, klass->GetArrayRank());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700147 EXPECT_TRUE(klass->IsInSamePackage(klass));
148 EXPECT_TRUE(Class::IsInSamePackage(klass->GetDescriptor(), klass->GetDescriptor()));
149 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700150 EXPECT_TRUE(klass->IsAbstract());
151 if (klass->NumDirectMethods() == 1) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700152 EXPECT_TRUE(klass->GetDirectMethod(0)->GetName()->Equals("<clinit>"));
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700153 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700154 } else {
155 EXPECT_EQ(0U, klass->NumDirectMethods());
156 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700157 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700158 if (!klass->IsSynthetic()) {
159 EXPECT_NE(0U, klass->NumDirectMethods());
160 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700161 }
162 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700163 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700164 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700166 }
167 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700168 EXPECT_FALSE(klass->IsAbstract());
169 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700170 }
171 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700172 EXPECT_FALSE(klass->IsFinal());
173 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700174 }
175
176 EXPECT_FALSE(klass->IsPrimitive());
177 EXPECT_TRUE(klass->CanAccess(klass));
178
179 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700180 Method* method = klass->GetDirectMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700181 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700182 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400183 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700184 }
185
186 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700187 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700188 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700189 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700190 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700191 }
192
193 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400194 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700195 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700196 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700197 }
198
199 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400200 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700201 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700202 EXPECT_TRUE(field->IsStatic());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700203 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700204
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 Wilson35baaab2011-08-10 16:18:03 -0400208 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700210 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700212 }
213 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400214 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 ASSERT_TRUE(field_type != NULL);
217 EXPECT_TRUE(field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700218 }
219
220 size_t total_num_reference_instance_fields = 0;
221 Class* k = klass;
222 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700223 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
224 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700225 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700226 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700227 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700228 }
229
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700230 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 Carlstromb63ec392011-08-27 17:38:27 -0700238 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700239 AssertArrayClass(descriptor, klass);
240 } else {
241 AssertClass(descriptor, klass);
242 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700243 }
244
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700245 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700246 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700247
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700248 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700249 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700250 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700251 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700252 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700253 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700254 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700255 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 Carlstrom75cb3b42011-07-28 02:13:36 -0700260 class_linker_->VisitRoots(TestRootVisitor, NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700261 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700262
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700263 static void TestRootVisitor(const Object* root, void* arg) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700264 EXPECT_TRUE(root != NULL);
265 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700266};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700267
268TEST_F(ClassLinkerTest, FindClassNonexistent) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700269 AssertNonExistentClass("NoSuchClass;");
270 AssertNonExistentClass("LNoSuchClass;");
Brian Carlstrom934486c2011-07-12 23:42:50 -0700271}
272
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700273TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700274 const ClassLoader* class_loader = LoadDex("Nested");
Brian Carlstrom934486c2011-07-12 23:42:50 -0700275
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 Class* outer = class_linker_->FindClass("LNested;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700277 ASSERT_TRUE(outer != NULL);
278 EXPECT_EQ(0U, outer->NumVirtualMethods());
279 EXPECT_EQ(1U, outer->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700280
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700281 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700282 ASSERT_TRUE(inner != NULL);
283 EXPECT_EQ(0U, inner->NumVirtualMethods());
284 EXPECT_EQ(1U, inner->NumDirectMethods());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700285}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700286
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700287TEST_F(ClassLinkerTest, FindClass_Primitives) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700288 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 Hughesbd935992011-08-22 11:59:34 -0700293 AssertNonExistentClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700294 } else {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700295 AssertPrimitiveClass(descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700296 }
297 }
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700298}
Elliott Hughesa5780da2011-07-17 11:39:39 -0700299
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700300TEST_F(ClassLinkerTest, FindClass) {
301 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom934486c2011-07-12 23:42:50 -0700302 ASSERT_TRUE(JavaLangObject != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700303 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
304 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700305 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700306 ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700307 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
308 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700309 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700310 EXPECT_FALSE(JavaLangObject->IsErroneous());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700311 EXPECT_TRUE(JavaLangObject->IsVerified());
312 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700313 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
314 EXPECT_FALSE(JavaLangObject->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 EXPECT_EQ(0, JavaLangObject->GetArrayRank());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700316 EXPECT_FALSE(JavaLangObject->IsInterface());
317 EXPECT_TRUE(JavaLangObject->IsPublic());
318 EXPECT_FALSE(JavaLangObject->IsFinal());
319 EXPECT_FALSE(JavaLangObject->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700320 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700321 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
322 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700323 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 Shapirof10edca2011-07-14 23:16:21 -0700327 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700328 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700329
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700330 const ClassLoader* class_loader = LoadDex("MyClass");
Elliott Hughesbd935992011-08-22 11:59:34 -0700331 AssertNonExistentClass("LMyClass;");
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700332 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333 ASSERT_TRUE(MyClass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700334 ASSERT_TRUE(MyClass->GetClass() != NULL);
335 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700336 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700337 ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;"));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
339 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700340 EXPECT_EQ(class_loader, MyClass->GetClassLoader());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700341 EXPECT_TRUE(MyClass->GetStatus() == Class::kStatusResolved);
342 EXPECT_FALSE(MyClass->IsErroneous());
343 EXPECT_FALSE(MyClass->IsVerified());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700344 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700345 EXPECT_FALSE(MyClass->IsArrayInstance());
346 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 EXPECT_EQ(0, JavaLangObject->GetArrayRank());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700348 EXPECT_FALSE(MyClass->IsInterface());
349 EXPECT_FALSE(MyClass->IsPublic());
350 EXPECT_FALSE(MyClass->IsFinal());
351 EXPECT_FALSE(MyClass->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700352 EXPECT_FALSE(MyClass->IsSynthetic());
Carl Shapirof10edca2011-07-14 23:16:21 -0700353 EXPECT_EQ(1U, MyClass->NumDirectMethods());
354 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
355 EXPECT_EQ(0U, MyClass->NumInstanceFields());
356 EXPECT_EQ(0U, MyClass->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700357 EXPECT_EQ(0U, MyClass->NumInterfaces());
Brian Carlstroma0808032011-07-18 00:39:23 -0700358
359 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
360
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700361 // created by class_linker
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700362 AssertArrayClass("[C", 1, "C", NULL);
363 AssertArrayClass("[Ljava/lang/Object;", 1, "Ljava/lang/Object;", NULL);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700364 // synthesized on the fly
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700365 AssertArrayClass("[[C", 2, "C", NULL);
366 AssertArrayClass("[[[LMyClass;", 3, "LMyClass;", class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700367 // or not available at all
Elliott Hughesbd935992011-08-22 11:59:34 -0700368 AssertNonExistentClass("[[[[LNonExistentClass;");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700369}
370
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700371TEST_F(ClassLinkerTest, LibCore) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700372 AssertDexFile(java_lang_dex_file_.get(), NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700373}
374
Brian Carlstrom693267a2011-09-06 09:25:34 -0700375struct 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
381struct 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
445struct 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
454struct 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
462struct 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
478struct 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
522struct 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
560struct 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
571struct 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
583struct 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
594struct 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
603struct 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
612struct PathClassLoaderOffsets : public CheckOffsets {
613 PathClassLoaderOffsets() {
614 s = sizeof(PathClassLoader);
615 c = "Ldalvik/system/PathClassLoader;";
616 };
617};
618
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400619// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400620// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700621// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400622TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700623 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 Carlstrom74eb46a2011-08-02 20:10:14 -0700634}
635
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400636// The first reference array element must be a multiple of 8 bytes from the
637// start of the object
638TEST_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 Rogers0cfe1fb2011-08-26 03:29:44 -0700642 uint32_t data_offset =
643 array_offset + ObjectArray<String>::DataOffset().Uint32Value();
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400644 EXPECT_EQ(16U, data_offset - array_offset);
645}
646
647TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700648 LongArray* array = LongArray::Alloc(0);
649 EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400650 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 Carlstrom74eb46a2011-08-02 20:10:14 -0700655TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700656 const ClassLoader* class_loader_1 = LoadDex("MyClass");
657 const ClassLoader* class_loader_2 = LoadDex("MyClass");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700658 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 Wilsonac5b9e22011-07-27 15:11:13 -0400663}
664
Jesse Wilson7833bd22011-08-09 18:31:44 -0400665TEST_F(ClassLinkerTest, StaticFields) {
666 // TODO: uncomment expectations of initial values when InitializeClass works
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700667 const ClassLoader* class_loader = LoadDex("Statics");
Jesse Wilson7833bd22011-08-09 18:31:44 -0400668 Class* statics = class_linker_->FindClass("LStatics;", class_loader);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700669 class_linker_->EnsureInitialized(statics);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400670
671 EXPECT_EQ(10U, statics->NumStaticFields());
672
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700673 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 Carlstrom4873d462011-08-21 15:23:39 -0700676 // EXPECT_EQ(true, s0->GetBoolean(NULL)); // TODO: needs clinit to be run?
677 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400678
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700679 Field* s1 = statics->FindStaticField("s1", class_linker_->FindClass("B", class_loader));
680 EXPECT_TRUE(s1->GetType()->IsPrimitiveByte());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700681 // EXPECT_EQ(5, s1->GetByte(NULL)); // TODO: needs clinit to be run?
682 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400683
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700684 Field* s2 = statics->FindStaticField("s2", class_linker_->FindClass("C", class_loader));
685 EXPECT_TRUE(s2->GetType()->IsPrimitiveChar());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700686 // EXPECT_EQ('a', s2->GetChar(NULL)); // TODO: needs clinit to be run?
687 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400688
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 Field* s3 = statics->FindStaticField("s3", class_linker_->FindClass("S", class_loader));
690 EXPECT_TRUE(s3->GetType()->IsPrimitiveShort());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700691 // EXPECT_EQ(65000, s3->GetShort(NULL)); // TODO: needs clinit to be run?
692 s3->SetShort(NULL, 65001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400693
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700694 Field* s4 = statics->FindStaticField("s4", class_linker_->FindClass("I", class_loader));
695 EXPECT_TRUE(s4->GetType()->IsPrimitiveInt());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700696 // EXPECT_EQ(2000000000, s4->GetInt(NULL)); // TODO: needs clinit to be run?
697 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400698
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699 Field* s5 = statics->FindStaticField("s5", class_linker_->FindClass("J", class_loader));
700 EXPECT_TRUE(s5->GetType()->IsPrimitiveLong());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700701 // EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL)); // TODO: needs clinit to be run?
702 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400703
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700704 Field* s6 = statics->FindStaticField("s6", class_linker_->FindClass("F", class_loader));
705 EXPECT_TRUE(s6->GetType()->IsPrimitiveFloat());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700706 // EXPECT_EQ(0.5, s6->GetFloat(NULL)); // TODO: needs clinit to be run?
707 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400708
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 Field* s7 = statics->FindStaticField("s7", class_linker_->FindClass("D", class_loader));
710 EXPECT_TRUE(s7->GetType()->IsPrimitiveDouble());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700711 // EXPECT_EQ(16777217, s7->GetDouble(NULL)); // TODO: needs clinit to be run?
712 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400713
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700714 Field* s8 = statics->FindStaticField("s8", class_linker_->FindClass("Ljava/lang/Object;", class_loader));
715 EXPECT_FALSE(s8->GetType()->IsPrimitive());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700716 // EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android")); // TODO: needs clinit to be run?
717 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400718
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700719 Field* s9 = statics->FindStaticField("s9", class_linker_->FindClass("[Ljava/lang/Object;", class_loader));
720 EXPECT_TRUE(s9->GetType()->IsArrayClass());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700721 // EXPECT_EQ(NULL, s9->GetObject(NULL)); // TODO: needs clinit to be run?
722 s9->SetObject(NULL, NULL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400723
Brian Carlstrom4873d462011-08-21 15:23:39 -0700724 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 Wilson7833bd22011-08-09 18:31:44 -0400733}
734
Brian Carlstrom30b94452011-08-25 21:35:26 -0700735TEST_F(ClassLinkerTest, Interfaces) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700736 const ClassLoader* class_loader = LoadDex("Interfaces");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700737 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader);
738 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader);
jeffhao5dbddee2011-09-07 16:38:26 -0700739 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700740 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader);
jeffhao5dbddee2011-09-07 16:38:26 -0700741 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700742 EXPECT_TRUE(I->IsAssignableFrom(A));
743 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700744 EXPECT_TRUE(J->IsAssignableFrom(K));
745 EXPECT_TRUE(K->IsAssignableFrom(B));
746 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700747
748 Method* Ii = I->FindVirtualMethod("i", "()V");
749 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
750 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700751 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
752 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
753 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700754 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);
jeffhao5dbddee2011-09-07 16:38:26 -0700760 ASSERT_TRUE(Kj1 != NULL);
761 ASSERT_TRUE(Kj2 != NULL);
762 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700763 ASSERT_TRUE(Ai != NULL);
764 ASSERT_TRUE(Aj1 != NULL);
765 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700766 EXPECT_NE(Ii, Ai);
767 EXPECT_NE(Jj1, Aj1);
768 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700769 EXPECT_EQ(Kj1, Jj1);
770 EXPECT_EQ(Kj2, Jj2);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700771 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 Carlstrom848a4b32011-09-04 11:29:27 -0700779TEST_F(ClassLinkerTest, InitializeStaticStorageFromCode) {
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700780 // 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 Carlstrom848a4b32011-09-04 11:29:27 -0700785 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 Carlstrom193a44d2011-09-04 12:01:42 -0700791 Method* getS8 = Statics->FindDirectMethod("getS8", "()Ljava/lang/Object;");
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700792 uint32_t type_idx = FindTypeIdxByDescriptor(*dex_file, "LStatics;");
793
794 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700795 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 Carlstrom848a4b32011-09-04 11:29:27 -0700801}
802
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400803} // namespace art