class-inl.h revision a7a4759946d9f11c88dc108b2b6a9518ce9c1e18
1/* 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 */ 16 17#ifndef ART_RUNTIME_MIRROR_CLASS_INL_H_ 18#define ART_RUNTIME_MIRROR_CLASS_INL_H_ 19 20#include "class.h" 21 22#include "art_field-inl.h" 23#include "art_method.h" 24#include "art_method-inl.h" 25#include "class_loader.h" 26#include "common_throws.h" 27#include "dex_cache.h" 28#include "dex_file.h" 29#include "gc/heap-inl.h" 30#include "iftable.h" 31#include "length_prefixed_array.h" 32#include "object_array-inl.h" 33#include "read_barrier-inl.h" 34#include "reference-inl.h" 35#include "runtime.h" 36#include "string.h" 37#include "utils.h" 38 39namespace art { 40namespace mirror { 41 42template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 43inline uint32_t Class::GetObjectSize() { 44 // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator. 45 DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this); 46 return GetField32(ObjectSizeOffset()); 47} 48 49inline Class* Class::GetSuperClass() { 50 // Can only get super class for loaded classes (hack for when runtime is 51 // initializing) 52 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); 53 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); 54} 55 56inline ClassLoader* Class::GetClassLoader() { 57 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_)); 58} 59 60template<VerifyObjectFlags kVerifyFlags> 61inline DexCache* Class::GetDexCache() { 62 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_)); 63} 64 65inline LengthPrefixedArray<ArtMethod>* Class::GetDirectMethodsPtr() { 66 DCHECK(IsLoaded() || IsErroneous()); 67 return GetDirectMethodsPtrUnchecked(); 68} 69 70inline LengthPrefixedArray<ArtMethod>* Class::GetDirectMethodsPtrUnchecked() { 71 return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( 72 GetField64(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_))); 73} 74 75inline LengthPrefixedArray<ArtMethod>* Class::GetVirtualMethodsPtrUnchecked() { 76 return reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( 77 GetField64(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_))); 78} 79 80inline void Class::SetDirectMethodsPtr(LengthPrefixedArray<ArtMethod>* new_direct_methods) { 81 DCHECK(GetDirectMethodsPtrUnchecked() == nullptr); 82 SetDirectMethodsPtrUnchecked(new_direct_methods); 83} 84 85inline void Class::SetDirectMethodsPtrUnchecked( 86 LengthPrefixedArray<ArtMethod>* new_direct_methods) { 87 SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), 88 reinterpret_cast<uint64_t>(new_direct_methods)); 89} 90 91inline ArtMethod* Class::GetDirectMethodUnchecked(size_t i, size_t pointer_size) { 92 CheckPointerSize(pointer_size); 93 auto* methods = GetDirectMethodsPtrUnchecked(); 94 DCHECK(methods != nullptr); 95 return &methods->At(i, 96 ArtMethod::Size(pointer_size), 97 ArtMethod::Alignment(pointer_size)); 98} 99 100inline ArtMethod* Class::GetDirectMethod(size_t i, size_t pointer_size) { 101 CheckPointerSize(pointer_size); 102 auto* methods = GetDirectMethodsPtr(); 103 DCHECK(methods != nullptr); 104 return &methods->At(i, 105 ArtMethod::Size(pointer_size), 106 ArtMethod::Alignment(pointer_size)); 107} 108 109template<VerifyObjectFlags kVerifyFlags> 110inline LengthPrefixedArray<ArtMethod>* Class::GetVirtualMethodsPtr() { 111 DCHECK(IsLoaded<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); 112 return GetVirtualMethodsPtrUnchecked(); 113} 114 115inline void Class::SetVirtualMethodsPtr(LengthPrefixedArray<ArtMethod>* new_virtual_methods) { 116 // TODO: we reassign virtual methods to grow the table for miranda 117 // methods.. they should really just be assigned once. 118 SetField64<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), 119 reinterpret_cast<uint64_t>(new_virtual_methods)); 120} 121 122template<VerifyObjectFlags kVerifyFlags> 123inline ArtMethod* Class::GetVirtualMethod(size_t i, size_t pointer_size) { 124 CheckPointerSize(pointer_size); 125 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()) 126 << PrettyClass(this) << " status=" << GetStatus(); 127 return GetVirtualMethodUnchecked(i, pointer_size); 128} 129 130inline ArtMethod* Class::GetVirtualMethodDuringLinking(size_t i, size_t pointer_size) { 131 CheckPointerSize(pointer_size); 132 DCHECK(IsLoaded() || IsErroneous()); 133 return GetVirtualMethodUnchecked(i, pointer_size); 134} 135 136inline ArtMethod* Class::GetVirtualMethodUnchecked(size_t i, size_t pointer_size) { 137 CheckPointerSize(pointer_size); 138 LengthPrefixedArray<ArtMethod>* methods = GetVirtualMethodsPtrUnchecked(); 139 DCHECK(methods != nullptr); 140 return &methods->At(i, 141 ArtMethod::Size(pointer_size), 142 ArtMethod::Alignment(pointer_size)); 143} 144 145inline PointerArray* Class::GetVTable() { 146 DCHECK(IsResolved() || IsErroneous()); 147 return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); 148} 149 150inline PointerArray* Class::GetVTableDuringLinking() { 151 DCHECK(IsLoaded() || IsErroneous()); 152 return GetFieldObject<PointerArray>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); 153} 154 155inline void Class::SetVTable(PointerArray* new_vtable) { 156 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable); 157} 158 159inline MemberOffset Class::EmbeddedImTableEntryOffset(uint32_t i, size_t pointer_size) { 160 DCHECK_LT(i, kImtSize); 161 return MemberOffset( 162 EmbeddedImTableOffset(pointer_size).Uint32Value() + i * ImTableEntrySize(pointer_size)); 163} 164 165inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i, size_t pointer_size) { 166 DCHECK(ShouldHaveEmbeddedImtAndVTable()); 167 return GetFieldPtrWithSize<ArtMethod*>( 168 EmbeddedImTableEntryOffset(i, pointer_size), pointer_size); 169} 170 171inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { 172 DCHECK(ShouldHaveEmbeddedImtAndVTable()); 173 SetFieldPtrWithSize<false>(EmbeddedImTableEntryOffset(i, pointer_size), method, pointer_size); 174} 175 176inline bool Class::HasVTable() { 177 return GetVTable() != nullptr || ShouldHaveEmbeddedImtAndVTable(); 178} 179 180inline int32_t Class::GetVTableLength() { 181 if (ShouldHaveEmbeddedImtAndVTable()) { 182 return GetEmbeddedVTableLength(); 183 } 184 return GetVTable() != nullptr ? GetVTable()->GetLength() : 0; 185} 186 187inline ArtMethod* Class::GetVTableEntry(uint32_t i, size_t pointer_size) { 188 if (ShouldHaveEmbeddedImtAndVTable()) { 189 return GetEmbeddedVTableEntry(i, pointer_size); 190 } 191 auto* vtable = GetVTable(); 192 DCHECK(vtable != nullptr); 193 return vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); 194} 195 196inline int32_t Class::GetEmbeddedVTableLength() { 197 return GetField32(MemberOffset(EmbeddedVTableLengthOffset())); 198} 199 200inline void Class::SetEmbeddedVTableLength(int32_t len) { 201 SetField32<false>(MemberOffset(EmbeddedVTableLengthOffset()), len); 202} 203 204inline MemberOffset Class::EmbeddedVTableEntryOffset(uint32_t i, size_t pointer_size) { 205 return MemberOffset( 206 EmbeddedVTableOffset(pointer_size).Uint32Value() + i * VTableEntrySize(pointer_size)); 207} 208 209inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i, size_t pointer_size) { 210 return GetFieldPtrWithSize<ArtMethod*>(EmbeddedVTableEntryOffset(i, pointer_size), pointer_size); 211} 212 213inline void Class::SetEmbeddedVTableEntryUnchecked( 214 uint32_t i, ArtMethod* method, size_t pointer_size) { 215 SetFieldPtrWithSize<false>(EmbeddedVTableEntryOffset(i, pointer_size), method, pointer_size); 216} 217 218inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method, size_t pointer_size) { 219 auto* vtable = GetVTableDuringLinking(); 220 CHECK_EQ(method, vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size)); 221 SetEmbeddedVTableEntryUnchecked(i, method, pointer_size); 222} 223 224inline bool Class::Implements(Class* klass) { 225 DCHECK(klass != nullptr); 226 DCHECK(klass->IsInterface()) << PrettyClass(this); 227 // All interfaces implemented directly and by our superclass, and 228 // recursively all super-interfaces of those interfaces, are listed 229 // in iftable_, so we can just do a linear scan through that. 230 int32_t iftable_count = GetIfTableCount(); 231 IfTable* iftable = GetIfTable(); 232 for (int32_t i = 0; i < iftable_count; i++) { 233 if (iftable->GetInterface(i) == klass) { 234 return true; 235 } 236 } 237 return false; 238} 239 240// Determine whether "this" is assignable from "src", where both of these 241// are array classes. 242// 243// Consider an array class, e.g. Y[][], where Y is a subclass of X. 244// Y[][] = Y[][] --> true (identity) 245// X[][] = Y[][] --> true (element superclass) 246// Y = Y[][] --> false 247// Y[] = Y[][] --> false 248// Object = Y[][] --> true (everything is an object) 249// Object[] = Y[][] --> true 250// Object[][] = Y[][] --> true 251// Object[][][] = Y[][] --> false (too many []s) 252// Serializable = Y[][] --> true (all arrays are Serializable) 253// Serializable[] = Y[][] --> true 254// Serializable[][] = Y[][] --> false (unless Y is Serializable) 255// 256// Don't forget about primitive types. 257// Object[] = int[] --> false 258// 259inline bool Class::IsArrayAssignableFromArray(Class* src) { 260 DCHECK(IsArrayClass()) << PrettyClass(this); 261 DCHECK(src->IsArrayClass()) << PrettyClass(src); 262 return GetComponentType()->IsAssignableFrom(src->GetComponentType()); 263} 264 265inline bool Class::IsAssignableFromArray(Class* src) { 266 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom 267 DCHECK(src->IsArrayClass()) << PrettyClass(src); 268 if (!IsArrayClass()) { 269 // If "this" is not also an array, it must be Object. 270 // src's super should be java_lang_Object, since it is an array. 271 Class* java_lang_Object = src->GetSuperClass(); 272 DCHECK(java_lang_Object != nullptr) << PrettyClass(src); 273 DCHECK(java_lang_Object->GetSuperClass() == nullptr) << PrettyClass(src); 274 return this == java_lang_Object; 275 } 276 return IsArrayAssignableFromArray(src); 277} 278 279template <bool throw_on_failure, bool use_referrers_cache> 280inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, 281 uint32_t field_idx, DexCache* dex_cache) { 282 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); 283 if (UNLIKELY(!this->CanAccess(access_to))) { 284 // The referrer class can't access the field's declaring class but may still be able 285 // to access the field if the FieldId specifies an accessible subclass of the declaring 286 // class rather than the declaring class itself. 287 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; 288 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; 289 // The referenced class has already been resolved with the field, get it from the dex cache. 290 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); 291 DCHECK(dex_access_to != nullptr); 292 if (UNLIKELY(!this->CanAccess(dex_access_to))) { 293 if (throw_on_failure) { 294 ThrowIllegalAccessErrorClass(this, dex_access_to); 295 } 296 return false; 297 } 298 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), 299 this->CanAccessMember(dex_access_to, field->GetAccessFlags())); 300 } 301 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { 302 return true; 303 } 304 if (throw_on_failure) { 305 ThrowIllegalAccessErrorField(this, field); 306 } 307 return false; 308} 309 310template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> 311inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, 312 uint32_t method_idx, DexCache* dex_cache) { 313 static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type"); 314 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); 315 if (UNLIKELY(!this->CanAccess(access_to))) { 316 // The referrer class can't access the method's declaring class but may still be able 317 // to access the method if the MethodId specifies an accessible subclass of the declaring 318 // class rather than the declaring class itself. 319 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; 320 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; 321 // The referenced class has already been resolved with the method, get it from the dex cache. 322 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); 323 DCHECK(dex_access_to != nullptr); 324 if (UNLIKELY(!this->CanAccess(dex_access_to))) { 325 if (throw_on_failure) { 326 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, 327 method, throw_invoke_type); 328 } 329 return false; 330 } 331 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), 332 this->CanAccessMember(dex_access_to, method->GetAccessFlags())); 333 } 334 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { 335 return true; 336 } 337 if (throw_on_failure) { 338 ThrowIllegalAccessErrorMethod(this, method); 339 } 340 return false; 341} 342 343inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, 344 DexCache* dex_cache, uint32_t field_idx) { 345 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache); 346} 347 348inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, 349 uint32_t field_idx) { 350 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); 351} 352 353inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, 354 DexCache* dex_cache, uint32_t method_idx) { 355 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache); 356} 357 358template <InvokeType throw_invoke_type> 359inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, 360 uint32_t method_idx) { 361 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, 362 nullptr); 363} 364 365inline bool Class::IsSubClass(Class* klass) { 366 DCHECK(!IsInterface()) << PrettyClass(this); 367 DCHECK(!IsArrayClass()) << PrettyClass(this); 368 Class* current = this; 369 do { 370 if (current == klass) { 371 return true; 372 } 373 current = current->GetSuperClass(); 374 } while (current != nullptr); 375 return false; 376} 377 378inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method, size_t pointer_size) { 379 Class* declaring_class = method->GetDeclaringClass(); 380 DCHECK(declaring_class != nullptr) << PrettyClass(this); 381 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); 382 // TODO cache to improve lookup speed 383 const int32_t iftable_count = GetIfTableCount(); 384 IfTable* iftable = GetIfTable(); 385 for (int32_t i = 0; i < iftable_count; i++) { 386 if (iftable->GetInterface(i) == declaring_class) { 387 return iftable->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( 388 method->GetMethodIndex(), pointer_size); 389 } 390 } 391 return nullptr; 392} 393 394inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method, size_t pointer_size) { 395 // Only miranda or default methods may come from interfaces and be used as a virtual. 396 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsDefault() || method->IsMiranda()); 397 // The argument method may from a super class. 398 // Use the index to a potentially overridden one for this instance's class. 399 return GetVTableEntry(method->GetMethodIndex(), pointer_size); 400} 401 402inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method, size_t pointer_size) { 403 DCHECK(!method->GetDeclaringClass()->IsInterface()); 404 return GetSuperClass()->GetVTableEntry(method->GetMethodIndex(), pointer_size); 405} 406 407inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method, 408 size_t pointer_size) { 409 if (method->IsDirect()) { 410 return method; 411 } 412 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { 413 return FindVirtualMethodForInterface(method, pointer_size); 414 } 415 return FindVirtualMethodForVirtual(method, pointer_size); 416} 417 418inline IfTable* Class::GetIfTable() { 419 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); 420} 421 422inline int32_t Class::GetIfTableCount() { 423 IfTable* iftable = GetIfTable(); 424 if (iftable == nullptr) { 425 return 0; 426 } 427 return iftable->Count(); 428} 429 430inline void Class::SetIfTable(IfTable* new_iftable) { 431 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); 432} 433 434inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtr() { 435 DCHECK(IsLoaded() || IsErroneous()); 436 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 437} 438 439inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() { 440 Class* super_class = GetSuperClass(); 441 return (super_class != nullptr) 442 ? MemberOffset(RoundUp(super_class->GetObjectSize(), 443 sizeof(mirror::HeapReference<mirror::Object>))) 444 : ClassOffset(); 445} 446 447inline MemberOffset Class::GetFirstReferenceStaticFieldOffset(size_t pointer_size) { 448 DCHECK(IsResolved()); 449 uint32_t base = sizeof(mirror::Class); // Static fields come after the class. 450 if (ShouldHaveEmbeddedImtAndVTable()) { 451 // Static fields come after the embedded tables. 452 base = mirror::Class::ComputeClassSize( 453 true, GetEmbeddedVTableLength(), 0, 0, 0, 0, 0, pointer_size); 454 } 455 return MemberOffset(base); 456} 457 458inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size) { 459 DCHECK(IsLoaded()); 460 uint32_t base = sizeof(mirror::Class); // Static fields come after the class. 461 if (ShouldHaveEmbeddedImtAndVTable()) { 462 // Static fields come after the embedded tables. 463 base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(), 464 0, 0, 0, 0, 0, pointer_size); 465 } 466 return MemberOffset(base); 467} 468 469inline void Class::SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) { 470 DCHECK(GetIFieldsPtrUnchecked() == nullptr); 471 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); 472} 473 474inline void Class::SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_ifields) { 475 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); 476} 477 478inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtrUnchecked() { 479 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); 480} 481 482inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtrUnchecked() { 483 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 484} 485 486inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtr() { 487 DCHECK(IsLoaded() || IsErroneous()) << GetStatus(); 488 return GetSFieldsPtrUnchecked(); 489} 490 491inline void Class::SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) { 492 DCHECK((IsRetired() && new_sfields == nullptr) || 493 GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr); 494 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); 495} 496 497inline void Class::SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) { 498 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); 499} 500 501inline ArtField* Class::GetStaticField(uint32_t i) { 502 return &GetSFieldsPtr()->At(i); 503} 504 505inline ArtField* Class::GetInstanceField(uint32_t i) { 506 return &GetIFieldsPtr()->At(i); 507} 508 509template<VerifyObjectFlags kVerifyFlags> 510inline uint32_t Class::GetReferenceInstanceOffsets() { 511 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); 512 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); 513} 514 515inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { 516 if (Runtime::Current()->IsActiveTransaction()) { 517 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 518 } else { 519 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 520 } 521} 522 523template<VerifyObjectFlags kVerifyFlags> 524inline uint32_t Class::GetAccessFlags() { 525 // Check class is loaded/retired or this is java.lang.String that has a 526 // circularity issue during loading the names of its members 527 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || 528 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || 529 this == String::GetJavaLangString()) 530 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() 531 << " IsRetired=" << IsRetired<kVerifyFlags>() 532 << " IsErroneous=" << 533 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() 534 << " IsString=" << (this == String::GetJavaLangString()) 535 << " descriptor=" << PrettyDescriptor(this); 536 return GetField32<kVerifyFlags>(AccessFlagsOffset()); 537} 538 539inline String* Class::GetName() { 540 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); 541} 542 543inline void Class::SetName(String* name) { 544 if (Runtime::Current()->IsActiveTransaction()) { 545 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 546 } else { 547 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 548 } 549} 550 551template<VerifyObjectFlags kVerifyFlags> 552inline Primitive::Type Class::GetPrimitiveType() { 553 static_assert(sizeof(Primitive::Type) == sizeof(int32_t), 554 "art::Primitive::Type and int32_t have different sizes."); 555 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); 556 Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF); 557 DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type)); 558 return type; 559} 560 561template<VerifyObjectFlags kVerifyFlags> 562inline size_t Class::GetPrimitiveTypeSizeShift() { 563 static_assert(sizeof(Primitive::Type) == sizeof(int32_t), 564 "art::Primitive::Type and int32_t have different sizes."); 565 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); 566 size_t size_shift = static_cast<Primitive::Type>(v32 >> 16); 567 DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF))); 568 return size_shift; 569} 570 571inline void Class::CheckObjectAlloc() { 572 DCHECK(!IsArrayClass()) 573 << PrettyClass(this) 574 << "A array shouldn't be allocated through this " 575 << "as it requires a pre-fence visitor that sets the class size."; 576 DCHECK(!IsClassClass()) 577 << PrettyClass(this) 578 << "A class object shouldn't be allocated through this " 579 << "as it requires a pre-fence visitor that sets the class size."; 580 DCHECK(!IsStringClass()) 581 << PrettyClass(this) 582 << "A string shouldn't be allocated through this " 583 << "as it requires a pre-fence visitor that sets the class size."; 584 DCHECK(IsInstantiable()) << PrettyClass(this); 585 // TODO: decide whether we want this check. It currently fails during bootstrap. 586 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); 587 DCHECK_GE(this->object_size_, sizeof(Object)); 588} 589 590template<bool kIsInstrumented, bool kCheckAddFinalizer> 591inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { 592 CheckObjectAlloc(); 593 gc::Heap* heap = Runtime::Current()->GetHeap(); 594 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); 595 if (!kCheckAddFinalizer) { 596 DCHECK(!IsFinalizable()); 597 } 598 mirror::Object* obj = 599 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, 600 allocator_type, VoidFunctor()); 601 if (add_finalizer && LIKELY(obj != nullptr)) { 602 heap->AddFinalizerReference(self, &obj); 603 if (UNLIKELY(self->IsExceptionPending())) { 604 // Failed to allocate finalizer reference, it means that the whole allocation failed. 605 obj = nullptr; 606 } 607 } 608 return obj; 609} 610 611inline Object* Class::AllocObject(Thread* self) { 612 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); 613} 614 615inline Object* Class::AllocNonMovableObject(Thread* self) { 616 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); 617} 618 619inline uint32_t Class::ComputeClassSize(bool has_embedded_tables, 620 uint32_t num_vtable_entries, 621 uint32_t num_8bit_static_fields, 622 uint32_t num_16bit_static_fields, 623 uint32_t num_32bit_static_fields, 624 uint32_t num_64bit_static_fields, 625 uint32_t num_ref_static_fields, 626 size_t pointer_size) { 627 // Space used by java.lang.Class and its instance fields. 628 uint32_t size = sizeof(Class); 629 // Space used by embedded tables. 630 if (has_embedded_tables) { 631 const uint32_t embedded_imt_size = kImtSize * ImTableEntrySize(pointer_size); 632 const uint32_t embedded_vtable_size = num_vtable_entries * VTableEntrySize(pointer_size); 633 size = RoundUp(size + sizeof(uint32_t) /* embedded vtable len */, pointer_size) + 634 embedded_imt_size + embedded_vtable_size; 635 } 636 637 // Space used by reference statics. 638 size += num_ref_static_fields * sizeof(HeapReference<Object>); 639 if (!IsAligned<8>(size) && num_64bit_static_fields > 0) { 640 uint32_t gap = 8 - (size & 0x7); 641 size += gap; // will be padded 642 // Shuffle 4-byte fields forward. 643 while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) { 644 --num_32bit_static_fields; 645 gap -= sizeof(uint32_t); 646 } 647 // Shuffle 2-byte fields forward. 648 while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) { 649 --num_16bit_static_fields; 650 gap -= sizeof(uint16_t); 651 } 652 // Shuffle byte fields forward. 653 while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) { 654 --num_8bit_static_fields; 655 gap -= sizeof(uint8_t); 656 } 657 } 658 // Guaranteed to be at least 4 byte aligned. No need for further alignments. 659 // Space used for primitive static fields. 660 size += num_8bit_static_fields * sizeof(uint8_t) + num_16bit_static_fields * sizeof(uint16_t) + 661 num_32bit_static_fields * sizeof(uint32_t) + num_64bit_static_fields * sizeof(uint64_t); 662 return size; 663} 664 665template <typename Visitor> 666inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { 667 VisitInstanceFieldsReferences(klass, visitor); 668 // Right after a class is allocated, but not yet loaded 669 // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it 670 // and scan it. IsTemp() may call Class::GetAccessFlags() but may 671 // fail in the DCHECK in Class::GetAccessFlags() because the class 672 // status is kStatusNotReady. To avoid it, rely on IsResolved() 673 // only. This is fine because a temp class never goes into the 674 // kStatusResolved state. 675 if (IsResolved()) { 676 // Temp classes don't ever populate imt/vtable or static fields and they are not even 677 // allocated with the right size for those. Also, unresolved classes don't have fields 678 // linked yet. 679 VisitStaticFieldsReferences(this, visitor); 680 } 681 // Since this class is reachable, we must also visit the associated roots when we scan it. 682 VisitNativeRoots(visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); 683} 684 685template<ReadBarrierOption kReadBarrierOption> 686inline bool Class::IsReferenceClass() const { 687 return this == Reference::GetJavaLangRefReference<kReadBarrierOption>(); 688} 689 690template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 691inline bool Class::IsClassClass() { 692 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> 693 template GetClass<kVerifyFlags, kReadBarrierOption>(); 694 return this == java_lang_Class; 695} 696 697inline const DexFile& Class::GetDexFile() { 698 return *GetDexCache()->GetDexFile(); 699} 700 701inline bool Class::DescriptorEquals(const char* match) { 702 if (IsArrayClass()) { 703 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1); 704 } else if (IsPrimitive()) { 705 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0; 706 } else if (IsProxyClass()) { 707 return ProxyDescriptorEquals(match); 708 } else { 709 const DexFile& dex_file = GetDexFile(); 710 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); 711 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0; 712 } 713} 714 715inline void Class::AssertInitializedOrInitializingInThread(Thread* self) { 716 if (kIsDebugBuild && !IsInitialized()) { 717 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus(); 718 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this) 719 << " is initializing in a different thread"; 720 } 721} 722 723inline ObjectArray<Class>* Class::GetInterfaces() { 724 CHECK(IsProxyClass()); 725 // First static field. 726 auto* field = GetStaticField(0); 727 DCHECK_STREQ(field->GetName(), "interfaces"); 728 MemberOffset field_offset = field->GetOffset(); 729 return GetFieldObject<ObjectArray<Class>>(field_offset); 730} 731 732inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() { 733 CHECK(IsProxyClass()); 734 // Second static field. 735 auto* field = GetStaticField(1); 736 DCHECK_STREQ(field->GetName(), "throws"); 737 MemberOffset field_offset = field->GetOffset(); 738 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset); 739} 740 741inline MemberOffset Class::GetDisableIntrinsicFlagOffset() { 742 CHECK(IsReferenceClass()); 743 // First static field 744 auto* field = GetStaticField(0); 745 DCHECK_STREQ(field->GetName(), "disableIntrinsic"); 746 return field->GetOffset(); 747} 748 749inline MemberOffset Class::GetSlowPathFlagOffset() { 750 CHECK(IsReferenceClass()); 751 // Second static field 752 auto* field = GetStaticField(1); 753 DCHECK_STREQ(field->GetName(), "slowPathEnabled"); 754 return field->GetOffset(); 755} 756 757inline bool Class::GetSlowPathEnabled() { 758 return GetFieldBoolean(GetSlowPathFlagOffset()); 759} 760 761inline void Class::SetSlowPath(bool enabled) { 762 SetFieldBoolean<false, false>(GetSlowPathFlagOffset(), enabled); 763} 764 765inline void Class::InitializeClassVisitor::operator()( 766 mirror::Object* obj, size_t usable_size) const { 767 DCHECK_LE(class_size_, usable_size); 768 // Avoid AsClass as object is not yet in live bitmap or allocation stack. 769 mirror::Class* klass = down_cast<mirror::Class*>(obj); 770 // DCHECK(klass->IsClass()); 771 klass->SetClassSize(class_size_); 772 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. 773 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. 774 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. 775} 776 777inline void Class::SetAccessFlags(uint32_t new_access_flags) { 778 // Called inside a transaction when setting pre-verified flag during boot image compilation. 779 if (Runtime::Current()->IsActiveTransaction()) { 780 SetField32<true>(AccessFlagsOffset(), new_access_flags); 781 } else { 782 SetField32<false>(AccessFlagsOffset(), new_access_flags); 783 } 784} 785 786inline void Class::SetClassFlags(uint32_t new_flags) { 787 if (Runtime::Current()->IsActiveTransaction()) { 788 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); 789 } else { 790 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); 791 } 792} 793 794inline uint32_t Class::NumDirectInterfaces() { 795 if (IsPrimitive()) { 796 return 0; 797 } else if (IsArrayClass()) { 798 return 2; 799 } else if (IsProxyClass()) { 800 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces(); 801 return interfaces != nullptr ? interfaces->GetLength() : 0; 802 } else { 803 const DexFile::TypeList* interfaces = GetInterfaceTypeList(); 804 if (interfaces == nullptr) { 805 return 0; 806 } else { 807 return interfaces->Size(); 808 } 809 } 810} 811 812inline void Class::SetDexCacheStrings(GcRoot<String>* new_dex_cache_strings) { 813 SetFieldPtr<false>(DexCacheStringsOffset(), new_dex_cache_strings); 814} 815 816inline GcRoot<String>* Class::GetDexCacheStrings() { 817 return GetFieldPtr<GcRoot<String>*>(DexCacheStringsOffset()); 818} 819 820template<class Visitor> 821void mirror::Class::VisitNativeRoots(Visitor& visitor, size_t pointer_size) { 822 for (ArtField& field : GetSFieldsUnchecked()) { 823 // Visit roots first in case the declaring class gets moved. 824 field.VisitRoots(visitor); 825 if (kIsDebugBuild && IsResolved()) { 826 CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); 827 } 828 } 829 for (ArtField& field : GetIFieldsUnchecked()) { 830 // Visit roots first in case the declaring class gets moved. 831 field.VisitRoots(visitor); 832 if (kIsDebugBuild && IsResolved()) { 833 CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); 834 } 835 } 836 for (ArtMethod& method : GetDirectMethods(pointer_size)) { 837 method.VisitRoots(visitor, pointer_size); 838 } 839 for (ArtMethod& method : GetVirtualMethods(pointer_size)) { 840 method.VisitRoots(visitor, pointer_size); 841 } 842} 843 844inline IterationRange<StrideIterator<ArtMethod>> Class::GetDirectMethods(size_t pointer_size) { 845 CheckPointerSize(pointer_size); 846 return MakeIterationRangeFromLengthPrefixedArray(GetDirectMethodsPtrUnchecked(), 847 ArtMethod::Size(pointer_size), 848 ArtMethod::Alignment(pointer_size)); 849} 850 851inline IterationRange<StrideIterator<ArtMethod>> Class::GetVirtualMethods(size_t pointer_size) { 852 CheckPointerSize(pointer_size); 853 return MakeIterationRangeFromLengthPrefixedArray(GetVirtualMethodsPtrUnchecked(), 854 ArtMethod::Size(pointer_size), 855 ArtMethod::Alignment(pointer_size)); 856} 857 858inline IterationRange<StrideIterator<ArtField>> Class::GetIFields() { 859 return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtr()); 860} 861 862inline IterationRange<StrideIterator<ArtField>> Class::GetSFields() { 863 return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtr()); 864} 865 866inline IterationRange<StrideIterator<ArtField>> Class::GetIFieldsUnchecked() { 867 return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtrUnchecked()); 868} 869 870inline IterationRange<StrideIterator<ArtField>> Class::GetSFieldsUnchecked() { 871 return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtrUnchecked()); 872} 873 874inline MemberOffset Class::EmbeddedImTableOffset(size_t pointer_size) { 875 CheckPointerSize(pointer_size); 876 // Round up since we want the embedded imt and vtable to be pointer size aligned in case 64 bits. 877 // Add 32 bits for embedded vtable length. 878 return MemberOffset( 879 RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), pointer_size)); 880} 881 882inline MemberOffset Class::EmbeddedVTableOffset(size_t pointer_size) { 883 CheckPointerSize(pointer_size); 884 return MemberOffset(EmbeddedImTableOffset(pointer_size).Uint32Value() + 885 kImtSize * ImTableEntrySize(pointer_size)); 886} 887 888inline void Class::CheckPointerSize(size_t pointer_size) { 889 DCHECK(ValidPointerSize(pointer_size)) << pointer_size; 890 DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); 891} 892 893template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 894inline Class* Class::GetComponentType() { 895 return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset()); 896} 897 898template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 899inline bool Class::IsArrayClass() { 900 return GetComponentType<kVerifyFlags, kReadBarrierOption>() != nullptr; 901} 902 903inline bool Class::IsAssignableFrom(Class* src) { 904 DCHECK(src != nullptr); 905 if (this == src) { 906 // Can always assign to things of the same type. 907 return true; 908 } else if (IsObjectClass()) { 909 // Can assign any reference to java.lang.Object. 910 return !src->IsPrimitive(); 911 } else if (IsInterface()) { 912 return src->Implements(this); 913 } else if (src->IsArrayClass()) { 914 return IsAssignableFromArray(src); 915 } else { 916 return !src->IsInterface() && src->IsSubClass(this); 917 } 918} 919 920inline uint32_t Class::NumDirectMethods() { 921 LengthPrefixedArray<ArtMethod>* arr = GetDirectMethodsPtrUnchecked(); 922 return arr != nullptr ? arr->size() : 0u; 923} 924 925inline uint32_t Class::NumVirtualMethods() { 926 LengthPrefixedArray<ArtMethod>* arr = GetVirtualMethodsPtrUnchecked(); 927 return arr != nullptr ? arr->size() : 0u; 928} 929 930inline uint32_t Class::NumInstanceFields() { 931 LengthPrefixedArray<ArtField>* arr = GetIFieldsPtrUnchecked(); 932 return arr != nullptr ? arr->size() : 0u; 933} 934 935inline uint32_t Class::NumStaticFields() { 936 LengthPrefixedArray<ArtField>* arr = GetSFieldsPtrUnchecked(); 937 return arr != nullptr ? arr->size() : 0u; 938} 939 940template <typename Visitor> 941inline void Class::FixupNativePointers(mirror::Class* dest, 942 size_t pointer_size, 943 const Visitor& visitor) { 944 // Update the field arrays. 945 LengthPrefixedArray<ArtField>* const sfields = GetSFieldsPtr(); 946 LengthPrefixedArray<ArtField>* const new_sfields = visitor(sfields); 947 if (sfields != new_sfields) { 948 dest->SetSFieldsPtrUnchecked(new_sfields); 949 } 950 LengthPrefixedArray<ArtField>* const ifields = GetIFieldsPtr(); 951 LengthPrefixedArray<ArtField>* const new_ifields = visitor(ifields); 952 if (ifields != new_ifields) { 953 dest->SetIFieldsPtrUnchecked(new_ifields); 954 } 955 // Update direct and virtual method arrays. 956 LengthPrefixedArray<ArtMethod>* direct_methods = GetDirectMethodsPtr(); 957 LengthPrefixedArray<ArtMethod>* new_direct_methods = visitor(direct_methods); 958 if (direct_methods != new_direct_methods) { 959 dest->SetDirectMethodsPtrUnchecked(new_direct_methods); 960 } 961 LengthPrefixedArray<ArtMethod>* virtual_methods = GetVirtualMethodsPtr(); 962 LengthPrefixedArray<ArtMethod>* new_virtual_methods = visitor(virtual_methods); 963 if (virtual_methods != new_virtual_methods) { 964 dest->SetVirtualMethodsPtr(new_virtual_methods); 965 } 966 // Update dex cache strings. 967 GcRoot<mirror::String>* strings = GetDexCacheStrings(); 968 GcRoot<mirror::String>* new_strings = visitor(strings); 969 if (strings != new_strings) { 970 dest->SetDexCacheStrings(new_strings); 971 } 972 // Fix up embedded tables. 973 if (!IsTemp() && ShouldHaveEmbeddedImtAndVTable()) { 974 for (int32_t i = 0, count = GetEmbeddedVTableLength(); i < count; ++i) { 975 ArtMethod* method = GetEmbeddedVTableEntry(i, pointer_size); 976 ArtMethod* new_method = visitor(method); 977 if (method != new_method) { 978 dest->SetEmbeddedVTableEntryUnchecked(i, new_method, pointer_size); 979 } 980 } 981 for (size_t i = 0; i < mirror::Class::kImtSize; ++i) { 982 ArtMethod* method = GetEmbeddedImTableEntry(i, pointer_size); 983 ArtMethod* new_method = visitor(method); 984 if (method != new_method) { 985 dest->SetEmbeddedImTableEntry(i, new_method, pointer_size); 986 } 987 } 988 } 989} 990 991} // namespace mirror 992} // namespace art 993 994#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ 995