blob: 52ddd13ab78750fb67bccb7ca2c56b8ecca18362 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "class_linker.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070018
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <string>
21
Andreas Gampe9186ced2016-12-12 14:28:21 -080022#include "android-base/strings.h"
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010028#include "class_root.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file_types.h"
31#include "dex/standard_dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "handle_scope-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070036#include "mirror/accessible_object.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000037#include "mirror/call_site.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070039#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010041#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010042#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070043#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010044#include "mirror/method_handle_impl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000045#include "mirror/method_handles_lookup.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070046#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
49#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070050#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070052#include "mirror/string-inl.h"
Orion Hodson005ac512017-10-24 15:43:43 +010053#include "mirror/var_handle.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070054#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070055#include "thread-current-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056
57namespace art {
58
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080059class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070060 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070063 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070064 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070065 EXPECT_TRUE(self->IsExceptionPending());
Vladimir Markoe9987b02018-05-22 16:26:43 +010066 StackHandleScope<1> hs(self);
67 Handle<mirror::Object> exception = hs.NewHandle<mirror::Object>(self->GetException());
Elliott Hughes885c3bd2011-08-22 16:59:20 -070068 self->ClearException();
Vladimir Markoe9987b02018-05-22 16:26:43 +010069 ObjPtr<mirror::Class> exception_class =
Ian Rogers98379392014-02-24 16:53:16 -080070 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070071 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070072 }
73
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080076 Thread* self = Thread::Current();
77 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070078 }
79
Vladimir Markoe9987b02018-05-22 16:26:43 +010080 void AssertPrimitiveClass(const std::string& descriptor, ObjPtr<mirror::Class> primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070082 ASSERT_TRUE(primitive != nullptr);
83 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070085 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070086 std::string temp;
87 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070088 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070089 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070090 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Vladimir Marko2c64a832018-01-04 11:31:56 +000091 EXPECT_EQ(ClassStatus::kInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070093 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094 EXPECT_TRUE(primitive->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +000095 EXPECT_FALSE(primitive->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070096 EXPECT_TRUE(primitive->IsVerified());
97 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070098 EXPECT_FALSE(primitive->IsArrayInstance());
99 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700100 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700101 EXPECT_FALSE(primitive->IsInterface());
102 EXPECT_TRUE(primitive->IsPublic());
103 EXPECT_TRUE(primitive->IsFinal());
104 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700105 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700106 EXPECT_EQ(0U, primitive->NumDirectMethods());
107 EXPECT_EQ(0U, primitive->NumVirtualMethods());
108 EXPECT_EQ(0U, primitive->NumInstanceFields());
109 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700110 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700111 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700112 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800113 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
114 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Elliott Hughes00626c22013-06-14 15:04:14 -0700115 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700116 }
117
Vladimir Markoe9987b02018-05-22 16:26:43 +0100118 void AssertObjectClass(ObjPtr<mirror::Class> JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000120 ASSERT_TRUE(JavaLangObject != nullptr);
121 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
122 ASSERT_EQ(JavaLangObject->GetClass(),
123 JavaLangObject->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100124 EXPECT_OBJ_PTR_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000125 std::string temp;
126 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
127 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
128 EXPECT_FALSE(JavaLangObject->HasSuperClass());
129 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Vladimir Marko2c64a832018-01-04 11:31:56 +0000130 EXPECT_EQ(ClassStatus::kInitialized, JavaLangObject->GetStatus());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000131 EXPECT_FALSE(JavaLangObject->IsErroneous());
132 EXPECT_TRUE(JavaLangObject->IsLoaded());
133 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000134 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000135 EXPECT_TRUE(JavaLangObject->IsVerified());
136 EXPECT_TRUE(JavaLangObject->IsInitialized());
137 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
138 EXPECT_FALSE(JavaLangObject->IsArrayClass());
139 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
140 EXPECT_FALSE(JavaLangObject->IsInterface());
141 EXPECT_TRUE(JavaLangObject->IsPublic());
142 EXPECT_FALSE(JavaLangObject->IsFinal());
143 EXPECT_FALSE(JavaLangObject->IsPrimitive());
144 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000145 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000146 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
147 if (!kUseBrooksReadBarrier) {
148 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
149 } else {
150 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
151 }
152 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
153 "shadow$_klass_");
154 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
155 "shadow$_monitor_");
156 if (kUseBrooksReadBarrier) {
157 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
158 "shadow$_x_rb_ptr_");
159 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
160 "shadow$_x_xpadding_");
161 }
162
163 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
164 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
165
Andreas Gampe542451c2016-07-26 09:02:02 -0700166 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000167 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
168 ImTable* imt = JavaLangObject->GetImt(pointer_size);
169 ASSERT_NE(nullptr, imt);
170 for (size_t i = 0; i < ImTable::kSize; ++i) {
171 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
172 }
173 }
174
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 void AssertArrayClass(const std::string& array_descriptor,
176 const std::string& component_type,
Vladimir Markoe9987b02018-05-22 16:26:43 +0100177 ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800179 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 StackHandleScope<2> hs(self);
181 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
182 Handle<mirror::Class> array(
183 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700184 std::string temp;
185 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100186 EXPECT_OBJ_PTR_EQ(loader.Get(), array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700187 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700188 AssertArrayClass(array_descriptor, array);
189 }
190
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700191 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800193 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700194 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700195 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700196 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197 std::string temp;
198 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700199 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800200 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100201 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"),
202 array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700203 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700204 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700205 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Vladimir Marko2c64a832018-01-04 11:31:56 +0000206 EXPECT_EQ(ClassStatus::kInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700207 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700208 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700209 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000210 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700211 EXPECT_TRUE(array->IsVerified());
212 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700213 EXPECT_FALSE(array->IsArrayInstance());
214 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700215 EXPECT_FALSE(array->IsInterface());
216 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
217 EXPECT_TRUE(array->IsFinal());
218 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700219 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700220 EXPECT_EQ(0U, array->NumDirectMethods());
221 EXPECT_EQ(0U, array->NumVirtualMethods());
222 EXPECT_EQ(0U, array->NumInstanceFields());
223 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700224 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000225 EXPECT_TRUE(array->ShouldHaveImt());
226 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700227 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700228 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000229 ObjPtr<mirror::Class> direct_interface0 =
230 mirror::Class::GetDirectInterface(self, array.Get(), 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700231 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700232 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Marko19a4d372016-12-08 14:41:46 +0000233 ObjPtr<mirror::Class> direct_interface1 =
234 mirror::Class::GetDirectInterface(self, array.Get(), 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700235 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Vladimir Markobcf17522018-06-01 13:14:32 +0100236 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, array->GetComponentType()), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000237
Andreas Gampe542451c2016-07-26 09:02:02 -0700238 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100239 ObjPtr<mirror::Class> JavaLangObject =
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000240 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
241 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
242 // IMT of a array class should be shared with the IMT of the java.lag.Object
243 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700244 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700247 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700249 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700250 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700251 }
252
Mathieu Chartier3398c782016-09-30 10:27:43 -0700253 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700255 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700256 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700257 EXPECT_TRUE(field->GetName() != nullptr);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000258 EXPECT_TRUE(field->ResolveType() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700259 }
260
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700261 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700263 std::string temp;
264 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700267 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700269 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700270 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700271 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700272 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700273 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700274 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700275 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000276 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700279 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700280 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700281 std::string temp2;
282 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
283 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700284 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700285 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800286 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700287 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800288 EXPECT_TRUE(m.IsStatic());
289 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700290 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700291 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700292 if (!klass->IsSynthetic()) {
293 EXPECT_NE(0U, klass->NumDirectMethods());
294 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700295 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700296 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100297 ObjPtr<mirror::IfTable> iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700298 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Vladimir Markoe9987b02018-05-22 16:26:43 +0100299 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700300 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700301 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700302 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700303 } else {
Alex Light705ad492015-09-21 11:36:30 -0700304 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700305 }
306 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700307 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700308 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311 }
312 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700313 EXPECT_FALSE(klass->IsAbstract());
314 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700315 }
316 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700317 EXPECT_FALSE(klass->IsFinal());
318 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700319 }
320
321 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700322 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700323
Andreas Gampe542451c2016-07-26 09:02:02 -0700324 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 AssertMethod(&method);
326 EXPECT_TRUE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100327 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700328 }
329
Andreas Gampe542451c2016-07-26 09:02:02 -0700330 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700331 AssertMethod(&method);
332 EXPECT_FALSE(method.IsDirect());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100333 EXPECT_OBJ_PTR_EQ(klass.Get(), method.GetDeclaringClass());
Alex Lighte64300b2015-12-15 15:02:47 -0800334 }
335
Andreas Gampe542451c2016-07-26 09:02:02 -0700336 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800337 AssertMethod(&method);
338 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800339 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800340 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700341 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800342 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700343 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700344 }
345
346 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700347 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700348 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700349 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700350 }
351
352 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700353 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700354 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700355 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700356 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700357
Vladimir Marko76649e82014-11-10 18:32:59 +0000358 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700359 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000360 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
361 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
362 klass->NumReferenceInstanceFields() *
363 sizeof(mirror::HeapReference<mirror::Object>));
364 MemberOffset current_ref_offset = start_ref_offset;
365 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700366 ArtField* field = klass->GetInstanceField(i);
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000367 ObjPtr<mirror::Class> field_type = field->ResolveType();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700368 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000369 if (!field->IsPrimitiveType()) {
370 ASSERT_TRUE(!field_type->IsPrimitive());
371 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
372 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
373 // While Reference.referent is not primitive, the ClassLinker
374 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700375 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000376 } else {
377 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
378 sizeof(mirror::HeapReference<mirror::Object>));
379 }
380 } else {
381 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
382 // Shuffled before references.
383 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
384 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
385 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700386 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000388 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700389
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700390 uint32_t total_num_reference_instance_fields = 0;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100391 ObjPtr<mirror::Class> k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700392 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700393 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
394 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700395 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700396 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
397 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
398 // The reference instance offsets have a bit set for each reference offset.
399 // +1 for Object's class.
400 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
401 total_num_reference_instance_fields);
402 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700403 }
404
Vladimir Markoe9987b02018-05-22 16:26:43 +0100405 void AssertDexFileClass(ObjPtr<mirror::ClassLoader> class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700406 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700407 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800408 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700409 StackHandleScope<1> hs(self);
410 Handle<mirror::Class> klass(
411 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800412 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700413 std::string temp;
Vladimir Markoe9987b02018-05-22 16:26:43 +0100414 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
415 EXPECT_OBJ_PTR_EQ(class_loader, klass->GetClassLoader());
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700416 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700417 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700418 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700419 AssertArrayClass(descriptor, klass);
420 } else {
421 AssertClass(descriptor, klass);
422 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700423 }
424
Vladimir Markoe9987b02018-05-22 16:26:43 +0100425 void AssertDexFile(const DexFile& dex, ObjPtr<mirror::ClassLoader> class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700426 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700427 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800428 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
429 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
430 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700431 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700432 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700433 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800434 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800435 const DexFile::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800436 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700437 AssertDexFileClass(class_loader, descriptor);
438 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700439 TestRootVisitor visitor;
440 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700441 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700442
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700443 class TestRootVisitor : public SingleRootVisitor {
444 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100445 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) override {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700446 EXPECT_TRUE(root != nullptr);
447 }
448 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700449};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700450
Narayan Kamath25352fc2016-08-03 12:46:58 +0100451class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
452 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100453 virtual void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100454 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100455 }
456};
457
Brian Carlstrom693267a2011-09-06 09:25:34 -0700458struct CheckOffset {
459 size_t cpp_offset;
460 const char* java_name;
461 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
462};
463
Elliott Hughes80609252011-09-23 17:24:51 -0700464template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700465struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800466 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
467 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700468 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700469 std::string class_descriptor;
470 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700472 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800473 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100474 ObjPtr<mirror::Class> klass =
Ian Rogers98379392014-02-24 16:53:16 -0800475 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700476 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700477
478 bool error = false;
479
Mathieu Chartiere401d142015-04-22 13:56:20 -0700480 // Classes have a different size due to padding field. Strings are variable length.
481 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700482 // Currently only required for AccessibleObject since of the padding fields. The class linker
483 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
484 // The RoundUp is to get around this case.
485 static constexpr size_t kPackAlignment = 4;
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100486 size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700487 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700488 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800489 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700490 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700491 << " Java=" << expected_size
492 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700493 error = true;
494 }
495 }
496
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700498 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800499 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700500 << " class=" << class_descriptor
501 << " Java=" << num_fields
502 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700503 error = true;
504 }
505
Brian Carlstromdbc05252011-09-09 01:59:59 -0700506 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700507 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700508 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700510 error = true;
511 }
512 }
513 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700514 for (size_t i = 0; i < offsets.size(); i++) {
515 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700516 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700517 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800519 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700520 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800521 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700522 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700524 << " CheckOffsets=" << offset.java_name;
525 }
526 }
527
Brian Carlstromdbc05252011-09-09 01:59:59 -0700528 for (size_t i = 0; i < offsets.size(); i++) {
529 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700530 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700531 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
532 error = true;
533 }
534 }
535 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700536 for (size_t i = 0; i < offsets.size(); i++) {
537 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700538 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700539 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800540 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800542 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700543 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
544 }
545 }
546
547 return !error;
548 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700549
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700550 void addOffset(size_t offset, const char* name) {
551 offsets.push_back(CheckOffset(offset, name));
552 }
553
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700554 private:
555 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700556};
557
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700558// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
559// is first since if it is failing, others are unlikely to succeed.
560
Brian Carlstromea46f952013-07-30 01:26:50 -0700561struct ObjectOffsets : public CheckOffsets<mirror::Object> {
562 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700563 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
564 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700565#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700566 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
567 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800568#endif
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800569 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700570};
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572struct ClassOffsets : public CheckOffsets<mirror::Class> {
573 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700574 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700575 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
577 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
578 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
579 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800580 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700581 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700582 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700584 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
586 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800587 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700588 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700589 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
590 "numReferenceInstanceFields");
591 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
592 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700593 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700594 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
595 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700596 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
597 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
598 "referenceInstanceOffsets");
599 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
600 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
601 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800602 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700603 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800604 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700605};
606
Alex Lightd6251582016-10-31 11:12:30 -0700607struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
608 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Lighta01de592016-11-15 10:43:06 -0800609 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
610 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000611 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
Alex Lightd6251582016-10-31 11:12:30 -0700612 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
613 }
614};
615
Brian Carlstromea46f952013-07-30 01:26:50 -0700616struct StringOffsets : public CheckOffsets<mirror::String> {
617 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700618 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000619 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800620 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700621};
622
Brian Carlstromea46f952013-07-30 01:26:50 -0700623struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
624 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000625 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700626 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
627 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700628 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
629 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800630 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700631};
632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700634 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
635 false, "Ljava/lang/StackTraceElement;") {
636 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
637 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
638 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
639 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800640 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700641};
642
Brian Carlstromea46f952013-07-30 01:26:50 -0700643struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
644 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700645 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700646 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700647 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
648 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
649 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800650 }
Brian Carlstrom693267a2011-09-06 09:25:34 -0700651};
652
Brian Carlstromea46f952013-07-30 01:26:50 -0700653struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
654 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700655 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800656 }
Jesse Wilson95caa792011-10-12 18:14:17 -0400657};
658
Brian Carlstromea46f952013-07-30 01:26:50 -0700659struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
660 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700661 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
662 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Orion Hodsonc069a302017-01-18 09:23:12 +0000663 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100664 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100665 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100666 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
667 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
668 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000669 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700670 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800675 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700676};
677
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700678struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
679 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700680 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
681 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
682 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
683 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800684 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700685};
686
687struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700688 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
689 false, "Ljava/lang/ref/FinalizerReference;") {
690 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
691 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
692 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800693 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700694};
695
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700696struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700697 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
698 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000699 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800700 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700701};
702
703struct FieldOffsets : public CheckOffsets<mirror::Field> {
704 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700705 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
706 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
707 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
708 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
709 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800710 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700711};
712
Neil Fuller16b21cd2016-08-12 09:37:02 +0100713struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
714 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
715 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100716 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
717 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
718 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
719 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
720 "declaringClassOfOverriddenMethod");
721 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100722 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
723 "hasRealParameterData");
724 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800725 }
Neil Fuller16b21cd2016-08-12 09:37:02 +0100726};
727
Narayan Kamathafa48272016-08-03 12:46:58 +0100728struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
729 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
730 false, "Ljava/lang/invoke/MethodType;") {
731 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
732 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
733 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
734 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
735 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
736 }
737};
738
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000739struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
740 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
741 false, "Ljava/lang/invoke/MethodHandle;") {
742 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
743 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
744 "cachedSpreadInvoker");
745 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
746 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
747 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
748 }
749};
750
Narayan Kamathafa48272016-08-03 12:46:58 +0100751struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
752 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000753 false, "Ljava/lang/invoke/MethodHandleImpl;") {
754 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100755 }
756};
757
Orion Hodsonc069a302017-01-18 09:23:12 +0000758struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
759 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
760 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
761 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
762 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
763 }
764};
765
Narayan Kamath000e1882016-10-24 17:14:25 +0100766struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
767 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
768 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000769 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100770 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
771 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
772 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
773 }
774};
775
Orion Hodsonc069a302017-01-18 09:23:12 +0000776struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
777 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
778 false, "Ljava/lang/invoke/CallSite;") {
779 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
780 }
781};
782
Orion Hodson005ac512017-10-24 15:43:43 +0100783struct VarHandleOffsets : public CheckOffsets<mirror::VarHandle> {
784 VarHandleOffsets() : CheckOffsets<mirror::VarHandle>(
785 false, "Ljava/lang/invoke/VarHandle;") {
786 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, access_modes_bit_mask_), "accessModesBitMask");
787 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type0_), "coordinateType0");
788 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, coordinate_type1_), "coordinateType1");
789 addOffset(OFFSETOF_MEMBER(mirror::VarHandle, var_type_), "varType");
790 }
791};
792
793struct FieldVarHandleOffsets : public CheckOffsets<mirror::FieldVarHandle> {
794 FieldVarHandleOffsets() : CheckOffsets<mirror::FieldVarHandle>(
795 false, "Ljava/lang/invoke/FieldVarHandle;") {
796 addOffset(OFFSETOF_MEMBER(mirror::FieldVarHandle, art_field_), "artField");
797 }
798};
799
Orion Hodsonffad6682018-01-02 10:18:58 +0000800struct ArrayElementVarHandleOffsets : public CheckOffsets<mirror::ArrayElementVarHandle> {
801 ArrayElementVarHandleOffsets() : CheckOffsets<mirror::ArrayElementVarHandle>(
802 false, "Ljava/lang/invoke/ArrayElementVarHandle;") {
803 }
804};
805
Orion Hodson005ac512017-10-24 15:43:43 +0100806struct ByteArrayViewVarHandleOffsets : public CheckOffsets<mirror::ByteArrayViewVarHandle> {
807 ByteArrayViewVarHandleOffsets() : CheckOffsets<mirror::ByteArrayViewVarHandle>(
808 false, "Ljava/lang/invoke/ByteArrayViewVarHandle;") {
809 addOffset(OFFSETOF_MEMBER(mirror::ByteArrayViewVarHandle, native_byte_order_),
810 "nativeByteOrder");
811 }
812};
813
814struct ByteBufferViewVarHandleOffsets : public CheckOffsets<mirror::ByteBufferViewVarHandle> {
815 ByteBufferViewVarHandleOffsets() : CheckOffsets<mirror::ByteBufferViewVarHandle>(
816 false, "Ljava/lang/invoke/ByteBufferViewVarHandle;") {
817 addOffset(OFFSETOF_MEMBER(mirror::ByteBufferViewVarHandle, native_byte_order_),
818 "nativeByteOrder");
819 }
820};
821
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400822// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400823// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700824// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400825TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700827 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700828 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700829 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700830 EXPECT_TRUE(StringOffsets().Check());
831 EXPECT_TRUE(ThrowableOffsets().Check());
832 EXPECT_TRUE(StackTraceElementOffsets().Check());
833 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400834 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700835 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700836 EXPECT_TRUE(ReferenceOffsets().Check());
837 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700838 EXPECT_TRUE(AccessibleObjectOffsets().Check());
839 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100840 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100841 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000842 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100843 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000844 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100845 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000846 EXPECT_TRUE(CallSiteOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100847 EXPECT_TRUE(VarHandleOffsets().Check());
848 EXPECT_TRUE(FieldVarHandleOffsets().Check());
Orion Hodsonffad6682018-01-02 10:18:58 +0000849 EXPECT_TRUE(ArrayElementVarHandleOffsets().Check());
Orion Hodson005ac512017-10-24 15:43:43 +0100850 EXPECT_TRUE(ByteArrayViewVarHandleOffsets().Check());
851 EXPECT_TRUE(ByteBufferViewVarHandleOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700852}
853
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700854TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700855 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856 AssertNonExistentClass("NoSuchClass;");
857 AssertNonExistentClass("LNoSuchClass;");
858}
859
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700860TEST_F(ClassLinkerTest, GetDexFiles) {
861 ScopedObjectAccess soa(Thread::Current());
862
863 jobject jclass_loader = LoadDex("Nested");
864 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
865 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800866 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700867
868 jobject jclass_loader2 = LoadDex("MultiDex");
869 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
870 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800871 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700872}
873
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700874TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700876 StackHandleScope<1> hs(soa.Self());
877 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700878 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700879
Vladimir Markoe9987b02018-05-22 16:26:43 +0100880 ObjPtr<mirror::Class> outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700881 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700882 EXPECT_EQ(0U, outer->NumVirtualMethods());
883 EXPECT_EQ(1U, outer->NumDirectMethods());
884
Vladimir Markoe9987b02018-05-22 16:26:43 +0100885 ObjPtr<mirror::Class> inner =
886 class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700887 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700888 EXPECT_EQ(0U, inner->NumVirtualMethods());
889 EXPECT_EQ(1U, inner->NumDirectMethods());
890}
891
892TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700894 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800895 for (int ch = 1; ch < 256; ++ch) {
896 std::string descriptor;
897 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700898 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700899 AssertNonExistentClass(descriptor);
900 } else {
901 AssertPrimitiveClass(descriptor);
902 }
903 }
904}
905
906TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700907 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100908 StackHandleScope<2> hs(soa.Self());
909 Handle<mirror::Class> JavaLangObject = hs.NewHandle(
910 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
911 AssertObjectClass(JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700912
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700913 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700914 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700915 AssertNonExistentClass("LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100916 ObjPtr<mirror::Class> MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700917 ASSERT_TRUE(MyClass != nullptr);
918 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700919 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100920 EXPECT_OBJ_PTR_EQ(JavaLangObject.Get(), MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000921 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700922 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Vladimir Markoe9987b02018-05-22 16:26:43 +0100923 EXPECT_OBJ_PTR_EQ(MyClass->GetSuperClass(), JavaLangObject.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700924 EXPECT_TRUE(MyClass->HasSuperClass());
Vladimir Markoe9987b02018-05-22 16:26:43 +0100925 EXPECT_OBJ_PTR_EQ(class_loader.Get(), MyClass->GetClassLoader());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000926 EXPECT_EQ(ClassStatus::kResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700927 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700928 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700929 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000930 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700931 EXPECT_FALSE(MyClass->IsVerified());
932 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700933 EXPECT_FALSE(MyClass->IsArrayInstance());
934 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700935 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700936 EXPECT_FALSE(MyClass->IsInterface());
937 EXPECT_FALSE(MyClass->IsPublic());
938 EXPECT_FALSE(MyClass->IsFinal());
939 EXPECT_FALSE(MyClass->IsPrimitive());
940 EXPECT_FALSE(MyClass->IsSynthetic());
941 EXPECT_EQ(1U, MyClass->NumDirectMethods());
942 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
943 EXPECT_EQ(0U, MyClass->NumInstanceFields());
944 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700945 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700946
947 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
948
949 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700950 AssertArrayClass("[C", "C", nullptr);
951 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700952 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700953 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700954 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700955 // or not available at all
956 AssertNonExistentClass("[[[[LNonExistentClass;");
957}
958
Mathieu Chartierb8901302016-09-30 10:27:43 -0700959TEST_F(ClassLinkerTest, LookupResolvedType) {
960 ScopedObjectAccess soa(Thread::Current());
961 StackHandleScope<1> hs(soa.Self());
962 Handle<mirror::ClassLoader> class_loader(
963 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
964 AssertNonExistentClass("LMyClass;");
965 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800966 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700967 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700968 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000969 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700970 klass);
971 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000972 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700973 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
974 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000975 class_linker_->LookupResolvedType(type_idx, dex_cache, class_loader.Get()),
Mathieu Chartierb8901302016-09-30 10:27:43 -0700976 klass);
977}
978
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000979TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
980 ScopedObjectAccess soa(Thread::Current());
981 StackHandleScope<2> hs(soa.Self());
982 Handle<mirror::ClassLoader> class_loader(
983 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
984 // Get the AllFields class for the dex cache and dex file.
985 ObjPtr<mirror::Class> all_fields_klass
986 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
987 ASSERT_OBJ_PTR_NE(all_fields_klass, ObjPtr<mirror::Class>(nullptr));
988 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
989 const DexFile& dex_file = *dex_cache->GetDexFile();
990 // Get the index of the array class we want to test.
991 const DexFile::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
992 ASSERT_TRUE(array_id != nullptr);
993 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
994 // Check that the array class wasn't resolved yet.
995 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000996 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000997 ObjPtr<mirror::Class>(nullptr));
998 // Resolve the array class we want to test.
999 ObjPtr<mirror::Class> array_klass
1000 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
1001 ASSERT_OBJ_PTR_NE(array_klass, ObjPtr<mirror::Class>(nullptr));
1002 // Test that LookupResolvedType() finds the array class.
1003 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001004 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001005 array_klass);
1006 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001007 dex_cache->ClearResolvedType(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001008 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
1009 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001010 class_linker_->LookupResolvedType(array_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +00001011 array_klass);
1012}
1013
Vladimir Marko72ab6842017-01-20 19:32:50 +00001014TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
1015 ScopedObjectAccess soa(Thread::Current());
1016 StackHandleScope<3> hs(soa.Self());
1017 Handle<mirror::ClassLoader> class_loader(
1018 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
1019 AssertNonExistentClass("LErroneousInit;");
1020 Handle<mirror::Class> klass =
1021 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
1022 ASSERT_OBJ_PTR_NE(klass.Get(), ObjPtr<mirror::Class>(nullptr));
1023 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
1024 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Vladimir Marko72ab6842017-01-20 19:32:50 +00001025 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001026 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001027 klass.Get());
1028 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001029 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001030 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1031 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001032 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001033 klass.Get());
1034 // Force initialization to turn the class erroneous.
1035 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1036 klass,
1037 /* can_init_fields */ true,
1038 /* can_init_parents */ true);
1039 EXPECT_FALSE(initialized);
1040 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1041 soa.Self()->ClearException();
1042 // Check that the LookupResolvedType() can still find the resolved type.
1043 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001044 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001045 klass.Get());
1046 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001047 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001048 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1049 EXPECT_OBJ_PTR_EQ(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001050 class_linker_->LookupResolvedType(type_idx, dex_cache.Get(), class_loader.Get()),
Vladimir Marko72ab6842017-01-20 19:32:50 +00001051 klass.Get());
1052}
1053
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001054TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001056 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1057 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001058}
1059
Ian Rogersa15e67d2012-02-28 13:51:55 -08001060// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001061// start of the object
1062TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001064 ObjPtr<mirror::Class> array_class =
1065 class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
1066 ObjPtr<mirror::ObjectArray<mirror::String>> array =
Brian Carlstromea46f952013-07-30 01:26:50 -07001067 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001068 uintptr_t data_offset =
1069 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1070 0));
1071 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001072 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1073 } else {
1074 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1075 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001076}
1077
1078TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001080 StackHandleScope<5> hs(soa.Self());
1081 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001082 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001083 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1084 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1085
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001086 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001087 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001088 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1089 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1090
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001091 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001092 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001093 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1094 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1095
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001096 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001097 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001098 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1099 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1100
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001101 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001102 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001103 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1104 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1105
1106 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001107}
1108
Elliott Hughes33203b52011-09-20 19:42:01 -07001109TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1110 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1111 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001113 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001114 ObjPtr<mirror::Class> c;
Ian Rogers98379392014-02-24 16:53:16 -08001115 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001116 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001117 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001118 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001119 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001120 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001121 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001122 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001123 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001124 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001125 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", 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/Long;", 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/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001130 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001131}
1132
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001133TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001135 StackHandleScope<3> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001136 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001137 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001138 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001139 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001140 Handle<mirror::Class> MyClass_1 = hs.NewHandle(
1141 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1));
1142 ObjPtr<mirror::Class> MyClass_2 =
1143 class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001144 EXPECT_TRUE(MyClass_1 != nullptr);
1145 EXPECT_TRUE(MyClass_2 != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001146 EXPECT_OBJ_PTR_NE(MyClass_1.Get(), MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001147}
1148
Jesse Wilson7833bd22011-08-09 18:31:44 -04001149TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001151 StackHandleScope<2> hs(soa.Self());
1152 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001153 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001154 Handle<mirror::Class> statics(
1155 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001156 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001157
jeffhaoabcfde32011-09-29 15:05:18 -07001158 // Static final primitives that are initialized by a compile-time constant
1159 // expression resolve to a copy of a constant value from the constant pool.
1160 // So <clinit> should be null.
Vladimir Markoba118822017-06-12 15:41:56 +01001161 ArtMethod* clinit = statics->FindClassMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001162 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001163
1164 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001165
Vladimir Marko19a4d372016-12-08 14:41:46 +00001166 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001167 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001168 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1169 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001170
Vladimir Marko19a4d372016-12-08 14:41:46 +00001171 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001172 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001173 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1174 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001175
Vladimir Marko19a4d372016-12-08 14:41:46 +00001176 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001177 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001178 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1179 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001180
Vladimir Marko19a4d372016-12-08 14:41:46 +00001181 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001182 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001183 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1184 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001185
Vladimir Marko19a4d372016-12-08 14:41:46 +00001186 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001187 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001188 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1189 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001190
Vladimir Marko19a4d372016-12-08 14:41:46 +00001191 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001192 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001193 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1194 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001195
Vladimir Marko19a4d372016-12-08 14:41:46 +00001196 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001197 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001198 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001199 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001200
Vladimir Marko19a4d372016-12-08 14:41:46 +00001201 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001202 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001203 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001204 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001205
Vladimir Marko19a4d372016-12-08 14:41:46 +00001206 ArtField* s8 = mirror::Class::FindStaticField(
1207 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001208 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001209 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001210 ObjPtr<mirror::String> str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
Mathieu Chartier3398c782016-09-30 10:27:43 -07001211 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001212
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001213 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1214 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001215 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1216 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1217 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1218 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1219 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1220 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001221 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1222 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001223 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001224}
1225
Brian Carlstrom30b94452011-08-25 21:35:26 -07001226TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001228 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001229 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001230 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001231 Handle<mirror::Class> I(
1232 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1233 Handle<mirror::Class> J(
1234 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1235 Handle<mirror::Class> K(
1236 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1237 Handle<mirror::Class> A(
1238 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1239 Handle<mirror::Class> B(
1240 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1241 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1242 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1243 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1244 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1245 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001246
Ian Rogersd91d6d62013-09-25 20:26:14 -07001247 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Vladimir Markoba118822017-06-12 15:41:56 +01001248 ArtMethod* Ii = I->FindClassMethod("i", void_sig, kRuntimePointerSize);
1249 ArtMethod* Jj1 = J->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1250 ArtMethod* Jj2 = J->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Andreas Gampe542451c2016-07-26 09:02:02 -07001251 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1252 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1253 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001254 ArtMethod* Ai = A->FindClassMethod("i", void_sig, kRuntimePointerSize);
1255 ArtMethod* Aj1 = A->FindClassMethod("j1", void_sig, kRuntimePointerSize);
1256 ArtMethod* Aj2 = A->FindClassMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001257 ASSERT_TRUE(Ii != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001258 ASSERT_FALSE(Ii->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001259 ASSERT_TRUE(Jj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001260 ASSERT_FALSE(Jj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001261 ASSERT_TRUE(Jj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001262 ASSERT_FALSE(Jj2->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001263 ASSERT_TRUE(Kj1 != nullptr);
1264 ASSERT_TRUE(Kj2 != nullptr);
1265 ASSERT_TRUE(Kk != nullptr);
1266 ASSERT_TRUE(Ai != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001267 ASSERT_FALSE(Ai->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001268 ASSERT_TRUE(Aj1 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001269 ASSERT_FALSE(Aj1->IsDirect());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001270 ASSERT_TRUE(Aj2 != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01001271 ASSERT_FALSE(Aj2->IsDirect());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001272 EXPECT_NE(Ii, Ai);
1273 EXPECT_NE(Jj1, Aj1);
1274 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001275 EXPECT_EQ(Kj1, Jj1);
1276 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001277 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1278 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1279 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1280 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1281 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1282 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001283
Vladimir Marko19a4d372016-12-08 14:41:46 +00001284 ArtField* Afoo =
1285 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1286 ArtField* Bfoo =
1287 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1288 ArtField* Jfoo =
1289 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1290 ArtField* Kfoo =
1291 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001292 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001293 EXPECT_EQ(Afoo, Bfoo);
1294 EXPECT_EQ(Afoo, Jfoo);
1295 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001296}
1297
Elliott Hughesf3778f62012-01-26 14:14:35 -08001298TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001299 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001300
jeffhaoabcfde32011-09-29 15:05:18 -07001301 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1302 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001303
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 ScopedObjectAccess soa(Thread::Current());
1305 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001306 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001307 StackHandleScope<1> hs(soa.Self());
1308 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001309 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001310 ObjPtr<mirror::Class> klass =
1311 class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001312 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001313 ArtMethod* getS0 =
1314 klass->FindClassMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
1315 ASSERT_TRUE(getS0 != nullptr);
1316 ASSERT_TRUE(getS0->IsStatic());
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001317 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001318 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001319 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001320 ObjPtr<mirror::Class> uninit = ResolveVerifyAndClinit(type_idx,
1321 clinit,
1322 soa.Self(),
1323 /* can_run_clinit */ true,
1324 /* verify_access */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001325 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001326 EXPECT_FALSE(uninit->IsInitialized());
Vladimir Marko28e012a2017-12-07 11:22:59 +00001327 ObjPtr<mirror::Class> init = ResolveVerifyAndClinit(type_idx,
1328 getS0,
1329 soa.Self(),
1330 /* can_run_clinit */ true,
1331 /* verify_access */ false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001332 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001333 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001334}
1335
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001336TEST_F(ClassLinkerTest, ErroneousClass) {
1337 ScopedObjectAccess soa(Thread::Current());
1338 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1339 StackHandleScope<1> hs(soa.Self());
1340 Handle<mirror::ClassLoader> class_loader(
1341 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1342 hs.Self()->AssertNoPendingException();
1343 const char* descriptor = "LErroneous;";
1344 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1345 // Erronenous since we are extending final class.
1346 hs.Self()->AssertPendingException();
1347 EXPECT_TRUE(klass == nullptr);
1348 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1349 EXPECT_FALSE(klass == nullptr);
1350 EXPECT_TRUE(klass->IsErroneous());
1351 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1352}
1353
Elliott Hughes20cde902011-10-04 17:37:27 -07001354TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001356 ObjPtr<mirror::Class> c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001357
1358 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001359 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001360 EXPECT_FALSE(c->IsFinalizable());
1361
1362 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001363 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001364 EXPECT_FALSE(c->IsFinalizable());
1365
1366 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001367 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001368 EXPECT_FALSE(c->IsFinalizable());
1369
1370 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001371 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001372 EXPECT_TRUE(c->IsFinalizable());
1373
1374 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001375 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001376 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001377 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001378 EXPECT_TRUE(c->IsFinalizable());
1379
1380 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1381 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001382 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001383 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001384 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001385 EXPECT_TRUE(c->IsFinalizable());
1386}
1387
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001388TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001389 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001390 std::string temp;
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001391 for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) {
1392 ObjPtr<mirror::Class> klass = GetClassRoot(ClassRoot(i), class_linker_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001393 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001394 EXPECT_STREQ(klass->GetDescriptor(&temp), GetClassRootDescriptor(ClassRoot(i))) << " i = " << i;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001395 }
1396}
1397
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001398TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1399 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001400 ScopedNullHandle<mirror::ClassLoader> class_loader;
Vladimir Markoe9987b02018-05-22 16:26:43 +01001401 ObjPtr<mirror::Class> c;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001402
1403 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001404 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001405 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001406
1407 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001408 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001409 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001410
1411 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001412 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001413 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001414
1415 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001416 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001417 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001418}
1419
Mathieu Chartiere401d142015-04-22 13:56:20 -07001420static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001421 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001422 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001423 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001424 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001425 }
1426}
1427
Vladimir Markoe9987b02018-05-22 16:26:43 +01001428static void CheckVerificationAttempted(ObjPtr<mirror::Class> c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001429 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001430 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001431 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001432 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001433 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001434 }
1435}
1436
1437TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1438 ScopedObjectAccess soa(Thread::Current());
1439
Vladimir Markoe9987b02018-05-22 16:26:43 +01001440 ObjPtr<mirror::Class> JavaLangObject =
1441 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001442 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001443 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1444 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001445 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001446}
1447
1448TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1449 ScopedObjectAccess soa(Thread::Current());
1450
1451 StackHandleScope<1> hs(soa.Self());
1452
1453 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1454 soa.Self(), "Ljava/lang/SecurityManager;")));
1455 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1456 "core";
1457
Igor Murashkindf707e42016-02-02 16:56:50 -08001458 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001459
Ian Rogers7b078e82014-09-10 14:44:24 -07001460 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001461 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001462}
1463
1464TEST_F(ClassLinkerTest, Preverified_App) {
1465 ScopedObjectAccess soa(Thread::Current());
1466
1467 StackHandleScope<2> hs(soa.Self());
1468 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001469 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001470 Handle<mirror::Class> statics(
1471 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1472
Igor Murashkindf707e42016-02-02 16:56:50 -08001473 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001474
Ian Rogers7b078e82014-09-10 14:44:24 -07001475 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001476 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001477}
1478
Sebastien Hertz6963e442014-11-26 22:11:27 +01001479TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1480 ScopedObjectAccess soa(Thread::Current());
1481
1482 StackHandleScope<3> hs(soa.Self());
1483 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001484 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001485
1486 // java.lang.Object is a bootstrap class.
1487 Handle<mirror::Class> jlo_class(
1488 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001489 ASSERT_TRUE(jlo_class != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001490 EXPECT_TRUE(jlo_class->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001491
1492 // Statics is not a bootstrap class.
1493 Handle<mirror::Class> statics(
1494 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001495 ASSERT_TRUE(statics != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001496 EXPECT_FALSE(statics->IsBootStrapClassLoaded());
Sebastien Hertz6963e442014-11-26 22:11:27 +01001497}
1498
Mathieu Chartier76172162016-01-26 14:54:06 -08001499// Regression test for b/26799552.
1500TEST_F(ClassLinkerTest, RegisterDexFileName) {
1501 ScopedObjectAccess soa(Thread::Current());
1502 StackHandleScope<2> hs(soa.Self());
1503 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1504 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1505 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001506 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001507 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001508 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001509 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001510 break;
1511 }
1512 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001513 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001514 }
1515 // Make a copy of the dex cache and change the name.
1516 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1517 const uint16_t data[] = { 0x20AC, 0x20A1 };
1518 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1519 arraysize(data),
1520 data)));
1521 dex_cache->SetLocation(location.Get());
1522 const DexFile* old_dex_file = dex_cache->GetDexFile();
1523
David Sehr0b426772018-07-03 23:03:42 +00001524 std::unique_ptr<DexFile> dex_file(new StandardDexFile(old_dex_file->Begin(),
1525 old_dex_file->Size(),
1526 location->ToModifiedUtf8(),
1527 0u,
1528 nullptr,
1529 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001530 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001531 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001532 // Check that inserting with a UTF16 name works.
Vladimir Markocd556b02017-02-03 11:47:34 +00001533 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001534 }
1535}
1536
Narayan Kamath25352fc2016-08-03 12:46:58 +01001537TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1538 ScopedObjectAccess soa(Thread::Current());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001539 StackHandleScope<7> hs(soa.Self());
1540
1541 Handle<mirror::ClassLoader> class_loader(
1542 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1543 Handle<mirror::Class> method_types(
1544 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1545 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1546
Vladimir Markoba118822017-06-12 15:41:56 +01001547 ArtMethod* method1 = method_types->FindClassMethod(
1548 "method1",
1549 "(Ljava/lang/String;)Ljava/lang/String;",
1550 kRuntimePointerSize);
1551 ASSERT_TRUE(method1 != nullptr);
1552 ASSERT_FALSE(method1->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001553
1554 const DexFile& dex_file = *(method1->GetDexFile());
1555 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +01001556 class_linker_->FindDexCache(soa.Self(), dex_file));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001557
1558 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
1559
1560 // This is the MethodType corresponding to the prototype of
1561 // String MethodTypes# method1(String).
1562 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001563 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001564 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001565 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001566
1567 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001568 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001569
1570 // Assert that the return type and the method arguments are as we expect.
Orion Hodsone7732be2017-10-11 14:35:20 +01001571 Handle<mirror::Class> string_class(hs.NewHandle(class_linker_->FindClass(soa.Self(),
1572 "Ljava/lang/String;",
1573 class_loader)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001574 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetRType());
1575 ASSERT_OBJ_PTR_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
Narayan Kamath25352fc2016-08-03 12:46:58 +01001576
1577 // Resolve the method type again and assert that we get back the same value.
1578 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001579 class_linker_->ResolveMethodType(soa.Self(), method1_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001580 ASSERT_OBJ_PTR_EQ(method1_type.Get(), method1_type2.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001581
1582 // Resolve the MethodType associated with a different method signature
1583 // and assert it's different.
Vladimir Markoba118822017-06-12 15:41:56 +01001584 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +01001585 "method2",
1586 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1587 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +01001588 ASSERT_TRUE(method2 != nullptr);
1589 ASSERT_FALSE(method2->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001590 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
1591 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Vladimir Markoaf940202017-12-08 15:01:18 +00001592 class_linker_->ResolveMethodType(soa.Self(), method2_id.proto_idx_, dex_cache, class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001593 ASSERT_OBJ_PTR_NE(method1_type.Get(), method2_type.Get());
Narayan Kamath25352fc2016-08-03 12:46:58 +01001594}
1595
Calin Juravle7865ac72017-06-28 11:03:12 -07001596// Verify that ClassLinker's CreateWellknownClassLoader works as expected
1597// by creating a chain of class loaders with various dex files.
1598TEST_F(ClassLinkerTest, CreateWellKnownClassLoader) {
1599 // LoadDexIn*ClassLoader methods already assert that the parent loader is the expected one.
1600 // No need to check again.
1601 jobject class_loader_a = LoadDexInPathClassLoader("MyClass", nullptr);
1602 jobject class_loader_b = LoadDexInDelegateLastClassLoader("Nested", class_loader_a);
1603 jobject class_loader_c = LoadDexInPathClassLoader("MultiDex", class_loader_b);
1604 LoadDexInDelegateLastClassLoader("Interfaces", class_loader_c);
1605}
1606
David Sehrc431b9d2018-03-02 12:01:51 -08001607TEST_F(ClassLinkerTest, PrettyClass) {
1608 ScopedObjectAccess soa(Thread::Current());
1609 EXPECT_EQ("null", mirror::Class::PrettyClass(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001610 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001611 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001612 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001613 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Class::PrettyClass(o->GetClass()));
1614}
1615
1616TEST_F(ClassLinkerTest, PrettyClassAndClassLoader) {
1617 ScopedObjectAccess soa(Thread::Current());
1618 EXPECT_EQ("null", mirror::Class::PrettyClassAndClassLoader(nullptr));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001619 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001620 ASSERT_TRUE(c != nullptr);
Vladimir Markoe9987b02018-05-22 16:26:43 +01001621 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
David Sehrc431b9d2018-03-02 12:01:51 -08001622 EXPECT_EQ("java.lang.Class<java.lang.String[],null>",
1623 mirror::Class::PrettyClassAndClassLoader(o->GetClass()));
1624}
1625
1626TEST_F(ClassLinkerTest, PrettyField) {
1627 ScopedObjectAccess soa(Thread::Current());
1628 EXPECT_EQ("null", ArtField::PrettyField(nullptr));
1629
Vladimir Markoe9987b02018-05-22 16:26:43 +01001630 ObjPtr<mirror::Class> java_lang_String = class_linker_->FindSystemClass(soa.Self(),
1631 "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001632
1633 ArtField* f;
1634 f = java_lang_String->FindDeclaredInstanceField("count", "I");
1635 EXPECT_EQ("int java.lang.String.count", f->PrettyField());
1636 EXPECT_EQ("java.lang.String.count", f->PrettyField(false));
1637}
1638
1639TEST_F(ClassLinkerTest, JniShortName_JniLongName) {
1640 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoe9987b02018-05-22 16:26:43 +01001641 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
David Sehrc431b9d2018-03-02 12:01:51 -08001642 ASSERT_TRUE(c != nullptr);
1643 ArtMethod* m;
1644
1645 m = c->FindClassMethod("charAt", "(I)C", kRuntimePointerSize);
1646 ASSERT_TRUE(m != nullptr);
1647 ASSERT_FALSE(m->IsDirect());
1648 EXPECT_EQ("Java_java_lang_String_charAt", m->JniShortName());
1649 EXPECT_EQ("Java_java_lang_String_charAt__I", m->JniLongName());
1650
1651 m = c->FindClassMethod("indexOf", "(Ljava/lang/String;I)I", kRuntimePointerSize);
1652 ASSERT_TRUE(m != nullptr);
1653 ASSERT_FALSE(m->IsDirect());
1654 EXPECT_EQ("Java_java_lang_String_indexOf", m->JniShortName());
1655 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", m->JniLongName());
1656
1657 m = c->FindClassMethod("copyValueOf", "([CII)Ljava/lang/String;", kRuntimePointerSize);
1658 ASSERT_TRUE(m != nullptr);
1659 ASSERT_TRUE(m->IsStatic());
1660 EXPECT_EQ("Java_java_lang_String_copyValueOf", m->JniShortName());
1661 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", m->JniLongName());
1662}
1663
Calin Juravlecdd49122017-07-05 20:09:53 -07001664class ClassLinkerClassLoaderTest : public ClassLinkerTest {
1665 protected:
1666 // Verifies that the class identified by the given descriptor is loaded with
1667 // the expected_class_loader_obj when search from class_loader_to_search_obj.
1668 // When expected_class_loader_obj is null the check will be done against BootClassLoader.
1669 void VerifyClassResolution(const std::string& descriptor,
1670 jobject class_loader_to_search_obj,
1671 jobject expected_class_loader_obj,
1672 bool should_find = true) {
1673 Thread* self = Thread::Current();
1674 ScopedObjectAccess soa(self);
1675 StackHandleScope<3> hs(self);
1676 Handle<mirror::ClassLoader> class_loader_to_search(
1677 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_to_search_obj)));
1678
1679 Handle<mirror::Class> klass = hs.NewHandle(
1680 class_linker_->FindClass(soa.Self(), descriptor.c_str(), class_loader_to_search));
1681
1682 if (!should_find) {
1683 if (self->IsExceptionPending()) {
1684 self->ClearException();
1685 }
1686 ASSERT_TRUE(klass == nullptr);
1687 } else if (expected_class_loader_obj == nullptr) {
1688 ASSERT_TRUE(ClassLinker::IsBootClassLoader(soa, klass->GetClassLoader()));
1689 } else {
1690 ASSERT_TRUE(klass != nullptr) << descriptor;
1691 Handle<mirror::ClassLoader> expected_class_loader(
1692 hs.NewHandle(soa.Decode<mirror::ClassLoader>(expected_class_loader_obj)));
Vladimir Markoe9987b02018-05-22 16:26:43 +01001693 ASSERT_OBJ_PTR_EQ(klass->GetClassLoader(), expected_class_loader.Get());
Calin Juravlecdd49122017-07-05 20:09:53 -07001694 }
1695 }
1696};
1697
1698TEST_F(ClassLinkerClassLoaderTest, CreatePathClassLoader) {
1699 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1700 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1701 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
1702 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find*/ false);
1703}
1704
1705TEST_F(ClassLinkerClassLoaderTest, CreateDelegateLastClassLoader) {
1706 jobject class_loader_a = LoadDexInDelegateLastClassLoader("ForClassLoaderA", nullptr);
1707 VerifyClassResolution("LDefinedInA;", class_loader_a, class_loader_a);
1708 VerifyClassResolution("Ljava/lang/String;", class_loader_a, nullptr);
1709 VerifyClassResolution("LDefinedInB;", class_loader_a, nullptr, /*should_find*/ false);
1710}
1711
1712TEST_F(ClassLinkerClassLoaderTest, CreateClassLoaderChain) {
1713 // The chain is
1714 // ClassLoaderA (PathClassLoader, defines: A, AB, AC, AD)
1715 // ^
1716 // |
1717 // ClassLoaderB (DelegateLastClassLoader, defines: B, AB, BC, BD)
1718 // ^
1719 // |
1720 // ClassLoaderC (PathClassLoader, defines: C, AC, BC, CD)
1721 // ^
1722 // |
Calin Juravlee64ed9e2017-07-06 22:01:00 -07001723 // ClassLoaderD (DelegateLastClassLoader, defines: D, AD, BD, CD)
Calin Juravlecdd49122017-07-05 20:09:53 -07001724
1725 jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
1726 jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
1727 jobject class_loader_c = LoadDexInPathClassLoader("ForClassLoaderC", class_loader_b);
1728 jobject class_loader_d = LoadDexInDelegateLastClassLoader("ForClassLoaderD", class_loader_c);
1729
1730 // Verify exclusive classes (present in only one class loader).
1731 VerifyClassResolution("LDefinedInD;", class_loader_d, class_loader_d);
1732 VerifyClassResolution("LDefinedInC;", class_loader_d, class_loader_c);
1733 VerifyClassResolution("LDefinedInB;", class_loader_d, class_loader_b);
1734 VerifyClassResolution("LDefinedInA;", class_loader_d, class_loader_a);
1735
1736 // Verify classes that are defined in multiple classloader.
1737
1738 // Classes defined in B should be found in B even if they are defined in A or C because
1739 // B is a DelegateLastClassLoader.
1740 VerifyClassResolution("LDefinedInAB;", class_loader_d, class_loader_b);
1741 VerifyClassResolution("LDefinedInABC;", class_loader_d, class_loader_b);
1742 VerifyClassResolution("LDefinedInBC;", class_loader_d, class_loader_b);
1743
1744 // Classes defined in D should be found in D even if they are defined in parent class loaders
1745 // as well because D is a DelegateLastClassLoader.
1746 VerifyClassResolution("LDefinedInAD;", class_loader_d, class_loader_d);
1747 VerifyClassResolution("LDefinedInBD;", class_loader_d, class_loader_d);
1748 VerifyClassResolution("LDefinedInCD;", class_loader_d, class_loader_d);
1749
1750
1751 // Classes not defined in the DelegateLastClassLoaders (i.e. D or B) should be found
1752 // in the top parent.
1753 VerifyClassResolution("LDefinedInAC;", class_loader_d, class_loader_a);
1754
Calin Juravlecdd49122017-07-05 20:09:53 -07001755 // Sanity check that we don't find an undefined class.
1756 VerifyClassResolution("LNotDefined;", class_loader_d, nullptr, /*should_find*/ false);
1757}
1758
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001759} // namespace art