blob: fb979c201d4d28a2edede2593ff24697ba2b9a3b [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "class_linker-inl.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "dex_file.h"
Ian Rogers7655f292013-07-29 11:07:13 -070025#include "entrypoints/entrypoint_utils.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_field-inl.h"
28#include "mirror/art_method.h"
29#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class-inl.h"
31#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
34#include "mirror/proxy.h"
35#include "mirror/stack_trace_element.h"
Ian Rogers1f539342012-10-03 21:09:42 -070036#include "sirt_ref.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
38namespace art {
39
Brian Carlstromf734cf52011-08-17 16:28:14 -070040class ClassLinkerTest : public CommonTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 void AssertNonExistentClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080044 EXPECT_TRUE(class_linker_->FindSystemClass(descriptor.c_str()) == NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070045 Thread* self = Thread::Current();
46 EXPECT_TRUE(self->IsExceptionPending());
Brian Carlstromea46f952013-07-30 01:26:50 -070047 mirror::Object* exception = self->GetException(NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070048 self->ClearException();
Brian Carlstromea46f952013-07-30 01:26:50 -070049 mirror::Class* exception_class = class_linker_->FindSystemClass("Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070050 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070051 }
52
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 void AssertPrimitiveClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -080055 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070056 }
57
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080060 ClassHelper primitive_ch(primitive);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061 ASSERT_TRUE(primitive != NULL);
62 ASSERT_TRUE(primitive->GetClass() != NULL);
63 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
64 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -080065 ASSERT_STREQ(descriptor.c_str(), primitive_ch.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070066 EXPECT_TRUE(primitive->GetSuperClass() == NULL);
67 EXPECT_FALSE(primitive->HasSuperClass());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070068 EXPECT_TRUE(primitive->GetClassLoader() == NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070069 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070071 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070072 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070073 EXPECT_TRUE(primitive->IsVerified());
74 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070075 EXPECT_FALSE(primitive->IsArrayInstance());
76 EXPECT_FALSE(primitive->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070077 EXPECT_TRUE(primitive->GetComponentType() == NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 EXPECT_FALSE(primitive->IsInterface());
79 EXPECT_TRUE(primitive->IsPublic());
80 EXPECT_TRUE(primitive->IsFinal());
81 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070083 EXPECT_EQ(0U, primitive->NumDirectMethods());
84 EXPECT_EQ(0U, primitive->NumVirtualMethods());
85 EXPECT_EQ(0U, primitive->NumInstanceFields());
86 EXPECT_EQ(0U, primitive->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -070087 EXPECT_EQ(0U, primitive_ch.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -070088 EXPECT_TRUE(primitive->GetVTable() == NULL);
89 EXPECT_EQ(0, primitive->GetIfTableCount());
90 EXPECT_TRUE(primitive->GetIfTable() == NULL);
Elliott Hughes00626c22013-06-14 15:04:14 -070091 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 }
93
Brian Carlstromaded5f72011-10-07 17:15:04 -070094 void AssertArrayClass(const std::string& array_descriptor,
95 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -070096 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080098 Thread* self = Thread::Current();
99 SirtRef<mirror::ClassLoader> loader(self, class_loader);
100 SirtRef<mirror::Class> array(self,
101 class_linker_->FindClass(array_descriptor.c_str(), loader));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800102 ClassHelper array_component_ch(array->GetComponentType());
Elliott Hughes91250e02011-12-13 22:30:35 -0800103 EXPECT_STREQ(component_type.c_str(), array_component_ch.GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700104 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700105 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700106 AssertArrayClass(array_descriptor, array);
107 }
108
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800109 void AssertArrayClass(const std::string& array_descriptor, const SirtRef<mirror::Class>& array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800111 ClassHelper kh(array.get());
112 ASSERT_TRUE(array.get() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700113 ASSERT_TRUE(array->GetClass() != NULL);
114 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
115 EXPECT_TRUE(array->GetClass()->GetSuperClass() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -0800116 ASSERT_STREQ(array_descriptor.c_str(), kh.GetDescriptor());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700117 EXPECT_TRUE(array->GetSuperClass() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700118 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700119 EXPECT_TRUE(array->HasSuperClass());
120 ASSERT_TRUE(array->GetComponentType() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800121 kh.ChangeClass(array->GetComponentType());
122 ASSERT_TRUE(kh.GetDescriptor() != NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -0700123 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700124 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700125 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700126 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700127 EXPECT_TRUE(array->IsVerified());
128 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700129 EXPECT_FALSE(array->IsArrayInstance());
130 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700131 EXPECT_FALSE(array->IsInterface());
132 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
133 EXPECT_TRUE(array->IsFinal());
134 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700135 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700136 EXPECT_EQ(0U, array->NumDirectMethods());
137 EXPECT_EQ(0U, array->NumVirtualMethods());
138 EXPECT_EQ(0U, array->NumInstanceFields());
139 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800140 kh.ChangeClass(array.get());
Ian Rogersd24e2642012-06-06 21:21:43 -0700141 EXPECT_EQ(2U, kh.NumDirectInterfaces());
Brian Carlstrom86927212011-09-15 11:31:11 -0700142 EXPECT_TRUE(array->GetVTable() != NULL);
143 EXPECT_EQ(2, array->GetIfTableCount());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800144 ASSERT_TRUE(array->GetIfTable() != NULL);
Ian Rogersd24e2642012-06-06 21:21:43 -0700145 kh.ChangeClass(kh.GetDirectInterface(0));
Elliott Hughes91250e02011-12-13 22:30:35 -0800146 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/lang/Cloneable;");
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800147 kh.ChangeClass(array.get());
Ian Rogersd24e2642012-06-06 21:21:43 -0700148 kh.ChangeClass(kh.GetDirectInterface(1));
Elliott Hughes91250e02011-12-13 22:30:35 -0800149 EXPECT_STREQ(kh.GetDescriptor(), "Ljava/io/Serializable;");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700150 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700151
Brian Carlstromea46f952013-07-30 01:26:50 -0700152 void AssertMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 MethodHelper mh(method);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700154 EXPECT_TRUE(method != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700155 EXPECT_TRUE(method->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 EXPECT_TRUE(mh.GetName() != NULL);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700157 EXPECT_TRUE(mh.GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159 EXPECT_TRUE(method->GetDexCacheStrings() != NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800160 EXPECT_TRUE(method->GetDexCacheResolvedMethods() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 EXPECT_TRUE(method->GetDexCacheResolvedTypes() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
163 method->GetDexCacheStrings());
Ian Rogers19846512012-02-24 11:42:47 -0800164 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
165 method->GetDexCacheResolvedMethods());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
167 method->GetDexCacheResolvedTypes());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700168 }
169
Brian Carlstromea46f952013-07-30 01:26:50 -0700170 void AssertField(mirror::Class* klass, mirror::ArtField* field)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 FieldHelper fh(field);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700173 EXPECT_TRUE(field != NULL);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700174 EXPECT_TRUE(field->GetClass() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700175 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 EXPECT_TRUE(fh.GetName() != NULL);
177 EXPECT_TRUE(fh.GetType() != NULL);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700178 }
179
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800180 void AssertClass(const std::string& descriptor, const SirtRef<mirror::Class>& klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800182 ClassHelper kh(klass.get());
Elliott Hughes91250e02011-12-13 22:30:35 -0800183 EXPECT_STREQ(descriptor.c_str(), kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800184 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700185 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700186 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700187 EXPECT_TRUE(klass->HasSuperClass());
188 EXPECT_TRUE(klass->GetSuperClass() != NULL);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700189 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700190 EXPECT_TRUE(klass->GetClass() != NULL);
191 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700192 EXPECT_TRUE(klass->GetDexCache() != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700193 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700194 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700195 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 EXPECT_FALSE(klass->IsArrayClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700197 EXPECT_TRUE(klass->GetComponentType() == NULL);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800198 EXPECT_TRUE(klass->IsInSamePackage(klass.get()));
Brian Carlstromea46f952013-07-30 01:26:50 -0700199 EXPECT_TRUE(mirror::Class::IsInSamePackage(kh.GetDescriptor(), kh.GetDescriptor()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700200 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700201 EXPECT_TRUE(klass->IsAbstract());
202 if (klass->NumDirectMethods() == 1) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 MethodHelper mh(klass->GetDirectMethod(0));
204 EXPECT_TRUE(mh.IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700205 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700206 } else {
207 EXPECT_EQ(0U, klass->NumDirectMethods());
208 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700209 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700210 if (!klass->IsSynthetic()) {
211 EXPECT_NE(0U, klass->NumDirectMethods());
212 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700213 }
Brian Carlstrom86927212011-09-15 11:31:11 -0700214 EXPECT_EQ(klass->IsInterface(), klass->GetVTable() == NULL);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700216 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700217 mirror::Class* interface = iftable->GetInterface(i);
Brian Carlstrom86927212011-09-15 11:31:11 -0700218 ASSERT_TRUE(interface != NULL);
Brian Carlstrom86927212011-09-15 11:31:11 -0700219 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700220 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700221 } else {
Ian Rogers9bc81912012-10-11 21:43:36 -0700222 EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700223 }
224 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700225 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700226 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700227 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700228 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700229 }
230 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700231 EXPECT_FALSE(klass->IsAbstract());
232 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700233 }
234 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 EXPECT_FALSE(klass->IsFinal());
236 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700237 }
238
239 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800240 EXPECT_TRUE(klass->CanAccess(klass.get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700241
242 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700243 mirror::ArtMethod* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700244 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700245 EXPECT_TRUE(method->IsDirect());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800246 EXPECT_EQ(klass.get(), method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700247 }
248
249 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700250 mirror::ArtMethod* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700251 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700252 EXPECT_FALSE(method->IsDirect());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800253 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass.get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700254 }
255
256 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700257 mirror::ArtField* field = klass->GetInstanceField(i);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800258 AssertField(klass.get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700259 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700260 }
261
262 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700263 mirror::ArtField* field = klass->GetStaticField(i);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800264 AssertField(klass.get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700265 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700266 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700267
268 // Confirm that all instances fields are packed together at the start
269 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 FieldHelper fh;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700271 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700272 mirror::ArtField* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 fh.ChangeField(field);
274 ASSERT_TRUE(!fh.IsPrimitiveType());
Brian Carlstromea46f952013-07-30 01:26:50 -0700275 mirror::Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 ASSERT_TRUE(field_type != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 ASSERT_TRUE(!field_type->IsPrimitive());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700278 }
279 for (size_t i = klass->NumReferenceInstanceFields(); i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700280 mirror::ArtField* field = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 fh.ChangeField(field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700282 mirror::Class* field_type = fh.GetType();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700283 ASSERT_TRUE(field_type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 if (!fh.IsPrimitiveType() || !field_type->IsPrimitive()) {
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700285 // While Reference.referent is not primitive, the ClassLinker
286 // treats it as such so that the garbage collector won't scan it.
287 EXPECT_EQ(PrettyField(field), "java.lang.Object java.lang.ref.Reference.referent");
288 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700289 }
290
291 size_t total_num_reference_instance_fields = 0;
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800292 mirror::Class* k = klass.get();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700293 while (k != NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700294 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
295 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700296 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800297 EXPECT_EQ(klass->GetReferenceInstanceOffsets() == 0, total_num_reference_instance_fields == 0);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700298 }
299
Brian Carlstromea46f952013-07-30 01:26:50 -0700300 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700302 ASSERT_TRUE(descriptor != NULL);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800303 SirtRef<mirror::Class> klass(Thread::Current(),
304 class_linker_->FindSystemClass(descriptor.c_str()));
305 ASSERT_TRUE(klass.get() != nullptr);
306 EXPECT_STREQ(descriptor.c_str(), ClassHelper(klass.get()).GetDescriptor());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700307 EXPECT_EQ(class_loader, klass->GetClassLoader());
308 if (klass->IsPrimitive()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800309 AssertPrimitiveClass(descriptor, klass.get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700310 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700311 AssertArrayClass(descriptor, klass);
312 } else {
313 AssertClass(descriptor, klass);
314 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700315 }
316
Brian Carlstromea46f952013-07-30 01:26:50 -0700317 void AssertDexFile(const DexFile* dex, mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700319 ASSERT_TRUE(dex != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700320
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700321 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700322 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700323 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700324 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700325 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700326 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700327 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700328 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
329 const DexFile::TypeId& type_id = dex->GetTypeId(i);
330 const char* descriptor = dex->GetTypeDescriptor(type_id);
331 AssertDexFileClass(class_loader, descriptor);
332 }
Mathieu Chartierc4621982013-09-16 19:43:47 -0700333 class_linker_->VisitRoots(TestRootVisitor, NULL, false, false);
Ian Rogers19846512012-02-24 11:42:47 -0800334 // Verify the dex cache has resolution methods in all resolved method slots
Brian Carlstromea46f952013-07-30 01:26:50 -0700335 mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex);
336 mirror::ObjectArray<mirror::ArtMethod>* resolved_methods = dex_cache->GetResolvedMethods();
Ian Rogers19846512012-02-24 11:42:47 -0800337 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
338 EXPECT_TRUE(resolved_methods->Get(i) != NULL);
339 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700340 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700341
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700342 static mirror::Object* TestRootVisitor(mirror::Object* root, void*) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700343 EXPECT_TRUE(root != NULL);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700344 return root;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700345 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700346};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700347
Brian Carlstrom693267a2011-09-06 09:25:34 -0700348struct CheckOffset {
349 size_t cpp_offset;
350 const char* java_name;
351 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
352};
353
Elliott Hughes80609252011-09-23 17:24:51 -0700354template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700355struct CheckOffsets {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700356 CheckOffsets(bool is_static, const char* class_descriptor)
357 : is_static(is_static), class_descriptor(class_descriptor) {}
358 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700359 std::string class_descriptor;
360 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700361
Ian Rogersb726dcb2012-09-05 08:57:23 -0700362 bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700363 mirror::Class* klass = Runtime::Current()->GetClassLinker()->FindSystemClass(class_descriptor.c_str());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700364 CHECK(klass != NULL) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700365
366 bool error = false;
367
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 if (!klass->IsClassClass() && !is_static) {
369 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700370 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800371 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700372 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700373 << " Java=" << expected_size
374 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700375 error = true;
376 }
377 }
378
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700380 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800381 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700382 << " class=" << class_descriptor
383 << " Java=" << num_fields
384 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700385 error = true;
386 }
387
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800388 FieldHelper fh;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700389 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700390 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 fh.ChangeField(field);
392 StringPiece field_name(fh.GetName());
393 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700394 error = true;
395 }
396 }
397 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700398 for (size_t i = 0; i < offsets.size(); i++) {
399 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 fh.ChangeField(field);
402 StringPiece field_name(fh.GetName());
403 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800404 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700405 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800406 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700407 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700409 << " CheckOffsets=" << offset.java_name;
410 }
411 }
412
Brian Carlstromdbc05252011-09-09 01:59:59 -0700413 for (size_t i = 0; i < offsets.size(); i++) {
414 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700415 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700416 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
417 error = true;
418 }
419 }
420 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700421 for (size_t i = 0; i < offsets.size(); i++) {
422 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700423 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700424 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800425 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700426 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800427 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700428 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
429 }
430 }
431
432 return !error;
433 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700434
435 private:
436 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700437};
438
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700439// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
440// is first since if it is failing, others are unlikely to succeed.
441
Brian Carlstromea46f952013-07-30 01:26:50 -0700442struct ObjectOffsets : public CheckOffsets<mirror::Object> {
443 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700444 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700445 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700446
447 // alphabetical 32-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700448 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700449 };
450};
451
Brian Carlstromea46f952013-07-30 01:26:50 -0700452struct ArtFieldOffsets : public CheckOffsets<mirror::ArtField> {
453 ArtFieldOffsets() : CheckOffsets<mirror::ArtField>(false, "Ljava/lang/reflect/ArtField;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700454 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700455 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, declaring_class_), "declaringClass"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700456
457 // alphabetical 32-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, access_flags_), "accessFlags"));
459 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, field_dex_idx_), "fieldDexIndex"));
460 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700461 };
462};
463
Brian Carlstromea46f952013-07-30 01:26:50 -0700464struct ArtMethodOffsets : public CheckOffsets<mirror::ArtMethod> {
465 ArtMethodOffsets() : CheckOffsets<mirror::ArtMethod>(false, "Ljava/lang/reflect/ArtMethod;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700466 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700467 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, declaring_class_), "declaringClass"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700468 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
469 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
470 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_strings_), "dexCacheStrings"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700471
Ian Rogersef7d42f2014-01-06 12:55:46 -0800472 // alphabetical 64-bit
473 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_interpreter_), "entryPointFromInterpreter"));
474 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_jni_), "entryPointFromJni"));
475 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_portable_compiled_code_), "entryPointFromPortableCompiledCode"));
476 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_quick_compiled_code_), "entryPointFromQuickCompiledCode"));
477 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, gc_map_), "gcMap"));
478 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, quick_mapping_table_), "quickMappingTable"));
479 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, quick_vmap_table_), "quickVmapTable"));
480
Brian Carlstromdbc05252011-09-09 01:59:59 -0700481 // alphabetical 32-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700482 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, access_flags_), "accessFlags"));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800483 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_code_item_offset_), "dexCodeItemOffset"));
484 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_method_index_), "dexMethodIndex"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700485 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_index_), "methodIndex"));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800486 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, quick_core_spill_mask_), "quickCoreSpillMask"));
487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, quick_fp_spill_mask_), "quickFpSpillMask"));
488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, quick_frame_size_in_bytes_), "quickFrameSizeInBytes"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700489 };
490};
491
Brian Carlstromea46f952013-07-30 01:26:50 -0700492struct ClassOffsets : public CheckOffsets<mirror::Class> {
493 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700494 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700495 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader"));
496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType"));
497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache"));
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods"));
499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields"));
500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable"));
Jeff Hao88474b42013-10-23 16:24:40 -0700501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, imtable_), "imTable"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass"));
505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_), "verifyErrorClass"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods"));
507 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700508
509 // alphabetical 32-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700510 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags"));
511 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize"));
512 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId"));
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700514 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex"));
515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), "numReferenceStaticFields"));
517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType"));
519 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), "referenceInstanceOffsets"));
520 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_static_offsets_), "referenceStaticOffsets"));
521 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700522 };
523};
524
Brian Carlstromea46f952013-07-30 01:26:50 -0700525struct StringOffsets : public CheckOffsets<mirror::String> {
526 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700527 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, array_), "value"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700529
530 // alphabetical 32-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700531 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, count_), "count"));
532 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode"));
533 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700534 };
535};
536
Brian Carlstromea46f952013-07-30 01:26:50 -0700537struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
538 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700539 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700540 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause"));
541 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage"));
542 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState"));
543 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace"));
544 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 };
546};
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
549 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700550 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700551 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"));
552 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName"));
553 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName"));
554 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700555 };
556};
557
Brian Carlstromea46f952013-07-30 01:26:50 -0700558struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
559 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700560 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages"));
562 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent"));
563 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700564 };
565};
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
568 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Jesse Wilson95caa792011-10-12 18:14:17 -0400569 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"));
Jesse Wilson95caa792011-10-12 18:14:17 -0400571 };
572};
573
Brian Carlstromea46f952013-07-30 01:26:50 -0700574struct ClassClassOffsets : public CheckOffsets<mirror::ClassClass> {
575 ClassClassOffsets() : CheckOffsets<mirror::ClassClass>(true, "Ljava/lang/Class;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700576 // alphabetical 64-bit
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassClass, serialVersionUID_), "serialVersionUID"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700578 };
579};
580
Brian Carlstromea46f952013-07-30 01:26:50 -0700581struct StringClassOffsets : public CheckOffsets<mirror::StringClass> {
582 StringClassOffsets() : CheckOffsets<mirror::StringClass>(true, "Ljava/lang/String;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700583 // alphabetical references
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, ASCII_), "ASCII"));
585 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, CASE_INSENSITIVE_ORDER_), "CASE_INSENSITIVE_ORDER"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700586
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700587 // alphabetical 32-bit
588 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, REPLACEMENT_CHAR_), "REPLACEMENT_CHAR"));
Jeff Hao88474b42013-10-23 16:24:40 -0700589
590 // alphabetical 64-bit
591 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StringClass, serialVersionUID_), "serialVersionUID"));
Brian Carlstromdbc05252011-09-09 01:59:59 -0700592 };
593};
594
Brian Carlstromea46f952013-07-30 01:26:50 -0700595struct ArtFieldClassOffsets : public CheckOffsets<mirror::ArtFieldClass> {
596 ArtFieldClassOffsets() : CheckOffsets<mirror::ArtFieldClass>(true, "Ljava/lang/reflect/ArtField;") {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700597 };
598};
599
Brian Carlstromea46f952013-07-30 01:26:50 -0700600struct ArtMethodClassOffsets : public CheckOffsets<mirror::ArtMethodClass> {
601 ArtMethodClassOffsets() : CheckOffsets<mirror::ArtMethodClass>(true, "Ljava/lang/reflect/ArtMethod;") {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700602 };
603};
604
Brian Carlstromea46f952013-07-30 01:26:50 -0700605struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
606 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700607 // alphabetical references
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700608 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location"));
610 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields"));
611 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods"));
612 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes"));
613 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings"));
614 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile"));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700615 };
616};
617
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400618// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400619// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700620// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400621TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700623 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstromea46f952013-07-30 01:26:50 -0700624 EXPECT_TRUE(ArtFieldOffsets().Check());
625 EXPECT_TRUE(ArtMethodOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700626 EXPECT_TRUE(ClassOffsets().Check());
627 EXPECT_TRUE(StringOffsets().Check());
628 EXPECT_TRUE(ThrowableOffsets().Check());
629 EXPECT_TRUE(StackTraceElementOffsets().Check());
630 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400631 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700632 EXPECT_TRUE(DexCacheOffsets().Check());
Brian Carlstromdbc05252011-09-09 01:59:59 -0700633
634 EXPECT_TRUE(ClassClassOffsets().Check());
635 EXPECT_TRUE(StringClassOffsets().Check());
Brian Carlstromea46f952013-07-30 01:26:50 -0700636 EXPECT_TRUE(ArtFieldClassOffsets().Check());
637 EXPECT_TRUE(ArtMethodClassOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700638}
639
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700640TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700642 AssertNonExistentClass("NoSuchClass;");
643 AssertNonExistentClass("LNoSuchClass;");
644}
645
646TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700648 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Nested")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700649
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 mirror::Class* outer = class_linker_->FindClass("LNested;", class_loader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700651 ASSERT_TRUE(outer != NULL);
652 EXPECT_EQ(0U, outer->NumVirtualMethods());
653 EXPECT_EQ(1U, outer->NumDirectMethods());
654
Mathieu Chartier590fee92013-09-13 13:46:47 -0700655 mirror::Class* inner = class_linker_->FindClass("LNested$Inner;", class_loader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700656 ASSERT_TRUE(inner != NULL);
657 EXPECT_EQ(0U, inner->NumVirtualMethods());
658 EXPECT_EQ(1U, inner->NumDirectMethods());
659}
660
661TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700663 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800664 for (int ch = 1; ch < 256; ++ch) {
665 std::string descriptor;
666 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700667 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700668 AssertNonExistentClass(descriptor);
669 } else {
670 AssertPrimitiveClass(descriptor);
671 }
672 }
673}
674
675TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700677 mirror::Class* JavaLangObject = class_linker_->FindSystemClass("Ljava/lang/Object;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800678 ClassHelper kh(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700679 ASSERT_TRUE(JavaLangObject != NULL);
680 ASSERT_TRUE(JavaLangObject->GetClass() != NULL);
681 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
682 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800683 ASSERT_STREQ(kh.GetDescriptor(), "Ljava/lang/Object;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700684 EXPECT_TRUE(JavaLangObject->GetSuperClass() == NULL);
685 EXPECT_FALSE(JavaLangObject->HasSuperClass());
686 EXPECT_TRUE(JavaLangObject->GetClassLoader() == NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -0700687 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700688 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700689 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700690 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromea46f952013-07-30 01:26:50 -0700691 EXPECT_TRUE(JavaLangObject->IsVerified());
692 EXPECT_TRUE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700693 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
694 EXPECT_FALSE(JavaLangObject->IsArrayClass());
695 EXPECT_TRUE(JavaLangObject->GetComponentType() == NULL);
696 EXPECT_FALSE(JavaLangObject->IsInterface());
697 EXPECT_TRUE(JavaLangObject->IsPublic());
698 EXPECT_FALSE(JavaLangObject->IsFinal());
699 EXPECT_FALSE(JavaLangObject->IsPrimitive());
700 EXPECT_FALSE(JavaLangObject->IsSynthetic());
701 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
702 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
703 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800704 FieldHelper fh(JavaLangObject->GetInstanceField(0));
705 EXPECT_STREQ(fh.GetName(), "shadow$_klass_");
706 fh.ChangeField(JavaLangObject->GetInstanceField(1));
707 EXPECT_STREQ(fh.GetName(), "shadow$_monitor_");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700708
709 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700710 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700711
Brian Carlstromea46f952013-07-30 01:26:50 -0700712 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700713 AssertNonExistentClass("LMyClass;");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700714 mirror::Class* MyClass = class_linker_->FindClass("LMyClass;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800715 kh.ChangeClass(MyClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700716 ASSERT_TRUE(MyClass != NULL);
717 ASSERT_TRUE(MyClass->GetClass() != NULL);
718 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
719 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Elliott Hughes91250e02011-12-13 22:30:35 -0800720 ASSERT_STREQ(kh.GetDescriptor(), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700721 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
722 EXPECT_TRUE(MyClass->HasSuperClass());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700723 EXPECT_EQ(class_loader.get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700724 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700725 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700726 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700727 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700728 EXPECT_FALSE(MyClass->IsVerified());
729 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700730 EXPECT_FALSE(MyClass->IsArrayInstance());
731 EXPECT_FALSE(MyClass->IsArrayClass());
732 EXPECT_TRUE(MyClass->GetComponentType() == NULL);
733 EXPECT_FALSE(MyClass->IsInterface());
734 EXPECT_FALSE(MyClass->IsPublic());
735 EXPECT_FALSE(MyClass->IsFinal());
736 EXPECT_FALSE(MyClass->IsPrimitive());
737 EXPECT_FALSE(MyClass->IsSynthetic());
738 EXPECT_EQ(1U, MyClass->NumDirectMethods());
739 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
740 EXPECT_EQ(0U, MyClass->NumInstanceFields());
741 EXPECT_EQ(0U, MyClass->NumStaticFields());
Ian Rogersd24e2642012-06-06 21:21:43 -0700742 EXPECT_EQ(0U, kh.NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700743
744 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
745
746 // created by class_linker
747 AssertArrayClass("[C", "C", NULL);
748 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", NULL);
749 // synthesized on the fly
750 AssertArrayClass("[[C", "[C", NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700751 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700752 // or not available at all
753 AssertNonExistentClass("[[[[LNonExistentClass;");
754}
755
756TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 ScopedObjectAccess soa(Thread::Current());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800758 AssertDexFile(java_lang_dex_file_, NULL);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700759}
760
Ian Rogersa15e67d2012-02-28 13:51:55 -0800761// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400762// start of the object
763TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700765 mirror::Class* array_class = class_linker_->FindSystemClass("[Ljava/lang/String;");
766 mirror::ObjectArray<mirror::String>* array =
767 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800768 uintptr_t data_offset =
769 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
770 0));
771 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800772 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
773 } else {
774 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
775 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400776}
777
778TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700780 SirtRef<mirror::LongArray> long_array(soa.Self(), mirror::LongArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800781 EXPECT_EQ(class_linker_->FindSystemClass("[J"), long_array->GetClass());
782 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
783 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
784
Brian Carlstromea46f952013-07-30 01:26:50 -0700785 SirtRef<mirror::DoubleArray> double_array(soa.Self(), mirror::DoubleArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800786 EXPECT_EQ(class_linker_->FindSystemClass("[D"), double_array->GetClass());
787 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
788 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
789
Brian Carlstromea46f952013-07-30 01:26:50 -0700790 SirtRef<mirror::IntArray> int_array(soa.Self(), mirror::IntArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800791 EXPECT_EQ(class_linker_->FindSystemClass("[I"), int_array->GetClass());
792 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
793 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
794
Brian Carlstromea46f952013-07-30 01:26:50 -0700795 SirtRef<mirror::CharArray> char_array(soa.Self(), mirror::CharArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800796 EXPECT_EQ(class_linker_->FindSystemClass("[C"), char_array->GetClass());
797 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
798 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
799
Brian Carlstromea46f952013-07-30 01:26:50 -0700800 SirtRef<mirror::ShortArray> short_array(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 0));
Ian Rogersa15e67d2012-02-28 13:51:55 -0800801 EXPECT_EQ(class_linker_->FindSystemClass("[S"), short_array->GetClass());
802 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
803 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
804
805 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400806}
807
Elliott Hughes33203b52011-09-20 19:42:01 -0700808TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
809 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
810 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700812 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700813 mirror::Class* c;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700814 c = class_linker_->FindClass("Ljava/lang/Boolean;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800815 FieldHelper fh(c->GetIFields()->Get(0));
816 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700817 c = class_linker_->FindClass("Ljava/lang/Byte;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800818 fh.ChangeField(c->GetIFields()->Get(0));
819 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700820 c = class_linker_->FindClass("Ljava/lang/Character;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800821 fh.ChangeField(c->GetIFields()->Get(0));
822 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700823 c = class_linker_->FindClass("Ljava/lang/Double;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800824 fh.ChangeField(c->GetIFields()->Get(0));
825 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700826 c = class_linker_->FindClass("Ljava/lang/Float;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800827 fh.ChangeField(c->GetIFields()->Get(0));
828 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700829 c = class_linker_->FindClass("Ljava/lang/Integer;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800830 fh.ChangeField(c->GetIFields()->Get(0));
831 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700832 c = class_linker_->FindClass("Ljava/lang/Long;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800833 fh.ChangeField(c->GetIFields()->Get(0));
834 EXPECT_STREQ("value", fh.GetName());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700835 c = class_linker_->FindClass("Ljava/lang/Short;", class_loader);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800836 fh.ChangeField(c->GetIFields()->Get(0));
837 EXPECT_STREQ("value", fh.GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700838}
839
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700840TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700842 SirtRef<mirror::ClassLoader> class_loader_1(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
843 SirtRef<mirror::ClassLoader> class_loader_2(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass")));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700844 mirror::Class* MyClass_1 = class_linker_->FindClass("LMyClass;", class_loader_1);
845 mirror::Class* MyClass_2 = class_linker_->FindClass("LMyClass;", class_loader_2);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700846 EXPECT_TRUE(MyClass_1 != NULL);
847 EXPECT_TRUE(MyClass_2 != NULL);
848 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400849}
850
Jesse Wilson7833bd22011-08-09 18:31:44 -0400851TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700853 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Statics")));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800854 SirtRef<mirror::Class> statics(soa.Self(), class_linker_->FindClass("LStatics;", class_loader));
Ian Rogers0045a292012-03-31 21:08:41 -0700855 class_linker_->EnsureInitialized(statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400856
jeffhaoabcfde32011-09-29 15:05:18 -0700857 // Static final primitives that are initialized by a compile-time constant
858 // expression resolve to a copy of a constant value from the constant pool.
859 // So <clinit> should be null.
Brian Carlstromea46f952013-07-30 01:26:50 -0700860 mirror::ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V");
jeffhaoabcfde32011-09-29 15:05:18 -0700861 EXPECT_TRUE(clinit == NULL);
862
863 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400864
Brian Carlstromea46f952013-07-30 01:26:50 -0700865 mirror::ArtField* s0 = statics->FindStaticField("s0", "Z");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800866 FieldHelper fh(s0);
Brian Carlstromea46f952013-07-30 01:26:50 -0700867 EXPECT_STREQ(ClassHelper(s0->GetClass()).GetDescriptor(), "Ljava/lang/reflect/ArtField;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimBoolean);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800869 EXPECT_EQ(true, s0->GetBoolean(statics.get()));
870 s0->SetBoolean(statics.get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400871
Brian Carlstromea46f952013-07-30 01:26:50 -0700872 mirror::ArtField* s1 = statics->FindStaticField("s1", "B");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800873 fh.ChangeField(s1);
874 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimByte);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800875 EXPECT_EQ(5, s1->GetByte(statics.get()));
876 s1->SetByte(statics.get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400877
Brian Carlstromea46f952013-07-30 01:26:50 -0700878 mirror::ArtField* s2 = statics->FindStaticField("s2", "C");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800879 fh.ChangeField(s2);
880 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimChar);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800881 EXPECT_EQ('a', s2->GetChar(statics.get()));
882 s2->SetChar(statics.get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400883
Brian Carlstromea46f952013-07-30 01:26:50 -0700884 mirror::ArtField* s3 = statics->FindStaticField("s3", "S");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885 fh.ChangeField(s3);
886 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimShort);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800887 EXPECT_EQ(-536, s3->GetShort(statics.get()));
888 s3->SetShort(statics.get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400889
Brian Carlstromea46f952013-07-30 01:26:50 -0700890 mirror::ArtField* s4 = statics->FindStaticField("s4", "I");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800891 fh.ChangeField(s4);
892 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimInt);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800893 EXPECT_EQ(2000000000, s4->GetInt(statics.get()));
894 s4->SetInt(statics.get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400895
Brian Carlstromea46f952013-07-30 01:26:50 -0700896 mirror::ArtField* s5 = statics->FindStaticField("s5", "J");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800897 fh.ChangeField(s5);
898 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimLong);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800899 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.get()));
900 s5->SetLong(statics.get(), 0x34567890abcdef12LL);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400901
Brian Carlstromea46f952013-07-30 01:26:50 -0700902 mirror::ArtField* s6 = statics->FindStaticField("s6", "F");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800903 fh.ChangeField(s6);
904 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimFloat);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800905 EXPECT_EQ(0.5, s6->GetFloat(statics.get()));
906 s6->SetFloat(statics.get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400907
Brian Carlstromea46f952013-07-30 01:26:50 -0700908 mirror::ArtField* s7 = statics->FindStaticField("s7", "D");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 fh.ChangeField(s7);
910 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimDouble);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800911 EXPECT_EQ(16777217, s7->GetDouble(statics.get()));
912 s7->SetDouble(statics.get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400913
Brian Carlstromea46f952013-07-30 01:26:50 -0700914 mirror::ArtField* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800915 fh.ChangeField(s8);
916 EXPECT_TRUE(fh.GetTypeAsPrimitiveType() == Primitive::kPrimNot);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800917 EXPECT_TRUE(s8->GetObject(statics.get())->AsString()->Equals("android"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700918 s8->SetObject(s8->GetDeclaringClass(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400919
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800920 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
921 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800922 EXPECT_FALSE(s0->GetBoolean(statics.get()));
923 EXPECT_EQ(6, s1->GetByte(statics.get()));
924 EXPECT_EQ('b', s2->GetChar(statics.get()));
925 EXPECT_EQ(-535, s3->GetShort(statics.get()));
926 EXPECT_EQ(2000000001, s4->GetInt(statics.get()));
927 EXPECT_EQ(0x34567890abcdef12LL, s5->GetLong(statics.get()));
928 EXPECT_EQ(0.75, s6->GetFloat(statics.get()));
929 EXPECT_EQ(16777219, s7->GetDouble(statics.get()));
930 EXPECT_TRUE(s8->GetObject(statics.get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400931}
932
Brian Carlstrom30b94452011-08-25 21:35:26 -0700933TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -0700935 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces")));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700936 mirror::Class* I = class_linker_->FindClass("LInterfaces$I;", class_loader);
937 mirror::Class* J = class_linker_->FindClass("LInterfaces$J;", class_loader);
938 mirror::Class* K = class_linker_->FindClass("LInterfaces$K;", class_loader);
939 mirror::Class* A = class_linker_->FindClass("LInterfaces$A;", class_loader);
940 mirror::Class* B = class_linker_->FindClass("LInterfaces$B;", class_loader);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700941 EXPECT_TRUE(I->IsAssignableFrom(A));
942 EXPECT_TRUE(J->IsAssignableFrom(A));
jeffhao5dbddee2011-09-07 16:38:26 -0700943 EXPECT_TRUE(J->IsAssignableFrom(K));
944 EXPECT_TRUE(K->IsAssignableFrom(B));
945 EXPECT_TRUE(J->IsAssignableFrom(B));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700946
Ian Rogersd91d6d62013-09-25 20:26:14 -0700947 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
948 mirror::ArtMethod* Ii = I->FindVirtualMethod("i", void_sig);
949 mirror::ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig);
950 mirror::ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig);
951 mirror::ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig);
952 mirror::ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig);
953 mirror::ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig);
954 mirror::ArtMethod* Ai = A->FindVirtualMethod("i", void_sig);
955 mirror::ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig);
956 mirror::ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700957 ASSERT_TRUE(Ii != NULL);
958 ASSERT_TRUE(Jj1 != NULL);
959 ASSERT_TRUE(Jj2 != NULL);
jeffhao5dbddee2011-09-07 16:38:26 -0700960 ASSERT_TRUE(Kj1 != NULL);
961 ASSERT_TRUE(Kj2 != NULL);
962 ASSERT_TRUE(Kk != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700963 ASSERT_TRUE(Ai != NULL);
964 ASSERT_TRUE(Aj1 != NULL);
965 ASSERT_TRUE(Aj2 != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700966 EXPECT_NE(Ii, Ai);
967 EXPECT_NE(Jj1, Aj1);
968 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700969 EXPECT_EQ(Kj1, Jj1);
970 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800971 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
972 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
973 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700974 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
975 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
976 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
Ian Rogersd24e2642012-06-06 21:21:43 -0700977
Brian Carlstromea46f952013-07-30 01:26:50 -0700978 mirror::ArtField* Afoo = A->FindStaticField("foo", "Ljava/lang/String;");
979 mirror::ArtField* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;");
980 mirror::ArtField* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;");
981 mirror::ArtField* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;");
Ian Rogersd24e2642012-06-06 21:21:43 -0700982 ASSERT_TRUE(Afoo != NULL);
983 EXPECT_EQ(Afoo, Bfoo);
984 EXPECT_EQ(Afoo, Jfoo);
985 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700986}
987
Elliott Hughesf3778f62012-01-26 14:14:35 -0800988TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -0700989 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700990
jeffhaoabcfde32011-09-29 15:05:18 -0700991 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
992 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700993
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(Thread::Current());
995 jobject jclass_loader = LoadDex("StaticsFromCode");
Brian Carlstromea46f952013-07-30 01:26:50 -0700996 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700998 CHECK(dex_file != NULL);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700999 mirror::Class* klass = class_linker_->FindClass("LStaticsFromCode;", class_loader);
Ian Rogers241b5de2013-10-09 17:58:57 -07001000 mirror::ArtMethod* clinit = klass->FindClassInitializer();
Brian Carlstromea46f952013-07-30 01:26:50 -07001001 mirror::ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001002 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
1003 ASSERT_TRUE(string_id != NULL);
1004 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
1005 ASSERT_TRUE(type_id != NULL);
1006 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001007 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001008 EXPECT_TRUE(uninit != NULL);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001009 EXPECT_FALSE(uninit->IsInitialized());
1010 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001011 EXPECT_TRUE(init != NULL);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001012 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001013}
1014
Elliott Hughes20cde902011-10-04 17:37:27 -07001015TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001017 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001018
1019 // Object has a finalize method, but we know it's empty.
1020 c = class_linker_->FindSystemClass("Ljava/lang/Object;");
1021 EXPECT_FALSE(c->IsFinalizable());
1022
1023 // Enum has a finalize method to prevent its subclasses from implementing one.
1024 c = class_linker_->FindSystemClass("Ljava/lang/Enum;");
1025 EXPECT_FALSE(c->IsFinalizable());
1026
1027 // RoundingMode is an enum.
1028 c = class_linker_->FindSystemClass("Ljava/math/RoundingMode;");
1029 EXPECT_FALSE(c->IsFinalizable());
1030
1031 // RandomAccessFile extends Object and overrides finalize.
1032 c = class_linker_->FindSystemClass("Ljava/io/RandomAccessFile;");
1033 EXPECT_TRUE(c->IsFinalizable());
1034
1035 // FileInputStream is finalizable and extends InputStream which isn't.
1036 c = class_linker_->FindSystemClass("Ljava/io/InputStream;");
1037 EXPECT_FALSE(c->IsFinalizable());
1038 c = class_linker_->FindSystemClass("Ljava/io/FileInputStream;");
1039 EXPECT_TRUE(c->IsFinalizable());
1040
1041 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1042 // extends ThreadPoolExecutor which does.
1043 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ThreadPoolExecutor;");
1044 EXPECT_TRUE(c->IsFinalizable());
1045 c = class_linker_->FindSystemClass("Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
1046 EXPECT_TRUE(c->IsFinalizable());
1047}
1048
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001049TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 ScopedObjectAccess soa(Thread::Current());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001051 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001052 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Mathieu Chartier590fee92013-09-13 13:46:47 -07001053 ClassHelper kh(klass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001054 EXPECT_TRUE(kh.GetDescriptor() != NULL);
Elliott Hughes91250e02011-12-13 22:30:35 -08001055 EXPECT_STREQ(kh.GetDescriptor(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1057 }
1058}
1059
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001060} // namespace art