blob: d1be1de73404e57b3e3b308d31be72977eee5d86 [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_"));
jeffhaoe23d93c2011-09-15 14:48:43 -0700442 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_data_), "shadow$_register_map_data_"));
443 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_header_), "shadow$_register_map_header_"));
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700444 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, shorty_), "shadow$_shorty_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700445 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, signature_), "shadow$_signature_"));
446
447 // alphabetical 32-bit
448 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_types_are_initialized_), "genericTypesAreInitialized"));
449 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "shadow$_access_flags_"));
450 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "shadow$_code_"));
451 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "shadow$_code_item_offset_"));
452 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "shadow$_core_spill_mask_"));
453 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "shadow$_fp_spill_mask_"));
454 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "shadow$_frame_size_in_bytes_"));
455 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "shadow$_invoke_stub_"));
456 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_idx_), "shadow$_java_return_type_idx_"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700457 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "shadow$_mapping_table_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "shadow$_method_index_"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "shadow$_native_method_"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_ins_), "shadow$_num_ins_"));
461 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_outs_), "shadow$_num_outs_"));
462 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_registers_), "shadow$_num_registers_"));
463 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, proto_idx_), "shadow$_proto_idx_"));
464 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, return_pc_offset_in_bytes_), "shadow$_return_pc_offset_in_bytes_"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700465 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, vmap_table_), "shadow$_vmap_table_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700466 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_slot_), "slot"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700467 };
468};
469
Elliott Hughes80609252011-09-23 17:24:51 -0700470struct ConstructorOffsets : public MethodOffsets {
471 ConstructorOffsets() : MethodOffsets() {
472 // We use Method* for both java.lang.reflect.Constructor and java.lang.reflect.Method.
473 class_descriptor = "Ljava/lang/reflect/Constructor;";
474 }
475};
476
477struct ClassOffsets : public CheckOffsets<Class> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 ClassOffsets() : CheckOffsets<Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700479 // alphabetical references
480 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
481 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "shadow$_class_loader_"));
482 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "shadow$_component_type_"));
483 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, descriptor_), "shadow$_descriptor_"));
484 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "shadow$_dex_cache_"));
485 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "shadow$_direct_methods_"));
486 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "shadow$_ifields_"));
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "shadow$_iftable_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_), "shadow$_interfaces_"));
489 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, interfaces_type_idx_), "shadow$_interfaces_type_idx_"));
490 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "shadow$_sfields_"));
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700491 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, source_file_), "shadow$_source_file_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700492 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "shadow$_super_class_"));
493 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "shadow$_verify_error_class_"));
494 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "shadow$_virtual_methods_"));
495 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "shadow$_vtable_"));
496
497 // alphabetical 32-bit
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "shadow$_access_flags_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "shadow$_class_size_"));
500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "shadow$_clinit_thread_id_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "shadow$_num_reference_instance_fields_"));
502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "shadow$_num_reference_static_fields_"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "shadow$_object_size_"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "shadow$_primitive_type_"));
505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "shadow$_reference_instance_offsets_"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "shadow$_reference_static_offsets_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700507 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "shadow$_status_"));
508 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_type_idx_), "shadow$_super_class_type_idx_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700509 };
510};
511
Elliott Hughes80609252011-09-23 17:24:51 -0700512struct StringOffsets : public CheckOffsets<String> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513 StringOffsets() : CheckOffsets<String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700514 // alphabetical references
515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
516
517 // alphabetical 32-bit
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
519 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
520 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700521 };
522};
523
Elliott Hughes80609252011-09-23 17:24:51 -0700524struct ThrowableOffsets : public CheckOffsets<Throwable> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 ThrowableOffsets() : CheckOffsets<Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700526 // alphabetical references
527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
530 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
531 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700532 };
533};
534
Elliott Hughes80609252011-09-23 17:24:51 -0700535struct StackTraceElementOffsets : public CheckOffsets<StackTraceElement> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700536 StackTraceElementOffsets() : CheckOffsets<StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700537 // alphabetical references
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass"));
539 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName"));
540 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName"));
541 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700542 };
543};
544
Elliott Hughes80609252011-09-23 17:24:51 -0700545struct ClassLoaderOffsets : public CheckOffsets<ClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 ClassLoaderOffsets() : CheckOffsets<ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700547 // alphabetical references
548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
549 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
Jesse Wilson1b5f4972011-10-11 21:41:08 -0400550 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551 };
552};
553
Elliott Hughes80609252011-09-23 17:24:51 -0700554struct BaseDexClassLoaderOffsets : public CheckOffsets<BaseDexClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555 BaseDexClassLoaderOffsets()
556 : CheckOffsets<BaseDexClassLoader>(false, "Ldalvik/system/BaseDexClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700557 // alphabetical references
558 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_path_), "originalPath"));
559 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, path_list_), "pathList"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700560 };
561};
562
Elliott Hughes80609252011-09-23 17:24:51 -0700563struct PathClassLoaderOffsets : public CheckOffsets<PathClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 PathClassLoaderOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700565 : CheckOffsets<PathClassLoader>(false, "Ldalvik/system/PathClassLoader;") {}
Brian Carlstromdbc05252011-09-09 01:59:59 -0700566};
567
Jesse Wilson95caa792011-10-12 18:14:17 -0400568struct ProxyOffsets : public CheckOffsets<Proxy> {
569 ProxyOffsets() : CheckOffsets<Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400570 // alphabetical references
571 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Proxy, h_), "h"));
572 };
573};
574
Elliott Hughes80609252011-09-23 17:24:51 -0700575struct ClassClassOffsets : public CheckOffsets<ClassClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700576 ClassClassOffsets() : CheckOffsets<ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700577 // padding 32-bit
578 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
579 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
580
Brian Carlstromdbc05252011-09-09 01:59:59 -0700581 // alphabetical 64-bit
582 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
583 };
584};
585
Elliott Hughes80609252011-09-23 17:24:51 -0700586struct StringClassOffsets : public CheckOffsets<StringClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700587 StringClassOffsets() : CheckOffsets<StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700588 // alphabetical references
589 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
590 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
591
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700592 // padding 32-bit
593 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
594
Brian Carlstromdbc05252011-09-09 01:59:59 -0700595 // alphabetical 64-bit
596 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
597 };
598};
599
Elliott Hughes80609252011-09-23 17:24:51 -0700600struct FieldClassOffsets : public CheckOffsets<FieldClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 FieldClassOffsets() : CheckOffsets<FieldClass>(true, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700602 // alphabetical references
603 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, ORDER_BY_NAME_AND_DECLARING_CLASS_), "ORDER_BY_NAME_AND_DECLARING_CLASS"));
604
605 // alphabetical 32-bit
606 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BOOLEAN_), "TYPE_BOOLEAN"));
607 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BYTE_), "TYPE_BYTE"));
608 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_CHAR_), "TYPE_CHAR"));
609 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_DOUBLE_), "TYPE_DOUBLE"));
610 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_FLOAT_), "TYPE_FLOAT"));
611 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_INTEGER_), "TYPE_INTEGER"));
612 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_LONG_), "TYPE_LONG"));
613 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_SHORT_), "TYPE_SHORT"));
614 };
615};
616
Elliott Hughes80609252011-09-23 17:24:51 -0700617struct MethodClassOffsets : public CheckOffsets<MethodClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700618 MethodClassOffsets() : CheckOffsets<MethodClass>(true, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700619 // alphabetical references
Brian Carlstromdbc05252011-09-09 01:59:59 -0700620 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700621 };
622};
623
Jesse Wilson95caa792011-10-12 18:14:17 -0400624struct ProxyClassOffsets : public CheckOffsets<ProxyClass> {
625 ProxyClassOffsets() : CheckOffsets<ProxyClass>(true, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400626 // alphabetical 32-bit
627 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ProxyClass, NextClassNameIndex_), "NextClassNameIndex"));
628
629 // alphabetical 64-bit
630 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ProxyClass, serialVersionUID_), "serialVersionUID"));
631 };
632};
633
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400634// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400635// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700636// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400637TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700638 EXPECT_TRUE(ObjectOffsets().Check());
639 EXPECT_TRUE(AccessibleObjectOffsets().Check());
Elliott Hughes80609252011-09-23 17:24:51 -0700640 EXPECT_TRUE(ConstructorOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700641 EXPECT_TRUE(FieldOffsets().Check());
642 EXPECT_TRUE(MethodOffsets().Check());
643 EXPECT_TRUE(ClassOffsets().Check());
644 EXPECT_TRUE(StringOffsets().Check());
645 EXPECT_TRUE(ThrowableOffsets().Check());
646 EXPECT_TRUE(StackTraceElementOffsets().Check());
647 EXPECT_TRUE(ClassLoaderOffsets().Check());
648 EXPECT_TRUE(BaseDexClassLoaderOffsets().Check());
649 EXPECT_TRUE(PathClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400650 EXPECT_TRUE(ProxyOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700651
652 EXPECT_TRUE(ClassClassOffsets().Check());
653 EXPECT_TRUE(StringClassOffsets().Check());
654 EXPECT_TRUE(FieldClassOffsets().Check());
655 EXPECT_TRUE(MethodClassOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400656 EXPECT_TRUE(ProxyClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700657}
658
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700659TEST_F(ClassLinkerTest, FindClassNonexistent) {
660 AssertNonExistentClass("NoSuchClass;");
661 AssertNonExistentClass("LNoSuchClass;");
662}
663
664TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700665 SirtRef<ClassLoader> class_loader(LoadDex("Nested"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700666
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700667 Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700668 ASSERT_TRUE(outer != NULL);
669 EXPECT_EQ(0U, outer->NumVirtualMethods());
670 EXPECT_EQ(1U, outer->NumDirectMethods());
671
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700672 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700673 ASSERT_TRUE(inner != NULL);
674 EXPECT_EQ(0U, inner->NumVirtualMethods());
675 EXPECT_EQ(1U, inner->NumDirectMethods());
676}
677
678TEST_F(ClassLinkerTest, FindClass_Primitives) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 const std::string expected("BCDFIJSZV");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700680 for (int ch = 0; ch < 255; ch++) {
681 char* s = reinterpret_cast<char*>(&ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 const std::string descriptor(s, 1);
683 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700684 AssertNonExistentClass(descriptor);
685 } else {
686 AssertPrimitiveClass(descriptor);
687 }
688 }
689}
690
691TEST_F(ClassLinkerTest, FindClass) {
692 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
693 ASSERT_TRUE(JavaLangObject != NULL);
694 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
695 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
696 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
697 ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;"));
698 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
699 EXPECT_FALSE(JavaLangObject->HasSuperClass());
700 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700701 EXPECT_EQ(Class::kStatusResolved, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700702 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700703 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700704 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700705 EXPECT_FALSE(JavaLangObject->IsVerified());
706 EXPECT_FALSE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700707 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
708 EXPECT_FALSE(JavaLangObject->IsArrayClass());
709 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
710 EXPECT_FALSE(JavaLangObject->IsInterface());
711 EXPECT_TRUE(JavaLangObject->IsPublic());
712 EXPECT_FALSE(JavaLangObject->IsFinal());
713 EXPECT_FALSE(JavaLangObject->IsPrimitive());
714 EXPECT_FALSE(JavaLangObject->IsSynthetic());
715 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
716 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
717 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
718 EXPECT_TRUE(JavaLangObject->GetInstanceField(0)->GetName()->Equals("shadow$_klass_"));
719 EXPECT_TRUE(JavaLangObject->GetInstanceField(1)->GetName()->Equals("shadow$_monitor_"));
720
721 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
722 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
723
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700724 SirtRef<ClassLoader> class_loader(LoadDex("MyClass"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700725 AssertNonExistentClass("LMyClass;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700726 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700727 ASSERT_TRUE(MyClass != NULL);
728 ASSERT_TRUE(MyClass->GetClass() != NULL);
729 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
730 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
731 ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;"));
732 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
733 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700734 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700735 EXPECT_EQ(Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700736 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700737 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700738 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700739 EXPECT_FALSE(MyClass->IsVerified());
740 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700741 EXPECT_FALSE(MyClass->IsArrayInstance());
742 EXPECT_FALSE(MyClass->IsArrayClass());
743 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
744 EXPECT_FALSE(MyClass->IsInterface());
745 EXPECT_FALSE(MyClass->IsPublic());
746 EXPECT_FALSE(MyClass->IsFinal());
747 EXPECT_FALSE(MyClass->IsPrimitive());
748 EXPECT_FALSE(MyClass->IsSynthetic());
749 EXPECT_EQ(1U, MyClass->NumDirectMethods());
750 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
751 EXPECT_EQ(0U, MyClass->NumInstanceFields());
752 EXPECT_EQ(0U, MyClass->NumStaticFields());
753 EXPECT_EQ(0U, MyClass->NumInterfaces());
754
755 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
756
757 // created by class_linker
758 AssertArrayClass("[C", "C", NULL);
759 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
760 // synthesized on the fly
761 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700762 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700763 // or not available at all
764 AssertNonExistentClass("[[[[LNonExistentClass;");
765}
766
767TEST_F(ClassLinkerTest, LibCore) {
768 AssertDexFile(java_lang_dex_file_.get(), NULL);
769}
770
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400771// The first reference array element must be a multiple of 8 bytes from the
772// start of the object
773TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
774 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
775 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
776 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700777 uint32_t data_offset =
778 array_offset + ObjectArray<String>::DataOffset().Uint32Value();
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400779 EXPECT_EQ(16U, data_offset - array_offset);
780}
781
782TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700783 SirtRef<LongArray> array(LongArray::Alloc(0));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700784 EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700785 uint32_t array_offset = reinterpret_cast<uint32_t>(array.get());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400786 uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData());
787 EXPECT_EQ(16U, data_offset - array_offset);
788}
789
Elliott Hughes33203b52011-09-20 19:42:01 -0700790TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
791 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
792 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
793 Class* c;
794 c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
795 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
796 c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
797 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
798 c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
799 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
800 c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
801 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
802 c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
803 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
804 c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
805 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
806 c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
807 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
808 c = class_linker_->FindClass("Ljava/lang/Short;", NULL);
809 EXPECT_EQ("value", c->GetIFields()->Get(0)->GetName()->ToModifiedUtf8());
810}
811
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700812TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700813 SirtRef<ClassLoader> class_loader_1(LoadDex("MyClass"));
814 SirtRef<ClassLoader> class_loader_2(LoadDex("MyClass"));
815 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
816 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700817 EXPECT_TRUE(MyClass_1 != NULL);
818 EXPECT_TRUE(MyClass_2 != NULL);
819 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400820}
821
Jesse Wilson7833bd22011-08-09 18:31:44 -0400822TEST_F(ClassLinkerTest, StaticFields) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700823 SirtRef<ClassLoader> class_loader(LoadDex("Statics"));
824 Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700825 class_linker_->EnsureInitialized(statics, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400826
jeffhaoabcfde32011-09-29 15:05:18 -0700827 // Static final primitives that are initialized by a compile-time constant
828 // expression resolve to a copy of a constant value from the constant pool.
829 // So <clinit> should be null.
830 Method* clinit = statics->FindDirectMethod("<clinit>", "()V");
831 EXPECT_TRUE(clinit == NULL);
832
833 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400834
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700835 Field* s0 = statics->FindStaticField("s0", class_linker_->FindClass("Z", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700836 EXPECT_TRUE(s0->GetClass()->GetDescriptor()->Equals("Ljava/lang/reflect/Field;"));
837 EXPECT_TRUE(s0->GetType()->IsPrimitiveBoolean());
jeffhaoabcfde32011-09-29 15:05:18 -0700838 EXPECT_EQ(true, s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700839 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400840
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700841 Field* s1 = statics->FindStaticField("s1", class_linker_->FindClass("B", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700842 EXPECT_TRUE(s1->GetType()->IsPrimitiveByte());
jeffhaoabcfde32011-09-29 15:05:18 -0700843 EXPECT_EQ(5, s1->GetByte(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700844 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400845
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700846 Field* s2 = statics->FindStaticField("s2", class_linker_->FindClass("C", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847 EXPECT_TRUE(s2->GetType()->IsPrimitiveChar());
jeffhaoabcfde32011-09-29 15:05:18 -0700848 EXPECT_EQ('a', s2->GetChar(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700849 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400850
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700851 Field* s3 = statics->FindStaticField("s3", class_linker_->FindClass("S", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 EXPECT_TRUE(s3->GetType()->IsPrimitiveShort());
Ian Rogers466bb252011-10-14 03:29:56 -0700853 EXPECT_EQ(-536, s3->GetShort(NULL));
854 s3->SetShort(NULL, -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400855
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700856 Field* s4 = statics->FindStaticField("s4", class_linker_->FindClass("I", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700857 EXPECT_TRUE(s4->GetType()->IsPrimitiveInt());
jeffhaoabcfde32011-09-29 15:05:18 -0700858 EXPECT_EQ(2000000000, s4->GetInt(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700859 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400860
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700861 Field* s5 = statics->FindStaticField("s5", class_linker_->FindClass("J", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700862 EXPECT_TRUE(s5->GetType()->IsPrimitiveLong());
jeffhaoabcfde32011-09-29 15:05:18 -0700863 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700864 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400865
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700866 Field* s6 = statics->FindStaticField("s6", class_linker_->FindClass("F", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700867 EXPECT_TRUE(s6->GetType()->IsPrimitiveFloat());
jeffhaoabcfde32011-09-29 15:05:18 -0700868 EXPECT_EQ(0.5, s6->GetFloat(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700869 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400870
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700871 Field* s7 = statics->FindStaticField("s7", class_linker_->FindClass("D", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700872 EXPECT_TRUE(s7->GetType()->IsPrimitiveDouble());
jeffhaoabcfde32011-09-29 15:05:18 -0700873 EXPECT_EQ(16777217, s7->GetDouble(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700874 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400875
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700876 Field* s8 = statics->FindStaticField("s8", class_linker_->FindClass("Ljava/lang/String;", class_loader.get()));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700877 EXPECT_FALSE(s8->GetType()->IsPrimitive());
jeffhaoabcfde32011-09-29 15:05:18 -0700878 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700879 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400880
Brian Carlstrom4873d462011-08-21 15:23:39 -0700881 EXPECT_EQ(false, s0->GetBoolean(NULL));
882 EXPECT_EQ(6, s1->GetByte(NULL));
883 EXPECT_EQ('b', s2->GetChar(NULL));
Ian Rogers466bb252011-10-14 03:29:56 -0700884 EXPECT_EQ(-535, s3->GetShort(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700885 EXPECT_EQ(2000000001, s4->GetInt(NULL));
886 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL));
887 EXPECT_EQ(0.75, s6->GetFloat(NULL));
888 EXPECT_EQ(16777219, s7->GetDouble(NULL));
889 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400890}
891
Brian Carlstrom30b94452011-08-25 21:35:26 -0700892TEST_F(ClassLinkerTest, Interfaces) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700893 SirtRef<ClassLoader> class_loader(LoadDex("Interfaces"));
894 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
895 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
896 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
897 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
898 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700899 EXPECT_TRUE(I->IsAssignableFrom(A));
900 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700901 EXPECT_TRUE(J->IsAssignableFrom(K));
902 EXPECT_TRUE(K->IsAssignableFrom(B));
903 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700904
905 Method* Ii = I->FindVirtualMethod("i", "()V");
906 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
907 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700908 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
909 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
910 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700911 Method* Ai = A->FindVirtualMethod("i", "()V");
912 Method* Aj1 = A->FindVirtualMethod("j1", "()V");
913 Method* Aj2 = A->FindVirtualMethod("j2", "()V");
914 ASSERT_TRUE(Ii != NULL);
915 ASSERT_TRUE(Jj1 != NULL);
916 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700917 ASSERT_TRUE(Kj1 != NULL);
918 ASSERT_TRUE(Kj2 != NULL);
919 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700920 ASSERT_TRUE(Ai != NULL);
921 ASSERT_TRUE(Aj1 != NULL);
922 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700923 EXPECT_NE(Ii, Ai);
924 EXPECT_NE(Jj1, Aj1);
925 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700926 EXPECT_EQ(Kj1, Jj1);
927 EXPECT_EQ(Kj2, Jj2);
Ian Rogersb04f69f2011-10-17 00:40:54 -0700928 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, true));
929 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, true));
930 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, true));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700931 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
932 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
933 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
934}
935
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700936TEST_F(ClassLinkerTest, InitializeStaticStorageFromCode) {
jeffhaoabcfde32011-09-29 15:05:18 -0700937 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700938
jeffhaoabcfde32011-09-29 15:05:18 -0700939 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
940 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700941
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700942 SirtRef<ClassLoader> class_loader(LoadDex("StaticsFromCode"));
943 const DexFile* dex_file = ClassLoader::GetCompileTimeClassPath(class_loader.get())[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700944 CHECK(dex_file != NULL);
945
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700946 Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
jeffhaoabcfde32011-09-29 15:05:18 -0700947 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
948 Method* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
949 uint32_t type_idx = FindTypeIdxByDescriptor(*dex_file, "LStaticsFromCode;");
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700950
951 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700952 StaticStorageBase* uninit = InitializeStaticStorage(type_idx, clinit, Thread::Current());
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700953 EXPECT_TRUE(uninit != NULL);
954 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700955 StaticStorageBase* init = InitializeStaticStorage(type_idx, getS0, Thread::Current());
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700956 EXPECT_TRUE(init != NULL);
957 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700958}
959
Elliott Hughes20cde902011-10-04 17:37:27 -0700960TEST_F(ClassLinkerTest, FinalizableBit) {
961 Class* c;
962
963 // Object has a finalize method, but we know it's empty.
964 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
965 EXPECT_FALSE(c->IsFinalizable());
966
967 // Enum has a finalize method to prevent its subclasses from implementing one.
968 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
969 EXPECT_FALSE(c->IsFinalizable());
970
971 // RoundingMode is an enum.
972 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
973 EXPECT_FALSE(c->IsFinalizable());
974
975 // RandomAccessFile extends Object and overrides finalize.
976 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
977 EXPECT_TRUE(c->IsFinalizable());
978
979 // FileInputStream is finalizable and extends InputStream which isn't.
980 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
981 EXPECT_FALSE(c->IsFinalizable());
982 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
983 EXPECT_TRUE(c->IsFinalizable());
984
985 // ScheduledThreadPoolExecutor doesn't have a finalize method but
986 // extends ThreadPoolExecutor which does.
987 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
988 EXPECT_TRUE(c->IsFinalizable());
989 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
990 EXPECT_TRUE(c->IsFinalizable());
991}
992
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400993} // namespace art