blob: a9343e9aec51c940cd43d207148cd31599cff957 [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 Markob4eb1b12018-05-24 11:09:38 +010029#include "class_root.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);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 ASSERT_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 Marko2c64a832018-01-04 11:31:56 +000095 EXPECT_EQ(ClassStatus::kInitialized, 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);
127 ASSERT_EQ(JavaLangObject->GetClass(),
128 JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100129 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000130 std::string temp;
131 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
132 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
133 EXPECT_FALSE(JavaLangObject->HasSuperClass());
134 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Marko2c64a832018-01-04 11:31:56 +0000135 EXPECT_EQ(ClassStatus::kInitialized, 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);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700200 ASSERT_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 Marko2c64a832018-01-04 11:31:56 +0000211 EXPECT_EQ(ClassStatus::kInitialized, 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);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700277 EXPECT_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;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700482
483 bool error = false;
484
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 // Classes have a different size due to padding field. Strings are variable length.
486 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700487 // Currently only required for AccessibleObject since of the padding fields. The class linker
488 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
489 // The RoundUp is to get around this case.
490 static constexpr size_t kPackAlignment = 4;
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100491 size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700492 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700493 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800494 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700495 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700496 << " Java=" << expected_size
497 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700498 error = true;
499 }
500 }
501
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700503 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800504 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700505 << " class=" << class_descriptor
506 << " Java=" << num_fields
507 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700508 error = true;
509 }
510
Brian Carlstromdbc05252011-09-09 01:59:59 -0700511 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700512 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000513 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700515 error = true;
516 }
517 }
518 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700519 for (size_t i = 0; i < offsets.size(); i++) {
520 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700521 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Vladimir Marko72101082019-02-05 16:16:30 +0000522 std::string_view field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800524 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700525 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800526 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700527 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700529 << " CheckOffsets=" << offset.java_name;
530 }
531 }
532
Brian Carlstromdbc05252011-09-09 01:59:59 -0700533 for (size_t i = 0; i < offsets.size(); i++) {
534 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700535 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700536 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
537 error = true;
538 }
539 }
540 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700541 for (size_t i = 0; i < offsets.size(); i++) {
542 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700543 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700544 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800545 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700546 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800547 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
549 }
550 }
551
552 return !error;
553 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700554
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700555 void addOffset(size_t offset, const char* name) {
556 offsets.push_back(CheckOffset(offset, name));
557 }
558
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700559 private:
560 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700561};
562
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700563// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
564// is first since if it is failing, others are unlikely to succeed.
565
Brian Carlstromea46f952013-07-30 01:26:50 -0700566struct ObjectOffsets : public CheckOffsets<mirror::Object> {
567 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700568 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
569 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700570#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700571 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
572 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800573#endif
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800574 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700575};
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577struct ClassOffsets : public CheckOffsets<mirror::Class> {
578 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700579 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700581 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
582 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
584 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800585 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700586 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700587 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700589 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700590 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
591 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800592 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700593 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700594 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
595 "numReferenceInstanceFields");
596 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
597 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700599 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
600 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700601 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
602 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
603 "referenceInstanceOffsets");
604 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
605 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
606 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800607 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700608 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800609 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700610};
611
Alex Lightd6251582016-10-31 11:12:30 -0700612struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
613 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Lighta01de592016-11-15 10:43:06 -0800614 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
615 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000616 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
David Brazdil1a658632018-12-01 17:54:26 +0000617 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_class_def_index_),
618 "preRedefineClassDefIndex");
619 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, pre_redefine_dex_file_ptr_),
620 "preRedefineDexFilePtr");
Alex Lightd6251582016-10-31 11:12:30 -0700621 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
622 }
623};
624
Brian Carlstromea46f952013-07-30 01:26:50 -0700625struct StringOffsets : public CheckOffsets<mirror::String> {
626 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700627 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000628 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800629 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700630};
631
Brian Carlstromea46f952013-07-30 01:26:50 -0700632struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
633 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000634 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700635 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
636 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700637 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
638 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800639 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700640};
641
Brian Carlstromea46f952013-07-30 01:26:50 -0700642struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700643 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
644 false, "Ljava/lang/StackTraceElement;") {
645 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
646 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
647 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
648 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800649 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700650};
651
Brian Carlstromea46f952013-07-30 01:26:50 -0700652struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
653 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700654 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700655 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700656 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
657 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
658 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800659 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700660};
661
Brian Carlstromea46f952013-07-30 01:26:50 -0700662struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
663 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700664 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800665 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400666};
667
Brian Carlstromea46f952013-07-30 01:26:50 -0700668struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
669 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700670 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_preresolved_strings_), "numPreResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
678 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700679 addOffset(OFFSETOF_MEMBER(mirror::DexCache, preresolved_strings_), "preResolvedStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000680 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700681 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100682 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700683 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
684 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
685 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800686 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700687};
688
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700689struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
690 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700691 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
692 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
693 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
694 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800695 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700696};
697
698struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700699 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
700 false, "Ljava/lang/ref/FinalizerReference;") {
701 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
702 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
703 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800704 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700705};
706
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700707struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700708 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
709 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000710 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800711 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700712};
713
714struct FieldOffsets : public CheckOffsets<mirror::Field> {
715 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700716 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
717 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
718 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
719 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
720 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800721 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700722};
723
Neil Fuller16b21cd2016-08-12 09:37:02 +0100724struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
725 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
726 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100727 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
728 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
729 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
730 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
731 "declaringClassOfOverriddenMethod");
732 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100733 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
734 "hasRealParameterData");
735 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800736 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100737};
738
Narayan Kamathafa48272016-08-03 12:46:58 +0100739struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
740 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
741 false, "Ljava/lang/invoke/MethodType;") {
742 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
743 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
744 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
745 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
746 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
747 }
748};
749
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000750struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
751 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
752 false, "Ljava/lang/invoke/MethodHandle;") {
753 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
754 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
755 "cachedSpreadInvoker");
756 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
757 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
758 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
759 }
760};
761
Narayan Kamathafa48272016-08-03 12:46:58 +0100762struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
763 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000764 false, "Ljava/lang/invoke/MethodHandleImpl;") {
765 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100766 }
767};
768
Orion Hodsonc069a302017-01-18 09:23:12 +0000769struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
770 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
771 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
772 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
773 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
774 }
775};
776
Narayan Kamath000e1882016-10-24 17:14:25 +0100777struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
778 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
779 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000780 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100781 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
782 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
783 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
784 }
785};
786
Orion Hodsonc069a302017-01-18 09:23:12 +0000787struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
788 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
789 false, "Ljava/lang/invoke/CallSite;") {
790 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
791 }
792};
793
Orion Hodson005ac512017-10-24 15:43:43 +0100794struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
795 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
796 false, "Ljava/lang/invoke/VarHandle;") {
797 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
798 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
799 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
800 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
801 }
802};
803
804struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
805 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
806 false, "Ljava/lang/invoke/FieldVarHandle;") {
807 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
808 }
809};
810
Orion Hodsonffad6682018-01-02 10:18:58 +0000811struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
812 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
813 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
814 }
815};
816
Orion Hodson005ac512017-10-24 15:43:43 +0100817struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
818 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
819 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
820 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
821 "nativeByteOrder");
822 }
823};
824
825struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
826 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
827 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
828 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
829 "nativeByteOrder");
830 }
831};
832
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400833// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400834// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700835// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400836TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700838 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700839 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700840 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700841 EXPECT_TRUE(StringOffsets().Check());
842 EXPECT_TRUE(ThrowableOffsets().Check());
843 EXPECT_TRUE(StackTraceElementOffsets().Check());
844 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400845 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700846 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700847 EXPECT_TRUE(ReferenceOffsets().Check());
848 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700849 EXPECT_TRUE(AccessibleObjectOffsets().Check());
850 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100851 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100852 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000853 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100854 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000855 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100856 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000857 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100858 EXPECT_TRUE(VarHandleOffsets().Check());
859 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000860 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100861 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
862 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700863}
864
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700865TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700866 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700867 AssertNonExistentClass("NoSuchClass;");
868 AssertNonExistentClass("LNoSuchClass;");
869}
870
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700871TEST_F(ClassLinkerTest, GetDexFiles) {
872 ScopedObjectAccess soa(Thread::Current());
873
874 jobject jclass_loader = LoadDex("Nested");
875 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
876 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800877 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700878
879 jobject jclass_loader2 = LoadDex("MultiDex");
880 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
881 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800882 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700883}
884
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700885TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700887 StackHandleScope<1> hs(soa.Self());
888 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700889 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700890
Vladimir Markoe9987b02018-05-22 16:26:43 +0100891 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700892 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700893 EXPECT_EQ(0U, outer->NumVirtualMethods());
894 EXPECT_EQ(1U, outer->NumDirectMethods());
895
Vladimir Markoe9987b02018-05-22 16:26:43 +0100896 ObjPtr<mirror::Class> inner =
897 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700898 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899 EXPECT_EQ(0U, inner->NumVirtualMethods());
900 EXPECT_EQ(1U, inner->NumDirectMethods());
901}
902
903TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700905 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800906 for (int ch = 1; ch < 256; ++ch) {
907 std::string descriptor;
908 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700909 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700910 AssertNonExistentClass(descriptor);
911 } else {
912 AssertPrimitiveClass(descriptor);
913 }
914 }
915}
916
917TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100919 StackHandleScope<2> hs(soa.Self());
920 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
921 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
922 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700923
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700924 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700925 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700926 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100927 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700928 ASSERT_TRUE(MyClass != nullptr);
929 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700930 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100931 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000932 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700933 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100934 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700935 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100936 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000937 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700938 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700939 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700940 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000941 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700942 EXPECT_FALSE(MyClass->IsVerified());
943 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700944 EXPECT_FALSE(MyClass->IsArrayInstance());
945 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700946 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700947 EXPECT_FALSE(MyClass->IsInterface());
948 EXPECT_FALSE(MyClass->IsPublic());
949 EXPECT_FALSE(MyClass->IsFinal());
950 EXPECT_FALSE(MyClass->IsPrimitive());
951 EXPECT_FALSE(MyClass->IsSynthetic());
952 EXPECT_EQ(1U, MyClass->NumDirectMethods());
953 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
954 EXPECT_EQ(0U, MyClass->NumInstanceFields());
955 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700956 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700957
958 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
959
960 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700961 AssertArrayClass("[C", "C", nullptr);
962 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700963 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700964 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700965 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700966 // or not available at all
967 AssertNonExistentClass("[[[[LNonExistentClass;");
968}
969
Mathieu Chartierb8901302016-09-30 10:27:43 -0700970TEST_F(ClassLinkerTest, LookupResolvedType) {
971 ScopedObjectAccess soa(Thread::Current());
972 StackHandleScope<1> hs(soa.Self());
973 Handle<mirror::ClassLoader> class_loader(
974 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
975 AssertNonExistentClass("LMyClass;");
976 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800977 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700978 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700979 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000980 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700981 klass);
982 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000983 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700984 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
985 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000986 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700987 klass);
988}
989
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000990TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
991 ScopedObjectAccess soa(Thread::Current());
992 StackHandleScope<2> hs(soa.Self());
993 Handle<mirror::ClassLoader> class_loader(
994 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
995 // Get the AllFields class for the dex cache and dex file.
996 ObjPtr<mirror::Class> all_fields_klass
997 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000998 ASSERT_TRUE(all_fields_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000999 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
1000 const DexFile& dex_file = *dex_cache->GetDexFile();
1001 // Get the index of the array class we want to test.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001002 const dex::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001003 ASSERT_TRUE(array_id != nullptr);
1004 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
1005 // Check that the array class wasn't resolved yet.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001006 EXPECT_TRUE(
1007 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()) == nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001008 // Resolve the array class we want to test.
1009 ObjPtr<mirror::Class> array_klass
1010 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001011 ASSERT_TRUE(array_klass != nullptr);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001012 // Test that LookupResolvedType() finds the array class.
1013 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001014 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001015 array_klass);
1016 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001017 dex_cache->ClearResolvedType(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001018 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
1019 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001020 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001021 array_klass);
1022}
1023
Vladimir Marko72ab6842017-01-20 19:32:50 +00001024TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
1025 ScopedObjectAccess soa(Thread::Current());
1026 StackHandleScope<3> hs(soa.Self());
1027 Handle<mirror::ClassLoader> class_loader(
1028 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
1029 AssertNonExistentClass("LErroneousInit;");
1030 Handle<mirror::Class> klass =
1031 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001032 ASSERT_TRUE(klass != nullptr);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001033 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
1034 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Vladimir Marko72ab6842017-01-20 19:32:50 +00001035 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001036 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001037 klass.Get());
1038 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001039 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001040 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1041 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001042 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001043 klass.Get());
1044 // Force initialization to turn the class erroneous.
1045 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1046 klass,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001047 /* can_init_fields= */ true,
1048 /* can_init_parents= */ true);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001049 EXPECT_FALSE(initialized);
1050 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1051 soa.Self()->ClearException();
1052 // Check that the LookupResolvedType() can still find the resolved type.
1053 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001054 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001055 klass.Get());
1056 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001057 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001058 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1059 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001060 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001061 klass.Get());
1062}
1063
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001064TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001066 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1067 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001068}
1069
Ian Rogersa15e67d2012-02-28 13:51:55 -08001070// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001071// start of the object
1072TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001074 ObjPtr<mirror::Class> array_class =
1075 class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
1076 ObjPtr<mirror::ObjectArray<mirror::String>> array =
Brian Carlstromea46f952013-07-30 01:26:50 -07001077 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001078 uintptr_t data_offset =
1079 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1080 0));
1081 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001082 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1083 } else {
1084 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1085 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001086}
1087
1088TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001090 StackHandleScope<5> hs(soa.Self());
1091 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001092 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001093 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1094 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1095
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001096 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001097 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001098 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1099 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1100
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001101 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001102 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001103 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1104 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1105
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001106 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001107 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001108 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1109 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1110
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001111 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001112 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001113 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1114 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1115
1116 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001117}
1118
Elliott Hughes33203b52011-09-20 19:42:01 -07001119TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1120 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1121 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001123 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001124 ObjPtr<mirror::Class> c;
Ian Rogers98379392014-02-24 16:53:16 -08001125 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001126 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001127 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001128 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001129 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001130 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001131 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001132 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001133 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001134 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001135 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001136 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001137 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001138 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001139 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001140 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001141}
1142
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001143TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001145 StackHandleScope<3> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001146 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001147 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001148 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001149 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001150 Handle<mirror::Class> MyClass_1 = hs.NewHandle(
1151 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
1152 ObjPtr<mirror::Class> MyClass_2 =
1153 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001154 EXPECT_TRUE(MyClass_1 != nullptr);
1155 EXPECT_TRUE(MyClass_2 != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001156 EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001157}
1158
Jesse Wilson7833bd22011-08-09 18:31:44 -04001159TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001161 StackHandleScope<2> hs(soa.Self());
1162 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001163 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001164 Handle<mirror::Class> statics(
1165 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001166 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001167
jeffhaoabcfde32011-09-29 15:05:18 -07001168 // Static final primitives that are initialized by a compile-time constant
1169 // expression resolve to a copy of a constant value from the constant pool.
1170 // So <clinit> should be null.
Vladimir Markoba118822017-06-12 15:41:56 +01001171 ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001172 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001173
1174 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001175
Vladimir Marko19a4d372016-12-08 14:41:46 +00001176 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001177 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001178 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1179 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001180
Vladimir Marko19a4d372016-12-08 14:41:46 +00001181 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001182 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001183 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1184 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001185
Vladimir Marko19a4d372016-12-08 14:41:46 +00001186 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001187 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001188 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1189 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001190
Vladimir Marko19a4d372016-12-08 14:41:46 +00001191 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001192 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001193 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1194 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001195
Vladimir Marko19a4d372016-12-08 14:41:46 +00001196 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001197 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001198 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1199 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001200
Vladimir Marko19a4d372016-12-08 14:41:46 +00001201 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001202 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001203 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1204 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001205
Vladimir Marko19a4d372016-12-08 14:41:46 +00001206 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001207 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001208 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001209 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001210
Vladimir Marko19a4d372016-12-08 14:41:46 +00001211 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001212 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001213 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001214 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001215
Vladimir Marko19a4d372016-12-08 14:41:46 +00001216 ArtField* s8 = mirror::Class::FindStaticField(
1217 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001218 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001219 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001220 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001221 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001222
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001223 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1224 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001225 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1226 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1227 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1228 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1229 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1230 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001231 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1232 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001233 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001234}
1235
Brian Carlstrom30b94452011-08-25 21:35:26 -07001236TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001238 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001239 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001240 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001241 Handle<mirror::Class> I(
1242 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1243 Handle<mirror::Class> J(
1244 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1245 Handle<mirror::Class> K(
1246 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1247 Handle<mirror::Class> A(
1248 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1249 Handle<mirror::Class> B(
1250 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1251 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1252 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1253 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1254 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1255 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001256
Vladimir Markoeb37ba52019-02-05 14:10:38 +00001257 const std::string_view void_sig("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001258 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1259 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1260 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001261 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1262 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1263 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001264 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1265 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1266 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001267 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001268 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001269 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001270 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001271 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001272 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001273 ASSERT_TRUE(Kj1 != nullptr);
1274 ASSERT_TRUE(Kj2 != nullptr);
1275 ASSERT_TRUE(Kk != nullptr);
1276 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001277 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001278 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001279 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001280 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001281 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001282 EXPECT_NE(Ii, Ai);
1283 EXPECT_NE(Jj1, Aj1);
1284 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001285 EXPECT_EQ(Kj1, Jj1);
1286 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001287 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1288 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1289 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1290 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1291 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1292 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001293
Vladimir Marko19a4d372016-12-08 14:41:46 +00001294 ArtField* Afoo =
1295 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1296 ArtField* Bfoo =
1297 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1298 ArtField* Jfoo =
1299 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1300 ArtField* Kfoo =
1301 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001302 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001303 EXPECT_EQ(Afoo, Bfoo);
1304 EXPECT_EQ(Afoo, Jfoo);
1305 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001306}
1307
Elliott Hughesf3778f62012-01-26 14:14:35 -08001308TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001309 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001310
jeffhaoabcfde32011-09-29 15:05:18 -07001311 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1312 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001313
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(Thread::Current());
1315 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001316 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001317 StackHandleScope<1> hs(soa.Self());
1318 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001319 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001320 ObjPtr<mirror::Class> klass =
1321 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001322 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001323 ArtMethod* getS0 =
1324 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1325 ASSERT_TRUE(getS0 != nullptr);
1326 ASSERT_TRUE(getS0->IsStatic());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001327 const dex::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001328 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001329 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001330 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1331 clinit,
1332 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001333 /* can_run_clinit= */ true,
1334 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001335 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001336 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001337 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1338 getS0,
1339 soa.Self(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001340 /* can_run_clinit= */ true,
1341 /* verify_access= */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001342 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001343 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001344}
1345
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001346TEST_F(ClassLinkerTest, ErroneousClass) {
1347 ScopedObjectAccess soa(Thread::Current());
1348 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1349 StackHandleScope<1> hs(soa.Self());
1350 Handle<mirror::ClassLoader> class_loader(
1351 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1352 hs.Self()->AssertNoPendingException();
1353 const char* descriptor = "LErroneous;";
1354 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1355 // Erronenous since we are extending final class.
1356 hs.Self()->AssertPendingException();
1357 EXPECT_TRUE(klass == nullptr);
1358 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1359 EXPECT_FALSE(klass == nullptr);
1360 EXPECT_TRUE(klass->IsErroneous());
1361 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1362}
1363
Elliott Hughes20cde902011-10-04 17:37:27 -07001364TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001365 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001366 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001367
1368 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001369 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001370 EXPECT_FALSE(c->IsFinalizable());
1371
1372 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001373 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001374 EXPECT_FALSE(c->IsFinalizable());
1375
1376 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001377 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001378 EXPECT_FALSE(c->IsFinalizable());
1379
1380 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001381 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001382 EXPECT_TRUE(c->IsFinalizable());
1383
1384 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001385 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001386 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001387 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001388 EXPECT_TRUE(c->IsFinalizable());
1389
1390 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1391 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001392 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001393 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001394 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001395 EXPECT_TRUE(c->IsFinalizable());
1396}
1397
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001398TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001400 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001401 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1402 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001403 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001404 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001405 }
1406}
1407
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001408TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1409 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001410 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001411 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001412
1413 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001414 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001415 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001416
1417 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001418 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001419 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001420
1421 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001422 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001423 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001424
1425 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001426 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001427 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001428}
1429
Mathieu Chartiere401d142015-04-22 13:56:20 -07001430static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001431 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001432 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001433 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001434 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001435 }
1436}
1437
Vladimir Markoe9987b02018-05-22 16:26:43 +01001438static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001439 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001440 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001441 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001442 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001443 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001444 }
1445}
1446
1447TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1448 ScopedObjectAccess soa(Thread::Current());
1449
Vladimir Markoe9987b02018-05-22 16:26:43 +01001450 ObjPtr<mirror::Class> JavaLangObject =
1451 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001452 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001453 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1454 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001455 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001456}
1457
1458TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1459 ScopedObjectAccess soa(Thread::Current());
1460
1461 StackHandleScope<1> hs(soa.Self());
1462
1463 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1464 soa.Self(), "Ljava/lang/SecurityManager;")));
1465 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1466 "core";
1467
Igor Murashkindf707e42016-02-02 16:56:50 -08001468 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001469
Ian Rogers7b078e82014-09-10 14:44:24 -07001470 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001471 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001472}
1473
1474TEST_F(ClassLinkerTest, Preverified_App) {
1475 ScopedObjectAccess soa(Thread::Current());
1476
1477 StackHandleScope<2> hs(soa.Self());
1478 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001479 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001480 Handle<mirror::Class> statics(
1481 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1482
Igor Murashkindf707e42016-02-02 16:56:50 -08001483 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001484
Ian Rogers7b078e82014-09-10 14:44:24 -07001485 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001486 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001487}
1488
Sebastien Hertz6963e442014-11-26 22:11:27 +01001489TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1490 ScopedObjectAccess soa(Thread::Current());
1491
1492 StackHandleScope<3> hs(soa.Self());
1493 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001494 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001495
1496 // java.lang.Object is a bootstrap class.
1497 Handle<mirror::Class> jlo_class(
1498 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001499 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001500 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001501
1502 // Statics is not a bootstrap class.
1503 Handle<mirror::Class> statics(
1504 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001505 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001506 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001507}
1508
Mathieu Chartier76172162016-01-26 14:54:06 -08001509// Regression test for b/26799552.
1510TEST_F(ClassLinkerTest, RegisterDexFileName) {
1511 ScopedObjectAccess soa(Thread::Current());
1512 StackHandleScope<2> hs(soa.Self());
1513 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1514 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1515 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001516 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001517 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001518 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001519 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001520 break;
1521 }
1522 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001523 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001524 }
1525 // Make a copy of the dex cache and change the name.
1526 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1527 const uint16_t data[] = { 0x20AC, 0x20A1 };
1528 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1529 arraysize(data),
1530 data)));
1531 dex_cache->SetLocation(location.Get());
1532 const DexFile* old_dex_file = dex_cache->GetDexFile();
1533
David Sehr0b426772018-07-03 23:03:42 +00001534 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1535 old_dex_file->Size(),
1536 location->ToModifiedUtf8(),
1537 0u,
1538 nullptr,
1539 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001540 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001541 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001542 // Check that inserting with a UTF16 name works.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001543 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader= */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001544 }
1545}
1546
Narayan Kamath25352fc2016-08-03 12:46:58 +01001547TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1548 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001549 StackHandleScope<7> hs(soa.Self());
1550
1551 Handle<mirror::ClassLoader> class_loader(
1552 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1553 Handle<mirror::Class> method_types(
1554 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1555 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1556
Vladimir Markoba118822017-06-12 15:41:56 +01001557 ArtMethod* method1 = method_types->FindClassMethod(
1558 "method1",
1559 "(Ljava/lang/String;)Ljava/lang/String;",
1560 kRuntimePointerSize);
1561 ASSERT_TRUE(method1 != nullptr);
1562 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001563
1564 const DexFile& dex_file = *(method1->GetDexFile());
1565 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001566 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001567
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001568 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001569
1570 // This is the MethodType corresponding to the prototype of
1571 // String MethodTypes# method1(String).
1572 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001573 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001574 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001575 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001576
1577 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001578 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001579
1580 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001581 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1582 "Ljava/lang/String;",
1583 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001584 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1585 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001586
1587 // Resolve the method type again and assert that we get back the same value.
1588 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001589 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001590 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001591
1592 // Resolve the MethodType associated with a different method signature
1593 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001594 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001595 "method2",
1596 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1597 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001598 ASSERT_TRUE(method2 != nullptr);
1599 ASSERT_FALSE(method2->IsDirect());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001600 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001601 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001602 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001603 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001604}
1605
Calin Juravle7865ac72017-06-28 11:03:12 -07001606// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1607// by creating a chain of class loaders with various dex files.
1608TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1609 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1610 // No need to check again.
1611 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1612 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1613 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1614 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1615}
1616
David Sehrc431b9d2018-03-02 12:01:51 -08001617TEST_F(ClassLinkerTest, PrettyClass) {
1618 ScopedObjectAccess soa(Thread::Current());
1619 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001620 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001621 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001622 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001623 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1624}
1625
1626TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1627 ScopedObjectAccess soa(Thread::Current());
1628 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001629 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001630 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001631 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001632 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1633 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1634}
1635
1636TEST_F(ClassLinkerTest, PrettyField) {
1637 ScopedObjectAccess soa(Thread::Current());
1638 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1639
Vladimir Markoe9987b02018-05-22 16:26:43 +01001640 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1641 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001642
1643 ArtField* f;
1644 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1645 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1646 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1647}
1648
1649TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1650 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001651 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001652 ASSERT_TRUE(c != nullptr);
1653 ArtMethod* m;
1654
1655 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1656 ASSERT_TRUE(m != nullptr);
1657 ASSERT_FALSE(m->IsDirect());
1658 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1659 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1660
1661 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1662 ASSERT_TRUE(m != nullptr);
1663 ASSERT_FALSE(m->IsDirect());
1664 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1665 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1666
1667 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1668 ASSERT_TRUE(m != nullptr);
1669 ASSERT_TRUE(m->IsStatic());
1670 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1671 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1672}
1673
Calin Juravlecdd49122017-07-05 20:09:53 -07001674class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1675 protected:
1676 // Verifies that the class identified by the given descriptor is loaded with
1677 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1678 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1679 void VerifyClassResolution(const std::string& descriptor,
1680 jobject class_loader_to_search_obj,
1681 jobject expected_class_loader_obj,
1682 bool should_find = true) {
1683 Thread* self = Thread::Current();
1684 ScopedObjectAccess soa(self);
1685 StackHandleScope<3> hs(self);
1686 Handle<mirror::ClassLoader> class_loader_to_search(
1687 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1688
1689 Handle<mirror::Class> klass = hs.NewHandle(
1690 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1691
1692 if (!should_find) {
1693 if (self->IsExceptionPending()) {
1694 self->ClearException();
1695 }
1696 ASSERT_TRUE(klass == nullptr);
1697 } else if (expected_class_loader_obj == nullptr) {
1698 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1699 } else {
1700 ASSERT_TRUE(klass != nullptr) << descriptor;
1701 Handle<mirror::ClassLoader> expected_class_loader(
1702 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001703 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001704 }
1705 }
1706};
1707
1708TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1709 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1710 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1711 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001712 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001713}
1714
1715TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1716 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1717 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1718 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001719 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001720}
1721
1722TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1723 // The chain is
1724 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1725 // ^
1726 // |
1727 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1728 // ^
1729 // |
1730 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1731 // ^
1732 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001733 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001734
1735 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1736 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1737 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1738 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1739
1740 // Verify exclusive classes (present in only one class loader).
1741 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1742 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1743 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1744 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1745
1746 // Verify classes that are defined in multiple classloader.
1747
1748 // Classes defined in B should be found in B even if they are defined in A or C because
1749 // B is a DelegateLastClassLoader.
1750 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1751 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1752 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1753
1754 // Classes defined in D should be found in D even if they are defined in parent class loaders
1755 // as well because D is a DelegateLastClassLoader.
1756 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1757 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1758 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1759
1760
1761 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1762 // in the top parent.
1763 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1764
Calin Juravlecdd49122017-07-05 20:09:53 -07001765 // Sanity check that we don't find an undefined class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001766 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find=*/ false);
Calin Juravlecdd49122017-07-05 20:09:53 -07001767}
1768
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001769} // namespace art