blob: e0ff3ddc0fac721f204d052e7b215fd6780c31c2 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "dex_file.h"
Alex Light705ad492015-09-21 11:36:30 -070028#include "experimental_flags.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/heap.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070031#include "mirror/abstract_method.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070032#include "mirror/accessible_object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class-inl.h"
34#include "mirror/dex_cache.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010035#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070036#include "mirror/field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070040#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070044#include "scoped_thread_state_change.h"
45#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
48
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080049class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070050 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070053 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070054 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070055 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000056 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070057 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080058 mirror::Class* exception_class =
59 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070060 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061 }
62
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080065 Thread* self = Thread::Current();
66 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070067 }
68
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070071 ASSERT_TRUE(primitive != nullptr);
72 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070073 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070074 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070075 std::string temp;
76 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070077 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070079 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070080 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070082 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070083 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070084 EXPECT_TRUE(primitive->IsVerified());
85 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070086 EXPECT_FALSE(primitive->IsArrayInstance());
87 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070088 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070089 EXPECT_FALSE(primitive->IsInterface());
90 EXPECT_TRUE(primitive->IsPublic());
91 EXPECT_TRUE(primitive->IsFinal());
92 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070093 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070094 EXPECT_EQ(0U, primitive->NumDirectMethods());
95 EXPECT_EQ(0U, primitive->NumVirtualMethods());
96 EXPECT_EQ(0U, primitive->NumInstanceFields());
97 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070098 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -070099 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700100 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700101 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -0700102 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103 }
104
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000105 void AssertObjectClass(mirror::Class* JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000107 ASSERT_TRUE(JavaLangObject != nullptr);
108 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
109 ASSERT_EQ(JavaLangObject->GetClass(),
110 JavaLangObject->GetClass()->GetClass());
111 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
112 std::string temp;
113 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
114 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
115 EXPECT_FALSE(JavaLangObject->HasSuperClass());
116 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
117 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
118 EXPECT_FALSE(JavaLangObject->IsErroneous());
119 EXPECT_TRUE(JavaLangObject->IsLoaded());
120 EXPECT_TRUE(JavaLangObject->IsResolved());
121 EXPECT_TRUE(JavaLangObject->IsVerified());
122 EXPECT_TRUE(JavaLangObject->IsInitialized());
123 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
124 EXPECT_FALSE(JavaLangObject->IsArrayClass());
125 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
126 EXPECT_FALSE(JavaLangObject->IsInterface());
127 EXPECT_TRUE(JavaLangObject->IsPublic());
128 EXPECT_FALSE(JavaLangObject->IsFinal());
129 EXPECT_FALSE(JavaLangObject->IsPrimitive());
130 EXPECT_FALSE(JavaLangObject->IsSynthetic());
131 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
132 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
133 if (!kUseBrooksReadBarrier) {
134 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
135 } else {
136 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
137 }
138 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
139 "shadow$_klass_");
140 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
141 "shadow$_monitor_");
142 if (kUseBrooksReadBarrier) {
143 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
144 "shadow$_x_rb_ptr_");
145 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
146 "shadow$_x_xpadding_");
147 }
148
149 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
150 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
151
Andreas Gampe542451c2016-07-26 09:02:02 -0700152 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000153 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
154 ImTable* imt = JavaLangObject->GetImt(pointer_size);
155 ASSERT_NE(nullptr, imt);
156 for (size_t i = 0; i < ImTable::kSize; ++i) {
157 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
158 }
159 }
160
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 void AssertArrayClass(const std::string& array_descriptor,
162 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700163 mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700164 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800165 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 StackHandleScope<2> hs(self);
167 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
168 Handle<mirror::Class> array(
169 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700170 std::string temp;
171 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700172 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700173 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700174 AssertArrayClass(array_descriptor, array);
175 }
176
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700177 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700179 ASSERT_TRUE(array.Get() != nullptr);
180 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700181 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700182 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700183 std::string temp;
184 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700185 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800186 Thread* self = Thread::Current();
187 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700188 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700189 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700191 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700192 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700193 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700194 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700195 EXPECT_TRUE(array->IsVerified());
196 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700197 EXPECT_FALSE(array->IsArrayInstance());
198 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700199 EXPECT_FALSE(array->IsInterface());
200 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
201 EXPECT_TRUE(array->IsFinal());
202 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700203 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700204 EXPECT_EQ(0U, array->NumDirectMethods());
205 EXPECT_EQ(0U, array->NumVirtualMethods());
206 EXPECT_EQ(0U, array->NumInstanceFields());
207 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700208 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000209 EXPECT_TRUE(array->ShouldHaveImt());
210 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700211 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700212 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
214 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700216 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700218 mirror::Class* array_ptr = array->GetComponentType();
219 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000220
Andreas Gampe542451c2016-07-26 09:02:02 -0700221 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000222 mirror::Class* JavaLangObject =
223 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
224 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
225 // IMT of a array class should be shared with the IMT of the java.lag.Object
226 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700227 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700230 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700232 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700233 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700234
Andreas Gampe542451c2016-07-26 09:02:02 -0700235 EXPECT_TRUE(method->HasDexCacheResolvedMethods(kRuntimePointerSize));
236 EXPECT_TRUE(method->HasDexCacheResolvedTypes(kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700237 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100238 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700239 kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700240 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
Vladimir Marko05792b92015-08-03 11:56:49 +0100241 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700242 kRuntimePointerSize));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700243 }
244
Mathieu Chartierc7853442015-03-27 14:35:38 -0700245 void AssertField(mirror::Class* klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700247 EXPECT_TRUE(field != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700248 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700249 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700250 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700251 }
252
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700253 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 std::string temp;
256 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700258 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700259 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700260 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700261 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700262 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700263 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700264 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700265 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700266 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700267 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700268 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700270 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700271 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800272 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
273 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700274 std::string temp2;
275 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
276 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700278 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800279 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700280 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800281 EXPECT_TRUE(m.IsStatic());
282 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700283 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700284 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700285 if (!klass->IsSynthetic()) {
286 EXPECT_NE(0U, klass->NumDirectMethods());
287 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700288 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700289 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800290 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700291 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700292 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700293 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700294 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700295 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700296 } else {
Alex Light705ad492015-09-21 11:36:30 -0700297 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700298 }
299 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700300 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700301 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700302 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700303 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700304 }
305 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700306 EXPECT_FALSE(klass->IsAbstract());
307 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700308 }
309 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 EXPECT_FALSE(klass->IsFinal());
311 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700312 }
313
314 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700315 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700316
Andreas Gampe542451c2016-07-26 09:02:02 -0700317 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 AssertMethod(&method);
319 EXPECT_TRUE(method.IsDirect());
320 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700321 }
322
Andreas Gampe542451c2016-07-26 09:02:02 -0700323 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 AssertMethod(&method);
325 EXPECT_FALSE(method.IsDirect());
Alex Lighte64300b2015-12-15 15:02:47 -0800326 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
327 }
328
Andreas Gampe542451c2016-07-26 09:02:02 -0700329 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800330 AssertMethod(&method);
331 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800332 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800333 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
334 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
335 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
336 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700337 }
338
339 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700340 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700341 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700342 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700343 }
344
345 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700346 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700347 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700348 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700349 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700350
Vladimir Marko76649e82014-11-10 18:32:59 +0000351 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700352 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000353 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
354 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
355 klass->NumReferenceInstanceFields() *
356 sizeof(mirror::HeapReference<mirror::Object>));
357 MemberOffset current_ref_offset = start_ref_offset;
358 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700359 ArtField* field = klass->GetInstanceField(i);
360 mirror::Class* field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700361 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000362 if (!field->IsPrimitiveType()) {
363 ASSERT_TRUE(!field_type->IsPrimitive());
364 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
365 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
366 // While Reference.referent is not primitive, the ClassLinker
367 // treats it as such so that the garbage collector won't scan it.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700368 EXPECT_EQ(PrettyField(field),
Vladimir Marko76649e82014-11-10 18:32:59 +0000369 "java.lang.Object java.lang.ref.Reference.referent");
370 } else {
371 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
372 sizeof(mirror::HeapReference<mirror::Object>));
373 }
374 } else {
375 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
376 // Shuffled before references.
377 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
378 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
379 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700380 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700381 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000382 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700383
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700384 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700385 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700386 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700387 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
388 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700389 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700390 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
391 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
392 // The reference instance offsets have a bit set for each reference offset.
393 // +1 for Object's class.
394 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
395 total_num_reference_instance_fields);
396 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700397 }
398
Brian Carlstromea46f952013-07-30 01:26:50 -0700399 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700400 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700401 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800402 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700403 StackHandleScope<1> hs(self);
404 Handle<mirror::Class> klass(
405 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
406 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700407 std::string temp;
408 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700409 EXPECT_EQ(class_loader, klass->GetClassLoader());
410 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700411 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700412 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700413 AssertArrayClass(descriptor, klass);
414 } else {
415 AssertClass(descriptor, klass);
416 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700417 }
418
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800419 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700420 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800422 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
423 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
424 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700425 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700426 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700427 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800428 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
429 const DexFile::TypeId& type_id = dex.GetTypeId(i);
430 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700431 AssertDexFileClass(class_loader, descriptor);
432 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700433 TestRootVisitor visitor;
434 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800435 // Verify the dex cache has resolution methods in all resolved method slots
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700436 mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100438 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
439 EXPECT_TRUE(
Andreas Gampe542451c2016-07-26 09:02:02 -0700440 mirror::DexCache::GetElementPtrSize(resolved_methods, i, kRuntimePointerSize) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800442 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700443 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700444
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700445 class TestRootVisitor : public SingleRootVisitor {
446 public:
447 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
448 EXPECT_TRUE(root != nullptr);
449 }
450 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700451};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700452
Brian Carlstrom693267a2011-09-06 09:25:34 -0700453struct CheckOffset {
454 size_t cpp_offset;
455 const char* java_name;
456 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
457};
458
Elliott Hughes80609252011-09-23 17:24:51 -0700459template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700460struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800461 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
462 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700464 std::string class_descriptor;
465 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700466
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700467 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800468 Thread* self = Thread::Current();
469 mirror::Class* klass =
470 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700471 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700472
473 bool error = false;
474
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 // Classes have a different size due to padding field. Strings are variable length.
476 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700477 // Currently only required for AccessibleObject since of the padding fields. The class linker
478 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
479 // The RoundUp is to get around this case.
480 static constexpr size_t kPackAlignment = 4;
481 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
482 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700483 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800484 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700485 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700486 << " Java=" << expected_size
487 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700488 error = true;
489 }
490 }
491
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700493 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800494 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700495 << " class=" << class_descriptor
496 << " Java=" << num_fields
497 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700498 error = true;
499 }
500
Brian Carlstromdbc05252011-09-09 01:59:59 -0700501 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700502 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700503 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700505 error = true;
506 }
507 }
508 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700509 for (size_t i = 0; i < offsets.size(); i++) {
510 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700511 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700512 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800513 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800514 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700515 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800516 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700517 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 << " CheckOffsets=" << offset.java_name;
520 }
521 }
522
Brian Carlstromdbc05252011-09-09 01:59:59 -0700523 for (size_t i = 0; i < offsets.size(); i++) {
524 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700525 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700526 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
527 error = true;
528 }
529 }
530 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700531 for (size_t i = 0; i < offsets.size(); i++) {
532 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700533 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700534 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800535 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700536 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800537 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
539 }
540 }
541
542 return !error;
543 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700545 void addOffset(size_t offset, const char* name) {
546 offsets.push_back(CheckOffset(offset, name));
547 }
548
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700549 private:
550 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551};
552
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700553// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
554// is first since if it is failing, others are unlikely to succeed.
555
Brian Carlstromea46f952013-07-30 01:26:50 -0700556struct ObjectOffsets : public CheckOffsets<mirror::Object> {
557 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700558 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
559 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700560#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700561 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
562 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800563#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700564 };
565};
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct ClassOffsets : public CheckOffsets<mirror::Class> {
568 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700569 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700570 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700571 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
572 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
573 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
574 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800575 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
577 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
578 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
579 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
581 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800582 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700583 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700584 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
585 "numReferenceInstanceFields");
586 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
587 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700588 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
589 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
590 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
591 "referenceInstanceOffsets");
592 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
593 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
594 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Andreas Gampe99babb62015-11-02 16:20:00 -0800595 addOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_), "verifyError");
Alex Lighte64300b2015-12-15 15:02:47 -0800596 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700597 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700598 };
599};
600
Brian Carlstromea46f952013-07-30 01:26:50 -0700601struct StringOffsets : public CheckOffsets<mirror::String> {
602 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700603 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000604 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700605 };
606};
607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
609 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000610 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700611 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
612 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700613 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
614 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700615 };
616};
617
Brian Carlstromea46f952013-07-30 01:26:50 -0700618struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700619 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
620 false, "Ljava/lang/StackTraceElement;") {
621 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
622 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
623 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
624 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700625 };
626};
627
Brian Carlstromea46f952013-07-30 01:26:50 -0700628struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
629 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700630 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700631 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700632 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
633 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
634 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700635 };
636};
637
Brian Carlstromea46f952013-07-30 01:26:50 -0700638struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
639 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700640 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400641 };
642};
643
Brian Carlstromea46f952013-07-30 01:26:50 -0700644struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
645 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700646 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
647 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
648 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Vladimir Marko05792b92015-08-03 11:56:49 +0100649 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
650 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
651 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
652 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700653 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
654 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
655 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
656 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700657 };
658};
659
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700660struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
661 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700662 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
663 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
664 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
665 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700666 };
667};
668
669struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700670 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
671 false, "Ljava/lang/ref/FinalizerReference;") {
672 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
673 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
674 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700675 };
676};
677
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700678struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700679 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
680 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000681 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700682 };
683};
684
685struct FieldOffsets : public CheckOffsets<mirror::Field> {
686 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700687 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
688 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
689 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
690 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
691 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
692 };
693};
694
Neil Fuller16b21cd2016-08-12 09:37:02 +0100695struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
696 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
697 false, "Ljava/lang/reflect/Executable;") {
698 };
699};
700
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700701struct AbstractMethodOffsets : public CheckOffsets<mirror::AbstractMethod> {
702 AbstractMethodOffsets() : CheckOffsets<mirror::AbstractMethod>(
703 false, "Ljava/lang/reflect/AbstractMethod;") {
704 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, access_flags_), "accessFlags");
705 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, art_method_), "artMethod");
706 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_), "declaringClass");
707 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_of_overridden_method_),
708 "declaringClassOfOverriddenMethod");
709 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, dex_method_index_), "dexMethodIndex");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700710 };
711};
712
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400713// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400714// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700715// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400716TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700718 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700719 EXPECT_TRUE(ClassOffsets().Check());
720 EXPECT_TRUE(StringOffsets().Check());
721 EXPECT_TRUE(ThrowableOffsets().Check());
722 EXPECT_TRUE(StackTraceElementOffsets().Check());
723 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400724 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700725 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700726 EXPECT_TRUE(ReferenceOffsets().Check());
727 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700728 EXPECT_TRUE(AccessibleObjectOffsets().Check());
729 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100730 EXPECT_TRUE(ExecutableOffsets().Check());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700731 EXPECT_TRUE(AbstractMethodOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700732}
733
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700734TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700736 AssertNonExistentClass("NoSuchClass;");
737 AssertNonExistentClass("LNoSuchClass;");
738}
739
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700740TEST_F(ClassLinkerTest, GetDexFiles) {
741 ScopedObjectAccess soa(Thread::Current());
742
743 jobject jclass_loader = LoadDex("Nested");
744 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
745 ASSERT_EQ(dex_files.size(), 1U);
746 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
747
748 jobject jclass_loader2 = LoadDex("MultiDex");
749 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
750 ASSERT_EQ(dex_files2.size(), 2U);
751 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
752}
753
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700754TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700756 StackHandleScope<1> hs(soa.Self());
757 Handle<mirror::ClassLoader> class_loader(
758 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700759
Ian Rogers98379392014-02-24 16:53:16 -0800760 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700761 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700762 EXPECT_EQ(0U, outer->NumVirtualMethods());
763 EXPECT_EQ(1U, outer->NumDirectMethods());
764
Ian Rogers98379392014-02-24 16:53:16 -0800765 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700766 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700767 EXPECT_EQ(0U, inner->NumVirtualMethods());
768 EXPECT_EQ(1U, inner->NumDirectMethods());
769}
770
771TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700773 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800774 for (int ch = 1; ch < 256; ++ch) {
775 std::string descriptor;
776 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700777 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700778 AssertNonExistentClass(descriptor);
779 } else {
780 AssertPrimitiveClass(descriptor);
781 }
782 }
783}
784
785TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800787 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000788 AssertObjectClass(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700789
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700790 StackHandleScope<1> hs(soa.Self());
791 Handle<mirror::ClassLoader> class_loader(
792 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700793 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800794 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700795 ASSERT_TRUE(MyClass != nullptr);
796 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700797 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
798 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000799 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700800 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700801 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
802 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700803 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700804 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700805 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700806 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700807 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700808 EXPECT_FALSE(MyClass->IsVerified());
809 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700810 EXPECT_FALSE(MyClass->IsArrayInstance());
811 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700812 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700813 EXPECT_FALSE(MyClass->IsInterface());
814 EXPECT_FALSE(MyClass->IsPublic());
815 EXPECT_FALSE(MyClass->IsFinal());
816 EXPECT_FALSE(MyClass->IsPrimitive());
817 EXPECT_FALSE(MyClass->IsSynthetic());
818 EXPECT_EQ(1U, MyClass->NumDirectMethods());
819 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
820 EXPECT_EQ(0U, MyClass->NumInstanceFields());
821 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700822 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700823
824 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
825
826 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700827 AssertArrayClass("[C", "C", nullptr);
828 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700829 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700830 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700831 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700832 // or not available at all
833 AssertNonExistentClass("[[[[LNonExistentClass;");
834}
835
836TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800838 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
839 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700840}
841
Ian Rogersa15e67d2012-02-28 13:51:55 -0800842// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400843// start of the object
844TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800846 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700847 mirror::ObjectArray<mirror::String>* array =
848 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800849 uintptr_t data_offset =
850 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
851 0));
852 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800853 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
854 } else {
855 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
856 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400857}
858
859TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700860 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700861 StackHandleScope<5> hs(soa.Self());
862 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800863 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800864 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
865 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
866
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700867 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800868 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800869 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
870 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
871
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700872 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800873 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800874 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
875 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
876
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700877 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800878 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800879 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
880 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
881
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700882 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800883 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800884 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
885 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
886
887 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400888}
889
Elliott Hughes33203b52011-09-20 19:42:01 -0700890TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
891 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
892 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800894 ScopedNullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700895 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800896 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700897 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800898 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700899 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800900 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700901 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800902 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700903 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800904 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700905 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800906 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700907 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800908 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700909 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800910 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700911 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700912}
913
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700914TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700916 StackHandleScope<2> hs(soa.Self());
917 Handle<mirror::ClassLoader> class_loader_1(
918 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
919 Handle<mirror::ClassLoader> class_loader_2(
920 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800921 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
922 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700923 EXPECT_TRUE(MyClass_1 != nullptr);
924 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700925 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400926}
927
Jesse Wilson7833bd22011-08-09 18:31:44 -0400928TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700930 StackHandleScope<2> hs(soa.Self());
931 Handle<mirror::ClassLoader> class_loader(
932 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
933 Handle<mirror::Class> statics(
934 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700935 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400936
jeffhaoabcfde32011-09-29 15:05:18 -0700937 // Static final primitives that are initialized by a compile-time constant
938 // expression resolve to a copy of a constant value from the constant pool.
939 // So <clinit> should be null.
Andreas Gampe542451c2016-07-26 09:02:02 -0700940 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700941 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700942
943 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400944
Mathieu Chartierc7853442015-03-27 14:35:38 -0700945 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700946 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700947 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
948 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400949
Mathieu Chartierc7853442015-03-27 14:35:38 -0700950 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700951 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700952 EXPECT_EQ(5, s1->GetByte(statics.Get()));
953 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400954
Mathieu Chartierc7853442015-03-27 14:35:38 -0700955 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700956 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700957 EXPECT_EQ('a', s2->GetChar(statics.Get()));
958 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400959
Mathieu Chartierc7853442015-03-27 14:35:38 -0700960 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700961 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700962 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
963 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400964
Mathieu Chartierc7853442015-03-27 14:35:38 -0700965 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700966 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700967 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
968 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400969
Mathieu Chartierc7853442015-03-27 14:35:38 -0700970 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700971 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700972 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
973 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400974
Mathieu Chartierc7853442015-03-27 14:35:38 -0700975 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700976 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700977 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700978 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400979
Mathieu Chartierc7853442015-03-27 14:35:38 -0700980 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700981 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700982 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700983 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400984
Mathieu Chartierc7853442015-03-27 14:35:38 -0700985 ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700986 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700987 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700988 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100989 s8->SetObject<false>(s8->GetDeclaringClass(),
990 mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400991
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800992 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
993 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700994 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
995 EXPECT_EQ(6, s1->GetByte(statics.Get()));
996 EXPECT_EQ('b', s2->GetChar(statics.Get()));
997 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
998 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
999 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001000 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1001 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001002 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001003}
1004
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001007 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001008 Handle<mirror::ClassLoader> class_loader(
1009 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001010 Handle<mirror::Class> I(
1011 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1012 Handle<mirror::Class> J(
1013 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1014 Handle<mirror::Class> K(
1015 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1016 Handle<mirror::Class> A(
1017 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1018 Handle<mirror::Class> B(
1019 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1020 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1021 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1022 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1023 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1024 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001025
Ian Rogersd91d6d62013-09-25 20:26:14 -07001026 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Andreas Gampe542451c2016-07-26 09:02:02 -07001027 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1028 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1029 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
1030 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1031 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1032 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
1033 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1034 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1035 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001036 ASSERT_TRUE(Ii != nullptr);
1037 ASSERT_TRUE(Jj1 != nullptr);
1038 ASSERT_TRUE(Jj2 != nullptr);
1039 ASSERT_TRUE(Kj1 != nullptr);
1040 ASSERT_TRUE(Kj2 != nullptr);
1041 ASSERT_TRUE(Kk != nullptr);
1042 ASSERT_TRUE(Ai != nullptr);
1043 ASSERT_TRUE(Aj1 != nullptr);
1044 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001045 EXPECT_NE(Ii, Ai);
1046 EXPECT_NE(Jj1, Aj1);
1047 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001048 EXPECT_EQ(Kj1, Jj1);
1049 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001050 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1051 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1052 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1053 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1054 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1055 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001056
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057 ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1058 ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1059 ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1060 ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001061 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001062 EXPECT_EQ(Afoo, Bfoo);
1063 EXPECT_EQ(Afoo, Jfoo);
1064 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001065}
1066
Elliott Hughesf3778f62012-01-26 14:14:35 -08001067TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001068 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001069
jeffhaoabcfde32011-09-29 15:05:18 -07001070 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1071 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001072
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(Thread::Current());
1074 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001075 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001076 StackHandleScope<1> hs(soa.Self());
1077 Handle<mirror::ClassLoader> class_loader(
1078 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001079 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001080 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
1081 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001082 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001083 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001084 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001085 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001086 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001087 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001088 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001089 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001090 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001091}
1092
Elliott Hughes20cde902011-10-04 17:37:27 -07001093TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001095 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001096
1097 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001098 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001099 EXPECT_FALSE(c->IsFinalizable());
1100
1101 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001102 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001103 EXPECT_FALSE(c->IsFinalizable());
1104
1105 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001106 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001107 EXPECT_FALSE(c->IsFinalizable());
1108
1109 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001110 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001111 EXPECT_TRUE(c->IsFinalizable());
1112
1113 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001114 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001115 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001116 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001117 EXPECT_TRUE(c->IsFinalizable());
1118
1119 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1120 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001121 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001122 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001123 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001124 EXPECT_TRUE(c->IsFinalizable());
1125}
1126
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001127TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001129 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001130 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001131 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001132 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1133 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001134 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1135 }
1136}
1137
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001138TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1139 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001140 ScopedNullHandle<mirror::ClassLoader> class_loader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001141 mirror::Class* c;
1142
1143 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001144 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001145 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001146
1147 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001148 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001149 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001150
1151 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001152 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001153 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001154
1155 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001157 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001158}
1159
Mathieu Chartiere401d142015-04-22 13:56:20 -07001160static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001161 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001162 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001163 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
Andreas Gampe48498592014-09-10 19:48:05 -07001164 << PrettyMethod(method, true);
1165 }
1166}
1167
Igor Murashkindf707e42016-02-02 16:56:50 -08001168static void CheckVerificationAttempted(mirror::Class* c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001169 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001170 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
Andreas Gampe48498592014-09-10 19:48:05 -07001171 << "Class " << PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001172 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001173 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001174 }
1175}
1176
1177TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1178 ScopedObjectAccess soa(Thread::Current());
1179
1180 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001181 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001182 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1183 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001184 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001185}
1186
1187TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1188 ScopedObjectAccess soa(Thread::Current());
1189
1190 StackHandleScope<1> hs(soa.Self());
1191
1192 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1193 soa.Self(), "Ljava/lang/SecurityManager;")));
1194 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1195 "core";
1196
Igor Murashkindf707e42016-02-02 16:56:50 -08001197 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001198
Ian Rogers7b078e82014-09-10 14:44:24 -07001199 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001200 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001201}
1202
1203TEST_F(ClassLinkerTest, Preverified_App) {
1204 ScopedObjectAccess soa(Thread::Current());
1205
1206 StackHandleScope<2> hs(soa.Self());
1207 Handle<mirror::ClassLoader> class_loader(
1208 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1209 Handle<mirror::Class> statics(
1210 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1211
Igor Murashkindf707e42016-02-02 16:56:50 -08001212 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001213
Ian Rogers7b078e82014-09-10 14:44:24 -07001214 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001215 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001216}
1217
Sebastien Hertz6963e442014-11-26 22:11:27 +01001218TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1219 ScopedObjectAccess soa(Thread::Current());
1220
1221 StackHandleScope<3> hs(soa.Self());
1222 Handle<mirror::ClassLoader> class_loader(
1223 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1224
1225 // java.lang.Object is a bootstrap class.
1226 Handle<mirror::Class> jlo_class(
1227 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1228 ASSERT_TRUE(jlo_class.Get() != nullptr);
1229 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1230
1231 // Statics is not a bootstrap class.
1232 Handle<mirror::Class> statics(
1233 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1234 ASSERT_TRUE(statics.Get() != nullptr);
1235 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1236}
1237
Mathieu Chartier76172162016-01-26 14:54:06 -08001238// Regression test for b/26799552.
1239TEST_F(ClassLinkerTest, RegisterDexFileName) {
1240 ScopedObjectAccess soa(Thread::Current());
1241 StackHandleScope<2> hs(soa.Self());
1242 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1243 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1244 {
1245 ReaderMutexLock mu(soa.Self(), *class_linker->DexLock());
1246 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1247 dex_cache.Assign(down_cast<mirror::DexCache*>(soa.Self()->DecodeJObject(data.weak_root)));
1248 if (dex_cache.Get() != nullptr) {
1249 break;
1250 }
1251 }
1252 ASSERT_TRUE(dex_cache.Get() != nullptr);
1253 }
1254 // Make a copy of the dex cache and change the name.
1255 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1256 const uint16_t data[] = { 0x20AC, 0x20A1 };
1257 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1258 arraysize(data),
1259 data)));
1260 dex_cache->SetLocation(location.Get());
1261 const DexFile* old_dex_file = dex_cache->GetDexFile();
1262
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001263 std::unique_ptr<DexFile> dex_file(new DexFile(old_dex_file->Begin(),
1264 old_dex_file->Size(),
1265 location->ToModifiedUtf8(),
1266 0u,
1267 nullptr,
1268 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001269 {
1270 WriterMutexLock mu(soa.Self(), *class_linker->DexLock());
1271 // Check that inserting with a UTF16 name works.
1272 class_linker->RegisterDexFileLocked(*dex_file, dex_cache);
1273 }
1274}
1275
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001276} // namespace art