blob: 2c086c59f0bb4e6a465741d713d7e1927648aaa7 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/heap.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/abstract_method.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070030#include "mirror/accessible_object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/class-inl.h"
32#include "mirror/dex_cache.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070033#include "mirror/field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
36#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070037#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#include "scoped_thread_state_change.h"
42#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
45
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080046class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 void AssertNonExistentClass(const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -070049 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070050 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070051 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070052 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000053 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070054 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080055 mirror::Class* exception_class =
56 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070057 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070058 }
59
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 void AssertPrimitiveClass(const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -070061 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080062 Thread* self = Thread::Current();
63 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070064 }
65
Ian Rogersef7d42f2014-01-06 12:55:46 -080066 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070068 ASSERT_TRUE(primitive != nullptr);
69 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070071 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070072 std::string temp;
73 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070074 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070075 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070076 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070077 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070079 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070080 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070081 EXPECT_TRUE(primitive->IsVerified());
82 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070083 EXPECT_FALSE(primitive->IsArrayInstance());
84 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070085 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070086 EXPECT_FALSE(primitive->IsInterface());
87 EXPECT_TRUE(primitive->IsPublic());
88 EXPECT_TRUE(primitive->IsFinal());
89 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070090 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 EXPECT_EQ(0U, primitive->NumDirectMethods());
92 EXPECT_EQ(0U, primitive->NumVirtualMethods());
93 EXPECT_EQ(0U, primitive->NumInstanceFields());
94 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070095 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -070096 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -070097 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070098 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -070099 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700100 }
101
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102 void AssertArrayClass(const std::string& array_descriptor,
103 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700104 mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700105 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800106 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 StackHandleScope<2> hs(self);
108 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
109 Handle<mirror::Class> array(
110 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700111 std::string temp;
112 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700113 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700114 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700115 AssertArrayClass(array_descriptor, array);
116 }
117
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700118 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700120 ASSERT_TRUE(array.Get() != nullptr);
121 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700122 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700123 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700124 std::string temp;
125 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700126 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800127 Thread* self = Thread::Current();
128 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700129 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700130 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700131 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700133 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700134 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700135 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700136 EXPECT_TRUE(array->IsVerified());
137 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700138 EXPECT_FALSE(array->IsArrayInstance());
139 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700140 EXPECT_FALSE(array->IsInterface());
141 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
142 EXPECT_TRUE(array->IsFinal());
143 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700144 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700145 EXPECT_EQ(0U, array->NumDirectMethods());
146 EXPECT_EQ(0U, array->NumVirtualMethods());
147 EXPECT_EQ(0U, array->NumInstanceFields());
148 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700149 EXPECT_EQ(2U, array->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700150 EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700151 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700152 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700153 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
154 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700155 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700156 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700157 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700158 mirror::Class* array_ptr = array->GetComponentType();
159 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700160 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700161
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 void AssertMethod(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700163 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700164 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700165 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700166 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700167
Vladimir Marko05792b92015-08-03 11:56:49 +0100168 EXPECT_TRUE(method->HasDexCacheResolvedMethods(sizeof(void*)));
169 EXPECT_TRUE(method->HasDexCacheResolvedTypes(sizeof(void*)));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700170 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100171 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
172 sizeof(void*)));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700173 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
Vladimir Marko05792b92015-08-03 11:56:49 +0100174 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
175 sizeof(void*)));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700176 }
177
Mathieu Chartierc7853442015-03-27 14:35:38 -0700178 void AssertField(mirror::Class* klass, ArtField* field)
Mathieu Chartier90443472015-07-16 20:32:27 -0700179 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700180 EXPECT_TRUE(field != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700181 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700182 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700183 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700184 }
185
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700186 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 std::string temp;
189 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700191 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700192 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700193 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700194 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700195 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700196 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700197 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700198 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700199 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700200 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700201 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700203 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700204 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800205 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
206 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 std::string temp2;
208 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
209 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700210 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700211 EXPECT_TRUE(klass->IsAbstract());
212 if (klass->NumDirectMethods() == 1) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsClassInitializer());
214 EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700215 } else {
216 EXPECT_EQ(0U, klass->NumDirectMethods());
217 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700218 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700219 if (!klass->IsSynthetic()) {
220 EXPECT_NE(0U, klass->NumDirectMethods());
221 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700222 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700223 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700225 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700226 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700227 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700228 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700229 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700230 } else {
Ian Rogers9bc81912012-10-11 21:43:36 -0700231 EXPECT_EQ(interface->NumVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700232 }
233 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700234 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700236 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700237 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700238 }
239 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700240 EXPECT_FALSE(klass->IsAbstract());
241 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700242 }
243 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700244 EXPECT_FALSE(klass->IsFinal());
245 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700246 }
247
248 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700250
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 for (ArtMethod& method : klass->GetDirectMethods(sizeof(void*))) {
252 AssertMethod(&method);
253 EXPECT_TRUE(method.IsDirect());
254 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700255 }
256
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 for (ArtMethod& method : klass->GetVirtualMethods(sizeof(void*))) {
258 AssertMethod(&method);
259 EXPECT_FALSE(method.IsDirect());
260 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700261 }
262
263 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700265 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700266 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700267 }
268
269 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700270 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700271 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700272 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700273 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700274
Vladimir Marko76649e82014-11-10 18:32:59 +0000275 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700276 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000277 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
278 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
279 klass->NumReferenceInstanceFields() *
280 sizeof(mirror::HeapReference<mirror::Object>));
281 MemberOffset current_ref_offset = start_ref_offset;
282 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700283 ArtField* field = klass->GetInstanceField(i);
284 mirror::Class* field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700285 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000286 if (!field->IsPrimitiveType()) {
287 ASSERT_TRUE(!field_type->IsPrimitive());
288 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
289 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
290 // While Reference.referent is not primitive, the ClassLinker
291 // treats it as such so that the garbage collector won't scan it.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292 EXPECT_EQ(PrettyField(field),
Vladimir Marko76649e82014-11-10 18:32:59 +0000293 "java.lang.Object java.lang.ref.Reference.referent");
294 } else {
295 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
296 sizeof(mirror::HeapReference<mirror::Object>));
297 }
298 } else {
299 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
300 // Shuffled before references.
301 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
302 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
303 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700304 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700305 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000306 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700307
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700308 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700309 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700310 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700311 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
312 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700313 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700314 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
315 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
316 // The reference instance offsets have a bit set for each reference offset.
317 // +1 for Object's class.
318 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
319 total_num_reference_instance_fields);
320 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700321 }
322
Brian Carlstromea46f952013-07-30 01:26:50 -0700323 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700324 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700325 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800326 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700327 StackHandleScope<1> hs(self);
328 Handle<mirror::Class> klass(
329 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
330 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700331 std::string temp;
332 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700333 EXPECT_EQ(class_loader, klass->GetClassLoader());
334 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700335 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700336 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700337 AssertArrayClass(descriptor, klass);
338 } else {
339 AssertClass(descriptor, klass);
340 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700341 }
342
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800343 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700344 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700345 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800346 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
347 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
348 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700349 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700350 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700351 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800352 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
353 const DexFile::TypeId& type_id = dex.GetTypeId(i);
354 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700355 AssertDexFileClass(class_loader, descriptor);
356 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700357 TestRootVisitor visitor;
358 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800359 // Verify the dex cache has resolution methods in all resolved method slots
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700360 mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100362 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
363 EXPECT_TRUE(
364 mirror::DexCache::GetElementPtrSize(resolved_methods, i, sizeof(void*)) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800366 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700367 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700368
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700369 class TestRootVisitor : public SingleRootVisitor {
370 public:
371 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
372 EXPECT_TRUE(root != nullptr);
373 }
374 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700375};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700376
Brian Carlstrom693267a2011-09-06 09:25:34 -0700377struct CheckOffset {
378 size_t cpp_offset;
379 const char* java_name;
380 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
381};
382
Elliott Hughes80609252011-09-23 17:24:51 -0700383template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700384struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800385 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
386 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700387 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700388 std::string class_descriptor;
389 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700390
Mathieu Chartier90443472015-07-16 20:32:27 -0700391 bool Check() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800392 Thread* self = Thread::Current();
393 mirror::Class* klass =
394 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700395 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700396
397 bool error = false;
398
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 // Classes have a different size due to padding field. Strings are variable length.
400 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700401 // Currently only required for AccessibleObject since of the padding fields. The class linker
402 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
403 // The RoundUp is to get around this case.
404 static constexpr size_t kPackAlignment = 4;
405 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
406 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700407 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800408 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700409 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700410 << " Java=" << expected_size
411 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700412 error = true;
413 }
414 }
415
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700416 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700417 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800418 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700419 << " class=" << class_descriptor
420 << " Java=" << num_fields
421 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700422 error = true;
423 }
424
Brian Carlstromdbc05252011-09-09 01:59:59 -0700425 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700426 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700427 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700429 error = true;
430 }
431 }
432 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700433 for (size_t i = 0; i < offsets.size(); i++) {
434 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700435 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700436 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800438 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700439 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800440 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700441 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700443 << " CheckOffsets=" << offset.java_name;
444 }
445 }
446
Brian Carlstromdbc05252011-09-09 01:59:59 -0700447 for (size_t i = 0; i < offsets.size(); i++) {
448 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700449 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700450 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
451 error = true;
452 }
453 }
454 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700455 for (size_t i = 0; i < offsets.size(); i++) {
456 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700457 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700458 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800459 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700460 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800461 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700462 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
463 }
464 }
465
466 return !error;
467 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700468
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700469 void addOffset(size_t offset, const char* name) {
470 offsets.push_back(CheckOffset(offset, name));
471 }
472
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 private:
474 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700475};
476
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700477// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
478// is first since if it is failing, others are unlikely to succeed.
479
Brian Carlstromea46f952013-07-30 01:26:50 -0700480struct ObjectOffsets : public CheckOffsets<mirror::Object> {
481 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700482 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
483 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700484#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700485 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
486 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800487#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700488 };
489};
490
Brian Carlstromea46f952013-07-30 01:26:50 -0700491struct ClassOffsets : public CheckOffsets<mirror::Class> {
492 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700493 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700494 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700495 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
496 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
497 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
498 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
499 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
500 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
501 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
502 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
503 addOffset(OFFSETOF_MEMBER(mirror::Class, direct_methods_), "directMethods");
504 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
505 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
506 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700507 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
508 "numReferenceInstanceFields");
509 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
510 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700511 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
512 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
513 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
514 "referenceInstanceOffsets");
515 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
516 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
517 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Andreas Gampe99babb62015-11-02 16:20:00 -0800518 addOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_), "verifyError");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700519 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_), "virtualMethods");
520 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700521 };
522};
523
Brian Carlstromea46f952013-07-30 01:26:50 -0700524struct StringOffsets : public CheckOffsets<mirror::String> {
525 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700526 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
527 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hashCode");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700528 };
529};
530
Brian Carlstromea46f952013-07-30 01:26:50 -0700531struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
532 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700533 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
534 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
535 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_state_), "stackState");
536 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
537 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538 };
539};
540
Brian Carlstromea46f952013-07-30 01:26:50 -0700541struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700542 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
543 false, "Ljava/lang/StackTraceElement;") {
544 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
545 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
546 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
547 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548 };
549};
550
Brian Carlstromea46f952013-07-30 01:26:50 -0700551struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
552 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700553 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700554 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700555 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
556 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
557 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558 };
559};
560
Brian Carlstromea46f952013-07-30 01:26:50 -0700561struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
562 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700563 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400564 };
565};
566
Brian Carlstromea46f952013-07-30 01:26:50 -0700567struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
568 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700569 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
570 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
571 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Vladimir Marko05792b92015-08-03 11:56:49 +0100572 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
573 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
574 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
575 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
577 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
578 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
579 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700580 };
581};
582
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700583struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
584 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700585 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
586 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
587 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
588 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700589 };
590};
591
592struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700593 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
594 false, "Ljava/lang/ref/FinalizerReference;") {
595 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
596 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
597 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700598 };
599};
600
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700601struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700602 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
603 false, "Ljava/lang/reflect/AccessibleObject;") {
604 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "flag");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700605 };
606};
607
608struct FieldOffsets : public CheckOffsets<mirror::Field> {
609 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700610 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
611 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
612 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
613 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
614 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
615 };
616};
617
618struct AbstractMethodOffsets : public CheckOffsets<mirror::AbstractMethod> {
619 AbstractMethodOffsets() : CheckOffsets<mirror::AbstractMethod>(
620 false, "Ljava/lang/reflect/AbstractMethod;") {
621 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, access_flags_), "accessFlags");
622 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, art_method_), "artMethod");
623 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_), "declaringClass");
624 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_of_overridden_method_),
625 "declaringClassOfOverriddenMethod");
626 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, dex_method_index_), "dexMethodIndex");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700627 };
628};
629
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400630// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400631// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700632// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400633TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700635 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700636 EXPECT_TRUE(ClassOffsets().Check());
637 EXPECT_TRUE(StringOffsets().Check());
638 EXPECT_TRUE(ThrowableOffsets().Check());
639 EXPECT_TRUE(StackTraceElementOffsets().Check());
640 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400641 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700642 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700643 EXPECT_TRUE(ReferenceOffsets().Check());
644 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700645 EXPECT_TRUE(AccessibleObjectOffsets().Check());
646 EXPECT_TRUE(FieldOffsets().Check());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700647 EXPECT_TRUE(AbstractMethodOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700648}
649
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700650TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700652 AssertNonExistentClass("NoSuchClass;");
653 AssertNonExistentClass("LNoSuchClass;");
654}
655
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700656TEST_F(ClassLinkerTest, GetDexFiles) {
657 ScopedObjectAccess soa(Thread::Current());
658
659 jobject jclass_loader = LoadDex("Nested");
660 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
661 ASSERT_EQ(dex_files.size(), 1U);
662 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
663
664 jobject jclass_loader2 = LoadDex("MultiDex");
665 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
666 ASSERT_EQ(dex_files2.size(), 2U);
667 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
668}
669
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700670TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700672 StackHandleScope<1> hs(soa.Self());
673 Handle<mirror::ClassLoader> class_loader(
674 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700675
Ian Rogers98379392014-02-24 16:53:16 -0800676 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700677 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700678 EXPECT_EQ(0U, outer->NumVirtualMethods());
679 EXPECT_EQ(1U, outer->NumDirectMethods());
680
Ian Rogers98379392014-02-24 16:53:16 -0800681 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700682 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700683 EXPECT_EQ(0U, inner->NumVirtualMethods());
684 EXPECT_EQ(1U, inner->NumDirectMethods());
685}
686
687TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700689 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800690 for (int ch = 1; ch < 256; ++ch) {
691 std::string descriptor;
692 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700693 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700694 AssertNonExistentClass(descriptor);
695 } else {
696 AssertPrimitiveClass(descriptor);
697 }
698 }
699}
700
701TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800703 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700704 ASSERT_TRUE(JavaLangObject != nullptr);
705 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700706 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
707 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700708 std::string temp;
709 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700710 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700711 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700712 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700713 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700714 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700715 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700716 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromea46f952013-07-30 01:26:50 -0700717 EXPECT_TRUE(JavaLangObject->IsVerified());
718 EXPECT_TRUE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700719 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
720 EXPECT_FALSE(JavaLangObject->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700721 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700722 EXPECT_FALSE(JavaLangObject->IsInterface());
723 EXPECT_TRUE(JavaLangObject->IsPublic());
724 EXPECT_FALSE(JavaLangObject->IsFinal());
725 EXPECT_FALSE(JavaLangObject->IsPrimitive());
726 EXPECT_FALSE(JavaLangObject->IsSynthetic());
727 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
728 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700729 if (!kUseBrooksReadBarrier) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800730 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
731 } else {
732 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
733 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700734 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_");
735 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700736 if (kUseBrooksReadBarrier) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700737 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_");
738 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800739 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700740
741 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700742 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700743
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700744 StackHandleScope<1> hs(soa.Self());
745 Handle<mirror::ClassLoader> class_loader(
746 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700747 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800748 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700749 ASSERT_TRUE(MyClass != nullptr);
750 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700751 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
752 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700753 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700754 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
755 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700756 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700757 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700758 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700759 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700760 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700761 EXPECT_FALSE(MyClass->IsVerified());
762 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700763 EXPECT_FALSE(MyClass->IsArrayInstance());
764 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700765 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700766 EXPECT_FALSE(MyClass->IsInterface());
767 EXPECT_FALSE(MyClass->IsPublic());
768 EXPECT_FALSE(MyClass->IsFinal());
769 EXPECT_FALSE(MyClass->IsPrimitive());
770 EXPECT_FALSE(MyClass->IsSynthetic());
771 EXPECT_EQ(1U, MyClass->NumDirectMethods());
772 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
773 EXPECT_EQ(0U, MyClass->NumInstanceFields());
774 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700775 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700776
777 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
778
779 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700780 AssertArrayClass("[C", "C", nullptr);
781 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700782 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700783 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700784 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700785 // or not available at all
786 AssertNonExistentClass("[[[[LNonExistentClass;");
787}
788
789TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800791 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
792 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700793}
794
Ian Rogersa15e67d2012-02-28 13:51:55 -0800795// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400796// start of the object
797TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700798 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800799 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700800 mirror::ObjectArray<mirror::String>* array =
801 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800802 uintptr_t data_offset =
803 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
804 0));
805 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800806 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
807 } else {
808 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
809 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400810}
811
812TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700814 StackHandleScope<5> hs(soa.Self());
815 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800816 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800817 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
818 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
819
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700820 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800821 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800822 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
823 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
824
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700825 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800826 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800827 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
828 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
829
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700830 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800831 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800832 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
833 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
834
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700835 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800836 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800837 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
838 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
839
840 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400841}
842
Elliott Hughes33203b52011-09-20 19:42:01 -0700843TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
844 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
845 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700847 NullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700848 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800849 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700850 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800851 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700852 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800853 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700854 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800855 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700856 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800857 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700858 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800859 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700860 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800861 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700862 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800863 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700864 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700865}
866
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700867TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700869 StackHandleScope<2> hs(soa.Self());
870 Handle<mirror::ClassLoader> class_loader_1(
871 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
872 Handle<mirror::ClassLoader> class_loader_2(
873 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800874 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
875 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700876 EXPECT_TRUE(MyClass_1 != nullptr);
877 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700878 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400879}
880
Jesse Wilson7833bd22011-08-09 18:31:44 -0400881TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700883 StackHandleScope<2> hs(soa.Self());
884 Handle<mirror::ClassLoader> class_loader(
885 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
886 Handle<mirror::Class> statics(
887 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700888 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400889
jeffhaoabcfde32011-09-29 15:05:18 -0700890 // Static final primitives that are initialized by a compile-time constant
891 // expression resolve to a copy of a constant value from the constant pool.
892 // So <clinit> should be null.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", sizeof(void*));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700894 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700895
896 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400897
Mathieu Chartierc7853442015-03-27 14:35:38 -0700898 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700899 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700900 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
901 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400902
Mathieu Chartierc7853442015-03-27 14:35:38 -0700903 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700904 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700905 EXPECT_EQ(5, s1->GetByte(statics.Get()));
906 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400907
Mathieu Chartierc7853442015-03-27 14:35:38 -0700908 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700909 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700910 EXPECT_EQ('a', s2->GetChar(statics.Get()));
911 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400912
Mathieu Chartierc7853442015-03-27 14:35:38 -0700913 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700914 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700915 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
916 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400917
Mathieu Chartierc7853442015-03-27 14:35:38 -0700918 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700919 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700920 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
921 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400922
Mathieu Chartierc7853442015-03-27 14:35:38 -0700923 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700924 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700925 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
926 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400927
Mathieu Chartierc7853442015-03-27 14:35:38 -0700928 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700929 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700930 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700931 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400932
Mathieu Chartierc7853442015-03-27 14:35:38 -0700933 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700934 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700935 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700936 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400937
Mathieu Chartierc7853442015-03-27 14:35:38 -0700938 ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700939 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700940 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700941 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100942 s8->SetObject<false>(s8->GetDeclaringClass(),
943 mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400944
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800945 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
946 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700947 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
948 EXPECT_EQ(6, s1->GetByte(statics.Get()));
949 EXPECT_EQ('b', s2->GetChar(statics.Get()));
950 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
951 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
952 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -0700953 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
954 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700955 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400956}
957
Brian Carlstrom30b94452011-08-25 21:35:26 -0700958TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700960 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700961 Handle<mirror::ClassLoader> class_loader(
962 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700963 Handle<mirror::Class> I(
964 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
965 Handle<mirror::Class> J(
966 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
967 Handle<mirror::Class> K(
968 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
969 Handle<mirror::Class> A(
970 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
971 Handle<mirror::Class> B(
972 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
973 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
974 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
975 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
976 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
977 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700978
Ian Rogersd91d6d62013-09-25 20:26:14 -0700979 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700980 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, sizeof(void*));
981 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, sizeof(void*));
982 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, sizeof(void*));
983 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, sizeof(void*));
984 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, sizeof(void*));
985 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, sizeof(void*));
986 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, sizeof(void*));
987 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, sizeof(void*));
988 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, sizeof(void*));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700989 ASSERT_TRUE(Ii != nullptr);
990 ASSERT_TRUE(Jj1 != nullptr);
991 ASSERT_TRUE(Jj2 != nullptr);
992 ASSERT_TRUE(Kj1 != nullptr);
993 ASSERT_TRUE(Kj2 != nullptr);
994 ASSERT_TRUE(Kk != nullptr);
995 ASSERT_TRUE(Ai != nullptr);
996 ASSERT_TRUE(Aj1 != nullptr);
997 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 EXPECT_NE(Ii, Ai);
999 EXPECT_NE(Jj1, Aj1);
1000 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001001 EXPECT_EQ(Kj1, Jj1);
1002 EXPECT_EQ(Kj2, Jj2);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001003 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, sizeof(void*)));
1004 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, sizeof(void*)));
1005 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, sizeof(void*)));
1006 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, sizeof(void*)));
1007 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, sizeof(void*)));
1008 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, sizeof(void*)));
Ian Rogersd24e2642012-06-06 21:21:43 -07001009
Mathieu Chartiere401d142015-04-22 13:56:20 -07001010 ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1011 ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1012 ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1013 ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001014 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001015 EXPECT_EQ(Afoo, Bfoo);
1016 EXPECT_EQ(Afoo, Jfoo);
1017 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001018}
1019
Elliott Hughesf3778f62012-01-26 14:14:35 -08001020TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001021 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001022
jeffhaoabcfde32011-09-29 15:05:18 -07001023 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1024 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001025
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 ScopedObjectAccess soa(Thread::Current());
1027 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001028 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001029 StackHandleScope<1> hs(soa.Self());
1030 Handle<mirror::ClassLoader> class_loader(
1031 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001032 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 ArtMethod* clinit = klass->FindClassInitializer(sizeof(void*));
1034 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", sizeof(void*));
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001035 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001036 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001037 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001038 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001039 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001040 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001041 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001042 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001043 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001044}
1045
Elliott Hughes20cde902011-10-04 17:37:27 -07001046TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001048 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001049
1050 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001051 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001052 EXPECT_FALSE(c->IsFinalizable());
1053
1054 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001055 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001056 EXPECT_FALSE(c->IsFinalizable());
1057
1058 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001059 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001060 EXPECT_FALSE(c->IsFinalizable());
1061
1062 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001063 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001064 EXPECT_TRUE(c->IsFinalizable());
1065
1066 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001067 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001068 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001069 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001070 EXPECT_TRUE(c->IsFinalizable());
1071
1072 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1073 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001074 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001075 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001076 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001077 EXPECT_TRUE(c->IsFinalizable());
1078}
1079
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001080TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001082 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001083 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001084 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001085 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1086 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001087 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1088 }
1089}
1090
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001091TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1092 ScopedObjectAccess soa(Thread::Current());
1093 NullHandle<mirror::ClassLoader> class_loader;
1094 mirror::Class* c;
1095
1096 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001097 ASSERT_TRUE(c != nullptr);
1098 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001099
1100 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001101 ASSERT_TRUE(c != nullptr);
1102 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001103
1104 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001105 ASSERT_TRUE(c != nullptr);
1106 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001107
1108 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001109 ASSERT_TRUE(c != nullptr);
1110 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001111}
1112
Mathieu Chartiere401d142015-04-22 13:56:20 -07001113static void CheckMethod(ArtMethod* method, bool verified)
Mathieu Chartier90443472015-07-16 20:32:27 -07001114 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001115 if (!method->IsNative() && !method->IsAbstract()) {
1116 EXPECT_EQ((method->GetAccessFlags() & kAccPreverified) != 0U, verified)
1117 << PrettyMethod(method, true);
1118 }
1119}
1120
1121static void CheckPreverified(mirror::Class* c, bool preverified)
Mathieu Chartier90443472015-07-16 20:32:27 -07001122 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001123 EXPECT_EQ((c->GetAccessFlags() & kAccPreverified) != 0U, preverified)
1124 << "Class " << PrettyClass(c) << " not as expected";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001125 for (auto& m : c->GetDirectMethods(sizeof(void*))) {
1126 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001127 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001128 for (auto& m : c->GetVirtualMethods(sizeof(void*))) {
1129 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001130 }
1131}
1132
1133TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1134 ScopedObjectAccess soa(Thread::Current());
1135
1136 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001137 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001138 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1139 "core";
1140 CheckPreverified(JavaLangObject, true);
1141}
1142
1143TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1144 ScopedObjectAccess soa(Thread::Current());
1145
1146 StackHandleScope<1> hs(soa.Self());
1147
1148 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1149 soa.Self(), "Ljava/lang/SecurityManager;")));
1150 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1151 "core";
1152
1153 CheckPreverified(security_manager.Get(), false);
1154
Ian Rogers7b078e82014-09-10 14:44:24 -07001155 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001156 CheckPreverified(security_manager.Get(), true);
1157}
1158
1159TEST_F(ClassLinkerTest, Preverified_App) {
1160 ScopedObjectAccess soa(Thread::Current());
1161
1162 StackHandleScope<2> hs(soa.Self());
1163 Handle<mirror::ClassLoader> class_loader(
1164 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1165 Handle<mirror::Class> statics(
1166 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1167
1168 CheckPreverified(statics.Get(), false);
1169
Ian Rogers7b078e82014-09-10 14:44:24 -07001170 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001171 CheckPreverified(statics.Get(), true);
1172}
1173
Sebastien Hertz6963e442014-11-26 22:11:27 +01001174TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1175 ScopedObjectAccess soa(Thread::Current());
1176
1177 StackHandleScope<3> hs(soa.Self());
1178 Handle<mirror::ClassLoader> class_loader(
1179 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1180
1181 // java.lang.Object is a bootstrap class.
1182 Handle<mirror::Class> jlo_class(
1183 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1184 ASSERT_TRUE(jlo_class.Get() != nullptr);
1185 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1186
1187 // Statics is not a bootstrap class.
1188 Handle<mirror::Class> statics(
1189 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1190 ASSERT_TRUE(statics.Get() != nullptr);
1191 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1192}
1193
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001194} // namespace art