blob: c561c4d5911caf255b777b8ea8d833f4ec337d0d [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);
Andreas Gampe5b20b352018-10-11 19:03:20 -0700119 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted,
120 primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700121 }
122
Vladimir Markoe9987b02018-05-22 16:26:43 +0100123 void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000125 ASSERT_TRUE(JavaLangObject != nullptr);
126 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100127 ASSERT_OBJ_PTR_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100128 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000129 std::string temp;
130 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
131 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
132 EXPECT_FALSE(JavaLangObject->HasSuperClass());
133 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Markobf121912019-06-04 13:49:05 +0100134 class_linker_->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
135 EXPECT_EQ(ClassStatus::kVisiblyInitialized, JavaLangObject->GetStatus());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000136 EXPECT_FALSE(JavaLangObject->IsErroneous());
137 EXPECT_TRUE(JavaLangObject->IsLoaded());
138 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000139 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000140 EXPECT_TRUE(JavaLangObject->IsVerified());
141 EXPECT_TRUE(JavaLangObject->IsInitialized());
142 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
143 EXPECT_FALSE(JavaLangObject->IsArrayClass());
144 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
145 EXPECT_FALSE(JavaLangObject->IsInterface());
146 EXPECT_TRUE(JavaLangObject->IsPublic());
147 EXPECT_FALSE(JavaLangObject->IsFinal());
148 EXPECT_FALSE(JavaLangObject->IsPrimitive());
149 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000150 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000151 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
152 if (!kUseBrooksReadBarrier) {
153 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
154 } else {
155 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
156 }
157 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
158 "shadow$_klass_");
159 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
160 "shadow$_monitor_");
161 if (kUseBrooksReadBarrier) {
162 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
163 "shadow$_x_rb_ptr_");
164 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
165 "shadow$_x_xpadding_");
166 }
167
168 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
169 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
170
Andreas Gampe542451c2016-07-26 09:02:02 -0700171 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000172 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
173 ImTable* imt = JavaLangObject->GetImt(pointer_size);
174 ASSERT_NE(nullptr, imt);
175 for (size_t i = 0; i < ImTable::kSize; ++i) {
176 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
177 }
178 }
179
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 void AssertArrayClass(const std::string& array_descriptor,
181 const std::string& component_type,
Vladimir Markoe9987b02018-05-22 16:26:43 +0100182 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 StackHandleScope<2> hs(self);
186 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
187 Handle<mirror::Class> array(
188 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 std::string temp;
190 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100191 EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700192 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700193 AssertArrayClass(array_descriptor, array);
194 }
195
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700196 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800198 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700199 ASSERT_TRUE(array->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100200 ASSERT_OBJ_PTR_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700201 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 std::string temp;
203 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700204 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800205 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100206 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
207 array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700208 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700209 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700210 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Vladimir Markobf121912019-06-04 13:49:05 +0100211 EXPECT_EQ(ClassStatus::kVisiblyInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700212 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700213 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700214 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000215 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700216 EXPECT_TRUE(array->IsVerified());
217 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700218 EXPECT_FALSE(array->IsArrayInstance());
219 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700220 EXPECT_FALSE(array->IsInterface());
221 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
222 EXPECT_TRUE(array->IsFinal());
223 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700225 EXPECT_EQ(0U, array->NumDirectMethods());
226 EXPECT_EQ(0U, array->NumVirtualMethods());
227 EXPECT_EQ(0U, array->NumInstanceFields());
228 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000230 EXPECT_TRUE(array->ShouldHaveImt());
231 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700232 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700233 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000234 ObjPtr<mirror::Class> direct_interface0 =
235 mirror::Class::GetDirectInterface(self, array.Get(), 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700236 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700237 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Marko19a4d372016-12-08 14:41:46 +0000238 ObjPtr<mirror::Class> direct_interface1 =
239 mirror::Class::GetDirectInterface(self, array.Get(), 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700240 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Vladimir Markobcf17522018-06-01 13:14:32 +0100241 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000242
Andreas Gampe542451c2016-07-26 09:02:02 -0700243 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100244 ObjPtr<mirror::Class> JavaLangObject =
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000245 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
246 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
247 // IMT of a array class should be shared with the IMT of the java.lag.Object
248 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700249 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700252 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700254 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700255 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700256 }
257
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700260 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700261 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700262 EXPECT_TRUE(field->GetName() != nullptr);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000263 EXPECT_TRUE(field->ResolveType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700264 }
265
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700266 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700268 std::string temp;
269 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700271 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700274 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700275 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700276 EXPECT_TRUE(klass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100277 EXPECT_OBJ_PTR_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700278 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700279 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700280 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000281 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700282 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700285 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700286 std::string temp2;
287 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
288 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700289 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700290 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800291 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700292 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800293 EXPECT_TRUE(m.IsStatic());
294 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700295 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700296 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700297 if (!klass->IsSynthetic()) {
298 EXPECT_NE(0U, klass->NumDirectMethods());
299 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700300 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700301 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100302 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700303 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100304 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700305 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700306 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700307 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700308 } else {
Alex Light705ad492015-09-21 11:36:30 -0700309 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700310 }
311 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700312 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700313 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700314 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700316 }
317 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700318 EXPECT_FALSE(klass->IsAbstract());
319 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700320 }
321 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 EXPECT_FALSE(klass->IsFinal());
323 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700324 }
325
326 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700327 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700328
Andreas Gampe542451c2016-07-26 09:02:02 -0700329 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 AssertMethod(&method);
331 EXPECT_TRUE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100332 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700333 }
334
Andreas Gampe542451c2016-07-26 09:02:02 -0700335 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 AssertMethod(&method);
337 EXPECT_FALSE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100338 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Alex Lighte64300b2015-12-15 15:02:47 -0800339 }
340
Andreas Gampe542451c2016-07-26 09:02:02 -0700341 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800342 AssertMethod(&method);
343 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800344 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800345 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700346 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800347 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700348 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700349 }
350
351 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700352 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700353 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700354 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700355 }
356
357 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700358 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700359 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700360 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700361 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700362
Vladimir Marko76649e82014-11-10 18:32:59 +0000363 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700364 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000365 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
366 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
367 klass->NumReferenceInstanceFields() *
368 sizeof(mirror::HeapReference<mirror::Object>));
369 MemberOffset current_ref_offset = start_ref_offset;
370 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700371 ArtField* field = klass->GetInstanceField(i);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000372 ObjPtr<mirror::Class> field_type = field->ResolveType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700373 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000374 if (!field->IsPrimitiveType()) {
375 ASSERT_TRUE(!field_type->IsPrimitive());
376 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
377 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
378 // While Reference.referent is not primitive, the ClassLinker
379 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700380 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000381 } else {
382 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
383 sizeof(mirror::HeapReference<mirror::Object>));
384 }
385 } else {
386 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
387 // Shuffled before references.
388 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
389 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
390 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700391 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700392 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000393 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700394
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700395 uint32_t total_num_reference_instance_fields = 0;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100396 ObjPtr<mirror::Class> k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700397 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700398 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
399 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700400 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700401 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
402 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
403 // The reference instance offsets have a bit set for each reference offset.
404 // +1 for Object's class.
405 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
406 total_num_reference_instance_fields);
407 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700408 }
409
Vladimir Markoe9987b02018-05-22 16:26:43 +0100410 void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700411 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000412 ASSERT_FALSE(descriptor.empty());
Ian Rogers98379392014-02-24 16:53:16 -0800413 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 StackHandleScope<1> hs(self);
415 Handle<mirror::Class> klass(
416 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800417 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 std::string temp;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100419 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
420 EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700422 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700423 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700424 AssertArrayClass(descriptor, klass);
425 } else {
426 AssertClass(descriptor, klass);
427 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700428 }
429
Vladimir Markoe9987b02018-05-22 16:26:43 +0100430 void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700431 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700432 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800433 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800434 const dex::ClassDef& class_def = dex.GetClassDef(i);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800435 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700436 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700437 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700438 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800439 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800440 const dex::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800441 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700442 AssertDexFileClass(class_loader, descriptor);
443 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700444 TestRootVisitor visitor;
445 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700446 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700447
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700448 class TestRootVisitor : public SingleRootVisitor {
449 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100450 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700451 EXPECT_TRUE(root != nullptr);
452 }
453 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700455
Narayan Kamath25352fc2016-08-03 12:46:58 +0100456class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
457 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100458 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100459 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100460 }
461};
462
Brian Carlstrom693267a2011-09-06 09:25:34 -0700463struct CheckOffset {
464 size_t cpp_offset;
465 const char* java_name;
466 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
467};
468
Elliott Hughes80609252011-09-23 17:24:51 -0700469template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700470struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800471 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
472 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700474 std::string class_descriptor;
475 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700476
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700477 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800478 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100479 ObjPtr<mirror::Class> klass =
Ian Rogers98379392014-02-24 16:53:16 -0800480 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700481 CHECK(klass != nullptr) << class_descriptor;
Alex Lightf1b18fa2019-11-11 14:22:00 -0800482 CHECK(klass->IsMirrored()) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700483
484 bool error = false;
485
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 // Classes have a different size due to padding field. Strings are variable length.
487 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Vladimir Markoc7993d52021-01-27 15:20:56 +0000488 // The RoundUp is required for some mirror classes that have a gap at the end,
489 // such as AccessibleObject, ByteArrayViewVarHandle and ByteBufferViewVarHandle.
490 // For example, the AccessibleObject has size 9 according to the class linker.
491 // However, the C++ sizeof(AccessibleObject) is 12 bytes due to alignment, even
492 // though members in C++ subclasses are actually starting at offset 9.
493 //
494 // TODO: We could define a subclass with a `uint8_t` member and check its offset instead.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700495 static constexpr size_t kPackAlignment = 4;
Vladimir Markoc7993d52021-01-27 15:20:56 +0000496 size_t expected_size = RoundUp(klass->GetObjectSize(), kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700497 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800498 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700499 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700500 << " Java=" << expected_size
501 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700502 error = true;
503 }
504 }
505
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700506 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700507 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800508 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700509 << " class=" << class_descriptor
510 << " Java=" << num_fields
511 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700512 error = true;
513 }
514
Brian Carlstromdbc05252011-09-09 01:59:59 -0700515 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700516 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000517 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 error = true;
520 }
521 }
522 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700523 for (size_t i = 0; i < offsets.size(); i++) {
524 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700525 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000526 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800528 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700529 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800530 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700531 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800532 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700533 << " CheckOffsets=" << offset.java_name;
534 }
535 }
536
Brian Carlstromdbc05252011-09-09 01:59:59 -0700537 for (size_t i = 0; i < offsets.size(); i++) {
538 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700539 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
541 error = true;
542 }
543 }
544 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700545 for (size_t i = 0; i < offsets.size(); i++) {
546 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700547 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800549 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700550 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800551 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700552 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
553 }
554 }
555
556 return !error;
557 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700558
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700559 void addOffset(size_t offset, const char* name) {
560 offsets.push_back(CheckOffset(offset, name));
561 }
562
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 private:
564 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700565};
566
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700567// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
568// is first since if it is failing, others are unlikely to succeed.
569
Brian Carlstromea46f952013-07-30 01:26:50 -0700570struct ObjectOffsets : public CheckOffsets<mirror::Object> {
571 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700572 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
573 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700574#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700575 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
576 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800577#endif
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800578 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700579};
580
Brian Carlstromea46f952013-07-30 01:26:50 -0700581struct ClassOffsets : public CheckOffsets<mirror::Class> {
582 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700583 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700584 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
586 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
587 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800589 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700590 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700591 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
592 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700593 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700594 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
595 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800596 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700597 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
599 "numReferenceInstanceFields");
600 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
601 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700602 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700603 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
604 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700605 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
606 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
607 "referenceInstanceOffsets");
608 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
609 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
610 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800611 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700612 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800613 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700614};
615
Alex Lightd6251582016-10-31 11:12:30 -0700616struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
617 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Light79d6c802019-06-27 15:50:11 +0000618 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, instance_jfield_ids_), "instanceJfieldIDs");
619 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, jmethod_ids_), "jmethodIDs");
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000620 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_class_), "obsoleteClass");
Alex Lighta01de592016-11-15 10:43:06 -0800621 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
622 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000623 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
David Brazdil1a658632018-12-01 17:54:26 +0000624 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
625 "preRedefineClassDefIndex");
626 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
627 "preRedefineDexFilePtr");
Alex Light79d6c802019-06-27 15:50:11 +0000628 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, static_jfield_ids_), "staticJfieldIDs");
Alex Lightd6251582016-10-31 11:12:30 -0700629 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
630 }
631};
632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633struct StringOffsets : public CheckOffsets<mirror::String> {
634 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700635 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000636 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800637 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700638};
639
Brian Carlstromea46f952013-07-30 01:26:50 -0700640struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
641 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000642 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700643 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
644 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700645 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
646 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800647 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700648};
649
Brian Carlstromea46f952013-07-30 01:26:50 -0700650struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700651 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
652 false, "Ljava/lang/StackTraceElement;") {
653 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
654 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
655 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
656 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800657 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700658};
659
Brian Carlstromea46f952013-07-30 01:26:50 -0700660struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
661 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700662 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700663 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700664 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
665 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
666 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800667 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700668};
669
Brian Carlstromea46f952013-07-30 01:26:50 -0700670struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
671 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700672 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800673 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400674};
675
Brian Carlstromea46f952013-07-30 01:26:50 -0700676struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
677 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Andreas Gampe8a1a0f72020-03-03 16:07:45 -0800678 addOffset(OFFSETOF_MEMBER(mirror::DexCache, class_loader_), "classLoader");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700679 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
680 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700681 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000682 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100683 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100684 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100685 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
686 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
687 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700688 addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000689 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700690 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100691 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700692 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
693 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
694 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800695 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700696};
697
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700698struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
699 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700700 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
701 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
702 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
703 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800704 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700705};
706
707struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700708 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
709 false, "Ljava/lang/ref/FinalizerReference;") {
710 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
711 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
712 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800713 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700714};
715
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700716struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700717 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
718 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000719 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800720 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700721};
722
723struct FieldOffsets : public CheckOffsets<mirror::Field> {
724 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700725 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
Alex Light5e251ea2020-01-09 10:55:49 -0800726 addOffset(OFFSETOF_MEMBER(mirror::Field, art_field_index_), "artFieldIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700727 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700728 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
729 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800730 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700731};
732
Neil Fuller16b21cd2016-08-12 09:37:02 +0100733struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
734 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
735 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100736 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
737 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
738 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
739 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
740 "declaringClassOfOverriddenMethod");
741 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100742 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
743 "hasRealParameterData");
744 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800745 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100746};
747
Narayan Kamathafa48272016-08-03 12:46:58 +0100748struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
749 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
750 false, "Ljava/lang/invoke/MethodType;") {
751 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
752 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
753 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
754 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
755 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
756 }
757};
758
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000759struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
760 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
761 false, "Ljava/lang/invoke/MethodHandle;") {
762 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
763 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
764 "cachedSpreadInvoker");
765 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
766 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
767 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
768 }
769};
770
Narayan Kamathafa48272016-08-03 12:46:58 +0100771struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
772 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000773 false, "Ljava/lang/invoke/MethodHandleImpl;") {
774 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100775 }
776};
777
Orion Hodsonc069a302017-01-18 09:23:12 +0000778struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
779 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
780 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
781 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
782 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
783 }
784};
785
Narayan Kamath000e1882016-10-24 17:14:25 +0100786struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
787 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
788 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000789 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100790 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
791 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
792 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
793 }
794};
795
Orion Hodsonc069a302017-01-18 09:23:12 +0000796struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
797 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
798 false, "Ljava/lang/invoke/CallSite;") {
799 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
800 }
801};
802
Orion Hodson005ac512017-10-24 15:43:43 +0100803struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
804 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
805 false, "Ljava/lang/invoke/VarHandle;") {
806 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
807 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
808 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
809 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
810 }
811};
812
813struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
814 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
815 false, "Ljava/lang/invoke/FieldVarHandle;") {
816 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
817 }
818};
819
Orion Hodsonffad6682018-01-02 10:18:58 +0000820struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
821 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
822 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
823 }
824};
825
Orion Hodson005ac512017-10-24 15:43:43 +0100826struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
827 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
828 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
829 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
830 "nativeByteOrder");
831 }
832};
833
834struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
835 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
836 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
837 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
838 "nativeByteOrder");
839 }
840};
841
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400842// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400843// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700844// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400845TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700847 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700848 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700849 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700850 EXPECT_TRUE(StringOffsets().Check());
851 EXPECT_TRUE(ThrowableOffsets().Check());
852 EXPECT_TRUE(StackTraceElementOffsets().Check());
853 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400854 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700855 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700856 EXPECT_TRUE(ReferenceOffsets().Check());
857 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700858 EXPECT_TRUE(AccessibleObjectOffsets().Check());
859 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100860 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100861 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000862 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100863 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000864 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100865 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000866 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100867 EXPECT_TRUE(VarHandleOffsets().Check());
868 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000869 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100870 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
871 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700872}
873
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700874TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700876 AssertNonExistentClass("NoSuchClass;");
877 AssertNonExistentClass("LNoSuchClass;");
878}
879
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700880TEST_F(ClassLinkerTest, GetDexFiles) {
881 ScopedObjectAccess soa(Thread::Current());
882
883 jobject jclass_loader = LoadDex("Nested");
884 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
885 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800886 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700887
888 jobject jclass_loader2 = LoadDex("MultiDex");
889 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
890 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800891 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700892}
893
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700894TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700896 StackHandleScope<1> hs(soa.Self());
897 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700898 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899
Vladimir Markoe9987b02018-05-22 16:26:43 +0100900 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700901 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700902 EXPECT_EQ(0U, outer->NumVirtualMethods());
903 EXPECT_EQ(1U, outer->NumDirectMethods());
904
Vladimir Markoe9987b02018-05-22 16:26:43 +0100905 ObjPtr<mirror::Class> inner =
906 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700907 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700908 EXPECT_EQ(0U, inner->NumVirtualMethods());
909 EXPECT_EQ(1U, inner->NumDirectMethods());
910}
911
912TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700914 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800915 for (int ch = 1; ch < 256; ++ch) {
916 std::string descriptor;
917 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700918 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700919 AssertNonExistentClass(descriptor);
920 } else {
921 AssertPrimitiveClass(descriptor);
922 }
923 }
924}
925
926TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100928 StackHandleScope<2> hs(soa.Self());
929 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
930 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
931 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700932
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700933 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700934 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700935 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100936 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700937 ASSERT_TRUE(MyClass != nullptr);
938 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100939 ASSERT_OBJ_PTR_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100940 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000941 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700942 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100943 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700944 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100945 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000946 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700947 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700948 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700949 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000950 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700951 EXPECT_FALSE(MyClass->IsVerified());
952 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700953 EXPECT_FALSE(MyClass->IsArrayInstance());
954 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700955 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700956 EXPECT_FALSE(MyClass->IsInterface());
957 EXPECT_FALSE(MyClass->IsPublic());
958 EXPECT_FALSE(MyClass->IsFinal());
959 EXPECT_FALSE(MyClass->IsPrimitive());
960 EXPECT_FALSE(MyClass->IsSynthetic());
961 EXPECT_EQ(1U, MyClass->NumDirectMethods());
962 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
963 EXPECT_EQ(0U, MyClass->NumInstanceFields());
964 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700965 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700966
Vladimir Markodfc0de72019-04-01 10:57:55 +0100967 EXPECT_OBJ_PTR_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700968
969 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700970 AssertArrayClass("[C", "C", nullptr);
971 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700972 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700973 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700974 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700975 // or not available at all
976 AssertNonExistentClass("[[[[LNonExistentClass;");
977}
978
Mathieu Chartierb8901302016-09-30 10:27:43 -0700979TEST_F(ClassLinkerTest, LookupResolvedType) {
980 ScopedObjectAccess soa(Thread::Current());
981 StackHandleScope<1> hs(soa.Self());
982 Handle<mirror::ClassLoader> class_loader(
983 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
984 AssertNonExistentClass("LMyClass;");
985 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800986 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700987 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700988 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000989 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700990 klass);
991 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000992 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700993 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
994 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000995 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700996 klass);
997}
998
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000999TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
1000 ScopedObjectAccess soa(Thread::Current());
1001 StackHandleScope<2> hs(soa.Self());
1002 Handle<mirror::ClassLoader> class_loader(
1003 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
1004 // Get the AllFields class for the dex cache and dex file.
1005 ObjPtr<mirror::Class> all_fields_klass
1006 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001007 ASSERT_TRUE(all_fields_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001008 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
1009 const DexFile& dex_file = *dex_cache->GetDexFile();
1010 // Get the index of the array class we want to test.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001011 const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001012 ASSERT_TRUE(array_id != nullptr);
1013 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
1014 // Check that the array class wasn't resolved yet.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001015 EXPECT_TRUE(
1016 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001017 // Resolve the array class we want to test.
1018 ObjPtr<mirror::Class> array_klass
1019 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001020 ASSERT_TRUE(array_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001021 // Test that LookupResolvedType() finds the array class.
1022 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001023 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001024 array_klass);
1025 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001026 dex_cache->ClearResolvedType(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001027 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
1028 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001029 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001030 array_klass);
1031}
1032
Vladimir Marko72ab6842017-01-20 19:32:50 +00001033TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
1034 ScopedObjectAccess soa(Thread::Current());
1035 StackHandleScope<3> hs(soa.Self());
1036 Handle<mirror::ClassLoader> class_loader(
1037 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
1038 AssertNonExistentClass("LErroneousInit;");
1039 Handle<mirror::Class> klass =
1040 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001041 ASSERT_TRUE(klass != nullptr);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001042 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
1043 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Vladimir Marko72ab6842017-01-20 19:32:50 +00001044 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001045 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001046 klass.Get());
1047 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001048 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001049 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1050 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001051 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001052 klass.Get());
1053 // Force initialization to turn the class erroneous.
1054 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1055 klass,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001056 /* can_init_fields= */ true,
1057 /* can_init_parents= */ true);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001058 EXPECT_FALSE(initialized);
1059 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1060 soa.Self()->ClearException();
1061 // Check that the LookupResolvedType() can still find the resolved type.
1062 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001063 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001064 klass.Get());
1065 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001066 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001067 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1068 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001069 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001070 klass.Get());
1071}
1072
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001073TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001075 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1076 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001077}
1078
Ian Rogersa15e67d2012-02-28 13:51:55 -08001079// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001080// start of the object
1081TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001083 ObjPtr<mirror::Class> array_class =
1084 class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
1085 ObjPtr<mirror::ObjectArray<mirror::String>> array =
Brian Carlstromea46f952013-07-30 01:26:50 -07001086 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001087 uintptr_t data_offset =
1088 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1089 0));
1090 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001091 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1092 } else {
1093 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1094 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001095}
1096
1097TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001099 StackHandleScope<5> hs(soa.Self());
1100 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001101 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001102 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1103 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1104
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001105 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001106 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001107 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1108 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1109
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001110 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001111 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001112 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1113 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1114
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001115 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001116 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001117 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1118 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1119
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001120 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001121 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001122 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1123 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1124
1125 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001126}
1127
Elliott Hughes33203b52011-09-20 19:42:01 -07001128TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1129 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1130 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001132 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001133 ObjPtr<mirror::Class> c;
Ian Rogers98379392014-02-24 16:53:16 -08001134 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001135 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001136 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001137 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001138 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001139 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001140 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001141 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001142 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001143 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001144 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001145 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001146 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001147 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001148 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001149 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001150}
1151
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001152TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001154 StackHandleScope<3> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001155 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001156 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001157 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001158 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001159 Handle<mirror::Class> MyClass_1 = hs.NewHandle(
1160 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
1161 ObjPtr<mirror::Class> MyClass_2 =
1162 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001163 EXPECT_TRUE(MyClass_1 != nullptr);
1164 EXPECT_TRUE(MyClass_2 != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001165 EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001166}
1167
Jesse Wilson7833bd22011-08-09 18:31:44 -04001168TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001170 StackHandleScope<2> hs(soa.Self());
1171 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001172 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001173 Handle<mirror::Class> statics(
1174 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001175 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001176
jeffhaoabcfde32011-09-29 15:05:18 -07001177 // Static final primitives that are initialized by a compile-time constant
1178 // expression resolve to a copy of a constant value from the constant pool.
1179 // So <clinit> should be null.
Vladimir Markoba118822017-06-12 15:41:56 +01001180 ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001181 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001182
1183 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001184
Vladimir Marko19a4d372016-12-08 14:41:46 +00001185 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001186 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001187 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1188 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001189
Vladimir Marko19a4d372016-12-08 14:41:46 +00001190 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001191 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001192 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1193 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001194
Vladimir Marko19a4d372016-12-08 14:41:46 +00001195 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001196 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001197 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1198 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001199
Vladimir Marko19a4d372016-12-08 14:41:46 +00001200 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001201 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001202 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1203 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001204
Vladimir Marko19a4d372016-12-08 14:41:46 +00001205 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001206 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001207 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1208 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001209
Vladimir Marko19a4d372016-12-08 14:41:46 +00001210 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001211 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001212 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1213 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001214
Vladimir Marko19a4d372016-12-08 14:41:46 +00001215 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001216 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001217 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001218 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001219
Vladimir Marko19a4d372016-12-08 14:41:46 +00001220 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001221 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001222 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001223 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001224
Vladimir Marko19a4d372016-12-08 14:41:46 +00001225 ArtField* s8 = mirror::Class::FindStaticField(
1226 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001227 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001228 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001229 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001230 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001231
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001232 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1233 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001234 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1235 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1236 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1237 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1238 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1239 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001240 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1241 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001242 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001243}
1244
Brian Carlstrom30b94452011-08-25 21:35:26 -07001245TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001247 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001248 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001249 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001250 Handle<mirror::Class> I(
1251 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1252 Handle<mirror::Class> J(
1253 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1254 Handle<mirror::Class> K(
1255 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1256 Handle<mirror::Class> A(
1257 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1258 Handle<mirror::Class> B(
1259 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1260 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1261 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1262 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1263 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1264 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001265
Vladimir Markoeb37ba52019-02-05 14:10:38 +00001266 const std::string_view void_sig("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001267 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1268 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1269 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001270 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1271 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1272 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001273 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1274 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1275 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001276 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001277 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001278 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001279 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001280 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001281 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001282 ASSERT_TRUE(Kj1 != nullptr);
1283 ASSERT_TRUE(Kj2 != nullptr);
1284 ASSERT_TRUE(Kk != nullptr);
1285 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001286 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001287 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001288 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001289 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001290 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001291 EXPECT_NE(Ii, Ai);
1292 EXPECT_NE(Jj1, Aj1);
1293 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001294 EXPECT_EQ(Kj1, Jj1);
1295 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001296 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1297 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1298 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1299 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1300 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1301 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001302
Vladimir Marko19a4d372016-12-08 14:41:46 +00001303 ArtField* Afoo =
1304 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1305 ArtField* Bfoo =
1306 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1307 ArtField* Jfoo =
1308 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1309 ArtField* Kfoo =
1310 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001311 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001312 EXPECT_EQ(Afoo, Bfoo);
1313 EXPECT_EQ(Afoo, Jfoo);
1314 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001315}
1316
Elliott Hughesf3778f62012-01-26 14:14:35 -08001317TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001318 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001319
jeffhaoabcfde32011-09-29 15:05:18 -07001320 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1321 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001322
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001323 ScopedObjectAccess soa(Thread::Current());
1324 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001325 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001326 StackHandleScope<1> hs(soa.Self());
1327 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001328 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001329 ObjPtr<mirror::Class> klass =
1330 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001331 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001332 ArtMethod* getS0 =
1333 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1334 ASSERT_TRUE(getS0 != nullptr);
1335 ASSERT_TRUE(getS0->IsStatic());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001336 const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001337 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001338 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001339 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1340 clinit,
1341 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001342 /* can_run_clinit= */ true,
1343 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001344 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001345 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001346 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1347 getS0,
1348 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001349 /* can_run_clinit= */ true,
1350 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001351 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001352 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001353}
1354
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001355TEST_F(ClassLinkerTest, ErroneousClass) {
1356 ScopedObjectAccess soa(Thread::Current());
1357 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1358 StackHandleScope<1> hs(soa.Self());
1359 Handle<mirror::ClassLoader> class_loader(
1360 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1361 hs.Self()->AssertNoPendingException();
1362 const char* descriptor = "LErroneous;";
1363 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1364 // Erronenous since we are extending final class.
1365 hs.Self()->AssertPendingException();
1366 EXPECT_TRUE(klass == nullptr);
1367 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1368 EXPECT_FALSE(klass == nullptr);
1369 EXPECT_TRUE(klass->IsErroneous());
1370 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1371}
1372
Elliott Hughes20cde902011-10-04 17:37:27 -07001373TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001375 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001376
1377 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001378 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001379 EXPECT_FALSE(c->IsFinalizable());
1380
1381 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001382 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001383 EXPECT_FALSE(c->IsFinalizable());
1384
1385 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001386 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001387 EXPECT_FALSE(c->IsFinalizable());
1388
1389 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001390 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001391 EXPECT_TRUE(c->IsFinalizable());
1392
1393 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001394 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001395 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001396 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001397 EXPECT_TRUE(c->IsFinalizable());
1398
1399 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1400 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001401 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001402 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001403 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001404 EXPECT_TRUE(c->IsFinalizable());
1405}
1406
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001407TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001409 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001410 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1411 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001412 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001413 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001414 }
1415}
1416
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001417TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1418 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001419 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001420 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001421
1422 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001423 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001424 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001425
1426 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001427 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001428 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001429
1430 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001431 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001432 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001433
1434 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001435 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001436 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001437}
1438
Mathieu Chartiere401d142015-04-22 13:56:20 -07001439static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001440 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001441 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001442 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001443 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001444 }
1445}
1446
Vladimir Markoe9987b02018-05-22 16:26:43 +01001447static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001448 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001449 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001450 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001451 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001452 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001453 }
1454}
1455
1456TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1457 ScopedObjectAccess soa(Thread::Current());
1458
Vladimir Markoe9987b02018-05-22 16:26:43 +01001459 ObjPtr<mirror::Class> JavaLangObject =
1460 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001461 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001462 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1463 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001464 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001465}
1466
1467TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1468 ScopedObjectAccess soa(Thread::Current());
1469
1470 StackHandleScope<1> hs(soa.Self());
1471
1472 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1473 soa.Self(), "Ljava/lang/SecurityManager;")));
1474 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1475 "core";
1476
Igor Murashkindf707e42016-02-02 16:56:50 -08001477 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001478
Ian Rogers7b078e82014-09-10 14:44:24 -07001479 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001480 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001481}
1482
1483TEST_F(ClassLinkerTest, Preverified_App) {
1484 ScopedObjectAccess soa(Thread::Current());
1485
1486 StackHandleScope<2> hs(soa.Self());
1487 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001488 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001489 Handle<mirror::Class> statics(
1490 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1491
Igor Murashkindf707e42016-02-02 16:56:50 -08001492 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001493
Ian Rogers7b078e82014-09-10 14:44:24 -07001494 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001495 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001496}
1497
Sebastien Hertz6963e442014-11-26 22:11:27 +01001498TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1499 ScopedObjectAccess soa(Thread::Current());
1500
1501 StackHandleScope<3> hs(soa.Self());
1502 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001503 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001504
1505 // java.lang.Object is a bootstrap class.
1506 Handle<mirror::Class> jlo_class(
1507 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001508 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001509 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001510
1511 // Statics is not a bootstrap class.
1512 Handle<mirror::Class> statics(
1513 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001514 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001515 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001516}
1517
Mathieu Chartier76172162016-01-26 14:54:06 -08001518// Regression test for b/26799552.
1519TEST_F(ClassLinkerTest, RegisterDexFileName) {
1520 ScopedObjectAccess soa(Thread::Current());
1521 StackHandleScope<2> hs(soa.Self());
1522 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1523 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1524 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001525 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001526 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001527 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001528 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001529 break;
1530 }
1531 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001532 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001533 }
Mathieu Chartier76172162016-01-26 14:54:06 -08001534 const uint16_t data[] = { 0x20AC, 0x20A1 };
1535 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1536 arraysize(data),
1537 data)));
Mathieu Chartier76172162016-01-26 14:54:06 -08001538 const DexFile* old_dex_file = dex_cache->GetDexFile();
1539
David Sehr0b426772018-07-03 23:03:42 +00001540 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1541 old_dex_file->Size(),
1542 location->ToModifiedUtf8(),
1543 0u,
1544 nullptr,
1545 nullptr));
David Srbecky86d6cd52020-12-02 18:13:10 +00001546 // Make a copy of the dex cache with changed name.
Orion Hodsonb9b7d912021-02-24 09:24:47 +00001547 LinearAlloc* alloc = Runtime::Current()->GetLinearAlloc();
1548 dex_cache.Assign(class_linker->AllocAndInitializeDexCache(Thread::Current(), *dex_file, alloc));
David Srbecky86d6cd52020-12-02 18:13:10 +00001549 DCHECK_EQ(dex_cache->GetLocation()->CompareTo(location.Get()), 0);
Mathieu Chartier76172162016-01-26 14:54:06 -08001550 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001551 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001552 // Check that inserting with a UTF16 name works.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001553 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001554 }
1555}
1556
Narayan Kamath25352fc2016-08-03 12:46:58 +01001557TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1558 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001559 StackHandleScope<7> hs(soa.Self());
1560
1561 Handle<mirror::ClassLoader> class_loader(
1562 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1563 Handle<mirror::Class> method_types(
1564 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1565 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1566
Vladimir Markoba118822017-06-12 15:41:56 +01001567 ArtMethod* method1 = method_types->FindClassMethod(
1568 "method1",
1569 "(Ljava/lang/String;)Ljava/lang/String;",
1570 kRuntimePointerSize);
1571 ASSERT_TRUE(method1 != nullptr);
1572 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001573
1574 const DexFile& dex_file = *(method1->GetDexFile());
1575 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001576 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001577
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001578 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001579
1580 // This is the MethodType corresponding to the prototype of
1581 // String MethodTypes# method1(String).
1582 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001583 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001584 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001585 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001586
1587 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001588 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001589
1590 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001591 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1592 "Ljava/lang/String;",
1593 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001594 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1595 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001596
1597 // Resolve the method type again and assert that we get back the same value.
1598 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001599 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001600 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001601
1602 // Resolve the MethodType associated with a different method signature
1603 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001604 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001605 "method2",
1606 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1607 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001608 ASSERT_TRUE(method2 != nullptr);
1609 ASSERT_FALSE(method2->IsDirect());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001610 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001611 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001612 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001613 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001614}
1615
Calin Juravle7865ac72017-06-28 11:03:12 -07001616// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1617// by creating a chain of class loaders with various dex files.
1618TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1619 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1620 // No need to check again.
1621 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1622 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1623 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1624 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1625}
1626
David Sehrc431b9d2018-03-02 12:01:51 -08001627TEST_F(ClassLinkerTest, PrettyClass) {
1628 ScopedObjectAccess soa(Thread::Current());
1629 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001630 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001631 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001632 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001633 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1634}
1635
1636TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1637 ScopedObjectAccess soa(Thread::Current());
1638 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001639 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001640 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001641 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001642 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1643 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1644}
1645
1646TEST_F(ClassLinkerTest, PrettyField) {
1647 ScopedObjectAccess soa(Thread::Current());
1648 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1649
Vladimir Markoe9987b02018-05-22 16:26:43 +01001650 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1651 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001652
1653 ArtField* f;
1654 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1655 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1656 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1657}
1658
1659TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1660 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001661 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001662 ASSERT_TRUE(c != nullptr);
1663 ArtMethod* m;
1664
1665 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1666 ASSERT_TRUE(m != nullptr);
1667 ASSERT_FALSE(m->IsDirect());
1668 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1669 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1670
1671 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1672 ASSERT_TRUE(m != nullptr);
1673 ASSERT_FALSE(m->IsDirect());
1674 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1675 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1676
1677 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1678 ASSERT_TRUE(m != nullptr);
1679 ASSERT_TRUE(m->IsStatic());
1680 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1681 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1682}
1683
Calin Juravlecdd49122017-07-05 20:09:53 -07001684class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1685 protected:
1686 // Verifies that the class identified by the given descriptor is loaded with
1687 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1688 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1689 void VerifyClassResolution(const std::string& descriptor,
1690 jobject class_loader_to_search_obj,
1691 jobject expected_class_loader_obj,
1692 bool should_find = true) {
1693 Thread* self = Thread::Current();
1694 ScopedObjectAccess soa(self);
1695 StackHandleScope<3> hs(self);
1696 Handle<mirror::ClassLoader> class_loader_to_search(
1697 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1698
1699 Handle<mirror::Class> klass = hs.NewHandle(
1700 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1701
1702 if (!should_find) {
1703 if (self->IsExceptionPending()) {
1704 self->ClearException();
1705 }
1706 ASSERT_TRUE(klass == nullptr);
1707 } else if (expected_class_loader_obj == nullptr) {
1708 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1709 } else {
1710 ASSERT_TRUE(klass != nullptr) << descriptor;
1711 Handle<mirror::ClassLoader> expected_class_loader(
1712 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001713 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001714 }
1715 }
1716};
1717
1718TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1719 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1720 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1721 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001722 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001723}
1724
1725TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1726 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1727 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1728 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001729 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001730}
1731
1732TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1733 // The chain is
1734 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1735 // ^
1736 // |
1737 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1738 // ^
1739 // |
1740 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1741 // ^
1742 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001743 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001744
1745 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1746 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1747 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1748 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1749
1750 // Verify exclusive classes (present in only one class loader).
1751 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1752 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1753 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1754 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1755
1756 // Verify classes that are defined in multiple classloader.
1757
1758 // Classes defined in B should be found in B even if they are defined in A or C because
1759 // B is a DelegateLastClassLoader.
1760 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1761 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1762 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1763
1764 // Classes defined in D should be found in D even if they are defined in parent class loaders
1765 // as well because D is a DelegateLastClassLoader.
1766 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1767 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1768 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1769
1770
1771 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1772 // in the top parent.
1773 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1774
Ian Pedowitz2d536432020-07-22 14:33:00 -07001775 // Check that we don't find an undefined class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001776 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001777}
1778
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001779} // namespace art