blob: b86da9fbb59941ab6778835e9c3942db35da7867 [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"
Alex Light705ad492015-09-21 11:36:30 -070027#include "experimental_flags.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/heap.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "mirror/abstract_method.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"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070034#include "mirror/field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/object-inl.h"
36#include "mirror/object_array-inl.h"
37#include "mirror/proxy.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070038#include "mirror/reference.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/stack_trace_element.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040#include "mirror/string-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "scoped_thread_state_change.h"
43#include "thread-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
46
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080047class ClassLinkerTest : public CommonRuntimeTest {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070048 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 void AssertNonExistentClass(const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -070050 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -070051 Thread* self = Thread::Current();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070052 EXPECT_TRUE(class_linker_->FindSystemClass(self, descriptor.c_str()) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -070053 EXPECT_TRUE(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +000054 mirror::Object* exception = self->GetException();
Elliott Hughes885c3bd2011-08-22 16:59:20 -070055 self->ClearException();
Ian Rogers98379392014-02-24 16:53:16 -080056 mirror::Class* exception_class =
57 class_linker_->FindSystemClass(self, "Ljava/lang/NoClassDefFoundError;");
Elliott Hughes885c3bd2011-08-22 16:59:20 -070058 EXPECT_TRUE(exception->InstanceOf(exception_class));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070059 }
60
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 void AssertPrimitiveClass(const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -080063 Thread* self = Thread::Current();
64 AssertPrimitiveClass(descriptor, class_linker_->FindSystemClass(self, descriptor.c_str()));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070065 }
66
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 void AssertPrimitiveClass(const std::string& descriptor, mirror::Class* primitive)
Mathieu Chartier90443472015-07-16 20:32:27 -070068 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070069 ASSERT_TRUE(primitive != nullptr);
70 ASSERT_TRUE(primitive->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070071 ASSERT_EQ(primitive->GetClass(), primitive->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070072 EXPECT_TRUE(primitive->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -070073 std::string temp;
74 ASSERT_STREQ(descriptor.c_str(), primitive->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070075 EXPECT_TRUE(primitive->GetSuperClass() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 EXPECT_FALSE(primitive->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070077 EXPECT_TRUE(primitive->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -070078 EXPECT_EQ(mirror::Class::kStatusInitialized, primitive->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070079 EXPECT_FALSE(primitive->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -070080 EXPECT_TRUE(primitive->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -070081 EXPECT_TRUE(primitive->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -070082 EXPECT_TRUE(primitive->IsVerified());
83 EXPECT_TRUE(primitive->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -070084 EXPECT_FALSE(primitive->IsArrayInstance());
85 EXPECT_FALSE(primitive->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070086 EXPECT_TRUE(primitive->GetComponentType() == nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087 EXPECT_FALSE(primitive->IsInterface());
88 EXPECT_TRUE(primitive->IsPublic());
89 EXPECT_TRUE(primitive->IsFinal());
90 EXPECT_TRUE(primitive->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070091 EXPECT_FALSE(primitive->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 EXPECT_EQ(0U, primitive->NumDirectMethods());
93 EXPECT_EQ(0U, primitive->NumVirtualMethods());
94 EXPECT_EQ(0U, primitive->NumInstanceFields());
95 EXPECT_EQ(0U, primitive->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -070096 EXPECT_EQ(0U, primitive->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -070097 EXPECT_FALSE(primitive->HasVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -070098 EXPECT_EQ(0, primitive->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070099 EXPECT_TRUE(primitive->GetIfTable() == nullptr);
Elliott Hughes00626c22013-06-14 15:04:14 -0700100 EXPECT_EQ(kAccPublic | kAccFinal | kAccAbstract, primitive->GetAccessFlags());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700101 }
102
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103 void AssertArrayClass(const std::string& array_descriptor,
104 const std::string& component_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700105 mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700106 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800107 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 StackHandleScope<2> hs(self);
109 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
110 Handle<mirror::Class> array(
111 hs.NewHandle(class_linker_->FindClass(self, array_descriptor.c_str(), loader)));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700112 std::string temp;
113 EXPECT_STREQ(component_type.c_str(), array->GetComponentType()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700114 EXPECT_EQ(class_loader, array->GetClassLoader());
Elliott Hughes00626c22013-06-14 15:04:14 -0700115 EXPECT_EQ(kAccFinal | kAccAbstract, (array->GetAccessFlags() & (kAccFinal | kAccAbstract)));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700116 AssertArrayClass(array_descriptor, array);
117 }
118
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700119 void AssertArrayClass(const std::string& array_descriptor, Handle<mirror::Class> array)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700121 ASSERT_TRUE(array.Get() != nullptr);
122 ASSERT_TRUE(array->GetClass() != nullptr);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700123 ASSERT_EQ(array->GetClass(), array->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700124 EXPECT_TRUE(array->GetClass()->GetSuperClass() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700125 std::string temp;
126 ASSERT_STREQ(array_descriptor.c_str(), array->GetDescriptor(&temp));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700127 EXPECT_TRUE(array->GetSuperClass() != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800128 Thread* self = Thread::Current();
129 EXPECT_EQ(class_linker_->FindSystemClass(self, "Ljava/lang/Object;"), array->GetSuperClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700130 EXPECT_TRUE(array->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700131 ASSERT_TRUE(array->GetComponentType() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700132 ASSERT_GT(strlen(array->GetComponentType()->GetDescriptor(&temp)), 0U);
Brian Carlstromea46f952013-07-30 01:26:50 -0700133 EXPECT_EQ(mirror::Class::kStatusInitialized, array->GetStatus());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700134 EXPECT_FALSE(array->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700135 EXPECT_TRUE(array->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700136 EXPECT_TRUE(array->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700137 EXPECT_TRUE(array->IsVerified());
138 EXPECT_TRUE(array->IsInitialized());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700139 EXPECT_FALSE(array->IsArrayInstance());
140 EXPECT_TRUE(array->IsArrayClass());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700141 EXPECT_FALSE(array->IsInterface());
142 EXPECT_EQ(array->GetComponentType()->IsPublic(), array->IsPublic());
143 EXPECT_TRUE(array->IsFinal());
144 EXPECT_FALSE(array->IsPrimitive());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 EXPECT_FALSE(array->IsSynthetic());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700146 EXPECT_EQ(0U, array->NumDirectMethods());
147 EXPECT_EQ(0U, array->NumVirtualMethods());
148 EXPECT_EQ(0U, array->NumInstanceFields());
149 EXPECT_EQ(0U, array->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700150 EXPECT_EQ(2U, array->NumDirectInterfaces());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700151 EXPECT_TRUE(array->ShouldHaveEmbeddedImtAndVTable());
Brian Carlstrom86927212011-09-15 11:31:11 -0700152 EXPECT_EQ(2, array->GetIfTableCount());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700153 ASSERT_TRUE(array->GetIfTable() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700154 mirror::Class* direct_interface0 = mirror::Class::GetDirectInterface(self, array, 0);
155 EXPECT_TRUE(direct_interface0 != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700156 EXPECT_STREQ(direct_interface0->GetDescriptor(&temp), "Ljava/lang/Cloneable;");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700157 mirror::Class* direct_interface1 = mirror::Class::GetDirectInterface(self, array, 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700158 EXPECT_STREQ(direct_interface1->GetDescriptor(&temp), "Ljava/io/Serializable;");
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700159 mirror::Class* array_ptr = array->GetComponentType();
160 EXPECT_EQ(class_linker_->FindArrayClass(self, &array_ptr), array.Get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700161 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700162
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 void AssertMethod(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700164 EXPECT_TRUE(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 EXPECT_TRUE(method->GetDeclaringClass() != nullptr);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700166 EXPECT_TRUE(method->GetName() != nullptr);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700167 EXPECT_TRUE(method->GetSignature() != Signature::NoSignature());
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700168
Vladimir Marko05792b92015-08-03 11:56:49 +0100169 EXPECT_TRUE(method->HasDexCacheResolvedMethods(sizeof(void*)));
170 EXPECT_TRUE(method->HasDexCacheResolvedTypes(sizeof(void*)));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700171 EXPECT_TRUE(method->HasSameDexCacheResolvedMethods(
Vladimir Marko05792b92015-08-03 11:56:49 +0100172 method->GetDeclaringClass()->GetDexCache()->GetResolvedMethods(),
173 sizeof(void*)));
Andreas Gampe58a5af82014-07-31 16:23:49 -0700174 EXPECT_TRUE(method->HasSameDexCacheResolvedTypes(
Vladimir Marko05792b92015-08-03 11:56:49 +0100175 method->GetDeclaringClass()->GetDexCache()->GetResolvedTypes(),
176 sizeof(void*)));
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700177 }
178
Mathieu Chartierc7853442015-03-27 14:35:38 -0700179 void AssertField(mirror::Class* klass, ArtField* field)
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700181 EXPECT_TRUE(field != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700182 EXPECT_EQ(klass, field->GetDeclaringClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700183 EXPECT_TRUE(field->GetName() != nullptr);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700184 EXPECT_TRUE(field->GetType<true>() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700185 }
186
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700187 void AssertClass(const std::string& descriptor, Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700188 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 std::string temp;
190 EXPECT_STREQ(descriptor.c_str(), klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700192 EXPECT_FALSE(klass->HasSuperClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700193 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700194 EXPECT_TRUE(klass->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700195 EXPECT_TRUE(klass->GetSuperClass() != nullptr);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700196 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700197 EXPECT_TRUE(klass->GetClass() != nullptr);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700198 EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700199 EXPECT_TRUE(klass->GetDexCache() != nullptr);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700200 EXPECT_TRUE(klass->IsLoaded());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700201 EXPECT_TRUE(klass->IsResolved());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700202 EXPECT_FALSE(klass->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 EXPECT_FALSE(klass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700204 EXPECT_TRUE(klass->GetComponentType() == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 EXPECT_TRUE(klass->IsInSamePackage(klass.Get()));
Mathieu Chartiereace4582014-11-24 18:29:54 -0800206 EXPECT_TRUE(klass->GetDexCacheStrings() != nullptr);
207 EXPECT_EQ(klass->GetDexCacheStrings(), klass->GetDexCache()->GetStrings());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 std::string temp2;
209 EXPECT_TRUE(mirror::Class::IsInSamePackage(klass->GetDescriptor(&temp),
210 klass->GetDescriptor(&temp2)));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700211 if (klass->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700212 EXPECT_TRUE(klass->IsAbstract());
213 if (klass->NumDirectMethods() == 1) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsClassInitializer());
215 EXPECT_TRUE(klass->GetDirectMethod(0, sizeof(void*))->IsDirect());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700216 } else {
217 EXPECT_EQ(0U, klass->NumDirectMethods());
218 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700219 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700220 if (!klass->IsSynthetic()) {
221 EXPECT_NE(0U, klass->NumDirectMethods());
222 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700223 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700224 EXPECT_EQ(klass->IsInterface(), !klass->HasVTable());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 mirror::IfTable* iftable = klass->GetIfTable();
Brian Carlstrom86927212011-09-15 11:31:11 -0700226 for (int i = 0; i < klass->GetIfTableCount(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700227 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700228 ASSERT_TRUE(interface != nullptr);
Brian Carlstrom86927212011-09-15 11:31:11 -0700229 if (klass->IsInterface()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700230 EXPECT_EQ(0U, iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700231 } else {
Alex Light705ad492015-09-21 11:36:30 -0700232 EXPECT_EQ(interface->NumDeclaredVirtualMethods(), iftable->GetMethodArrayCount(i));
Brian Carlstrom86927212011-09-15 11:31:11 -0700233 }
234 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700235 if (klass->IsAbstract()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700236 EXPECT_FALSE(klass->IsFinal());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700237 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700238 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700239 }
240 if (klass->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700241 EXPECT_FALSE(klass->IsAbstract());
242 EXPECT_FALSE(klass->IsAnnotation());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700243 }
244 if (klass->IsAnnotation()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700245 EXPECT_FALSE(klass->IsFinal());
246 EXPECT_TRUE(klass->IsAbstract());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700247 }
248
249 EXPECT_FALSE(klass->IsPrimitive());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700250 EXPECT_TRUE(klass->CanAccess(klass.Get()));
Brian Carlstromae3ac012011-07-27 01:30:28 -0700251
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 for (ArtMethod& method : klass->GetDirectMethods(sizeof(void*))) {
253 AssertMethod(&method);
254 EXPECT_TRUE(method.IsDirect());
255 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700256 }
257
Alex Lighte64300b2015-12-15 15:02:47 -0800258 for (ArtMethod& method : klass->GetDeclaredVirtualMethods(sizeof(void*))) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 AssertMethod(&method);
260 EXPECT_FALSE(method.IsDirect());
Alex Lighte64300b2015-12-15 15:02:47 -0800261 EXPECT_EQ(klass.Get(), method.GetDeclaringClass());
262 }
263
264 for (ArtMethod& method : klass->GetCopiedMethods(sizeof(void*))) {
265 AssertMethod(&method);
266 EXPECT_FALSE(method.IsDirect());
267 EXPECT_TRUE(method.IsMiranda() || method.IsDefault() || method.IsDefaultConflicting());
268 EXPECT_TRUE(method.GetDeclaringClass()->IsInterface())
269 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
270 EXPECT_TRUE(method.GetDeclaringClass()->IsAssignableFrom(klass.Get()))
271 << "declaring class: " << PrettyClass(method.GetDeclaringClass());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700272 }
273
274 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700275 ArtField* field = klass->GetInstanceField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700276 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700277 EXPECT_FALSE(field->IsStatic());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700278 }
279
280 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700281 ArtField* field = klass->GetStaticField(i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 AssertField(klass.Get(), field);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700283 EXPECT_TRUE(field->IsStatic());
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700284 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700285
Vladimir Marko76649e82014-11-10 18:32:59 +0000286 // Confirm that all instances field offsets are packed together at the start.
Brian Carlstromae3ac012011-07-27 01:30:28 -0700287 EXPECT_GE(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Vladimir Marko76649e82014-11-10 18:32:59 +0000288 MemberOffset start_ref_offset = klass->GetFirstReferenceInstanceFieldOffset();
289 MemberOffset end_ref_offset(start_ref_offset.Uint32Value() +
290 klass->NumReferenceInstanceFields() *
291 sizeof(mirror::HeapReference<mirror::Object>));
292 MemberOffset current_ref_offset = start_ref_offset;
293 for (size_t i = 0; i < klass->NumInstanceFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294 ArtField* field = klass->GetInstanceField(i);
295 mirror::Class* field_type = field->GetType<true>();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700296 ASSERT_TRUE(field_type != nullptr);
Vladimir Marko76649e82014-11-10 18:32:59 +0000297 if (!field->IsPrimitiveType()) {
298 ASSERT_TRUE(!field_type->IsPrimitive());
299 ASSERT_EQ(current_ref_offset.Uint32Value(), field->GetOffset().Uint32Value());
300 if (current_ref_offset.Uint32Value() == end_ref_offset.Uint32Value()) {
301 // While Reference.referent is not primitive, the ClassLinker
302 // treats it as such so that the garbage collector won't scan it.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303 EXPECT_EQ(PrettyField(field),
Vladimir Marko76649e82014-11-10 18:32:59 +0000304 "java.lang.Object java.lang.ref.Reference.referent");
305 } else {
306 current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() +
307 sizeof(mirror::HeapReference<mirror::Object>));
308 }
309 } else {
310 if (field->GetOffset().Uint32Value() < end_ref_offset.Uint32Value()) {
311 // Shuffled before references.
312 ASSERT_LT(field->GetOffset().Uint32Value(), start_ref_offset.Uint32Value());
313 CHECK(!IsAligned<4>(field->GetOffset().Uint32Value()));
314 }
Brian Carlstromfbfdce62011-10-05 17:33:32 -0700315 }
Brian Carlstromae3ac012011-07-27 01:30:28 -0700316 }
Vladimir Marko76649e82014-11-10 18:32:59 +0000317 ASSERT_EQ(end_ref_offset.Uint32Value(), current_ref_offset.Uint32Value());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700318
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700319 uint32_t total_num_reference_instance_fields = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700320 mirror::Class* k = klass.Get();
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700321 while (k != nullptr) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 total_num_reference_instance_fields += k->NumReferenceInstanceFields();
323 k = k->GetSuperClass();
Brian Carlstromae3ac012011-07-27 01:30:28 -0700324 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700325 EXPECT_GE(total_num_reference_instance_fields, 1U); // Should always have Object's class.
326 if (klass->GetReferenceInstanceOffsets() != mirror::Class::kClassWalkSuper) {
327 // The reference instance offsets have a bit set for each reference offset.
328 // +1 for Object's class.
329 EXPECT_EQ(static_cast<uint32_t>(POPCOUNT(klass->GetReferenceInstanceOffsets())) + 1,
330 total_num_reference_instance_fields);
331 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700332 }
333
Brian Carlstromea46f952013-07-30 01:26:50 -0700334 void AssertDexFileClass(mirror::ClassLoader* class_loader, const std::string& descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700335 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700336 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800337 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700338 StackHandleScope<1> hs(self);
339 Handle<mirror::Class> klass(
340 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
341 ASSERT_TRUE(klass.Get() != nullptr);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700342 std::string temp;
343 EXPECT_STREQ(descriptor.c_str(), klass.Get()->GetDescriptor(&temp));
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700344 EXPECT_EQ(class_loader, klass->GetClassLoader());
345 if (klass->IsPrimitive()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700346 AssertPrimitiveClass(descriptor, klass.Get());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700347 } else if (klass->IsArrayClass()) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700348 AssertArrayClass(descriptor, klass);
349 } else {
350 AssertClass(descriptor, klass);
351 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700352 }
353
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800354 void AssertDexFile(const DexFile& dex, mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700355 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700356 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800357 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
358 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
359 const char* descriptor = dex.GetClassDescriptor(class_def);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700360 AssertDexFileClass(class_loader, descriptor);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700361 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700362 // Verify all the types referenced by this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800363 for (size_t i = 0; i < dex.NumTypeIds(); i++) {
364 const DexFile::TypeId& type_id = dex.GetTypeId(i);
365 const char* descriptor = dex.GetTypeDescriptor(type_id);
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700366 AssertDexFileClass(class_loader, descriptor);
367 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700368 TestRootVisitor visitor;
369 class_linker_->VisitRoots(&visitor, kVisitRootFlagAllRoots);
Ian Rogers19846512012-02-24 11:42:47 -0800370 // Verify the dex cache has resolution methods in all resolved method slots
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700371 mirror::DexCache* dex_cache = class_linker_->FindDexCache(Thread::Current(), dex);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 auto* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100373 for (size_t i = 0, num_methods = dex_cache->NumResolvedMethods(); i != num_methods; ++i) {
374 EXPECT_TRUE(
375 mirror::DexCache::GetElementPtrSize(resolved_methods, i, sizeof(void*)) != nullptr)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700376 << dex.GetLocation() << " i=" << i;
Ian Rogers19846512012-02-24 11:42:47 -0800377 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700378 }
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700379
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700380 class TestRootVisitor : public SingleRootVisitor {
381 public:
382 void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE {
383 EXPECT_TRUE(root != nullptr);
384 }
385 };
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700386};
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700387
Brian Carlstrom693267a2011-09-06 09:25:34 -0700388struct CheckOffset {
389 size_t cpp_offset;
390 const char* java_name;
391 CheckOffset(size_t c, const char* j) : cpp_offset(c), java_name(j) {}
392};
393
Elliott Hughes80609252011-09-23 17:24:51 -0700394template <typename T>
Brian Carlstrom693267a2011-09-06 09:25:34 -0700395struct CheckOffsets {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800396 CheckOffsets(bool is_static_in, const char* class_descriptor_in)
397 : is_static(is_static_in), class_descriptor(class_descriptor_in) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700398 bool is_static;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700399 std::string class_descriptor;
400 std::vector<CheckOffset> offsets;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700401
Mathieu Chartier90443472015-07-16 20:32:27 -0700402 bool Check() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers98379392014-02-24 16:53:16 -0800403 Thread* self = Thread::Current();
404 mirror::Class* klass =
405 Runtime::Current()->GetClassLinker()->FindSystemClass(self, class_descriptor.c_str());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700406 CHECK(klass != nullptr) << class_descriptor;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700407
408 bool error = false;
409
Mathieu Chartiere401d142015-04-22 13:56:20 -0700410 // Classes have a different size due to padding field. Strings are variable length.
411 if (!klass->IsClassClass() && !klass->IsStringClass() && !is_static) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700412 // Currently only required for AccessibleObject since of the padding fields. The class linker
413 // says AccessibleObject is 9 bytes but sizeof(AccessibleObject) is 12 bytes due to padding.
414 // The RoundUp is to get around this case.
415 static constexpr size_t kPackAlignment = 4;
416 size_t expected_size = RoundUp(is_static ? klass->GetClassSize(): klass->GetObjectSize(),
417 kPackAlignment);
Elliott Hughes80609252011-09-23 17:24:51 -0700418 if (sizeof(T) != expected_size) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800419 LOG(ERROR) << "Class size mismatch:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700420 << " class=" << class_descriptor
Elliott Hughes80609252011-09-23 17:24:51 -0700421 << " Java=" << expected_size
422 << " C++=" << sizeof(T);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700423 error = true;
424 }
425 }
426
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700427 size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700428 if (offsets.size() != num_fields) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800429 LOG(ERROR) << "Field count mismatch:"
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700430 << " class=" << class_descriptor
431 << " Java=" << num_fields
432 << " C++=" << offsets.size();
Brian Carlstrom693267a2011-09-06 09:25:34 -0700433 error = true;
434 }
435
Brian Carlstromdbc05252011-09-09 01:59:59 -0700436 for (size_t i = 0; i < offsets.size(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700437 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700438 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 if (field_name != offsets[i].java_name) {
Brian Carlstrom693267a2011-09-06 09:25:34 -0700440 error = true;
441 }
442 }
443 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700444 for (size_t i = 0; i < offsets.size(); i++) {
445 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700446 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700447 StringPiece field_name(field->GetName());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 if (field_name != offsets[i].java_name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800449 LOG(ERROR) << "JAVA FIELD ORDER MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700450 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800451 LOG(ERROR) << "Java field order:"
Brian Carlstromdbc05252011-09-09 01:59:59 -0700452 << " i=" << i << " class=" << class_descriptor
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800453 << " Java=" << field_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700454 << " CheckOffsets=" << offset.java_name;
455 }
456 }
457
Brian Carlstromdbc05252011-09-09 01:59:59 -0700458 for (size_t i = 0; i < offsets.size(); i++) {
459 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700460 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700461 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
462 error = true;
463 }
464 }
465 if (error) {
Brian Carlstromdbc05252011-09-09 01:59:59 -0700466 for (size_t i = 0; i < offsets.size(); i++) {
467 CheckOffset& offset = offsets[i];
Mathieu Chartierc7853442015-03-27 14:35:38 -0700468 ArtField* field = is_static ? klass->GetStaticField(i) : klass->GetInstanceField(i);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700469 if (field->GetOffset().Uint32Value() != offset.cpp_offset) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800470 LOG(ERROR) << "OFFSET MISMATCH NEXT LINE:";
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800472 LOG(ERROR) << "Offset: class=" << class_descriptor << " field=" << offset.java_name
Brian Carlstrom693267a2011-09-06 09:25:34 -0700473 << " Java=" << field->GetOffset().Uint32Value() << " C++=" << offset.cpp_offset;
474 }
475 }
476
477 return !error;
478 };
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700479
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700480 void addOffset(size_t offset, const char* name) {
481 offsets.push_back(CheckOffset(offset, name));
482 }
483
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700484 private:
485 DISALLOW_IMPLICIT_CONSTRUCTORS(CheckOffsets);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700486};
487
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700488// Note that ClassLinkerTest.ValidateFieldOrderOfJavaCppUnionClasses
489// is first since if it is failing, others are unlikely to succeed.
490
Brian Carlstromea46f952013-07-30 01:26:50 -0700491struct ObjectOffsets : public CheckOffsets<mirror::Object> {
492 ObjectOffsets() : CheckOffsets<mirror::Object>(false, "Ljava/lang/Object;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700493 addOffset(OFFSETOF_MEMBER(mirror::Object, klass_), "shadow$_klass_");
494 addOffset(OFFSETOF_MEMBER(mirror::Object, monitor_), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700495#ifdef USE_BROOKS_READ_BARRIER
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700496 addOffset(OFFSETOF_MEMBER(mirror::Object, x_rb_ptr_), "shadow$_x_rb_ptr_");
497 addOffset(OFFSETOF_MEMBER(mirror::Object, x_xpadding_), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800498#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700499 };
500};
501
Brian Carlstromea46f952013-07-30 01:26:50 -0700502struct ClassOffsets : public CheckOffsets<mirror::Class> {
503 ClassOffsets() : CheckOffsets<mirror::Class>(false, "Ljava/lang/Class;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700504 addOffset(OFFSETOF_MEMBER(mirror::Class, access_flags_), "accessFlags");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000505 addOffset(OFFSETOF_MEMBER(mirror::Class, annotation_type_), "annotationType");
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700506 addOffset(OFFSETOF_MEMBER(mirror::Class, class_flags_), "classFlags");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700507 addOffset(OFFSETOF_MEMBER(mirror::Class, class_loader_), "classLoader");
508 addOffset(OFFSETOF_MEMBER(mirror::Class, class_size_), "classSize");
509 addOffset(OFFSETOF_MEMBER(mirror::Class, clinit_thread_id_), "clinitThreadId");
510 addOffset(OFFSETOF_MEMBER(mirror::Class, component_type_), "componentType");
Alex Lighte64300b2015-12-15 15:02:47 -0800511 addOffset(OFFSETOF_MEMBER(mirror::Class, copied_methods_offset_), "copiedMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700512 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_), "dexCache");
513 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_cache_strings_), "dexCacheStrings");
514 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_class_def_idx_), "dexClassDefIndex");
515 addOffset(OFFSETOF_MEMBER(mirror::Class, dex_type_idx_), "dexTypeIndex");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700516 addOffset(OFFSETOF_MEMBER(mirror::Class, ifields_), "iFields");
517 addOffset(OFFSETOF_MEMBER(mirror::Class, iftable_), "ifTable");
Alex Lighte64300b2015-12-15 15:02:47 -0800518 addOffset(OFFSETOF_MEMBER(mirror::Class, methods_), "methods");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700519 addOffset(OFFSETOF_MEMBER(mirror::Class, name_), "name");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700520 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_instance_fields_),
521 "numReferenceInstanceFields");
522 addOffset(OFFSETOF_MEMBER(mirror::Class, num_reference_static_fields_),
523 "numReferenceStaticFields");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700524 addOffset(OFFSETOF_MEMBER(mirror::Class, object_size_), "objectSize");
525 addOffset(OFFSETOF_MEMBER(mirror::Class, primitive_type_), "primitiveType");
526 addOffset(OFFSETOF_MEMBER(mirror::Class, reference_instance_offsets_),
527 "referenceInstanceOffsets");
528 addOffset(OFFSETOF_MEMBER(mirror::Class, sfields_), "sFields");
529 addOffset(OFFSETOF_MEMBER(mirror::Class, status_), "status");
530 addOffset(OFFSETOF_MEMBER(mirror::Class, super_class_), "superClass");
Andreas Gampe99babb62015-11-02 16:20:00 -0800531 addOffset(OFFSETOF_MEMBER(mirror::Class, verify_error_), "verifyError");
Alex Lighte64300b2015-12-15 15:02:47 -0800532 addOffset(OFFSETOF_MEMBER(mirror::Class, virtual_methods_offset_), "virtualMethodsOffset");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700533 addOffset(OFFSETOF_MEMBER(mirror::Class, vtable_), "vtable");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700534 };
535};
536
Brian Carlstromea46f952013-07-30 01:26:50 -0700537struct StringOffsets : public CheckOffsets<mirror::String> {
538 StringOffsets() : CheckOffsets<mirror::String>(false, "Ljava/lang/String;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700539 addOffset(OFFSETOF_MEMBER(mirror::String, count_), "count");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000540 addOffset(OFFSETOF_MEMBER(mirror::String, hash_code_), "hash");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541 };
542};
543
Brian Carlstromea46f952013-07-30 01:26:50 -0700544struct ThrowableOffsets : public CheckOffsets<mirror::Throwable> {
545 ThrowableOffsets() : CheckOffsets<mirror::Throwable>(false, "Ljava/lang/Throwable;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000546 addOffset(OFFSETOF_MEMBER(mirror::Throwable, backtrace_), "backtrace");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700547 addOffset(OFFSETOF_MEMBER(mirror::Throwable, cause_), "cause");
548 addOffset(OFFSETOF_MEMBER(mirror::Throwable, detail_message_), "detailMessage");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700549 addOffset(OFFSETOF_MEMBER(mirror::Throwable, stack_trace_), "stackTrace");
550 addOffset(OFFSETOF_MEMBER(mirror::Throwable, suppressed_exceptions_), "suppressedExceptions");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551 };
552};
553
Brian Carlstromea46f952013-07-30 01:26:50 -0700554struct StackTraceElementOffsets : public CheckOffsets<mirror::StackTraceElement> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700555 StackTraceElementOffsets() : CheckOffsets<mirror::StackTraceElement>(
556 false, "Ljava/lang/StackTraceElement;") {
557 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, declaring_class_), "declaringClass");
558 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, file_name_), "fileName");
559 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, line_number_), "lineNumber");
560 addOffset(OFFSETOF_MEMBER(mirror::StackTraceElement, method_name_), "methodName");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700561 };
562};
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564struct ClassLoaderOffsets : public CheckOffsets<mirror::ClassLoader> {
565 ClassLoaderOffsets() : CheckOffsets<mirror::ClassLoader>(false, "Ljava/lang/ClassLoader;") {
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700566 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, allocator_), "allocator");
Mathieu Chartier6b069532015-08-05 15:08:12 -0700567 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, class_table_), "classTable");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700568 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, packages_), "packages");
569 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, parent_), "parent");
570 addOffset(OFFSETOF_MEMBER(mirror::ClassLoader, proxyCache_), "proxyCache");
Brian Carlstrom693267a2011-09-06 09:25:34 -0700571 };
572};
573
Brian Carlstromea46f952013-07-30 01:26:50 -0700574struct ProxyOffsets : public CheckOffsets<mirror::Proxy> {
575 ProxyOffsets() : CheckOffsets<mirror::Proxy>(false, "Ljava/lang/reflect/Proxy;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700576 addOffset(OFFSETOF_MEMBER(mirror::Proxy, h_), "h");
Jesse Wilson95caa792011-10-12 18:14:17 -0400577 };
578};
579
Brian Carlstromea46f952013-07-30 01:26:50 -0700580struct DexCacheOffsets : public CheckOffsets<mirror::DexCache> {
581 DexCacheOffsets() : CheckOffsets<mirror::DexCache>(false, "Ljava/lang/DexCache;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700582 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_), "dex");
583 addOffset(OFFSETOF_MEMBER(mirror::DexCache, dex_file_), "dexFile");
584 addOffset(OFFSETOF_MEMBER(mirror::DexCache, location_), "location");
Vladimir Marko05792b92015-08-03 11:56:49 +0100585 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_fields_), "numResolvedFields");
586 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_methods_), "numResolvedMethods");
587 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_resolved_types_), "numResolvedTypes");
588 addOffset(OFFSETOF_MEMBER(mirror::DexCache, num_strings_), "numStrings");
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700589 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_fields_), "resolvedFields");
590 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_methods_), "resolvedMethods");
591 addOffset(OFFSETOF_MEMBER(mirror::DexCache, resolved_types_), "resolvedTypes");
592 addOffset(OFFSETOF_MEMBER(mirror::DexCache, strings_), "strings");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700593 };
594};
595
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700596struct ReferenceOffsets : public CheckOffsets<mirror::Reference> {
597 ReferenceOffsets() : CheckOffsets<mirror::Reference>(false, "Ljava/lang/ref/Reference;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700598 addOffset(OFFSETOF_MEMBER(mirror::Reference, pending_next_), "pendingNext");
599 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_), "queue");
600 addOffset(OFFSETOF_MEMBER(mirror::Reference, queue_next_), "queueNext");
601 addOffset(OFFSETOF_MEMBER(mirror::Reference, referent_), "referent");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700602 };
603};
604
605struct FinalizerReferenceOffsets : public CheckOffsets<mirror::FinalizerReference> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700606 FinalizerReferenceOffsets() : CheckOffsets<mirror::FinalizerReference>(
607 false, "Ljava/lang/ref/FinalizerReference;") {
608 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, next_), "next");
609 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, prev_), "prev");
610 addOffset(OFFSETOF_MEMBER(mirror::FinalizerReference, zombie_), "zombie");
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700611 };
612};
613
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700614struct AccessibleObjectOffsets : public CheckOffsets<mirror::AccessibleObject> {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700615 AccessibleObjectOffsets() : CheckOffsets<mirror::AccessibleObject>(
616 false, "Ljava/lang/reflect/AccessibleObject;") {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000617 addOffset(mirror::AccessibleObject::FlagOffset().Uint32Value(), "override");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700618 };
619};
620
621struct FieldOffsets : public CheckOffsets<mirror::Field> {
622 FieldOffsets() : CheckOffsets<mirror::Field>(false, "Ljava/lang/reflect/Field;") {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700623 addOffset(OFFSETOF_MEMBER(mirror::Field, access_flags_), "accessFlags");
624 addOffset(OFFSETOF_MEMBER(mirror::Field, declaring_class_), "declaringClass");
625 addOffset(OFFSETOF_MEMBER(mirror::Field, dex_field_index_), "dexFieldIndex");
626 addOffset(OFFSETOF_MEMBER(mirror::Field, offset_), "offset");
627 addOffset(OFFSETOF_MEMBER(mirror::Field, type_), "type");
628 };
629};
630
631struct AbstractMethodOffsets : public CheckOffsets<mirror::AbstractMethod> {
632 AbstractMethodOffsets() : CheckOffsets<mirror::AbstractMethod>(
633 false, "Ljava/lang/reflect/AbstractMethod;") {
634 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, access_flags_), "accessFlags");
635 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, art_method_), "artMethod");
636 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_), "declaringClass");
637 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, declaring_class_of_overridden_method_),
638 "declaringClassOfOverriddenMethod");
639 addOffset(OFFSETOF_MEMBER(mirror::AbstractMethod, dex_method_index_), "dexMethodIndex");
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700640 };
641};
642
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400643// C++ fields must exactly match the fields in the Java classes. If this fails,
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400644// reorder the fields in the C++ class. Managed class fields are ordered by
Brian Carlstrom693267a2011-09-06 09:25:34 -0700645// ClassLinker::LinkFields.
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400646TEST_F(ClassLinkerTest, ValidateFieldOrderOfJavaCppUnionClasses) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700648 EXPECT_TRUE(ObjectOffsets().Check());
Brian Carlstrom693267a2011-09-06 09:25:34 -0700649 EXPECT_TRUE(ClassOffsets().Check());
650 EXPECT_TRUE(StringOffsets().Check());
651 EXPECT_TRUE(ThrowableOffsets().Check());
652 EXPECT_TRUE(StackTraceElementOffsets().Check());
653 EXPECT_TRUE(ClassLoaderOffsets().Check());
Jesse Wilson95caa792011-10-12 18:14:17 -0400654 EXPECT_TRUE(ProxyOffsets().Check());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700655 EXPECT_TRUE(DexCacheOffsets().Check());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700656 EXPECT_TRUE(ReferenceOffsets().Check());
657 EXPECT_TRUE(FinalizerReferenceOffsets().Check());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700658 EXPECT_TRUE(AccessibleObjectOffsets().Check());
659 EXPECT_TRUE(FieldOffsets().Check());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700660 EXPECT_TRUE(AbstractMethodOffsets().Check());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700661}
662
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700663TEST_F(ClassLinkerTest, FindClassNonexistent) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700665 AssertNonExistentClass("NoSuchClass;");
666 AssertNonExistentClass("LNoSuchClass;");
667}
668
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700669TEST_F(ClassLinkerTest, GetDexFiles) {
670 ScopedObjectAccess soa(Thread::Current());
671
672 jobject jclass_loader = LoadDex("Nested");
673 std::vector<const DexFile*> dex_files(GetDexFiles(jclass_loader));
674 ASSERT_EQ(dex_files.size(), 1U);
675 EXPECT_TRUE(EndsWith(dex_files[0]->GetLocation(), "Nested.jar"));
676
677 jobject jclass_loader2 = LoadDex("MultiDex");
678 std::vector<const DexFile*> dex_files2(GetDexFiles(jclass_loader2));
679 ASSERT_EQ(dex_files2.size(), 2U);
680 EXPECT_TRUE(EndsWith(dex_files2[0]->GetLocation(), "MultiDex.jar"));
681}
682
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700683TEST_F(ClassLinkerTest, FindClassNested) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700685 StackHandleScope<1> hs(soa.Self());
686 Handle<mirror::ClassLoader> class_loader(
687 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Nested"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700688
Ian Rogers98379392014-02-24 16:53:16 -0800689 mirror::Class* outer = class_linker_->FindClass(soa.Self(), "LNested;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700690 ASSERT_TRUE(outer != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700691 EXPECT_EQ(0U, outer->NumVirtualMethods());
692 EXPECT_EQ(1U, outer->NumDirectMethods());
693
Ian Rogers98379392014-02-24 16:53:16 -0800694 mirror::Class* inner = class_linker_->FindClass(soa.Self(), "LNested$Inner;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700695 ASSERT_TRUE(inner != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700696 EXPECT_EQ(0U, inner->NumVirtualMethods());
697 EXPECT_EQ(1U, inner->NumDirectMethods());
698}
699
700TEST_F(ClassLinkerTest, FindClass_Primitives) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700702 const std::string expected("BCDFIJSZV");
Elliott Hughesdb7d5e92011-12-16 18:47:37 -0800703 for (int ch = 1; ch < 256; ++ch) {
704 std::string descriptor;
705 descriptor.push_back(ch);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700706 if (expected.find(ch) == std::string::npos) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700707 AssertNonExistentClass(descriptor);
708 } else {
709 AssertPrimitiveClass(descriptor);
710 }
711 }
712}
713
714TEST_F(ClassLinkerTest, FindClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800716 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700717 ASSERT_TRUE(JavaLangObject != nullptr);
718 ASSERT_TRUE(JavaLangObject->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700719 ASSERT_EQ(JavaLangObject->GetClass(), JavaLangObject->GetClass()->GetClass());
720 EXPECT_EQ(JavaLangObject, JavaLangObject->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700721 std::string temp;
722 ASSERT_STREQ(JavaLangObject->GetDescriptor(&temp), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700723 EXPECT_TRUE(JavaLangObject->GetSuperClass() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700724 EXPECT_FALSE(JavaLangObject->HasSuperClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700725 EXPECT_TRUE(JavaLangObject->GetClassLoader() == nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700726 EXPECT_EQ(mirror::Class::kStatusInitialized, JavaLangObject->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700727 EXPECT_FALSE(JavaLangObject->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700728 EXPECT_TRUE(JavaLangObject->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700729 EXPECT_TRUE(JavaLangObject->IsResolved());
Brian Carlstromea46f952013-07-30 01:26:50 -0700730 EXPECT_TRUE(JavaLangObject->IsVerified());
731 EXPECT_TRUE(JavaLangObject->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700732 EXPECT_FALSE(JavaLangObject->IsArrayInstance());
733 EXPECT_FALSE(JavaLangObject->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700734 EXPECT_TRUE(JavaLangObject->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700735 EXPECT_FALSE(JavaLangObject->IsInterface());
736 EXPECT_TRUE(JavaLangObject->IsPublic());
737 EXPECT_FALSE(JavaLangObject->IsFinal());
738 EXPECT_FALSE(JavaLangObject->IsPrimitive());
739 EXPECT_FALSE(JavaLangObject->IsSynthetic());
740 EXPECT_EQ(2U, JavaLangObject->NumDirectMethods());
741 EXPECT_EQ(11U, JavaLangObject->NumVirtualMethods());
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700742 if (!kUseBrooksReadBarrier) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800743 EXPECT_EQ(2U, JavaLangObject->NumInstanceFields());
744 } else {
745 EXPECT_EQ(4U, JavaLangObject->NumInstanceFields());
746 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700747 EXPECT_STREQ(JavaLangObject->GetInstanceField(0)->GetName(), "shadow$_klass_");
748 EXPECT_STREQ(JavaLangObject->GetInstanceField(1)->GetName(), "shadow$_monitor_");
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700749 if (kUseBrooksReadBarrier) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700750 EXPECT_STREQ(JavaLangObject->GetInstanceField(2)->GetName(), "shadow$_x_rb_ptr_");
751 EXPECT_STREQ(JavaLangObject->GetInstanceField(3)->GetName(), "shadow$_x_xpadding_");
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800752 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700753
754 EXPECT_EQ(0U, JavaLangObject->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700755 EXPECT_EQ(0U, JavaLangObject->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700756
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700757 StackHandleScope<1> hs(soa.Self());
758 Handle<mirror::ClassLoader> class_loader(
759 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700760 AssertNonExistentClass("LMyClass;");
Ian Rogers98379392014-02-24 16:53:16 -0800761 mirror::Class* MyClass = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700762 ASSERT_TRUE(MyClass != nullptr);
763 ASSERT_TRUE(MyClass->GetClass() != nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700764 ASSERT_EQ(MyClass->GetClass(), MyClass->GetClass()->GetClass());
765 EXPECT_EQ(JavaLangObject, MyClass->GetClass()->GetSuperClass());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700766 ASSERT_STREQ(MyClass->GetDescriptor(&temp), "LMyClass;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700767 EXPECT_TRUE(MyClass->GetSuperClass() == JavaLangObject);
768 EXPECT_TRUE(MyClass->HasSuperClass());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700769 EXPECT_EQ(class_loader.Get(), MyClass->GetClassLoader());
Brian Carlstromea46f952013-07-30 01:26:50 -0700770 EXPECT_EQ(mirror::Class::kStatusResolved, MyClass->GetStatus());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700771 EXPECT_FALSE(MyClass->IsErroneous());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700772 EXPECT_TRUE(MyClass->IsLoaded());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700773 EXPECT_TRUE(MyClass->IsResolved());
Brian Carlstrom25c33252011-09-18 15:58:35 -0700774 EXPECT_FALSE(MyClass->IsVerified());
775 EXPECT_FALSE(MyClass->IsInitialized());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700776 EXPECT_FALSE(MyClass->IsArrayInstance());
777 EXPECT_FALSE(MyClass->IsArrayClass());
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700778 EXPECT_TRUE(MyClass->GetComponentType() == nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700779 EXPECT_FALSE(MyClass->IsInterface());
780 EXPECT_FALSE(MyClass->IsPublic());
781 EXPECT_FALSE(MyClass->IsFinal());
782 EXPECT_FALSE(MyClass->IsPrimitive());
783 EXPECT_FALSE(MyClass->IsSynthetic());
784 EXPECT_EQ(1U, MyClass->NumDirectMethods());
785 EXPECT_EQ(0U, MyClass->NumVirtualMethods());
786 EXPECT_EQ(0U, MyClass->NumInstanceFields());
787 EXPECT_EQ(0U, MyClass->NumStaticFields());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700788 EXPECT_EQ(0U, MyClass->NumDirectInterfaces());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700789
790 EXPECT_EQ(JavaLangObject->GetClass()->GetClass(), MyClass->GetClass()->GetClass());
791
792 // created by class_linker
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700793 AssertArrayClass("[C", "C", nullptr);
794 AssertArrayClass("[Ljava/lang/Object;", "Ljava/lang/Object;", nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700795 // synthesized on the fly
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700796 AssertArrayClass("[[C", "[C", nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700797 AssertArrayClass("[[[LMyClass;", "[[LMyClass;", class_loader.Get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700798 // or not available at all
799 AssertNonExistentClass("[[[[LNonExistentClass;");
800}
801
802TEST_F(ClassLinkerTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800804 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
805 AssertDexFile(*java_lang_dex_file_, nullptr);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700806}
807
Ian Rogersa15e67d2012-02-28 13:51:55 -0800808// The first reference array element must be a multiple of 4 bytes from the
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400809// start of the object
810TEST_F(ClassLinkerTest, ValidateObjectArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800812 mirror::Class* array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700813 mirror::ObjectArray<mirror::String>* array =
814 mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), array_class, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800815 uintptr_t data_offset =
816 reinterpret_cast<uintptr_t>(array->GetRawData(sizeof(mirror::HeapReference<mirror::String>),
817 0));
818 if (sizeof(mirror::HeapReference<mirror::String>) == sizeof(int32_t)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -0800819 EXPECT_TRUE(IsAligned<4>(data_offset)); // Check 4 byte alignment.
820 } else {
821 EXPECT_TRUE(IsAligned<8>(data_offset)); // Check 8 byte alignment.
822 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400823}
824
825TEST_F(ClassLinkerTest, ValidatePrimitiveArrayElementsOffset) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700827 StackHandleScope<5> hs(soa.Self());
828 Handle<mirror::LongArray> long_array(hs.NewHandle(mirror::LongArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800829 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[J"), long_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800830 uintptr_t data_offset = reinterpret_cast<uintptr_t>(long_array->GetData());
831 EXPECT_TRUE(IsAligned<8>(data_offset)); // Longs require 8 byte alignment
832
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700833 Handle<mirror::DoubleArray> double_array(hs.NewHandle(mirror::DoubleArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800834 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[D"), double_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800835 data_offset = reinterpret_cast<uintptr_t>(double_array->GetData());
836 EXPECT_TRUE(IsAligned<8>(data_offset)); // Doubles require 8 byte alignment
837
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700838 Handle<mirror::IntArray> int_array(hs.NewHandle(mirror::IntArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800839 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[I"), int_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800840 data_offset = reinterpret_cast<uintptr_t>(int_array->GetData());
841 EXPECT_TRUE(IsAligned<4>(data_offset)); // Ints require 4 byte alignment
842
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700843 Handle<mirror::CharArray> char_array(hs.NewHandle(mirror::CharArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800844 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[C"), char_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800845 data_offset = reinterpret_cast<uintptr_t>(char_array->GetData());
846 EXPECT_TRUE(IsAligned<2>(data_offset)); // Chars require 2 byte alignment
847
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700848 Handle<mirror::ShortArray> short_array(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0)));
Ian Rogers98379392014-02-24 16:53:16 -0800849 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "[S"), short_array->GetClass());
Ian Rogersa15e67d2012-02-28 13:51:55 -0800850 data_offset = reinterpret_cast<uintptr_t>(short_array->GetData());
851 EXPECT_TRUE(IsAligned<2>(data_offset)); // Shorts require 2 byte alignment
852
853 // Take it as given that bytes and booleans have byte alignment
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400854}
855
Elliott Hughes33203b52011-09-20 19:42:01 -0700856TEST_F(ClassLinkerTest, ValidateBoxedTypes) {
857 // Validate that the "value" field is always the 0th field in each of java.lang's box classes.
858 // This lets UnboxPrimitive avoid searching for the field by name at runtime.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800860 ScopedNullHandle<mirror::ClassLoader> class_loader;
Brian Carlstromea46f952013-07-30 01:26:50 -0700861 mirror::Class* c;
Ian Rogers98379392014-02-24 16:53:16 -0800862 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700863 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800864 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Byte;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700865 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800866 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Character;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700867 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800868 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Double;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700869 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800870 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Float;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700871 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800872 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Integer;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700873 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800874 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Long;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700875 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Ian Rogers98379392014-02-24 16:53:16 -0800876 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Short;", class_loader);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700877 EXPECT_STREQ("value", c->GetIFieldsPtr()->At(0).GetName());
Elliott Hughes33203b52011-09-20 19:42:01 -0700878}
879
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700880TEST_F(ClassLinkerTest, TwoClassLoadersOneClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700881 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700882 StackHandleScope<2> hs(soa.Self());
883 Handle<mirror::ClassLoader> class_loader_1(
884 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
885 Handle<mirror::ClassLoader> class_loader_2(
886 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("MyClass"))));
Ian Rogers98379392014-02-24 16:53:16 -0800887 mirror::Class* MyClass_1 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_1);
888 mirror::Class* MyClass_2 = class_linker_->FindClass(soa.Self(), "LMyClass;", class_loader_2);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700889 EXPECT_TRUE(MyClass_1 != nullptr);
890 EXPECT_TRUE(MyClass_2 != nullptr);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700891 EXPECT_NE(MyClass_1, MyClass_2);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400892}
893
Jesse Wilson7833bd22011-08-09 18:31:44 -0400894TEST_F(ClassLinkerTest, StaticFields) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700896 StackHandleScope<2> hs(soa.Self());
897 Handle<mirror::ClassLoader> class_loader(
898 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
899 Handle<mirror::Class> statics(
900 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
Ian Rogers7b078e82014-09-10 14:44:24 -0700901 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400902
jeffhaoabcfde32011-09-29 15:05:18 -0700903 // Static final primitives that are initialized by a compile-time constant
904 // expression resolve to a copy of a constant value from the constant pool.
905 // So <clinit> should be null.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 ArtMethod* clinit = statics->FindDirectMethod("<clinit>", "()V", sizeof(void*));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700907 EXPECT_TRUE(clinit == nullptr);
jeffhaoabcfde32011-09-29 15:05:18 -0700908
909 EXPECT_EQ(9U, statics->NumStaticFields());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400910
Mathieu Chartierc7853442015-03-27 14:35:38 -0700911 ArtField* s0 = mirror::Class::FindStaticField(soa.Self(), statics, "s0", "Z");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700912 EXPECT_EQ(s0->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700913 EXPECT_EQ(true, s0->GetBoolean(statics.Get()));
914 s0->SetBoolean<false>(statics.Get(), false);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400915
Mathieu Chartierc7853442015-03-27 14:35:38 -0700916 ArtField* s1 = mirror::Class::FindStaticField(soa.Self(), statics, "s1", "B");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700917 EXPECT_EQ(s1->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700918 EXPECT_EQ(5, s1->GetByte(statics.Get()));
919 s1->SetByte<false>(statics.Get(), 6);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400920
Mathieu Chartierc7853442015-03-27 14:35:38 -0700921 ArtField* s2 = mirror::Class::FindStaticField(soa.Self(), statics, "s2", "C");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700922 EXPECT_EQ(s2->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700923 EXPECT_EQ('a', s2->GetChar(statics.Get()));
924 s2->SetChar<false>(statics.Get(), 'b');
Jesse Wilson7833bd22011-08-09 18:31:44 -0400925
Mathieu Chartierc7853442015-03-27 14:35:38 -0700926 ArtField* s3 = mirror::Class::FindStaticField(soa.Self(), statics, "s3", "S");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700927 EXPECT_EQ(s3->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700928 EXPECT_EQ(-536, s3->GetShort(statics.Get()));
929 s3->SetShort<false>(statics.Get(), -535);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400930
Mathieu Chartierc7853442015-03-27 14:35:38 -0700931 ArtField* s4 = mirror::Class::FindStaticField(soa.Self(), statics, "s4", "I");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700932 EXPECT_EQ(s4->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700933 EXPECT_EQ(2000000000, s4->GetInt(statics.Get()));
934 s4->SetInt<false>(statics.Get(), 2000000001);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400935
Mathieu Chartierc7853442015-03-27 14:35:38 -0700936 ArtField* s5 = mirror::Class::FindStaticField(soa.Self(), statics, "s5", "J");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700937 EXPECT_EQ(s5->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700938 EXPECT_EQ(0x1234567890abcdefLL, s5->GetLong(statics.Get()));
939 s5->SetLong<false>(statics.Get(), INT64_C(0x34567890abcdef12));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400940
Mathieu Chartierc7853442015-03-27 14:35:38 -0700941 ArtField* s6 = mirror::Class::FindStaticField(soa.Self(), statics, "s6", "F");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700942 EXPECT_EQ(s6->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700943 EXPECT_DOUBLE_EQ(0.5, s6->GetFloat(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700944 s6->SetFloat<false>(statics.Get(), 0.75);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400945
Mathieu Chartierc7853442015-03-27 14:35:38 -0700946 ArtField* s7 = mirror::Class::FindStaticField(soa.Self(), statics, "s7", "D");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700947 EXPECT_EQ(s7->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700948 EXPECT_DOUBLE_EQ(16777217.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700949 s7->SetDouble<false>(statics.Get(), 16777219);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400950
Mathieu Chartierc7853442015-03-27 14:35:38 -0700951 ArtField* s8 = mirror::Class::FindStaticField(soa.Self(), statics, "s8",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700952 "Ljava/lang/String;");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700953 EXPECT_EQ(s8->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700954 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("android"));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100955 s8->SetObject<false>(s8->GetDeclaringClass(),
956 mirror::String::AllocFromModifiedUtf8(soa.Self(), "robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400957
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800958 // TODO: Remove EXPECT_FALSE when GCC can handle EXPECT_EQ
959 // http://code.google.com/p/googletest/issues/detail?id=322
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700960 EXPECT_FALSE(s0->GetBoolean(statics.Get()));
961 EXPECT_EQ(6, s1->GetByte(statics.Get()));
962 EXPECT_EQ('b', s2->GetChar(statics.Get()));
963 EXPECT_EQ(-535, s3->GetShort(statics.Get()));
964 EXPECT_EQ(2000000001, s4->GetInt(statics.Get()));
965 EXPECT_EQ(INT64_C(0x34567890abcdef12), s5->GetLong(statics.Get()));
Ian Rogers647b1a82014-10-10 11:02:11 -0700966 EXPECT_FLOAT_EQ(0.75, s6->GetFloat(statics.Get()));
967 EXPECT_DOUBLE_EQ(16777219.0, s7->GetDouble(statics.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700968 EXPECT_TRUE(s8->GetObject(statics.Get())->AsString()->Equals("robot"));
Jesse Wilson7833bd22011-08-09 18:31:44 -0400969}
970
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971TEST_F(ClassLinkerTest, Interfaces) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700973 StackHandleScope<6> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700974 Handle<mirror::ClassLoader> class_loader(
975 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Interfaces"))));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700976 Handle<mirror::Class> I(
977 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
978 Handle<mirror::Class> J(
979 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
980 Handle<mirror::Class> K(
981 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$K;", class_loader)));
982 Handle<mirror::Class> A(
983 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$A;", class_loader)));
984 Handle<mirror::Class> B(
985 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInterfaces$B;", class_loader)));
986 EXPECT_TRUE(I->IsAssignableFrom(A.Get()));
987 EXPECT_TRUE(J->IsAssignableFrom(A.Get()));
988 EXPECT_TRUE(J->IsAssignableFrom(K.Get()));
989 EXPECT_TRUE(K->IsAssignableFrom(B.Get()));
990 EXPECT_TRUE(J->IsAssignableFrom(B.Get()));
Brian Carlstrom30b94452011-08-25 21:35:26 -0700991
Ian Rogersd91d6d62013-09-25 20:26:14 -0700992 const Signature void_sig = I->GetDexCache()->GetDexFile()->CreateSignature("()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700993 ArtMethod* Ii = I->FindVirtualMethod("i", void_sig, sizeof(void*));
994 ArtMethod* Jj1 = J->FindVirtualMethod("j1", void_sig, sizeof(void*));
995 ArtMethod* Jj2 = J->FindVirtualMethod("j2", void_sig, sizeof(void*));
996 ArtMethod* Kj1 = K->FindInterfaceMethod("j1", void_sig, sizeof(void*));
997 ArtMethod* Kj2 = K->FindInterfaceMethod("j2", void_sig, sizeof(void*));
998 ArtMethod* Kk = K->FindInterfaceMethod("k", void_sig, sizeof(void*));
999 ArtMethod* Ai = A->FindVirtualMethod("i", void_sig, sizeof(void*));
1000 ArtMethod* Aj1 = A->FindVirtualMethod("j1", void_sig, sizeof(void*));
1001 ArtMethod* Aj2 = A->FindVirtualMethod("j2", void_sig, sizeof(void*));
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001002 ASSERT_TRUE(Ii != nullptr);
1003 ASSERT_TRUE(Jj1 != nullptr);
1004 ASSERT_TRUE(Jj2 != nullptr);
1005 ASSERT_TRUE(Kj1 != nullptr);
1006 ASSERT_TRUE(Kj2 != nullptr);
1007 ASSERT_TRUE(Kk != nullptr);
1008 ASSERT_TRUE(Ai != nullptr);
1009 ASSERT_TRUE(Aj1 != nullptr);
1010 ASSERT_TRUE(Aj2 != nullptr);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001011 EXPECT_NE(Ii, Ai);
1012 EXPECT_NE(Jj1, Aj1);
1013 EXPECT_NE(Jj2, Aj2);
jeffhao5dbddee2011-09-07 16:38:26 -07001014 EXPECT_EQ(Kj1, Jj1);
1015 EXPECT_EQ(Kj2, Jj2);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001016 EXPECT_EQ(Ai, A->FindVirtualMethodForInterface(Ii, sizeof(void*)));
1017 EXPECT_EQ(Aj1, A->FindVirtualMethodForInterface(Jj1, sizeof(void*)));
1018 EXPECT_EQ(Aj2, A->FindVirtualMethodForInterface(Jj2, sizeof(void*)));
1019 EXPECT_EQ(Ai, A->FindVirtualMethodForVirtualOrInterface(Ii, sizeof(void*)));
1020 EXPECT_EQ(Aj1, A->FindVirtualMethodForVirtualOrInterface(Jj1, sizeof(void*)));
1021 EXPECT_EQ(Aj2, A->FindVirtualMethodForVirtualOrInterface(Jj2, sizeof(void*)));
Ian Rogersd24e2642012-06-06 21:21:43 -07001022
Mathieu Chartiere401d142015-04-22 13:56:20 -07001023 ArtField* Afoo = mirror::Class::FindStaticField(soa.Self(), A, "foo", "Ljava/lang/String;");
1024 ArtField* Bfoo = mirror::Class::FindStaticField(soa.Self(), B, "foo", "Ljava/lang/String;");
1025 ArtField* Jfoo = mirror::Class::FindStaticField(soa.Self(), J, "foo", "Ljava/lang/String;");
1026 ArtField* Kfoo = mirror::Class::FindStaticField(soa.Self(), K, "foo", "Ljava/lang/String;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001027 ASSERT_TRUE(Afoo != nullptr);
Ian Rogersd24e2642012-06-06 21:21:43 -07001028 EXPECT_EQ(Afoo, Bfoo);
1029 EXPECT_EQ(Afoo, Jfoo);
1030 EXPECT_EQ(Afoo, Kfoo);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001031}
1032
Elliott Hughesf3778f62012-01-26 14:14:35 -08001033TEST_F(ClassLinkerTest, ResolveVerifyAndClinit) {
jeffhaoabcfde32011-09-29 15:05:18 -07001034 // pretend we are trying to get the static storage for the StaticsFromCode class.
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001035
jeffhaoabcfde32011-09-29 15:05:18 -07001036 // case 1, get the uninitialized storage from StaticsFromCode.<clinit>
1037 // case 2, get the initialized storage from StaticsFromCode.getS0
Brian Carlstrom193a44d2011-09-04 12:01:42 -07001038
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(Thread::Current());
1040 jobject jclass_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001041 const DexFile* dex_file = GetFirstDexFile(jclass_loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001042 StackHandleScope<1> hs(soa.Self());
1043 Handle<mirror::ClassLoader> class_loader(
1044 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -08001045 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001046 ArtMethod* clinit = klass->FindClassInitializer(sizeof(void*));
1047 ArtMethod* getS0 = klass->FindDirectMethod("getS0", "()Ljava/lang/Object;", sizeof(void*));
Mathieu Chartier9507fa22015-10-29 15:08:57 -07001048 const DexFile::TypeId* type_id = dex_file->FindTypeId("LStaticsFromCode;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001049 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -08001050 uint32_t type_idx = dex_file->GetIndexForTypeId(*type_id);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001051 mirror::Class* uninit = ResolveVerifyAndClinit(type_idx, clinit, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001052 EXPECT_TRUE(uninit != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001053 EXPECT_FALSE(uninit->IsInitialized());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001054 mirror::Class* init = ResolveVerifyAndClinit(type_idx, getS0, soa.Self(), true, false);
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001055 EXPECT_TRUE(init != nullptr);
Ian Rogers5ddb4102014-01-07 08:58:46 -08001056 EXPECT_TRUE(init->IsInitialized());
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001057}
1058
Elliott Hughes20cde902011-10-04 17:37:27 -07001059TEST_F(ClassLinkerTest, FinalizableBit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromea46f952013-07-30 01:26:50 -07001061 mirror::Class* c;
Elliott Hughes20cde902011-10-04 17:37:27 -07001062
1063 // Object has a finalize method, but we know it's empty.
Ian Rogers98379392014-02-24 16:53:16 -08001064 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001065 EXPECT_FALSE(c->IsFinalizable());
1066
1067 // Enum has a finalize method to prevent its subclasses from implementing one.
Ian Rogers98379392014-02-24 16:53:16 -08001068 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Enum;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001069 EXPECT_FALSE(c->IsFinalizable());
1070
1071 // RoundingMode is an enum.
Ian Rogers98379392014-02-24 16:53:16 -08001072 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/math/RoundingMode;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001073 EXPECT_FALSE(c->IsFinalizable());
1074
1075 // RandomAccessFile extends Object and overrides finalize.
Ian Rogers98379392014-02-24 16:53:16 -08001076 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/RandomAccessFile;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001077 EXPECT_TRUE(c->IsFinalizable());
1078
1079 // FileInputStream is finalizable and extends InputStream which isn't.
Ian Rogers98379392014-02-24 16:53:16 -08001080 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/InputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001081 EXPECT_FALSE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001082 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/FileInputStream;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001083 EXPECT_TRUE(c->IsFinalizable());
1084
1085 // ScheduledThreadPoolExecutor doesn't have a finalize method but
1086 // extends ThreadPoolExecutor which does.
Ian Rogers98379392014-02-24 16:53:16 -08001087 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001088 EXPECT_TRUE(c->IsFinalizable());
Ian Rogers98379392014-02-24 16:53:16 -08001089 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ScheduledThreadPoolExecutor;");
Elliott Hughes20cde902011-10-04 17:37:27 -07001090 EXPECT_TRUE(c->IsFinalizable());
1091}
1092
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001093TEST_F(ClassLinkerTest, ClassRootDescriptors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001095 std::string temp;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001096 for (int i = 0; i < ClassLinker::kClassRootsMax; i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001097 mirror::Class* klass = class_linker_->GetClassRoot(ClassLinker::ClassRoot(i));
Ian Rogers1ff3c982014-08-12 02:30:58 -07001098 EXPECT_GT(strlen(klass->GetDescriptor(&temp)), 0U);
1099 EXPECT_STREQ(klass->GetDescriptor(&temp),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001100 class_linker_->GetClassRootDescriptor(ClassLinker::ClassRoot(i))) << " i = " << i;
1101 }
1102}
1103
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001104TEST_F(ClassLinkerTest, ValidatePredefinedClassSizes) {
1105 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier9865bde2015-12-21 09:58:16 -08001106 ScopedNullHandle<mirror::ClassLoader> class_loader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001107 mirror::Class* c;
1108
1109 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Class;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001110 ASSERT_TRUE(c != nullptr);
1111 EXPECT_EQ(c->GetClassSize(), mirror::Class::ClassClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001112
1113 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/Object;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001114 ASSERT_TRUE(c != nullptr);
1115 EXPECT_EQ(c->GetClassSize(), mirror::Object::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001116
1117 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/String;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001118 ASSERT_TRUE(c != nullptr);
1119 EXPECT_EQ(c->GetClassSize(), mirror::String::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001120
1121 c = class_linker_->FindClass(soa.Self(), "Ljava/lang/DexCache;", class_loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001122 ASSERT_TRUE(c != nullptr);
1123 EXPECT_EQ(c->GetClassSize(), mirror::DexCache::ClassSize(sizeof(void*)));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001124}
1125
Mathieu Chartiere401d142015-04-22 13:56:20 -07001126static void CheckMethod(ArtMethod* method, bool verified)
Mathieu Chartier90443472015-07-16 20:32:27 -07001127 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe48498592014-09-10 19:48:05 -07001128 if (!method->IsNative() && !method->IsAbstract()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001129 EXPECT_EQ((method->GetAccessFlags() & kAccSkipAccessChecks) != 0U, verified)
Andreas Gampe48498592014-09-10 19:48:05 -07001130 << PrettyMethod(method, true);
1131 }
1132}
1133
Igor Murashkindf707e42016-02-02 16:56:50 -08001134static void CheckVerificationAttempted(mirror::Class* c, bool preverified)
Mathieu Chartier90443472015-07-16 20:32:27 -07001135 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001136 EXPECT_EQ((c->GetAccessFlags() & kAccVerificationAttempted) != 0U, preverified)
Andreas Gampe48498592014-09-10 19:48:05 -07001137 << "Class " << PrettyClass(c) << " not as expected";
Alex Lighte64300b2015-12-15 15:02:47 -08001138 for (auto& m : c->GetMethods(sizeof(void*))) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001139 CheckMethod(&m, preverified);
Andreas Gampe48498592014-09-10 19:48:05 -07001140 }
1141}
1142
1143TEST_F(ClassLinkerTest, Preverified_InitializedBoot) {
1144 ScopedObjectAccess soa(Thread::Current());
1145
1146 mirror::Class* JavaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Ian Rogerscdc1aaf2014-10-09 13:21:38 -07001147 ASSERT_TRUE(JavaLangObject != nullptr);
Andreas Gampe48498592014-09-10 19:48:05 -07001148 EXPECT_TRUE(JavaLangObject->IsInitialized()) << "Not testing already initialized class from the "
1149 "core";
Igor Murashkindf707e42016-02-02 16:56:50 -08001150 CheckVerificationAttempted(JavaLangObject, true);
Andreas Gampe48498592014-09-10 19:48:05 -07001151}
1152
1153TEST_F(ClassLinkerTest, Preverified_UninitializedBoot) {
1154 ScopedObjectAccess soa(Thread::Current());
1155
1156 StackHandleScope<1> hs(soa.Self());
1157
1158 Handle<mirror::Class> security_manager(hs.NewHandle(class_linker_->FindSystemClass(
1159 soa.Self(), "Ljava/lang/SecurityManager;")));
1160 EXPECT_FALSE(security_manager->IsInitialized()) << "Not testing uninitialized class from the "
1161 "core";
1162
Igor Murashkindf707e42016-02-02 16:56:50 -08001163 CheckVerificationAttempted(security_manager.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001164
Ian Rogers7b078e82014-09-10 14:44:24 -07001165 class_linker_->EnsureInitialized(soa.Self(), security_manager, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001166 CheckVerificationAttempted(security_manager.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001167}
1168
1169TEST_F(ClassLinkerTest, Preverified_App) {
1170 ScopedObjectAccess soa(Thread::Current());
1171
1172 StackHandleScope<2> hs(soa.Self());
1173 Handle<mirror::ClassLoader> class_loader(
1174 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1175 Handle<mirror::Class> statics(
1176 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1177
Igor Murashkindf707e42016-02-02 16:56:50 -08001178 CheckVerificationAttempted(statics.Get(), false);
Andreas Gampe48498592014-09-10 19:48:05 -07001179
Ian Rogers7b078e82014-09-10 14:44:24 -07001180 class_linker_->EnsureInitialized(soa.Self(), statics, true, true);
Igor Murashkindf707e42016-02-02 16:56:50 -08001181 CheckVerificationAttempted(statics.Get(), true);
Andreas Gampe48498592014-09-10 19:48:05 -07001182}
1183
Sebastien Hertz6963e442014-11-26 22:11:27 +01001184TEST_F(ClassLinkerTest, IsBootStrapClassLoaded) {
1185 ScopedObjectAccess soa(Thread::Current());
1186
1187 StackHandleScope<3> hs(soa.Self());
1188 Handle<mirror::ClassLoader> class_loader(
1189 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Statics"))));
1190
1191 // java.lang.Object is a bootstrap class.
1192 Handle<mirror::Class> jlo_class(
1193 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
1194 ASSERT_TRUE(jlo_class.Get() != nullptr);
1195 EXPECT_TRUE(jlo_class.Get()->IsBootStrapClassLoaded());
1196
1197 // Statics is not a bootstrap class.
1198 Handle<mirror::Class> statics(
1199 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStatics;", class_loader)));
1200 ASSERT_TRUE(statics.Get() != nullptr);
1201 EXPECT_FALSE(statics.Get()->IsBootStrapClassLoaded());
1202}
1203
Mathieu Chartier76172162016-01-26 14:54:06 -08001204// Regression test for b/26799552.
1205TEST_F(ClassLinkerTest, RegisterDexFileName) {
1206 ScopedObjectAccess soa(Thread::Current());
1207 StackHandleScope<2> hs(soa.Self());
1208 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1209 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
1210 {
1211 ReaderMutexLock mu(soa.Self(), *class_linker->DexLock());
1212 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1213 dex_cache.Assign(down_cast<mirror::DexCache*>(soa.Self()->DecodeJObject(data.weak_root)));
1214 if (dex_cache.Get() != nullptr) {
1215 break;
1216 }
1217 }
1218 ASSERT_TRUE(dex_cache.Get() != nullptr);
1219 }
1220 // Make a copy of the dex cache and change the name.
1221 dex_cache.Assign(dex_cache->Clone(soa.Self())->AsDexCache());
1222 const uint16_t data[] = { 0x20AC, 0x20A1 };
1223 Handle<mirror::String> location(hs.NewHandle(mirror::String::AllocFromUtf16(soa.Self(),
1224 arraysize(data),
1225 data)));
1226 dex_cache->SetLocation(location.Get());
1227 const DexFile* old_dex_file = dex_cache->GetDexFile();
1228
1229 DexFile* dex_file = new DexFile(old_dex_file->Begin(),
1230 old_dex_file->Size(),
1231 location->ToModifiedUtf8(),
1232 0u,
1233 nullptr,
1234 nullptr);
1235 {
1236 WriterMutexLock mu(soa.Self(), *class_linker->DexLock());
1237 // Check that inserting with a UTF16 name works.
1238 class_linker->RegisterDexFileLocked(*dex_file, dex_cache);
1239 }
1240}
1241
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001242} // namespace art