blob: 1b43825edd2b60fd7b38939f245f6f79ccb509b4 [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"
Ian Rogersce9eca62011-10-07 17:11:03 -070012#include "runtime_support.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070013
14namespace art {
15
Brian Carlstromf734cf52011-08-17 16:28:14 -070016class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017 protected:
Brian Carlstromaded5f72011-10-07 17:15:04 -070018 void AssertNonExistentClass(const std::string& descriptor) {
Carl Shapiro7a909592011-07-24 19:21:59 -070019 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070020 Thread* self = Thread::Current();
21 EXPECT_TRUE(self->IsExceptionPending());
22 Object* exception = self->GetException();
23 self->ClearException();
24 Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
25 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026 }
27
Brian Carlstromaded5f72011-10-07 17:15:04 -070028 void AssertPrimitiveClass(const std::string& descriptor) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070029 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor));
30 }
31
Brian Carlstromaded5f72011-10-07 17:15:04 -070032 void AssertPrimitiveClass(const std::string& descriptor, const Class* primitive) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033 ASSERT_TRUE(primitive != NULL);
34 ASSERT_TRUE(primitive->GetClass() != NULL);
35 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
36 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070037 ASSERT_TRUE(primitive->GetDescriptor()->Equals(descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070038 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
39 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070040 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -070041 EXPECT_EQ(Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070043 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070044 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070045 EXPECT_TRUE(primitive->IsVerified());
46 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070047 EXPECT_FALSE(primitive->IsArrayInstance());
48 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070049 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070050 EXPECT_FALSE(primitive->IsInterface());
51 EXPECT_TRUE(primitive->IsPublic());
52 EXPECT_TRUE(primitive->IsFinal());
53 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070054 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070055 EXPECT_EQ(0U, primitive->NumDirectMethods());
56 EXPECT_EQ(0U, primitive->NumVirtualMethods());
57 EXPECT_EQ(0U, primitive->NumInstanceFields());
58 EXPECT_EQ(0U, primitive->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070059 EXPECT_EQ(0U, primitive->NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070060 EXPECT_TRUE(primitive->GetVTable() == NULL);
61 EXPECT_EQ(0, primitive->GetIfTableCount());
62 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070063 }
64
Brian Carlstromaded5f72011-10-07 17:15:04 -070065 void AssertArrayClass(const std::string& array_descriptor,
66 const std::string& component_type,
Brian Carlstrom8a487412011-08-29 20:08:52 -070067 const ClassLoader* class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070068 Class* array = class_linker_->FindClass(array_descriptor, class_loader);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070069 EXPECT_TRUE(array->GetComponentType()->GetDescriptor()->Equals(component_type));
70 EXPECT_EQ(class_loader, array->GetClassLoader());
71 AssertArrayClass(array_descriptor, array);
72 }
73
Brian Carlstromaded5f72011-10-07 17:15:04 -070074 void AssertArrayClass(const std::string& array_descriptor, Class* array) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070075 ASSERT_TRUE(array != NULL);
76 ASSERT_TRUE(array->GetClass() != NULL);
77 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
78 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070079 ASSERT_TRUE(array->GetDescriptor()->Equals(array_descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070081 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070082 EXPECT_TRUE(array->HasSuperClass());
83 ASSERT_TRUE(array->GetComponentType() != NULL);
84 ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -070085 EXPECT_EQ(Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070087 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070088 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070089 EXPECT_TRUE(array->IsVerified());
90 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070091 EXPECT_FALSE(array->IsArrayInstance());
92 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 EXPECT_FALSE(array->IsInterface());
94 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
95 EXPECT_TRUE(array->IsFinal());
96 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070097 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070098 EXPECT_EQ(0U, array->NumDirectMethods());
99 EXPECT_EQ(0U, array->NumVirtualMethods());
100 EXPECT_EQ(0U, array->NumInstanceFields());
101 EXPECT_EQ(0U, array->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700102 EXPECT_EQ(2U, array->NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -0700103 EXPECT_TRUE(array->GetVTable() != NULL);
104 EXPECT_EQ(2, array->GetIfTableCount());
105 ObjectArray<InterfaceEntry>* iftable = array->GetIfTable();
106 ASSERT_TRUE(iftable != NULL);
107 EXPECT_TRUE(iftable->Get(0)->GetInterface()->GetDescriptor()->Equals("Ljava/lang/Cloneable;"));
108 EXPECT_TRUE(iftable->Get(1)->GetInterface()->GetDescriptor()->Equals("Ljava/io/Serializable;"));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700109 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700110
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700111 void AssertMethod(Class* klass, Method* method) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700112 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700113 EXPECT_TRUE(method->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700114 EXPECT_TRUE(method->GetName() != NULL);
115 EXPECT_TRUE(method->GetSignature() != NULL);
116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
118 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
119 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
120 EXPECT_TRUE(method->GetDexCacheResolvedFields() != NULL);
121 EXPECT_TRUE(method->GetDexCacheCodeAndDirectMethods() != NULL);
122 EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
123 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
124 method->GetDexCacheStrings());
125 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
126 method->GetDexCacheResolvedTypes());
127 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
128 method->GetDexCacheResolvedMethods());
129 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedFields(),
130 method->GetDexCacheResolvedFields());
131 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetCodeAndDirectMethods(),
132 method->GetDexCacheCodeAndDirectMethods());
133 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
134 method->GetDexCacheInitializedStaticStorage());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700135 }
136
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700137 void AssertField(Class* klass, Field* field) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700138 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700139 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700140 EXPECT_EQ(klass, field->GetDeclaringClass());
141 EXPECT_TRUE(field->GetName() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 EXPECT_TRUE(field->GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700143 }
144
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145 void AssertClass(const std::string& descriptor, Class* klass) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700146 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700147 SirtRef<String> Object_descriptor(String::AllocFromModifiedUtf8("Ljava/lang/Object;"));
148 if (klass->GetDescriptor()->Equals(Object_descriptor.get())) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700149 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700150 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700151 EXPECT_TRUE(klass->HasSuperClass());
152 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700153 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700154 EXPECT_TRUE(klass->GetClass() != NULL);
155 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700156 EXPECT_TRUE(klass->GetDexCache() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700157 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700158 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700159 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700160 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700161 EXPECT_TRUE(klass->GetComponentType() == NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700162 EXPECT_TRUE(klass->IsInSamePackage(klass));
163 EXPECT_TRUE(Class::IsInSamePackage(klass->GetDescriptor(), klass->GetDescriptor()));
164 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 EXPECT_TRUE(klass->IsAbstract());
166 if (klass->NumDirectMethods() == 1) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700167 EXPECT_TRUE(klass->GetDirectMethod(0)->IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700168 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700169 } else {
170 EXPECT_EQ(0U, klass->NumDirectMethods());
171 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700172 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700173 if (!klass->IsSynthetic()) {
174 EXPECT_NE(0U, klass->NumDirectMethods());
175 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700176 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700177 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
178 for (int i = 0; i < klass->GetIfTableCount(); i++) {
179 const InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i);
180 ASSERT_TRUE(interface_entry != NULL);
181 Class* interface = interface_entry->GetInterface();
182 ASSERT_TRUE(interface != NULL);
183 EXPECT_TRUE(interface_entry->GetInterface() != NULL);
184 if (klass->IsInterface()) {
185 EXPECT_EQ(0U, interface_entry->GetMethodArrayCount());
186 } else {
187 CHECK_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
188 EXPECT_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
189 }
190 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700191 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700192 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700193 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700194 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700195 }
196 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700197 EXPECT_FALSE(klass->IsAbstract());
198 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700199 }
200 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700201 EXPECT_FALSE(klass->IsFinal());
202 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700203 }
204
205 EXPECT_FALSE(klass->IsPrimitive());
206 EXPECT_TRUE(klass->CanAccess(klass));
207
208 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700209 Method* method = klass->GetDirectMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700210 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700211 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400212 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700213 }
214
215 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700217 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700218 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700219 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700220 }
221
222 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400223 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700224 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700225 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700226 }
227
228 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400229 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700230 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700231 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700232 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700233
234 // Confirm that all instances fields are packed together at the start
235 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
236 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400237 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700238 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700239 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700241 }
242 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400243 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700244 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700245 ASSERT_TRUE(field_type != NULL);
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700246 if (!field_type->IsPrimitive()) {
247 // While Reference.referent is not primitive, the ClassLinker
248 // treats it as such so that the garbage collector won't scan it.
249 EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
250 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700251 }
252
253 size_t total_num_reference_instance_fields = 0;
254 Class* k = klass;
255 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700256 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
257 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700258 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700259 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700260 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700261 }
262
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 void AssertDexFileClass(ClassLoader* class_loader, const std::string& descriptor) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700264 ASSERT_TRUE(descriptor != NULL);
265 Class* klass = class_linker_->FindSystemClass(descriptor);
266 ASSERT_TRUE(klass != NULL);
267 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
268 EXPECT_EQ(class_loader, klass->GetClassLoader());
269 if (klass->IsPrimitive()) {
270 AssertPrimitiveClass(descriptor, klass);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700271 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700272 AssertArrayClass(descriptor, klass);
273 } else {
274 AssertClass(descriptor, klass);
275 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 }
277
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700278 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700279 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700280
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700281 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700282 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700283 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700284 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700285 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700286 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700287 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700288 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
289 const DexFile::TypeId& type_id = dex->GetTypeId(i);
290 const char* descriptor = dex->GetTypeDescriptor(type_id);
291 AssertDexFileClass(class_loader, descriptor);
292 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700293 class_linker_->VisitRoots(TestRootVisitor, NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700294 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700295
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700296 static void TestRootVisitor(const Object* root, void* arg) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700297 EXPECT_TRUE(root != NULL);
298 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700299};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700300
Brian Carlstrom693267a2011-09-06 09:25:34 -0700301struct CheckOffset {
302 size_t cpp_offset;
303 const char* java_name;
304 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
305};
306
Elliott Hughes80609252011-09-23 17:24:51 -0700307template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700308struct CheckOffsets {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700309 CheckOffsets(bool is_static, const char* class_descriptor)
310 : is_static(is_static), class_descriptor(class_descriptor) {}
311 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700312 std::string class_descriptor;
313 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700314
315 bool Check() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700316 Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor);
317 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700318
319 bool error = false;
320
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700321 if (!klass->IsClassClass() && !is_static) {
322 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700323 if (sizeof(T) != expected_size) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700324 LG << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700325 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700326 << " Java=" << expected_size
327 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700328 error = true;
329 }
330 }
331
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700332 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700333 if (offsets.size() != num_fields) {
334 LG << "Field count mismatch:"
335 << " class=" << class_descriptor
336 << " Java=" << num_fields
337 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700338 error = true;
339 }
340
Brian Carlstromdbc05252011-09-09 01:59:59 -0700341 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700342 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstromdbc05252011-09-09 01:59:59 -0700343 if (!field->GetName()->Equals(offsets[i].java_name)) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700344 error = true;
345 }
346 }
347 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700348 for (size_t i = 0; i < offsets.size(); i++) {
349 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstromdbc05252011-09-09 01:59:59 -0700351 if (!field->GetName()->Equals(offsets[i].java_name)) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700352 LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
353 }
354 LG << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700355 << " i=" << i << " class=" << class_descriptor
Brian Carlstrom693267a2011-09-06 09:25:34 -0700356 << " Java=" << field->GetName()->ToModifiedUtf8()
357 << " CheckOffsets=" << offset.java_name;
358 }
359 }
360
Brian Carlstromdbc05252011-09-09 01:59:59 -0700361 for (size_t i = 0; i < offsets.size(); i++) {
362 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700363 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700364 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
365 error = true;
366 }
367 }
368 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700369 for (size_t i = 0; i < offsets.size(); i++) {
370 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700371 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700372 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
373 LG << "OFFSET MISMATCH NEXT LINE:";
374 }
Brian Carlstromdbc05252011-09-09 01:59:59 -0700375 LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700376 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
377 }
378 }
379
380 return !error;
381 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700382
383 private:
384 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700385};
386
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700387// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
388// is first since if it is failing, others are unlikely to succeed.
389
Elliott Hughes80609252011-09-23 17:24:51 -0700390struct ObjectOffsets : public CheckOffsets<Object> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700391 ObjectOffsets() : CheckOffsets<Object>(false, "Ljava/lang/Object;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700392 // alphabetical references
393 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_"));
394
395 // alphabetical 32-bit
396 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700397 };
398};
399
Elliott Hughes80609252011-09-23 17:24:51 -0700400struct AccessibleObjectOffsets : public CheckOffsets<AccessibleObject> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700401 AccessibleObjectOffsets()
402 : CheckOffsets<AccessibleObject>(false, "Ljava/lang/reflect/AccessibleObject;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700403 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AccessibleObject, java_flag_), "flag"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700404 };
405};
406
Elliott Hughes80609252011-09-23 17:24:51 -0700407struct FieldOffsets : public CheckOffsets<Field> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408 FieldOffsets() : CheckOffsets<Field>(false, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700409 // alphabetical references
410 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass"));
411 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_type_), "genericType"));
412 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, name_), "name"));
413 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_), "type"));
414
415 // alphabetical 32-bit
416 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_types_are_initialized_), "genericTypesAreInitialized"));
417 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "shadow$_access_flags_"));
418 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "shadow$_offset_"));
419 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_idx_), "shadow$_type_idx_"));
420 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, slot_), "slot"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700421 };
422};
423
Elliott Hughes80609252011-09-23 17:24:51 -0700424struct MethodOffsets : public CheckOffsets<Method> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 MethodOffsets() : CheckOffsets<Method>(false, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700426 // alphabetical references
427 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, declaring_class_), "declaringClass"));
428 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_exception_types_), "exceptionTypes"));
429 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_formal_type_parameters_), "formalTypeParameters"));
430 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_exception_types_), "genericExceptionTypes"));
431 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_parameter_types_), "genericParameterTypes"));
432 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_return_type_), "genericReturnType"));
433 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, name_), "name"));
434 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_parameter_types_), "parameterTypes"));
435 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_), "returnType"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700436 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_code_and_direct_methods_), "shadow$_dex_cache_code_and_direct_methods_"));
437 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_initialized_static_storage_), "shadow$_dex_cache_initialized_static_storage_"));
438 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_fields_), "shadow$_dex_cache_resolved_fields_"));
439 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_methods_), "shadow$_dex_cache_resolved_methods_"));
440 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_types_), "shadow$_dex_cache_resolved_types_"));
441 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_strings_), "shadow$_dex_cache_strings_"));
Ian Rogersd81871c2011-10-03 13:57:23 -0700442 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, gc_map_), "shadow$_gc_map_"));
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700443 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, shorty_), "shadow$_shorty_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700444 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, signature_), "shadow$_signature_"));
445
446 // alphabetical 32-bit
447 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_types_are_initialized_), "genericTypesAreInitialized"));
448 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "shadow$_access_flags_"));
449 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "shadow$_code_"));
450 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "shadow$_code_item_offset_"));
451 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "shadow$_core_spill_mask_"));
452 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "shadow$_fp_spill_mask_"));
453 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "shadow$_frame_size_in_bytes_"));
454 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "shadow$_invoke_stub_"));
455 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_idx_), "shadow$_java_return_type_idx_"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700456 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "shadow$_mapping_table_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700457 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "shadow$_method_index_"));
458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "shadow$_native_method_"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_ins_), "shadow$_num_ins_"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_outs_), "shadow$_num_outs_"));
461 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_registers_), "shadow$_num_registers_"));
462 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, proto_idx_), "shadow$_proto_idx_"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, vmap_table_), "shadow$_vmap_table_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700464 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_slot_), "slot"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700465 };
466};
467
Elliott Hughes80609252011-09-23 17:24:51 -0700468struct ConstructorOffsets : public MethodOffsets {
469 ConstructorOffsets() : MethodOffsets() {
470 // We use Method* for both java.lang.reflect.Constructor and java.lang.reflect.Method.
471 class_descriptor = "Ljava/lang/reflect/Constructor;";
472 }
473};
474
475struct ClassOffsets : public CheckOffsets<Class> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 ClassOffsets() : CheckOffsets<Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700477 // alphabetical references
478 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
479 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "shadow$_class_loader_"));
480 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "shadow$_component_type_"));
481 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, descriptor_), "shadow$_descriptor_"));
482 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "shadow$_dex_cache_"));
483 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "shadow$_direct_methods_"));
484 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "shadow$_ifields_"));
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700485 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "shadow$_iftable_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700486 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_), "shadow$_interfaces_"));
487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_type_idx_), "shadow$_interfaces_type_idx_"));
488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "shadow$_sfields_"));
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700489 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, source_file_), "shadow$_source_file_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700490 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "shadow$_super_class_"));
491 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "shadow$_verify_error_class_"));
492 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "shadow$_virtual_methods_"));
493 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "shadow$_vtable_"));
494
495 // alphabetical 32-bit
496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "shadow$_access_flags_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "shadow$_class_size_"));
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "shadow$_clinit_thread_id_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "shadow$_num_reference_instance_fields_"));
500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "shadow$_num_reference_static_fields_"));
501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "shadow$_object_size_"));
502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "shadow$_primitive_type_"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "shadow$_reference_instance_offsets_"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "shadow$_reference_static_offsets_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "shadow$_status_"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_type_idx_), "shadow$_super_class_type_idx_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700507 };
508};
509
Elliott Hughes80609252011-09-23 17:24:51 -0700510struct StringOffsets : public CheckOffsets<String> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700511 StringOffsets() : CheckOffsets<String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700512 // alphabetical references
513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
514
515 // alphabetical 32-bit
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 };
520};
521
Elliott Hughes80609252011-09-23 17:24:51 -0700522struct ThrowableOffsets : public CheckOffsets<Throwable> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 ThrowableOffsets() : CheckOffsets<Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 // alphabetical references
525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
526 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700530 };
531};
532
Elliott Hughes80609252011-09-23 17:24:51 -0700533struct StackTraceElementOffsets : public CheckOffsets<StackTraceElement> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700534 StackTraceElementOffsets() : CheckOffsets<StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700535 // alphabetical references
536 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass"));
537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName"));
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName"));
539 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540 };
541};
542
Elliott Hughes80609252011-09-23 17:24:51 -0700543struct ClassLoaderOffsets : public CheckOffsets<ClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544 ClassLoaderOffsets() : CheckOffsets<ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700545 // alphabetical references
546 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
547 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
Jesse Wilson1b5f4972011-10-11 21:41:08 -0400548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 };
550};
551
Elliott Hughes80609252011-09-23 17:24:51 -0700552struct BaseDexClassLoaderOffsets : public CheckOffsets<BaseDexClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553 BaseDexClassLoaderOffsets()
554 : CheckOffsets<BaseDexClassLoader>(false, "Ldalvik/system/BaseDexClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700555 // alphabetical references
556 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_path_), "originalPath"));
557 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, path_list_), "pathList"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558 };
559};
560
Elliott Hughes80609252011-09-23 17:24:51 -0700561struct PathClassLoaderOffsets : public CheckOffsets<PathClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700562 PathClassLoaderOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700563 : CheckOffsets<PathClassLoader>(false, "Ldalvik/system/PathClassLoader;") {}
Brian Carlstromdbc05252011-09-09 01:59:59 -0700564};
565
Jesse Wilson95caa792011-10-12 18:14:17 -0400566struct ProxyOffsets : public CheckOffsets<Proxy> {
567 ProxyOffsets() : CheckOffsets<Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400568 // alphabetical references
569 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Proxy, h_), "h"));
570 };
571};
572
Elliott Hughes80609252011-09-23 17:24:51 -0700573struct ClassClassOffsets : public CheckOffsets<ClassClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700574 ClassClassOffsets() : CheckOffsets<ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700575 // padding 32-bit
576 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
577 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
578
Brian Carlstromdbc05252011-09-09 01:59:59 -0700579 // alphabetical 64-bit
580 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
581 };
582};
583
Elliott Hughes80609252011-09-23 17:24:51 -0700584struct StringClassOffsets : public CheckOffsets<StringClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700585 StringClassOffsets() : CheckOffsets<StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700586 // alphabetical references
587 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
588 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
589
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700590 // padding 32-bit
591 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
592
Brian Carlstromdbc05252011-09-09 01:59:59 -0700593 // alphabetical 64-bit
594 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
595 };
596};
597
Elliott Hughes80609252011-09-23 17:24:51 -0700598struct FieldClassOffsets : public CheckOffsets<FieldClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599 FieldClassOffsets() : CheckOffsets<FieldClass>(true, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700600 // alphabetical references
601 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, ORDER_BY_NAME_AND_DECLARING_CLASS_), "ORDER_BY_NAME_AND_DECLARING_CLASS"));
602
603 // alphabetical 32-bit
604 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BOOLEAN_), "TYPE_BOOLEAN"));
605 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BYTE_), "TYPE_BYTE"));
606 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_CHAR_), "TYPE_CHAR"));
607 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_DOUBLE_), "TYPE_DOUBLE"));
608 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_FLOAT_), "TYPE_FLOAT"));
609 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_INTEGER_), "TYPE_INTEGER"));
610 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_LONG_), "TYPE_LONG"));
611 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_SHORT_), "TYPE_SHORT"));
612 };
613};
614
Elliott Hughes80609252011-09-23 17:24:51 -0700615struct MethodClassOffsets : public CheckOffsets<MethodClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700616 MethodClassOffsets() : CheckOffsets<MethodClass>(true, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700617 // alphabetical references
Brian Carlstromdbc05252011-09-09 01:59:59 -0700618 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700619 };
620};
621
Jesse Wilson95caa792011-10-12 18:14:17 -0400622struct ProxyClassOffsets : public CheckOffsets<ProxyClass> {
623 ProxyClassOffsets() : CheckOffsets<ProxyClass>(true, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400624 // alphabetical 32-bit
625 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ProxyClass, NextClassNameIndex_), "NextClassNameIndex"));
626
627 // alphabetical 64-bit
628 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ProxyClass, serialVersionUID_), "serialVersionUID"));
629 };
630};
631
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400632// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400633// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700634// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400635TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700636 EXPECT_TRUE(ObjectOffsets().Check());
637 EXPECT_TRUE(AccessibleObjectOffsets().Check());
Elliott Hughes80609252011-09-23 17:24:51 -0700638 EXPECT_TRUE(ConstructorOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700639 EXPECT_TRUE(FieldOffsets().Check());
640 EXPECT_TRUE(MethodOffsets().Check());
641 EXPECT_TRUE(ClassOffsets().Check());
642 EXPECT_TRUE(StringOffsets().Check());
643 EXPECT_TRUE(ThrowableOffsets().Check());
644 EXPECT_TRUE(StackTraceElementOffsets().Check());
645 EXPECT_TRUE(ClassLoaderOffsets().Check());
646 EXPECT_TRUE(BaseDexClassLoaderOffsets().Check());
647 EXPECT_TRUE(PathClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400648 EXPECT_TRUE(ProxyOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700649
650 EXPECT_TRUE(ClassClassOffsets().Check());
651 EXPECT_TRUE(StringClassOffsets().Check());
652 EXPECT_TRUE(FieldClassOffsets().Check());
653 EXPECT_TRUE(MethodClassOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400654 EXPECT_TRUE(ProxyClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700655}
656
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700657TEST_F(ClassLinkerTest, FindClassNonexistent) {
658 AssertNonExistentClass("NoSuchClass;");
659 AssertNonExistentClass("LNoSuchClass;");
660}
661
662TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700663 SirtRef<ClassLoader> class_loader(LoadDex("Nested"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700664
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700665 Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700666 ASSERT_TRUE(outer != NULL);
667 EXPECT_EQ(0U, outer->NumVirtualMethods());
668 EXPECT_EQ(1U, outer->NumDirectMethods());
669
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700670 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700671 ASSERT_TRUE(inner != NULL);
672 EXPECT_EQ(0U, inner->NumVirtualMethods());
673 EXPECT_EQ(1U, inner->NumDirectMethods());
674}
675
676TEST_F(ClassLinkerTest, FindClass_Primitives) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700677 const std::string expected("BCDFIJSZV");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700678 for (int ch = 0; ch < 255; ch++) {
679 char* s = reinterpret_cast<char*>(&ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700680 const std::string descriptor(s, 1);
681 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700682 AssertNonExistentClass(descriptor);
683 } else {
684 AssertPrimitiveClass(descriptor);
685 }
686 }
687}
688
689TEST_F(ClassLinkerTest, FindClass) {
690 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
691 ASSERT_TRUE(JavaLangObject != NULL);
692 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
693 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
694 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
695 ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;"));
696 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
697 EXPECT_FALSE(JavaLangObject->HasSuperClass());
698 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700699 EXPECT_EQ(Class::kStatusResolved, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700700 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700701 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700702 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700703 EXPECT_FALSE(JavaLangObject->IsVerified());
704 EXPECT_FALSE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700705 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
706 EXPECT_FALSE(JavaLangObject->IsArrayClass());
707 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
708 EXPECT_FALSE(JavaLangObject->IsInterface());
709 EXPECT_TRUE(JavaLangObject->IsPublic());
710 EXPECT_FALSE(JavaLangObject->IsFinal());
711 EXPECT_FALSE(JavaLangObject->IsPrimitive());
712 EXPECT_FALSE(JavaLangObject->IsSynthetic());
713 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
714 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
715 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
716 EXPECT_TRUE(JavaLangObject->GetInstanceField(0)->GetName()->Equals("shadow$_klass_"));
717 EXPECT_TRUE(JavaLangObject->GetInstanceField(1)->GetName()->Equals("shadow$_monitor_"));
718
719 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
720 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
721
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700722 SirtRef<ClassLoader> class_loader(LoadDex("MyClass"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700723 AssertNonExistentClass("LMyClass;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700724 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700725 ASSERT_TRUE(MyClass != NULL);
726 ASSERT_TRUE(MyClass->GetClass() != NULL);
727 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
728 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
729 ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;"));
730 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
731 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700732 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700733 EXPECT_EQ(Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700734 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700735 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700736 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700737 EXPECT_FALSE(MyClass->IsVerified());
738 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700739 EXPECT_FALSE(MyClass->IsArrayInstance());
740 EXPECT_FALSE(MyClass->IsArrayClass());
741 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
742 EXPECT_FALSE(MyClass->IsInterface());
743 EXPECT_FALSE(MyClass->IsPublic());
744 EXPECT_FALSE(MyClass->IsFinal());
745 EXPECT_FALSE(MyClass->IsPrimitive());
746 EXPECT_FALSE(MyClass->IsSynthetic());
747 EXPECT_EQ(1U, MyClass->NumDirectMethods());
748 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
749 EXPECT_EQ(0U, MyClass->NumInstanceFields());
750 EXPECT_EQ(0U, MyClass->NumStaticFields());
751 EXPECT_EQ(0U, MyClass->NumInterfaces());
752
753 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
754
755 // created by class_linker
756 AssertArrayClass("[C", "C", NULL);
757 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
758 // synthesized on the fly
759 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700760 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700761 // or not available at all
762 AssertNonExistentClass("[[[[LNonExistentClass;");
763}
764
765TEST_F(ClassLinkerTest, LibCore) {
766 AssertDexFile(java_lang_dex_file_.get(), NULL);
767}
768
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400769// The first reference array element must be a multiple of 8 bytes from the
770// start of the object
771TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
772 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
773 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
774 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775 uint32_t data_offset =
776 array_offset + ObjectArray<String>::DataOffset().Uint32Value();
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400777 EXPECT_EQ(16U, data_offset - array_offset);
778}
779
780TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700781 SirtRef<LongArray> array(LongArray::Alloc(0));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700782 EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700783 uint32_t array_offset = reinterpret_cast<uint32_t>(array.get());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400784 uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData());
785 EXPECT_EQ(16U, data_offset - array_offset);
786}
787
Elliott Hughes33203b52011-09-20 19:42:01 -0700788TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
789 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
790 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
791 Class* c;
792 c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
793 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
794 c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
795 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
796 c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
797 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
798 c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
799 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
800 c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
801 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
802 c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
803 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
804 c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
805 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
806 c = class_linker_->FindClass("Ljava/lang/Short;", NULL);
807 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
808}
809
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700810TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700811 SirtRef<ClassLoader> class_loader_1(LoadDex("MyClass"));
812 SirtRef<ClassLoader> class_loader_2(LoadDex("MyClass"));
813 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
814 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700815 EXPECT_TRUE(MyClass_1 != NULL);
816 EXPECT_TRUE(MyClass_2 != NULL);
817 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400818}
819
Jesse Wilson7833bd22011-08-09 18:31:44 -0400820TEST_F(ClassLinkerTest, StaticFields) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700821 SirtRef<ClassLoader> class_loader(LoadDex("Statics"));
822 Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700823 class_linker_->EnsureInitialized(statics, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400824
jeffhaoabcfde32011-09-29 15:05:18 -0700825 // Static final primitives that are initialized by a compile-time constant
826 // expression resolve to a copy of a constant value from the constant pool.
827 // So <clinit> should be null.
828 Method* clinit = statics->FindDirectMethod("<clinit>", "()V");
829 EXPECT_TRUE(clinit == NULL);
830
831 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400832
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700833 Field* s0 = statics->FindStaticField("s0", class_linker_->FindClass("Z", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700834 EXPECT_TRUE(s0->GetClass()->GetDescriptor()->Equals("Ljava/lang/reflect/Field;"));
835 EXPECT_TRUE(s0->GetType()->IsPrimitiveBoolean());
jeffhaoabcfde32011-09-29 15:05:18 -0700836 EXPECT_EQ(true, s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700837 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400838
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700839 Field* s1 = statics->FindStaticField("s1", class_linker_->FindClass("B", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700840 EXPECT_TRUE(s1->GetType()->IsPrimitiveByte());
jeffhaoabcfde32011-09-29 15:05:18 -0700841 EXPECT_EQ(5, s1->GetByte(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700842 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400843
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700844 Field* s2 = statics->FindStaticField("s2", class_linker_->FindClass("C", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700845 EXPECT_TRUE(s2->GetType()->IsPrimitiveChar());
jeffhaoabcfde32011-09-29 15:05:18 -0700846 EXPECT_EQ('a', s2->GetChar(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700847 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400848
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700849 Field* s3 = statics->FindStaticField("s3", class_linker_->FindClass("S", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700850 EXPECT_TRUE(s3->GetType()->IsPrimitiveShort());
Ian Rogers466bb252011-10-14 03:29:56 -0700851 EXPECT_EQ(-536, s3->GetShort(NULL));
852 s3->SetShort(NULL, -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400853
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700854 Field* s4 = statics->FindStaticField("s4", class_linker_->FindClass("I", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700855 EXPECT_TRUE(s4->GetType()->IsPrimitiveInt());
jeffhaoabcfde32011-09-29 15:05:18 -0700856 EXPECT_EQ(2000000000, s4->GetInt(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700857 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400858
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700859 Field* s5 = statics->FindStaticField("s5", class_linker_->FindClass("J", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 EXPECT_TRUE(s5->GetType()->IsPrimitiveLong());
jeffhaoabcfde32011-09-29 15:05:18 -0700861 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700862 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400863
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700864 Field* s6 = statics->FindStaticField("s6", class_linker_->FindClass("F", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700865 EXPECT_TRUE(s6->GetType()->IsPrimitiveFloat());
jeffhaoabcfde32011-09-29 15:05:18 -0700866 EXPECT_EQ(0.5, s6->GetFloat(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700867 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400868
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700869 Field* s7 = statics->FindStaticField("s7", class_linker_->FindClass("D", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700870 EXPECT_TRUE(s7->GetType()->IsPrimitiveDouble());
jeffhaoabcfde32011-09-29 15:05:18 -0700871 EXPECT_EQ(16777217, s7->GetDouble(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700872 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400873
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700874 Field* s8 = statics->FindStaticField("s8", class_linker_->FindClass("Ljava/lang/String;", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700875 EXPECT_FALSE(s8->GetType()->IsPrimitive());
jeffhaoabcfde32011-09-29 15:05:18 -0700876 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700877 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400878
Brian Carlstrom4873d462011-08-21 15:23:39 -0700879 EXPECT_EQ(false, s0->GetBoolean(NULL));
880 EXPECT_EQ(6, s1->GetByte(NULL));
881 EXPECT_EQ('b', s2->GetChar(NULL));
Ian Rogers466bb252011-10-14 03:29:56 -0700882 EXPECT_EQ(-535, s3->GetShort(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700883 EXPECT_EQ(2000000001, s4->GetInt(NULL));
884 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL));
885 EXPECT_EQ(0.75, s6->GetFloat(NULL));
886 EXPECT_EQ(16777219, s7->GetDouble(NULL));
887 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400888}
889
Brian Carlstrom30b94452011-08-25 21:35:26 -0700890TEST_F(ClassLinkerTest, Interfaces) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700891 SirtRef<ClassLoader> class_loader(LoadDex("Interfaces"));
892 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
893 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
894 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
895 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
896 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700897 EXPECT_TRUE(I->IsAssignableFrom(A));
898 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700899 EXPECT_TRUE(J->IsAssignableFrom(K));
900 EXPECT_TRUE(K->IsAssignableFrom(B));
901 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700902
903 Method* Ii = I->FindVirtualMethod("i", "()V");
904 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
905 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700906 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
907 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
908 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700909 Method* Ai = A->FindVirtualMethod("i", "()V");
910 Method* Aj1 = A->FindVirtualMethod("j1", "()V");
911 Method* Aj2 = A->FindVirtualMethod("j2", "()V");
912 ASSERT_TRUE(Ii != NULL);
913 ASSERT_TRUE(Jj1 != NULL);
914 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700915 ASSERT_TRUE(Kj1 != NULL);
916 ASSERT_TRUE(Kj2 != NULL);
917 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700918 ASSERT_TRUE(Ai != NULL);
919 ASSERT_TRUE(Aj1 != NULL);
920 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700921 EXPECT_NE(Ii, Ai);
922 EXPECT_NE(Jj1, Aj1);
923 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700924 EXPECT_EQ(Kj1, Jj1);
925 EXPECT_EQ(Kj2, Jj2);
Ian Rogersb04f69f2011-10-17 00:40:54 -0700926 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, true));
927 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, true));
928 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, true));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700929 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
930 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
931 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
932}
933
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700934TEST_F(ClassLinkerTest, InitializeStaticStorageFromCode) {
jeffhaoabcfde32011-09-29 15:05:18 -0700935 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700936
jeffhaoabcfde32011-09-29 15:05:18 -0700937 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
938 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700939
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700940 SirtRef<ClassLoader> class_loader(LoadDex("StaticsFromCode"));
941 const DexFile* dex_file = ClassLoader::GetCompileTimeClassPath(class_loader.get())[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700942 CHECK(dex_file != NULL);
943
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700944 Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
jeffhaoabcfde32011-09-29 15:05:18 -0700945 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
946 Method* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
947 uint32_t type_idx = FindTypeIdxByDescriptor(*dex_file, "LStaticsFromCode;");
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700948
949 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700950 StaticStorageBase* uninit = InitializeStaticStorage(type_idx, clinit, Thread::Current());
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700951 EXPECT_TRUE(uninit != NULL);
952 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700953 StaticStorageBase* init = InitializeStaticStorage(type_idx, getS0, Thread::Current());
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700954 EXPECT_TRUE(init != NULL);
955 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700956}
957
Elliott Hughes20cde902011-10-04 17:37:27 -0700958TEST_F(ClassLinkerTest, FinalizableBit) {
959 Class* c;
960
961 // Object has a finalize method, but we know it's empty.
962 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
963 EXPECT_FALSE(c->IsFinalizable());
964
965 // Enum has a finalize method to prevent its subclasses from implementing one.
966 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
967 EXPECT_FALSE(c->IsFinalizable());
968
969 // RoundingMode is an enum.
970 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
971 EXPECT_FALSE(c->IsFinalizable());
972
973 // RandomAccessFile extends Object and overrides finalize.
974 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
975 EXPECT_TRUE(c->IsFinalizable());
976
977 // FileInputStream is finalizable and extends InputStream which isn't.
978 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
979 EXPECT_FALSE(c->IsFinalizable());
980 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
981 EXPECT_TRUE(c->IsFinalizable());
982
983 // ScheduledThreadPoolExecutor doesn't have a finalize method but
984 // extends ThreadPoolExecutor which does.
985 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
986 EXPECT_TRUE(c->IsFinalizable());
987 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
988 EXPECT_TRUE(c->IsFinalizable());
989}
990
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400991} // namespace art