blob: 1eb5e0d1c4603910574373cfead5944f88daaaf0 [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:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 void AssertNonExistentClass(const std::string& descriptor)
33 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080034 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor.c_str()) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070035 Thread* self = Thread::Current();
36 EXPECT_TRUE(self->IsExceptionPending());
37 Object* exception = self->GetException();
38 self->ClearException();
39 Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
40 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041 }
42
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 void AssertPrimitiveClass(const std::string& descriptor)
44 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080045 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070046 }
47
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 void AssertPrimitiveClass(const std::string& descriptor, const Class* primitive)
49 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080050 ClassHelper primitive_ch(primitive);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070051 ASSERT_TRUE(primitive != NULL);
52 ASSERT_TRUE(primitive->GetClass() != NULL);
53 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
54 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -080055 ASSERT_STREQ(descriptor.c_str(), primitive_ch.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070056 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
57 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070058 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -070059 EXPECT_EQ(Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070060 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070061 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070062 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070063 EXPECT_TRUE(primitive->IsVerified());
64 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070065 EXPECT_FALSE(primitive->IsArrayInstance());
66 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070067 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070068 EXPECT_FALSE(primitive->IsInterface());
69 EXPECT_TRUE(primitive->IsPublic());
70 EXPECT_TRUE(primitive->IsFinal());
71 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070072 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070073 EXPECT_EQ(0U, primitive->NumDirectMethods());
74 EXPECT_EQ(0U, primitive->NumVirtualMethods());
75 EXPECT_EQ(0U, primitive->NumInstanceFields());
76 EXPECT_EQ(0U, primitive->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -070077 EXPECT_EQ(0U, primitive_ch.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070078 EXPECT_TRUE(primitive->GetVTable() == NULL);
79 EXPECT_EQ(0, primitive->GetIfTableCount());
80 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081 }
82
Brian Carlstromaded5f72011-10-07 17:15:04 -070083 void AssertArrayClass(const std::string& array_descriptor,
84 const std::string& component_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 ClassLoader* class_loader)
86 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080087 Class* array = class_linker_->FindClass(array_descriptor.c_str(), class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 ClassHelper array_component_ch(array->GetComponentType());
Elliott Hughes91250e02011-12-13 22:30:35 -080089 EXPECT_STREQ(component_type.c_str(), array_component_ch.GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070090 EXPECT_EQ(class_loader, array->GetClassLoader());
91 AssertArrayClass(array_descriptor, array);
92 }
93
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 void AssertArrayClass(const std::string& array_descriptor, Class* array)
95 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080096 ClassHelper kh(array);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070097 ASSERT_TRUE(array != NULL);
98 ASSERT_TRUE(array->GetClass() != NULL);
99 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
100 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800101 ASSERT_STREQ(array_descriptor.c_str(), kh.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700102 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700103 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104 EXPECT_TRUE(array->HasSuperClass());
105 ASSERT_TRUE(array->GetComponentType() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 kh.ChangeClass(array->GetComponentType());
107 ASSERT_TRUE(kh.GetDescriptor() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700108 EXPECT_EQ(Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700109 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700110 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700111 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700112 EXPECT_TRUE(array->IsVerified());
113 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700114 EXPECT_FALSE(array->IsArrayInstance());
115 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700116 EXPECT_FALSE(array->IsInterface());
117 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
118 EXPECT_TRUE(array->IsFinal());
119 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700120 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700121 EXPECT_EQ(0U, array->NumDirectMethods());
122 EXPECT_EQ(0U, array->NumVirtualMethods());
123 EXPECT_EQ(0U, array->NumInstanceFields());
124 EXPECT_EQ(0U, array->NumStaticFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125 kh.ChangeClass(array);
Ian Rogersd24e2642012-06-06 21:21:43 -0700126 EXPECT_EQ(2U, kh.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -0700127 EXPECT_TRUE(array->GetVTable() != NULL);
128 EXPECT_EQ(2, array->GetIfTableCount());
129 ObjectArray<InterfaceEntry>* iftable = array->GetIfTable();
130 ASSERT_TRUE(iftable != NULL);
Ian Rogersd24e2642012-06-06 21:21:43 -0700131 kh.ChangeClass(kh.GetDirectInterface(0));
Elliott Hughes91250e02011-12-13 22:30:35 -0800132 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 kh.ChangeClass(array);
Ian Rogersd24e2642012-06-06 21:21:43 -0700134 kh.ChangeClass(kh.GetDirectInterface(1));
Elliott Hughes91250e02011-12-13 22:30:35 -0800135 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/io/Serializable;");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700136 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700137
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 void AssertMethod(Method* method) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800139 MethodHelper mh(method);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700140 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700141 EXPECT_TRUE(method->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800142 EXPECT_TRUE(mh.GetName() != NULL);
143 EXPECT_TRUE(mh.GetSignature() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700144
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700145 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800146 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700147 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 EXPECT_TRUE(method->GetDexCacheInitializedStaticStorage() != NULL);
149 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
150 method->GetDexCacheStrings());
Ian Rogers19846512012-02-24 11:42:47 -0800151 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
152 method->GetDexCacheResolvedMethods());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700153 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
154 method->GetDexCacheResolvedTypes());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetInitializedStaticStorage(),
156 method->GetDexCacheInitializedStaticStorage());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700157 }
158
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 void AssertField(Class* klass, Field* field)
160 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800161 FieldHelper fh(field);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700162 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700163 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700164 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 EXPECT_TRUE(fh.GetName() != NULL);
166 EXPECT_TRUE(fh.GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700167 }
168
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 void AssertClass(const std::string& descriptor, Class* klass)
170 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800171 ClassHelper kh(klass);
Elliott Hughes91250e02011-12-13 22:30:35 -0800172 EXPECT_STREQ(descriptor.c_str(), kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800173 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700174 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700175 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700176 EXPECT_TRUE(klass->HasSuperClass());
177 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700178 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700179 EXPECT_TRUE(klass->GetClass() != NULL);
180 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700181 EXPECT_TRUE(klass->GetDexCache() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700182 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700183 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700184 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700186 EXPECT_TRUE(klass->GetComponentType() == NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700187 EXPECT_TRUE(klass->IsInSamePackage(klass));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800188 EXPECT_TRUE(Class::IsInSamePackage(kh.GetDescriptor(), kh.GetDescriptor()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700189 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700190 EXPECT_TRUE(klass->IsAbstract());
191 if (klass->NumDirectMethods() == 1) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 MethodHelper mh(klass->GetDirectMethod(0));
193 EXPECT_TRUE(mh.IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700194 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700195 } else {
196 EXPECT_EQ(0U, klass->NumDirectMethods());
197 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700198 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700199 if (!klass->IsSynthetic()) {
200 EXPECT_NE(0U, klass->NumDirectMethods());
201 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700202 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700203 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
204 for (int i = 0; i < klass->GetIfTableCount(); i++) {
205 const InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i);
206 ASSERT_TRUE(interface_entry != NULL);
207 Class* interface = interface_entry->GetInterface();
208 ASSERT_TRUE(interface != NULL);
209 EXPECT_TRUE(interface_entry->GetInterface() != NULL);
210 if (klass->IsInterface()) {
211 EXPECT_EQ(0U, interface_entry->GetMethodArrayCount());
212 } else {
213 CHECK_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
214 EXPECT_EQ(interface->NumVirtualMethods(), interface_entry->GetMethodArrayCount());
215 }
216 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700217 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700219 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700220 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700221 }
222 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700223 EXPECT_FALSE(klass->IsAbstract());
224 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700225 }
226 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700227 EXPECT_FALSE(klass->IsFinal());
228 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700229 }
230
231 EXPECT_FALSE(klass->IsPrimitive());
232 EXPECT_TRUE(klass->CanAccess(klass));
233
234 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 Method* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700236 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700237 EXPECT_TRUE(method->IsDirect());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400238 EXPECT_EQ(klass, method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700239 }
240
241 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700242 Method* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700243 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700244 EXPECT_FALSE(method->IsDirect());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700245 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700246 }
247
248 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400249 Field* field = klass->GetInstanceField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700250 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700251 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700252 }
253
254 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400255 Field* field = klass->GetStaticField(i);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700256 AssertField(klass, field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700257 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700258 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700259
260 // Confirm that all instances fields are packed together at the start
261 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 FieldHelper fh;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700263 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400264 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 fh.ChangeField(field);
266 ASSERT_TRUE(!fh.IsPrimitiveType());
267 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700270 }
271 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400272 Field* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 fh.ChangeField(field);
274 Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700275 ASSERT_TRUE(field_type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 if (!fh.IsPrimitiveType() || !field_type->IsPrimitive()) {
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700277 // While Reference.referent is not primitive, the ClassLinker
278 // treats it as such so that the garbage collector won't scan it.
279 EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
280 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700281 }
282
283 size_t total_num_reference_instance_fields = 0;
284 Class* k = klass;
285 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700286 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
287 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700288 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700289 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0,
Brian Carlstromae3ac012011-07-27 01:30:28 -0700290 total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700291 }
292
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 void AssertDexFileClass(ClassLoader* class_loader, const std::string& descriptor)
294 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700295 ASSERT_TRUE(descriptor != NULL);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800296 Class* klass = class_linker_->FindSystemClass(descriptor.c_str());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700297 ASSERT_TRUE(klass != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800298 EXPECT_STREQ(descriptor.c_str(), ClassHelper(klass).GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700299 EXPECT_EQ(class_loader, klass->GetClassLoader());
300 if (klass->IsPrimitive()) {
301 AssertPrimitiveClass(descriptor, klass);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700302 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700303 AssertArrayClass(descriptor, klass);
304 } else {
305 AssertClass(descriptor, klass);
306 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700307 }
308
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 void AssertDexFile(const DexFile* dex, ClassLoader* class_loader)
310 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700312
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700313 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700314 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700315 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700316 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700317 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700318 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700319 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700320 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
321 const DexFile::TypeId& type_id = dex->GetTypeId(i);
322 const char* descriptor = dex->GetTypeDescriptor(type_id);
323 AssertDexFileClass(class_loader, descriptor);
324 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700325 class_linker_->VisitRoots(TestRootVisitor, NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800326 // Verify the dex cache has resolution methods in all resolved method slots
327 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
328 ObjectArray<Method>* resolved_methods = dex_cache->GetResolvedMethods();
329 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
330 EXPECT_TRUE(resolved_methods->Get(i) != NULL);
331 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700332 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700333
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700334 static void TestRootVisitor(const Object* root, void*) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700335 EXPECT_TRUE(root != NULL);
336 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700337};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700338
Brian Carlstrom693267a2011-09-06 09:25:34 -0700339struct CheckOffset {
340 size_t cpp_offset;
341 const char* java_name;
342 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
343};
344
Elliott Hughes80609252011-09-23 17:24:51 -0700345template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700346struct CheckOffsets {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700347 CheckOffsets(bool is_static, const char* class_descriptor)
348 : is_static(is_static), class_descriptor(class_descriptor) {}
349 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700350 std::string class_descriptor;
351 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700352
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 bool Check() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800354 Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor.c_str());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700355 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700356
357 bool error = false;
358
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 if (!klass->IsClassClass() && !is_static) {
360 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700361 if (sizeof(T) != expected_size) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700362 LG << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700363 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700364 << " Java=" << expected_size
365 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700366 error = true;
367 }
368 }
369
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700370 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700371 if (offsets.size() != num_fields) {
372 LG << "Field count mismatch:"
373 << " class=" << class_descriptor
374 << " Java=" << num_fields
375 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700376 error = true;
377 }
378
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800379 FieldHelper fh;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700380 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700381 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 fh.ChangeField(field);
383 StringPiece field_name(fh.GetName());
384 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700385 error = true;
386 }
387 }
388 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700389 for (size_t i = 0; i < offsets.size(); i++) {
390 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700391 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800392 fh.ChangeField(field);
393 StringPiece field_name(fh.GetName());
394 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700395 LG << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
396 }
397 LG << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700398 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800399 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700400 << " CheckOffsets=" << offset.java_name;
401 }
402 }
403
Brian Carlstromdbc05252011-09-09 01:59:59 -0700404 for (size_t i = 0; i < offsets.size(); i++) {
405 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700406 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700407 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
408 error = true;
409 }
410 }
411 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700412 for (size_t i = 0; i < offsets.size(); i++) {
413 CheckOffset& offset = offsets[i];
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700414 Field* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700415 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
416 LG << "OFFSET MISMATCH NEXT LINE:";
417 }
Brian Carlstromdbc05252011-09-09 01:59:59 -0700418 LG << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700419 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
420 }
421 }
422
423 return !error;
424 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425
426 private:
427 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700428};
429
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700430// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
431// is first since if it is failing, others are unlikely to succeed.
432
Elliott Hughes80609252011-09-23 17:24:51 -0700433struct ObjectOffsets : public CheckOffsets<Object> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700434 ObjectOffsets() : CheckOffsets<Object>(false, "Ljava/lang/Object;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700435 // alphabetical references
436 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, klass_), "shadow$_klass_"));
437
438 // alphabetical 32-bit
439 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700440 };
441};
442
Elliott Hughes80609252011-09-23 17:24:51 -0700443struct FieldOffsets : public CheckOffsets<Field> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700444 FieldOffsets() : CheckOffsets<Field>(false, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700445 // alphabetical references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, declaring_class_), "declaringClass"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700447
448 // alphabetical 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800449 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, access_flags_), "accessFlags"));
450 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, field_dex_idx_), "fieldDexIndex"));
451 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Field, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700452 };
453};
454
Elliott Hughes80609252011-09-23 17:24:51 -0700455struct MethodOffsets : public CheckOffsets<Method> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700456 MethodOffsets() : CheckOffsets<Method>(false, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700457 // alphabetical references
458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, declaring_class_), "declaringClass"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_initialized_static_storage_), "dexCacheInitializedStaticStorage"));
Ian Rogers19846512012-02-24 11:42:47 -0800460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800461 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
462 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, dex_cache_strings_), "dexCacheStrings"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700463
464 // alphabetical 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800465 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, access_flags_), "accessFlags"));
466 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_), "code"));
467 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, code_item_offset_), "codeItemOffset"));
468 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, core_spill_mask_), "coreSpillMask"));
469 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, fp_spill_mask_), "fpSpillMask"));
470 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, frame_size_in_bytes_), "frameSizeInBytes"));
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800471 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, gc_map_), "gcMap"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800472 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, invoke_stub_), "invokeStub"));
473 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, mapping_table_), "mappingTable"));
474 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_dex_index_), "methodDexIndex"));
475 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, method_index_), "methodIndex"));
476 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, native_method_), "nativeMethod"));
477 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Method, vmap_table_), "vmapTable"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700478 };
479};
480
Elliott Hughes80609252011-09-23 17:24:51 -0700481struct ConstructorOffsets : public MethodOffsets {
482 ConstructorOffsets() : MethodOffsets() {
483 // We use Method* for both java.lang.reflect.Constructor and java.lang.reflect.Method.
484 class_descriptor = "Ljava/lang/reflect/Constructor;";
485 }
486};
487
488struct ClassOffsets : public CheckOffsets<Class> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700489 ClassOffsets() : CheckOffsets<Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700490 // alphabetical references
Ian Rogersd418eda2012-01-30 12:14:28 -0800491 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_loader_), "classLoader"));
492 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, component_type_), "componentType"));
493 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_cache_), "dexCache"));
494 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, direct_methods_), "directMethods"));
495 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, ifields_), "iFields"));
496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, iftable_), "ifTable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, name_), "name"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, sfields_), "sFields"));
499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, super_class_), "superClass"));
500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, verify_error_class_), "verifyErrorClass"));
501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, virtual_methods_), "virtualMethods"));
502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, vtable_), "vtable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700503
504 // alphabetical 32-bit
Ian Rogersd418eda2012-01-30 12:14:28 -0800505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, access_flags_), "accessFlags"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, class_size_), "classSize"));
507 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, clinit_thread_id_), "clinitThreadId"));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, dex_type_idx_), "dexTypeIndex"));
Ian Rogersd418eda2012-01-30 12:14:28 -0800509 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
510 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, num_reference_static_fields_), "numReferenceStaticFields"));
511 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, object_size_), "objectSize"));
512 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, primitive_type_), "primitiveType"));
513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_instance_offsets_), "referenceInstanceOffsets"));
514 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, reference_static_offsets_), "referenceStaticOffsets"));
515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Class, status_), "status"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700516 };
517};
518
Elliott Hughes80609252011-09-23 17:24:51 -0700519struct StringOffsets : public CheckOffsets<String> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520 StringOffsets() : CheckOffsets<String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700521 // alphabetical references
522 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, array_), "value"));
523
524 // alphabetical 32-bit
525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, count_), "count"));
526 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, hash_code_), "hashCode"));
527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700528 };
529};
530
Elliott Hughes80609252011-09-23 17:24:51 -0700531struct ThrowableOffsets : public CheckOffsets<Throwable> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700532 ThrowableOffsets() : CheckOffsets<Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700533 // alphabetical references
534 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, cause_), "cause"));
535 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, detail_message_), "detailMessage"));
536 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_state_), "stackState"));
537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, stack_trace_), "stackTrace"));
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700539 };
540};
541
Elliott Hughes80609252011-09-23 17:24:51 -0700542struct StackTraceElementOffsets : public CheckOffsets<StackTraceElement> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700543 StackTraceElementOffsets() : CheckOffsets<StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700544 // alphabetical references
545 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, declaring_class_), "declaringClass"));
546 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, file_name_), "fileName"));
547 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, method_name_), "methodName"));
548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 };
550};
551
Elliott Hughes80609252011-09-23 17:24:51 -0700552struct ClassLoaderOffsets : public CheckOffsets<ClassLoader> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553 ClassLoaderOffsets() : CheckOffsets<ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700554 // alphabetical references
Brian Carlstrom87293d02012-04-01 19:53:04 -0700555 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, packages_), "packages"));
556 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, parent_), "parent"));
557 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558 };
559};
560
Jesse Wilson95caa792011-10-12 18:14:17 -0400561struct ProxyOffsets : public CheckOffsets<Proxy> {
562 ProxyOffsets() : CheckOffsets<Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400563 // alphabetical references
564 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(Proxy, h_), "h"));
565 };
566};
567
Elliott Hughes80609252011-09-23 17:24:51 -0700568struct ClassClassOffsets : public CheckOffsets<ClassClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700569 ClassClassOffsets() : CheckOffsets<ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700570 // padding 32-bit
571 CHECK_EQ(OFFSETOF_MEMBER(ClassClass, padding_) + 4,
572 OFFSETOF_MEMBER(ClassClass, serialVersionUID_));
573
Brian Carlstromdbc05252011-09-09 01:59:59 -0700574 // alphabetical 64-bit
575 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(ClassClass, serialVersionUID_), "serialVersionUID"));
576 };
577};
578
Elliott Hughes80609252011-09-23 17:24:51 -0700579struct StringClassOffsets : public CheckOffsets<StringClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700580 StringClassOffsets() : CheckOffsets<StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700581 // alphabetical references
582 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, ASCII_), "ASCII"));
583 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
584
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700585 // padding 32-bit
586 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
587
Brian Carlstromdbc05252011-09-09 01:59:59 -0700588 // alphabetical 64-bit
589 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(StringClass, serialVersionUID_), "serialVersionUID"));
590 };
591};
592
Elliott Hughes80609252011-09-23 17:24:51 -0700593struct FieldClassOffsets : public CheckOffsets<FieldClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700594 FieldClassOffsets() : CheckOffsets<FieldClass>(true, "Ljava/lang/reflect/Field;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700595 // alphabetical references
596 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 -0700597 };
598};
599
Elliott Hughes80609252011-09-23 17:24:51 -0700600struct MethodClassOffsets : public CheckOffsets<MethodClass> {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 MethodClassOffsets() : CheckOffsets<MethodClass>(true, "Ljava/lang/reflect/Method;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700602 // alphabetical references
Brian Carlstromdbc05252011-09-09 01:59:59 -0700603 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700604 };
605};
606
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400607// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400608// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700609// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400610TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700612 EXPECT_TRUE(ObjectOffsets().Check());
Elliott Hughes80609252011-09-23 17:24:51 -0700613 EXPECT_TRUE(ConstructorOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700614 EXPECT_TRUE(FieldOffsets().Check());
615 EXPECT_TRUE(MethodOffsets().Check());
616 EXPECT_TRUE(ClassOffsets().Check());
617 EXPECT_TRUE(StringOffsets().Check());
618 EXPECT_TRUE(ThrowableOffsets().Check());
619 EXPECT_TRUE(StackTraceElementOffsets().Check());
620 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400621 EXPECT_TRUE(ProxyOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700622
623 EXPECT_TRUE(ClassClassOffsets().Check());
624 EXPECT_TRUE(StringClassOffsets().Check());
625 EXPECT_TRUE(FieldClassOffsets().Check());
626 EXPECT_TRUE(MethodClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700627}
628
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700629TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700631 AssertNonExistentClass("NoSuchClass;");
632 AssertNonExistentClass("LNoSuchClass;");
633}
634
635TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(Thread::Current());
637 SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(LoadDex("Nested")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700638
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700639 Class* outer = class_linker_->FindClass("LNested;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700640 ASSERT_TRUE(outer != NULL);
641 EXPECT_EQ(0U, outer->NumVirtualMethods());
642 EXPECT_EQ(1U, outer->NumDirectMethods());
643
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700644 Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700645 ASSERT_TRUE(inner != NULL);
646 EXPECT_EQ(0U, inner->NumVirtualMethods());
647 EXPECT_EQ(1U, inner->NumDirectMethods());
648}
649
650TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700652 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800653 for (int ch = 1; ch < 256; ++ch) {
654 std::string descriptor;
655 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700656 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700657 AssertNonExistentClass(descriptor);
658 } else {
659 AssertPrimitiveClass(descriptor);
660 }
661 }
662}
663
664TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700666 Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800667 ClassHelper kh(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700668 ASSERT_TRUE(JavaLangObject != NULL);
669 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
670 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
671 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800672 ASSERT_STREQ(kh.GetDescriptor(), "Ljava/lang/Object;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700673 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
674 EXPECT_FALSE(JavaLangObject->HasSuperClass());
675 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700676 EXPECT_EQ(Class::kStatusResolved, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700677 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700678 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700679 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700680 EXPECT_FALSE(JavaLangObject->IsVerified());
681 EXPECT_FALSE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700682 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
683 EXPECT_FALSE(JavaLangObject->IsArrayClass());
684 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
685 EXPECT_FALSE(JavaLangObject->IsInterface());
686 EXPECT_TRUE(JavaLangObject->IsPublic());
687 EXPECT_FALSE(JavaLangObject->IsFinal());
688 EXPECT_FALSE(JavaLangObject->IsPrimitive());
689 EXPECT_FALSE(JavaLangObject->IsSynthetic());
690 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
691 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
692 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800693 FieldHelper fh(JavaLangObject->GetInstanceField(0));
694 EXPECT_STREQ(fh.GetName(), "shadow$_klass_");
695 fh.ChangeField(JavaLangObject->GetInstanceField(1));
696 EXPECT_STREQ(fh.GetName(), "shadow$_monitor_");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700697
698 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700699 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700700
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(LoadDex("MyClass")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700702 AssertNonExistentClass("LMyClass;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700703 Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800704 kh.ChangeClass(MyClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700705 ASSERT_TRUE(MyClass != NULL);
706 ASSERT_TRUE(MyClass->GetClass() != NULL);
707 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
708 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800709 ASSERT_STREQ(kh.GetDescriptor(), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700710 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
711 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700712 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700713 EXPECT_EQ(Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700714 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700715 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700716 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700717 EXPECT_FALSE(MyClass->IsVerified());
718 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700719 EXPECT_FALSE(MyClass->IsArrayInstance());
720 EXPECT_FALSE(MyClass->IsArrayClass());
721 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
722 EXPECT_FALSE(MyClass->IsInterface());
723 EXPECT_FALSE(MyClass->IsPublic());
724 EXPECT_FALSE(MyClass->IsFinal());
725 EXPECT_FALSE(MyClass->IsPrimitive());
726 EXPECT_FALSE(MyClass->IsSynthetic());
727 EXPECT_EQ(1U, MyClass->NumDirectMethods());
728 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
729 EXPECT_EQ(0U, MyClass->NumInstanceFields());
730 EXPECT_EQ(0U, MyClass->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700731 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700732
733 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
734
735 // created by class_linker
736 AssertArrayClass("[C", "C", NULL);
737 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
738 // synthesized on the fly
739 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700740 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700741 // or not available at all
742 AssertNonExistentClass("[[[[LNonExistentClass;");
743}
744
745TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 ScopedObjectAccess soa(Thread::Current());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800747 AssertDexFile(java_lang_dex_file_, NULL);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700748}
749
Ian Rogersa15e67d2012-02-28 13:51:55 -0800750// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400751// start of the object
752TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(Thread::Current());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400754 Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
755 ObjectArray<String>* array = ObjectArray<String>::Alloc(array_class, 0);
756 uint32_t array_offset = reinterpret_cast<uint32_t>(array);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700757 uint32_t data_offset =
Ian Rogersa15e67d2012-02-28 13:51:55 -0800758 array_offset + ObjectArray<String>::DataOffset(sizeof(String*)).Uint32Value();
759 if (sizeof(String*) == sizeof(int32_t)) {
760 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
761 } else {
762 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
763 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400764}
765
766TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 ScopedObjectAccess soa(Thread::Current());
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.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700799 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes33203b52011-09-20 19:42:01 -0700800 Class* c;
801 c = class_linker_->FindClass("Ljava/lang/Boolean;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800802 FieldHelper fh(c->GetIFields()->Get(0));
803 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700804 c = class_linker_->FindClass("Ljava/lang/Byte;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800805 fh.ChangeField(c->GetIFields()->Get(0));
806 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700807 c = class_linker_->FindClass("Ljava/lang/Character;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800808 fh.ChangeField(c->GetIFields()->Get(0));
809 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700810 c = class_linker_->FindClass("Ljava/lang/Double;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800811 fh.ChangeField(c->GetIFields()->Get(0));
812 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700813 c = class_linker_->FindClass("Ljava/lang/Float;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800814 fh.ChangeField(c->GetIFields()->Get(0));
815 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700816 c = class_linker_->FindClass("Ljava/lang/Integer;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800817 fh.ChangeField(c->GetIFields()->Get(0));
818 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700819 c = class_linker_->FindClass("Ljava/lang/Long;", NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800820 fh.ChangeField(c->GetIFields()->Get(0));
821 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700822 c = class_linker_->FindClass("Ljava/lang/Short;", 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}
826
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700827TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(Thread::Current());
829 SirtRef<ClassLoader> class_loader_1(soa.Decode<ClassLoader*>(LoadDex("MyClass")));
830 SirtRef<ClassLoader> class_loader_2(soa.Decode<ClassLoader*>(LoadDex("MyClass")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700831 Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1.get());
832 Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2.get());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700833 EXPECT_TRUE(MyClass_1 != NULL);
834 EXPECT_TRUE(MyClass_2 != NULL);
835 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400836}
837
Jesse Wilson7833bd22011-08-09 18:31:44 -0400838TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 ScopedObjectAccess soa(Thread::Current());
840 SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(LoadDex("Statics")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700841 Class* statics = class_linker_->FindClass("LStatics;", class_loader.get());
Ian Rogers0045a292012-03-31 21:08:41 -0700842 class_linker_->EnsureInitialized(statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400843
jeffhaoabcfde32011-09-29 15:05:18 -0700844 // Static final primitives that are initialized by a compile-time constant
845 // expression resolve to a copy of a constant value from the constant pool.
846 // So <clinit> should be null.
847 Method* clinit = statics->FindDirectMethod("<clinit>", "()V");
848 EXPECT_TRUE(clinit == NULL);
849
850 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400851
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700852 Field* s0 = statics->FindStaticField("s0", "Z");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800853 FieldHelper fh(s0);
Elliott Hughes91250e02011-12-13 22:30:35 -0800854 EXPECT_STREQ(ClassHelper(s0->GetClass()).GetDescriptor(), "Ljava/lang/reflect/Field;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800855 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimBoolean);
jeffhaoabcfde32011-09-29 15:05:18 -0700856 EXPECT_EQ(true, s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700857 s0->SetBoolean(NULL, false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400858
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700859 Field* s1 = statics->FindStaticField("s1", "B");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800860 fh.ChangeField(s1);
861 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimByte);
jeffhaoabcfde32011-09-29 15:05:18 -0700862 EXPECT_EQ(5, s1->GetByte(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700863 s1->SetByte(NULL, 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400864
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700865 Field* s2 = statics->FindStaticField("s2", "C");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800866 fh.ChangeField(s2);
867 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimChar);
jeffhaoabcfde32011-09-29 15:05:18 -0700868 EXPECT_EQ('a', s2->GetChar(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700869 s2->SetChar(NULL, 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400870
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700871 Field* s3 = statics->FindStaticField("s3", "S");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800872 fh.ChangeField(s3);
873 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimShort);
Ian Rogers466bb252011-10-14 03:29:56 -0700874 EXPECT_EQ(-536, s3->GetShort(NULL));
875 s3->SetShort(NULL, -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400876
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700877 Field* s4 = statics->FindStaticField("s4", "I");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800878 fh.ChangeField(s4);
879 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimInt);
jeffhaoabcfde32011-09-29 15:05:18 -0700880 EXPECT_EQ(2000000000, s4->GetInt(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700881 s4->SetInt(NULL, 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400882
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700883 Field* s5 = statics->FindStaticField("s5", "J");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800884 fh.ChangeField(s5);
885 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimLong);
jeffhaoabcfde32011-09-29 15:05:18 -0700886 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700887 s5->SetLong(NULL, 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400888
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700889 Field* s6 = statics->FindStaticField("s6", "F");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800890 fh.ChangeField(s6);
891 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimFloat);
jeffhaoabcfde32011-09-29 15:05:18 -0700892 EXPECT_EQ(0.5, s6->GetFloat(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700893 s6->SetFloat(NULL, 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400894
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700895 Field* s7 = statics->FindStaticField("s7", "D");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 fh.ChangeField(s7);
897 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimDouble);
jeffhaoabcfde32011-09-29 15:05:18 -0700898 EXPECT_EQ(16777217, s7->GetDouble(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700899 s7->SetDouble(NULL, 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400900
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700901 Field* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800902 fh.ChangeField(s8);
903 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimNot);
jeffhaoabcfde32011-09-29 15:05:18 -0700904 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("android"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700905 s8->SetObject(NULL, String::AllocFromModifiedUtf8("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400906
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800907 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
908 // http://code.google.com/p/googletest/issues/detail?id=322
909 EXPECT_FALSE( s0->GetBoolean(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700910 EXPECT_EQ(6, s1->GetByte(NULL));
911 EXPECT_EQ('b', s2->GetChar(NULL));
Ian Rogers466bb252011-10-14 03:29:56 -0700912 EXPECT_EQ(-535, s3->GetShort(NULL));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700913 EXPECT_EQ(2000000001, s4->GetInt(NULL));
914 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(NULL));
915 EXPECT_EQ(0.75, s6->GetFloat(NULL));
916 EXPECT_EQ(16777219, s7->GetDouble(NULL));
917 EXPECT_TRUE(s8->GetObject(NULL)->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400918}
919
Brian Carlstrom30b94452011-08-25 21:35:26 -0700920TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 ScopedObjectAccess soa(Thread::Current());
922 SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(LoadDex("Interfaces")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700923 Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader.get());
924 Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader.get());
925 Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader.get());
926 Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader.get());
927 Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader.get());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700928 EXPECT_TRUE(I->IsAssignableFrom(A));
929 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700930 EXPECT_TRUE(J->IsAssignableFrom(K));
931 EXPECT_TRUE(K->IsAssignableFrom(B));
932 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700933
934 Method* Ii = I->FindVirtualMethod("i", "()V");
935 Method* Jj1 = J->FindVirtualMethod("j1", "()V");
936 Method* Jj2 = J->FindVirtualMethod("j2", "()V");
jeffhao5dbddee2011-09-07 16:38:26 -0700937 Method* Kj1 = K->FindInterfaceMethod("j1", "()V");
938 Method* Kj2 = K->FindInterfaceMethod("j2", "()V");
939 Method* Kk = K->FindInterfaceMethod("k", "()V");
Brian Carlstrom30b94452011-08-25 21:35:26 -0700940 Method* Ai = A->FindVirtualMethod("i", "()V");
941 Method* Aj1 = A->FindVirtualMethod("j1", "()V");
942 Method* Aj2 = A->FindVirtualMethod("j2", "()V");
943 ASSERT_TRUE(Ii != NULL);
944 ASSERT_TRUE(Jj1 != NULL);
945 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700946 ASSERT_TRUE(Kj1 != NULL);
947 ASSERT_TRUE(Kj2 != NULL);
948 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700949 ASSERT_TRUE(Ai != NULL);
950 ASSERT_TRUE(Aj1 != NULL);
951 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 EXPECT_NE(Ii, Ai);
953 EXPECT_NE(Jj1, Aj1);
954 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700955 EXPECT_EQ(Kj1, Jj1);
956 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800957 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
958 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
959 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700960 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
961 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
962 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
Ian Rogersd24e2642012-06-06 21:21:43 -0700963
964 Field* Afoo = A->FindStaticField("foo", "Ljava/lang/String;");
965 Field* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;");
966 Field* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;");
967 Field* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;");
968 ASSERT_TRUE(Afoo != NULL);
969 EXPECT_EQ(Afoo, Bfoo);
970 EXPECT_EQ(Afoo, Jfoo);
971 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700972}
973
Elliott Hughesf3778f62012-01-26 14:14:35 -0800974TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -0700975 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700976
jeffhaoabcfde32011-09-29 15:05:18 -0700977 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
978 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700979
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(Thread::Current());
981 jobject jclass_loader = LoadDex("StaticsFromCode");
982 SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(jclass_loader));
983 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700984 CHECK(dex_file != NULL);
985
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700986 Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader.get());
jeffhaoabcfde32011-09-29 15:05:18 -0700987 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
988 Method* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800989 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
990 ASSERT_TRUE(string_id != NULL);
991 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
992 ASSERT_TRUE(type_id != NULL);
993 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700994 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800995 StaticStorageBase* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700996 EXPECT_TRUE(uninit != NULL);
997 EXPECT_TRUE(clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx) == NULL);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800998 StaticStorageBase* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700999 EXPECT_TRUE(init != NULL);
1000 EXPECT_EQ(init, clinit->GetDexCacheInitializedStaticStorage()->Get(type_idx));
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001001}
1002
Elliott Hughes20cde902011-10-04 17:37:27 -07001003TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes20cde902011-10-04 17:37:27 -07001005 Class* c;
1006
1007 // Object has a finalize method, but we know it's empty.
1008 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
1009 EXPECT_FALSE(c->IsFinalizable());
1010
1011 // Enum has a finalize method to prevent its subclasses from implementing one.
1012 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
1013 EXPECT_FALSE(c->IsFinalizable());
1014
1015 // RoundingMode is an enum.
1016 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
1017 EXPECT_FALSE(c->IsFinalizable());
1018
1019 // RandomAccessFile extends Object and overrides finalize.
1020 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
1021 EXPECT_TRUE(c->IsFinalizable());
1022
1023 // FileInputStream is finalizable and extends InputStream which isn't.
1024 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
1025 EXPECT_FALSE(c->IsFinalizable());
1026 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
1027 EXPECT_TRUE(c->IsFinalizable());
1028
1029 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1030 // extends ThreadPoolExecutor which does.
1031 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
1032 EXPECT_TRUE(c->IsFinalizable());
1033 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
1034 EXPECT_TRUE(c->IsFinalizable());
1035}
1036
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(Thread::Current());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001039 ClassHelper kh;
1040 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
1041 Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
1042 kh.ChangeClass(klass);
1043 EXPECT_TRUE(kh.GetDescriptor() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -08001044 EXPECT_STREQ(kh.GetDescriptor(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001045 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1046 }
1047}
1048
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001049} // namespace art