blob: fe45b9e1f006cb3072e12f90a5248c49f51d0f9f [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
Andreas Gampe9186ced2016-12-12 14:28:21 -080022#include "android-base/strings.h"
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010028#include "class_root.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file_types.h"
31#include "dex/standard_dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "handle_scope-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070036#include "mirror/array-alloc-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070037#include "mirror/accessible_object.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000038#include "mirror/call_site.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070040#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010042#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010043#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070044#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010045#include "mirror/method_handle_impl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000046#include "mirror/method_handles_lookup.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070047#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070049#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/object_array-inl.h"
51#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070052#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070054#include "mirror/string-inl.h"
Orion Hodson005ac512017-10-24 15:43:43 +010055#include "mirror/var_handle.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070056#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070057#include "thread-current-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058
59namespace art {
60
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080061class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070062 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070065 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070066 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070067 EXPECT_TRUE(self->IsExceptionPending());
Vladimir Markoe9987b02018-05-22 16:26:43 +010068 StackHandleScope<1> hs(self);
69 Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException());
Elliott Hughes885c3bd2011-08-22 16:59:20 -070070 self->ClearException();
Vladimir Markoe9987b02018-05-22 16:26:43 +010071 ObjPtr<mirror::Class> exception_class =
Ian Rogers98379392014-02-24 16:53:16 -080072 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070073 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 }
75
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080078 Thread* self = Thread::Current();
79 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070080 }
81
Vladimir Markoe9987b02018-05-22 16:26:43 +010082 void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070084 ASSERT_TRUE(primitive != nullptr);
85 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070087 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070088 std::string temp;
89 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070090 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070092 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Vladimir Marko2c64a832018-01-04 11:31:56 +000093 EXPECT_EQ(ClassStatus::kInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070094 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070095 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096 EXPECT_TRUE(primitive->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +000097 EXPECT_FALSE(primitive->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070098 EXPECT_TRUE(primitive->IsVerified());
99 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700100 EXPECT_FALSE(primitive->IsArrayInstance());
101 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700102 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103 EXPECT_FALSE(primitive->IsInterface());
104 EXPECT_TRUE(primitive->IsPublic());
105 EXPECT_TRUE(primitive->IsFinal());
106 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700107 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700108 EXPECT_EQ(0U, primitive->NumDirectMethods());
109 EXPECT_EQ(0U, primitive->NumVirtualMethods());
110 EXPECT_EQ(0U, primitive->NumInstanceFields());
111 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700112 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700113 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700114 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800115 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
116 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Elliott Hughes00626c22013-06-14 15:04:14 -0700117 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700118 }
119
Vladimir Markoe9987b02018-05-22 16:26:43 +0100120 void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000122 ASSERT_TRUE(JavaLangObject != nullptr);
123 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
124 ASSERT_EQ(JavaLangObject->GetClass(),
125 JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100126 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000127 std::string temp;
128 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
129 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
130 EXPECT_FALSE(JavaLangObject->HasSuperClass());
131 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Marko2c64a832018-01-04 11:31:56 +0000132 EXPECT_EQ(ClassStatus::kInitialized, JavaLangObject->GetStatus());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000133 EXPECT_FALSE(JavaLangObject->IsErroneous());
134 EXPECT_TRUE(JavaLangObject->IsLoaded());
135 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000136 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000137 EXPECT_TRUE(JavaLangObject->IsVerified());
138 EXPECT_TRUE(JavaLangObject->IsInitialized());
139 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
140 EXPECT_FALSE(JavaLangObject->IsArrayClass());
141 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
142 EXPECT_FALSE(JavaLangObject->IsInterface());
143 EXPECT_TRUE(JavaLangObject->IsPublic());
144 EXPECT_FALSE(JavaLangObject->IsFinal());
145 EXPECT_FALSE(JavaLangObject->IsPrimitive());
146 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000147 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000148 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
149 if (!kUseBrooksReadBarrier) {
150 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
151 } else {
152 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
153 }
154 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
155 "shadow$_klass_");
156 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
157 "shadow$_monitor_");
158 if (kUseBrooksReadBarrier) {
159 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
160 "shadow$_x_rb_ptr_");
161 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
162 "shadow$_x_xpadding_");
163 }
164
165 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
166 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
167
Andreas Gampe542451c2016-07-26 09:02:02 -0700168 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000169 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
170 ImTable* imt = JavaLangObject->GetImt(pointer_size);
171 ASSERT_NE(nullptr, imt);
172 for (size_t i = 0; i < ImTable::kSize; ++i) {
173 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
174 }
175 }
176
Brian Carlstromaded5f72011-10-07 17:15:04 -0700177 void AssertArrayClass(const std::string& array_descriptor,
178 const std::string& component_type,
Vladimir Markoe9987b02018-05-22 16:26:43 +0100179 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800181 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 StackHandleScope<2> hs(self);
183 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
184 Handle<mirror::Class> array(
185 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700186 std::string temp;
187 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100188 EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700189 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700190 AssertArrayClass(array_descriptor, array);
191 }
192
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700193 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800195 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700196 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700197 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700198 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700199 std::string temp;
200 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700201 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800202 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100203 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
204 array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700205 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700206 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Vladimir Marko2c64a832018-01-04 11:31:56 +0000208 EXPECT_EQ(ClassStatus::kInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700209 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700210 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700211 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000212 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700213 EXPECT_TRUE(array->IsVerified());
214 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700215 EXPECT_FALSE(array->IsArrayInstance());
216 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700217 EXPECT_FALSE(array->IsInterface());
218 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
219 EXPECT_TRUE(array->IsFinal());
220 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700221 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700222 EXPECT_EQ(0U, array->NumDirectMethods());
223 EXPECT_EQ(0U, array->NumVirtualMethods());
224 EXPECT_EQ(0U, array->NumInstanceFields());
225 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700226 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000227 EXPECT_TRUE(array->ShouldHaveImt());
228 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700229 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700230 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000231 ObjPtr<mirror::Class> direct_interface0 =
232 mirror::Class::GetDirectInterface(self, array.Get(), 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700233 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700234 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Marko19a4d372016-12-08 14:41:46 +0000235 ObjPtr<mirror::Class> direct_interface1 =
236 mirror::Class::GetDirectInterface(self, array.Get(), 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700237 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Vladimir Markobcf17522018-06-01 13:14:32 +0100238 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000239
Andreas Gampe542451c2016-07-26 09:02:02 -0700240 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100241 ObjPtr<mirror::Class> JavaLangObject =
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000242 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
243 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
244 // IMT of a array class should be shared with the IMT of the java.lag.Object
245 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700246 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700249 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700251 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700253 }
254
Mathieu Chartier3398c782016-09-30 10:27:43 -0700255 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700257 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700259 EXPECT_TRUE(field->GetName() != nullptr);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000260 EXPECT_TRUE(field->ResolveType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700261 }
262
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700263 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700265 std::string temp;
266 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700269 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700270 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700271 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700273 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700274 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700275 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700276 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700277 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000278 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700279 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700281 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700283 std::string temp2;
284 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
285 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700286 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700287 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800288 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700289 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800290 EXPECT_TRUE(m.IsStatic());
291 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700292 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700293 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700294 if (!klass->IsSynthetic()) {
295 EXPECT_NE(0U, klass->NumDirectMethods());
296 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700297 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700298 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100299 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700300 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100301 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700302 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700303 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700304 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700305 } else {
Alex Light705ad492015-09-21 11:36:30 -0700306 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700307 }
308 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700312 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700313 }
314 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 EXPECT_FALSE(klass->IsAbstract());
316 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700317 }
318 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700319 EXPECT_FALSE(klass->IsFinal());
320 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700321 }
322
323 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700324 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700325
Andreas Gampe542451c2016-07-26 09:02:02 -0700326 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 AssertMethod(&method);
328 EXPECT_TRUE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100329 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700330 }
331
Andreas Gampe542451c2016-07-26 09:02:02 -0700332 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 AssertMethod(&method);
334 EXPECT_FALSE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100335 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Alex Lighte64300b2015-12-15 15:02:47 -0800336 }
337
Andreas Gampe542451c2016-07-26 09:02:02 -0700338 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800339 AssertMethod(&method);
340 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800341 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800342 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700343 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800344 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700345 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700346 }
347
348 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700349 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700350 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700351 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700352 }
353
354 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700355 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700356 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700357 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700358 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700359
Vladimir Marko76649e82014-11-10 18:32:59 +0000360 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700361 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000362 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
363 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
364 klass->NumReferenceInstanceFields() *
365 sizeof(mirror::HeapReference<mirror::Object>));
366 MemberOffset current_ref_offset = start_ref_offset;
367 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700368 ArtField* field = klass->GetInstanceField(i);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000369 ObjPtr<mirror::Class> field_type = field->ResolveType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700370 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000371 if (!field->IsPrimitiveType()) {
372 ASSERT_TRUE(!field_type->IsPrimitive());
373 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
374 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
375 // While Reference.referent is not primitive, the ClassLinker
376 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700377 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000378 } else {
379 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
380 sizeof(mirror::HeapReference<mirror::Object>));
381 }
382 } else {
383 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
384 // Shuffled before references.
385 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
386 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
387 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700388 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700389 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000390 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700391
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700392 uint32_t total_num_reference_instance_fields = 0;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100393 ObjPtr<mirror::Class> k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700394 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700395 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
396 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700397 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700398 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
399 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
400 // The reference instance offsets have a bit set for each reference offset.
401 // +1 for Object's class.
402 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
403 total_num_reference_instance_fields);
404 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700405 }
406
Vladimir Markoe9987b02018-05-22 16:26:43 +0100407 void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700408 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700409 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800410 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700411 StackHandleScope<1> hs(self);
412 Handle<mirror::Class> klass(
413 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800414 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700415 std::string temp;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100416 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
417 EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700418 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700420 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 AssertArrayClass(descriptor, klass);
422 } else {
423 AssertClass(descriptor, klass);
424 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700425 }
426
Vladimir Markoe9987b02018-05-22 16:26:43 +0100427 void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700428 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700429 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800430 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
431 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
432 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700433 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700434 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700435 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800436 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800437 const DexFile::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800438 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700439 AssertDexFileClass(class_loader, descriptor);
440 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700441 TestRootVisitor visitor;
442 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
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:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100447 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700448 EXPECT_TRUE(root != nullptr);
449 }
450 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700451};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700452
Narayan Kamath25352fc2016-08-03 12:46:58 +0100453class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
454 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100455 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100456 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100457 }
458};
459
Brian Carlstrom693267a2011-09-06 09:25:34 -0700460struct CheckOffset {
461 size_t cpp_offset;
462 const char* java_name;
463 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
464};
465
Elliott Hughes80609252011-09-23 17:24:51 -0700466template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700467struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800468 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
469 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700470 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700471 std::string class_descriptor;
472 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700473
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700474 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800475 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100476 ObjPtr<mirror::Class> klass =
Ian Rogers98379392014-02-24 16:53:16 -0800477 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700478 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700479
480 bool error = false;
481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 // Classes have a different size due to padding field. Strings are variable length.
483 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700484 // Currently only required for AccessibleObject since of the padding fields. The class linker
485 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
486 // The RoundUp is to get around this case.
487 static constexpr size_t kPackAlignment = 4;
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100488 size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700489 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700490 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800491 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700492 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700493 << " Java=" << expected_size
494 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700495 error = true;
496 }
497 }
498
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700500 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800501 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700502 << " class=" << class_descriptor
503 << " Java=" << num_fields
504 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700505 error = true;
506 }
507
Brian Carlstromdbc05252011-09-09 01:59:59 -0700508 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700509 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700510 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700512 error = true;
513 }
514 }
515 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700516 for (size_t i = 0; i < offsets.size(); i++) {
517 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700518 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700519 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800521 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700522 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800523 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700526 << " CheckOffsets=" << offset.java_name;
527 }
528 }
529
Brian Carlstromdbc05252011-09-09 01:59:59 -0700530 for (size_t i = 0; i < offsets.size(); i++) {
531 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700532 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700533 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
534 error = true;
535 }
536 }
537 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700538 for (size_t i = 0; i < offsets.size(); i++) {
539 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700540 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800542 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700543 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800544 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
546 }
547 }
548
549 return !error;
550 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700551
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700552 void addOffset(size_t offset, const char* name) {
553 offsets.push_back(CheckOffset(offset, name));
554 }
555
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700556 private:
557 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558};
559
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700560// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
561// is first since if it is failing, others are unlikely to succeed.
562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563struct ObjectOffsets : public CheckOffsets<mirror::Object> {
564 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700565 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
566 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700567#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700568 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
569 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800570#endif
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800571 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700572};
573
Brian Carlstromea46f952013-07-30 01:26:50 -0700574struct ClassOffsets : public CheckOffsets<mirror::Class> {
575 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700577 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700578 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
579 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
580 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
581 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800582 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700583 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700584 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
585 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700586 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700587 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800589 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700590 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700591 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
592 "numReferenceInstanceFields");
593 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
594 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700595 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700596 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
597 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
599 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
600 "referenceInstanceOffsets");
601 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
602 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
603 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800604 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700605 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800606 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700607};
608
Alex Lightd6251582016-10-31 11:12:30 -0700609struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
610 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Lighta01de592016-11-15 10:43:06 -0800611 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
612 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000613 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
David Brazdil1a658632018-12-01 17:54:26 +0000614 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
615 "preRedefineClassDefIndex");
616 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
617 "preRedefineDexFilePtr");
Alex Lightd6251582016-10-31 11:12:30 -0700618 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
619 }
620};
621
Brian Carlstromea46f952013-07-30 01:26:50 -0700622struct StringOffsets : public CheckOffsets<mirror::String> {
623 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700624 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000625 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800626 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700627};
628
Brian Carlstromea46f952013-07-30 01:26:50 -0700629struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
630 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000631 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700632 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
633 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700634 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
635 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800636 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700637};
638
Brian Carlstromea46f952013-07-30 01:26:50 -0700639struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700640 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
641 false, "Ljava/lang/StackTraceElement;") {
642 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
643 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
644 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
645 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800646 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700647};
648
Brian Carlstromea46f952013-07-30 01:26:50 -0700649struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
650 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700651 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700652 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700653 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
654 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
655 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800656 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700657};
658
Brian Carlstromea46f952013-07-30 01:26:50 -0700659struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
660 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700661 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800662 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400663};
664
Brian Carlstromea46f952013-07-30 01:26:50 -0700665struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
666 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700667 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
668 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700669 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000670 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700678 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100679 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700680 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
681 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
682 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800683 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700684};
685
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700686struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
687 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700688 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
689 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
690 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
691 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800692 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700693};
694
695struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700696 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
697 false, "Ljava/lang/ref/FinalizerReference;") {
698 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
699 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
700 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800701 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700702};
703
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700704struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700705 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
706 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000707 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800708 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700709};
710
711struct FieldOffsets : public CheckOffsets<mirror::Field> {
712 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700713 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
714 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
715 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
716 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
717 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800718 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700719};
720
Neil Fuller16b21cd2016-08-12 09:37:02 +0100721struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
722 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
723 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100724 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
725 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
726 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
727 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
728 "declaringClassOfOverriddenMethod");
729 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100730 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
731 "hasRealParameterData");
732 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800733 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100734};
735
Narayan Kamathafa48272016-08-03 12:46:58 +0100736struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
737 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
738 false, "Ljava/lang/invoke/MethodType;") {
739 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
740 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
741 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
742 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
743 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
744 }
745};
746
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000747struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
748 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
749 false, "Ljava/lang/invoke/MethodHandle;") {
750 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
751 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
752 "cachedSpreadInvoker");
753 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
754 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
755 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
756 }
757};
758
Narayan Kamathafa48272016-08-03 12:46:58 +0100759struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
760 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000761 false, "Ljava/lang/invoke/MethodHandleImpl;") {
762 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100763 }
764};
765
Orion Hodsonc069a302017-01-18 09:23:12 +0000766struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
767 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
768 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
769 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
770 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
771 }
772};
773
Narayan Kamath000e1882016-10-24 17:14:25 +0100774struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
775 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
776 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000777 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100778 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
779 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
780 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
781 }
782};
783
Orion Hodsonc069a302017-01-18 09:23:12 +0000784struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
785 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
786 false, "Ljava/lang/invoke/CallSite;") {
787 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
788 }
789};
790
Orion Hodson005ac512017-10-24 15:43:43 +0100791struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
792 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
793 false, "Ljava/lang/invoke/VarHandle;") {
794 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
795 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
796 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
797 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
798 }
799};
800
801struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
802 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
803 false, "Ljava/lang/invoke/FieldVarHandle;") {
804 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
805 }
806};
807
Orion Hodsonffad6682018-01-02 10:18:58 +0000808struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
809 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
810 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
811 }
812};
813
Orion Hodson005ac512017-10-24 15:43:43 +0100814struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
815 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
816 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
817 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
818 "nativeByteOrder");
819 }
820};
821
822struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
823 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
824 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
825 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
826 "nativeByteOrder");
827 }
828};
829
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400830// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400831// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700832// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400833TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700835 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700836 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700837 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700838 EXPECT_TRUE(StringOffsets().Check());
839 EXPECT_TRUE(ThrowableOffsets().Check());
840 EXPECT_TRUE(StackTraceElementOffsets().Check());
841 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400842 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700843 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700844 EXPECT_TRUE(ReferenceOffsets().Check());
845 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700846 EXPECT_TRUE(AccessibleObjectOffsets().Check());
847 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100848 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100849 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000850 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100851 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000852 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100853 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000854 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100855 EXPECT_TRUE(VarHandleOffsets().Check());
856 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000857 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100858 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
859 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700860}
861
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700862TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700864 AssertNonExistentClass("NoSuchClass;");
865 AssertNonExistentClass("LNoSuchClass;");
866}
867
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700868TEST_F(ClassLinkerTest, GetDexFiles) {
869 ScopedObjectAccess soa(Thread::Current());
870
871 jobject jclass_loader = LoadDex("Nested");
872 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
873 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800874 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700875
876 jobject jclass_loader2 = LoadDex("MultiDex");
877 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
878 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800879 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700880}
881
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700882TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700883 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700884 StackHandleScope<1> hs(soa.Self());
885 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700886 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700887
Vladimir Markoe9987b02018-05-22 16:26:43 +0100888 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700889 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700890 EXPECT_EQ(0U, outer->NumVirtualMethods());
891 EXPECT_EQ(1U, outer->NumDirectMethods());
892
Vladimir Markoe9987b02018-05-22 16:26:43 +0100893 ObjPtr<mirror::Class> inner =
894 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700895 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700896 EXPECT_EQ(0U, inner->NumVirtualMethods());
897 EXPECT_EQ(1U, inner->NumDirectMethods());
898}
899
900TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700902 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800903 for (int ch = 1; ch < 256; ++ch) {
904 std::string descriptor;
905 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700906 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700907 AssertNonExistentClass(descriptor);
908 } else {
909 AssertPrimitiveClass(descriptor);
910 }
911 }
912}
913
914TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100916 StackHandleScope<2> hs(soa.Self());
917 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
918 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
919 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700920
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700921 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700922 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700923 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100924 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700925 ASSERT_TRUE(MyClass != nullptr);
926 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700927 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100928 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000929 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700930 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100931 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700932 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100933 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000934 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700935 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700936 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700937 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000938 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700939 EXPECT_FALSE(MyClass->IsVerified());
940 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700941 EXPECT_FALSE(MyClass->IsArrayInstance());
942 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700943 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700944 EXPECT_FALSE(MyClass->IsInterface());
945 EXPECT_FALSE(MyClass->IsPublic());
946 EXPECT_FALSE(MyClass->IsFinal());
947 EXPECT_FALSE(MyClass->IsPrimitive());
948 EXPECT_FALSE(MyClass->IsSynthetic());
949 EXPECT_EQ(1U, MyClass->NumDirectMethods());
950 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
951 EXPECT_EQ(0U, MyClass->NumInstanceFields());
952 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700953 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700954
955 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
956
957 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700958 AssertArrayClass("[C", "C", nullptr);
959 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700960 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700961 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700962 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700963 // or not available at all
964 AssertNonExistentClass("[[[[LNonExistentClass;");
965}
966
Mathieu Chartierb8901302016-09-30 10:27:43 -0700967TEST_F(ClassLinkerTest, LookupResolvedType) {
968 ScopedObjectAccess soa(Thread::Current());
969 StackHandleScope<1> hs(soa.Self());
970 Handle<mirror::ClassLoader> class_loader(
971 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
972 AssertNonExistentClass("LMyClass;");
973 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800974 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700975 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700976 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000977 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700978 klass);
979 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000980 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700981 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
982 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000983 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700984 klass);
985}
986
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000987TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
988 ScopedObjectAccess soa(Thread::Current());
989 StackHandleScope<2> hs(soa.Self());
990 Handle<mirror::ClassLoader> class_loader(
991 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
992 // Get the AllFields class for the dex cache and dex file.
993 ObjPtr<mirror::Class> all_fields_klass
994 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
995 ASSERT_OBJ_PTR_NE(all_fields_klass, ObjPtr<mirror::Class>(nullptr));
996 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
997 const DexFile& dex_file = *dex_cache->GetDexFile();
998 // Get the index of the array class we want to test.
999 const DexFile::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
1000 ASSERT_TRUE(array_id != nullptr);
1001 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
1002 // Check that the array class wasn't resolved yet.
1003 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001004 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001005 ObjPtr<mirror::Class>(nullptr));
1006 // Resolve the array class we want to test.
1007 ObjPtr<mirror::Class> array_klass
1008 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
1009 ASSERT_OBJ_PTR_NE(array_klass, ObjPtr<mirror::Class>(nullptr));
1010 // Test that LookupResolvedType() finds the array class.
1011 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001012 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001013 array_klass);
1014 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001015 dex_cache->ClearResolvedType(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001016 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
1017 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001018 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001019 array_klass);
1020}
1021
Vladimir Marko72ab6842017-01-20 19:32:50 +00001022TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
1023 ScopedObjectAccess soa(Thread::Current());
1024 StackHandleScope<3> hs(soa.Self());
1025 Handle<mirror::ClassLoader> class_loader(
1026 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
1027 AssertNonExistentClass("LErroneousInit;");
1028 Handle<mirror::Class> klass =
1029 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
1030 ASSERT_OBJ_PTR_NE(klass.Get(), ObjPtr<mirror::Class>(nullptr));
1031 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
1032 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Vladimir Marko72ab6842017-01-20 19:32:50 +00001033 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001034 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001035 klass.Get());
1036 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001037 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001038 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1039 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001040 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001041 klass.Get());
1042 // Force initialization to turn the class erroneous.
1043 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1044 klass,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001045 /* can_init_fields= */ true,
1046 /* can_init_parents= */ true);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001047 EXPECT_FALSE(initialized);
1048 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1049 soa.Self()->ClearException();
1050 // Check that the LookupResolvedType() can still find the resolved type.
1051 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001052 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001053 klass.Get());
1054 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001055 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001056 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1057 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001058 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001059 klass.Get());
1060}
1061
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001062TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001064 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1065 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001066}
1067
Ian Rogersa15e67d2012-02-28 13:51:55 -08001068// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001069// start of the object
1070TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001072 ObjPtr<mirror::Class> array_class =
1073 class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
1074 ObjPtr<mirror::ObjectArray<mirror::String>> array =
Brian Carlstromea46f952013-07-30 01:26:50 -07001075 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001076 uintptr_t data_offset =
1077 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1078 0));
1079 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001080 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1081 } else {
1082 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1083 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001084}
1085
1086TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001088 StackHandleScope<5> hs(soa.Self());
1089 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001090 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001091 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1092 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1093
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001094 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001095 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001096 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1097 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1098
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001099 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001100 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001101 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1102 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1103
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001104 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001105 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001106 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1107 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1108
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001109 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001110 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001111 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1112 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1113
1114 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001115}
1116
Elliott Hughes33203b52011-09-20 19:42:01 -07001117TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1118 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1119 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001121 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001122 ObjPtr<mirror::Class> c;
Ian Rogers98379392014-02-24 16:53:16 -08001123 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001124 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001125 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001126 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001127 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001128 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001129 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001130 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001131 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001132 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001133 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001134 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001135 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001136 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001137 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001138 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001139}
1140
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001141TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001143 StackHandleScope<3> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001144 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001145 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001146 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001147 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001148 Handle<mirror::Class> MyClass_1 = hs.NewHandle(
1149 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
1150 ObjPtr<mirror::Class> MyClass_2 =
1151 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001152 EXPECT_TRUE(MyClass_1 != nullptr);
1153 EXPECT_TRUE(MyClass_2 != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001154 EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001155}
1156
Jesse Wilson7833bd22011-08-09 18:31:44 -04001157TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001159 StackHandleScope<2> hs(soa.Self());
1160 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001161 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001162 Handle<mirror::Class> statics(
1163 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001164 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001165
jeffhaoabcfde32011-09-29 15:05:18 -07001166 // Static final primitives that are initialized by a compile-time constant
1167 // expression resolve to a copy of a constant value from the constant pool.
1168 // So <clinit> should be null.
Vladimir Markoba118822017-06-12 15:41:56 +01001169 ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001170 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001171
1172 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001173
Vladimir Marko19a4d372016-12-08 14:41:46 +00001174 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001175 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001176 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1177 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001178
Vladimir Marko19a4d372016-12-08 14:41:46 +00001179 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001180 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001181 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1182 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001183
Vladimir Marko19a4d372016-12-08 14:41:46 +00001184 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001185 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001186 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1187 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001188
Vladimir Marko19a4d372016-12-08 14:41:46 +00001189 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001190 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001191 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1192 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001193
Vladimir Marko19a4d372016-12-08 14:41:46 +00001194 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001195 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001196 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1197 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001198
Vladimir Marko19a4d372016-12-08 14:41:46 +00001199 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001200 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001201 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1202 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001203
Vladimir Marko19a4d372016-12-08 14:41:46 +00001204 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001205 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001206 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001207 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001208
Vladimir Marko19a4d372016-12-08 14:41:46 +00001209 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001210 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001211 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001212 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001213
Vladimir Marko19a4d372016-12-08 14:41:46 +00001214 ArtField* s8 = mirror::Class::FindStaticField(
1215 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001216 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001217 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001218 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001219 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001220
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001221 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1222 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001223 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1224 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1225 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1226 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1227 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1228 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001229 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1230 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001231 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001232}
1233
Brian Carlstrom30b94452011-08-25 21:35:26 -07001234TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001236 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001237 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001238 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001239 Handle<mirror::Class> I(
1240 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1241 Handle<mirror::Class> J(
1242 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1243 Handle<mirror::Class> K(
1244 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1245 Handle<mirror::Class> A(
1246 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1247 Handle<mirror::Class> B(
1248 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1249 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1250 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1251 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1252 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1253 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001254
Ian Rogersd91d6d62013-09-25 20:26:14 -07001255 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001256 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1257 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1258 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001259 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1260 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1261 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001262 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1263 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1264 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001265 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001266 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001267 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001268 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001269 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001270 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001271 ASSERT_TRUE(Kj1 != nullptr);
1272 ASSERT_TRUE(Kj2 != nullptr);
1273 ASSERT_TRUE(Kk != nullptr);
1274 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001275 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001276 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001277 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001278 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001279 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001280 EXPECT_NE(Ii, Ai);
1281 EXPECT_NE(Jj1, Aj1);
1282 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001283 EXPECT_EQ(Kj1, Jj1);
1284 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001285 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1286 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1287 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1288 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1289 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1290 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001291
Vladimir Marko19a4d372016-12-08 14:41:46 +00001292 ArtField* Afoo =
1293 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1294 ArtField* Bfoo =
1295 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1296 ArtField* Jfoo =
1297 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1298 ArtField* Kfoo =
1299 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001300 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001301 EXPECT_EQ(Afoo, Bfoo);
1302 EXPECT_EQ(Afoo, Jfoo);
1303 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001304}
1305
Elliott Hughesf3778f62012-01-26 14:14:35 -08001306TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001307 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001308
jeffhaoabcfde32011-09-29 15:05:18 -07001309 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1310 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001311
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(Thread::Current());
1313 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001314 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001315 StackHandleScope<1> hs(soa.Self());
1316 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001317 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001318 ObjPtr<mirror::Class> klass =
1319 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001320 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001321 ArtMethod* getS0 =
1322 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1323 ASSERT_TRUE(getS0 != nullptr);
1324 ASSERT_TRUE(getS0->IsStatic());
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001325 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001326 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001327 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001328 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1329 clinit,
1330 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001331 /* can_run_clinit= */ true,
1332 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001333 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001334 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001335 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1336 getS0,
1337 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001338 /* can_run_clinit= */ true,
1339 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001340 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001341 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001342}
1343
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001344TEST_F(ClassLinkerTest, ErroneousClass) {
1345 ScopedObjectAccess soa(Thread::Current());
1346 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1347 StackHandleScope<1> hs(soa.Self());
1348 Handle<mirror::ClassLoader> class_loader(
1349 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1350 hs.Self()->AssertNoPendingException();
1351 const char* descriptor = "LErroneous;";
1352 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1353 // Erronenous since we are extending final class.
1354 hs.Self()->AssertPendingException();
1355 EXPECT_TRUE(klass == nullptr);
1356 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1357 EXPECT_FALSE(klass == nullptr);
1358 EXPECT_TRUE(klass->IsErroneous());
1359 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1360}
1361
Elliott Hughes20cde902011-10-04 17:37:27 -07001362TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001364 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001365
1366 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001367 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001368 EXPECT_FALSE(c->IsFinalizable());
1369
1370 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001371 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001372 EXPECT_FALSE(c->IsFinalizable());
1373
1374 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001375 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001376 EXPECT_FALSE(c->IsFinalizable());
1377
1378 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001379 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001380 EXPECT_TRUE(c->IsFinalizable());
1381
1382 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001383 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001384 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001385 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001386 EXPECT_TRUE(c->IsFinalizable());
1387
1388 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1389 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001390 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001391 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001392 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001393 EXPECT_TRUE(c->IsFinalizable());
1394}
1395
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001396TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001398 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001399 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1400 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001401 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001402 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001403 }
1404}
1405
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001406TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1407 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001408 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001409 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001410
1411 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001412 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001413 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001414
1415 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001416 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001417 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001418
1419 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001420 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001421 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001422
1423 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001424 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001425 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001426}
1427
Mathieu Chartiere401d142015-04-22 13:56:20 -07001428static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001429 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001430 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001431 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001432 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001433 }
1434}
1435
Vladimir Markoe9987b02018-05-22 16:26:43 +01001436static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001437 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001438 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001439 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001440 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001441 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001442 }
1443}
1444
1445TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1446 ScopedObjectAccess soa(Thread::Current());
1447
Vladimir Markoe9987b02018-05-22 16:26:43 +01001448 ObjPtr<mirror::Class> JavaLangObject =
1449 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001450 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001451 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1452 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001453 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001454}
1455
1456TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1457 ScopedObjectAccess soa(Thread::Current());
1458
1459 StackHandleScope<1> hs(soa.Self());
1460
1461 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1462 soa.Self(), "Ljava/lang/SecurityManager;")));
1463 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1464 "core";
1465
Igor Murashkindf707e42016-02-02 16:56:50 -08001466 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001467
Ian Rogers7b078e82014-09-10 14:44:24 -07001468 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001469 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001470}
1471
1472TEST_F(ClassLinkerTest, Preverified_App) {
1473 ScopedObjectAccess soa(Thread::Current());
1474
1475 StackHandleScope<2> hs(soa.Self());
1476 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001477 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001478 Handle<mirror::Class> statics(
1479 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1480
Igor Murashkindf707e42016-02-02 16:56:50 -08001481 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001482
Ian Rogers7b078e82014-09-10 14:44:24 -07001483 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001484 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001485}
1486
Sebastien Hertz6963e442014-11-26 22:11:27 +01001487TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1488 ScopedObjectAccess soa(Thread::Current());
1489
1490 StackHandleScope<3> hs(soa.Self());
1491 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001492 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001493
1494 // java.lang.Object is a bootstrap class.
1495 Handle<mirror::Class> jlo_class(
1496 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001497 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001498 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001499
1500 // Statics is not a bootstrap class.
1501 Handle<mirror::Class> statics(
1502 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001503 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001504 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001505}
1506
Mathieu Chartier76172162016-01-26 14:54:06 -08001507// Regression test for b/26799552.
1508TEST_F(ClassLinkerTest, RegisterDexFileName) {
1509 ScopedObjectAccess soa(Thread::Current());
1510 StackHandleScope<2> hs(soa.Self());
1511 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1512 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1513 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001514 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001515 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001516 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001517 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001518 break;
1519 }
1520 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001521 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001522 }
1523 // Make a copy of the dex cache and change the name.
1524 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1525 const uint16_t data[] = { 0x20AC, 0x20A1 };
1526 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1527 arraysize(data),
1528 data)));
1529 dex_cache->SetLocation(location.Get());
1530 const DexFile* old_dex_file = dex_cache->GetDexFile();
1531
David Sehr0b426772018-07-03 23:03:42 +00001532 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1533 old_dex_file->Size(),
1534 location->ToModifiedUtf8(),
1535 0u,
1536 nullptr,
1537 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001538 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001539 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001540 // Check that inserting with a UTF16 name works.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001541 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001542 }
1543}
1544
Narayan Kamath25352fc2016-08-03 12:46:58 +01001545TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1546 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001547 StackHandleScope<7> hs(soa.Self());
1548
1549 Handle<mirror::ClassLoader> class_loader(
1550 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1551 Handle<mirror::Class> method_types(
1552 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1553 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1554
Vladimir Markoba118822017-06-12 15:41:56 +01001555 ArtMethod* method1 = method_types->FindClassMethod(
1556 "method1",
1557 "(Ljava/lang/String;)Ljava/lang/String;",
1558 kRuntimePointerSize);
1559 ASSERT_TRUE(method1 != nullptr);
1560 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001561
1562 const DexFile& dex_file = *(method1->GetDexFile());
1563 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001564 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001565
1566 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
1567
1568 // This is the MethodType corresponding to the prototype of
1569 // String MethodTypes# method1(String).
1570 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001571 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001572 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001573 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001574
1575 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001576 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001577
1578 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001579 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1580 "Ljava/lang/String;",
1581 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001582 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1583 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001584
1585 // Resolve the method type again and assert that we get back the same value.
1586 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001587 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001588 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001589
1590 // Resolve the MethodType associated with a different method signature
1591 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001592 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001593 "method2",
1594 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1595 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001596 ASSERT_TRUE(method2 != nullptr);
1597 ASSERT_FALSE(method2->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001598 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
1599 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001600 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001601 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001602}
1603
Calin Juravle7865ac72017-06-28 11:03:12 -07001604// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1605// by creating a chain of class loaders with various dex files.
1606TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1607 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1608 // No need to check again.
1609 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1610 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1611 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1612 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1613}
1614
David Sehrc431b9d2018-03-02 12:01:51 -08001615TEST_F(ClassLinkerTest, PrettyClass) {
1616 ScopedObjectAccess soa(Thread::Current());
1617 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001618 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001619 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001620 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001621 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1622}
1623
1624TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1625 ScopedObjectAccess soa(Thread::Current());
1626 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001627 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001628 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001629 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001630 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1631 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1632}
1633
1634TEST_F(ClassLinkerTest, PrettyField) {
1635 ScopedObjectAccess soa(Thread::Current());
1636 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1637
Vladimir Markoe9987b02018-05-22 16:26:43 +01001638 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1639 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001640
1641 ArtField* f;
1642 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1643 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1644 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1645}
1646
1647TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1648 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001649 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001650 ASSERT_TRUE(c != nullptr);
1651 ArtMethod* m;
1652
1653 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1654 ASSERT_TRUE(m != nullptr);
1655 ASSERT_FALSE(m->IsDirect());
1656 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1657 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1658
1659 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1660 ASSERT_TRUE(m != nullptr);
1661 ASSERT_FALSE(m->IsDirect());
1662 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1663 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1664
1665 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1666 ASSERT_TRUE(m != nullptr);
1667 ASSERT_TRUE(m->IsStatic());
1668 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1669 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1670}
1671
Calin Juravlecdd49122017-07-05 20:09:53 -07001672class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1673 protected:
1674 // Verifies that the class identified by the given descriptor is loaded with
1675 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1676 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1677 void VerifyClassResolution(const std::string& descriptor,
1678 jobject class_loader_to_search_obj,
1679 jobject expected_class_loader_obj,
1680 bool should_find = true) {
1681 Thread* self = Thread::Current();
1682 ScopedObjectAccess soa(self);
1683 StackHandleScope<3> hs(self);
1684 Handle<mirror::ClassLoader> class_loader_to_search(
1685 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1686
1687 Handle<mirror::Class> klass = hs.NewHandle(
1688 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1689
1690 if (!should_find) {
1691 if (self->IsExceptionPending()) {
1692 self->ClearException();
1693 }
1694 ASSERT_TRUE(klass == nullptr);
1695 } else if (expected_class_loader_obj == nullptr) {
1696 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1697 } else {
1698 ASSERT_TRUE(klass != nullptr) << descriptor;
1699 Handle<mirror::ClassLoader> expected_class_loader(
1700 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001701 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001702 }
1703 }
1704};
1705
1706TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1707 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1708 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1709 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001710 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001711}
1712
1713TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1714 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1715 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1716 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001717 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001718}
1719
1720TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1721 // The chain is
1722 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1723 // ^
1724 // |
1725 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1726 // ^
1727 // |
1728 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1729 // ^
1730 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001731 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001732
1733 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1734 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1735 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1736 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1737
1738 // Verify exclusive classes (present in only one class loader).
1739 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1740 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1741 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1742 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1743
1744 // Verify classes that are defined in multiple classloader.
1745
1746 // Classes defined in B should be found in B even if they are defined in A or C because
1747 // B is a DelegateLastClassLoader.
1748 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1749 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1750 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1751
1752 // Classes defined in D should be found in D even if they are defined in parent class loaders
1753 // as well because D is a DelegateLastClassLoader.
1754 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1755 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1756 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1757
1758
1759 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1760 // in the top parent.
1761 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1762
Calin Juravlecdd49122017-07-05 20:09:53 -07001763 // Sanity check that we don't find an undefined class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001764 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001765}
1766
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001767} // namespace art