blob: d32e91e98af36ef92675efe4f8eb76b3f6b7ddd1 [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"
Ian Rogers1f539342012-10-03 21:09:42 -070027#include "sirt_ref.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028
29namespace art {
30
Brian Carlstromf734cf52011-08-17 16:28:14 -070031class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 void AssertNonExistentClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080035 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor.c_str()) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070036 Thread* self = Thread::Current();
37 EXPECT_TRUE(self->IsExceptionPending());
38 Object* exception = self->GetException();
39 self->ClearException();
40 Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
41 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042 }
43
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 void AssertPrimitiveClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080046 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070047 }
48
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 void AssertPrimitiveClass(const std::string& descriptor, const Class* primitive)
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080051 ClassHelper primitive_ch(primitive);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 ASSERT_TRUE(primitive != NULL);
53 ASSERT_TRUE(primitive->GetClass() != NULL);
54 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
55 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -080056 ASSERT_STREQ(descriptor.c_str(), primitive_ch.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
58 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070059 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -070060 EXPECT_EQ(Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070062 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070063 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070064 EXPECT_TRUE(primitive->IsVerified());
65 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070066 EXPECT_FALSE(primitive->IsArrayInstance());
67 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070068 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070069 EXPECT_FALSE(primitive->IsInterface());
70 EXPECT_TRUE(primitive->IsPublic());
71 EXPECT_TRUE(primitive->IsFinal());
72 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070073 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 EXPECT_EQ(0U, primitive->NumDirectMethods());
75 EXPECT_EQ(0U, primitive->NumVirtualMethods());
76 EXPECT_EQ(0U, primitive->NumInstanceFields());
77 EXPECT_EQ(0U, primitive->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -070078 EXPECT_EQ(0U, primitive_ch.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070079 EXPECT_TRUE(primitive->GetVTable() == NULL);
80 EXPECT_EQ(0, primitive->GetIfTableCount());
81 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070082 }
83
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 void AssertArrayClass(const std::string& array_descriptor,
85 const std::string& component_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080088 Class* array = class_linker_->FindClass(array_descriptor.c_str(), class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080089 ClassHelper array_component_ch(array->GetComponentType());
Elliott Hughes91250e02011-12-13 22:30:35 -080090 EXPECT_STREQ(component_type.c_str(), array_component_ch.GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070091 EXPECT_EQ(class_loader, array->GetClassLoader());
92 AssertArrayClass(array_descriptor, array);
93 }
94
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 void AssertArrayClass(const std::string& array_descriptor, Class* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080097 ClassHelper kh(array);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070098 ASSERT_TRUE(array != NULL);
99 ASSERT_TRUE(array->GetClass() != NULL);
100 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
101 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800102 ASSERT_STREQ(array_descriptor.c_str(), kh.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700104 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700105 EXPECT_TRUE(array->HasSuperClass());
106 ASSERT_TRUE(array->GetComponentType() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800107 kh.ChangeClass(array->GetComponentType());
108 ASSERT_TRUE(kh.GetDescriptor() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700109 EXPECT_EQ(Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700110 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700111 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700112 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700113 EXPECT_TRUE(array->IsVerified());
114 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700115 EXPECT_FALSE(array->IsArrayInstance());
116 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700117 EXPECT_FALSE(array->IsInterface());
118 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
119 EXPECT_TRUE(array->IsFinal());
120 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700121 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700122 EXPECT_EQ(0U, array->NumDirectMethods());
123 EXPECT_EQ(0U, array->NumVirtualMethods());
124 EXPECT_EQ(0U, array->NumInstanceFields());
125 EXPECT_EQ(0U, array->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 kh.ChangeClass(array);
Ian Rogersd24e2642012-06-06 21:21:43 -0700127 EXPECT_EQ(2U, kh.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -0700128 EXPECT_TRUE(array->GetVTable() != NULL);
129 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogers9bc81912012-10-11 21:43:36 -0700130 IfTable* iftable = array->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700131 ASSERT_TRUE(iftable != NULL);
Ian Rogersd24e2642012-06-06 21:21:43 -0700132 kh.ChangeClass(kh.GetDirectInterface(0));
Elliott Hughes91250e02011-12-13 22:30:35 -0800133 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 kh.ChangeClass(array);
Ian Rogersd24e2642012-06-06 21:21:43 -0700135 kh.ChangeClass(kh.GetDirectInterface(1));
Elliott Hughes91250e02011-12-13 22:30:35 -0800136 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/io/Serializable;");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700137 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700138
Mathieu Chartier66f19252012-09-18 08:57:04 -0700139 void AssertMethod(AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 MethodHelper mh(method);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700141 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700142 EXPECT_TRUE(method->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 EXPECT_TRUE(mh.GetName() != NULL);
144 EXPECT_TRUE(mh.GetSignature() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700145
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800147 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700149 EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
150 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
151 method->GetDexCacheStrings());
Ian Rogers19846512012-02-24 11:42:47 -0800152 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
153 method->GetDexCacheResolvedMethods());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
155 method->GetDexCacheResolvedTypes());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
157 method->GetDexCacheInitializedStaticStorage());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700158 }
159
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 void AssertField(Class* klass, Field* field)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800162 FieldHelper fh(field);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700163 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700164 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700165 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 EXPECT_TRUE(fh.GetName() != NULL);
167 EXPECT_TRUE(fh.GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700168 }
169
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 void AssertClass(const std::string& descriptor, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 ClassHelper kh(klass);
Elliott Hughes91250e02011-12-13 22:30:35 -0800173 EXPECT_STREQ(descriptor.c_str(), kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700175 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700176 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177 EXPECT_TRUE(klass->HasSuperClass());
178 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700179 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700180 EXPECT_TRUE(klass->GetClass() != NULL);
181 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700182 EXPECT_TRUE(klass->GetDexCache() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700183 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700184 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700185 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700187 EXPECT_TRUE(klass->GetComponentType() == NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700188 EXPECT_TRUE(klass->IsInSamePackage(klass));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 EXPECT_TRUE(Class::IsInSamePackage(kh.GetDescriptor(), kh.GetDescriptor()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700190 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700191 EXPECT_TRUE(klass->IsAbstract());
192 if (klass->NumDirectMethods() == 1) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 MethodHelper mh(klass->GetDirectMethod(0));
194 EXPECT_TRUE(mh.IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700195 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700196 } else {
197 EXPECT_EQ(0U, klass->NumDirectMethods());
198 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700199 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700200 if (!klass->IsSynthetic()) {
201 EXPECT_NE(0U, klass->NumDirectMethods());
202 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700203 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700204 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
Ian Rogers9bc81912012-10-11 21:43:36 -0700205 const IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700206 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700207 Class* interface = iftable->GetInterface(i);
Brian Carlstrom86927212011-09-15 11:31:11 -0700208 ASSERT_TRUE(interface != NULL);
Brian Carlstrom86927212011-09-15 11:31:11 -0700209 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700210 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700211 } else {
Ian Rogers9bc81912012-10-11 21:43:36 -0700212 EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700213 }
214 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700215 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700217 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700219 }
220 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700221 EXPECT_FALSE(klass->IsAbstract());
222 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700223 }
224 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700225 EXPECT_FALSE(klass->IsFinal());
226 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700227 }
228
229 EXPECT_FALSE(klass->IsPrimitive());
230 EXPECT_TRUE(klass->CanAccess(klass));
231
232 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700233 AbstractMethod* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700234 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700235 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400236 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700237 }
238
239 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700240 AbstractMethod* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700241 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700242 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700243 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700244 }
245
246 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400247 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700248 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700249 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700250 }
251
252 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400253 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700254 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700255 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700256 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700257
258 // Confirm that all instances fields are packed together at the start
259 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 FieldHelper fh;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700261 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400262 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 fh.ChangeField(field);
264 ASSERT_TRUE(!fh.IsPrimitiveType());
265 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700268 }
269 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400270 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 fh.ChangeField(field);
272 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 ASSERT_TRUE(field_type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 if (!fh.IsPrimitiveType() || !field_type->IsPrimitive()) {
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700275 // While Reference.referent is not primitive, the ClassLinker
276 // treats it as such so that the garbage collector won't scan it.
277 EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
278 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700279 }
280
281 size_t total_num_reference_instance_fields = 0;
282 Class* k = klass;
283 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
285 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700286 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700287 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700288 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700289 }
290
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 void AssertDexFileClass(ClassLoader* class_loader, const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700293 ASSERT_TRUE(descriptor != NULL);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800294 Class* klass = class_linker_->FindSystemClass(descriptor.c_str());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700295 ASSERT_TRUE(klass != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800296 EXPECT_STREQ(descriptor.c_str(), ClassHelper(klass).GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700297 EXPECT_EQ(class_loader, klass->GetClassLoader());
298 if (klass->IsPrimitive()) {
299 AssertPrimitiveClass(descriptor, klass);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700300 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700301 AssertArrayClass(descriptor, klass);
302 } else {
303 AssertClass(descriptor, klass);
304 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700305 }
306
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700310
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700311 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700312 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700313 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700314 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700316 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700317 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700318 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
319 const DexFile::TypeId& type_id = dex->GetTypeId(i);
320 const char* descriptor = dex->GetTypeDescriptor(type_id);
321 AssertDexFileClass(class_loader, descriptor);
322 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700323 class_linker_->VisitRoots(TestRootVisitor, NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800324 // Verify the dex cache has resolution methods in all resolved method slots
325 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700326 ObjectArray<AbstractMethod>* resolved_methods = dex_cache->GetResolvedMethods();
Ian Rogers19846512012-02-24 11:42:47 -0800327 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
328 EXPECT_TRUE(resolved_methods->Get(i) != NULL);
329 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700330 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700331
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700332 static void TestRootVisitor(const Object* root, void*) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700333 EXPECT_TRUE(root != NULL);
334 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700335};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700336
Brian Carlstrom693267a2011-09-06 09:25:34 -0700337struct CheckOffset {
338 size_t cpp_offset;
339 const char* java_name;
340 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
341};
342
Elliott Hughes80609252011-09-23 17:24:51 -0700343template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700344struct CheckOffsets {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700345 CheckOffsets(bool is_static, const char* class_descriptor)
346 : is_static(is_static), class_descriptor(class_descriptor) {}
347 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700348 std::string class_descriptor;
349 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700350
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800352 Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor.c_str());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700353 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700354
355 bool error = false;
356
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357 if (!klass->IsClassClass() && !is_static) {
358 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700359 if (sizeof(T) != expected_size) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700360 LG << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700361 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700362 << " Java=" << expected_size
363 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700364 error = true;
365 }
366 }
367
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700369 if (offsets.size() != num_fields) {
370 LG << "Field count mismatch:"
371 << " class=" << class_descriptor
372 << " Java=" << num_fields
373 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700374 error = true;
375 }
376
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800377 FieldHelper fh;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700378 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380 fh.ChangeField(field);
381 StringPiece field_name(fh.GetName());
382 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700383 error = true;
384 }
385 }
386 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700387 for (size_t i = 0; i < offsets.size(); i++) {
388 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700389 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 fh.ChangeField(field);
391 StringPiece field_name(fh.GetName());
392 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700393 LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
394 }
395 LG << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700396 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800397 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700398 << " CheckOffsets=" << offset.java_name;
399 }
400 }
401
Brian Carlstromdbc05252011-09-09 01:59:59 -0700402 for (size_t i = 0; i < offsets.size(); i++) {
403 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700404 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700405 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
406 error = true;
407 }
408 }
409 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700410 for (size_t i = 0; i < offsets.size(); i++) {
411 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700412 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700413 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
414 LG << "OFFSET MISMATCH NEXT LINE:";
415 }
Brian Carlstromdbc05252011-09-09 01:59:59 -0700416 LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700417 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
418 }
419 }
420
421 return !error;
422 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700423
424 private:
425 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700426};
427
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700428// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
429// is first since if it is failing, others are unlikely to succeed.
430
Elliott Hughes80609252011-09-23 17:24:51 -0700431struct ObjectOffsets : public CheckOffsets<Object> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700432 ObjectOffsets() : CheckOffsets<Object>(false, "Ljava/lang/Object;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700433 // alphabetical references
434 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_"));
435
436 // alphabetical 32-bit
437 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700438 };
439};
440
Elliott Hughes80609252011-09-23 17:24:51 -0700441struct FieldOffsets : public CheckOffsets<Field> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700442 FieldOffsets() : CheckOffsets<Field>(false, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700443 // alphabetical references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700445
446 // alphabetical 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "accessFlags"));
448 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, field_dex_idx_), "fieldDexIndex"));
449 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700450 };
451};
452
Mathieu Chartier66f19252012-09-18 08:57:04 -0700453struct AbstractMethodOffsets : public CheckOffsets<AbstractMethod> {
454 AbstractMethodOffsets() : CheckOffsets<AbstractMethod>(false, "Ljava/lang/reflect/AbstractMethod;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700455 // alphabetical references
Mathieu Chartier66f19252012-09-18 08:57:04 -0700456 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, declaring_class_), "declaringClass"));
457 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_), "dexCacheInitializedStaticStorage"));
458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, dex_cache_strings_), "dexCacheStrings"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700461
462 // alphabetical 32-bit
Mathieu Chartier66f19252012-09-18 08:57:04 -0700463 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, access_flags_), "accessFlags"));
464 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, code_), "code"));
465 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, code_item_offset_), "codeItemOffset"));
466 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, core_spill_mask_), "coreSpillMask"));
467 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, fp_spill_mask_), "fpSpillMask"));
468 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, frame_size_in_bytes_), "frameSizeInBytes"));
469 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, native_gc_map_), "gcMap"));
470 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, invoke_stub_), "invokeStub"));
471 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, mapping_table_), "mappingTable"));
472 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, method_dex_index_), "methodDexIndex"));
473 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, method_index_), "methodIndex"));
474 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, native_method_), "nativeMethod"));
475 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(AbstractMethod, vmap_table_), "vmapTable"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700476 };
477};
478
Mathieu Chartier66f19252012-09-18 08:57:04 -0700479struct ConstructorOffsets : public CheckOffsets<Constructor> {
480 // java.lang.reflect.Constructor is a subclass of java.lang.reflect.AbstractMethod
481 ConstructorOffsets() : CheckOffsets<Constructor>(false, "Ljava/lang/reflect/Constructor;") {
482 }
483};
484
485struct MethodOffsets : public CheckOffsets<Method> {
486 // java.lang.reflect.Method is a subclass of java.lang.reflect.AbstractMethod
487 MethodOffsets() : CheckOffsets<Method>(false, "Ljava/lang/reflect/Method;") {
Elliott Hughes80609252011-09-23 17:24:51 -0700488 }
489};
490
491struct ClassOffsets : public CheckOffsets<Class> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 ClassOffsets() : CheckOffsets<Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700493 // alphabetical references
Ian Rogersd418eda2012-01-30 12:14:28 -0800494 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "classLoader"));
495 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "componentType"));
496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "dexCache"));
497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "directMethods"));
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "iFields"));
499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "ifTable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "sFields"));
502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "superClass"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "verifyErrorClass"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "virtualMethods"));
505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "vtable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700506
507 // alphabetical 32-bit
Ian Rogersd418eda2012-01-30 12:14:28 -0800508 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "accessFlags"));
509 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "classSize"));
510 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "clinitThreadId"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_type_idx_), "dexTypeIndex"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800512 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "numReferenceStaticFields"));
514 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "objectSize"));
515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "primitiveType"));
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "referenceInstanceOffsets"));
517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "referenceStaticOffsets"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "status"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 };
520};
521
Elliott Hughes80609252011-09-23 17:24:51 -0700522struct StringOffsets : public CheckOffsets<String> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 StringOffsets() : CheckOffsets<String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 // alphabetical references
525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
526
527 // alphabetical 32-bit
528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
530 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700531 };
532};
533
Elliott Hughes80609252011-09-23 17:24:51 -0700534struct ThrowableOffsets : public CheckOffsets<Throwable> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535 ThrowableOffsets() : CheckOffsets<Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700536 // alphabetical references
537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
539 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
540 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
541 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700542 };
543};
544
Elliott Hughes80609252011-09-23 17:24:51 -0700545struct StackTraceElementOffsets : public CheckOffsets<StackTraceElement> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 StackTraceElementOffsets() : CheckOffsets<StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700547 // 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
Elliott Hughes80609252011-09-23 17:24:51 -0700555struct ClassLoaderOffsets : public CheckOffsets<ClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700556 ClassLoaderOffsets() : CheckOffsets<ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700557 // alphabetical references
Brian Carlstrom87293d02012-04-01 19:53:04 -0700558 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
559 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
560 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700561 };
562};
563
Jesse Wilson95caa792011-10-12 18:14:17 -0400564struct ProxyOffsets : public CheckOffsets<Proxy> {
565 ProxyOffsets() : CheckOffsets<Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400566 // alphabetical references
567 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Proxy, h_), "h"));
568 };
569};
570
Elliott Hughes80609252011-09-23 17:24:51 -0700571struct ClassClassOffsets : public CheckOffsets<ClassClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700572 ClassClassOffsets() : CheckOffsets<ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700573 // padding 32-bit
574 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
575 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
576
Brian Carlstromdbc05252011-09-09 01:59:59 -0700577 // alphabetical 64-bit
578 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
579 };
580};
581
Elliott Hughes80609252011-09-23 17:24:51 -0700582struct StringClassOffsets : public CheckOffsets<StringClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700583 StringClassOffsets() : CheckOffsets<StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700584 // alphabetical references
585 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
586 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
587
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700588 // padding 32-bit
589 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
590
Brian Carlstromdbc05252011-09-09 01:59:59 -0700591 // alphabetical 64-bit
592 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
593 };
594};
595
Elliott Hughes80609252011-09-23 17:24:51 -0700596struct FieldClassOffsets : public CheckOffsets<FieldClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700597 FieldClassOffsets() : CheckOffsets<FieldClass>(true, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700598 // alphabetical references
599 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 -0700600 };
601};
602
Elliott Hughes80609252011-09-23 17:24:51 -0700603struct MethodClassOffsets : public CheckOffsets<MethodClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700604 MethodClassOffsets() : CheckOffsets<MethodClass>(true, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700605 // alphabetical references
Brian Carlstromdbc05252011-09-09 01:59:59 -0700606 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700607 };
608};
609
Mathieu Chartier66f19252012-09-18 08:57:04 -0700610struct DexCacheOffsets : public CheckOffsets<DexCache> {
611 DexCacheOffsets() : CheckOffsets<DexCache>(false, "Ljava/lang/DexCache;") {
612 // alphabetical references
613 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, initialized_static_storage_), "initializedStaticStorage"));
614 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, location_), "location"));
615 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, resolved_fields_), "resolvedFields"));
616 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, resolved_methods_), "resolvedMethods"));
617 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, resolved_types_), "resolvedTypes"));
618 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, strings_), "strings"));
619 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(DexCache, dex_file_), "dexFile"));
620 };
621};
622
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400623// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400624// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700625// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400626TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700628 EXPECT_TRUE(ObjectOffsets().Check());
Elliott Hughes80609252011-09-23 17:24:51 -0700629 EXPECT_TRUE(ConstructorOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700630 EXPECT_TRUE(MethodOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700631 EXPECT_TRUE(FieldOffsets().Check());
632 EXPECT_TRUE(AbstractMethodOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700633 EXPECT_TRUE(ClassOffsets().Check());
634 EXPECT_TRUE(StringOffsets().Check());
635 EXPECT_TRUE(ThrowableOffsets().Check());
636 EXPECT_TRUE(StackTraceElementOffsets().Check());
637 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400638 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700639 EXPECT_TRUE(DexCacheOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700640
641 EXPECT_TRUE(ClassClassOffsets().Check());
642 EXPECT_TRUE(StringClassOffsets().Check());
643 EXPECT_TRUE(FieldClassOffsets().Check());
644 EXPECT_TRUE(MethodClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700645}
646
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700647TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700649 AssertNonExistentClass("NoSuchClass;");
650 AssertNonExistentClass("LNoSuchClass;");
651}
652
653TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700655 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("Nested")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700656
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700657 Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700658 ASSERT_TRUE(outer != NULL);
659 EXPECT_EQ(0U, outer->NumVirtualMethods());
660 EXPECT_EQ(1U, outer->NumDirectMethods());
661
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700662 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700663 ASSERT_TRUE(inner != NULL);
664 EXPECT_EQ(0U, inner->NumVirtualMethods());
665 EXPECT_EQ(1U, inner->NumDirectMethods());
666}
667
668TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700670 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800671 for (int ch = 1; ch < 256; ++ch) {
672 std::string descriptor;
673 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700674 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700675 AssertNonExistentClass(descriptor);
676 } else {
677 AssertPrimitiveClass(descriptor);
678 }
679 }
680}
681
682TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700684 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800685 ClassHelper kh(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700686 ASSERT_TRUE(JavaLangObject != NULL);
687 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
688 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
689 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800690 ASSERT_STREQ(kh.GetDescriptor(), "Ljava/lang/Object;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700691 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
692 EXPECT_FALSE(JavaLangObject->HasSuperClass());
693 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700694 EXPECT_EQ(Class::kStatusResolved, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700695 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700696 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700697 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700698 EXPECT_FALSE(JavaLangObject->IsVerified());
699 EXPECT_FALSE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700700 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
701 EXPECT_FALSE(JavaLangObject->IsArrayClass());
702 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
703 EXPECT_FALSE(JavaLangObject->IsInterface());
704 EXPECT_TRUE(JavaLangObject->IsPublic());
705 EXPECT_FALSE(JavaLangObject->IsFinal());
706 EXPECT_FALSE(JavaLangObject->IsPrimitive());
707 EXPECT_FALSE(JavaLangObject->IsSynthetic());
708 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
709 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
710 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800711 FieldHelper fh(JavaLangObject->GetInstanceField(0));
712 EXPECT_STREQ(fh.GetName(), "shadow$_klass_");
713 fh.ChangeField(JavaLangObject->GetInstanceField(1));
714 EXPECT_STREQ(fh.GetName(), "shadow$_monitor_");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700715
716 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700717 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700718
Ian Rogers1f539342012-10-03 21:09:42 -0700719 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("MyClass")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700720 AssertNonExistentClass("LMyClass;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700721 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800722 kh.ChangeClass(MyClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700723 ASSERT_TRUE(MyClass != NULL);
724 ASSERT_TRUE(MyClass->GetClass() != NULL);
725 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
726 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800727 ASSERT_STREQ(kh.GetDescriptor(), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700728 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
729 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700730 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700731 EXPECT_EQ(Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700732 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700733 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700734 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700735 EXPECT_FALSE(MyClass->IsVerified());
736 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700737 EXPECT_FALSE(MyClass->IsArrayInstance());
738 EXPECT_FALSE(MyClass->IsArrayClass());
739 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
740 EXPECT_FALSE(MyClass->IsInterface());
741 EXPECT_FALSE(MyClass->IsPublic());
742 EXPECT_FALSE(MyClass->IsFinal());
743 EXPECT_FALSE(MyClass->IsPrimitive());
744 EXPECT_FALSE(MyClass->IsSynthetic());
745 EXPECT_EQ(1U, MyClass->NumDirectMethods());
746 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
747 EXPECT_EQ(0U, MyClass->NumInstanceFields());
748 EXPECT_EQ(0U, MyClass->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700749 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700750
751 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
752
753 // created by class_linker
754 AssertArrayClass("[C", "C", NULL);
755 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
756 // synthesized on the fly
757 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700758 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700759 // or not available at all
760 AssertNonExistentClass("[[[[LNonExistentClass;");
761}
762
763TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 ScopedObjectAccess soa(Thread::Current());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800765 AssertDexFile(java_lang_dex_file_, NULL);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700766}
767
Ian Rogersa15e67d2012-02-28 13:51:55 -0800768// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400769// start of the object
770TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(Thread::Current());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400772 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700773 ObjectArray<String>* array = ObjectArray<String>::Alloc(soa.Self(), array_class, 0);
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400774 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775 uint32_t data_offset =
Ian Rogersa15e67d2012-02-28 13:51:55 -0800776 array_offset + ObjectArray<String>::DataOffset(sizeof(String*)).Uint32Value();
777 if (sizeof(String*) == sizeof(int32_t)) {
778 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
779 } else {
780 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
781 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400782}
783
784TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785 ScopedObjectAccess soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700786 SirtRef<LongArray> long_array(soa.Self(), LongArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800787 EXPECT_EQ(class_linker_->FindSystemClass("[J"), long_array->GetClass());
788 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
789 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
790
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 SirtRef<DoubleArray> double_array(soa.Self(), DoubleArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800792 EXPECT_EQ(class_linker_->FindSystemClass("[D"), double_array->GetClass());
793 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
794 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
795
Ian Rogers50b35e22012-10-04 10:09:15 -0700796 SirtRef<IntArray> int_array(soa.Self(), IntArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800797 EXPECT_EQ(class_linker_->FindSystemClass("[I"), int_array->GetClass());
798 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
799 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
800
Ian Rogers50b35e22012-10-04 10:09:15 -0700801 SirtRef<CharArray> char_array(soa.Self(), CharArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800802 EXPECT_EQ(class_linker_->FindSystemClass("[C"), char_array->GetClass());
803 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
804 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
805
Ian Rogers50b35e22012-10-04 10:09:15 -0700806 SirtRef<ShortArray> short_array(soa.Self(), ShortArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800807 EXPECT_EQ(class_linker_->FindSystemClass("[S"), short_array->GetClass());
808 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
809 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
810
811 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400812}
813
Elliott Hughes33203b52011-09-20 19:42:01 -0700814TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
815 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
816 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700817 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes33203b52011-09-20 19:42:01 -0700818 Class* c;
819 c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800820 FieldHelper fh(c->GetIFields()->Get(0));
821 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700822 c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800823 fh.ChangeField(c->GetIFields()->Get(0));
824 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700825 c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800826 fh.ChangeField(c->GetIFields()->Get(0));
827 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700828 c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800829 fh.ChangeField(c->GetIFields()->Get(0));
830 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700831 c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800832 fh.ChangeField(c->GetIFields()->Get(0));
833 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700834 c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800835 fh.ChangeField(c->GetIFields()->Get(0));
836 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700837 c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800838 fh.ChangeField(c->GetIFields()->Get(0));
839 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700840 c = class_linker_->FindClass("Ljava/lang/Short;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800841 fh.ChangeField(c->GetIFields()->Get(0));
842 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700843}
844
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700845TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700847 SirtRef<ClassLoader> class_loader_1(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("MyClass")));
848 SirtRef<ClassLoader> class_loader_2(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("MyClass")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700849 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
850 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700851 EXPECT_TRUE(MyClass_1 != NULL);
852 EXPECT_TRUE(MyClass_2 != NULL);
853 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400854}
855
Jesse Wilson7833bd22011-08-09 18:31:44 -0400856TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700858 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("Statics")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700859 Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
Ian Rogers0045a292012-03-31 21:08:41 -0700860 class_linker_->EnsureInitialized(statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400861
jeffhaoabcfde32011-09-29 15:05:18 -0700862 // Static final primitives that are initialized by a compile-time constant
863 // expression resolve to a copy of a constant value from the constant pool.
864 // So <clinit> should be null.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700865 AbstractMethod* clinit = statics->FindDirectMethod("<clinit>", "()V");
jeffhaoabcfde32011-09-29 15:05:18 -0700866 EXPECT_TRUE(clinit == NULL);
867
868 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400869
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700870 Field* s0 = statics->FindStaticField("s0", "Z");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800871 FieldHelper fh(s0);
Elliott Hughes91250e02011-12-13 22:30:35 -0800872 EXPECT_STREQ(ClassHelper(s0->GetClass()).GetDescriptor(), "Ljava/lang/reflect/Field;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800873 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimBoolean);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700874 EXPECT_EQ(true, s0->GetBoolean(statics));
875 s0->SetBoolean(statics, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400876
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700877 Field* s1 = statics->FindStaticField("s1", "B");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800878 fh.ChangeField(s1);
879 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimByte);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700880 EXPECT_EQ(5, s1->GetByte(statics));
881 s1->SetByte(statics, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400882
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700883 Field* s2 = statics->FindStaticField("s2", "C");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800884 fh.ChangeField(s2);
885 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimChar);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700886 EXPECT_EQ('a', s2->GetChar(statics));
887 s2->SetChar(statics, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400888
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700889 Field* s3 = statics->FindStaticField("s3", "S");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800890 fh.ChangeField(s3);
891 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimShort);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700892 EXPECT_EQ(-536, s3->GetShort(statics));
893 s3->SetShort(statics, -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400894
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700895 Field* s4 = statics->FindStaticField("s4", "I");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 fh.ChangeField(s4);
897 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimInt);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700898 EXPECT_EQ(2000000000, s4->GetInt(statics));
899 s4->SetInt(statics, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400900
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700901 Field* s5 = statics->FindStaticField("s5", "J");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800902 fh.ChangeField(s5);
903 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimLong);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700904 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics));
905 s5->SetLong(statics, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400906
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700907 Field* s6 = statics->FindStaticField("s6", "F");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800908 fh.ChangeField(s6);
909 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimFloat);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700910 EXPECT_EQ(0.5, s6->GetFloat(statics));
911 s6->SetFloat(statics, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400912
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700913 Field* s7 = statics->FindStaticField("s7", "D");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 fh.ChangeField(s7);
915 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimDouble);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700916 EXPECT_EQ(16777217, s7->GetDouble(statics));
917 s7->SetDouble(statics, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400918
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700919 Field* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 fh.ChangeField(s8);
921 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimNot);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700922 EXPECT_TRUE(s8->GetObject(statics)->AsString()->Equals("android"));
923 s8->SetObject(s8->GetDeclaringClass(), String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400924
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800925 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
926 // http://code.google.com/p/googletest/issues/detail?id=322
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700927 EXPECT_FALSE( s0->GetBoolean(statics));
928 EXPECT_EQ(6, s1->GetByte(statics));
929 EXPECT_EQ('b', s2->GetChar(statics));
930 EXPECT_EQ(-535, s3->GetShort(statics));
931 EXPECT_EQ(2000000001, s4->GetInt(statics));
932 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(statics));
933 EXPECT_EQ(0.75, s6->GetFloat(statics));
934 EXPECT_EQ(16777219, s7->GetDouble(statics));
935 EXPECT_TRUE(s8->GetObject(statics)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400936}
937
Brian Carlstrom30b94452011-08-25 21:35:26 -0700938TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700940 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(LoadDex("Interfaces")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700941 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
942 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
943 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
944 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
945 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700946 EXPECT_TRUE(I->IsAssignableFrom(A));
947 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700948 EXPECT_TRUE(J->IsAssignableFrom(K));
949 EXPECT_TRUE(K->IsAssignableFrom(B));
950 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700951
Mathieu Chartier66f19252012-09-18 08:57:04 -0700952 AbstractMethod* Ii = I->FindVirtualMethod("i", "()V");
953 AbstractMethod* Jj1 = J->FindVirtualMethod("j1", "()V");
954 AbstractMethod* Jj2 = J->FindVirtualMethod("j2", "()V");
955 AbstractMethod* Kj1 = K->FindInterfaceMethod("j1", "()V");
956 AbstractMethod* Kj2 = K->FindInterfaceMethod("j2", "()V");
957 AbstractMethod* Kk = K->FindInterfaceMethod("k", "()V");
958 AbstractMethod* Ai = A->FindVirtualMethod("i", "()V");
959 AbstractMethod* Aj1 = A->FindVirtualMethod("j1", "()V");
960 AbstractMethod* Aj2 = A->FindVirtualMethod("j2", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700961 ASSERT_TRUE(Ii != NULL);
962 ASSERT_TRUE(Jj1 != NULL);
963 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700964 ASSERT_TRUE(Kj1 != NULL);
965 ASSERT_TRUE(Kj2 != NULL);
966 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700967 ASSERT_TRUE(Ai != NULL);
968 ASSERT_TRUE(Aj1 != NULL);
969 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700970 EXPECT_NE(Ii, Ai);
971 EXPECT_NE(Jj1, Aj1);
972 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700973 EXPECT_EQ(Kj1, Jj1);
974 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800975 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
976 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
977 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700978 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
979 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
980 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
Ian Rogersd24e2642012-06-06 21:21:43 -0700981
982 Field* Afoo = A->FindStaticField("foo", "Ljava/lang/String;");
983 Field* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;");
984 Field* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;");
985 Field* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;");
986 ASSERT_TRUE(Afoo != NULL);
987 EXPECT_EQ(Afoo, Bfoo);
988 EXPECT_EQ(Afoo, Jfoo);
989 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700990}
991
Elliott Hughesf3778f62012-01-26 14:14:35 -0800992TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -0700993 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700994
jeffhaoabcfde32011-09-29 15:05:18 -0700995 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
996 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700997
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(Thread::Current());
999 jobject jclass_loader = LoadDex("StaticsFromCode");
Ian Rogers1f539342012-10-03 21:09:42 -07001000 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001002 CHECK(dex_file != NULL);
1003
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001004 Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
Mathieu Chartier66f19252012-09-18 08:57:04 -07001005 AbstractMethod* clinit = klass->FindDirectMethod("<clinit>", "()V");
1006 AbstractMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001007 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
1008 ASSERT_TRUE(string_id != NULL);
1009 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
1010 ASSERT_TRUE(type_id != NULL);
1011 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001012 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001013 StaticStorageBase* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001014 EXPECT_TRUE(uninit != NULL);
1015 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001016 StaticStorageBase* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001017 EXPECT_TRUE(init != NULL);
1018 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001019}
1020
Elliott Hughes20cde902011-10-04 17:37:27 -07001021TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes20cde902011-10-04 17:37:27 -07001023 Class* c;
1024
1025 // Object has a finalize method, but we know it's empty.
1026 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
1027 EXPECT_FALSE(c->IsFinalizable());
1028
1029 // Enum has a finalize method to prevent its subclasses from implementing one.
1030 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
1031 EXPECT_FALSE(c->IsFinalizable());
1032
1033 // RoundingMode is an enum.
1034 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
1035 EXPECT_FALSE(c->IsFinalizable());
1036
1037 // RandomAccessFile extends Object and overrides finalize.
1038 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
1039 EXPECT_TRUE(c->IsFinalizable());
1040
1041 // FileInputStream is finalizable and extends InputStream which isn't.
1042 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
1043 EXPECT_FALSE(c->IsFinalizable());
1044 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
1045 EXPECT_TRUE(c->IsFinalizable());
1046
1047 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1048 // extends ThreadPoolExecutor which does.
1049 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
1050 EXPECT_TRUE(c->IsFinalizable());
1051 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
1052 EXPECT_TRUE(c->IsFinalizable());
1053}
1054
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001055TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(Thread::Current());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001057 ClassHelper kh;
1058 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
1059 Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
1060 kh.ChangeClass(klass);
1061 EXPECT_TRUE(kh.GetDescriptor() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -08001062 EXPECT_STREQ(kh.GetDescriptor(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001063 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1064 }
1065}
1066
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001067} // namespace art