blob: 684a261cca1f99199c181ea947d138eb324c1277 [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"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080030#include "dex_file_types.h"
Alex Light705ad492015-09-21 11:36:30 -070031#include "experimental_flags.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/heap.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070034#include "mirror/accessible_object.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000035#include "mirror/call_site.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070037#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010039#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010040#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070041#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010042#include "mirror/method_type.h"
43#include "mirror/method_handle_impl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000044#include "mirror/method_handles_lookup.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
47#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070048#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070050#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070051#include "handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070053#include "thread-current-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
55namespace art {
56
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080057class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070058 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070061 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070062 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070063 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000064 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070065 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080066 mirror::Class* exception_class =
67 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070068 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070069 }
70
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080073 Thread* self = Thread::Current();
74 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070075 }
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070079 ASSERT_TRUE(primitive != nullptr);
80 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070082 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070083 std::string temp;
84 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070085 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070087 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070088 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070089 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070090 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070091 EXPECT_TRUE(primitive->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +000092 EXPECT_FALSE(primitive->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070093 EXPECT_TRUE(primitive->IsVerified());
94 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070095 EXPECT_FALSE(primitive->IsArrayInstance());
96 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070097 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070098 EXPECT_FALSE(primitive->IsInterface());
99 EXPECT_TRUE(primitive->IsPublic());
100 EXPECT_TRUE(primitive->IsFinal());
101 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700102 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103 EXPECT_EQ(0U, primitive->NumDirectMethods());
104 EXPECT_EQ(0U, primitive->NumVirtualMethods());
105 EXPECT_EQ(0U, primitive->NumInstanceFields());
106 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700107 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700108 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700109 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800110 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
111 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Elliott Hughes00626c22013-06-14 15:04:14 -0700112 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700113 }
114
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000115 void AssertObjectClass(mirror::Class* JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000117 ASSERT_TRUE(JavaLangObject != nullptr);
118 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
119 ASSERT_EQ(JavaLangObject->GetClass(),
120 JavaLangObject->GetClass()->GetClass());
121 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
122 std::string temp;
123 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
124 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
125 EXPECT_FALSE(JavaLangObject->HasSuperClass());
126 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
127 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
128 EXPECT_FALSE(JavaLangObject->IsErroneous());
129 EXPECT_TRUE(JavaLangObject->IsLoaded());
130 EXPECT_TRUE(JavaLangObject->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000131 EXPECT_FALSE(JavaLangObject->IsErroneousResolved());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000132 EXPECT_TRUE(JavaLangObject->IsVerified());
133 EXPECT_TRUE(JavaLangObject->IsInitialized());
134 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
135 EXPECT_FALSE(JavaLangObject->IsArrayClass());
136 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
137 EXPECT_FALSE(JavaLangObject->IsInterface());
138 EXPECT_TRUE(JavaLangObject->IsPublic());
139 EXPECT_FALSE(JavaLangObject->IsFinal());
140 EXPECT_FALSE(JavaLangObject->IsPrimitive());
141 EXPECT_FALSE(JavaLangObject->IsSynthetic());
Nicolas Geoffray6e311ac2017-02-27 11:46:03 +0000142 EXPECT_EQ(4U, JavaLangObject->NumDirectMethods());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000143 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
144 if (!kUseBrooksReadBarrier) {
145 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
146 } else {
147 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
148 }
149 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
150 "shadow$_klass_");
151 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
152 "shadow$_monitor_");
153 if (kUseBrooksReadBarrier) {
154 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
155 "shadow$_x_rb_ptr_");
156 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
157 "shadow$_x_xpadding_");
158 }
159
160 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
161 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
162
Andreas Gampe542451c2016-07-26 09:02:02 -0700163 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000164 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
165 ImTable* imt = JavaLangObject->GetImt(pointer_size);
166 ASSERT_NE(nullptr, imt);
167 for (size_t i = 0; i < ImTable::kSize; ++i) {
168 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
169 }
170 }
171
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 void AssertArrayClass(const std::string& array_descriptor,
173 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700174 mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800176 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 StackHandleScope<2> hs(self);
178 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
179 Handle<mirror::Class> array(
180 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700181 std::string temp;
182 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700183 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700184 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700185 AssertArrayClass(array_descriptor, array);
186 }
187
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700188 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800190 ASSERT_TRUE(array != nullptr);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700191 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700192 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700193 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700194 std::string temp;
195 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700196 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800197 Thread* self = Thread::Current();
198 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700199 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700200 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700201 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700203 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700204 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700205 EXPECT_TRUE(array->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000206 EXPECT_FALSE(array->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700207 EXPECT_TRUE(array->IsVerified());
208 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700209 EXPECT_FALSE(array->IsArrayInstance());
210 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700211 EXPECT_FALSE(array->IsInterface());
212 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
213 EXPECT_TRUE(array->IsFinal());
214 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700215 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700216 EXPECT_EQ(0U, array->NumDirectMethods());
217 EXPECT_EQ(0U, array->NumVirtualMethods());
218 EXPECT_EQ(0U, array->NumInstanceFields());
219 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700220 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000221 EXPECT_TRUE(array->ShouldHaveImt());
222 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700223 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700224 ASSERT_TRUE(array->GetIfTable() != nullptr);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000225 ObjPtr<mirror::Class> direct_interface0 =
226 mirror::Class::GetDirectInterface(self, array.Get(), 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700228 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Vladimir Marko19a4d372016-12-08 14:41:46 +0000229 ObjPtr<mirror::Class> direct_interface1 =
230 mirror::Class::GetDirectInterface(self, array.Get(), 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700231 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700232 ObjPtr<mirror::Class> array_ptr = array->GetComponentType();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700233 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000234
Andreas Gampe542451c2016-07-26 09:02:02 -0700235 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000236 mirror::Class* JavaLangObject =
237 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
238 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
239 // IMT of a array class should be shared with the IMT of the java.lag.Object
240 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700241 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700242
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700244 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700246 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700247 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700248
Andreas Gampe542451c2016-07-26 09:02:02 -0700249 EXPECT_TRUE(method->HasDexCacheResolvedMethods(kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700250 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100251 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700252 kRuntimePointerSize));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700253 }
254
Mathieu Chartier3398c782016-09-30 10:27:43 -0700255 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700257 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700259 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700260 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700261 }
262
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700263 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700265 std::string temp;
266 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700269 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700270 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700271 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700273 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700274 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700275 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700276 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700277 EXPECT_TRUE(klass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000278 EXPECT_FALSE(klass->IsErroneousResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700279 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700281 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700283 std::string temp2;
284 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
285 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700286 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700287 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800288 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700289 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800290 EXPECT_TRUE(m.IsStatic());
291 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700292 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700293 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700294 if (!klass->IsSynthetic()) {
295 EXPECT_NE(0U, klass->NumDirectMethods());
296 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700297 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700298 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700300 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700301 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700302 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700303 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700304 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700305 } else {
Alex Light705ad492015-09-21 11:36:30 -0700306 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700307 }
308 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700311 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700312 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700313 }
314 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700315 EXPECT_FALSE(klass->IsAbstract());
316 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700317 }
318 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700319 EXPECT_FALSE(klass->IsFinal());
320 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700321 }
322
323 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700324 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700325
Andreas Gampe542451c2016-07-26 09:02:02 -0700326 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 AssertMethod(&method);
328 EXPECT_TRUE(method.IsDirect());
329 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700330 }
331
Andreas Gampe542451c2016-07-26 09:02:02 -0700332 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 AssertMethod(&method);
334 EXPECT_FALSE(method.IsDirect());
Alex Lighte64300b2015-12-15 15:02:47 -0800335 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
336 }
337
Andreas Gampe542451c2016-07-26 09:02:02 -0700338 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800339 AssertMethod(&method);
340 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800341 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800342 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700343 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800344 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700345 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700346 }
347
348 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700349 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700350 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700351 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700352 }
353
354 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700355 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700356 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700357 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700358 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700359
Vladimir Marko76649e82014-11-10 18:32:59 +0000360 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700361 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000362 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
363 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
364 klass->NumReferenceInstanceFields() *
365 sizeof(mirror::HeapReference<mirror::Object>));
366 MemberOffset current_ref_offset = start_ref_offset;
367 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700368 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700369 ObjPtr<mirror::Class> field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700370 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000371 if (!field->IsPrimitiveType()) {
372 ASSERT_TRUE(!field_type->IsPrimitive());
373 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
374 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
375 // While Reference.referent is not primitive, the ClassLinker
376 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700377 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000378 } else {
379 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
380 sizeof(mirror::HeapReference<mirror::Object>));
381 }
382 } else {
383 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
384 // Shuffled before references.
385 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
386 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
387 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700388 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700389 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000390 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700391
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700392 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700393 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700394 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700395 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
396 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700397 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700398 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
399 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
400 // The reference instance offsets have a bit set for each reference offset.
401 // +1 for Object's class.
402 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
403 total_num_reference_instance_fields);
404 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700405 }
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700408 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700409 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800410 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700411 StackHandleScope<1> hs(self);
412 Handle<mirror::Class> klass(
413 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800414 ASSERT_TRUE(klass != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700415 std::string temp;
416 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700417 EXPECT_EQ(class_loader, klass->GetClassLoader());
418 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700420 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700421 AssertArrayClass(descriptor, klass);
422 } else {
423 AssertClass(descriptor, klass);
424 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700425 }
426
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800427 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700428 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700429 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800430 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
431 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
432 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700433 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700434 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700435 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800436 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800437 const DexFile::TypeId& type_id = dex.GetTypeId(dex::TypeIndex(i));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800438 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700439 AssertDexFileClass(class_loader, descriptor);
440 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700441 TestRootVisitor visitor;
442 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800443 // Verify the dex cache has resolution methods in all resolved method slots
Vladimir Markocd556b02017-02-03 11:47:34 +0000444 ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100446 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
447 EXPECT_TRUE(
Andreas Gampe542451c2016-07-26 09:02:02 -0700448 mirror::DexCache::GetElementPtrSize(resolved_methods, i, kRuntimePointerSize) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800450 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700451 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700452
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700453 class TestRootVisitor : public SingleRootVisitor {
454 public:
455 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
456 EXPECT_TRUE(root != nullptr);
457 }
458 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700459};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700460
Narayan Kamath25352fc2016-08-03 12:46:58 +0100461class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
462 protected:
463 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
464 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100465 }
466};
467
Brian Carlstrom693267a2011-09-06 09:25:34 -0700468struct CheckOffset {
469 size_t cpp_offset;
470 const char* java_name;
471 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
472};
473
Elliott Hughes80609252011-09-23 17:24:51 -0700474template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700475struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800476 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
477 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700478 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700479 std::string class_descriptor;
480 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700481
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700482 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800483 Thread* self = Thread::Current();
484 mirror::Class* klass =
485 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700486 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700487
488 bool error = false;
489
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 // Classes have a different size due to padding field. Strings are variable length.
491 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700492 // Currently only required for AccessibleObject since of the padding fields. The class linker
493 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
494 // The RoundUp is to get around this case.
495 static constexpr size_t kPackAlignment = 4;
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100496 size_t expected_size = RoundUp(is_static ? klass->GetClassSize() : klass->GetObjectSize(),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700497 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700498 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800499 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700500 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700501 << " Java=" << expected_size
502 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700503 error = true;
504 }
505 }
506
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700508 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800509 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700510 << " class=" << class_descriptor
511 << " Java=" << num_fields
512 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700513 error = true;
514 }
515
Brian Carlstromdbc05252011-09-09 01:59:59 -0700516 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700517 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700518 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800519 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700520 error = true;
521 }
522 }
523 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700524 for (size_t i = 0; i < offsets.size(); i++) {
525 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700526 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700527 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800529 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700530 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800531 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700532 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800533 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700534 << " CheckOffsets=" << offset.java_name;
535 }
536 }
537
Brian Carlstromdbc05252011-09-09 01:59:59 -0700538 for (size_t i = 0; i < offsets.size(); i++) {
539 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700540 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
542 error = true;
543 }
544 }
545 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700546 for (size_t i = 0; i < offsets.size(); i++) {
547 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700548 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800550 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800552 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700553 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
554 }
555 }
556
557 return !error;
558 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700559
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700560 void addOffset(size_t offset, const char* name) {
561 offsets.push_back(CheckOffset(offset, name));
562 }
563
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 private:
565 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700566};
567
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700568// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
569// is first since if it is failing, others are unlikely to succeed.
570
Brian Carlstromea46f952013-07-30 01:26:50 -0700571struct ObjectOffsets : public CheckOffsets<mirror::Object> {
572 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700573 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
574 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700575#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
577 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800578#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700579 };
580};
581
Brian Carlstromea46f952013-07-30 01:26:50 -0700582struct ClassOffsets : public CheckOffsets<mirror::Class> {
583 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700584 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700586 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
587 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
589 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800590 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700591 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700592 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
593 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700594 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700595 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
596 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800597 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700599 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
600 "numReferenceInstanceFields");
601 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
602 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700603 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700604 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
605 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700606 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
607 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
608 "referenceInstanceOffsets");
609 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
610 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
611 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800612 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700613 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700614 };
615};
616
Alex Lightd6251582016-10-31 11:12:30 -0700617struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
618 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
Alex Lighta01de592016-11-15 10:43:06 -0800619 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_dex_caches_), "obsoleteDexCaches");
620 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, obsolete_methods_), "obsoleteMethods");
Alex Light2f814aa2017-03-24 15:21:34 +0000621 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, original_dex_file_), "originalDexFile");
Alex Lightd6251582016-10-31 11:12:30 -0700622 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
623 }
624};
625
Brian Carlstromea46f952013-07-30 01:26:50 -0700626struct StringOffsets : public CheckOffsets<mirror::String> {
627 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700628 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000629 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700630 };
631};
632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
634 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000635 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700636 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
637 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700638 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
639 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700640 };
641};
642
Brian Carlstromea46f952013-07-30 01:26:50 -0700643struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700644 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
645 false, "Ljava/lang/StackTraceElement;") {
646 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
647 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
648 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
649 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700650 };
651};
652
Brian Carlstromea46f952013-07-30 01:26:50 -0700653struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
654 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700655 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700656 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700657 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
658 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
659 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700660 };
661};
662
Brian Carlstromea46f952013-07-30 01:26:50 -0700663struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
664 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700665 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400666 };
667};
668
Brian Carlstromea46f952013-07-30 01:26:50 -0700669struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
670 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Orion Hodsonc069a302017-01-18 09:23:12 +0000673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_call_sites_), "numResolvedCallSites");
Vladimir Marko05792b92015-08-03 11:56:49 +0100674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
678 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Orion Hodsonc069a302017-01-18 09:23:12 +0000679 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_call_sites_), "resolvedCallSites");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700680 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100681 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700682 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
683 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
684 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700685 };
686};
687
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700688struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
689 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700690 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
691 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
692 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
693 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700694 };
695};
696
697struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700698 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
699 false, "Ljava/lang/ref/FinalizerReference;") {
700 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
701 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
702 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700703 };
704};
705
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700706struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700707 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
708 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000709 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700710 };
711};
712
713struct FieldOffsets : public CheckOffsets<mirror::Field> {
714 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700715 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
716 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
717 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
718 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
719 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
720 };
721};
722
Neil Fuller16b21cd2016-08-12 09:37:02 +0100723struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
724 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
725 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100726 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
727 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
728 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
729 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
730 "declaringClassOfOverriddenMethod");
731 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100732 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
733 "hasRealParameterData");
734 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Neil Fuller16b21cd2016-08-12 09:37:02 +0100735 };
736};
737
Narayan Kamathafa48272016-08-03 12:46:58 +0100738struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
739 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
740 false, "Ljava/lang/invoke/MethodType;") {
741 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
742 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
743 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
744 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
745 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
746 }
747};
748
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000749struct MethodHandleOffsets : public CheckOffsets<mirror::MethodHandle> {
750 MethodHandleOffsets() : CheckOffsets<mirror::MethodHandle>(
751 false, "Ljava/lang/invoke/MethodHandle;") {
752 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, art_field_or_method_), "artFieldOrMethod");
753 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, cached_spread_invoker_),
754 "cachedSpreadInvoker");
755 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, handle_kind_), "handleKind");
756 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, nominal_type_), "nominalType");
757 addOffset(OFFSETOF_MEMBER(mirror::MethodHandle, method_type_), "type");
758 }
759};
760
Narayan Kamathafa48272016-08-03 12:46:58 +0100761struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
762 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000763 false, "Ljava/lang/invoke/MethodHandleImpl;") {
764 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, info_), "info");
Narayan Kamathafa48272016-08-03 12:46:58 +0100765 }
766};
767
Orion Hodsonc069a302017-01-18 09:23:12 +0000768struct MethodHandlesLookupOffsets : public CheckOffsets<mirror::MethodHandlesLookup> {
769 MethodHandlesLookupOffsets() : CheckOffsets<mirror::MethodHandlesLookup>(
770 false, "Ljava/lang/invoke/MethodHandles$Lookup;") {
771 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, allowed_modes_), "allowedModes");
772 addOffset(OFFSETOF_MEMBER(mirror::MethodHandlesLookup, lookup_class_), "lookupClass");
773 }
774};
775
Narayan Kamath000e1882016-10-24 17:14:25 +0100776struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
777 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
778 false, "Ldalvik/system/EmulatedStackFrame;") {
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000779 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, callsite_type_), "callsiteType");
Narayan Kamath000e1882016-10-24 17:14:25 +0100780 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
781 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
782 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
783 }
784};
785
Orion Hodsonc069a302017-01-18 09:23:12 +0000786struct CallSiteOffsets : public CheckOffsets<mirror::CallSite> {
787 CallSiteOffsets() : CheckOffsets<mirror::CallSite>(
788 false, "Ljava/lang/invoke/CallSite;") {
789 addOffset(OFFSETOF_MEMBER(mirror::CallSite, target_), "target");
790 }
791};
792
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400793// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400794// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700795// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400796TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700798 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700799 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700800 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700801 EXPECT_TRUE(StringOffsets().Check());
802 EXPECT_TRUE(ThrowableOffsets().Check());
803 EXPECT_TRUE(StackTraceElementOffsets().Check());
804 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400805 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700806 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700807 EXPECT_TRUE(ReferenceOffsets().Check());
808 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700809 EXPECT_TRUE(AccessibleObjectOffsets().Check());
810 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100811 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100812 EXPECT_TRUE(MethodTypeOffsets().Check());
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000813 EXPECT_TRUE(MethodHandleOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100814 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000815 EXPECT_TRUE(MethodHandlesLookupOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100816 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Orion Hodsonc069a302017-01-18 09:23:12 +0000817 EXPECT_TRUE(CallSiteOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700818}
819
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700820TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700822 AssertNonExistentClass("NoSuchClass;");
823 AssertNonExistentClass("LNoSuchClass;");
824}
825
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700826TEST_F(ClassLinkerTest, GetDexFiles) {
827 ScopedObjectAccess soa(Thread::Current());
828
829 jobject jclass_loader = LoadDex("Nested");
830 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
831 ASSERT_EQ(dex_files.size(), 1U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800832 EXPECT_TRUE(android::base::EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700833
834 jobject jclass_loader2 = LoadDex("MultiDex");
835 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
836 ASSERT_EQ(dex_files2.size(), 2U);
Andreas Gampe9186ced2016-12-12 14:28:21 -0800837 EXPECT_TRUE(android::base::EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700838}
839
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700840TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700842 StackHandleScope<1> hs(soa.Self());
843 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700844 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845
Ian Rogers98379392014-02-24 16:53:16 -0800846 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700847 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700848 EXPECT_EQ(0U, outer->NumVirtualMethods());
849 EXPECT_EQ(1U, outer->NumDirectMethods());
850
Ian Rogers98379392014-02-24 16:53:16 -0800851 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700852 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700853 EXPECT_EQ(0U, inner->NumVirtualMethods());
854 EXPECT_EQ(1U, inner->NumDirectMethods());
855}
856
857TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700859 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800860 for (int ch = 1; ch < 256; ++ch) {
861 std::string descriptor;
862 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700863 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700864 AssertNonExistentClass(descriptor);
865 } else {
866 AssertPrimitiveClass(descriptor);
867 }
868 }
869}
870
871TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800873 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000874 AssertObjectClass(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700875
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("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700879 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800880 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700881 ASSERT_TRUE(MyClass != nullptr);
882 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700883 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
884 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000885 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700886 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700887 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
888 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700889 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700890 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700891 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700892 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700893 EXPECT_TRUE(MyClass->IsResolved());
Vladimir Marko72ab6842017-01-20 19:32:50 +0000894 EXPECT_FALSE(MyClass->IsErroneousResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700895 EXPECT_FALSE(MyClass->IsVerified());
896 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700897 EXPECT_FALSE(MyClass->IsArrayInstance());
898 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700899 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700900 EXPECT_FALSE(MyClass->IsInterface());
901 EXPECT_FALSE(MyClass->IsPublic());
902 EXPECT_FALSE(MyClass->IsFinal());
903 EXPECT_FALSE(MyClass->IsPrimitive());
904 EXPECT_FALSE(MyClass->IsSynthetic());
905 EXPECT_EQ(1U, MyClass->NumDirectMethods());
906 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
907 EXPECT_EQ(0U, MyClass->NumInstanceFields());
908 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700909 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700910
911 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
912
913 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700914 AssertArrayClass("[C", "C", nullptr);
915 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700916 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700917 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700918 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700919 // or not available at all
920 AssertNonExistentClass("[[[[LNonExistentClass;");
921}
922
Mathieu Chartierb8901302016-09-30 10:27:43 -0700923TEST_F(ClassLinkerTest, LookupResolvedType) {
924 ScopedObjectAccess soa(Thread::Current());
925 StackHandleScope<1> hs(soa.Self());
926 Handle<mirror::ClassLoader> class_loader(
927 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
928 AssertNonExistentClass("LMyClass;");
929 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800930 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
Mathieu Chartierb8901302016-09-30 10:27:43 -0700931 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
932 const DexFile& dex_file = klass->GetDexFile();
Mathieu Chartierb8901302016-09-30 10:27:43 -0700933 EXPECT_OBJ_PTR_EQ(
934 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
935 klass);
936 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000937 dex_cache->ClearResolvedType(type_idx);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700938 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
939 EXPECT_OBJ_PTR_EQ(
940 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
941 klass);
942}
943
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000944TEST_F(ClassLinkerTest, LookupResolvedTypeArray) {
945 ScopedObjectAccess soa(Thread::Current());
946 StackHandleScope<2> hs(soa.Self());
947 Handle<mirror::ClassLoader> class_loader(
948 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("AllFields"))));
949 // Get the AllFields class for the dex cache and dex file.
950 ObjPtr<mirror::Class> all_fields_klass
951 = class_linker_->FindClass(soa.Self(), "LAllFields;", class_loader);
952 ASSERT_OBJ_PTR_NE(all_fields_klass, ObjPtr<mirror::Class>(nullptr));
953 Handle<mirror::DexCache> dex_cache = hs.NewHandle(all_fields_klass->GetDexCache());
954 const DexFile& dex_file = *dex_cache->GetDexFile();
955 // Get the index of the array class we want to test.
956 const DexFile::TypeId* array_id = dex_file.FindTypeId("[Ljava/lang/Object;");
957 ASSERT_TRUE(array_id != nullptr);
958 dex::TypeIndex array_idx = dex_file.GetIndexForTypeId(*array_id);
959 // Check that the array class wasn't resolved yet.
960 EXPECT_OBJ_PTR_EQ(
961 class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()),
962 ObjPtr<mirror::Class>(nullptr));
963 // Resolve the array class we want to test.
964 ObjPtr<mirror::Class> array_klass
965 = class_linker_->FindClass(soa.Self(), "[Ljava/lang/Object;", class_loader);
966 ASSERT_OBJ_PTR_NE(array_klass, ObjPtr<mirror::Class>(nullptr));
967 // Test that LookupResolvedType() finds the array class.
968 EXPECT_OBJ_PTR_EQ(
969 class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()),
970 array_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(array_idx);
Vladimir Marko6ad2f6d2017-01-18 15:22:59 +0000973 EXPECT_TRUE(dex_cache->GetResolvedType(array_idx) == nullptr);
974 EXPECT_OBJ_PTR_EQ(
975 class_linker_->LookupResolvedType(dex_file, array_idx, dex_cache.Get(), class_loader.Get()),
976 array_klass);
977}
978
Vladimir Marko72ab6842017-01-20 19:32:50 +0000979TEST_F(ClassLinkerTest, LookupResolvedTypeErroneousInit) {
980 ScopedObjectAccess soa(Thread::Current());
981 StackHandleScope<3> hs(soa.Self());
982 Handle<mirror::ClassLoader> class_loader(
983 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("ErroneousInit"))));
984 AssertNonExistentClass("LErroneousInit;");
985 Handle<mirror::Class> klass =
986 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LErroneousInit;", class_loader));
987 ASSERT_OBJ_PTR_NE(klass.Get(), ObjPtr<mirror::Class>(nullptr));
988 dex::TypeIndex type_idx = klass->GetClassDef()->class_idx_;
989 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
990 const DexFile& dex_file = klass->GetDexFile();
991 EXPECT_OBJ_PTR_EQ(
992 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()),
993 klass.Get());
994 // Zero out the resolved type and make sure LookupResolvedType still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000995 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000996 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
997 EXPECT_OBJ_PTR_EQ(
998 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()),
999 klass.Get());
1000 // Force initialization to turn the class erroneous.
1001 bool initialized = class_linker_->EnsureInitialized(soa.Self(),
1002 klass,
1003 /* can_init_fields */ true,
1004 /* can_init_parents */ true);
1005 EXPECT_FALSE(initialized);
1006 EXPECT_TRUE(soa.Self()->IsExceptionPending());
1007 soa.Self()->ClearException();
1008 // Check that the LookupResolvedType() can still find the resolved type.
1009 EXPECT_OBJ_PTR_EQ(
1010 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()),
1011 klass.Get());
1012 // Zero out the resolved type and make sure LookupResolvedType() still finds it.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001013 dex_cache->ClearResolvedType(type_idx);
Vladimir Marko72ab6842017-01-20 19:32:50 +00001014 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
1015 EXPECT_OBJ_PTR_EQ(
1016 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()),
1017 klass.Get());
1018}
1019
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001020TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001022 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
1023 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001024}
1025
Ian Rogersa15e67d2012-02-28 13:51:55 -08001026// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001027// start of the object
1028TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -08001030 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -07001031 mirror::ObjectArray<mirror::String>* array =
1032 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001033 uintptr_t data_offset =
1034 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
1035 0));
1036 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001037 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
1038 } else {
1039 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
1040 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001041}
1042
1043TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001045 StackHandleScope<5> hs(soa.Self());
1046 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -08001047 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001048 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
1049 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
1050
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001051 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -08001052 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001053 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
1054 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
1055
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001056 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -08001057 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001058 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
1059 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
1060
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001061 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -08001062 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001063 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
1064 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
1065
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001066 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -08001067 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -08001068 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
1069 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
1070
1071 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001072}
1073
Elliott Hughes33203b52011-09-20 19:42:01 -07001074TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
1075 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
1076 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001078 ScopedNullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -07001079 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -08001080 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001081 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001082 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001083 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001084 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001085 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001086 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001087 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001088 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001089 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001090 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001091 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001092 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001093 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -08001094 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001095 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -07001096}
1097
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001098TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001100 StackHandleScope<2> hs(soa.Self());
1101 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001102 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001103 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001104 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -08001105 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
1106 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001107 EXPECT_TRUE(MyClass_1 != nullptr);
1108 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001109 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -04001110}
1111
Jesse Wilson7833bd22011-08-09 18:31:44 -04001112TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001114 StackHandleScope<2> hs(soa.Self());
1115 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001116 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001117 Handle<mirror::Class> statics(
1118 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001119 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001120
jeffhaoabcfde32011-09-29 15:05:18 -07001121 // Static final primitives that are initialized by a compile-time constant
1122 // expression resolve to a copy of a constant value from the constant pool.
1123 // So <clinit> should be null.
Andreas Gampe542451c2016-07-26 09:02:02 -07001124 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001125 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001126
1127 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001128
Vladimir Marko19a4d372016-12-08 14:41:46 +00001129 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001130 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001131 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1132 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001133
Vladimir Marko19a4d372016-12-08 14:41:46 +00001134 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001135 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001136 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1137 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001138
Vladimir Marko19a4d372016-12-08 14:41:46 +00001139 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001140 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001141 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1142 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001143
Vladimir Marko19a4d372016-12-08 14:41:46 +00001144 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001145 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001146 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1147 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001148
Vladimir Marko19a4d372016-12-08 14:41:46 +00001149 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001150 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001151 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1152 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001153
Vladimir Marko19a4d372016-12-08 14:41:46 +00001154 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001155 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001156 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1157 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001158
Vladimir Marko19a4d372016-12-08 14:41:46 +00001159 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001160 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001161 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001162 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001163
Vladimir Marko19a4d372016-12-08 14:41:46 +00001164 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics.Get(), "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001165 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001166 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001167 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001168
Vladimir Marko19a4d372016-12-08 14:41:46 +00001169 ArtField* s8 = mirror::Class::FindStaticField(
1170 soa.Self(), statics.Get(), "s8", "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001171 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001172 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Mathieu Chartier3398c782016-09-30 10:27:43 -07001173 mirror::String* str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
1174 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001175
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001176 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1177 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001178 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1179 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1180 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1181 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1182 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1183 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001184 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1185 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001186 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001187}
1188
Brian Carlstrom30b94452011-08-25 21:35:26 -07001189TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001191 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001192 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001193 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001194 Handle<mirror::Class> I(
1195 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1196 Handle<mirror::Class> J(
1197 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1198 Handle<mirror::Class> K(
1199 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1200 Handle<mirror::Class> A(
1201 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1202 Handle<mirror::Class> B(
1203 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1204 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1205 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1206 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1207 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1208 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001209
Ian Rogersd91d6d62013-09-25 20:26:14 -07001210 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Andreas Gampe542451c2016-07-26 09:02:02 -07001211 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1212 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1213 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
1214 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1215 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1216 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
1217 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1218 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1219 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001220 ASSERT_TRUE(Ii != nullptr);
1221 ASSERT_TRUE(Jj1 != nullptr);
1222 ASSERT_TRUE(Jj2 != nullptr);
1223 ASSERT_TRUE(Kj1 != nullptr);
1224 ASSERT_TRUE(Kj2 != nullptr);
1225 ASSERT_TRUE(Kk != nullptr);
1226 ASSERT_TRUE(Ai != nullptr);
1227 ASSERT_TRUE(Aj1 != nullptr);
1228 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001229 EXPECT_NE(Ii, Ai);
1230 EXPECT_NE(Jj1, Aj1);
1231 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001232 EXPECT_EQ(Kj1, Jj1);
1233 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001234 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1235 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1236 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1237 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1238 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1239 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001240
Vladimir Marko19a4d372016-12-08 14:41:46 +00001241 ArtField* Afoo =
1242 mirror::Class::FindStaticField(soa.Self(), A.Get(), "foo", "Ljava/lang/String;");
1243 ArtField* Bfoo =
1244 mirror::Class::FindStaticField(soa.Self(), B.Get(), "foo", "Ljava/lang/String;");
1245 ArtField* Jfoo =
1246 mirror::Class::FindStaticField(soa.Self(), J.Get(), "foo", "Ljava/lang/String;");
1247 ArtField* Kfoo =
1248 mirror::Class::FindStaticField(soa.Self(), K.Get(), "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001249 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001250 EXPECT_EQ(Afoo, Bfoo);
1251 EXPECT_EQ(Afoo, Jfoo);
1252 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001253}
1254
Elliott Hughesf3778f62012-01-26 14:14:35 -08001255TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001256 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001257
jeffhaoabcfde32011-09-29 15:05:18 -07001258 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1259 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001260
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(Thread::Current());
1262 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001263 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001264 StackHandleScope<1> hs(soa.Self());
1265 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001266 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001267 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001268 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
1269 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001270 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001271 ASSERT_TRUE(type_id != nullptr);
Andreas Gampea5b09a62016-11-17 15:21:22 -08001272 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001273 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001274 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001275 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001276 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001277 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001278 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001279}
1280
Mathieu Chartier1e4841e2016-12-15 14:21:04 -08001281TEST_F(ClassLinkerTest, ErroneousClass) {
1282 ScopedObjectAccess soa(Thread::Current());
1283 jobject jclass_loader = LoadMultiDex("ErroneousA", "ErroneousB");
1284 StackHandleScope<1> hs(soa.Self());
1285 Handle<mirror::ClassLoader> class_loader(
1286 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1287 hs.Self()->AssertNoPendingException();
1288 const char* descriptor = "LErroneous;";
1289 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), descriptor, class_loader);
1290 // Erronenous since we are extending final class.
1291 hs.Self()->AssertPendingException();
1292 EXPECT_TRUE(klass == nullptr);
1293 klass = class_linker_->LookupClass(soa.Self(), descriptor, class_loader.Get());
1294 EXPECT_FALSE(klass == nullptr);
1295 EXPECT_TRUE(klass->IsErroneous());
1296 EXPECT_TRUE(klass->GetIfTable() != nullptr);
1297}
1298
Elliott Hughes20cde902011-10-04 17:37:27 -07001299TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001301 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001302
1303 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001304 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001305 EXPECT_FALSE(c->IsFinalizable());
1306
1307 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001308 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001309 EXPECT_FALSE(c->IsFinalizable());
1310
1311 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001312 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001313 EXPECT_FALSE(c->IsFinalizable());
1314
1315 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001316 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001317 EXPECT_TRUE(c->IsFinalizable());
1318
1319 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001320 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001321 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001322 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001323 EXPECT_TRUE(c->IsFinalizable());
1324
1325 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1326 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001327 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001328 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001329 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001330 EXPECT_TRUE(c->IsFinalizable());
1331}
1332
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001333TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001334 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001335 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001336 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001337 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001338 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1339 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001340 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1341 }
1342}
1343
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001344TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1345 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001346 ScopedNullHandle<mirror::ClassLoader> class_loader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001347 mirror::Class* c;
1348
1349 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001350 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001351 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001352
1353 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001354 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001355 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001356
1357 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001358 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001359 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001360
1361 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001362 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001363 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001364}
1365
Mathieu Chartiere401d142015-04-22 13:56:20 -07001366static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001367 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001368 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001369 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001370 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001371 }
1372}
1373
Igor Murashkindf707e42016-02-02 16:56:50 -08001374static void CheckVerificationAttempted(mirror::Class* c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001375 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001376 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001377 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001378 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001379 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001380 }
1381}
1382
1383TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1384 ScopedObjectAccess soa(Thread::Current());
1385
1386 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001387 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001388 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1389 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001390 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001391}
1392
1393TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1394 ScopedObjectAccess soa(Thread::Current());
1395
1396 StackHandleScope<1> hs(soa.Self());
1397
1398 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1399 soa.Self(), "Ljava/lang/SecurityManager;")));
1400 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1401 "core";
1402
Igor Murashkindf707e42016-02-02 16:56:50 -08001403 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001404
Ian Rogers7b078e82014-09-10 14:44:24 -07001405 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001406 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001407}
1408
1409TEST_F(ClassLinkerTest, Preverified_App) {
1410 ScopedObjectAccess soa(Thread::Current());
1411
1412 StackHandleScope<2> hs(soa.Self());
1413 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001414 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001415 Handle<mirror::Class> statics(
1416 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1417
Igor Murashkindf707e42016-02-02 16:56:50 -08001418 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001419
Ian Rogers7b078e82014-09-10 14:44:24 -07001420 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001421 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001422}
1423
Sebastien Hertz6963e442014-11-26 22:11:27 +01001424TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1425 ScopedObjectAccess soa(Thread::Current());
1426
1427 StackHandleScope<3> hs(soa.Self());
1428 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001429 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001430
1431 // java.lang.Object is a bootstrap class.
1432 Handle<mirror::Class> jlo_class(
1433 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001434 ASSERT_TRUE(jlo_class != nullptr);
Sebastien Hertz6963e442014-11-26 22:11:27 +01001435 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1436
1437 // Statics is not a bootstrap class.
1438 Handle<mirror::Class> statics(
1439 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001440 ASSERT_TRUE(statics != nullptr);
Sebastien Hertz6963e442014-11-26 22:11:27 +01001441 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1442}
1443
Mathieu Chartier76172162016-01-26 14:54:06 -08001444// Regression test for b/26799552.
1445TEST_F(ClassLinkerTest, RegisterDexFileName) {
1446 ScopedObjectAccess soa(Thread::Current());
1447 StackHandleScope<2> hs(soa.Self());
1448 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1449 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1450 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001451 ReaderMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001452 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001453 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Andreas Gampefa4333d2017-02-14 11:10:34 -08001454 if (dex_cache != nullptr) {
Mathieu Chartier76172162016-01-26 14:54:06 -08001455 break;
1456 }
1457 }
Andreas Gampefa4333d2017-02-14 11:10:34 -08001458 ASSERT_TRUE(dex_cache != nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001459 }
1460 // Make a copy of the dex cache and change the name.
1461 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1462 const uint16_t data[] = { 0x20AC, 0x20A1 };
1463 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1464 arraysize(data),
1465 data)));
1466 dex_cache->SetLocation(location.Get());
1467 const DexFile* old_dex_file = dex_cache->GetDexFile();
1468
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001469 std::unique_ptr<DexFile> dex_file(new DexFile(old_dex_file->Begin(),
1470 old_dex_file->Size(),
1471 location->ToModifiedUtf8(),
1472 0u,
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001473 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001474 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001475 WriterMutexLock mu(soa.Self(), *Locks::dex_lock_);
Mathieu Chartier76172162016-01-26 14:54:06 -08001476 // Check that inserting with a UTF16 name works.
Vladimir Markocd556b02017-02-03 11:47:34 +00001477 class_linker->RegisterDexFileLocked(*dex_file, dex_cache.Get(), /* class_loader */ nullptr);
Mathieu Chartier76172162016-01-26 14:54:06 -08001478 }
1479}
1480
Narayan Kamath25352fc2016-08-03 12:46:58 +01001481TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1482 ScopedObjectAccess soa(Thread::Current());
1483
1484 StackHandleScope<7> hs(soa.Self());
1485
1486 Handle<mirror::ClassLoader> class_loader(
1487 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1488 Handle<mirror::Class> method_types(
1489 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1490 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1491
1492 ArtMethod* method1 = method_types->FindVirtualMethod("method1",
1493 "(Ljava/lang/String;)Ljava/lang/String;",
1494 kRuntimePointerSize);
1495
1496 const DexFile& dex_file = *(method1->GetDexFile());
1497 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
1498 class_linker_->FindDexCache(Thread::Current(), dex_file));
1499
1500 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
1501
1502 // This is the MethodType corresponding to the prototype of
1503 // String MethodTypes# method1(String).
1504 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001505 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001506 Handle<mirror::MethodType> method1_type = hs.NewHandle(
1507 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1508
1509 // Assert that the method type was resolved successfully.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001510 ASSERT_TRUE(method1_type != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +01001511
1512 // Assert that the return type and the method arguments are as we expect.
1513 Handle<mirror::Class> string_class(
1514 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader)));
1515 ASSERT_EQ(string_class.Get(), method1_type->GetRType());
1516 ASSERT_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
1517
1518 // Resolve the method type again and assert that we get back the same value.
1519 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
1520 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1521 ASSERT_EQ(method1_type.Get(), method1_type2.Get());
1522
1523 // Resolve the MethodType associated with a different method signature
1524 // and assert it's different.
1525 ArtMethod* method2 = method_types->FindVirtualMethod(
1526 "method2",
1527 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1528 kRuntimePointerSize);
1529 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
1530 Handle<mirror::MethodType> method2_type = hs.NewHandle(
1531 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
1532
1533 ASSERT_TRUE(method1_type.Get() != method2_type.Get());
1534}
1535
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001536} // namespace art