blob: 3e727e7408523e2a3af5f6b010fdcaff428dea77 [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 Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/heap.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070027#include "mirror/accessible_object.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"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070033#include "mirror/field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
36#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070037#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#include "scoped_thread_state_change.h"
42#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
45
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080046class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 void AssertNonExistentClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070050 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070051 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070052 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000053 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070054 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080055 mirror::Class* exception_class =
56 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070057 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070058 }
59
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 void AssertPrimitiveClass(const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070061 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080062 Thread* self = Thread::Current();
63 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070064 }
65
Ian Rogersef7d42f2014-01-06 12:55:46 -080066 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Ian Rogersb726dcb2012-09-05 08:57:23 -070067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070068 ASSERT_TRUE(primitive != nullptr);
69 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070071 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070072 std::string temp;
73 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070074 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070075 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070076 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070077 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070079 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070080 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070081 EXPECT_TRUE(primitive->IsVerified());
82 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070083 EXPECT_FALSE(primitive->IsArrayInstance());
84 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070085 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086 EXPECT_FALSE(primitive->IsInterface());
87 EXPECT_TRUE(primitive->IsPublic());
88 EXPECT_TRUE(primitive->IsFinal());
89 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070090 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 EXPECT_EQ(0U, primitive->NumDirectMethods());
92 EXPECT_EQ(0U, primitive->NumVirtualMethods());
93 EXPECT_EQ(0U, primitive->NumInstanceFields());
94 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070095 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -070096 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -070097 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070098 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -070099 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700100 }
101
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102 void AssertArrayClass(const std::string& array_descriptor,
103 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700104 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800106 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 StackHandleScope<2> hs(self);
108 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
109 Handle<mirror::Class> array(
110 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700111 std::string temp;
112 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700113 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700114 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700115 AssertArrayClass(array_descriptor, array);
116 }
117
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700118 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700120 ASSERT_TRUE(array.Get() != nullptr);
121 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700122 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700123 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700124 std::string temp;
125 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700126 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800127 Thread* self = Thread::Current();
128 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700129 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700130 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700131 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700133 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700134 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700135 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700136 EXPECT_TRUE(array->IsVerified());
137 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700138 EXPECT_FALSE(array->IsArrayInstance());
139 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700140 EXPECT_FALSE(array->IsInterface());
141 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
142 EXPECT_TRUE(array->IsFinal());
143 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700144 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700145 EXPECT_EQ(0U, array->NumDirectMethods());
146 EXPECT_EQ(0U, array->NumVirtualMethods());
147 EXPECT_EQ(0U, array->NumInstanceFields());
148 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700149 EXPECT_EQ(2U, array->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700150 EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700151 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700152 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700153 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
154 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700155 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700156 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700157 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700158 mirror::Class* array_ptr = array->GetComponentType();
159 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700160 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700161
Brian Carlstromea46f952013-07-30 01:26:50 -0700162 void AssertMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700163 EXPECT_TRUE(method != nullptr);
164 EXPECT_TRUE(method->GetClass() != nullptr);
165 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700166 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700167
Andreas Gampe58a5af82014-07-31 16:23:49 -0700168 EXPECT_TRUE(method->HasDexCacheResolvedMethods());
169 EXPECT_TRUE(method->HasDexCacheResolvedTypes());
Andreas Gampe58a5af82014-07-31 16:23:49 -0700170 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
171 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods()));
172 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
173 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes()));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700174 }
175
Brian Carlstromea46f952013-07-30 01:26:50 -0700176 void AssertField(mirror::Class* klass, mirror::ArtField* field)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700178 EXPECT_TRUE(field != nullptr);
179 EXPECT_TRUE(field->GetClass() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700180 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700181 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700182 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700183 }
184
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700185 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700187 std::string temp;
188 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700190 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700191 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700192 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700193 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700194 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700195 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700196 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700197 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700198 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700199 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700200 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700202 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800204 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
205 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700206 std::string temp2;
207 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
208 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700209 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700210 EXPECT_TRUE(klass->IsAbstract());
211 if (klass->NumDirectMethods() == 1) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700212 EXPECT_TRUE(klass->GetDirectMethod(0)->IsClassInitializer());
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700213 EXPECT_TRUE(klass->GetDirectMethod(0)->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700214 } else {
215 EXPECT_EQ(0U, klass->NumDirectMethods());
216 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700217 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 if (!klass->IsSynthetic()) {
219 EXPECT_NE(0U, klass->NumDirectMethods());
220 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700221 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700222 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700224 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700225 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700226 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700227 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700228 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700229 } else {
Ian Rogers9bc81912012-10-11 21:43:36 -0700230 EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700231 }
232 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700233 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700234 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700235 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700236 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700237 }
238 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700239 EXPECT_FALSE(klass->IsAbstract());
240 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700241 }
242 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700243 EXPECT_FALSE(klass->IsFinal());
244 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700245 }
246
247 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700248 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700249
250 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700251 mirror::ArtMethod* method = klass->GetDirectMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700252 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700253 EXPECT_TRUE(method->IsDirect());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700254 EXPECT_EQ(klass.Get(), method->GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700255 }
256
257 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700258 mirror::ArtMethod* method = klass->GetVirtualMethod(i);
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700259 AssertMethod(method);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700260 EXPECT_FALSE(method->IsDirect());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700261 EXPECT_TRUE(method->GetDeclaringClass()->IsAssignableFrom(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700262 }
263
264 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700265 mirror::ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700267 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700268 }
269
270 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700271 mirror::ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700272 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700273 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700274 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700275
Vladimir Marko76649e82014-11-10 18:32:59 +0000276 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700278 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700279 MutableHandle<mirror::ArtField> fhandle = hs.NewHandle<mirror::ArtField>(nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000280 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
281 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
282 klass->NumReferenceInstanceFields() *
283 sizeof(mirror::HeapReference<mirror::Object>));
284 MemberOffset current_ref_offset = start_ref_offset;
285 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700286 mirror::ArtField* field = klass->GetInstanceField(i);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700287 fhandle.Assign(field);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700288 mirror::Class* field_type = fhandle->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700289 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000290 if (!field->IsPrimitiveType()) {
291 ASSERT_TRUE(!field_type->IsPrimitive());
292 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
293 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
294 // While Reference.referent is not primitive, the ClassLinker
295 // treats it as such so that the garbage collector won't scan it.
Ian Rogers08f1f502014-12-02 15:04:37 -0800296 EXPECT_EQ(PrettyField(fhandle.Get()),
Vladimir Marko76649e82014-11-10 18:32:59 +0000297 "java.lang.Object java.lang.ref.Reference.referent");
298 } else {
299 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
300 sizeof(mirror::HeapReference<mirror::Object>));
301 }
302 } else {
303 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
304 // Shuffled before references.
305 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
306 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
307 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700308 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000310 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700312 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700313 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700314 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
316 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700317 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700318 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
319 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
320 // The reference instance offsets have a bit set for each reference offset.
321 // +1 for Object's class.
322 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
323 total_num_reference_instance_fields);
324 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700325 }
326
Brian Carlstromea46f952013-07-30 01:26:50 -0700327 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700329 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800330 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700331 StackHandleScope<1> hs(self);
332 Handle<mirror::Class> klass(
333 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
334 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700335 std::string temp;
336 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700337 EXPECT_EQ(class_loader, klass->GetClassLoader());
338 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700339 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700340 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700341 AssertArrayClass(descriptor, klass);
342 } else {
343 AssertClass(descriptor, klass);
344 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700345 }
346
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800347 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700349 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800350 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
351 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
352 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700353 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700354 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700355 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800356 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
357 const DexFile::TypeId& type_id = dex.GetTypeId(i);
358 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700359 AssertDexFileClass(class_loader, descriptor);
360 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700361 class_linker_->VisitRoots(TestRootVisitor, nullptr, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800362 // Verify the dex cache has resolution methods in all resolved method slots
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800363 mirror::DexCache* dex_cache = class_linker_->FindDexCache(dex);
Brian Carlstromea46f952013-07-30 01:26:50 -0700364 mirror::ObjectArray<mirror::ArtMethod>* resolved_methods = dex_cache->GetResolvedMethods();
Ian Rogers19846512012-02-24 11:42:47 -0800365 for (size_t i = 0; i < static_cast<size_t>(resolved_methods->GetLength()); i++) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800366 EXPECT_TRUE(resolved_methods->Get(i) != nullptr) << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800367 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700368 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700369
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800370 static void TestRootVisitor(mirror::Object** root, void*, const RootInfo&) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700371 EXPECT_TRUE(*root != nullptr);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700372 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700373};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700374
Brian Carlstrom693267a2011-09-06 09:25:34 -0700375struct CheckOffset {
376 size_t cpp_offset;
377 const char* java_name;
378 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
379};
380
Elliott Hughes80609252011-09-23 17:24:51 -0700381template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700382struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800383 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
384 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700385 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700386 std::string class_descriptor;
387 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700388
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 bool Check() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800390 Thread* self = Thread::Current();
391 mirror::Class* klass =
392 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700393 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700394
395 bool error = false;
396
Mathieu Chartiereace4582014-11-24 18:29:54 -0800397 // Art method have a different size due to the padding field.
398 if (!klass->IsArtMethodClass() && !klass->IsClassClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700399 // Currently only required for AccessibleObject since of the padding fields. The class linker
400 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
401 // The RoundUp is to get around this case.
402 static constexpr size_t kPackAlignment = 4;
403 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
404 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700405 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800406 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700407 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700408 << " Java=" << expected_size
409 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700410 error = true;
411 }
412 }
413
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700414 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700415 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800416 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700417 << " class=" << class_descriptor
418 << " Java=" << num_fields
419 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700420 error = true;
421 }
422
Brian Carlstromdbc05252011-09-09 01:59:59 -0700423 for (size_t i = 0; i < offsets.size(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700424 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700425 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700427 error = true;
428 }
429 }
430 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700431 for (size_t i = 0; i < offsets.size(); i++) {
432 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700433 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700434 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800436 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700437 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800438 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700439 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700441 << " CheckOffsets=" << offset.java_name;
442 }
443 }
444
Brian Carlstromdbc05252011-09-09 01:59:59 -0700445 for (size_t i = 0; i < offsets.size(); i++) {
446 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700448 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
449 error = true;
450 }
451 }
452 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700453 for (size_t i = 0; i < offsets.size(); i++) {
454 CheckOffset& offset = offsets[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700455 mirror::ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700456 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800457 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700458 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800459 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700460 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
461 }
462 }
463
464 return !error;
465 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700466
467 private:
468 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700469};
470
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700471// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
472// is first since if it is failing, others are unlikely to succeed.
473
Brian Carlstromea46f952013-07-30 01:26:50 -0700474struct ObjectOffsets : public CheckOffsets<mirror::Object> {
475 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700476 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700477 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_"));
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700478#ifdef USE_BAKER_OR_BROOKS_READ_BARRIER
479 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_"));
480 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_"));
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800481#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700482 };
483};
484
Brian Carlstromea46f952013-07-30 01:26:50 -0700485struct ArtFieldOffsets : public CheckOffsets<mirror::ArtField> {
486 ArtFieldOffsets() : CheckOffsets<mirror::ArtField>(false, "Ljava/lang/reflect/ArtField;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700487 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, access_flags_), "accessFlags"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000488 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, declaring_class_), "declaringClass"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, field_dex_idx_), "fieldDexIndex"));
490 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtField, offset_), "offset"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700491 };
492};
493
Brian Carlstromea46f952013-07-30 01:26:50 -0700494struct ArtMethodOffsets : public CheckOffsets<mirror::ArtMethod> {
495 ArtMethodOffsets() : CheckOffsets<mirror::ArtMethod>(false, "Ljava/lang/reflect/ArtMethod;") {
Vladimir Marko76649e82014-11-10 18:32:59 +0000496 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, access_flags_), "accessFlags"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700497 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, declaring_class_), "declaringClass"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700498 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_methods_), "dexCacheResolvedMethods"));
499 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, dex_cache_resolved_types_), "dexCacheResolvedTypes"));
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"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700502 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ArtMethod, method_index_), "methodIndex"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700503 };
504};
505
Brian Carlstromea46f952013-07-30 01:26:50 -0700506struct ClassOffsets : public CheckOffsets<mirror::Class> {
507 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Vladimir Marko76649e82014-11-10 18:32:59 +0000508 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700509 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000510 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize"));
511 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType"));
513 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache"));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800514 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000515 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex"));
516 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods"));
518 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields"));
519 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable"));
520 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700521 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_), "numReferenceInstanceFields"));
522 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_), "numReferenceStaticFields"));
523 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize"));
524 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType"));
525 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_), "referenceInstanceOffsets"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000526 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700527 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000528 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass"));
529 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_class_), "verifyErrorClass"));
530 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods"));
531 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700532 };
533};
534
Brian Carlstromea46f952013-07-30 01:26:50 -0700535struct StringOffsets : public CheckOffsets<mirror::String> {
536 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700537 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, count_), "count"));
538 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode"));
539 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, offset_), "offset"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000540 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::String, array_), "value"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541 };
542};
543
Brian Carlstromea46f952013-07-30 01:26:50 -0700544struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
545 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700546 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause"));
547 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage"));
548 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState"));
549 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace"));
550 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551 };
552};
553
Brian Carlstromea46f952013-07-30 01:26:50 -0700554struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
555 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(false, "Ljava/lang/StackTraceElement;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass"));
557 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700558 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000559 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700560 };
561};
562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
564 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700565 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages"));
566 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent"));
567 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache"));
Brian Carlstrom693267a2011-09-06 09:25:34 -0700568 };
569};
570
Brian Carlstromea46f952013-07-30 01:26:50 -0700571struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
572 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h"));
Jesse Wilson95caa792011-10-12 18:14:17 -0400574 };
575};
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
578 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700579 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex"));
Vladimir Marko76649e82014-11-10 18:32:59 +0000580 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile"));
Brian Carlstromea46f952013-07-30 01:26:50 -0700581 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location"));
582 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields"));
583 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods"));
584 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes"));
585 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings"));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700586 };
587};
588
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700589struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
590 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700591 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext"));
592 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue"));
593 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext"));
594 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent"));
595 };
596};
597
598struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
599 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(false, "Ljava/lang/ref/FinalizerReference;") {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700600 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next"));
601 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev"));
602 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie"));
603 };
604};
605
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700606struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
607 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(false, "Ljava/lang/reflect/AccessibleObject;") {
608 offsets.push_back(CheckOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "flag"));
609 };
610};
611
612struct FieldOffsets : public CheckOffsets<mirror::Field> {
613 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
614 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags"));
615 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass"));
616 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex"));
617 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset"));
618 offsets.push_back(CheckOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type"));
619 };
620};
621
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400622// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400623// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700624// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400625TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700627 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstromea46f952013-07-30 01:26:50 -0700628 EXPECT_TRUE(ArtFieldOffsets().Check());
629 EXPECT_TRUE(ArtMethodOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700630 EXPECT_TRUE(ClassOffsets().Check());
631 EXPECT_TRUE(StringOffsets().Check());
632 EXPECT_TRUE(ThrowableOffsets().Check());
633 EXPECT_TRUE(StackTraceElementOffsets().Check());
634 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400635 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700636 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700637 EXPECT_TRUE(ReferenceOffsets().Check());
638 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700639 EXPECT_TRUE(AccessibleObjectOffsets().Check());
640 EXPECT_TRUE(FieldOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700641}
642
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700643TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700645 AssertNonExistentClass("NoSuchClass;");
646 AssertNonExistentClass("LNoSuchClass;");
647}
648
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700649TEST_F(ClassLinkerTest, GetDexFiles) {
650 ScopedObjectAccess soa(Thread::Current());
651
652 jobject jclass_loader = LoadDex("Nested");
653 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
654 ASSERT_EQ(dex_files.size(), 1U);
655 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
656
657 jobject jclass_loader2 = LoadDex("MultiDex");
658 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
659 ASSERT_EQ(dex_files2.size(), 2U);
660 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
661}
662
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700663TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700665 StackHandleScope<1> hs(soa.Self());
666 Handle<mirror::ClassLoader> class_loader(
667 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700668
Ian Rogers98379392014-02-24 16:53:16 -0800669 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700670 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700671 EXPECT_EQ(0U, outer->NumVirtualMethods());
672 EXPECT_EQ(1U, outer->NumDirectMethods());
673
Ian Rogers98379392014-02-24 16:53:16 -0800674 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700675 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700676 EXPECT_EQ(0U, inner->NumVirtualMethods());
677 EXPECT_EQ(1U, inner->NumDirectMethods());
678}
679
680TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800683 for (int ch = 1; ch < 256; ++ch) {
684 std::string descriptor;
685 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700686 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700687 AssertNonExistentClass(descriptor);
688 } else {
689 AssertPrimitiveClass(descriptor);
690 }
691 }
692}
693
694TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800696 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700697 ASSERT_TRUE(JavaLangObject != nullptr);
698 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700699 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
700 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700701 std::string temp;
702 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700703 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700704 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700705 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700706 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700707 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700708 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700709 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromea46f952013-07-30 01:26:50 -0700710 EXPECT_TRUE(JavaLangObject->IsVerified());
711 EXPECT_TRUE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700712 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
713 EXPECT_FALSE(JavaLangObject->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700714 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700715 EXPECT_FALSE(JavaLangObject->IsInterface());
716 EXPECT_TRUE(JavaLangObject->IsPublic());
717 EXPECT_FALSE(JavaLangObject->IsFinal());
718 EXPECT_FALSE(JavaLangObject->IsPrimitive());
719 EXPECT_FALSE(JavaLangObject->IsSynthetic());
720 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
721 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700722 if (!kUseBakerOrBrooksReadBarrier) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800723 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
724 } else {
725 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
726 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700727 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_");
728 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_");
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700729 if (kUseBakerOrBrooksReadBarrier) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700730 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_");
731 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800732 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700733
734 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700735 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700736
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700737 StackHandleScope<1> hs(soa.Self());
738 Handle<mirror::ClassLoader> class_loader(
739 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700740 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800741 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700742 ASSERT_TRUE(MyClass != nullptr);
743 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700744 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
745 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700746 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700747 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
748 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700749 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700750 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700751 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700752 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700753 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700754 EXPECT_FALSE(MyClass->IsVerified());
755 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700756 EXPECT_FALSE(MyClass->IsArrayInstance());
757 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700758 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700759 EXPECT_FALSE(MyClass->IsInterface());
760 EXPECT_FALSE(MyClass->IsPublic());
761 EXPECT_FALSE(MyClass->IsFinal());
762 EXPECT_FALSE(MyClass->IsPrimitive());
763 EXPECT_FALSE(MyClass->IsSynthetic());
764 EXPECT_EQ(1U, MyClass->NumDirectMethods());
765 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
766 EXPECT_EQ(0U, MyClass->NumInstanceFields());
767 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700768 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700769
770 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
771
772 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700773 AssertArrayClass("[C", "C", nullptr);
774 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700775 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700776 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700777 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700778 // or not available at all
779 AssertNonExistentClass("[[[[LNonExistentClass;");
780}
781
782TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800784 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
785 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700786}
787
Ian Rogersa15e67d2012-02-28 13:51:55 -0800788// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400789// start of the object
790TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700791 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800792 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700793 mirror::ObjectArray<mirror::String>* array =
794 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800795 uintptr_t data_offset =
796 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
797 0));
798 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800799 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
800 } else {
801 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
802 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400803}
804
805TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700807 StackHandleScope<5> hs(soa.Self());
808 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800809 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800810 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
811 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
812
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700813 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800814 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800815 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
816 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
817
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700818 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800819 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800820 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
821 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
822
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700823 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800824 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800825 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
826 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
827
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700828 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800829 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800830 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
831 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
832
833 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400834}
835
Elliott Hughes33203b52011-09-20 19:42:01 -0700836TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
837 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
838 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700840 NullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700841 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800842 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700843 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800844 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700845 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800846 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700847 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800848 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700849 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800850 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700851 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800852 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700853 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800854 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700855 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800856 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700857 EXPECT_STREQ("value", c->GetIFields()->Get(0)->GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700858}
859
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700860TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700862 StackHandleScope<2> hs(soa.Self());
863 Handle<mirror::ClassLoader> class_loader_1(
864 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
865 Handle<mirror::ClassLoader> class_loader_2(
866 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800867 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
868 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700869 EXPECT_TRUE(MyClass_1 != nullptr);
870 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700871 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400872}
873
Jesse Wilson7833bd22011-08-09 18:31:44 -0400874TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700876 StackHandleScope<2> hs(soa.Self());
877 Handle<mirror::ClassLoader> class_loader(
878 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
879 Handle<mirror::Class> statics(
880 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700881 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400882
jeffhaoabcfde32011-09-29 15:05:18 -0700883 // Static final primitives that are initialized by a compile-time constant
884 // expression resolve to a copy of a constant value from the constant pool.
885 // So <clinit> should be null.
Brian Carlstromea46f952013-07-30 01:26:50 -0700886 mirror::ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700887 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700888
889 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400890
Mathieu Chartierf8322842014-05-16 10:59:25 -0700891 mirror::ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Ian Rogers1ff3c982014-08-12 02:30:58 -0700892 std::string temp;
893 EXPECT_STREQ(s0->GetClass()->GetDescriptor(&temp), "Ljava/lang/reflect/ArtField;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700894 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700895 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
896 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400897
Mathieu Chartierf8322842014-05-16 10:59:25 -0700898 mirror::ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700899 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700900 EXPECT_EQ(5, s1->GetByte(statics.Get()));
901 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400902
Mathieu Chartierf8322842014-05-16 10:59:25 -0700903 mirror::ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700904 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700905 EXPECT_EQ('a', s2->GetChar(statics.Get()));
906 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400907
Mathieu Chartierf8322842014-05-16 10:59:25 -0700908 mirror::ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700909 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700910 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
911 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400912
Mathieu Chartierf8322842014-05-16 10:59:25 -0700913 mirror::ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700914 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700915 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
916 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400917
Mathieu Chartierf8322842014-05-16 10:59:25 -0700918 mirror::ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700919 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700920 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
921 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400922
Mathieu Chartierf8322842014-05-16 10:59:25 -0700923 mirror::ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700924 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700925 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700926 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400927
Mathieu Chartierf8322842014-05-16 10:59:25 -0700928 mirror::ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700929 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700930 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700931 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400932
Mathieu Chartierf8322842014-05-16 10:59:25 -0700933 mirror::ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
934 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700935 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700936 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100937 s8->SetObject<false>(s8->GetDeclaringClass(),
938 mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400939
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800940 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
941 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700942 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
943 EXPECT_EQ(6, s1->GetByte(statics.Get()));
944 EXPECT_EQ('b', s2->GetChar(statics.Get()));
945 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
946 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
947 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -0700948 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
949 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700950 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400951}
952
Brian Carlstrom30b94452011-08-25 21:35:26 -0700953TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700955 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700956 Handle<mirror::ClassLoader> class_loader(
957 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700958 Handle<mirror::Class> I(
959 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
960 Handle<mirror::Class> J(
961 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
962 Handle<mirror::Class> K(
963 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
964 Handle<mirror::Class> A(
965 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
966 Handle<mirror::Class> B(
967 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
968 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
969 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
970 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
971 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
972 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973
Ian Rogersd91d6d62013-09-25 20:26:14 -0700974 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
975 mirror::ArtMethod* Ii = I->FindVirtualMethod("i", void_sig);
976 mirror::ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig);
977 mirror::ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig);
978 mirror::ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig);
979 mirror::ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig);
980 mirror::ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig);
981 mirror::ArtMethod* Ai = A->FindVirtualMethod("i", void_sig);
982 mirror::ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig);
983 mirror::ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700984 ASSERT_TRUE(Ii != nullptr);
985 ASSERT_TRUE(Jj1 != nullptr);
986 ASSERT_TRUE(Jj2 != nullptr);
987 ASSERT_TRUE(Kj1 != nullptr);
988 ASSERT_TRUE(Kj2 != nullptr);
989 ASSERT_TRUE(Kk != nullptr);
990 ASSERT_TRUE(Ai != nullptr);
991 ASSERT_TRUE(Aj1 != nullptr);
992 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700993 EXPECT_NE(Ii, Ai);
994 EXPECT_NE(Jj1, Aj1);
995 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -0700996 EXPECT_EQ(Kj1, Jj1);
997 EXPECT_EQ(Kj2, Jj2);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800998 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii));
999 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1));
1000 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001001 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii));
1002 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1));
1003 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2));
Ian Rogersd24e2642012-06-06 21:21:43 -07001004
Mathieu Chartierf8322842014-05-16 10:59:25 -07001005 mirror::ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo",
1006 "Ljava/lang/String;");
1007 mirror::ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo",
1008 "Ljava/lang/String;");
1009 mirror::ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo",
1010 "Ljava/lang/String;");
1011 mirror::ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo",
1012 "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001013 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001014 EXPECT_EQ(Afoo, Bfoo);
1015 EXPECT_EQ(Afoo, Jfoo);
1016 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017}
1018
Elliott Hughesf3778f62012-01-26 14:14:35 -08001019TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001020 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001021
jeffhaoabcfde32011-09-29 15:05:18 -07001022 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1023 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001024
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(Thread::Current());
1026 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001027 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001028 StackHandleScope<1> hs(soa.Self());
1029 Handle<mirror::ClassLoader> class_loader(
1030 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001031 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Ian Rogers241b5de2013-10-09 17:58:57 -07001032 mirror::ArtMethod* clinit = klass->FindClassInitializer();
Brian Carlstromea46f952013-07-30 01:26:50 -07001033 mirror::ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;");
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001034 const DexFile::StringId* string_id = dex_file->FindStringId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001035 ASSERT_TRUE(string_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001036 const DexFile::TypeId* type_id = dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001037 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001038 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001039 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, Thread::Current(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001040 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001041 EXPECT_FALSE(uninit->IsInitialized());
1042 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, Thread::Current(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001043 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001044 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001045}
1046
Elliott Hughes20cde902011-10-04 17:37:27 -07001047TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001049 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001050
1051 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001052 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001053 EXPECT_FALSE(c->IsFinalizable());
1054
1055 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001056 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001057 EXPECT_FALSE(c->IsFinalizable());
1058
1059 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001060 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001061 EXPECT_FALSE(c->IsFinalizable());
1062
1063 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001064 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001065 EXPECT_TRUE(c->IsFinalizable());
1066
1067 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001068 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001069 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001070 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001071 EXPECT_TRUE(c->IsFinalizable());
1072
1073 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1074 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001075 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001076 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001077 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001078 EXPECT_TRUE(c->IsFinalizable());
1079}
1080
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001081TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001083 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001084 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001085 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001086 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1087 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001088 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1089 }
1090}
1091
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001092TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1093 ScopedObjectAccess soa(Thread::Current());
1094 NullHandle<mirror::ClassLoader> class_loader;
1095 mirror::Class* c;
1096
1097 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
1098 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize());
1099
1100 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
1101 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize());
1102
1103 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
1104 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize());
1105
1106 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
1107 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize());
1108
1109 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtField;", class_loader);
1110 EXPECT_EQ(c->GetClassSize(), mirror::ArtField::ClassSize());
1111
1112 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/reflect/ArtMethod;", class_loader);
1113 EXPECT_EQ(c->GetClassSize(), mirror::ArtMethod::ClassSize());
1114}
1115
Andreas Gampe48498592014-09-10 19:48:05 -07001116static void CheckMethod(mirror::ArtMethod* method, bool verified)
1117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1118 if (!method->IsNative() && !method->IsAbstract()) {
1119 EXPECT_EQ((method->GetAccessFlags() & kAccPreverified) != 0U, verified)
1120 << PrettyMethod(method, true);
1121 }
1122}
1123
1124static void CheckPreverified(mirror::Class* c, bool preverified)
1125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1126 EXPECT_EQ((c->GetAccessFlags() & kAccPreverified) != 0U, preverified)
1127 << "Class " << PrettyClass(c) << " not as expected";
1128 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
1129 CheckMethod(c->GetDirectMethod(i), preverified);
1130 }
1131 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
1132 CheckMethod(c->GetVirtualMethod(i), preverified);
1133 }
1134}
1135
1136TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1137 ScopedObjectAccess soa(Thread::Current());
1138
1139 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001140 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001141 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1142 "core";
1143 CheckPreverified(JavaLangObject, true);
1144}
1145
1146TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1147 ScopedObjectAccess soa(Thread::Current());
1148
1149 StackHandleScope<1> hs(soa.Self());
1150
1151 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1152 soa.Self(), "Ljava/lang/SecurityManager;")));
1153 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1154 "core";
1155
1156 CheckPreverified(security_manager.Get(), false);
1157
Ian Rogers7b078e82014-09-10 14:44:24 -07001158 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001159 CheckPreverified(security_manager.Get(), true);
1160}
1161
1162TEST_F(ClassLinkerTest, Preverified_App) {
1163 ScopedObjectAccess soa(Thread::Current());
1164
1165 StackHandleScope<2> hs(soa.Self());
1166 Handle<mirror::ClassLoader> class_loader(
1167 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1168 Handle<mirror::Class> statics(
1169 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1170
1171 CheckPreverified(statics.Get(), false);
1172
Ian Rogers7b078e82014-09-10 14:44:24 -07001173 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001174 CheckPreverified(statics.Get(), true);
1175}
1176
Sebastien Hertz6963e442014-11-26 22:11:27 +01001177TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1178 ScopedObjectAccess soa(Thread::Current());
1179
1180 StackHandleScope<3> hs(soa.Self());
1181 Handle<mirror::ClassLoader> class_loader(
1182 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1183
1184 // java.lang.Object is a bootstrap class.
1185 Handle<mirror::Class> jlo_class(
1186 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1187 ASSERT_TRUE(jlo_class.Get() != nullptr);
1188 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1189
1190 // Statics is not a bootstrap class.
1191 Handle<mirror::Class> statics(
1192 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1193 ASSERT_TRUE(statics.Get() != nullptr);
1194 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1195}
1196
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001197} // namespace art