blob: ba5aa3d75a3b90701e1b8f952acfd64d22bbdaa7 [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 Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <string>
21
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070026#include "field_helper.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_field-inl.h"
29#include "mirror/art_method.h"
30#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/class-inl.h"
32#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
35#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070036#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "scoped_thread_state_change.h"
41#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
43namespace art {
44
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080045class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070046 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 void AssertNonExistentClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070049 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070050 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070051 EXPECT_TRUE(self->IsExceptionPending());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070052 mirror::Object* exception = self->GetException(nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070053 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080054 mirror::Class* exception_class =
55 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070056 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 }
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 void AssertPrimitiveClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080061 Thread* self = Thread::Current();
62 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070063 }
64
Ian Rogersef7d42f2014-01-06 12:55:46 -080065 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070067 ASSERT_TRUE(primitive != nullptr);
68 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070069 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070070 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070071 std::string temp;
72 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070073 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070075 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070076 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070077 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070078 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070079 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070080 EXPECT_TRUE(primitive->IsVerified());
81 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070082 EXPECT_FALSE(primitive->IsArrayInstance());
83 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070084 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085 EXPECT_FALSE(primitive->IsInterface());
86 EXPECT_TRUE(primitive->IsPublic());
87 EXPECT_TRUE(primitive->IsFinal());
88 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070089 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070090 EXPECT_EQ(0U, primitive->NumDirectMethods());
91 EXPECT_EQ(0U, primitive->NumVirtualMethods());
92 EXPECT_EQ(0U, primitive->NumInstanceFields());
93 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070094 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -070095 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -070096 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070097 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -070098 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070099 }
100
Brian Carlstromaded5f72011-10-07 17:15:04 -0700101 void AssertArrayClass(const std::string& array_descriptor,
102 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700103 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800105 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700106 StackHandleScope<2> hs(self);
107 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
108 Handle<mirror::Class> array(
109 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700110 std::string temp;
111 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700112 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700113 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700114 AssertArrayClass(array_descriptor, array);
115 }
116
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700117 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700119 ASSERT_TRUE(array.Get() != nullptr);
120 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700121 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700122 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700123 std::string temp;
124 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700125 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800126 Thread* self = Thread::Current();
127 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700128 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700129 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700130 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700131 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700132 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700133 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700134 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700135 EXPECT_TRUE(array->IsVerified());
136 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700137 EXPECT_FALSE(array->IsArrayInstance());
138 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700139 EXPECT_FALSE(array->IsInterface());
140 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
141 EXPECT_TRUE(array->IsFinal());
142 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700143 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700144 EXPECT_EQ(0U, array->NumDirectMethods());
145 EXPECT_EQ(0U, array->NumVirtualMethods());
146 EXPECT_EQ(0U, array->NumInstanceFields());
147 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700148 EXPECT_EQ(2U, array->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700149 EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700150 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700151 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700152 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
153 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700154 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700155 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700156 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700157 mirror::Class* array_ptr = array->GetComponentType();
158 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700159 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700160
Brian Carlstromea46f952013-07-30 01:26:50 -0700161 void AssertMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700162 EXPECT_TRUE(method != nullptr);
163 EXPECT_TRUE(method->GetClass() != nullptr);
164 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700165 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700166
Andreas Gampe58a5af82014-07-31 16:23:49 -0700167 EXPECT_TRUE(method->GetDexCacheStrings() != nullptr);
168 EXPECT_TRUE(method->HasDexCacheResolvedMethods());
169 EXPECT_TRUE(method->HasDexCacheResolvedTypes());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 EXPECT_EQ(method->GetDeclaringClass()->GetDexCache()->GetStrings(),
171 method->GetDexCacheStrings());
Andreas Gampe58a5af82014-07-31 16:23:49 -0700172 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
173 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods()));
174 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
175 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes()));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700176 }
177
Brian Carlstromea46f952013-07-30 01:26:50 -0700178 void AssertField(mirror::Class* klass, mirror::ArtField* field)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700180 EXPECT_TRUE(field != nullptr);
181 EXPECT_TRUE(field->GetClass() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700182 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700183 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700184 StackHandleScope<1> hs(Thread::Current());
185 FieldHelper fh(hs.NewHandle(field));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700186 EXPECT_TRUE(fh.GetType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700187 }
188
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700189 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700191 std::string temp;
192 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700194 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700195 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700196 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700197 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700198 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700199 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700200 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700201 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700202 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700203 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700204 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700206 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 std::string temp2;
209 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
210 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700211 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700212 EXPECT_TRUE(klass->IsAbstract());
213 if (klass->NumDirectMethods() == 1) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700214 EXPECT_TRUE(klass->GetDirectMethod(0)->IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700215 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 } else {
217 EXPECT_EQ(0U, klass->NumDirectMethods());
218 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700219 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700220 if (!klass->IsSynthetic()) {
221 EXPECT_NE(0U, klass->NumDirectMethods());
222 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700223 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700224 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700226 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700227 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700228 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700229 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700230 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700231 } else {
Ian Rogers9bc81912012-10-11 21:43:36 -0700232 EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700233 }
234 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700235 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700236 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700237 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700238 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700239 }
240 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700241 EXPECT_FALSE(klass->IsAbstract());
242 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700243 }
244 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700245 EXPECT_FALSE(klass->IsFinal());
246 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700247 }
248
249 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700250 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700251
252 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700253 mirror::ArtMethod* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700254 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700255 EXPECT_TRUE(method->IsDirect());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700256 EXPECT_EQ(klass.Get(), method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700257 }
258
259 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700260 mirror::ArtMethod* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700261 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700262 EXPECT_FALSE(method->IsDirect());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700263 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700264 }
265
266 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700267 mirror::ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700268 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700269 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700270 }
271
272 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700273 mirror::ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700274 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700275 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700276 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277
Vladimir Marko76649e82014-11-10 18:32:59 +0000278 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700279 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700280 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700281 MutableHandle<mirror::ArtField> fhandle = hs.NewHandle<mirror::ArtField>(nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000282 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
283 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
284 klass->NumReferenceInstanceFields() *
285 sizeof(mirror::HeapReference<mirror::Object>));
286 MemberOffset current_ref_offset = start_ref_offset;
287 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700288 mirror::ArtField* field = klass->GetInstanceField(i);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700289 fhandle.Assign(field);
290 FieldHelper fh(fhandle);
Brian Carlstromea46f952013-07-30 01:26:50 -0700291 mirror::Class* field_type = fh.GetType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700292 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000293 if (!field->IsPrimitiveType()) {
294 ASSERT_TRUE(!field_type->IsPrimitive());
295 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
296 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
297 // While Reference.referent is not primitive, the ClassLinker
298 // treats it as such so that the garbage collector won't scan it.
299 EXPECT_EQ(PrettyField(fh.GetField()),
300 "java.lang.Object java.lang.ref.Reference.referent");
301 } else {
302 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
303 sizeof(mirror::HeapReference<mirror::Object>));
304 }
305 } else {
306 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
307 // Shuffled before references.
308 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
309 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
310 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700311 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700312 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000313 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700314
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700315 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700317 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700318 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
319 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700320 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700321 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
322 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
323 // The reference instance offsets have a bit set for each reference offset.
324 // +1 for Object's class.
325 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
326 total_num_reference_instance_fields);
327 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700328 }
329
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700332 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800333 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700334 StackHandleScope<1> hs(self);
335 Handle<mirror::Class> klass(
336 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
337 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700338 std::string temp;
339 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700340 EXPECT_EQ(class_loader, klass->GetClassLoader());
341 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700343 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700344 AssertArrayClass(descriptor, klass);
345 } else {
346 AssertClass(descriptor, klass);
347 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700348 }
349
Brian Carlstromea46f952013-07-30 01:26:50 -0700350 void AssertDexFile(const DexFile* dex, mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700352 ASSERT_TRUE(dex != nullptr);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700353
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700354 // Verify all the classes defined in this file
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700355 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700356 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700357 const char* descriptor = dex->GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700358 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700359 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700360 // Verify all the types referenced by this file
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700361 for (size_t i = 0; i < dex->NumTypeIds(); i++) {
362 const DexFile::TypeId& type_id = dex->GetTypeId(i);
363 const char* descriptor = dex->GetTypeDescriptor(type_id);
364 AssertDexFileClass(class_loader, descriptor);
365 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700366 class_linker_->VisitRoots(TestRootVisitor, nullptr, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800367 // Verify the dex cache has resolution methods in all resolved method slots
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex);
369 mirror::ObjectArray<mirror::ArtMethod>* resolved_methods = dex_cache->GetResolvedMethods();
Ian Rogers19846512012-02-24 11:42:47 -0800370 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700371 EXPECT_TRUE(resolved_methods->Get(i) != nullptr) << dex->GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800372 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700373 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700374
Mathieu Chartier815873e2014-02-13 18:02:13 -0800375 static void TestRootVisitor(mirror::Object** root, void*, uint32_t, RootType) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700376 EXPECT_TRUE(*root != nullptr);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700377 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700378};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700379
Brian Carlstrom693267a2011-09-06 09:25:34 -0700380struct CheckOffset {
381 size_t cpp_offset;
382 const char* java_name;
383 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
384};
385
Elliott Hughes80609252011-09-23 17:24:51 -0700386template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700387struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800388 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
389 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700390 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700391 std::string class_descriptor;
392 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700393
Ian Rogersb726dcb2012-09-05 08:57:23 -0700394 bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800395 Thread* self = Thread::Current();
396 mirror::Class* klass =
397 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700398 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700399
400 bool error = false;
401
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700402 if (!klass->IsClassClass() && !is_static) {
403 size_t expected_size = is_static ? klass->GetClassSize(): klass->GetObjectSize();
Elliott Hughes80609252011-09-23 17:24:51 -0700404 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800405 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700406 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700407 << " Java=" << expected_size
408 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700409 error = true;
410 }
411 }
412
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700413 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700414 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800415 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700416 << " class=" << class_descriptor
417 << " Java=" << num_fields
418 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700419 error = true;
420 }
421
Brian Carlstromdbc05252011-09-09 01:59:59 -0700422 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700423 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700424 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800425 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700426 error = true;
427 }
428 }
429 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700430 for (size_t i = 0; i < offsets.size(); i++) {
431 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700432 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700433 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800435 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700436 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800437 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700438 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700440 << " CheckOffsets=" << offset.java_name;
441 }
442 }
443
Brian Carlstromdbc05252011-09-09 01:59:59 -0700444 for (size_t i = 0; i < offsets.size(); i++) {
445 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700447 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
448 error = true;
449 }
450 }
451 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700452 for (size_t i = 0; i < offsets.size(); i++) {
453 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700454 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700455 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800456 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700457 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800458 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700459 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
460 }
461 }
462
463 return !error;
464 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700465
466 private:
467 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700468};
469
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700470// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
471// is first since if it is failing, others are unlikely to succeed.
472
Brian Carlstromea46f952013-07-30 01:26:50 -0700473struct ObjectOffsets : public CheckOffsets<mirror::Object> {
474 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700475 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700476 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"));
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700477#ifdef USE_BAKER_OR_BROOKS_READ_BARRIER
478 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_"));
479 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_"));
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800480#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700481 };
482};
483
Brian Carlstromea46f952013-07-30 01:26:50 -0700484struct ArtFieldOffsets : public CheckOffsets<mirror::ArtField> {
485 ArtFieldOffsets() : CheckOffsets<mirror::ArtField>(false, "Ljava/lang/reflect/ArtField;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700486 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, access_flags_), "accessFlags"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, declaring_class_), "declaringClass"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, field_dex_idx_), "fieldDexIndex"));
489 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700490 };
491};
492
Brian Carlstromea46f952013-07-30 01:26:50 -0700493struct ArtMethodOffsets : public CheckOffsets<mirror::ArtMethod> {
494 ArtMethodOffsets() : CheckOffsets<mirror::ArtMethod>(false, "Ljava/lang/reflect/ArtMethod;") {
Vladimir Marko76649e82014-11-10 18:32:59 +0000495 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, access_flags_), "accessFlags"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, declaring_class_), "declaringClass"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_strings_), "dexCacheStrings"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000500 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_code_item_offset_), "dexCodeItemOffset"));
501 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_method_index_), "dexMethodIndex"));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_interpreter_), "entryPointFromInterpreter"));
503 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_jni_), "entryPointFromJni"));
504 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_portable_compiled_code_), "entryPointFromPortableCompiledCode"));
505 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, entry_point_from_quick_compiled_code_), "entryPointFromQuickCompiledCode"));
506 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, gc_map_), "gcMap"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700507 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_index_), "methodIndex"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700508 };
509};
510
Brian Carlstromea46f952013-07-30 01:26:50 -0700511struct ClassOffsets : public CheckOffsets<mirror::Class> {
512 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Vladimir Marko76649e82014-11-10 18:32:59 +0000513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700514 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize"));
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000519 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex"));
520 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700521 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods"));
522 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields"));
523 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable"));
524 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
526 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), "numReferenceStaticFields"));
527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize"));
528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), "referenceInstanceOffsets"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000530 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700531 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000532 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass"));
533 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_), "verifyErrorClass"));
534 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods"));
535 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700536 };
537};
538
Brian Carlstromea46f952013-07-30 01:26:50 -0700539struct StringOffsets : public CheckOffsets<mirror::String> {
540 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700541 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, count_), "count"));
542 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode"));
543 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, offset_), "offset"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000544 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, array_), "value"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 };
546};
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
549 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700550 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause"));
551 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage"));
552 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState"));
553 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace"));
554 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700555 };
556};
557
Brian Carlstromea46f952013-07-30 01:26:50 -0700558struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
559 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700560 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"));
561 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700562 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000563 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700564 };
565};
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
568 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700569 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages"));
570 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent"));
571 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700572 };
573};
574
Brian Carlstromea46f952013-07-30 01:26:50 -0700575struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
576 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"));
Jesse Wilson95caa792011-10-12 18:14:17 -0400578 };
579};
580
Brian Carlstromea46f952013-07-30 01:26:50 -0700581struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
582 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700583 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000584 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location"));
586 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields"));
587 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods"));
588 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes"));
589 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings"));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700590 };
591};
592
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700593struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
594 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700595 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext"));
596 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue"));
597 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext"));
598 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent"));
599 };
600};
601
602struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
603 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(false, "Ljava/lang/ref/FinalizerReference;") {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700604 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next"));
605 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev"));
606 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie"));
607 };
608};
609
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400610// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400611// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700612// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400613TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700615 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstromea46f952013-07-30 01:26:50 -0700616 EXPECT_TRUE(ArtFieldOffsets().Check());
617 EXPECT_TRUE(ArtMethodOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700618 EXPECT_TRUE(ClassOffsets().Check());
619 EXPECT_TRUE(StringOffsets().Check());
620 EXPECT_TRUE(ThrowableOffsets().Check());
621 EXPECT_TRUE(StackTraceElementOffsets().Check());
622 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400623 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700624 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700625 EXPECT_TRUE(ReferenceOffsets().Check());
626 EXPECT_TRUE(FinalizerReferenceOffsets().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());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700637 StackHandleScope<1> hs(soa.Self());
638 Handle<mirror::ClassLoader> class_loader(
639 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700640
Ian Rogers98379392014-02-24 16:53:16 -0800641 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700642 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700643 EXPECT_EQ(0U, outer->NumVirtualMethods());
644 EXPECT_EQ(1U, outer->NumDirectMethods());
645
Ian Rogers98379392014-02-24 16:53:16 -0800646 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700647 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700648 EXPECT_EQ(0U, inner->NumVirtualMethods());
649 EXPECT_EQ(1U, inner->NumDirectMethods());
650}
651
652TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700654 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800655 for (int ch = 1; ch < 256; ++ch) {
656 std::string descriptor;
657 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700658 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700659 AssertNonExistentClass(descriptor);
660 } else {
661 AssertPrimitiveClass(descriptor);
662 }
663 }
664}
665
666TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700667 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800668 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700669 ASSERT_TRUE(JavaLangObject != nullptr);
670 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700671 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
672 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700673 std::string temp;
674 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700675 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700676 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700677 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700679 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700680 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700681 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromea46f952013-07-30 01:26:50 -0700682 EXPECT_TRUE(JavaLangObject->IsVerified());
683 EXPECT_TRUE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700684 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
685 EXPECT_FALSE(JavaLangObject->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700686 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700687 EXPECT_FALSE(JavaLangObject->IsInterface());
688 EXPECT_TRUE(JavaLangObject->IsPublic());
689 EXPECT_FALSE(JavaLangObject->IsFinal());
690 EXPECT_FALSE(JavaLangObject->IsPrimitive());
691 EXPECT_FALSE(JavaLangObject->IsSynthetic());
692 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
693 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700694 if (!kUseBakerOrBrooksReadBarrier) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800695 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
696 } else {
697 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
698 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700699 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_");
700 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_");
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700701 if (kUseBakerOrBrooksReadBarrier) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700702 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_");
703 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800704 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700705
706 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700707 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700708
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700709 StackHandleScope<1> hs(soa.Self());
710 Handle<mirror::ClassLoader> class_loader(
711 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700712 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800713 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700714 ASSERT_TRUE(MyClass != nullptr);
715 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700716 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
717 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700718 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700719 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
720 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700721 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700722 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700723 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700724 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700725 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700726 EXPECT_FALSE(MyClass->IsVerified());
727 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700728 EXPECT_FALSE(MyClass->IsArrayInstance());
729 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700730 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700731 EXPECT_FALSE(MyClass->IsInterface());
732 EXPECT_FALSE(MyClass->IsPublic());
733 EXPECT_FALSE(MyClass->IsFinal());
734 EXPECT_FALSE(MyClass->IsPrimitive());
735 EXPECT_FALSE(MyClass->IsSynthetic());
736 EXPECT_EQ(1U, MyClass->NumDirectMethods());
737 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
738 EXPECT_EQ(0U, MyClass->NumInstanceFields());
739 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700740 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700741
742 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
743
744 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700745 AssertArrayClass("[C", "C", nullptr);
746 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700747 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700748 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700749 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700750 // or not available at all
751 AssertNonExistentClass("[[[[LNonExistentClass;");
752}
753
754TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(Thread::Current());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700756 AssertDexFile(java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700757}
758
Ian Rogersa15e67d2012-02-28 13:51:55 -0800759// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400760// start of the object
761TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700762 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800763 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700764 mirror::ObjectArray<mirror::String>* array =
765 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 uintptr_t data_offset =
767 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
768 0));
769 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800770 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
771 } else {
772 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
773 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400774}
775
776TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700778 StackHandleScope<5> hs(soa.Self());
779 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800780 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800781 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
782 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
783
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700784 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800785 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800786 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
787 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
788
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700789 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800790 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800791 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
792 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
793
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700794 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800795 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800796 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
797 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
798
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700799 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800800 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800801 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
802 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
803
804 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400805}
806
Elliott Hughes33203b52011-09-20 19:42:01 -0700807TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
808 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
809 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700811 NullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700812 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800813 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700814 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800815 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700816 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800817 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700818 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800819 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700820 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800821 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700822 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800823 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700824 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800825 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700826 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800827 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700828 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700829}
830
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700831TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700833 StackHandleScope<2> hs(soa.Self());
834 Handle<mirror::ClassLoader> class_loader_1(
835 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
836 Handle<mirror::ClassLoader> class_loader_2(
837 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800838 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
839 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700840 EXPECT_TRUE(MyClass_1 != nullptr);
841 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700842 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400843}
844
Jesse Wilson7833bd22011-08-09 18:31:44 -0400845TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700847 StackHandleScope<2> hs(soa.Self());
848 Handle<mirror::ClassLoader> class_loader(
849 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
850 Handle<mirror::Class> statics(
851 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700852 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400853
jeffhaoabcfde32011-09-29 15:05:18 -0700854 // Static final primitives that are initialized by a compile-time constant
855 // expression resolve to a copy of a constant value from the constant pool.
856 // So <clinit> should be null.
Brian Carlstromea46f952013-07-30 01:26:50 -0700857 mirror::ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700858 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700859
860 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400861
Mathieu Chartierf8322842014-05-16 10:59:25 -0700862 mirror::ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Ian Rogers1ff3c982014-08-12 02:30:58 -0700863 std::string temp;
864 EXPECT_STREQ(s0->GetClass()->GetDescriptor(&temp), "Ljava/lang/reflect/ArtField;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700865 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700866 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
867 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400868
Mathieu Chartierf8322842014-05-16 10:59:25 -0700869 mirror::ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700870 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700871 EXPECT_EQ(5, s1->GetByte(statics.Get()));
872 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400873
Mathieu Chartierf8322842014-05-16 10:59:25 -0700874 mirror::ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700875 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700876 EXPECT_EQ('a', s2->GetChar(statics.Get()));
877 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400878
Mathieu Chartierf8322842014-05-16 10:59:25 -0700879 mirror::ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700880 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700881 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
882 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400883
Mathieu Chartierf8322842014-05-16 10:59:25 -0700884 mirror::ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700885 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700886 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
887 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400888
Mathieu Chartierf8322842014-05-16 10:59:25 -0700889 mirror::ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700890 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700891 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
892 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400893
Mathieu Chartierf8322842014-05-16 10:59:25 -0700894 mirror::ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700895 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700896 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700897 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400898
Mathieu Chartierf8322842014-05-16 10:59:25 -0700899 mirror::ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700900 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700901 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700902 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400903
Mathieu Chartierf8322842014-05-16 10:59:25 -0700904 mirror::ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
905 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700906 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700907 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100908 s8->SetObject<false>(s8->GetDeclaringClass(),
909 mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400910
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800911 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
912 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700913 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
914 EXPECT_EQ(6, s1->GetByte(statics.Get()));
915 EXPECT_EQ('b', s2->GetChar(statics.Get()));
916 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
917 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
918 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -0700919 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
920 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700921 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400922}
923
Brian Carlstrom30b94452011-08-25 21:35:26 -0700924TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700926 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700927 Handle<mirror::ClassLoader> class_loader(
928 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700929 Handle<mirror::Class> I(
930 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
931 Handle<mirror::Class> J(
932 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
933 Handle<mirror::Class> K(
934 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
935 Handle<mirror::Class> A(
936 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
937 Handle<mirror::Class> B(
938 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
939 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
940 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
941 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
942 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
943 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700944
Ian Rogersd91d6d62013-09-25 20:26:14 -0700945 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
946 mirror::ArtMethod* Ii = I->FindVirtualMethod("i", void_sig);
947 mirror::ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig);
948 mirror::ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig);
949 mirror::ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig);
950 mirror::ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig);
951 mirror::ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig);
952 mirror::ArtMethod* Ai = A->FindVirtualMethod("i", void_sig);
953 mirror::ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig);
954 mirror::ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700955 ASSERT_TRUE(Ii != nullptr);
956 ASSERT_TRUE(Jj1 != nullptr);
957 ASSERT_TRUE(Jj2 != nullptr);
958 ASSERT_TRUE(Kj1 != nullptr);
959 ASSERT_TRUE(Kj2 != nullptr);
960 ASSERT_TRUE(Kk != nullptr);
961 ASSERT_TRUE(Ai != nullptr);
962 ASSERT_TRUE(Aj1 != nullptr);
963 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 EXPECT_NE(Ii, Ai);
965 EXPECT_NE(Jj1, Aj1);
966 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700967 EXPECT_EQ(Kj1, Jj1);
968 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800969 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
970 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
971 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700972 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
973 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
974 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
Ian Rogersd24e2642012-06-06 21:21:43 -0700975
Mathieu Chartierf8322842014-05-16 10:59:25 -0700976 mirror::ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo",
977 "Ljava/lang/String;");
978 mirror::ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo",
979 "Ljava/lang/String;");
980 mirror::ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo",
981 "Ljava/lang/String;");
982 mirror::ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo",
983 "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700984 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -0700985 EXPECT_EQ(Afoo, Bfoo);
986 EXPECT_EQ(Afoo, Jfoo);
987 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700988}
989
Elliott Hughesf3778f62012-01-26 14:14:35 -0800990TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -0700991 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700992
jeffhaoabcfde32011-09-29 15:05:18 -0700993 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
994 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -0700995
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(Thread::Current());
997 jobject jclass_loader = LoadDex("StaticsFromCode");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700998 StackHandleScope<1> hs(soa.Self());
999 Handle<mirror::ClassLoader> class_loader(
1000 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(jclass_loader)[0];
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001002 CHECK(dex_file != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -08001003 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Ian Rogers241b5de2013-10-09 17:58:57 -07001004 mirror::ArtMethod* clinit = klass->FindClassInitializer();
Brian Carlstromea46f952013-07-30 01:26:50 -07001005 mirror::ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001006 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001007 ASSERT_TRUE(string_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001008 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001009 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001010 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001011 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001012 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001013 EXPECT_FALSE(uninit->IsInitialized());
1014 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001015 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001016 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001017}
1018
Elliott Hughes20cde902011-10-04 17:37:27 -07001019TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001021 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001022
1023 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001024 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001025 EXPECT_FALSE(c->IsFinalizable());
1026
1027 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001028 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001029 EXPECT_FALSE(c->IsFinalizable());
1030
1031 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001032 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001033 EXPECT_FALSE(c->IsFinalizable());
1034
1035 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001036 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001037 EXPECT_TRUE(c->IsFinalizable());
1038
1039 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001040 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001041 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001042 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001043 EXPECT_TRUE(c->IsFinalizable());
1044
1045 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1046 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001047 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001048 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001049 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001050 EXPECT_TRUE(c->IsFinalizable());
1051}
1052
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001053TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001055 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001057 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001058 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1059 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001060 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1061 }
1062}
1063
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001064TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1065 ScopedObjectAccess soa(Thread::Current());
1066 NullHandle<mirror::ClassLoader> class_loader;
1067 mirror::Class* c;
1068
1069 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
1070 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize());
1071
1072 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
1073 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize());
1074
1075 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
1076 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize());
1077
1078 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
1079 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize());
1080
1081 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtField;", class_loader);
1082 EXPECT_EQ(c->GetClassSize(), mirror::ArtField::ClassSize());
1083
1084 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtMethod;", class_loader);
1085 EXPECT_EQ(c->GetClassSize(), mirror::ArtMethod::ClassSize());
1086}
1087
Andreas Gampe48498592014-09-10 19:48:05 -07001088static void CheckMethod(mirror::ArtMethod* method, bool verified)
1089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1090 if (!method->IsNative() && !method->IsAbstract()) {
1091 EXPECT_EQ((method->GetAccessFlags() & kAccPreverified) != 0U, verified)
1092 << PrettyMethod(method, true);
1093 }
1094}
1095
1096static void CheckPreverified(mirror::Class* c, bool preverified)
1097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1098 EXPECT_EQ((c->GetAccessFlags() & kAccPreverified) != 0U, preverified)
1099 << "Class " << PrettyClass(c) << " not as expected";
1100 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
1101 CheckMethod(c->GetDirectMethod(i), preverified);
1102 }
1103 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
1104 CheckMethod(c->GetVirtualMethod(i), preverified);
1105 }
1106}
1107
1108TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1109 ScopedObjectAccess soa(Thread::Current());
1110
1111 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001112 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001113 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1114 "core";
1115 CheckPreverified(JavaLangObject, true);
1116}
1117
1118TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1119 ScopedObjectAccess soa(Thread::Current());
1120
1121 StackHandleScope<1> hs(soa.Self());
1122
1123 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1124 soa.Self(), "Ljava/lang/SecurityManager;")));
1125 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1126 "core";
1127
1128 CheckPreverified(security_manager.Get(), false);
1129
Ian Rogers7b078e82014-09-10 14:44:24 -07001130 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001131 CheckPreverified(security_manager.Get(), true);
1132}
1133
1134TEST_F(ClassLinkerTest, Preverified_App) {
1135 ScopedObjectAccess soa(Thread::Current());
1136
1137 StackHandleScope<2> hs(soa.Self());
1138 Handle<mirror::ClassLoader> class_loader(
1139 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1140 Handle<mirror::Class> statics(
1141 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1142
1143 CheckPreverified(statics.Get(), false);
1144
Ian Rogers7b078e82014-09-10 14:44:24 -07001145 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001146 CheckPreverified(statics.Get(), true);
1147}
1148
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001149} // namespace art