blob: 44590ba327c9b6abb4bca673b35c5d43dc8a40f7 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "dex_file.h"
Alex Light705ad492015-09-21 11:36:30 -070028#include "experimental_flags.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/heap.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070031#include "mirror/accessible_object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070033#include "mirror/class_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/dex_cache.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010035#include "mirror/emulated_stack_frame.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010036#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070037#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010038#include "mirror/method_type.h"
39#include "mirror/method_handle_impl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
42#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070043#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070045#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070047#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070048#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
50namespace art {
51
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080052class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070053 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070056 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070057 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070058 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000059 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070060 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080061 mirror::Class* exception_class =
62 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070063 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070064 }
65
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080068 Thread* self = Thread::Current();
69 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070070 }
71
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070074 ASSERT_TRUE(primitive != nullptr);
75 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070077 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070078 std::string temp;
79 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070080 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070082 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070083 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070084 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070085 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070086 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070087 EXPECT_TRUE(primitive->IsVerified());
88 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070089 EXPECT_FALSE(primitive->IsArrayInstance());
90 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070091 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 EXPECT_FALSE(primitive->IsInterface());
93 EXPECT_TRUE(primitive->IsPublic());
94 EXPECT_TRUE(primitive->IsFinal());
95 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070096 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070097 EXPECT_EQ(0U, primitive->NumDirectMethods());
98 EXPECT_EQ(0U, primitive->NumVirtualMethods());
99 EXPECT_EQ(0U, primitive->NumInstanceFields());
100 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700101 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700102 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700103 EXPECT_EQ(0, primitive->GetIfTableCount());
Mathieu Chartier6beced42016-11-15 15:51:31 -0800104 EXPECT_TRUE(primitive->GetIfTable() != nullptr);
105 EXPECT_EQ(primitive->GetIfTable()->Count(), 0u);
Elliott Hughes00626c22013-06-14 15:04:14 -0700106 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700107 }
108
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000109 void AssertObjectClass(mirror::Class* JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000111 ASSERT_TRUE(JavaLangObject != nullptr);
112 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
113 ASSERT_EQ(JavaLangObject->GetClass(),
114 JavaLangObject->GetClass()->GetClass());
115 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
116 std::string temp;
117 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
118 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
119 EXPECT_FALSE(JavaLangObject->HasSuperClass());
120 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
121 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
122 EXPECT_FALSE(JavaLangObject->IsErroneous());
123 EXPECT_TRUE(JavaLangObject->IsLoaded());
124 EXPECT_TRUE(JavaLangObject->IsResolved());
125 EXPECT_TRUE(JavaLangObject->IsVerified());
126 EXPECT_TRUE(JavaLangObject->IsInitialized());
127 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
128 EXPECT_FALSE(JavaLangObject->IsArrayClass());
129 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
130 EXPECT_FALSE(JavaLangObject->IsInterface());
131 EXPECT_TRUE(JavaLangObject->IsPublic());
132 EXPECT_FALSE(JavaLangObject->IsFinal());
133 EXPECT_FALSE(JavaLangObject->IsPrimitive());
134 EXPECT_FALSE(JavaLangObject->IsSynthetic());
135 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
136 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
137 if (!kUseBrooksReadBarrier) {
138 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
139 } else {
140 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
141 }
142 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
143 "shadow$_klass_");
144 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
145 "shadow$_monitor_");
146 if (kUseBrooksReadBarrier) {
147 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
148 "shadow$_x_rb_ptr_");
149 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
150 "shadow$_x_xpadding_");
151 }
152
153 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
154 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
155
Andreas Gampe542451c2016-07-26 09:02:02 -0700156 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000157 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
158 ImTable* imt = JavaLangObject->GetImt(pointer_size);
159 ASSERT_NE(nullptr, imt);
160 for (size_t i = 0; i < ImTable::kSize; ++i) {
161 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
162 }
163 }
164
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 void AssertArrayClass(const std::string& array_descriptor,
166 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700167 mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700168 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800169 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 StackHandleScope<2> hs(self);
171 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
172 Handle<mirror::Class> array(
173 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700174 std::string temp;
175 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700176 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700177 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700178 AssertArrayClass(array_descriptor, array);
179 }
180
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700181 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700183 ASSERT_TRUE(array.Get() != nullptr);
184 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700185 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700186 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700187 std::string temp;
188 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700189 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800190 Thread* self = Thread::Current();
191 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700192 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700193 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700194 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700196 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700197 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700198 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700199 EXPECT_TRUE(array->IsVerified());
200 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700201 EXPECT_FALSE(array->IsArrayInstance());
202 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700203 EXPECT_FALSE(array->IsInterface());
204 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
205 EXPECT_TRUE(array->IsFinal());
206 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700207 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700208 EXPECT_EQ(0U, array->NumDirectMethods());
209 EXPECT_EQ(0U, array->NumVirtualMethods());
210 EXPECT_EQ(0U, array->NumInstanceFields());
211 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700212 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000213 EXPECT_TRUE(array->ShouldHaveImt());
214 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700215 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700216 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700217 ObjPtr<mirror::Class> direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700218 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700219 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700220 ObjPtr<mirror::Class> direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700222 ObjPtr<mirror::Class> array_ptr = array->GetComponentType();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700223 EXPECT_OBJ_PTR_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000224
Andreas Gampe542451c2016-07-26 09:02:02 -0700225 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000226 mirror::Class* JavaLangObject =
227 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
228 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
229 // IMT of a array class should be shared with the IMT of the java.lag.Object
230 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700231 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700234 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700236 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700237 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700238
Andreas Gampe542451c2016-07-26 09:02:02 -0700239 EXPECT_TRUE(method->HasDexCacheResolvedMethods(kRuntimePointerSize));
240 EXPECT_TRUE(method->HasDexCacheResolvedTypes(kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700241 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100242 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700243 kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700244 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
Vladimir Marko05792b92015-08-03 11:56:49 +0100245 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700246 kRuntimePointerSize));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700247 }
248
Mathieu Chartier3398c782016-09-30 10:27:43 -0700249 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700251 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700252 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700253 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700254 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700255 }
256
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700257 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700259 std::string temp;
260 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700262 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700263 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700265 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700266 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700267 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700268 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700269 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700270 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700271 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700274 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800276 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
277 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700278 std::string temp2;
279 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
280 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700281 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800283 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700284 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800285 EXPECT_TRUE(m.IsStatic());
286 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700287 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700288 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700289 if (!klass->IsSynthetic()) {
290 EXPECT_NE(0U, klass->NumDirectMethods());
291 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700292 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700293 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800294 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700295 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700296 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700297 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700298 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700299 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700300 } else {
Alex Light705ad492015-09-21 11:36:30 -0700301 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700302 }
303 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700304 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700305 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700306 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700307 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700308 }
309 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 EXPECT_FALSE(klass->IsAbstract());
311 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700312 }
313 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700314 EXPECT_FALSE(klass->IsFinal());
315 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700316 }
317
318 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700319 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700320
Andreas Gampe542451c2016-07-26 09:02:02 -0700321 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 AssertMethod(&method);
323 EXPECT_TRUE(method.IsDirect());
324 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700325 }
326
Andreas Gampe542451c2016-07-26 09:02:02 -0700327 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 AssertMethod(&method);
329 EXPECT_FALSE(method.IsDirect());
Alex Lighte64300b2015-12-15 15:02:47 -0800330 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
331 }
332
Andreas Gampe542451c2016-07-26 09:02:02 -0700333 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800334 AssertMethod(&method);
335 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800336 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800337 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
David Sehr709b0702016-10-13 09:12:37 -0700338 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Alex Lighte64300b2015-12-15 15:02:47 -0800339 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
David Sehr709b0702016-10-13 09:12:37 -0700340 << "declaring class: " << method.GetDeclaringClass()->PrettyClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700341 }
342
343 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700344 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700345 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700346 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700347 }
348
349 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700350 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700352 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700353 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700354
Vladimir Marko76649e82014-11-10 18:32:59 +0000355 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700356 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000357 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
358 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
359 klass->NumReferenceInstanceFields() *
360 sizeof(mirror::HeapReference<mirror::Object>));
361 MemberOffset current_ref_offset = start_ref_offset;
362 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700363 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700364 ObjPtr<mirror::Class> field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700365 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000366 if (!field->IsPrimitiveType()) {
367 ASSERT_TRUE(!field_type->IsPrimitive());
368 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
369 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
370 // While Reference.referent is not primitive, the ClassLinker
371 // treats it as such so that the garbage collector won't scan it.
David Sehr709b0702016-10-13 09:12:37 -0700372 EXPECT_EQ(field->PrettyField(), "java.lang.Object java.lang.ref.Reference.referent");
Vladimir Marko76649e82014-11-10 18:32:59 +0000373 } else {
374 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
375 sizeof(mirror::HeapReference<mirror::Object>));
376 }
377 } else {
378 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
379 // Shuffled before references.
380 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
381 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
382 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700383 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700384 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000385 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700386
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700387 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700388 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700389 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700390 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
391 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700392 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700393 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
394 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
395 // The reference instance offsets have a bit set for each reference offset.
396 // +1 for Object's class.
397 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
398 total_num_reference_instance_fields);
399 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700400 }
401
Brian Carlstromea46f952013-07-30 01:26:50 -0700402 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700403 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700404 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800405 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700406 StackHandleScope<1> hs(self);
407 Handle<mirror::Class> klass(
408 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
409 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700410 std::string temp;
411 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700412 EXPECT_EQ(class_loader, klass->GetClassLoader());
413 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700415 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700416 AssertArrayClass(descriptor, klass);
417 } else {
418 AssertClass(descriptor, klass);
419 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700420 }
421
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800422 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700423 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700424 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800425 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
426 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
427 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700428 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700429 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700430 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800431 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
432 const DexFile::TypeId& type_id = dex.GetTypeId(i);
433 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700434 AssertDexFileClass(class_loader, descriptor);
435 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700436 TestRootVisitor visitor;
437 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800438 // Verify the dex cache has resolution methods in all resolved method slots
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700439 mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100441 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
442 EXPECT_TRUE(
Andreas Gampe542451c2016-07-26 09:02:02 -0700443 mirror::DexCache::GetElementPtrSize(resolved_methods, i, kRuntimePointerSize) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800445 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700446 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700447
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700448 class TestRootVisitor : public SingleRootVisitor {
449 public:
450 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
451 EXPECT_TRUE(root != nullptr);
452 }
453 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700455
Narayan Kamath25352fc2016-08-03 12:46:58 +0100456class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
457 protected:
458 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
459 CommonRuntimeTest::SetUpRuntimeOptions(options);
460 options->push_back(std::make_pair("-Xexperimental:method-handles", nullptr));
461 }
462};
463
Brian Carlstrom693267a2011-09-06 09:25:34 -0700464struct CheckOffset {
465 size_t cpp_offset;
466 const char* java_name;
467 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
468};
469
Elliott Hughes80609252011-09-23 17:24:51 -0700470template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800472 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
473 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700475 std::string class_descriptor;
476 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700477
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700478 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800479 Thread* self = Thread::Current();
480 mirror::Class* klass =
481 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700482 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700483
484 bool error = false;
485
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 // Classes have a different size due to padding field. Strings are variable length.
487 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700488 // Currently only required for AccessibleObject since of the padding fields. The class linker
489 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
490 // The RoundUp is to get around this case.
491 static constexpr size_t kPackAlignment = 4;
492 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
493 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700494 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800495 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700496 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700497 << " Java=" << expected_size
498 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700499 error = true;
500 }
501 }
502
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700504 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800505 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700506 << " class=" << class_descriptor
507 << " Java=" << num_fields
508 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700509 error = true;
510 }
511
Brian Carlstromdbc05252011-09-09 01:59:59 -0700512 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700513 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700514 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800515 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700516 error = true;
517 }
518 }
519 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700520 for (size_t i = 0; i < offsets.size(); i++) {
521 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700522 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700523 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800525 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700526 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800527 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700528 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700530 << " CheckOffsets=" << offset.java_name;
531 }
532 }
533
Brian Carlstromdbc05252011-09-09 01:59:59 -0700534 for (size_t i = 0; i < offsets.size(); i++) {
535 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700536 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
538 error = true;
539 }
540 }
541 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700542 for (size_t i = 0; i < offsets.size(); i++) {
543 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700544 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800546 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700547 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800548 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
550 }
551 }
552
553 return !error;
554 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700556 void addOffset(size_t offset, const char* name) {
557 offsets.push_back(CheckOffset(offset, name));
558 }
559
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700560 private:
561 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700562};
563
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700564// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
565// is first since if it is failing, others are unlikely to succeed.
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct ObjectOffsets : public CheckOffsets<mirror::Object> {
568 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700569 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
570 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700571#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700572 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
573 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800574#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700575 };
576};
577
Brian Carlstromea46f952013-07-30 01:26:50 -0700578struct ClassOffsets : public CheckOffsets<mirror::Class> {
579 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700581 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700582 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
584 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
585 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800586 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700587 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
589 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
590 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Alex Lightd6251582016-10-31 11:12:30 -0700591 addOffset(OFFSETOF_MEMBER(mirror::Class, ext_data_), "extData");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700592 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
593 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800594 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700595 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700596 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
597 "numReferenceInstanceFields");
598 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
599 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700600 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700601 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
602 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700603 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
604 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
605 "referenceInstanceOffsets");
606 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
607 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
608 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Alex Lighte64300b2015-12-15 15:02:47 -0800609 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700610 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700611 };
612};
613
Alex Lightd6251582016-10-31 11:12:30 -0700614struct ClassExtOffsets : public CheckOffsets<mirror::ClassExt> {
615 ClassExtOffsets() : CheckOffsets<mirror::ClassExt>(false, "Ldalvik/system/ClassExt;") {
616 addOffset(OFFSETOF_MEMBER(mirror::ClassExt, verify_error_), "verifyError");
617 }
618};
619
Brian Carlstromea46f952013-07-30 01:26:50 -0700620struct StringOffsets : public CheckOffsets<mirror::String> {
621 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700622 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000623 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700624 };
625};
626
Brian Carlstromea46f952013-07-30 01:26:50 -0700627struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
628 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000629 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700630 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
631 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700632 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
633 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700634 };
635};
636
Brian Carlstromea46f952013-07-30 01:26:50 -0700637struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700638 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
639 false, "Ljava/lang/StackTraceElement;") {
640 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
641 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
642 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
643 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700644 };
645};
646
Brian Carlstromea46f952013-07-30 01:26:50 -0700647struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
648 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700649 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700650 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700651 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
652 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
653 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700654 };
655};
656
Brian Carlstromea46f952013-07-30 01:26:50 -0700657struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
658 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700659 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400660 };
661};
662
Brian Carlstromea46f952013-07-30 01:26:50 -0700663struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
664 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700665 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
666 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
667 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Vladimir Marko05792b92015-08-03 11:56:49 +0100668 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100669 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100670 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
671 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
672 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700673 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100674 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700675 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
676 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
677 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700678 };
679};
680
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700681struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
682 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700683 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
684 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
685 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
686 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700687 };
688};
689
690struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700691 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
692 false, "Ljava/lang/ref/FinalizerReference;") {
693 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
694 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
695 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700696 };
697};
698
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700699struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700700 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
701 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000702 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700703 };
704};
705
706struct FieldOffsets : public CheckOffsets<mirror::Field> {
707 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700708 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
709 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
710 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
711 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
712 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
713 };
714};
715
Neil Fuller16b21cd2016-08-12 09:37:02 +0100716struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
717 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
718 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100719 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
720 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
721 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
722 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
723 "declaringClassOfOverriddenMethod");
724 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100725 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
726 "hasRealParameterData");
727 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Neil Fuller16b21cd2016-08-12 09:37:02 +0100728 };
729};
730
Narayan Kamathafa48272016-08-03 12:46:58 +0100731struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
732 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
733 false, "Ljava/lang/invoke/MethodType;") {
734 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
735 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
736 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
737 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
738 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
739 }
740};
741
742struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
743 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
744 false, "Ljava/lang/invoke/MethodHandle;") {
745 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, art_field_or_method_), "artFieldOrMethod");
Narayan Kamathafa48272016-08-03 12:46:58 +0100746 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, handle_kind_), "handleKind");
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000747 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, nominal_type_), "nominalType");
Narayan Kamathafa48272016-08-03 12:46:58 +0100748 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, method_type_), "type");
749 }
750};
751
Narayan Kamath000e1882016-10-24 17:14:25 +0100752struct EmulatedStackFrameOffsets : public CheckOffsets<mirror::EmulatedStackFrame> {
753 EmulatedStackFrameOffsets() : CheckOffsets<mirror::EmulatedStackFrame>(
754 false, "Ldalvik/system/EmulatedStackFrame;") {
755 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, references_), "references");
756 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, stack_frame_), "stackFrame");
757 addOffset(OFFSETOF_MEMBER(mirror::EmulatedStackFrame, type_), "type");
758 }
759};
760
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400761// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400762// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700763// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400764TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700765 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700766 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700767 EXPECT_TRUE(ClassOffsets().Check());
Alex Lightd6251582016-10-31 11:12:30 -0700768 EXPECT_TRUE(ClassExtOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700769 EXPECT_TRUE(StringOffsets().Check());
770 EXPECT_TRUE(ThrowableOffsets().Check());
771 EXPECT_TRUE(StackTraceElementOffsets().Check());
772 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400773 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700774 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700775 EXPECT_TRUE(ReferenceOffsets().Check());
776 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700777 EXPECT_TRUE(AccessibleObjectOffsets().Check());
778 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100779 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100780 EXPECT_TRUE(MethodTypeOffsets().Check());
781 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Narayan Kamath000e1882016-10-24 17:14:25 +0100782 EXPECT_TRUE(EmulatedStackFrameOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700783}
784
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700785TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700787 AssertNonExistentClass("NoSuchClass;");
788 AssertNonExistentClass("LNoSuchClass;");
789}
790
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700791TEST_F(ClassLinkerTest, GetDexFiles) {
792 ScopedObjectAccess soa(Thread::Current());
793
794 jobject jclass_loader = LoadDex("Nested");
795 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
796 ASSERT_EQ(dex_files.size(), 1U);
797 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
798
799 jobject jclass_loader2 = LoadDex("MultiDex");
800 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
801 ASSERT_EQ(dex_files2.size(), 2U);
802 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
803}
804
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700805TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700807 StackHandleScope<1> hs(soa.Self());
808 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700809 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700810
Ian Rogers98379392014-02-24 16:53:16 -0800811 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700812 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700813 EXPECT_EQ(0U, outer->NumVirtualMethods());
814 EXPECT_EQ(1U, outer->NumDirectMethods());
815
Ian Rogers98379392014-02-24 16:53:16 -0800816 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700817 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700818 EXPECT_EQ(0U, inner->NumVirtualMethods());
819 EXPECT_EQ(1U, inner->NumDirectMethods());
820}
821
822TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700824 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800825 for (int ch = 1; ch < 256; ++ch) {
826 std::string descriptor;
827 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700828 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700829 AssertNonExistentClass(descriptor);
830 } else {
831 AssertPrimitiveClass(descriptor);
832 }
833 }
834}
835
836TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800838 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000839 AssertObjectClass(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700840
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700841 StackHandleScope<1> hs(soa.Self());
842 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700843 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700844 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800845 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700846 ASSERT_TRUE(MyClass != nullptr);
847 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700848 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
849 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000850 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700851 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
853 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700854 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700855 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700857 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700859 EXPECT_FALSE(MyClass->IsVerified());
860 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861 EXPECT_FALSE(MyClass->IsArrayInstance());
862 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700863 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700864 EXPECT_FALSE(MyClass->IsInterface());
865 EXPECT_FALSE(MyClass->IsPublic());
866 EXPECT_FALSE(MyClass->IsFinal());
867 EXPECT_FALSE(MyClass->IsPrimitive());
868 EXPECT_FALSE(MyClass->IsSynthetic());
869 EXPECT_EQ(1U, MyClass->NumDirectMethods());
870 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
871 EXPECT_EQ(0U, MyClass->NumInstanceFields());
872 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700873 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700874
875 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
876
877 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700878 AssertArrayClass("[C", "C", nullptr);
879 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700880 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700881 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700882 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700883 // or not available at all
884 AssertNonExistentClass("[[[[LNonExistentClass;");
885}
886
Mathieu Chartierb8901302016-09-30 10:27:43 -0700887TEST_F(ClassLinkerTest, LookupResolvedType) {
888 ScopedObjectAccess soa(Thread::Current());
889 StackHandleScope<1> hs(soa.Self());
890 Handle<mirror::ClassLoader> class_loader(
891 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
892 AssertNonExistentClass("LMyClass;");
893 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
894 uint32_t type_idx = klass->GetClassDef()->class_idx_;
895 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
896 const DexFile& dex_file = klass->GetDexFile();
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700897 EXPECT_OBJ_PTR_EQ(dex_cache->GetResolvedType(type_idx), klass);
Mathieu Chartierb8901302016-09-30 10:27:43 -0700898 EXPECT_OBJ_PTR_EQ(
899 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
900 klass);
901 // Zero out the resolved type and make sure LookupResolvedType still finds it.
902 dex_cache->SetResolvedType(type_idx, nullptr);
903 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
904 EXPECT_OBJ_PTR_EQ(
905 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
906 klass);
907}
908
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700909TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800911 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
912 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700913}
914
Ian Rogersa15e67d2012-02-28 13:51:55 -0800915// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400916// start of the object
917TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800919 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700920 mirror::ObjectArray<mirror::String>* array =
921 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800922 uintptr_t data_offset =
923 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
924 0));
925 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800926 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
927 } else {
928 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
929 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400930}
931
932TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700934 StackHandleScope<5> hs(soa.Self());
935 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800936 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800937 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
938 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
939
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700940 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800941 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800942 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
943 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
944
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700945 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800946 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800947 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
948 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
949
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700950 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800951 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800952 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
953 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
954
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700955 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800956 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800957 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
958 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
959
960 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400961}
962
Elliott Hughes33203b52011-09-20 19:42:01 -0700963TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
964 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
965 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800967 ScopedNullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700968 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800969 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700970 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800971 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700972 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800973 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700974 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800975 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700976 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800977 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700978 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800979 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700980 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800981 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700982 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800983 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700984 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700985}
986
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700987TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700989 StackHandleScope<2> hs(soa.Self());
990 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700991 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700992 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700993 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800994 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
995 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700996 EXPECT_TRUE(MyClass_1 != nullptr);
997 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700998 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400999}
1000
Jesse Wilson7833bd22011-08-09 18:31:44 -04001001TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001003 StackHandleScope<2> hs(soa.Self());
1004 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001005 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001006 Handle<mirror::Class> statics(
1007 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -07001008 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001009
jeffhaoabcfde32011-09-29 15:05:18 -07001010 // Static final primitives that are initialized by a compile-time constant
1011 // expression resolve to a copy of a constant value from the constant pool.
1012 // So <clinit> should be null.
Andreas Gampe542451c2016-07-26 09:02:02 -07001013 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001014 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -07001015
1016 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -04001017
Mathieu Chartierc7853442015-03-27 14:35:38 -07001018 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001019 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001020 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1021 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001022
Mathieu Chartierc7853442015-03-27 14:35:38 -07001023 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001024 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001025 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1026 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001027
Mathieu Chartierc7853442015-03-27 14:35:38 -07001028 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001029 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001030 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1031 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001032
Mathieu Chartierc7853442015-03-27 14:35:38 -07001033 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001034 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001035 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1036 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001037
Mathieu Chartierc7853442015-03-27 14:35:38 -07001038 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001039 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001040 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1041 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001042
Mathieu Chartierc7853442015-03-27 14:35:38 -07001043 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001044 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001045 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1046 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001047
Mathieu Chartierc7853442015-03-27 14:35:38 -07001048 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001049 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001050 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001051 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001052
Mathieu Chartierc7853442015-03-27 14:35:38 -07001053 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001054 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001055 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001056 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001057
Mathieu Chartierc7853442015-03-27 14:35:38 -07001058 ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
Mathieu Chartierf8322842014-05-16 10:59:25 -07001059 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001060 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001061 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Mathieu Chartier3398c782016-09-30 10:27:43 -07001062 mirror::String* str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
1063 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001064
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001065 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1066 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001067 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1068 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1069 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1070 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1071 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1072 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001073 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1074 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001075 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001076}
1077
Brian Carlstrom30b94452011-08-25 21:35:26 -07001078TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001080 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001081 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001082 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001083 Handle<mirror::Class> I(
1084 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1085 Handle<mirror::Class> J(
1086 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1087 Handle<mirror::Class> K(
1088 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1089 Handle<mirror::Class> A(
1090 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1091 Handle<mirror::Class> B(
1092 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1093 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1094 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1095 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1096 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1097 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098
Ian Rogersd91d6d62013-09-25 20:26:14 -07001099 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Andreas Gampe542451c2016-07-26 09:02:02 -07001100 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1101 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1102 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
1103 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1104 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1105 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
1106 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1107 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1108 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001109 ASSERT_TRUE(Ii != nullptr);
1110 ASSERT_TRUE(Jj1 != nullptr);
1111 ASSERT_TRUE(Jj2 != nullptr);
1112 ASSERT_TRUE(Kj1 != nullptr);
1113 ASSERT_TRUE(Kj2 != nullptr);
1114 ASSERT_TRUE(Kk != nullptr);
1115 ASSERT_TRUE(Ai != nullptr);
1116 ASSERT_TRUE(Aj1 != nullptr);
1117 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001118 EXPECT_NE(Ii, Ai);
1119 EXPECT_NE(Jj1, Aj1);
1120 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001121 EXPECT_EQ(Kj1, Jj1);
1122 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001123 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1124 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1125 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1126 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1127 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1128 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001129
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130 ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1131 ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1132 ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1133 ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001134 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001135 EXPECT_EQ(Afoo, Bfoo);
1136 EXPECT_EQ(Afoo, Jfoo);
1137 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001138}
1139
Elliott Hughesf3778f62012-01-26 14:14:35 -08001140TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001141 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001142
jeffhaoabcfde32011-09-29 15:05:18 -07001143 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1144 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001145
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 ScopedObjectAccess soa(Thread::Current());
1147 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001148 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001149 StackHandleScope<1> hs(soa.Self());
1150 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001151 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001152 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001153 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
1154 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001155 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001156 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001157 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001158 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001159 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001160 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001161 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001162 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001163 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001164}
1165
Elliott Hughes20cde902011-10-04 17:37:27 -07001166TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001168 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001169
1170 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001171 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001172 EXPECT_FALSE(c->IsFinalizable());
1173
1174 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001175 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001176 EXPECT_FALSE(c->IsFinalizable());
1177
1178 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001179 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001180 EXPECT_FALSE(c->IsFinalizable());
1181
1182 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001183 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001184 EXPECT_TRUE(c->IsFinalizable());
1185
1186 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001187 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001188 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001189 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001190 EXPECT_TRUE(c->IsFinalizable());
1191
1192 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1193 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001194 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001195 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001196 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001197 EXPECT_TRUE(c->IsFinalizable());
1198}
1199
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001200TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001202 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001203 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001204 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001205 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1206 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001207 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1208 }
1209}
1210
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001211TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1212 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001213 ScopedNullHandle<mirror::ClassLoader> class_loader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001214 mirror::Class* c;
1215
1216 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001217 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001218 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001219
1220 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001221 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001222 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001223
1224 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001226 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001227
1228 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001229 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001230 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001231}
1232
Mathieu Chartiere401d142015-04-22 13:56:20 -07001233static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001234 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001235 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001236 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
David Sehr709b0702016-10-13 09:12:37 -07001237 << method->PrettyMethod(true);
Andreas Gampe48498592014-09-10 19:48:05 -07001238 }
1239}
1240
Igor Murashkindf707e42016-02-02 16:56:50 -08001241static void CheckVerificationAttempted(mirror::Class* c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001242 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001243 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
David Sehr709b0702016-10-13 09:12:37 -07001244 << "Class " << mirror::Class::PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001245 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001246 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001247 }
1248}
1249
1250TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1251 ScopedObjectAccess soa(Thread::Current());
1252
1253 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001254 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001255 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1256 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001257 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001258}
1259
1260TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1261 ScopedObjectAccess soa(Thread::Current());
1262
1263 StackHandleScope<1> hs(soa.Self());
1264
1265 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1266 soa.Self(), "Ljava/lang/SecurityManager;")));
1267 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1268 "core";
1269
Igor Murashkindf707e42016-02-02 16:56:50 -08001270 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001271
Ian Rogers7b078e82014-09-10 14:44:24 -07001272 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001273 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001274}
1275
1276TEST_F(ClassLinkerTest, Preverified_App) {
1277 ScopedObjectAccess soa(Thread::Current());
1278
1279 StackHandleScope<2> hs(soa.Self());
1280 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001281 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001282 Handle<mirror::Class> statics(
1283 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1284
Igor Murashkindf707e42016-02-02 16:56:50 -08001285 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001286
Ian Rogers7b078e82014-09-10 14:44:24 -07001287 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001288 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001289}
1290
Sebastien Hertz6963e442014-11-26 22:11:27 +01001291TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1292 ScopedObjectAccess soa(Thread::Current());
1293
1294 StackHandleScope<3> hs(soa.Self());
1295 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001296 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001297
1298 // java.lang.Object is a bootstrap class.
1299 Handle<mirror::Class> jlo_class(
1300 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1301 ASSERT_TRUE(jlo_class.Get() != nullptr);
1302 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1303
1304 // Statics is not a bootstrap class.
1305 Handle<mirror::Class> statics(
1306 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1307 ASSERT_TRUE(statics.Get() != nullptr);
1308 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1309}
1310
Mathieu Chartier76172162016-01-26 14:54:06 -08001311// Regression test for b/26799552.
1312TEST_F(ClassLinkerTest, RegisterDexFileName) {
1313 ScopedObjectAccess soa(Thread::Current());
1314 StackHandleScope<2> hs(soa.Self());
1315 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1316 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1317 {
1318 ReaderMutexLock mu(soa.Self(), *class_linker->DexLock());
1319 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001320 dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache());
Mathieu Chartier76172162016-01-26 14:54:06 -08001321 if (dex_cache.Get() != nullptr) {
1322 break;
1323 }
1324 }
1325 ASSERT_TRUE(dex_cache.Get() != nullptr);
1326 }
1327 // Make a copy of the dex cache and change the name.
1328 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1329 const uint16_t data[] = { 0x20AC, 0x20A1 };
1330 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1331 arraysize(data),
1332 data)));
1333 dex_cache->SetLocation(location.Get());
1334 const DexFile* old_dex_file = dex_cache->GetDexFile();
1335
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001336 std::unique_ptr<DexFile> dex_file(new DexFile(old_dex_file->Begin(),
1337 old_dex_file->Size(),
1338 location->ToModifiedUtf8(),
1339 0u,
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001340 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001341 {
1342 WriterMutexLock mu(soa.Self(), *class_linker->DexLock());
1343 // Check that inserting with a UTF16 name works.
1344 class_linker->RegisterDexFileLocked(*dex_file, dex_cache);
1345 }
1346}
1347
Narayan Kamath25352fc2016-08-03 12:46:58 +01001348TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1349 ScopedObjectAccess soa(Thread::Current());
1350
1351 StackHandleScope<7> hs(soa.Self());
1352
1353 Handle<mirror::ClassLoader> class_loader(
1354 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1355 Handle<mirror::Class> method_types(
1356 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1357 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1358
1359 ArtMethod* method1 = method_types->FindVirtualMethod("method1",
1360 "(Ljava/lang/String;)Ljava/lang/String;",
1361 kRuntimePointerSize);
1362
1363 const DexFile& dex_file = *(method1->GetDexFile());
1364 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
1365 class_linker_->FindDexCache(Thread::Current(), dex_file));
1366
1367 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
1368
1369 // This is the MethodType corresponding to the prototype of
1370 // String MethodTypes# method1(String).
1371 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001372 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001373 Handle<mirror::MethodType> method1_type = hs.NewHandle(
1374 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1375
1376 // Assert that the method type was resolved successfully.
1377 ASSERT_TRUE(method1_type.Get() != nullptr);
1378
1379 // Assert that the return type and the method arguments are as we expect.
1380 Handle<mirror::Class> string_class(
1381 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader)));
1382 ASSERT_EQ(string_class.Get(), method1_type->GetRType());
1383 ASSERT_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
1384
1385 // Resolve the method type again and assert that we get back the same value.
1386 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
1387 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1388 ASSERT_EQ(method1_type.Get(), method1_type2.Get());
1389
1390 // Resolve the MethodType associated with a different method signature
1391 // and assert it's different.
1392 ArtMethod* method2 = method_types->FindVirtualMethod(
1393 "method2",
1394 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1395 kRuntimePointerSize);
1396 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
1397 Handle<mirror::MethodType> method2_type = hs.NewHandle(
1398 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
1399
1400 ASSERT_TRUE(method1_type.Get() != method2_type.Get());
1401}
1402
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001403} // namespace art