blob: 4a926e761973b3cdf8e41b3e087221a7b39b9730 [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"
33#include "mirror/dex_cache.h"
Neil Fuller16b21cd2016-08-12 09:37:02 +010034#include "mirror/executable.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070035#include "mirror/field.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010036#include "mirror/method_type.h"
37#include "mirror/method_handle_impl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
40#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070041#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070043#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070045#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070046#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
48namespace art {
49
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080050class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070051 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 void AssertNonExistentClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070053 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070054 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070055 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070056 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000057 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070058 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080059 mirror::Class* exception_class =
60 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070061 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070062 }
63
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 void AssertPrimitiveClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080066 Thread* self = Thread::Current();
67 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070068 }
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070072 ASSERT_TRUE(primitive != nullptr);
73 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070075 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070076 std::string temp;
77 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070078 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070079 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070080 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070081 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070082 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070083 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070084 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070085 EXPECT_TRUE(primitive->IsVerified());
86 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070087 EXPECT_FALSE(primitive->IsArrayInstance());
88 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070089 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070090 EXPECT_FALSE(primitive->IsInterface());
91 EXPECT_TRUE(primitive->IsPublic());
92 EXPECT_TRUE(primitive->IsFinal());
93 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070094 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070095 EXPECT_EQ(0U, primitive->NumDirectMethods());
96 EXPECT_EQ(0U, primitive->NumVirtualMethods());
97 EXPECT_EQ(0U, primitive->NumInstanceFields());
98 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070099 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700100 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700101 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700102 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -0700103 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104 }
105
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000106 void AssertObjectClass(mirror::Class* JavaLangObject)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000108 ASSERT_TRUE(JavaLangObject != nullptr);
109 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
110 ASSERT_EQ(JavaLangObject->GetClass(),
111 JavaLangObject->GetClass()->GetClass());
112 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
113 std::string temp;
114 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
115 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
116 EXPECT_FALSE(JavaLangObject->HasSuperClass());
117 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
118 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
119 EXPECT_FALSE(JavaLangObject->IsErroneous());
120 EXPECT_TRUE(JavaLangObject->IsLoaded());
121 EXPECT_TRUE(JavaLangObject->IsResolved());
122 EXPECT_TRUE(JavaLangObject->IsVerified());
123 EXPECT_TRUE(JavaLangObject->IsInitialized());
124 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
125 EXPECT_FALSE(JavaLangObject->IsArrayClass());
126 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
127 EXPECT_FALSE(JavaLangObject->IsInterface());
128 EXPECT_TRUE(JavaLangObject->IsPublic());
129 EXPECT_FALSE(JavaLangObject->IsFinal());
130 EXPECT_FALSE(JavaLangObject->IsPrimitive());
131 EXPECT_FALSE(JavaLangObject->IsSynthetic());
132 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
133 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
134 if (!kUseBrooksReadBarrier) {
135 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
136 } else {
137 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
138 }
139 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(),
140 "shadow$_klass_");
141 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(),
142 "shadow$_monitor_");
143 if (kUseBrooksReadBarrier) {
144 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(),
145 "shadow$_x_rb_ptr_");
146 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(),
147 "shadow$_x_xpadding_");
148 }
149
150 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
151 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
152
Andreas Gampe542451c2016-07-26 09:02:02 -0700153 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000154 ArtMethod* unimplemented = runtime_->GetImtUnimplementedMethod();
155 ImTable* imt = JavaLangObject->GetImt(pointer_size);
156 ASSERT_NE(nullptr, imt);
157 for (size_t i = 0; i < ImTable::kSize; ++i) {
158 ASSERT_EQ(unimplemented, imt->Get(i, pointer_size));
159 }
160 }
161
Brian Carlstromaded5f72011-10-07 17:15:04 -0700162 void AssertArrayClass(const std::string& array_descriptor,
163 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700164 mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800166 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 StackHandleScope<2> hs(self);
168 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
169 Handle<mirror::Class> array(
170 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700171 std::string temp;
172 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700173 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700174 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700175 AssertArrayClass(array_descriptor, array);
176 }
177
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700178 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700180 ASSERT_TRUE(array.Get() != nullptr);
181 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700182 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700183 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700184 std::string temp;
185 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700186 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800187 Thread* self = Thread::Current();
188 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700189 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700190 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700191 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700193 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700194 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700195 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700196 EXPECT_TRUE(array->IsVerified());
197 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700198 EXPECT_FALSE(array->IsArrayInstance());
199 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700200 EXPECT_FALSE(array->IsInterface());
201 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
202 EXPECT_TRUE(array->IsFinal());
203 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700204 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700205 EXPECT_EQ(0U, array->NumDirectMethods());
206 EXPECT_EQ(0U, array->NumVirtualMethods());
207 EXPECT_EQ(0U, array->NumInstanceFields());
208 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700209 EXPECT_EQ(2U, array->NumDirectInterfaces());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000210 EXPECT_TRUE(array->ShouldHaveImt());
211 EXPECT_TRUE(array->ShouldHaveEmbeddedVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700212 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700213 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
215 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700217 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700218 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700219 mirror::Class* array_ptr = array->GetComponentType();
220 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000221
Andreas Gampe542451c2016-07-26 09:02:02 -0700222 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000223 mirror::Class* JavaLangObject =
224 class_linker_->FindSystemClass(self, "Ljava/lang/Object;");
225 ImTable* JavaLangObject_imt = JavaLangObject->GetImt(pointer_size);
226 // IMT of a array class should be shared with the IMT of the java.lag.Object
227 ASSERT_EQ(JavaLangObject_imt, array->GetImt(pointer_size));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700228 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 void AssertMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700231 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700233 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700234 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700235
Andreas Gampe542451c2016-07-26 09:02:02 -0700236 EXPECT_TRUE(method->HasDexCacheResolvedMethods(kRuntimePointerSize));
237 EXPECT_TRUE(method->HasDexCacheResolvedTypes(kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700238 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100239 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700240 kRuntimePointerSize));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700241 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
Vladimir Marko05792b92015-08-03 11:56:49 +0100242 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700243 kRuntimePointerSize));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700244 }
245
Mathieu Chartier3398c782016-09-30 10:27:43 -0700246 void AssertField(ObjPtr<mirror::Class> klass, ArtField* field)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700248 EXPECT_TRUE(field != nullptr);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700249 EXPECT_OBJ_PTR_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700250 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700251 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700252 }
253
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700254 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700256 std::string temp;
257 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700259 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700260 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700261 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700262 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700263 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700264 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700265 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700266 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700267 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700268 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700269 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700271 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700272 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800273 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
274 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700275 std::string temp2;
276 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
277 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700278 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700279 EXPECT_TRUE(klass->IsAbstract());
Alex Light42d511d2016-02-03 13:45:41 -0800280 // Check that all direct methods are static (either <clinit> or a regular static method).
Andreas Gampe542451c2016-07-26 09:02:02 -0700281 for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) {
Alex Light42d511d2016-02-03 13:45:41 -0800282 EXPECT_TRUE(m.IsStatic());
283 EXPECT_TRUE(m.IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700285 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700286 if (!klass->IsSynthetic()) {
287 EXPECT_NE(0U, klass->NumDirectMethods());
288 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700289 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700290 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800291 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700292 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700293 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700294 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700295 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700296 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700297 } else {
Alex Light705ad492015-09-21 11:36:30 -0700298 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700299 }
300 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700301 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700302 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700303 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700304 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700305 }
306 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700307 EXPECT_FALSE(klass->IsAbstract());
308 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700309 }
310 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700311 EXPECT_FALSE(klass->IsFinal());
312 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700313 }
314
315 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700317
Andreas Gampe542451c2016-07-26 09:02:02 -0700318 for (ArtMethod& method : klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 AssertMethod(&method);
320 EXPECT_TRUE(method.IsDirect());
321 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700322 }
323
Andreas Gampe542451c2016-07-26 09:02:02 -0700324 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 AssertMethod(&method);
326 EXPECT_FALSE(method.IsDirect());
Alex Lighte64300b2015-12-15 15:02:47 -0800327 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
328 }
329
Andreas Gampe542451c2016-07-26 09:02:02 -0700330 for (ArtMethod& method : klass->GetCopiedMethods(kRuntimePointerSize)) {
Alex Lighte64300b2015-12-15 15:02:47 -0800331 AssertMethod(&method);
332 EXPECT_FALSE(method.IsDirect());
Alex Light36121492016-02-22 13:43:29 -0800333 EXPECT_TRUE(method.IsCopied());
Alex Lighte64300b2015-12-15 15:02:47 -0800334 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
335 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
336 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
337 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700338 }
339
340 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700341 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700343 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700344 }
345
346 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700347 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700348 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700349 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700350 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700351
Vladimir Marko76649e82014-11-10 18:32:59 +0000352 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700353 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000354 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
355 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
356 klass->NumReferenceInstanceFields() *
357 sizeof(mirror::HeapReference<mirror::Object>));
358 MemberOffset current_ref_offset = start_ref_offset;
359 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700360 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700361 ObjPtr<mirror::Class> field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700362 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000363 if (!field->IsPrimitiveType()) {
364 ASSERT_TRUE(!field_type->IsPrimitive());
365 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
366 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
367 // While Reference.referent is not primitive, the ClassLinker
368 // treats it as such so that the garbage collector won't scan it.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700369 EXPECT_EQ(PrettyField(field),
Vladimir Marko76649e82014-11-10 18:32:59 +0000370 "java.lang.Object java.lang.ref.Reference.referent");
371 } else {
372 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
373 sizeof(mirror::HeapReference<mirror::Object>));
374 }
375 } else {
376 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
377 // Shuffled before references.
378 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
379 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
380 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700381 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700382 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000383 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700384
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700385 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700386 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700387 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700388 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
389 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700390 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700391 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
392 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
393 // The reference instance offsets have a bit set for each reference offset.
394 // +1 for Object's class.
395 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
396 total_num_reference_instance_fields);
397 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700398 }
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700402 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800403 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700404 StackHandleScope<1> hs(self);
405 Handle<mirror::Class> klass(
406 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
407 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700408 std::string temp;
409 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700410 EXPECT_EQ(class_loader, klass->GetClassLoader());
411 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700412 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700413 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700414 AssertArrayClass(descriptor, klass);
415 } else {
416 AssertClass(descriptor, klass);
417 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700418 }
419
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800420 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700422 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800423 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
424 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
425 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700426 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700427 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700428 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800429 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
430 const DexFile::TypeId& type_id = dex.GetTypeId(i);
431 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700432 AssertDexFileClass(class_loader, descriptor);
433 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700434 TestRootVisitor visitor;
435 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800436 // Verify the dex cache has resolution methods in all resolved method slots
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700437 mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100439 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
440 EXPECT_TRUE(
Andreas Gampe542451c2016-07-26 09:02:02 -0700441 mirror::DexCache::GetElementPtrSize(resolved_methods, i, kRuntimePointerSize) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800443 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700444 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700445
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700446 class TestRootVisitor : public SingleRootVisitor {
447 public:
448 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
449 EXPECT_TRUE(root != nullptr);
450 }
451 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700452};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700453
Narayan Kamath25352fc2016-08-03 12:46:58 +0100454class ClassLinkerMethodHandlesTest : public ClassLinkerTest {
455 protected:
456 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
457 CommonRuntimeTest::SetUpRuntimeOptions(options);
458 options->push_back(std::make_pair("-Xexperimental:method-handles", nullptr));
459 }
460};
461
Brian Carlstrom693267a2011-09-06 09:25:34 -0700462struct CheckOffset {
463 size_t cpp_offset;
464 const char* java_name;
465 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
466};
467
Elliott Hughes80609252011-09-23 17:24:51 -0700468template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700469struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800470 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
471 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700472 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700473 std::string class_descriptor;
474 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700475
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700476 bool Check() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800477 Thread* self = Thread::Current();
478 mirror::Class* klass =
479 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700480 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700481
482 bool error = false;
483
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 // Classes have a different size due to padding field. Strings are variable length.
485 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700486 // Currently only required for AccessibleObject since of the padding fields. The class linker
487 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
488 // The RoundUp is to get around this case.
489 static constexpr size_t kPackAlignment = 4;
490 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
491 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700492 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800493 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700494 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700495 << " Java=" << expected_size
496 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700497 error = true;
498 }
499 }
500
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700502 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800503 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700504 << " class=" << class_descriptor
505 << " Java=" << num_fields
506 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700507 error = true;
508 }
509
Brian Carlstromdbc05252011-09-09 01:59:59 -0700510 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700511 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700512 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800513 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700514 error = true;
515 }
516 }
517 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700518 for (size_t i = 0; i < offsets.size(); i++) {
519 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700520 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700521 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800523 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700524 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800525 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700526 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700528 << " CheckOffsets=" << offset.java_name;
529 }
530 }
531
Brian Carlstromdbc05252011-09-09 01:59:59 -0700532 for (size_t i = 0; i < offsets.size(); i++) {
533 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700534 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700535 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
536 error = true;
537 }
538 }
539 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700540 for (size_t i = 0; i < offsets.size(); i++) {
541 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700542 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700543 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800544 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800546 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700547 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
548 }
549 }
550
551 return !error;
552 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700554 void addOffset(size_t offset, const char* name) {
555 offsets.push_back(CheckOffset(offset, name));
556 }
557
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700558 private:
559 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700560};
561
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700562// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
563// is first since if it is failing, others are unlikely to succeed.
564
Brian Carlstromea46f952013-07-30 01:26:50 -0700565struct ObjectOffsets : public CheckOffsets<mirror::Object> {
566 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700567 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
568 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700569#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700570 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
571 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800572#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700573 };
574};
575
Brian Carlstromea46f952013-07-30 01:26:50 -0700576struct ClassOffsets : public CheckOffsets<mirror::Class> {
577 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700578 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700579 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700580 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
581 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
582 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
583 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800584 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
586 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
587 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
588 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700589 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
590 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800591 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700592 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700593 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
594 "numReferenceInstanceFields");
595 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
596 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700597 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_alloc_fast_path_),
599 "objectSizeAllocFastPath");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700600 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
601 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
602 "referenceInstanceOffsets");
603 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
604 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
605 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Andreas Gampe99babb62015-11-02 16:20:00 -0800606 addOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_), "verifyError");
Alex Lighte64300b2015-12-15 15:02:47 -0800607 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700608 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700609 };
610};
611
Brian Carlstromea46f952013-07-30 01:26:50 -0700612struct StringOffsets : public CheckOffsets<mirror::String> {
613 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700614 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000615 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700616 };
617};
618
Brian Carlstromea46f952013-07-30 01:26:50 -0700619struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
620 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000621 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700622 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
623 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700624 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
625 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700626 };
627};
628
Brian Carlstromea46f952013-07-30 01:26:50 -0700629struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700630 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
631 false, "Ljava/lang/StackTraceElement;") {
632 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
633 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
634 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
635 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700636 };
637};
638
Brian Carlstromea46f952013-07-30 01:26:50 -0700639struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
640 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700641 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700642 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700643 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
644 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
645 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700646 };
647};
648
Brian Carlstromea46f952013-07-30 01:26:50 -0700649struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
650 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700651 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400652 };
653};
654
Brian Carlstromea46f952013-07-30 01:26:50 -0700655struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
656 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700657 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
658 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
659 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Vladimir Marko05792b92015-08-03 11:56:49 +0100660 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100661 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_method_types_), "numResolvedMethodTypes");
Vladimir Marko05792b92015-08-03 11:56:49 +0100662 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
663 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
664 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700665 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
Narayan Kamath25352fc2016-08-03 12:46:58 +0100666 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_method_types_), "resolvedMethodTypes");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700667 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
668 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
669 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700670 };
671};
672
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700673struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
674 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700675 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
676 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
677 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
678 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700679 };
680};
681
682struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700683 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
684 false, "Ljava/lang/ref/FinalizerReference;") {
685 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
686 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
687 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700688 };
689};
690
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700691struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700692 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
693 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000694 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700695 };
696};
697
698struct FieldOffsets : public CheckOffsets<mirror::Field> {
699 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700700 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
701 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
702 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
703 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
704 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
705 };
706};
707
Neil Fuller16b21cd2016-08-12 09:37:02 +0100708struct ExecutableOffsets : public CheckOffsets<mirror::Executable> {
709 ExecutableOffsets() : CheckOffsets<mirror::Executable>(
710 false, "Ljava/lang/reflect/Executable;") {
Neil Fuller0e844392016-09-08 13:43:31 +0100711 addOffset(OFFSETOF_MEMBER(mirror::Executable, access_flags_), "accessFlags");
712 addOffset(OFFSETOF_MEMBER(mirror::Executable, art_method_), "artMethod");
713 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_), "declaringClass");
714 addOffset(OFFSETOF_MEMBER(mirror::Executable, declaring_class_of_overridden_method_),
715 "declaringClassOfOverriddenMethod");
716 addOffset(OFFSETOF_MEMBER(mirror::Executable, dex_method_index_), "dexMethodIndex");
Neil Fuller60458a02016-09-01 15:32:44 +0100717 addOffset(OFFSETOF_MEMBER(mirror::Executable, has_real_parameter_data_),
718 "hasRealParameterData");
719 addOffset(OFFSETOF_MEMBER(mirror::Executable, parameters_), "parameters");
Neil Fuller16b21cd2016-08-12 09:37:02 +0100720 };
721};
722
Narayan Kamathafa48272016-08-03 12:46:58 +0100723struct MethodTypeOffsets : public CheckOffsets<mirror::MethodType> {
724 MethodTypeOffsets() : CheckOffsets<mirror::MethodType>(
725 false, "Ljava/lang/invoke/MethodType;") {
726 addOffset(OFFSETOF_MEMBER(mirror::MethodType, form_), "form");
727 addOffset(OFFSETOF_MEMBER(mirror::MethodType, method_descriptor_), "methodDescriptor");
728 addOffset(OFFSETOF_MEMBER(mirror::MethodType, p_types_), "ptypes");
729 addOffset(OFFSETOF_MEMBER(mirror::MethodType, r_type_), "rtype");
730 addOffset(OFFSETOF_MEMBER(mirror::MethodType, wrap_alt_), "wrapAlt");
731 }
732};
733
734struct MethodHandleImplOffsets : public CheckOffsets<mirror::MethodHandleImpl> {
735 MethodHandleImplOffsets() : CheckOffsets<mirror::MethodHandleImpl>(
736 false, "Ljava/lang/invoke/MethodHandle;") {
737 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, art_field_or_method_), "artFieldOrMethod");
738 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, as_type_cache_), "asTypeCache");
739 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, handle_kind_), "handleKind");
740 addOffset(OFFSETOF_MEMBER(mirror::MethodHandleImpl, method_type_), "type");
741 }
742};
743
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400744// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400745// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700746// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400747TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700749 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700750 EXPECT_TRUE(ClassOffsets().Check());
751 EXPECT_TRUE(StringOffsets().Check());
752 EXPECT_TRUE(ThrowableOffsets().Check());
753 EXPECT_TRUE(StackTraceElementOffsets().Check());
754 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400755 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700756 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700757 EXPECT_TRUE(ReferenceOffsets().Check());
758 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700759 EXPECT_TRUE(AccessibleObjectOffsets().Check());
760 EXPECT_TRUE(FieldOffsets().Check());
Neil Fuller16b21cd2016-08-12 09:37:02 +0100761 EXPECT_TRUE(ExecutableOffsets().Check());
Narayan Kamathafa48272016-08-03 12:46:58 +0100762 EXPECT_TRUE(MethodTypeOffsets().Check());
763 EXPECT_TRUE(MethodHandleImplOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700764}
765
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700766TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700768 AssertNonExistentClass("NoSuchClass;");
769 AssertNonExistentClass("LNoSuchClass;");
770}
771
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700772TEST_F(ClassLinkerTest, GetDexFiles) {
773 ScopedObjectAccess soa(Thread::Current());
774
775 jobject jclass_loader = LoadDex("Nested");
776 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
777 ASSERT_EQ(dex_files.size(), 1U);
778 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
779
780 jobject jclass_loader2 = LoadDex("MultiDex");
781 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
782 ASSERT_EQ(dex_files2.size(), 2U);
783 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
784}
785
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700786TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700788 StackHandleScope<1> hs(soa.Self());
789 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700790 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700791
Ian Rogers98379392014-02-24 16:53:16 -0800792 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700793 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700794 EXPECT_EQ(0U, outer->NumVirtualMethods());
795 EXPECT_EQ(1U, outer->NumDirectMethods());
796
Ian Rogers98379392014-02-24 16:53:16 -0800797 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700798 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700799 EXPECT_EQ(0U, inner->NumVirtualMethods());
800 EXPECT_EQ(1U, inner->NumDirectMethods());
801}
802
803TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700805 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800806 for (int ch = 1; ch < 256; ++ch) {
807 std::string descriptor;
808 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700809 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700810 AssertNonExistentClass(descriptor);
811 } else {
812 AssertPrimitiveClass(descriptor);
813 }
814 }
815}
816
817TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800819 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000820 AssertObjectClass(JavaLangObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700821
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700822 StackHandleScope<1> hs(soa.Self());
823 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700824 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700825 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800826 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700827 ASSERT_TRUE(MyClass != nullptr);
828 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700829 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
830 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000831 std::string temp;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700832 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700833 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
834 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700835 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700836 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700837 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700838 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700839 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700840 EXPECT_FALSE(MyClass->IsVerified());
841 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700842 EXPECT_FALSE(MyClass->IsArrayInstance());
843 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700844 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 EXPECT_FALSE(MyClass->IsInterface());
846 EXPECT_FALSE(MyClass->IsPublic());
847 EXPECT_FALSE(MyClass->IsFinal());
848 EXPECT_FALSE(MyClass->IsPrimitive());
849 EXPECT_FALSE(MyClass->IsSynthetic());
850 EXPECT_EQ(1U, MyClass->NumDirectMethods());
851 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
852 EXPECT_EQ(0U, MyClass->NumInstanceFields());
853 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700854 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700855
856 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
857
858 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700859 AssertArrayClass("[C", "C", nullptr);
860 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700862 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700863 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700864 // or not available at all
865 AssertNonExistentClass("[[[[LNonExistentClass;");
866}
867
Mathieu Chartierb8901302016-09-30 10:27:43 -0700868TEST_F(ClassLinkerTest, LookupResolvedType) {
869 ScopedObjectAccess soa(Thread::Current());
870 StackHandleScope<1> hs(soa.Self());
871 Handle<mirror::ClassLoader> class_loader(
872 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
873 AssertNonExistentClass("LMyClass;");
874 ObjPtr<mirror::Class> klass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
875 uint32_t type_idx = klass->GetClassDef()->class_idx_;
876 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
877 const DexFile& dex_file = klass->GetDexFile();
878 EXPECT_EQ(dex_cache->GetResolvedType(type_idx), klass.Decode());
879 EXPECT_OBJ_PTR_EQ(
880 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
881 klass);
882 // Zero out the resolved type and make sure LookupResolvedType still finds it.
883 dex_cache->SetResolvedType(type_idx, nullptr);
884 EXPECT_TRUE(dex_cache->GetResolvedType(type_idx) == nullptr);
885 EXPECT_OBJ_PTR_EQ(
886 class_linker_->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader.Get()),
887 klass);
888}
889
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700890TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800892 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
893 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700894}
895
Ian Rogersa15e67d2012-02-28 13:51:55 -0800896// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400897// start of the object
898TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800900 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700901 mirror::ObjectArray<mirror::String>* array =
902 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800903 uintptr_t data_offset =
904 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
905 0));
906 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800907 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
908 } else {
909 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
910 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400911}
912
913TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700915 StackHandleScope<5> hs(soa.Self());
916 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800917 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800918 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
919 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
920
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700921 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800922 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800923 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
924 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
925
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700926 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800927 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800928 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
929 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
930
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700931 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800932 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800933 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
934 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
935
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700936 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800937 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800938 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
939 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
940
941 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400942}
943
Elliott Hughes33203b52011-09-20 19:42:01 -0700944TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
945 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
946 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800948 ScopedNullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700949 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800950 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700951 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800952 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700953 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800954 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700955 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800956 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700957 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800958 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700959 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800960 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700961 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800962 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700963 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800964 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700965 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700966}
967
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700968TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700970 StackHandleScope<2> hs(soa.Self());
971 Handle<mirror::ClassLoader> class_loader_1(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700972 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700973 Handle<mirror::ClassLoader> class_loader_2(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700974 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800975 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
976 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700977 EXPECT_TRUE(MyClass_1 != nullptr);
978 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700979 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400980}
981
Jesse Wilson7833bd22011-08-09 18:31:44 -0400982TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700984 StackHandleScope<2> hs(soa.Self());
985 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700986 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700987 Handle<mirror::Class> statics(
988 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700989 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400990
jeffhaoabcfde32011-09-29 15:05:18 -0700991 // Static final primitives that are initialized by a compile-time constant
992 // expression resolve to a copy of a constant value from the constant pool.
993 // So <clinit> should be null.
Andreas Gampe542451c2016-07-26 09:02:02 -0700994 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700995 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700996
997 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400998
Mathieu Chartierc7853442015-03-27 14:35:38 -0700999 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001000 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001001 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
1002 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001003
Mathieu Chartierc7853442015-03-27 14:35:38 -07001004 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001005 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001006 EXPECT_EQ(5, s1->GetByte(statics.Get()));
1007 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001008
Mathieu Chartierc7853442015-03-27 14:35:38 -07001009 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001010 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001011 EXPECT_EQ('a', s2->GetChar(statics.Get()));
1012 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -04001013
Mathieu Chartierc7853442015-03-27 14:35:38 -07001014 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001015 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001016 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
1017 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001018
Mathieu Chartierc7853442015-03-27 14:35:38 -07001019 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001020 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001021 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
1022 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001023
Mathieu Chartierc7853442015-03-27 14:35:38 -07001024 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001025 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001026 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
1027 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001028
Mathieu Chartierc7853442015-03-27 14:35:38 -07001029 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001030 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -07001031 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001032 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001033
Mathieu Chartierc7853442015-03-27 14:35:38 -07001034 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001035 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -07001036 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001037 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001038
Mathieu Chartierc7853442015-03-27 14:35:38 -07001039 ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
Mathieu Chartierf8322842014-05-16 10:59:25 -07001040 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001041 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001042 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Mathieu Chartier3398c782016-09-30 10:27:43 -07001043 mirror::String* str_value = mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot");
1044 s8->SetObject<false>(s8->GetDeclaringClass(), str_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001045
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001046 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
1047 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001048 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
1049 EXPECT_EQ(6, s1->GetByte(statics.Get()));
1050 EXPECT_EQ('b', s2->GetChar(statics.Get()));
1051 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
1052 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
1053 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -07001054 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
1055 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001056 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -04001057}
1058
Brian Carlstrom30b94452011-08-25 21:35:26 -07001059TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001061 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001062 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001063 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -07001064 Handle<mirror::Class> I(
1065 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
1066 Handle<mirror::Class> J(
1067 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
1068 Handle<mirror::Class> K(
1069 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
1070 Handle<mirror::Class> A(
1071 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
1072 Handle<mirror::Class> B(
1073 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
1074 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
1075 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
1076 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
1077 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
1078 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079
Ian Rogersd91d6d62013-09-25 20:26:14 -07001080 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Andreas Gampe542451c2016-07-26 09:02:02 -07001081 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1082 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1083 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
1084 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, kRuntimePointerSize);
1085 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, kRuntimePointerSize);
1086 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, kRuntimePointerSize);
1087 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, kRuntimePointerSize);
1088 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, kRuntimePointerSize);
1089 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, kRuntimePointerSize);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001090 ASSERT_TRUE(Ii != nullptr);
1091 ASSERT_TRUE(Jj1 != nullptr);
1092 ASSERT_TRUE(Jj2 != nullptr);
1093 ASSERT_TRUE(Kj1 != nullptr);
1094 ASSERT_TRUE(Kj2 != nullptr);
1095 ASSERT_TRUE(Kk != nullptr);
1096 ASSERT_TRUE(Ai != nullptr);
1097 ASSERT_TRUE(Aj1 != nullptr);
1098 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001099 EXPECT_NE(Ii, Ai);
1100 EXPECT_NE(Jj1, Aj1);
1101 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001102 EXPECT_EQ(Kj1, Jj1);
1103 EXPECT_EQ(Kj2, Jj2);
Andreas Gampe542451c2016-07-26 09:02:02 -07001104 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, kRuntimePointerSize));
1105 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, kRuntimePointerSize));
1106 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, kRuntimePointerSize));
1107 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, kRuntimePointerSize));
1108 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, kRuntimePointerSize));
1109 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, kRuntimePointerSize));
Ian Rogersd24e2642012-06-06 21:21:43 -07001110
Mathieu Chartiere401d142015-04-22 13:56:20 -07001111 ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1112 ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1113 ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1114 ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001115 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001116 EXPECT_EQ(Afoo, Bfoo);
1117 EXPECT_EQ(Afoo, Jfoo);
1118 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001119}
1120
Elliott Hughesf3778f62012-01-26 14:14:35 -08001121TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001122 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001123
jeffhaoabcfde32011-09-29 15:05:18 -07001124 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1125 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001126
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 ScopedObjectAccess soa(Thread::Current());
1128 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001129 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001130 StackHandleScope<1> hs(soa.Self());
1131 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001132 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001133 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Andreas Gampe542451c2016-07-26 09:02:02 -07001134 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
1135 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", kRuntimePointerSize);
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001136 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001137 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001138 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001139 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001140 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001141 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001142 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001143 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001144 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001145}
1146
Elliott Hughes20cde902011-10-04 17:37:27 -07001147TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001149 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001150
1151 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001152 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001153 EXPECT_FALSE(c->IsFinalizable());
1154
1155 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001156 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001157 EXPECT_FALSE(c->IsFinalizable());
1158
1159 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001160 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001161 EXPECT_FALSE(c->IsFinalizable());
1162
1163 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001164 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001165 EXPECT_TRUE(c->IsFinalizable());
1166
1167 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001168 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001169 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001170 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001171 EXPECT_TRUE(c->IsFinalizable());
1172
1173 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1174 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001175 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001176 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001177 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001178 EXPECT_TRUE(c->IsFinalizable());
1179}
1180
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001181TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001183 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001184 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001185 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001186 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1187 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001188 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1189 }
1190}
1191
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001192TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1193 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001194 ScopedNullHandle<mirror::ClassLoader> class_loader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001195 mirror::Class* c;
1196
1197 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001198 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001199 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001200
1201 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001202 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001203 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001204
1205 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001206 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001207 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001208
1209 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001210 ASSERT_TRUE(c != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001211 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(kRuntimePointerSize));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001212}
1213
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214static void CheckMethod(ArtMethod* method, bool verified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001215 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001216 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001217 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
Andreas Gampe48498592014-09-10 19:48:05 -07001218 << PrettyMethod(method, true);
1219 }
1220}
1221
Igor Murashkindf707e42016-02-02 16:56:50 -08001222static void CheckVerificationAttempted(mirror::Class* c, bool preverified)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001223 REQUIRES_SHARED(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001224 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
Andreas Gampe48498592014-09-10 19:48:05 -07001225 << "Class " << PrettyClass(c) << " not as expected";
Andreas Gampe542451c2016-07-26 09:02:02 -07001226 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001227 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001228 }
1229}
1230
1231TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1232 ScopedObjectAccess soa(Thread::Current());
1233
1234 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001235 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001236 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1237 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001238 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001239}
1240
1241TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1242 ScopedObjectAccess soa(Thread::Current());
1243
1244 StackHandleScope<1> hs(soa.Self());
1245
1246 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1247 soa.Self(), "Ljava/lang/SecurityManager;")));
1248 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1249 "core";
1250
Igor Murashkindf707e42016-02-02 16:56:50 -08001251 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001252
Ian Rogers7b078e82014-09-10 14:44:24 -07001253 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001254 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001255}
1256
1257TEST_F(ClassLinkerTest, Preverified_App) {
1258 ScopedObjectAccess soa(Thread::Current());
1259
1260 StackHandleScope<2> hs(soa.Self());
1261 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001262 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Andreas Gampe48498592014-09-10 19:48:05 -07001263 Handle<mirror::Class> statics(
1264 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1265
Igor Murashkindf707e42016-02-02 16:56:50 -08001266 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001267
Ian Rogers7b078e82014-09-10 14:44:24 -07001268 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001269 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001270}
1271
Sebastien Hertz6963e442014-11-26 22:11:27 +01001272TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1273 ScopedObjectAccess soa(Thread::Current());
1274
1275 StackHandleScope<3> hs(soa.Self());
1276 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001277 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Statics"))));
Sebastien Hertz6963e442014-11-26 22:11:27 +01001278
1279 // java.lang.Object is a bootstrap class.
1280 Handle<mirror::Class> jlo_class(
1281 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1282 ASSERT_TRUE(jlo_class.Get() != nullptr);
1283 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1284
1285 // Statics is not a bootstrap class.
1286 Handle<mirror::Class> statics(
1287 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1288 ASSERT_TRUE(statics.Get() != nullptr);
1289 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1290}
1291
Mathieu Chartier76172162016-01-26 14:54:06 -08001292// Regression test for b/26799552.
1293TEST_F(ClassLinkerTest, RegisterDexFileName) {
1294 ScopedObjectAccess soa(Thread::Current());
1295 StackHandleScope<2> hs(soa.Self());
1296 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1297 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1298 {
1299 ReaderMutexLock mu(soa.Self(), *class_linker->DexLock());
1300 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1301 dex_cache.Assign(down_cast<mirror::DexCache*>(soa.Self()->DecodeJObject(data.weak_root)));
1302 if (dex_cache.Get() != nullptr) {
1303 break;
1304 }
1305 }
1306 ASSERT_TRUE(dex_cache.Get() != nullptr);
1307 }
1308 // Make a copy of the dex cache and change the name.
1309 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1310 const uint16_t data[] = { 0x20AC, 0x20A1 };
1311 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1312 arraysize(data),
1313 data)));
1314 dex_cache->SetLocation(location.Get());
1315 const DexFile* old_dex_file = dex_cache->GetDexFile();
1316
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001317 std::unique_ptr<DexFile> dex_file(new DexFile(old_dex_file->Begin(),
1318 old_dex_file->Size(),
1319 location->ToModifiedUtf8(),
1320 0u,
Hiroshi Yamauchi906ae5d2016-02-22 13:30:35 -08001321 nullptr));
Mathieu Chartier76172162016-01-26 14:54:06 -08001322 {
1323 WriterMutexLock mu(soa.Self(), *class_linker->DexLock());
1324 // Check that inserting with a UTF16 name works.
1325 class_linker->RegisterDexFileLocked(*dex_file, dex_cache);
1326 }
1327}
1328
Narayan Kamath25352fc2016-08-03 12:46:58 +01001329TEST_F(ClassLinkerMethodHandlesTest, TestResolveMethodTypes) {
1330 ScopedObjectAccess soa(Thread::Current());
1331
1332 StackHandleScope<7> hs(soa.Self());
1333
1334 Handle<mirror::ClassLoader> class_loader(
1335 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("MethodTypes"))));
1336 Handle<mirror::Class> method_types(
1337 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
1338 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
1339
1340 ArtMethod* method1 = method_types->FindVirtualMethod("method1",
1341 "(Ljava/lang/String;)Ljava/lang/String;",
1342 kRuntimePointerSize);
1343
1344 const DexFile& dex_file = *(method1->GetDexFile());
1345 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
1346 class_linker_->FindDexCache(Thread::Current(), dex_file));
1347
1348 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
1349
1350 // This is the MethodType corresponding to the prototype of
1351 // String MethodTypes# method1(String).
1352 // Its RType = Ljava/lang/String;
Narayan Kamath42b3dd02016-10-03 11:33:01 +01001353 // Its PTypes = { Ljava/lang/String; }
Narayan Kamath25352fc2016-08-03 12:46:58 +01001354 Handle<mirror::MethodType> method1_type = hs.NewHandle(
1355 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1356
1357 // Assert that the method type was resolved successfully.
1358 ASSERT_TRUE(method1_type.Get() != nullptr);
1359
1360 // Assert that the return type and the method arguments are as we expect.
1361 Handle<mirror::Class> string_class(
1362 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader)));
1363 ASSERT_EQ(string_class.Get(), method1_type->GetRType());
1364 ASSERT_EQ(string_class.Get(), method1_type->GetPTypes()->Get(0));
1365
1366 // Resolve the method type again and assert that we get back the same value.
1367 Handle<mirror::MethodType> method1_type2 = hs.NewHandle(
1368 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
1369 ASSERT_EQ(method1_type.Get(), method1_type2.Get());
1370
1371 // Resolve the MethodType associated with a different method signature
1372 // and assert it's different.
1373 ArtMethod* method2 = method_types->FindVirtualMethod(
1374 "method2",
1375 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
1376 kRuntimePointerSize);
1377 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
1378 Handle<mirror::MethodType> method2_type = hs.NewHandle(
1379 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
1380
1381 ASSERT_TRUE(method1_type.Get() != method2_type.Get());
1382}
1383
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001384} // namespace art