blob: 07a981637dfa6d414b7f13d056bd457fed057cc1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "class_linker.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070018
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070019#include <string>
20
Elliott Hughes90a33692011-08-30 13:27:07 -070021#include "UniquePtr.h"
22#include "common_test.h"
Brian Carlstromc4fa2c02011-08-21 03:00:12 -070023#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "dex_file.h"
25#include "heap.h"
Ian Rogersce9eca62011-10-07 17:11:03 -070026#include "runtime_support.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027
28namespace art {
29
Brian Carlstromf734cf52011-08-17 16:28:14 -070030class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031 protected:
Brian Carlstromaded5f72011-10-07 17:15:04 -070032 void AssertNonExistentClass(const std::string& descriptor) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080033 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor.c_str()) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070034 Thread* self = Thread::Current();
35 EXPECT_TRUE(self->IsExceptionPending());
36 Object* exception = self->GetException();
37 self->ClearException();
38 Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
39 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040 }
41
Brian Carlstromaded5f72011-10-07 17:15:04 -070042 void AssertPrimitiveClass(const std::string& descriptor) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080043 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070044 }
45
Brian Carlstromaded5f72011-10-07 17:15:04 -070046 void AssertPrimitiveClass(const std::string& descriptor, const Class* primitive) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080047 ClassHelper primitive_ch(primitive);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070048 ASSERT_TRUE(primitive != NULL);
49 ASSERT_TRUE(primitive->GetClass() != NULL);
50 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
51 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -080052 ASSERT_STREQ(descriptor.c_str(), primitive_ch.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070053 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
54 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -070056 EXPECT_EQ(Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070058 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070059 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070060 EXPECT_TRUE(primitive->IsVerified());
61 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070062 EXPECT_FALSE(primitive->IsArrayInstance());
63 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070064 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070065 EXPECT_FALSE(primitive->IsInterface());
66 EXPECT_TRUE(primitive->IsPublic());
67 EXPECT_TRUE(primitive->IsFinal());
68 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070069 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 EXPECT_EQ(0U, primitive->NumDirectMethods());
71 EXPECT_EQ(0U, primitive->NumVirtualMethods());
72 EXPECT_EQ(0U, primitive->NumInstanceFields());
73 EXPECT_EQ(0U, primitive->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080074 EXPECT_EQ(0U, primitive_ch.NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070075 EXPECT_TRUE(primitive->GetVTable() == NULL);
76 EXPECT_EQ(0, primitive->GetIfTableCount());
77 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 }
79
Brian Carlstromaded5f72011-10-07 17:15:04 -070080 void AssertArrayClass(const std::string& array_descriptor,
81 const std::string& component_type,
Brian Carlstrom8a487412011-08-29 20:08:52 -070082 const ClassLoader* class_loader) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080083 Class* array = class_linker_->FindClass(array_descriptor.c_str(), class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080084 ClassHelper array_component_ch(array->GetComponentType());
Elliott Hughes91250e02011-12-13 22:30:35 -080085 EXPECT_STREQ(component_type.c_str(), array_component_ch.GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070086 EXPECT_EQ(class_loader, array->GetClassLoader());
87 AssertArrayClass(array_descriptor, array);
88 }
89
Brian Carlstromaded5f72011-10-07 17:15:04 -070090 void AssertArrayClass(const std::string& array_descriptor, Class* array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080091 ClassHelper kh(array);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 ASSERT_TRUE(array != NULL);
93 ASSERT_TRUE(array->GetClass() != NULL);
94 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
95 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -080096 ASSERT_STREQ(array_descriptor.c_str(), kh.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070097 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -070098 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070099 EXPECT_TRUE(array->HasSuperClass());
100 ASSERT_TRUE(array->GetComponentType() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101 kh.ChangeClass(array->GetComponentType());
102 ASSERT_TRUE(kh.GetDescriptor() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700103 EXPECT_EQ(Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700105 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700106 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700107 EXPECT_TRUE(array->IsVerified());
108 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700109 EXPECT_FALSE(array->IsArrayInstance());
110 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700111 EXPECT_FALSE(array->IsInterface());
112 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
113 EXPECT_TRUE(array->IsFinal());
114 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700115 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700116 EXPECT_EQ(0U, array->NumDirectMethods());
117 EXPECT_EQ(0U, array->NumVirtualMethods());
118 EXPECT_EQ(0U, array->NumInstanceFields());
119 EXPECT_EQ(0U, array->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 kh.ChangeClass(array);
121 EXPECT_EQ(2U, kh.NumInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -0700122 EXPECT_TRUE(array->GetVTable() != NULL);
123 EXPECT_EQ(2, array->GetIfTableCount());
124 ObjectArray<InterfaceEntry>* iftable = array->GetIfTable();
125 ASSERT_TRUE(iftable != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 kh.ChangeClass(kh.GetInterface(0));
Elliott Hughes91250e02011-12-13 22:30:35 -0800127 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 kh.ChangeClass(array);
129 kh.ChangeClass(kh.GetInterface(1));
Elliott Hughes91250e02011-12-13 22:30:35 -0800130 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/io/Serializable;");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700131 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700132
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700133 void AssertMethod(Method* method) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 MethodHelper mh(method);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700135 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700136 EXPECT_TRUE(method->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 EXPECT_TRUE(mh.GetName() != NULL);
138 EXPECT_TRUE(mh.GetSignature() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700139
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700140 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800141 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700143 EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
144 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
145 method->GetDexCacheStrings());
Ian Rogers19846512012-02-24 11:42:47 -0800146 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
147 method->GetDexCacheResolvedMethods());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
149 method->GetDexCacheResolvedTypes());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
151 method->GetDexCacheInitializedStaticStorage());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700152 }
153
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700154 void AssertField(Class* klass, Field* field) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800155 FieldHelper fh(field);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700156 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700157 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700158 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 EXPECT_TRUE(fh.GetName() != NULL);
160 EXPECT_TRUE(fh.GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700161 }
162
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 void AssertClass(const std::string& descriptor, Class* klass) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800164 ClassHelper kh(klass);
Elliott Hughes91250e02011-12-13 22:30:35 -0800165 EXPECT_STREQ(descriptor.c_str(), kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700167 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700168 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700169 EXPECT_TRUE(klass->HasSuperClass());
170 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700171 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700172 EXPECT_TRUE(klass->GetClass() != NULL);
173 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700174 EXPECT_TRUE(klass->GetDexCache() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700175 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700176 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700177 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700179 EXPECT_TRUE(klass->GetComponentType() == NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700180 EXPECT_TRUE(klass->IsInSamePackage(klass));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 EXPECT_TRUE(Class::IsInSamePackage(kh.GetDescriptor(), kh.GetDescriptor()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700182 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700183 EXPECT_TRUE(klass->IsAbstract());
184 if (klass->NumDirectMethods() == 1) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 MethodHelper mh(klass->GetDirectMethod(0));
186 EXPECT_TRUE(mh.IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700187 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700188 } else {
189 EXPECT_EQ(0U, klass->NumDirectMethods());
190 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700191 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700192 if (!klass->IsSynthetic()) {
193 EXPECT_NE(0U, klass->NumDirectMethods());
194 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700195 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700196 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
197 for (int i = 0; i < klass->GetIfTableCount(); i++) {
198 const InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i);
199 ASSERT_TRUE(interface_entry != NULL);
200 Class* interface = interface_entry->GetInterface();
201 ASSERT_TRUE(interface != NULL);
202 EXPECT_TRUE(interface_entry->GetInterface() != NULL);
203 if (klass->IsInterface()) {
204 EXPECT_EQ(0U, interface_entry->GetMethodArrayCount());
205 } else {
206 CHECK_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
207 EXPECT_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
208 }
209 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700210 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700211 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700212 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700213 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700214 }
215 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 EXPECT_FALSE(klass->IsAbstract());
217 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700218 }
219 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700220 EXPECT_FALSE(klass->IsFinal());
221 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700222 }
223
224 EXPECT_FALSE(klass->IsPrimitive());
225 EXPECT_TRUE(klass->CanAccess(klass));
226
227 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700228 Method* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700229 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700230 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400231 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700232 }
233
234 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 Method* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700236 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700237 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700238 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700239 }
240
241 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400242 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700243 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700244 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700245 }
246
247 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400248 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700249 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700250 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700251 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700252
253 // Confirm that all instances fields are packed together at the start
254 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 FieldHelper fh;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700256 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400257 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 fh.ChangeField(field);
259 ASSERT_TRUE(!fh.IsPrimitiveType());
260 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700261 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700263 }
264 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400265 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 fh.ChangeField(field);
267 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 ASSERT_TRUE(field_type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 if (!fh.IsPrimitiveType() || !field_type->IsPrimitive()) {
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700270 // While Reference.referent is not primitive, the ClassLinker
271 // treats it as such so that the garbage collector won't scan it.
272 EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
273 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700274 }
275
276 size_t total_num_reference_instance_fields = 0;
277 Class* k = klass;
278 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700279 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
280 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700281 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700282 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700283 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700284 }
285
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 void AssertDexFileClass(ClassLoader* class_loader, const std::string& descriptor) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700287 ASSERT_TRUE(descriptor != NULL);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800288 Class* klass = class_linker_->FindSystemClass(descriptor.c_str());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700289 ASSERT_TRUE(klass != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800290 EXPECT_STREQ(descriptor.c_str(), ClassHelper(klass).GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700291 EXPECT_EQ(class_loader, klass->GetClassLoader());
292 if (klass->IsPrimitive()) {
293 AssertPrimitiveClass(descriptor, klass);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700294 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700295 AssertArrayClass(descriptor, klass);
296 } else {
297 AssertClass(descriptor, klass);
298 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700299 }
300
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700301 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700302 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700303
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700304 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700305 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700306 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700307 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700308 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700309 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700310 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700311 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
312 const DexFile::TypeId& type_id = dex->GetTypeId(i);
313 const char* descriptor = dex->GetTypeDescriptor(type_id);
314 AssertDexFileClass(class_loader, descriptor);
315 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700316 class_linker_->VisitRoots(TestRootVisitor, NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800317 // Verify the dex cache has resolution methods in all resolved method slots
318 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
319 ObjectArray<Method>* resolved_methods = dex_cache->GetResolvedMethods();
320 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
321 EXPECT_TRUE(resolved_methods->Get(i) != NULL);
322 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700323 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700324
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700325 static void TestRootVisitor(const Object* root, void*) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700326 EXPECT_TRUE(root != NULL);
327 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700328};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700329
Brian Carlstrom693267a2011-09-06 09:25:34 -0700330struct CheckOffset {
331 size_t cpp_offset;
332 const char* java_name;
333 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
334};
335
Elliott Hughes80609252011-09-23 17:24:51 -0700336template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700337struct CheckOffsets {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700338 CheckOffsets(bool is_static, const char* class_descriptor)
339 : is_static(is_static), class_descriptor(class_descriptor) {}
340 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700341 std::string class_descriptor;
342 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700343
344 bool Check() {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800345 Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor.c_str());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700346 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700347
348 bool error = false;
349
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350 if (!klass->IsClassClass() && !is_static) {
351 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700352 if (sizeof(T) != expected_size) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700353 LG << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700354 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700355 << " Java=" << expected_size
356 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700357 error = true;
358 }
359 }
360
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700361 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700362 if (offsets.size() != num_fields) {
363 LG << "Field count mismatch:"
364 << " class=" << class_descriptor
365 << " Java=" << num_fields
366 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700367 error = true;
368 }
369
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800370 FieldHelper fh;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700371 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 fh.ChangeField(field);
374 StringPiece field_name(fh.GetName());
375 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700376 error = true;
377 }
378 }
379 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700380 for (size_t i = 0; i < offsets.size(); i++) {
381 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700382 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 fh.ChangeField(field);
384 StringPiece field_name(fh.GetName());
385 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700386 LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
387 }
388 LG << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700389 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700391 << " CheckOffsets=" << offset.java_name;
392 }
393 }
394
Brian Carlstromdbc05252011-09-09 01:59:59 -0700395 for (size_t i = 0; i < offsets.size(); i++) {
396 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700398 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
399 error = true;
400 }
401 }
402 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700403 for (size_t i = 0; i < offsets.size(); i++) {
404 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700406 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
407 LG << "OFFSET MISMATCH NEXT LINE:";
408 }
Brian Carlstromdbc05252011-09-09 01:59:59 -0700409 LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700410 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
411 }
412 }
413
414 return !error;
415 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700416
417 private:
418 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700419};
420
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700421// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
422// is first since if it is failing, others are unlikely to succeed.
423
Elliott Hughes80609252011-09-23 17:24:51 -0700424struct ObjectOffsets : public CheckOffsets<Object> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 ObjectOffsets() : CheckOffsets<Object>(false, "Ljava/lang/Object;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700426 // alphabetical references
427 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_"));
428
429 // alphabetical 32-bit
430 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700431 };
432};
433
Elliott Hughes80609252011-09-23 17:24:51 -0700434struct FieldOffsets : public CheckOffsets<Field> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700435 FieldOffsets() : CheckOffsets<Field>(false, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700436 // alphabetical references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700438
439 // alphabetical 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "accessFlags"));
441 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, field_dex_idx_), "fieldDexIndex"));
442 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700443 };
444};
445
Elliott Hughes80609252011-09-23 17:24:51 -0700446struct MethodOffsets : public CheckOffsets<Method> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447 MethodOffsets() : CheckOffsets<Method>(false, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700448 // alphabetical references
449 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, declaring_class_), "declaringClass"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_initialized_static_storage_), "dexCacheInitializedStaticStorage"));
Ian Rogers19846512012-02-24 11:42:47 -0800451 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800452 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
453 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_strings_), "dexCacheStrings"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700454
455 // alphabetical 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800456 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "accessFlags"));
457 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "code"));
458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "codeItemOffset"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "coreSpillMask"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "fpSpillMask"));
461 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "frameSizeInBytes"));
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800462 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, gc_map_), "gcMap"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800463 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "invokeStub"));
464 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "mappingTable"));
465 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_dex_index_), "methodDexIndex"));
466 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "methodIndex"));
467 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "nativeMethod"));
468 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, vmap_table_), "vmapTable"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700469 };
470};
471
Elliott Hughes80609252011-09-23 17:24:51 -0700472struct ConstructorOffsets : public MethodOffsets {
473 ConstructorOffsets() : MethodOffsets() {
474 // We use Method* for both java.lang.reflect.Constructor and java.lang.reflect.Method.
475 class_descriptor = "Ljava/lang/reflect/Constructor;";
476 }
477};
478
479struct ClassOffsets : public CheckOffsets<Class> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 ClassOffsets() : CheckOffsets<Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700481 // alphabetical references
Ian Rogersd418eda2012-01-30 12:14:28 -0800482 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "classLoader"));
483 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "componentType"));
484 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "dexCache"));
485 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "directMethods"));
486 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "iFields"));
487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "ifTable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800489 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "sFields"));
490 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "superClass"));
491 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "verifyErrorClass"));
492 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "virtualMethods"));
493 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "vtable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700494
495 // alphabetical 32-bit
Ian Rogersd418eda2012-01-30 12:14:28 -0800496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "accessFlags"));
497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "classSize"));
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "clinitThreadId"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_type_idx_), "dexTypeIndex"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "numReferenceStaticFields"));
502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "objectSize"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "primitiveType"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "referenceInstanceOffsets"));
505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "referenceStaticOffsets"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "status"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700507 };
508};
509
Elliott Hughes80609252011-09-23 17:24:51 -0700510struct StringOffsets : public CheckOffsets<String> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700511 StringOffsets() : CheckOffsets<String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700512 // alphabetical references
513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
514
515 // alphabetical 32-bit
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 };
520};
521
Elliott Hughes80609252011-09-23 17:24:51 -0700522struct ThrowableOffsets : public CheckOffsets<Throwable> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 ThrowableOffsets() : CheckOffsets<Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 // alphabetical references
525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
526 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700530 };
531};
532
Elliott Hughes80609252011-09-23 17:24:51 -0700533struct StackTraceElementOffsets : public CheckOffsets<StackTraceElement> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700534 StackTraceElementOffsets() : CheckOffsets<StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700535 // alphabetical references
536 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass"));
537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName"));
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName"));
539 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540 };
541};
542
Elliott Hughes80609252011-09-23 17:24:51 -0700543struct ClassLoaderOffsets : public CheckOffsets<ClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544 ClassLoaderOffsets() : CheckOffsets<ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700545 // alphabetical references
Brian Carlstrom87293d02012-04-01 19:53:04 -0700546 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
547 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 };
550};
551
Elliott Hughes80609252011-09-23 17:24:51 -0700552struct BaseDexClassLoaderOffsets : public CheckOffsets<BaseDexClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553 BaseDexClassLoaderOffsets()
554 : CheckOffsets<BaseDexClassLoader>(false, "Ldalvik/system/BaseDexClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700555 // alphabetical references
Brian Carlstrom87293d02012-04-01 19:53:04 -0700556 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_library_path_), "originalLibraryPath"));
557 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, original_path_), "originalPath"));
558 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(BaseDexClassLoader, path_list_), "pathList"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700559 };
560};
561
Elliott Hughes80609252011-09-23 17:24:51 -0700562struct PathClassLoaderOffsets : public CheckOffsets<PathClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 PathClassLoaderOffsets()
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700564 : CheckOffsets<PathClassLoader>(false, "Ldalvik/system/PathClassLoader;") {}
Brian Carlstromdbc05252011-09-09 01:59:59 -0700565};
566
Jesse Wilson95caa792011-10-12 18:14:17 -0400567struct ProxyOffsets : public CheckOffsets<Proxy> {
568 ProxyOffsets() : CheckOffsets<Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400569 // alphabetical references
570 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Proxy, h_), "h"));
571 };
572};
573
Elliott Hughes80609252011-09-23 17:24:51 -0700574struct ClassClassOffsets : public CheckOffsets<ClassClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700575 ClassClassOffsets() : CheckOffsets<ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700576 // padding 32-bit
577 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
578 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
579
Brian Carlstromdbc05252011-09-09 01:59:59 -0700580 // alphabetical 64-bit
581 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
582 };
583};
584
Elliott Hughes80609252011-09-23 17:24:51 -0700585struct StringClassOffsets : public CheckOffsets<StringClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700586 StringClassOffsets() : CheckOffsets<StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700587 // alphabetical references
588 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
589 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
590
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700591 // padding 32-bit
592 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
593
Brian Carlstromdbc05252011-09-09 01:59:59 -0700594 // alphabetical 64-bit
595 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
596 };
597};
598
Elliott Hughes80609252011-09-23 17:24:51 -0700599struct FieldClassOffsets : public CheckOffsets<FieldClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700600 FieldClassOffsets() : CheckOffsets<FieldClass>(true, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700601 // alphabetical references
602 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(FieldClass, ORDER_BY_NAME_AND_DECLARING_CLASS_), "ORDER_BY_NAME_AND_DECLARING_CLASS"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700603 };
604};
605
Elliott Hughes80609252011-09-23 17:24:51 -0700606struct MethodClassOffsets : public CheckOffsets<MethodClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700607 MethodClassOffsets() : CheckOffsets<MethodClass>(true, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700608 // alphabetical references
Brian Carlstromdbc05252011-09-09 01:59:59 -0700609 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700610 };
611};
612
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400613// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400614// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700615// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400616TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700617 EXPECT_TRUE(ObjectOffsets().Check());
Elliott Hughes80609252011-09-23 17:24:51 -0700618 EXPECT_TRUE(ConstructorOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700619 EXPECT_TRUE(FieldOffsets().Check());
620 EXPECT_TRUE(MethodOffsets().Check());
621 EXPECT_TRUE(ClassOffsets().Check());
622 EXPECT_TRUE(StringOffsets().Check());
623 EXPECT_TRUE(ThrowableOffsets().Check());
624 EXPECT_TRUE(StackTraceElementOffsets().Check());
625 EXPECT_TRUE(ClassLoaderOffsets().Check());
626 EXPECT_TRUE(BaseDexClassLoaderOffsets().Check());
627 EXPECT_TRUE(PathClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400628 EXPECT_TRUE(ProxyOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700629
630 EXPECT_TRUE(ClassClassOffsets().Check());
631 EXPECT_TRUE(StringClassOffsets().Check());
632 EXPECT_TRUE(FieldClassOffsets().Check());
633 EXPECT_TRUE(MethodClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700634}
635
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700636TEST_F(ClassLinkerTest, FindClassNonexistent) {
637 AssertNonExistentClass("NoSuchClass;");
638 AssertNonExistentClass("LNoSuchClass;");
639}
640
641TEST_F(ClassLinkerTest, FindClassNested) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700642 SirtRef<ClassLoader> class_loader(LoadDex("Nested"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700643
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700644 Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700645 ASSERT_TRUE(outer != NULL);
646 EXPECT_EQ(0U, outer->NumVirtualMethods());
647 EXPECT_EQ(1U, outer->NumDirectMethods());
648
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700649 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700650 ASSERT_TRUE(inner != NULL);
651 EXPECT_EQ(0U, inner->NumVirtualMethods());
652 EXPECT_EQ(1U, inner->NumDirectMethods());
653}
654
655TEST_F(ClassLinkerTest, FindClass_Primitives) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700656 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800657 for (int ch = 1; ch < 256; ++ch) {
658 std::string descriptor;
659 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700660 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700661 AssertNonExistentClass(descriptor);
662 } else {
663 AssertPrimitiveClass(descriptor);
664 }
665 }
666}
667
668TEST_F(ClassLinkerTest, FindClass) {
669 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800670 ClassHelper kh(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700671 ASSERT_TRUE(JavaLangObject != NULL);
672 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
673 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
674 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800675 ASSERT_STREQ(kh.GetDescriptor(), "Ljava/lang/Object;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700676 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
677 EXPECT_FALSE(JavaLangObject->HasSuperClass());
678 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700679 EXPECT_EQ(Class::kStatusResolved, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700680 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700681 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700682 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700683 EXPECT_FALSE(JavaLangObject->IsVerified());
684 EXPECT_FALSE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700685 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
686 EXPECT_FALSE(JavaLangObject->IsArrayClass());
687 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
688 EXPECT_FALSE(JavaLangObject->IsInterface());
689 EXPECT_TRUE(JavaLangObject->IsPublic());
690 EXPECT_FALSE(JavaLangObject->IsFinal());
691 EXPECT_FALSE(JavaLangObject->IsPrimitive());
692 EXPECT_FALSE(JavaLangObject->IsSynthetic());
693 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
694 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
695 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800696 FieldHelper fh(JavaLangObject->GetInstanceField(0));
697 EXPECT_STREQ(fh.GetName(), "shadow$_klass_");
698 fh.ChangeField(JavaLangObject->GetInstanceField(1));
699 EXPECT_STREQ(fh.GetName(), "shadow$_monitor_");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700700
701 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800702 EXPECT_EQ(0U, kh.NumInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700703
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700704 SirtRef<ClassLoader> class_loader(LoadDex("MyClass"));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700705 AssertNonExistentClass("LMyClass;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700706 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800707 kh.ChangeClass(MyClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700708 ASSERT_TRUE(MyClass != NULL);
709 ASSERT_TRUE(MyClass->GetClass() != NULL);
710 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
711 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800712 ASSERT_STREQ(kh.GetDescriptor(), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700713 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
714 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700715 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700716 EXPECT_EQ(Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700717 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700718 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700719 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700720 EXPECT_FALSE(MyClass->IsVerified());
721 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700722 EXPECT_FALSE(MyClass->IsArrayInstance());
723 EXPECT_FALSE(MyClass->IsArrayClass());
724 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
725 EXPECT_FALSE(MyClass->IsInterface());
726 EXPECT_FALSE(MyClass->IsPublic());
727 EXPECT_FALSE(MyClass->IsFinal());
728 EXPECT_FALSE(MyClass->IsPrimitive());
729 EXPECT_FALSE(MyClass->IsSynthetic());
730 EXPECT_EQ(1U, MyClass->NumDirectMethods());
731 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
732 EXPECT_EQ(0U, MyClass->NumInstanceFields());
733 EXPECT_EQ(0U, MyClass->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800734 EXPECT_EQ(0U, kh.NumInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700735
736 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
737
738 // created by class_linker
739 AssertArrayClass("[C", "C", NULL);
740 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
741 // synthesized on the fly
742 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700743 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700744 // or not available at all
745 AssertNonExistentClass("[[[[LNonExistentClass;");
746}
747
748TEST_F(ClassLinkerTest, LibCore) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800749 AssertDexFile(java_lang_dex_file_, NULL);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700750}
751
Ian Rogersa15e67d2012-02-28 13:51:55 -0800752// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400753// start of the object
754TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
755 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
756 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
757 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700758 uint32_t data_offset =
Ian Rogersa15e67d2012-02-28 13:51:55 -0800759 array_offset + ObjectArray<String>::DataOffset(sizeof(String*)).Uint32Value();
760 if (sizeof(String*) == sizeof(int32_t)) {
761 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
762 } else {
763 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
764 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400765}
766
767TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800768 SirtRef<LongArray> long_array(LongArray::Alloc(0));
769 EXPECT_EQ(class_linker_->FindSystemClass("[J"), long_array->GetClass());
770 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
771 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
772
773 SirtRef<DoubleArray> double_array(DoubleArray::Alloc(0));
774 EXPECT_EQ(class_linker_->FindSystemClass("[D"), double_array->GetClass());
775 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
776 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
777
778 SirtRef<IntArray> int_array(IntArray::Alloc(0));
779 EXPECT_EQ(class_linker_->FindSystemClass("[I"), int_array->GetClass());
780 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
781 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
782
783 SirtRef<CharArray> char_array(CharArray::Alloc(0));
784 EXPECT_EQ(class_linker_->FindSystemClass("[C"), char_array->GetClass());
785 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
786 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
787
788 SirtRef<ShortArray> short_array(ShortArray::Alloc(0));
789 EXPECT_EQ(class_linker_->FindSystemClass("[S"), short_array->GetClass());
790 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
791 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
792
793 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400794}
795
Elliott Hughes33203b52011-09-20 19:42:01 -0700796TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
797 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
798 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
799 Class* c;
800 c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800801 FieldHelper fh(c->GetIFields()->Get(0));
802 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700803 c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800804 fh.ChangeField(c->GetIFields()->Get(0));
805 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700806 c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800807 fh.ChangeField(c->GetIFields()->Get(0));
808 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700809 c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800810 fh.ChangeField(c->GetIFields()->Get(0));
811 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700812 c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800813 fh.ChangeField(c->GetIFields()->Get(0));
814 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700815 c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800816 fh.ChangeField(c->GetIFields()->Get(0));
817 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700818 c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800819 fh.ChangeField(c->GetIFields()->Get(0));
820 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700821 c = class_linker_->FindClass("Ljava/lang/Short;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800822 fh.ChangeField(c->GetIFields()->Get(0));
823 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700824}
825
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700826TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700827 SirtRef<ClassLoader> class_loader_1(LoadDex("MyClass"));
828 SirtRef<ClassLoader> class_loader_2(LoadDex("MyClass"));
829 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
830 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700831 EXPECT_TRUE(MyClass_1 != NULL);
832 EXPECT_TRUE(MyClass_2 != NULL);
833 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400834}
835
Jesse Wilson7833bd22011-08-09 18:31:44 -0400836TEST_F(ClassLinkerTest, StaticFields) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700837 SirtRef<ClassLoader> class_loader(LoadDex("Statics"));
838 Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
Ian Rogers0045a292012-03-31 21:08:41 -0700839 class_linker_->EnsureInitialized(statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400840
jeffhaoabcfde32011-09-29 15:05:18 -0700841 // Static final primitives that are initialized by a compile-time constant
842 // expression resolve to a copy of a constant value from the constant pool.
843 // So <clinit> should be null.
844 Method* clinit = statics->FindDirectMethod("<clinit>", "()V");
845 EXPECT_TRUE(clinit == NULL);
846
847 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400848
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700849 Field* s0 = statics->FindStaticField("s0", "Z");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800850 FieldHelper fh(s0);
Elliott Hughes91250e02011-12-13 22:30:35 -0800851 EXPECT_STREQ(ClassHelper(s0->GetClass()).GetDescriptor(), "Ljava/lang/reflect/Field;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800852 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimBoolean);
jeffhaoabcfde32011-09-29 15:05:18 -0700853 EXPECT_EQ(true, s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700854 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400855
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700856 Field* s1 = statics->FindStaticField("s1", "B");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800857 fh.ChangeField(s1);
858 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimByte);
jeffhaoabcfde32011-09-29 15:05:18 -0700859 EXPECT_EQ(5, s1->GetByte(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700860 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400861
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700862 Field* s2 = statics->FindStaticField("s2", "C");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800863 fh.ChangeField(s2);
864 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimChar);
jeffhaoabcfde32011-09-29 15:05:18 -0700865 EXPECT_EQ('a', s2->GetChar(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700866 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400867
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700868 Field* s3 = statics->FindStaticField("s3", "S");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800869 fh.ChangeField(s3);
870 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimShort);
Ian Rogers466bb252011-10-14 03:29:56 -0700871 EXPECT_EQ(-536, s3->GetShort(NULL));
872 s3->SetShort(NULL, -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400873
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700874 Field* s4 = statics->FindStaticField("s4", "I");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800875 fh.ChangeField(s4);
876 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimInt);
jeffhaoabcfde32011-09-29 15:05:18 -0700877 EXPECT_EQ(2000000000, s4->GetInt(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700878 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400879
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700880 Field* s5 = statics->FindStaticField("s5", "J");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800881 fh.ChangeField(s5);
882 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimLong);
jeffhaoabcfde32011-09-29 15:05:18 -0700883 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700884 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400885
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700886 Field* s6 = statics->FindStaticField("s6", "F");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800887 fh.ChangeField(s6);
888 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimFloat);
jeffhaoabcfde32011-09-29 15:05:18 -0700889 EXPECT_EQ(0.5, s6->GetFloat(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700890 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400891
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700892 Field* s7 = statics->FindStaticField("s7", "D");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800893 fh.ChangeField(s7);
894 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimDouble);
jeffhaoabcfde32011-09-29 15:05:18 -0700895 EXPECT_EQ(16777217, s7->GetDouble(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700896 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400897
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700898 Field* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800899 fh.ChangeField(s8);
900 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimNot);
jeffhaoabcfde32011-09-29 15:05:18 -0700901 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700902 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400903
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800904 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
905 // http://code.google.com/p/googletest/issues/detail?id=322
906 EXPECT_FALSE( s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700907 EXPECT_EQ(6, s1->GetByte(NULL));
908 EXPECT_EQ('b', s2->GetChar(NULL));
Ian Rogers466bb252011-10-14 03:29:56 -0700909 EXPECT_EQ(-535, s3->GetShort(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700910 EXPECT_EQ(2000000001, s4->GetInt(NULL));
911 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL));
912 EXPECT_EQ(0.75, s6->GetFloat(NULL));
913 EXPECT_EQ(16777219, s7->GetDouble(NULL));
914 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400915}
916
Brian Carlstrom30b94452011-08-25 21:35:26 -0700917TEST_F(ClassLinkerTest, Interfaces) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700918 SirtRef<ClassLoader> class_loader(LoadDex("Interfaces"));
919 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
920 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
921 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
922 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
923 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700924 EXPECT_TRUE(I->IsAssignableFrom(A));
925 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700926 EXPECT_TRUE(J->IsAssignableFrom(K));
927 EXPECT_TRUE(K->IsAssignableFrom(B));
928 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700929
930 Method* Ii = I->FindVirtualMethod("i", "()V");
931 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
932 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700933 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
934 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
935 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700936 Method* Ai = A->FindVirtualMethod("i", "()V");
937 Method* Aj1 = A->FindVirtualMethod("j1", "()V");
938 Method* Aj2 = A->FindVirtualMethod("j2", "()V");
939 ASSERT_TRUE(Ii != NULL);
940 ASSERT_TRUE(Jj1 != NULL);
941 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700942 ASSERT_TRUE(Kj1 != NULL);
943 ASSERT_TRUE(Kj2 != NULL);
944 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700945 ASSERT_TRUE(Ai != NULL);
946 ASSERT_TRUE(Aj1 != NULL);
947 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700948 EXPECT_NE(Ii, Ai);
949 EXPECT_NE(Jj1, Aj1);
950 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700951 EXPECT_EQ(Kj1, Jj1);
952 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800953 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
954 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
955 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700956 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
957 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
958 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
959}
960
Elliott Hughesf3778f62012-01-26 14:14:35 -0800961TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -0700962 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700963
jeffhaoabcfde32011-09-29 15:05:18 -0700964 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
965 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700966
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700967 SirtRef<ClassLoader> class_loader(LoadDex("StaticsFromCode"));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800968 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(class_loader.get())[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700969 CHECK(dex_file != NULL);
970
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700971 Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
jeffhaoabcfde32011-09-29 15:05:18 -0700972 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
973 Method* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800974 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
975 ASSERT_TRUE(string_id != NULL);
976 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
977 ASSERT_TRUE(type_id != NULL);
978 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700979 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800980 StaticStorageBase* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700981 EXPECT_TRUE(uninit != NULL);
982 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800983 StaticStorageBase* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700984 EXPECT_TRUE(init != NULL);
985 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700986}
987
Elliott Hughes20cde902011-10-04 17:37:27 -0700988TEST_F(ClassLinkerTest, FinalizableBit) {
989 Class* c;
990
991 // Object has a finalize method, but we know it's empty.
992 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
993 EXPECT_FALSE(c->IsFinalizable());
994
995 // Enum has a finalize method to prevent its subclasses from implementing one.
996 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
997 EXPECT_FALSE(c->IsFinalizable());
998
999 // RoundingMode is an enum.
1000 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
1001 EXPECT_FALSE(c->IsFinalizable());
1002
1003 // RandomAccessFile extends Object and overrides finalize.
1004 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
1005 EXPECT_TRUE(c->IsFinalizable());
1006
1007 // FileInputStream is finalizable and extends InputStream which isn't.
1008 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
1009 EXPECT_FALSE(c->IsFinalizable());
1010 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
1011 EXPECT_TRUE(c->IsFinalizable());
1012
1013 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1014 // extends ThreadPoolExecutor which does.
1015 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
1016 EXPECT_TRUE(c->IsFinalizable());
1017 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
1018 EXPECT_TRUE(c->IsFinalizable());
1019}
1020
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001021TEST_F(ClassLinkerTest, ClassRootDescriptors) {
1022 ClassHelper kh;
1023 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
1024 Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
1025 kh.ChangeClass(klass);
1026 EXPECT_TRUE(kh.GetDescriptor() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -08001027 EXPECT_STREQ(kh.GetDescriptor(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001028 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1029 }
1030}
1031
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001032} // namespace art