blob: 5e605c4a9ece0e3c77fc75ec2443ba8a2141f871 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07004
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07005#include <string>
6
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
8#include "common_test.h"
Brian Carlstromc4fa2c02011-08-21 03:00:12 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_file.h"
11#include "heap.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012
13namespace art {
14
Brian Carlstromf734cf52011-08-17 16:28:14 -070015class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016 protected:
Elliott Hughesbd935992011-08-22 11:59:34 -070017 void AssertNonExistentClass(const StringPiece& descriptor) {
Carl Shapiro7a909592011-07-24 19:21:59 -070018 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070019 Thread* self = Thread::Current();
20 EXPECT_TRUE(self->IsExceptionPending());
21 Object* exception = self->GetException();
22 self->ClearException();
23 Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
24 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 }
26
27 void AssertPrimitiveClass(const StringPiece& descriptor) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070028 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor));
29 }
30
31 void AssertPrimitiveClass(const StringPiece& descriptor, const Class* primitive) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032 ASSERT_TRUE(primitive != NULL);
33 ASSERT_TRUE(primitive->GetClass() != NULL);
34 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
35 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070036 ASSERT_TRUE(primitive->GetDescriptor()->Equals(descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070037 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
38 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070039 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040 EXPECT_TRUE(primitive->GetStatus() == Class::kStatusInitialized);
41 EXPECT_FALSE(primitive->IsErroneous());
42 EXPECT_TRUE(primitive->IsVerified());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070043 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -070044 EXPECT_FALSE(primitive->IsArrayInstance());
45 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070046 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047 EXPECT_FALSE(primitive->IsInterface());
48 EXPECT_TRUE(primitive->IsPublic());
49 EXPECT_TRUE(primitive->IsFinal());
50 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070051 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 EXPECT_EQ(0U, primitive->NumDirectMethods());
53 EXPECT_EQ(0U, primitive->NumVirtualMethods());
54 EXPECT_EQ(0U, primitive->NumInstanceFields());
55 EXPECT_EQ(0U, primitive->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070056 EXPECT_EQ(0U, primitive->NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070057 EXPECT_TRUE(primitive->GetVTable() == NULL);
58 EXPECT_EQ(0, primitive->GetIfTableCount());
59 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070060 }
61
62 void AssertArrayClass(const StringPiece& array_descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070063 const StringPiece& component_type,
Brian Carlstrom8a487412011-08-29 20:08:52 -070064 const ClassLoader* class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070065 Class* array = class_linker_->FindClass(array_descriptor, class_loader);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070066 EXPECT_TRUE(array->GetComponentType()->GetDescriptor()->Equals(component_type));
67 EXPECT_EQ(class_loader, array->GetClassLoader());
68 AssertArrayClass(array_descriptor, array);
69 }
70
71 void AssertArrayClass(const StringPiece& array_descriptor, Class* array) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070072 ASSERT_TRUE(array != NULL);
73 ASSERT_TRUE(array->GetClass() != NULL);
74 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
75 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070076 ASSERT_TRUE(array->GetDescriptor()->Equals(array_descriptor));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070077 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070078 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070079 EXPECT_TRUE(array->HasSuperClass());
80 ASSERT_TRUE(array->GetComponentType() != NULL);
81 ASSERT_TRUE(array->GetComponentType()->GetDescriptor() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070082 EXPECT_TRUE(array->GetStatus() == Class::kStatusInitialized);
83 EXPECT_FALSE(array->IsErroneous());
84 EXPECT_TRUE(array->IsVerified());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070085 EXPECT_TRUE(array->IsResolved());
Brian Carlstromb63ec392011-08-27 17:38:27 -070086 EXPECT_FALSE(array->IsArrayInstance());
87 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 EXPECT_FALSE(array->IsInterface());
89 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
90 EXPECT_TRUE(array->IsFinal());
91 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070092 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 EXPECT_EQ(0U, array->NumDirectMethods());
94 EXPECT_EQ(0U, array->NumVirtualMethods());
95 EXPECT_EQ(0U, array->NumInstanceFields());
96 EXPECT_EQ(0U, array->NumStaticFields());
Brian Carlstrom913af1b2011-07-23 21:41:13 -070097 EXPECT_EQ(2U, array->NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070098 EXPECT_TRUE(array->GetVTable() != NULL);
99 EXPECT_EQ(2, array->GetIfTableCount());
100 ObjectArray<InterfaceEntry>* iftable = array->GetIfTable();
101 ASSERT_TRUE(iftable != NULL);
102 EXPECT_TRUE(iftable->Get(0)->GetInterface()->GetDescriptor()->Equals("Ljava/lang/Cloneable;"));
103 EXPECT_TRUE(iftable->Get(1)->GetInterface()->GetDescriptor()->Equals("Ljava/io/Serializable;"));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700105
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700106 void AssertMethod(Class* klass, Method* method) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700107 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700108 EXPECT_TRUE(method->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700109 EXPECT_TRUE(method->GetName() != NULL);
110 EXPECT_TRUE(method->GetSignature() != NULL);
111
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700112 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
113 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
114 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
115 EXPECT_TRUE(method->GetDexCacheResolvedFields() != NULL);
116 EXPECT_TRUE(method->GetDexCacheCodeAndDirectMethods() != NULL);
117 EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
118 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
119 method->GetDexCacheStrings());
120 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
121 method->GetDexCacheResolvedTypes());
122 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
123 method->GetDexCacheResolvedMethods());
124 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedFields(),
125 method->GetDexCacheResolvedFields());
126 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetCodeAndDirectMethods(),
127 method->GetDexCacheCodeAndDirectMethods());
128 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
129 method->GetDexCacheInitializedStaticStorage());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700130 }
131
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700132 void AssertField(Class* klass, Field* field) {
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700133 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700134 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700135 EXPECT_EQ(klass, field->GetDeclaringClass());
136 EXPECT_TRUE(field->GetName() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700137 EXPECT_TRUE(field->GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700138 }
139
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700140 void AssertClass(const StringPiece& descriptor, Class* klass) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700141 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 if (klass->GetDescriptor()->Equals(String::AllocFromModifiedUtf8("Ljava/lang/Object;"))) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700143 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700144 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700145 EXPECT_TRUE(klass->HasSuperClass());
146 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700147 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700148 EXPECT_TRUE(klass->GetClass() != NULL);
149 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700150 EXPECT_TRUE(klass->GetDexCache() != NULL);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700151 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700152 EXPECT_FALSE(klass->IsErroneous());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700153 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700154 EXPECT_TRUE(klass->IsLoaded());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700156 EXPECT_TRUE(klass->GetComponentType() == NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700157 EXPECT_TRUE(klass->IsInSamePackage(klass));
158 EXPECT_TRUE(Class::IsInSamePackage(klass->GetDescriptor(), klass->GetDescriptor()));
159 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700160 EXPECT_TRUE(klass->IsAbstract());
161 if (klass->NumDirectMethods() == 1) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700162 EXPECT_TRUE(klass->GetDirectMethod(0)->GetName()->Equals("<clinit>"));
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700163 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700164 } else {
165 EXPECT_EQ(0U, klass->NumDirectMethods());
166 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700167 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700168 if (!klass->IsSynthetic()) {
169 EXPECT_NE(0U, klass->NumDirectMethods());
170 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700171 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700172 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
173 for (int i = 0; i < klass->GetIfTableCount(); i++) {
174 const InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i);
175 ASSERT_TRUE(interface_entry != NULL);
176 Class* interface = interface_entry->GetInterface();
177 ASSERT_TRUE(interface != NULL);
178 EXPECT_TRUE(interface_entry->GetInterface() != NULL);
179 if (klass->IsInterface()) {
180 EXPECT_EQ(0U, interface_entry->GetMethodArrayCount());
181 } else {
182 CHECK_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
183 EXPECT_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
184 }
185 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700186 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700187 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700188 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700189 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700190 }
191 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700192 EXPECT_FALSE(klass->IsAbstract());
193 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700194 }
195 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700196 EXPECT_FALSE(klass->IsFinal());
197 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700198 }
199
200 EXPECT_FALSE(klass->IsPrimitive());
201 EXPECT_TRUE(klass->CanAccess(klass));
202
203 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700204 Method* method = klass->GetDirectMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700205 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700206 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400207 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700208 }
209
210 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700211 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700212 AssertMethod(klass, method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700213 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700214 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700215 }
216
217 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400218 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700219 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700220 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700221 }
222
223 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400224 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700225 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700226 EXPECT_TRUE(field->IsStatic());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700227 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700228
229 // Confirm that all instances fields are packed together at the start
230 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
231 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400232 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700234 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700236 }
237 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400238 Field* field = klass->GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 Class* field_type = field->GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700240 ASSERT_TRUE(field_type != NULL);
241 EXPECT_TRUE(field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700242 }
243
244 size_t total_num_reference_instance_fields = 0;
245 Class* k = klass;
246 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700247 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
248 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700249 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700250 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700251 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700252 }
253
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700254 void AssertDexFileClass(ClassLoader* class_loader, const StringPiece& descriptor) {
255 ASSERT_TRUE(descriptor != NULL);
256 Class* klass = class_linker_->FindSystemClass(descriptor);
257 ASSERT_TRUE(klass != NULL);
258 EXPECT_TRUE(klass->GetDescriptor()->Equals(descriptor));
259 EXPECT_EQ(class_loader, klass->GetClassLoader());
260 if (klass->IsPrimitive()) {
261 AssertPrimitiveClass(descriptor, klass);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700262 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700263 AssertArrayClass(descriptor, klass);
264 } else {
265 AssertClass(descriptor, klass);
266 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700267 }
268
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700269 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700270 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700271
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700272 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700273 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700274 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700275 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700277 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700278 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700279 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
280 const DexFile::TypeId& type_id = dex->GetTypeId(i);
281 const char* descriptor = dex->GetTypeDescriptor(type_id);
282 AssertDexFileClass(class_loader, descriptor);
283 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700284 class_linker_->VisitRoots(TestRootVisitor, NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700285 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700286
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700287 static void TestRootVisitor(const Object* root, void* arg) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700288 EXPECT_TRUE(root != NULL);
289 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700290};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700291
Brian Carlstrom693267a2011-09-06 09:25:34 -0700292struct CheckOffset {
293 size_t cpp_offset;
294 const char* java_name;
295 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
296};
297
298struct CheckOffsets {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700299 bool instance;
300 size_t size;
301 std::string class_descriptor;
302 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700303
304 bool Check() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700305 Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor);
306 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700307
308 bool error = false;
309
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700310 if (!klass->IsClassClass() && instance) {
311 size_t expected_size = instance ? klass->GetObjectSize() : klass->GetClassSize();
312 if (size != expected_size) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700313 LG << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700314 << " class=" << class_descriptor
Brian Carlstrom693267a2011-09-06 09:25:34 -0700315 << " Java=" << klass->GetObjectSize()
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700316 << " C++=" << expected_size;
317 error = true;
318 }
319 }
320
321 size_t num_fields = instance ? klass->NumInstanceFields() : klass->NumStaticFields();
322 if (offsets.size() != num_fields) {
323 LG << "Field count mismatch:"
324 << " class=" << class_descriptor
325 << " Java=" << num_fields
326 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700327 error = true;
328 }
329
Brian Carlstromdbc05252011-09-09 01:59:59 -0700330 for (size_t i = 0; i < offsets.size(); i++) {
331 Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i);
332 if (!field->GetName()->Equals(offsets[i].java_name)) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700333 error = true;
334 }
335 }
336 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700337 for (size_t i = 0; i < offsets.size(); i++) {
338 CheckOffset& offset = offsets[i];
339 Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i);
340 if (!field->GetName()->Equals(offsets[i].java_name)) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700341 LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
342 }
343 LG << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700344 << " i=" << i << " class=" << class_descriptor
Brian Carlstrom693267a2011-09-06 09:25:34 -0700345 << " Java=" << field->GetName()->ToModifiedUtf8()
346 << " CheckOffsets=" << offset.java_name;
347 }
348 }
349
Brian Carlstromdbc05252011-09-09 01:59:59 -0700350 for (size_t i = 0; i < offsets.size(); i++) {
351 CheckOffset& offset = offsets[i];
352 Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700353 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
354 error = true;
355 }
356 }
357 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700358 for (size_t i = 0; i < offsets.size(); i++) {
359 CheckOffset& offset = offsets[i];
360 Field* field = instance ? klass->GetInstanceField(i) : klass->GetStaticField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700361 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
362 LG << "OFFSET MISMATCH NEXT LINE:";
363 }
Brian Carlstromdbc05252011-09-09 01:59:59 -0700364 LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700365 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
366 }
367 }
368
369 return !error;
370 };
371};
372
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700373// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
374// is first since if it is failing, others are unlikely to succeed.
375
Brian Carlstrom693267a2011-09-06 09:25:34 -0700376struct ObjectOffsets : public CheckOffsets {
377 ObjectOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700378 instance = true;
379 size = sizeof(Object);
380 class_descriptor = "Ljava/lang/Object;";
381
382 // alphabetical references
383 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_"));
384
385 // alphabetical 32-bit
386 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700387 };
388};
389
390struct AccessibleObjectOffsets : public CheckOffsets {
391 AccessibleObjectOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700392 instance = true;
393 size = sizeof(AccessibleObject);
394 class_descriptor = "Ljava/lang/reflect/AccessibleObject;";
395 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AccessibleObject, java_flag_), "flag"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700396 };
397};
398
399struct FieldOffsets : public CheckOffsets {
400 FieldOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700401 instance = true;
402 size = sizeof(Field);
403 class_descriptor = "Ljava/lang/reflect/Field;";
404
405 // alphabetical references
406 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass"));
407 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_type_), "genericType"));
408 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, name_), "name"));
409 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_), "type"));
410
411 // alphabetical 32-bit
412 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, generic_types_are_initialized_), "genericTypesAreInitialized"));
413 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "shadow$_access_flags_"));
414 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "shadow$_offset_"));
415 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, type_idx_), "shadow$_type_idx_"));
416 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, slot_), "slot"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700417 };
418};
419
420struct MethodOffsets : public CheckOffsets {
421 MethodOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700422 instance = true;
423 size = sizeof(Method);
424 class_descriptor = "Ljava/lang/reflect/Method;";
425
426 // alphabetical references
427 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, declaring_class_), "declaringClass"));
428 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_exception_types_), "exceptionTypes"));
429 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_formal_type_parameters_), "formalTypeParameters"));
430 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_exception_types_), "genericExceptionTypes"));
431 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_parameter_types_), "genericParameterTypes"));
432 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_return_type_), "genericReturnType"));
433 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, name_), "name"));
434 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_parameter_types_), "parameterTypes"));
435 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_), "returnType"));
436 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_array_), "shadow$_code_array_"));
437 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_code_and_direct_methods_), "shadow$_dex_cache_code_and_direct_methods_"));
438 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_initialized_static_storage_), "shadow$_dex_cache_initialized_static_storage_"));
439 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_fields_), "shadow$_dex_cache_resolved_fields_"));
440 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_methods_), "shadow$_dex_cache_resolved_methods_"));
441 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_types_), "shadow$_dex_cache_resolved_types_"));
442 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_strings_), "shadow$_dex_cache_strings_"));
443 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_array_), "shadow$_invoke_stub_array_"));
444 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "shadow$_mapping_table_"));
jeffhaoe23d93c2011-09-15 14:48:43 -0700445 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_data_), "shadow$_register_map_data_"));
446 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, register_map_header_), "shadow$_register_map_header_"));
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700447 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, shorty_), "shadow$_shorty_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700448 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, signature_), "shadow$_signature_"));
449
450 // alphabetical 32-bit
451 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_generic_types_are_initialized_), "genericTypesAreInitialized"));
452 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "shadow$_access_flags_"));
453 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "shadow$_code_"));
454 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "shadow$_code_item_offset_"));
455 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "shadow$_core_spill_mask_"));
456 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "shadow$_fp_spill_mask_"));
457 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "shadow$_frame_size_in_bytes_"));
458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "shadow$_invoke_stub_"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_return_type_idx_), "shadow$_java_return_type_idx_"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "shadow$_method_index_"));
461 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "shadow$_native_method_"));
462 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_ins_), "shadow$_num_ins_"));
463 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_outs_), "shadow$_num_outs_"));
464 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, num_registers_), "shadow$_num_registers_"));
465 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, proto_idx_), "shadow$_proto_idx_"));
466 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, return_pc_offset_in_bytes_), "shadow$_return_pc_offset_in_bytes_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700467 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, java_slot_), "slot"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700468 };
469};
470
471struct ClassOffsets : public CheckOffsets {
472 ClassOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700473 instance = true;
474 size = sizeof(Class);
475 class_descriptor = "Ljava/lang/Class;";
476
477 // alphabetical references
478 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
479 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "shadow$_class_loader_"));
480 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "shadow$_component_type_"));
481 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, descriptor_), "shadow$_descriptor_"));
482 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "shadow$_dex_cache_"));
483 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "shadow$_direct_methods_"));
484 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "shadow$_ifields_"));
Brian 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
510struct StringOffsets : public CheckOffsets {
511 StringOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700512 instance = true;
513 size = sizeof(String);
514 class_descriptor = "Ljava/lang/String;";
515
516 // alphabetical references
517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
518
519 // alphabetical 32-bit
520 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
521 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
522 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700523 };
524};
525
526struct ThrowableOffsets : public CheckOffsets {
527 ThrowableOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700528 instance = true;
529 size = sizeof(Throwable);
530 class_descriptor = "Ljava/lang/Throwable;";
531
532 // alphabetical references
533 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
534 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
535 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
536 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538 };
539};
540
541struct StackTraceElementOffsets : public CheckOffsets {
542 StackTraceElementOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700543 instance = true;
544 size = sizeof(StackTraceElement);
545 class_descriptor = "Ljava/lang/StackTraceElement;";
546
547 // alphabetical references
548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass"));
549 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName"));
550 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName"));
551 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700552 };
553};
554
555struct ClassLoaderOffsets : public CheckOffsets {
556 ClassLoaderOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700557 instance = true;
558 size = sizeof(ClassLoader);
559 class_descriptor = "Ljava/lang/ClassLoader;";
560
561 // alphabetical references
562 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
563 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700564 };
565};
566
567struct BaseDexClassLoaderOffsets : public CheckOffsets {
568 BaseDexClassLoaderOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700569 instance = true;
570 size = sizeof(BaseDexClassLoader);
571 class_descriptor = "Ldalvik/system/BaseDexClassLoader;";
572
573 // alphabetical references
574 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_path_), "originalPath"));
575 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, path_list_), "pathList"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700576 };
577};
578
579struct PathClassLoaderOffsets : public CheckOffsets {
580 PathClassLoaderOffsets() {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700581 instance = true;
582 size = sizeof(PathClassLoader);
583 class_descriptor = "Ldalvik/system/PathClassLoader;";
584 };
585};
586
587struct ClassClassOffsets : public CheckOffsets {
588 ClassClassOffsets() {
589 instance = false;
590 size = sizeof(ClassClass);
591 class_descriptor = "Ljava/lang/Class;";
592
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700593 // padding 32-bit
594 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
595 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
596
Brian Carlstromdbc05252011-09-09 01:59:59 -0700597 // alphabetical 64-bit
598 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
599 };
600};
601
602struct StringClassOffsets : public CheckOffsets {
603 StringClassOffsets() {
604 instance = false;
605 size = sizeof(StringClass);
606 class_descriptor = "Ljava/lang/String;";
607
608 // alphabetical references
609 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
610 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
611
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700612 // padding 32-bit
613 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
614
Brian Carlstromdbc05252011-09-09 01:59:59 -0700615 // alphabetical 64-bit
616 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
617 };
618};
619
620struct FieldClassOffsets : public CheckOffsets {
621 FieldClassOffsets() {
622 instance = false;
623 size = sizeof(FieldClass);
624 class_descriptor = "Ljava/lang/reflect/Field;";
625
626 // alphabetical references
627 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, ORDER_BY_NAME_AND_DECLARING_CLASS_), "ORDER_BY_NAME_AND_DECLARING_CLASS"));
628
629 // alphabetical 32-bit
630 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BOOLEAN_), "TYPE_BOOLEAN"));
631 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_BYTE_), "TYPE_BYTE"));
632 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_CHAR_), "TYPE_CHAR"));
633 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_DOUBLE_), "TYPE_DOUBLE"));
634 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_FLOAT_), "TYPE_FLOAT"));
635 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_INTEGER_), "TYPE_INTEGER"));
636 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_LONG_), "TYPE_LONG"));
637 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, TYPE_SHORT_), "TYPE_SHORT"));
638 };
639};
640
641struct MethodClassOffsets : public CheckOffsets {
642 MethodClassOffsets() {
643 instance = false;
644 size = sizeof(MethodClass);
645 class_descriptor = "Ljava/lang/reflect/Method;";
646
647 // alphabetical references
648 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, NO_ANNOTATIONS_), "NO_ANNOTATIONS"));
649 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700650 };
651};
652
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400653// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400654// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700655// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400656TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700657 EXPECT_TRUE(ObjectOffsets().Check());
658 EXPECT_TRUE(AccessibleObjectOffsets().Check());
659 EXPECT_TRUE(FieldOffsets().Check());
660 EXPECT_TRUE(MethodOffsets().Check());
661 EXPECT_TRUE(ClassOffsets().Check());
662 EXPECT_TRUE(StringOffsets().Check());
663 EXPECT_TRUE(ThrowableOffsets().Check());
664 EXPECT_TRUE(StackTraceElementOffsets().Check());
665 EXPECT_TRUE(ClassLoaderOffsets().Check());
666 EXPECT_TRUE(BaseDexClassLoaderOffsets().Check());
667 EXPECT_TRUE(PathClassLoaderOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700668
669 EXPECT_TRUE(ClassClassOffsets().Check());
670 EXPECT_TRUE(StringClassOffsets().Check());
671 EXPECT_TRUE(FieldClassOffsets().Check());
672 EXPECT_TRUE(MethodClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700673}
674
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700675TEST_F(ClassLinkerTest, FindClassNonexistent) {
676 AssertNonExistentClass("NoSuchClass;");
677 AssertNonExistentClass("LNoSuchClass;");
678}
679
680TEST_F(ClassLinkerTest, FindClassNested) {
681 const ClassLoader* class_loader = LoadDex("Nested");
682
683 Class* outer = class_linker_->FindClass("LNested;", class_loader);
684 ASSERT_TRUE(outer != NULL);
685 EXPECT_EQ(0U, outer->NumVirtualMethods());
686 EXPECT_EQ(1U, outer->NumDirectMethods());
687
688 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader);
689 ASSERT_TRUE(inner != NULL);
690 EXPECT_EQ(0U, inner->NumVirtualMethods());
691 EXPECT_EQ(1U, inner->NumDirectMethods());
692}
693
694TEST_F(ClassLinkerTest, FindClass_Primitives) {
695 StringPiece expected = "BCDFIJSZV";
696 for (int ch = 0; ch < 255; ch++) {
697 char* s = reinterpret_cast<char*>(&ch);
698 StringPiece descriptor(s, 1);
699 if (expected.find(ch) == StringPiece::npos) {
700 AssertNonExistentClass(descriptor);
701 } else {
702 AssertPrimitiveClass(descriptor);
703 }
704 }
705}
706
707TEST_F(ClassLinkerTest, FindClass) {
708 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
709 ASSERT_TRUE(JavaLangObject != NULL);
710 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
711 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
712 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
713 ASSERT_TRUE(JavaLangObject->GetDescriptor()->Equals("Ljava/lang/Object;"));
714 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
715 EXPECT_FALSE(JavaLangObject->HasSuperClass());
716 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
717 EXPECT_FALSE(JavaLangObject->IsErroneous());
718 EXPECT_TRUE(JavaLangObject->IsVerified());
719 EXPECT_TRUE(JavaLangObject->IsResolved());
720 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
721 EXPECT_FALSE(JavaLangObject->IsArrayClass());
722 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
723 EXPECT_FALSE(JavaLangObject->IsInterface());
724 EXPECT_TRUE(JavaLangObject->IsPublic());
725 EXPECT_FALSE(JavaLangObject->IsFinal());
726 EXPECT_FALSE(JavaLangObject->IsPrimitive());
727 EXPECT_FALSE(JavaLangObject->IsSynthetic());
728 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
729 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
730 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
731 EXPECT_TRUE(JavaLangObject->GetInstanceField(0)->GetName()->Equals("shadow$_klass_"));
732 EXPECT_TRUE(JavaLangObject->GetInstanceField(1)->GetName()->Equals("shadow$_monitor_"));
733
734 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
735 EXPECT_EQ(0U, JavaLangObject->NumInterfaces());
736
737 const ClassLoader* class_loader = LoadDex("MyClass");
738 AssertNonExistentClass("LMyClass;");
739 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader);
740 ASSERT_TRUE(MyClass != NULL);
741 ASSERT_TRUE(MyClass->GetClass() != NULL);
742 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
743 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
744 ASSERT_TRUE(MyClass->GetDescriptor()->Equals("LMyClass;"));
745 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
746 EXPECT_TRUE(MyClass->HasSuperClass());
747 EXPECT_EQ(class_loader, MyClass->GetClassLoader());
748 EXPECT_TRUE(MyClass->GetStatus() == Class::kStatusResolved);
749 EXPECT_FALSE(MyClass->IsErroneous());
750 EXPECT_FALSE(MyClass->IsVerified());
751 EXPECT_TRUE(MyClass->IsResolved());
752 EXPECT_FALSE(MyClass->IsArrayInstance());
753 EXPECT_FALSE(MyClass->IsArrayClass());
754 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
755 EXPECT_FALSE(MyClass->IsInterface());
756 EXPECT_FALSE(MyClass->IsPublic());
757 EXPECT_FALSE(MyClass->IsFinal());
758 EXPECT_FALSE(MyClass->IsPrimitive());
759 EXPECT_FALSE(MyClass->IsSynthetic());
760 EXPECT_EQ(1U, MyClass->NumDirectMethods());
761 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
762 EXPECT_EQ(0U, MyClass->NumInstanceFields());
763 EXPECT_EQ(0U, MyClass->NumStaticFields());
764 EXPECT_EQ(0U, MyClass->NumInterfaces());
765
766 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
767
768 // created by class_linker
769 AssertArrayClass("[C", "C", NULL);
770 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
771 // synthesized on the fly
772 AssertArrayClass("[[C", "[C", NULL);
773 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader);
774 // or not available at all
775 AssertNonExistentClass("[[[[LNonExistentClass;");
776}
777
778TEST_F(ClassLinkerTest, LibCore) {
779 AssertDexFile(java_lang_dex_file_.get(), NULL);
780}
781
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400782// The first reference array element must be a multiple of 8 bytes from the
783// start of the object
784TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
785 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
786 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
787 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700788 uint32_t data_offset =
789 array_offset + ObjectArray<String>::DataOffset().Uint32Value();
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400790 EXPECT_EQ(16U, data_offset - array_offset);
791}
792
793TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700794 LongArray* array = LongArray::Alloc(0);
795 EXPECT_EQ(class_linker_->FindSystemClass("[J"), array->GetClass());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400796 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
797 uint32_t data_offset = reinterpret_cast<uint32_t>(array->GetData());
798 EXPECT_EQ(16U, data_offset - array_offset);
799}
800
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700801TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700802 const ClassLoader* class_loader_1 = LoadDex("MyClass");
803 const ClassLoader* class_loader_2 = LoadDex("MyClass");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700804 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1);
805 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2);
806 EXPECT_TRUE(MyClass_1 != NULL);
807 EXPECT_TRUE(MyClass_2 != NULL);
808 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400809}
810
Jesse Wilson7833bd22011-08-09 18:31:44 -0400811TEST_F(ClassLinkerTest, StaticFields) {
812 // TODO: uncomment expectations of initial values when InitializeClass works
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700813 const ClassLoader* class_loader = LoadDex("Statics");
Jesse Wilson7833bd22011-08-09 18:31:44 -0400814 Class* statics = class_linker_->FindClass("LStatics;", class_loader);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700815 class_linker_->EnsureInitialized(statics);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400816
817 EXPECT_EQ(10U, statics->NumStaticFields());
818
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700819 Field* s0 = statics->FindStaticField("s0", class_linker_->FindClass("Z", class_loader));
820 EXPECT_TRUE(s0->GetClass()->GetDescriptor()->Equals("Ljava/lang/reflect/Field;"));
821 EXPECT_TRUE(s0->GetType()->IsPrimitiveBoolean());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700822 // EXPECT_EQ(true, s0->GetBoolean(NULL)); // TODO: needs clinit to be run?
823 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400824
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700825 Field* s1 = statics->FindStaticField("s1", class_linker_->FindClass("B", class_loader));
826 EXPECT_TRUE(s1->GetType()->IsPrimitiveByte());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700827 // EXPECT_EQ(5, s1->GetByte(NULL)); // TODO: needs clinit to be run?
828 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400829
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700830 Field* s2 = statics->FindStaticField("s2", class_linker_->FindClass("C", class_loader));
831 EXPECT_TRUE(s2->GetType()->IsPrimitiveChar());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700832 // EXPECT_EQ('a', s2->GetChar(NULL)); // TODO: needs clinit to be run?
833 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400834
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700835 Field* s3 = statics->FindStaticField("s3", class_linker_->FindClass("S", class_loader));
836 EXPECT_TRUE(s3->GetType()->IsPrimitiveShort());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700837 // EXPECT_EQ(65000, s3->GetShort(NULL)); // TODO: needs clinit to be run?
838 s3->SetShort(NULL, 65001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400839
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700840 Field* s4 = statics->FindStaticField("s4", class_linker_->FindClass("I", class_loader));
841 EXPECT_TRUE(s4->GetType()->IsPrimitiveInt());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700842 // EXPECT_EQ(2000000000, s4->GetInt(NULL)); // TODO: needs clinit to be run?
843 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400844
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700845 Field* s5 = statics->FindStaticField("s5", class_linker_->FindClass("J", class_loader));
846 EXPECT_TRUE(s5->GetType()->IsPrimitiveLong());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700847 // EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL)); // TODO: needs clinit to be run?
848 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400849
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700850 Field* s6 = statics->FindStaticField("s6", class_linker_->FindClass("F", class_loader));
851 EXPECT_TRUE(s6->GetType()->IsPrimitiveFloat());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700852 // EXPECT_EQ(0.5, s6->GetFloat(NULL)); // TODO: needs clinit to be run?
853 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400854
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700855 Field* s7 = statics->FindStaticField("s7", class_linker_->FindClass("D", class_loader));
856 EXPECT_TRUE(s7->GetType()->IsPrimitiveDouble());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700857 // EXPECT_EQ(16777217, s7->GetDouble(NULL)); // TODO: needs clinit to be run?
858 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400859
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 Field* s8 = statics->FindStaticField("s8", class_linker_->FindClass("Ljava/lang/Object;", class_loader));
861 EXPECT_FALSE(s8->GetType()->IsPrimitive());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700862 // EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android")); // TODO: needs clinit to be run?
863 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400864
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700865 Field* s9 = statics->FindStaticField("s9", class_linker_->FindClass("[Ljava/lang/Object;", class_loader));
866 EXPECT_TRUE(s9->GetType()->IsArrayClass());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700867 // EXPECT_EQ(NULL, s9->GetObject(NULL)); // TODO: needs clinit to be run?
868 s9->SetObject(NULL, NULL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400869
Brian Carlstrom4873d462011-08-21 15:23:39 -0700870 EXPECT_EQ(false, s0->GetBoolean(NULL));
871 EXPECT_EQ(6, s1->GetByte(NULL));
872 EXPECT_EQ('b', s2->GetChar(NULL));
873 EXPECT_EQ(65001, s3->GetShort(NULL));
874 EXPECT_EQ(2000000001, s4->GetInt(NULL));
875 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL));
876 EXPECT_EQ(0.75, s6->GetFloat(NULL));
877 EXPECT_EQ(16777219, s7->GetDouble(NULL));
878 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400879}
880
Brian Carlstrom30b94452011-08-25 21:35:26 -0700881TEST_F(ClassLinkerTest, Interfaces) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700882 const ClassLoader* class_loader = LoadDex("Interfaces");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700883 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader);
884 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader);
jeffhao5dbddee2011-09-07 16:38:26 -0700885 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700886 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader);
jeffhao5dbddee2011-09-07 16:38:26 -0700887 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700888 EXPECT_TRUE(I->IsAssignableFrom(A));
889 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700890 EXPECT_TRUE(J->IsAssignableFrom(K));
891 EXPECT_TRUE(K->IsAssignableFrom(B));
892 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700893
894 Method* Ii = I->FindVirtualMethod("i", "()V");
895 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
896 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700897 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
898 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
899 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700900 Method* Ai = A->FindVirtualMethod("i", "()V");
901 Method* Aj1 = A->FindVirtualMethod("j1", "()V");
902 Method* Aj2 = A->FindVirtualMethod("j2", "()V");
903 ASSERT_TRUE(Ii != NULL);
904 ASSERT_TRUE(Jj1 != NULL);
905 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700906 ASSERT_TRUE(Kj1 != NULL);
907 ASSERT_TRUE(Kj2 != NULL);
908 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700909 ASSERT_TRUE(Ai != NULL);
910 ASSERT_TRUE(Aj1 != NULL);
911 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700912 EXPECT_NE(Ii, Ai);
913 EXPECT_NE(Jj1, Aj1);
914 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700915 EXPECT_EQ(Kj1, Jj1);
916 EXPECT_EQ(Kj2, Jj2);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700917 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
918 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
919 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
920 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
921 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
922 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
923}
924
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700925TEST_F(ClassLinkerTest, InitializeStaticStorageFromCode) {
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700926 // pretend we are trying to get the static storage for the Statics class.
927
928 // case 1, get the uninitialized storage from Statics.<clinit>
929 // case 2, get the initialized storage from Statics.getS8
930
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700931 const ClassLoader* class_loader = LoadDex("Statics");
932 const DexFile* dex_file = ClassLoader::GetClassPath(class_loader)[0];
933 CHECK(dex_file != NULL);
934
935 Class* Statics = class_linker_->FindClass("LStatics;", class_loader);
936 Method* clinit = Statics->FindDirectMethod("<clinit>", "()V");
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700937 Method* getS8 = Statics->FindDirectMethod("getS8", "()Ljava/lang/Object;");
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700938 uint32_t type_idx = FindTypeIdxByDescriptor(*dex_file, "LStatics;");
939
940 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700941 StaticStorageBase* uninit = class_linker_->InitializeStaticStorageFromCode(type_idx, clinit);
942 EXPECT_TRUE(uninit != NULL);
943 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
944 StaticStorageBase* init = class_linker_->InitializeStaticStorageFromCode(type_idx, getS8);
945 EXPECT_TRUE(init != NULL);
946 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700947}
948
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400949} // namespace art