blob: 010b384498d5e19a03ff515d277272c0ca36811f [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>
Vladimir Marko72101082019-02-05 16:16:30 +000021#include <string_view>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070022
Andreas Gampe9186ced2016-12-12 14:28:21 -080023#include "android-base/strings.h"
24
Mathieu Chartierc7853442015-03-27 14:35:38 -070025#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070026#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010029#include "class_root-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file_types.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080032#include "dex/signature-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/standard_dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "handle_scope-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070038#include "mirror/array-alloc-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070039#include "mirror/accessible_object.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000040#include "mirror/call_site.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070042#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010044#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010045#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070046#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010047#include "mirror/method_handle_impl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000048#include "mirror/method_handles_lookup.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000049#include "mirror/method_type-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070051#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "mirror/object_array-inl.h"
53#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070054#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070056#include "mirror/string-inl.h"
Orion Hodson005ac512017-10-24 15:43:43 +010057#include "mirror/var_handle.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070058#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070059#include "thread-current-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
61namespace art {
62
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080063class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070064 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070067 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070068 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070069 EXPECT_TRUE(self->IsExceptionPending());
Vladimir Markoe9987b02018-05-22 16:26:43 +010070 StackHandleScope<1> hs(self);
71 Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException());
Elliott Hughes885c3bd2011-08-22 16:59:20 -070072 self->ClearException();
Vladimir Markoe9987b02018-05-22 16:26:43 +010073 ObjPtr<mirror::Class> exception_class =
Ian Rogers98379392014-02-24 16:53:16 -080074 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070075 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 }
77
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080080 Thread* self = Thread::Current();
81 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070082 }
83
Vladimir Markoe9987b02018-05-22 16:26:43 +010084 void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070086 ASSERT_TRUE(primitive != nullptr);
87 ASSERT_TRUE(primitive->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +010088 ASSERT_OBJ_PTR_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070089 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070090 std::string temp;
91 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070092 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070094 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Vladimir Markobf121912019-06-04 13:49:05 +010095 EXPECT_EQ(ClassStatus::kVisiblyInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070096 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070097 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070098 EXPECT_TRUE(primitive->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +000099 EXPECT_FALSE(primitive->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700100 EXPECT_TRUE(primitive->IsVerified());
101 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700102 EXPECT_FALSE(primitive->IsArrayInstance());
103 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700104 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700105 EXPECT_FALSE(primitive->IsInterface());
106 EXPECT_TRUE(primitive->IsPublic());
107 EXPECT_TRUE(primitive->IsFinal());
108 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700109 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700110 EXPECT_EQ(0U, primitive->NumDirectMethods());
111 EXPECT_EQ(0U, primitive->NumVirtualMethods());
112 EXPECT_EQ(0U, primitive->NumInstanceFields());
113 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700114 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700115 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700116 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800117 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
118 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Nicolas Geoffray66934ef2021-07-07 14:56:23 +0100119 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700120 }
121
Vladimir Markoe9987b02018-05-22 16:26:43 +0100122 void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000124 ASSERT_TRUE(JavaLangObject != nullptr);
125 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100126 ASSERT_OBJ_PTR_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100127 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000128 std::string temp;
129 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
130 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
131 EXPECT_FALSE(JavaLangObject->HasSuperClass());
132 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Markobf121912019-06-04 13:49:05 +0100133 class_linker_->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
134 EXPECT_EQ(ClassStatus::kVisiblyInitialized, JavaLangObject->GetStatus());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000135 EXPECT_FALSE(JavaLangObject->IsErroneous());
136 EXPECT_TRUE(JavaLangObject->IsLoaded());
137 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000138 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000139 EXPECT_TRUE(JavaLangObject->IsVerified());
140 EXPECT_TRUE(JavaLangObject->IsInitialized());
141 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
142 EXPECT_FALSE(JavaLangObject->IsArrayClass());
143 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
144 EXPECT_FALSE(JavaLangObject->IsInterface());
145 EXPECT_TRUE(JavaLangObject->IsPublic());
146 EXPECT_FALSE(JavaLangObject->IsFinal());
147 EXPECT_FALSE(JavaLangObject->IsPrimitive());
148 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000149 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000150 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Vladimir Markofd846c52021-10-19 07:54:13 +0100151 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000152 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
153 "shadow$_klass_");
154 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
155 "shadow$_monitor_");
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000156
157 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
158 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
159
Andreas Gampe542451c2016-07-26 09:02:02 -0700160 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000161 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
162 ImTable* imt = JavaLangObject->GetImt(pointer_size);
163 ASSERT_NE(nullptr, imt);
164 for (size_t i = 0; i < ImTable::kSize; ++i) {
165 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
166 }
167 }
168
Brian Carlstromaded5f72011-10-07 17:15:04 -0700169 void AssertArrayClass(const std::string& array_descriptor,
170 const std::string& component_type,
Vladimir Markoe9987b02018-05-22 16:26:43 +0100171 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800173 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 StackHandleScope<2> hs(self);
175 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
176 Handle<mirror::Class> array(
177 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700178 std::string temp;
179 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100180 EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700181 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700182 AssertArrayClass(array_descriptor, array);
183 }
184
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700185 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800187 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700188 ASSERT_TRUE(array->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100189 ASSERT_OBJ_PTR_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700190 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700191 std::string temp;
192 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700193 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800194 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100195 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
196 array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700197 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700198 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700199 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Vladimir Markobf121912019-06-04 13:49:05 +0100200 EXPECT_EQ(ClassStatus::kVisiblyInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700201 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700202 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700203 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000204 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700205 EXPECT_TRUE(array->IsVerified());
206 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700207 EXPECT_FALSE(array->IsArrayInstance());
208 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700209 EXPECT_FALSE(array->IsInterface());
210 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
211 EXPECT_TRUE(array->IsFinal());
212 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700213 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700214 EXPECT_EQ(0U, array->NumDirectMethods());
215 EXPECT_EQ(0U, array->NumVirtualMethods());
216 EXPECT_EQ(0U, array->NumInstanceFields());
217 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700218 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000219 EXPECT_TRUE(array->ShouldHaveImt());
220 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700221 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700222 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Markob10668c2021-06-10 09:52:53 +0100223 ObjPtr<mirror::Class> direct_interface0 = array->GetDirectInterface(0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700224 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700225 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Markob10668c2021-06-10 09:52:53 +0100226 ObjPtr<mirror::Class> direct_interface1 = array->GetDirectInterface(1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700227 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Vladimir Markobcf17522018-06-01 13:14:32 +0100228 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000229
Andreas Gampe542451c2016-07-26 09:02:02 -0700230 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100231 ObjPtr<mirror::Class> JavaLangObject =
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000232 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
233 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
234 // IMT of a array class should be shared with the IMT of the java.lag.Object
235 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700236 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700237
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700239 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700241 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700242 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700243 }
244
Mathieu Chartier3398c782016-09-30 10:27:43 -0700245 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700247 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700248 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700249 EXPECT_TRUE(field->GetName() != nullptr);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000250 EXPECT_TRUE(field->ResolveType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700251 }
252
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700253 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 std::string temp;
256 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700258 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700259 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700260 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700261 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700262 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700263 EXPECT_TRUE(klass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100264 EXPECT_OBJ_PTR_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700265 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700266 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700267 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000268 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700269 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700271 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700272 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700273 std::string temp2;
274 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
275 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700276 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700277 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800278 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700279 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800280 EXPECT_TRUE(m.IsStatic());
281 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700283 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 if (!klass->IsSynthetic()) {
285 EXPECT_NE(0U, klass->NumDirectMethods());
286 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700287 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700288 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100289 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700290 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100291 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700292 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700293 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700294 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700295 } else {
Alex Light705ad492015-09-21 11:36:30 -0700296 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700297 }
298 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700299 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700300 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700301 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700302 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700303 }
304 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700305 EXPECT_FALSE(klass->IsAbstract());
306 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700307 }
308 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700309 EXPECT_FALSE(klass->IsFinal());
310 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311 }
312
313 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700314 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700315
Andreas Gampe542451c2016-07-26 09:02:02 -0700316 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 AssertMethod(&method);
318 EXPECT_TRUE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100319 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700320 }
321
Andreas Gampe542451c2016-07-26 09:02:02 -0700322 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 AssertMethod(&method);
324 EXPECT_FALSE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100325 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Alex Lighte64300b2015-12-15 15:02:47 -0800326 }
327
Andreas Gampe542451c2016-07-26 09:02:02 -0700328 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800329 AssertMethod(&method);
330 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800331 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800332 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700333 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800334 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700335 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700336 }
337
338 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700339 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700340 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700341 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700342 }
343
344 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700345 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700346 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700347 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700348 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700349
Vladimir Marko76649e82014-11-10 18:32:59 +0000350 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700351 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000352 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
353 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
354 klass->NumReferenceInstanceFields() *
355 sizeof(mirror::HeapReference<mirror::Object>));
356 MemberOffset current_ref_offset = start_ref_offset;
357 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700358 ArtField* field = klass->GetInstanceField(i);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000359 ObjPtr<mirror::Class> field_type = field->ResolveType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700360 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000361 if (!field->IsPrimitiveType()) {
362 ASSERT_TRUE(!field_type->IsPrimitive());
363 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
364 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
365 // While Reference.referent is not primitive, the ClassLinker
366 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700367 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000368 } else {
369 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
370 sizeof(mirror::HeapReference<mirror::Object>));
371 }
372 } else {
373 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
374 // Shuffled before references.
375 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
376 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
377 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700378 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700379 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000380 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700381
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700382 uint32_t total_num_reference_instance_fields = 0;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100383 ObjPtr<mirror::Class> k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700384 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700385 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
386 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700388 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
389 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
390 // The reference instance offsets have a bit set for each reference offset.
391 // +1 for Object's class.
392 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
393 total_num_reference_instance_fields);
394 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700395 }
396
Vladimir Markoe9987b02018-05-22 16:26:43 +0100397 void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700398 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000399 ASSERT_FALSE(descriptor.empty());
Ian Rogers98379392014-02-24 16:53:16 -0800400 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700401 StackHandleScope<1> hs(self);
402 Handle<mirror::Class> klass(
403 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800404 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700405 std::string temp;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100406 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
407 EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700408 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700409 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700410 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700411 AssertArrayClass(descriptor, klass);
412 } else {
413 AssertClass(descriptor, klass);
414 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415 }
416
Vladimir Markoe9987b02018-05-22 16:26:43 +0100417 void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700419 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800420 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800421 const dex::ClassDef& class_def = dex.GetClassDef(i);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800422 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700423 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700424 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700425 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800426 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800427 const dex::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800428 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700429 AssertDexFileClass(class_loader, descriptor);
430 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700431 TestRootVisitor visitor;
432 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700433 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700434
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700435 class TestRootVisitor : public SingleRootVisitor {
436 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100437 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700438 EXPECT_TRUE(root != nullptr);
439 }
440 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700441};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700442
Narayan Kamath25352fc2016-08-03 12:46:58 +0100443class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
444 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100445 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100446 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100447 }
448};
449
Brian Carlstrom693267a2011-09-06 09:25:34 -0700450struct CheckOffset {
451 size_t cpp_offset;
452 const char* java_name;
453 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
454};
455
Elliott Hughes80609252011-09-23 17:24:51 -0700456template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700457struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800458 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
459 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700460 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700461 std::string class_descriptor;
462 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700463
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700464 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800465 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100466 ObjPtr<mirror::Class> klass =
Ian Rogers98379392014-02-24 16:53:16 -0800467 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700468 CHECK(klass != nullptr) << class_descriptor;
Alex Lightf1b18fa2019-11-11 14:22:00 -0800469 CHECK(klass->IsMirrored()) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700470
471 bool error = false;
472
Mathieu Chartiere401d142015-04-22 13:56:20 -0700473 // Classes have a different size due to padding field. Strings are variable length.
474 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Vladimir Markoc7993d52021-01-27 15:20:56 +0000475 // The RoundUp is required for some mirror classes that have a gap at the end,
476 // such as AccessibleObject, ByteArrayViewVarHandle and ByteBufferViewVarHandle.
477 // For example, the AccessibleObject has size 9 according to the class linker.
478 // However, the C++ sizeof(AccessibleObject) is 12 bytes due to alignment, even
479 // though members in C++ subclasses are actually starting at offset 9.
480 //
481 // TODO: We could define a subclass with a `uint8_t` member and check its offset instead.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700482 static constexpr size_t kPackAlignment = 4;
Vladimir Markoc7993d52021-01-27 15:20:56 +0000483 size_t expected_size = RoundUp(klass->GetObjectSize(), kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700484 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800485 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700486 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700487 << " Java=" << expected_size
488 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700489 error = true;
490 }
491 }
492
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700494 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800495 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700496 << " class=" << class_descriptor
497 << " Java=" << num_fields
498 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700499 error = true;
500 }
501
Brian Carlstromdbc05252011-09-09 01:59:59 -0700502 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700503 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000504 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800505 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700506 error = true;
507 }
508 }
509 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700510 for (size_t i = 0; i < offsets.size(); i++) {
511 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700512 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000513 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800515 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700516 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800517 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700518 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800519 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700520 << " CheckOffsets=" << offset.java_name;
521 }
522 }
523
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 for (size_t i = 0; i < offsets.size(); i++) {
525 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700526 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700527 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
528 error = true;
529 }
530 }
531 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700532 for (size_t i = 0; i < offsets.size(); i++) {
533 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700534 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700535 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800536 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800538 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700539 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
540 }
541 }
542
543 return !error;
544 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700545
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700546 void addOffset(size_t offset, const char* name) {
547 offsets.push_back(CheckOffset(offset, name));
548 }
549
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700550 private:
551 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700552};
553
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700554// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
555// is first since if it is failing, others are unlikely to succeed.
556
Brian Carlstromea46f952013-07-30 01:26:50 -0700557struct ObjectOffsets : public CheckOffsets<mirror::Object> {
558 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700559 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
560 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800561 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700562};
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564struct ClassOffsets : public CheckOffsets<mirror::Class> {
565 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700566 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700567 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700568 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
569 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
570 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
571 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800572 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700573 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700574 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
575 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700577 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
578 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800579 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700581 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
582 "numReferenceInstanceFields");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
584 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700586 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
587 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700588 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
589 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
590 "referenceInstanceOffsets");
591 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
592 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
593 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800594 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700595 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800596 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700597};
598
Alex Lightd6251582016-10-31 11:12:30 -0700599struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
600 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Nicolas Geoffray4dc65892021-07-05 17:43:35 +0100601 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, erroneous_state_error_), "erroneousStateError");
Alex Light79d6c802019-06-27 15:50:11 +0000602 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, instance_jfield_ids_), "instanceJfieldIDs");
603 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, jmethod_ids_), "jmethodIDs");
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000604 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_class_), "obsoleteClass");
Alex Lighta01de592016-11-15 10:43:06 -0800605 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
606 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000607 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
David Brazdil1a658632018-12-01 17:54:26 +0000608 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
609 "preRedefineClassDefIndex");
610 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
611 "preRedefineDexFilePtr");
Alex Light79d6c802019-06-27 15:50:11 +0000612 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, static_jfield_ids_), "staticJfieldIDs");
Alex Lightd6251582016-10-31 11:12:30 -0700613 }
614};
615
Brian Carlstromea46f952013-07-30 01:26:50 -0700616struct StringOffsets : public CheckOffsets<mirror::String> {
617 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700618 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000619 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800620 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700621};
622
Brian Carlstromea46f952013-07-30 01:26:50 -0700623struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
624 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000625 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700626 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
627 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700628 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
629 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800630 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700631};
632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700634 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
635 false, "Ljava/lang/StackTraceElement;") {
636 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
637 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
638 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
639 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800640 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700641};
642
Brian Carlstromea46f952013-07-30 01:26:50 -0700643struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
644 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700645 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700646 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700647 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
648 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
649 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800650 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700651};
652
Brian Carlstromea46f952013-07-30 01:26:50 -0700653struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
654 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700655 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800656 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400657};
658
Brian Carlstromea46f952013-07-30 01:26:50 -0700659struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
660 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Andreas Gampe8a1a0f72020-03-03 16:07:45 -0800661 addOffset(OFFSETOF_MEMBER(mirror::DexCache, class_loader_), "classLoader");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700662 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
663 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700664 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000665 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100666 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100667 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100668 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
669 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
670 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800678 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700679};
680
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700681struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
682 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700683 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
684 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
685 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
686 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800687 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700688};
689
690struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700691 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
692 false, "Ljava/lang/ref/FinalizerReference;") {
693 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
694 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
695 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800696 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700697};
698
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700699struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700700 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
701 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000702 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800703 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700704};
705
706struct FieldOffsets : public CheckOffsets<mirror::Field> {
707 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700708 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
Alex Light5e251ea2020-01-09 10:55:49 -0800709 addOffset(OFFSETOF_MEMBER(mirror::Field, art_field_index_), "artFieldIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700710 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700711 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
712 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800713 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700714};
715
Neil Fuller16b21cd2016-08-12 09:37:02 +0100716struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
717 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
718 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100719 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
720 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
721 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
722 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
723 "declaringClassOfOverriddenMethod");
724 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100725 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
726 "hasRealParameterData");
727 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800728 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100729};
730
Narayan Kamathafa48272016-08-03 12:46:58 +0100731struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
732 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
733 false, "Ljava/lang/invoke/MethodType;") {
734 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
735 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
736 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
737 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
738 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
739 }
740};
741
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000742struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
743 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
744 false, "Ljava/lang/invoke/MethodHandle;") {
745 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
Orion Hodsonb08e1152022-01-05 14:29:05 +0000746 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, as_type_cache_), "asTypeCache");
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000747 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
748 "cachedSpreadInvoker");
749 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000750 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
751 }
752};
753
Narayan Kamathafa48272016-08-03 12:46:58 +0100754struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
755 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000756 false, "Ljava/lang/invoke/MethodHandleImpl;") {
757 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100758 }
759};
760
Orion Hodsonc069a302017-01-18 09:23:12 +0000761struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
762 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
763 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
764 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
765 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
766 }
767};
768
Narayan Kamath000e1882016-10-24 17:14:25 +0100769struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
770 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
771 false, "Ldalvik/system/EmulatedStackFrame;") {
772 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
773 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
774 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
775 }
776};
777
Orion Hodsonc069a302017-01-18 09:23:12 +0000778struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
779 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
780 false, "Ljava/lang/invoke/CallSite;") {
781 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
782 }
783};
784
Orion Hodson005ac512017-10-24 15:43:43 +0100785struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
786 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
787 false, "Ljava/lang/invoke/VarHandle;") {
788 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
789 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
790 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
791 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
792 }
793};
794
795struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
796 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
797 false, "Ljava/lang/invoke/FieldVarHandle;") {
798 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
799 }
800};
801
Orion Hodsondd411962021-06-25 08:55:22 +0100802struct StaticFieldVarHandleOffsets : public CheckOffsets<mirror::StaticFieldVarHandle> {
803 StaticFieldVarHandleOffsets() : CheckOffsets<mirror::StaticFieldVarHandle>(
804 false, "Ljava/lang/invoke/StaticFieldVarHandle;") {
805 addOffset(OFFSETOF_MEMBER(mirror::StaticFieldVarHandle, declaring_class_), "declaringClass");
806 }
807};
808
Orion Hodsonffad6682018-01-02 10:18:58 +0000809struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
810 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
811 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
812 }
813};
814
Orion Hodson005ac512017-10-24 15:43:43 +0100815struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
816 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
817 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
818 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
819 "nativeByteOrder");
820 }
821};
822
823struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
824 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
825 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
826 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
827 "nativeByteOrder");
828 }
829};
830
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400831// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400832// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700833// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400834TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700836 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700837 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700838 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700839 EXPECT_TRUE(StringOffsets().Check());
840 EXPECT_TRUE(ThrowableOffsets().Check());
841 EXPECT_TRUE(StackTraceElementOffsets().Check());
842 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400843 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700844 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700845 EXPECT_TRUE(ReferenceOffsets().Check());
846 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700847 EXPECT_TRUE(AccessibleObjectOffsets().Check());
848 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100849 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100850 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000851 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100852 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000853 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100854 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000855 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100856 EXPECT_TRUE(VarHandleOffsets().Check());
857 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000858 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100859 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
860 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700861}
862
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700863TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700865 AssertNonExistentClass("NoSuchClass;");
866 AssertNonExistentClass("LNoSuchClass;");
867}
868
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700869TEST_F(ClassLinkerTest, GetDexFiles) {
870 ScopedObjectAccess soa(Thread::Current());
871
872 jobject jclass_loader = LoadDex("Nested");
873 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
874 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800875 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700876
877 jobject jclass_loader2 = LoadDex("MultiDex");
878 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
879 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800880 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700881}
882
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700883TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700885 StackHandleScope<1> hs(soa.Self());
886 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700887 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700888
Vladimir Markoe9987b02018-05-22 16:26:43 +0100889 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700890 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700891 EXPECT_EQ(0U, outer->NumVirtualMethods());
892 EXPECT_EQ(1U, outer->NumDirectMethods());
893
Vladimir Markoe9987b02018-05-22 16:26:43 +0100894 ObjPtr<mirror::Class> inner =
895 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700896 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700897 EXPECT_EQ(0U, inner->NumVirtualMethods());
898 EXPECT_EQ(1U, inner->NumDirectMethods());
899}
900
901TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700903 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800904 for (int ch = 1; ch < 256; ++ch) {
905 std::string descriptor;
906 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700907 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700908 AssertNonExistentClass(descriptor);
909 } else {
910 AssertPrimitiveClass(descriptor);
911 }
912 }
913}
914
915TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700916 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100917 StackHandleScope<2> hs(soa.Self());
918 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
919 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
920 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700921
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700922 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700923 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700924 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100925 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700926 ASSERT_TRUE(MyClass != nullptr);
927 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100928 ASSERT_OBJ_PTR_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100929 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000930 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700931 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100932 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700933 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100934 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000935 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700936 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700937 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700938 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000939 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700940 EXPECT_FALSE(MyClass->IsVerified());
941 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700942 EXPECT_FALSE(MyClass->IsArrayInstance());
943 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700944 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700945 EXPECT_FALSE(MyClass->IsInterface());
946 EXPECT_FALSE(MyClass->IsPublic());
947 EXPECT_FALSE(MyClass->IsFinal());
948 EXPECT_FALSE(MyClass->IsPrimitive());
949 EXPECT_FALSE(MyClass->IsSynthetic());
950 EXPECT_EQ(1U, MyClass->NumDirectMethods());
951 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
952 EXPECT_EQ(0U, MyClass->NumInstanceFields());
953 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700954 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700955
Vladimir Markodfc0de72019-04-01 10:57:55 +0100956 EXPECT_OBJ_PTR_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700957
958 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700959 AssertArrayClass("[C", "C", nullptr);
960 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700961 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700962 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700963 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700964 // or not available at all
965 AssertNonExistentClass("[[[[LNonExistentClass;");
966}
967
Mathieu Chartierb8901302016-09-30 10:27:43 -0700968TEST_F(ClassLinkerTest, LookupResolvedType) {
969 ScopedObjectAccess soa(Thread::Current());
970 StackHandleScope<1> hs(soa.Self());
971 Handle<mirror::ClassLoader> class_loader(
972 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
973 AssertNonExistentClass("LMyClass;");
974 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800975 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700976 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700977 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000978 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700979 klass);
980 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000981 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700982 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
983 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000984 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700985 klass);
986}
987
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000988TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
989 ScopedObjectAccess soa(Thread::Current());
990 StackHandleScope<2> hs(soa.Self());
991 Handle<mirror::ClassLoader> class_loader(
992 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
993 // Get the AllFields class for the dex cache and dex file.
994 ObjPtr<mirror::Class> all_fields_klass
995 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000996 ASSERT_TRUE(all_fields_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000997 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
998 const DexFile& dex_file = *dex_cache->GetDexFile();
999 // Get the index of the array class we want to test.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001000 const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001001 ASSERT_TRUE(array_id != nullptr);
1002 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
1003 // Check that the array class wasn't resolved yet.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001004 EXPECT_TRUE(
1005 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001006 // 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);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001009 ASSERT_TRUE(array_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001010 // 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));
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001030 ASSERT_TRUE(klass != nullptr);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001031 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 Markob10668c2021-06-10 09:52:53 +01001174 ArtField* s0 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001179 ArtField* s1 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001184 ArtField* s2 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001189 ArtField* s3 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001194 ArtField* s4 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001199 ArtField* s5 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001204 ArtField* s6 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001209 ArtField* s7 = statics->FindStaticField("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 Markob10668c2021-06-10 09:52:53 +01001214 ArtField* s8 = statics->FindStaticField("s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001215 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001216 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001217 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001218 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001219
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001220 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1221 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001222 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1223 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1224 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1225 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1226 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1227 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001228 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1229 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001230 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001231}
1232
Brian Carlstrom30b94452011-08-25 21:35:26 -07001233TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001235 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001236 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001237 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001238 Handle<mirror::Class> I(
1239 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1240 Handle<mirror::Class> J(
1241 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1242 Handle<mirror::Class> K(
1243 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1244 Handle<mirror::Class> A(
1245 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1246 Handle<mirror::Class> B(
1247 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1248 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1249 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1250 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1251 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1252 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001253
Vladimir Markoeb37ba52019-02-05 14:10:38 +00001254 const std::string_view void_sig("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001255 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1256 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1257 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001258 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1259 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1260 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001261 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1262 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1263 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001264 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001265 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001266 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001267 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001268 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001269 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001270 ASSERT_TRUE(Kj1 != nullptr);
1271 ASSERT_TRUE(Kj2 != nullptr);
1272 ASSERT_TRUE(Kk != nullptr);
1273 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001274 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001275 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001276 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001277 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001278 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001279 EXPECT_NE(Ii, Ai);
1280 EXPECT_NE(Jj1, Aj1);
1281 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001282 EXPECT_EQ(Kj1, Jj1);
1283 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001284 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1285 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1286 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1287 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1288 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1289 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001290
Vladimir Markob10668c2021-06-10 09:52:53 +01001291 ArtField* Afoo = A->FindStaticField("foo", "Ljava/lang/String;");
1292 ArtField* Bfoo = B->FindStaticField("foo", "Ljava/lang/String;");
1293 ArtField* Jfoo = J->FindStaticField("foo", "Ljava/lang/String;");
1294 ArtField* Kfoo = K->FindStaticField("foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001295 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001296 EXPECT_EQ(Afoo, Bfoo);
1297 EXPECT_EQ(Afoo, Jfoo);
1298 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001299}
1300
Elliott Hughesf3778f62012-01-26 14:14:35 -08001301TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001302 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001303
jeffhaoabcfde32011-09-29 15:05:18 -07001304 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1305 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001306
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 ScopedObjectAccess soa(Thread::Current());
1308 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001309 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001310 StackHandleScope<1> hs(soa.Self());
1311 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001312 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001313 ObjPtr<mirror::Class> klass =
1314 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001315 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001316 ArtMethod* getS0 =
1317 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1318 ASSERT_TRUE(getS0 != nullptr);
1319 ASSERT_TRUE(getS0->IsStatic());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001320 const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001321 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001322 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001323 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1324 clinit,
1325 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001326 /* can_run_clinit= */ true,
1327 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001328 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001329 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001330 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1331 getS0,
1332 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001333 /* can_run_clinit= */ true,
1334 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001335 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001336 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001337}
1338
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001339TEST_F(ClassLinkerTest, ErroneousClass) {
1340 ScopedObjectAccess soa(Thread::Current());
1341 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1342 StackHandleScope<1> hs(soa.Self());
1343 Handle<mirror::ClassLoader> class_loader(
1344 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1345 hs.Self()->AssertNoPendingException();
1346 const char* descriptor = "LErroneous;";
1347 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1348 // Erronenous since we are extending final class.
1349 hs.Self()->AssertPendingException();
1350 EXPECT_TRUE(klass == nullptr);
1351 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1352 EXPECT_FALSE(klass == nullptr);
1353 EXPECT_TRUE(klass->IsErroneous());
1354 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1355}
1356
Elliott Hughes20cde902011-10-04 17:37:27 -07001357TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001359 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001360
1361 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001362 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001363 EXPECT_FALSE(c->IsFinalizable());
1364
1365 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001366 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001367 EXPECT_FALSE(c->IsFinalizable());
1368
1369 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001370 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001371 EXPECT_FALSE(c->IsFinalizable());
1372
1373 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001374 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001375 EXPECT_TRUE(c->IsFinalizable());
1376
1377 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001378 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001379 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001380 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001381 EXPECT_TRUE(c->IsFinalizable());
1382
1383 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1384 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001385 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001386 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001387 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001388 EXPECT_TRUE(c->IsFinalizable());
1389}
1390
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001391TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001393 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001394 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1395 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001396 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001397 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001398 }
1399}
1400
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001401TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1402 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001403 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001404 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001405
1406 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001407 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001408 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001409
1410 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001411 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001412 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001413
1414 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001415 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001416 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001417
1418 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001419 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001420 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001421}
1422
Mathieu Chartiere401d142015-04-22 13:56:20 -07001423static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001424 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001425 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001426 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001427 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001428 }
1429}
1430
Vladimir Markoe9987b02018-05-22 16:26:43 +01001431static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001432 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001433 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001434 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001435 }
1436}
1437
1438TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1439 ScopedObjectAccess soa(Thread::Current());
1440
Vladimir Markoe9987b02018-05-22 16:26:43 +01001441 ObjPtr<mirror::Class> JavaLangObject =
1442 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001443 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001444 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1445 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001446 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001447}
1448
1449TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1450 ScopedObjectAccess soa(Thread::Current());
1451
1452 StackHandleScope<1> hs(soa.Self());
1453
1454 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1455 soa.Self(), "Ljava/lang/SecurityManager;")));
1456 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1457 "core";
1458
Igor Murashkindf707e42016-02-02 16:56:50 -08001459 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001460
Ian Rogers7b078e82014-09-10 14:44:24 -07001461 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001462 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001463}
1464
1465TEST_F(ClassLinkerTest, Preverified_App) {
1466 ScopedObjectAccess soa(Thread::Current());
1467
1468 StackHandleScope<2> hs(soa.Self());
1469 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001470 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001471 Handle<mirror::Class> statics(
1472 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1473
Igor Murashkindf707e42016-02-02 16:56:50 -08001474 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001475
Ian Rogers7b078e82014-09-10 14:44:24 -07001476 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001477 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001478}
1479
Sebastien Hertz6963e442014-11-26 22:11:27 +01001480TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1481 ScopedObjectAccess soa(Thread::Current());
1482
1483 StackHandleScope<3> hs(soa.Self());
1484 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001485 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001486
1487 // java.lang.Object is a bootstrap class.
1488 Handle<mirror::Class> jlo_class(
1489 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001490 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001491 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001492
1493 // Statics is not a bootstrap class.
1494 Handle<mirror::Class> statics(
1495 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001496 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001497 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001498}
1499
Mathieu Chartier76172162016-01-26 14:54:06 -08001500// Regression test for b/26799552.
1501TEST_F(ClassLinkerTest, RegisterDexFileName) {
1502 ScopedObjectAccess soa(Thread::Current());
1503 StackHandleScope<2> hs(soa.Self());
1504 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1505 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1506 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001507 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
David Srbecky6fbcc292021-02-23 01:05:32 +00001508 for (const auto& entry : class_linker->GetDexCachesData()) {
1509 const ClassLinker::DexCacheData& data = entry.second;
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001510 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001511 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001512 break;
1513 }
1514 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001515 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001516 }
Mathieu Chartier76172162016-01-26 14:54:06 -08001517 const uint16_t data[] = { 0x20AC, 0x20A1 };
1518 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1519 arraysize(data),
1520 data)));
Mathieu Chartier76172162016-01-26 14:54:06 -08001521 const DexFile* old_dex_file = dex_cache->GetDexFile();
1522
David Sehr0b426772018-07-03 23:03:42 +00001523 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1524 old_dex_file->Size(),
1525 location->ToModifiedUtf8(),
1526 0u,
1527 nullptr,
1528 nullptr));
David Srbecky86d6cd52020-12-02 18:13:10 +00001529 // Make a copy of the dex cache with changed name.
David Srbecky33df0e32021-09-30 14:36:32 +00001530 dex_cache.Assign(class_linker->AllocAndInitializeDexCache(Thread::Current(),
1531 *dex_file,
1532 /* class_loader= */ nullptr));
David Srbecky86d6cd52020-12-02 18:13:10 +00001533 DCHECK_EQ(dex_cache->GetLocation()->CompareTo(location.Get()), 0);
Mathieu Chartier76172162016-01-26 14:54:06 -08001534 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001535 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001536 // Check that inserting with a UTF16 name works.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001537 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001538 }
1539}
1540
Narayan Kamath25352fc2016-08-03 12:46:58 +01001541TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1542 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001543 StackHandleScope<7> hs(soa.Self());
1544
1545 Handle<mirror::ClassLoader> class_loader(
1546 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1547 Handle<mirror::Class> method_types(
1548 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1549 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1550
Vladimir Markoba118822017-06-12 15:41:56 +01001551 ArtMethod* method1 = method_types->FindClassMethod(
1552 "method1",
1553 "(Ljava/lang/String;)Ljava/lang/String;",
1554 kRuntimePointerSize);
1555 ASSERT_TRUE(method1 != nullptr);
1556 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001557
1558 const DexFile& dex_file = *(method1->GetDexFile());
1559 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001560 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001561
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001562 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001563
1564 // This is the MethodType corresponding to the prototype of
1565 // String MethodTypes# method1(String).
1566 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001567 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001568 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001569 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001570
1571 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001572 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001573
1574 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001575 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1576 "Ljava/lang/String;",
1577 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001578 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1579 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001580
1581 // Resolve the method type again and assert that we get back the same value.
1582 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001583 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001584 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001585
1586 // Resolve the MethodType associated with a different method signature
1587 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001588 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001589 "method2",
1590 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1591 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001592 ASSERT_TRUE(method2 != nullptr);
1593 ASSERT_FALSE(method2->IsDirect());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001594 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001595 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001596 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001597 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001598}
1599
Calin Juravle7865ac72017-06-28 11:03:12 -07001600// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1601// by creating a chain of class loaders with various dex files.
1602TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1603 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1604 // No need to check again.
1605 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1606 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1607 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1608 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1609}
1610
David Sehrc431b9d2018-03-02 12:01:51 -08001611TEST_F(ClassLinkerTest, PrettyClass) {
1612 ScopedObjectAccess soa(Thread::Current());
1613 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001614 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001615 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001616 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001617 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1618}
1619
1620TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1621 ScopedObjectAccess soa(Thread::Current());
1622 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001623 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001624 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001625 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001626 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1627 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1628}
1629
1630TEST_F(ClassLinkerTest, PrettyField) {
1631 ScopedObjectAccess soa(Thread::Current());
1632 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1633
Vladimir Markoe9987b02018-05-22 16:26:43 +01001634 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1635 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001636
1637 ArtField* f;
1638 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1639 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1640 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1641}
1642
1643TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1644 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001645 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001646 ASSERT_TRUE(c != nullptr);
1647 ArtMethod* m;
1648
1649 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1650 ASSERT_TRUE(m != nullptr);
1651 ASSERT_FALSE(m->IsDirect());
1652 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1653 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1654
1655 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1656 ASSERT_TRUE(m != nullptr);
1657 ASSERT_FALSE(m->IsDirect());
1658 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1659 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1660
1661 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1662 ASSERT_TRUE(m != nullptr);
1663 ASSERT_TRUE(m->IsStatic());
1664 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1665 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1666}
1667
Calin Juravlecdd49122017-07-05 20:09:53 -07001668class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1669 protected:
1670 // Verifies that the class identified by the given descriptor is loaded with
1671 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1672 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1673 void VerifyClassResolution(const std::string& descriptor,
1674 jobject class_loader_to_search_obj,
1675 jobject expected_class_loader_obj,
1676 bool should_find = true) {
1677 Thread* self = Thread::Current();
1678 ScopedObjectAccess soa(self);
1679 StackHandleScope<3> hs(self);
1680 Handle<mirror::ClassLoader> class_loader_to_search(
1681 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1682
1683 Handle<mirror::Class> klass = hs.NewHandle(
1684 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1685
1686 if (!should_find) {
1687 if (self->IsExceptionPending()) {
1688 self->ClearException();
1689 }
1690 ASSERT_TRUE(klass == nullptr);
1691 } else if (expected_class_loader_obj == nullptr) {
1692 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1693 } else {
1694 ASSERT_TRUE(klass != nullptr) << descriptor;
1695 Handle<mirror::ClassLoader> expected_class_loader(
1696 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001697 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001698 }
1699 }
1700};
1701
1702TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1703 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1704 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1705 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001706 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001707}
1708
1709TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1710 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1711 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1712 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001713 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001714}
1715
1716TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1717 // The chain is
1718 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1719 // ^
1720 // |
1721 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1722 // ^
1723 // |
1724 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1725 // ^
1726 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001727 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001728
1729 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1730 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1731 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1732 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1733
1734 // Verify exclusive classes (present in only one class loader).
1735 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1736 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1737 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1738 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1739
1740 // Verify classes that are defined in multiple classloader.
1741
1742 // Classes defined in B should be found in B even if they are defined in A or C because
1743 // B is a DelegateLastClassLoader.
1744 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1745 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1746 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1747
1748 // Classes defined in D should be found in D even if they are defined in parent class loaders
1749 // as well because D is a DelegateLastClassLoader.
1750 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1751 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1752 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1753
1754
1755 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1756 // in the top parent.
1757 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1758
Ian Pedowitz2d536432020-07-22 14:33:00 -07001759 // Check that we don't find an undefined class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001760 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001761}
1762
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001763} // namespace art