class-inl.h revision 1147b9bd68323c753ed1a0b6106b205fd640c820
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 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda()); 396 // The argument method may from a super class. 397 // Use the index to a potentially overridden one for this instance's class. 398 return GetVTableEntry(method->GetMethodIndex(), pointer_size); 399} 400 401inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method, size_t pointer_size) { 402 DCHECK(!method->GetDeclaringClass()->IsInterface()); 403 return GetSuperClass()->GetVTableEntry(method->GetMethodIndex(), pointer_size); 404} 405 406inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method, 407 size_t pointer_size) { 408 if (method->IsDirect()) { 409 return method; 410 } 411 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { 412 return FindVirtualMethodForInterface(method, pointer_size); 413 } 414 return FindVirtualMethodForVirtual(method, pointer_size); 415} 416 417inline IfTable* Class::GetIfTable() { 418 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); 419} 420 421inline int32_t Class::GetIfTableCount() { 422 IfTable* iftable = GetIfTable(); 423 if (iftable == nullptr) { 424 return 0; 425 } 426 return iftable->Count(); 427} 428 429inline void Class::SetIfTable(IfTable* new_iftable) { 430 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); 431} 432 433inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtr() { 434 DCHECK(IsLoaded() || IsErroneous()); 435 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 436} 437 438inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() { 439 Class* super_class = GetSuperClass(); 440 return (super_class != nullptr) 441 ? MemberOffset(RoundUp(super_class->GetObjectSize(), 442 sizeof(mirror::HeapReference<mirror::Object>))) 443 : ClassOffset(); 444} 445 446inline MemberOffset Class::GetFirstReferenceStaticFieldOffset(size_t pointer_size) { 447 DCHECK(IsResolved()); 448 uint32_t base = sizeof(mirror::Class); // Static fields come after the class. 449 if (ShouldHaveEmbeddedImtAndVTable()) { 450 // Static fields come after the embedded tables. 451 base = mirror::Class::ComputeClassSize( 452 true, GetEmbeddedVTableLength(), 0, 0, 0, 0, 0, pointer_size); 453 } 454 return MemberOffset(base); 455} 456 457inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking(size_t pointer_size) { 458 DCHECK(IsLoaded()); 459 uint32_t base = sizeof(mirror::Class); // Static fields come after the class. 460 if (ShouldHaveEmbeddedImtAndVTable()) { 461 // Static fields come after the embedded tables. 462 base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(), 463 0, 0, 0, 0, 0, pointer_size); 464 } 465 return MemberOffset(base); 466} 467 468inline void Class::SetIFieldsPtr(LengthPrefixedArray<ArtField>* new_ifields) { 469 DCHECK(GetIFieldsPtrUnchecked() == nullptr); 470 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); 471} 472 473inline void Class::SetIFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_ifields) { 474 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); 475} 476 477inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtrUnchecked() { 478 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); 479} 480 481inline LengthPrefixedArray<ArtField>* Class::GetIFieldsPtrUnchecked() { 482 return GetFieldPtr<LengthPrefixedArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 483} 484 485inline LengthPrefixedArray<ArtField>* Class::GetSFieldsPtr() { 486 DCHECK(IsLoaded() || IsErroneous()) << GetStatus(); 487 return GetSFieldsPtrUnchecked(); 488} 489 490inline void Class::SetSFieldsPtr(LengthPrefixedArray<ArtField>* new_sfields) { 491 DCHECK((IsRetired() && new_sfields == nullptr) || 492 GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr); 493 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); 494} 495 496inline void Class::SetSFieldsPtrUnchecked(LengthPrefixedArray<ArtField>* new_sfields) { 497 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); 498} 499 500inline ArtField* Class::GetStaticField(uint32_t i) { 501 return &GetSFieldsPtr()->At(i); 502} 503 504inline ArtField* Class::GetInstanceField(uint32_t i) { 505 return &GetIFieldsPtr()->At(i); 506} 507 508template<VerifyObjectFlags kVerifyFlags> 509inline uint32_t Class::GetReferenceInstanceOffsets() { 510 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); 511 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); 512} 513 514inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { 515 if (Runtime::Current()->IsActiveTransaction()) { 516 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 517 } else { 518 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 519 } 520} 521 522inline void Class::SetVerifyErrorClass(Class* klass) { 523 CHECK(klass != nullptr) << PrettyClass(this); 524 if (Runtime::Current()->IsActiveTransaction()) { 525 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); 526 } else { 527 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); 528 } 529} 530 531template<VerifyObjectFlags kVerifyFlags> 532inline uint32_t Class::GetAccessFlags() { 533 // Check class is loaded/retired or this is java.lang.String that has a 534 // circularity issue during loading the names of its members 535 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || 536 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || 537 this == String::GetJavaLangString()) 538 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() 539 << " IsRetired=" << IsRetired<kVerifyFlags>() 540 << " IsErroneous=" << 541 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() 542 << " IsString=" << (this == String::GetJavaLangString()) 543 << " descriptor=" << PrettyDescriptor(this); 544 return GetField32<kVerifyFlags>(AccessFlagsOffset()); 545} 546 547inline String* Class::GetName() { 548 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); 549} 550 551inline void Class::SetName(String* name) { 552 if (Runtime::Current()->IsActiveTransaction()) { 553 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 554 } else { 555 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 556 } 557} 558 559template<VerifyObjectFlags kVerifyFlags> 560inline Primitive::Type Class::GetPrimitiveType() { 561 static_assert(sizeof(Primitive::Type) == sizeof(int32_t), 562 "art::Primitive::Type and int32_t have different sizes."); 563 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); 564 Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF); 565 DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type)); 566 return type; 567} 568 569template<VerifyObjectFlags kVerifyFlags> 570inline size_t Class::GetPrimitiveTypeSizeShift() { 571 static_assert(sizeof(Primitive::Type) == sizeof(int32_t), 572 "art::Primitive::Type and int32_t have different sizes."); 573 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)); 574 size_t size_shift = static_cast<Primitive::Type>(v32 >> 16); 575 DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF))); 576 return size_shift; 577} 578 579inline void Class::CheckObjectAlloc() { 580 DCHECK(!IsArrayClass()) 581 << PrettyClass(this) 582 << "A array shouldn't be allocated through this " 583 << "as it requires a pre-fence visitor that sets the class size."; 584 DCHECK(!IsClassClass()) 585 << PrettyClass(this) 586 << "A class object shouldn't be allocated through this " 587 << "as it requires a pre-fence visitor that sets the class size."; 588 DCHECK(!IsStringClass()) 589 << PrettyClass(this) 590 << "A string shouldn't be allocated through this " 591 << "as it requires a pre-fence visitor that sets the class size."; 592 DCHECK(IsInstantiable()) << PrettyClass(this); 593 // TODO: decide whether we want this check. It currently fails during bootstrap. 594 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); 595 DCHECK_GE(this->object_size_, sizeof(Object)); 596} 597 598template<bool kIsInstrumented, bool kCheckAddFinalizer> 599inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { 600 CheckObjectAlloc(); 601 gc::Heap* heap = Runtime::Current()->GetHeap(); 602 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); 603 if (!kCheckAddFinalizer) { 604 DCHECK(!IsFinalizable()); 605 } 606 mirror::Object* obj = 607 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, 608 allocator_type, VoidFunctor()); 609 if (add_finalizer && LIKELY(obj != nullptr)) { 610 heap->AddFinalizerReference(self, &obj); 611 if (UNLIKELY(self->IsExceptionPending())) { 612 // Failed to allocate finalizer reference, it means that the whole allocation failed. 613 obj = nullptr; 614 } 615 } 616 return obj; 617} 618 619inline Object* Class::AllocObject(Thread* self) { 620 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); 621} 622 623inline Object* Class::AllocNonMovableObject(Thread* self) { 624 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); 625} 626 627inline uint32_t Class::ComputeClassSize(bool has_embedded_tables, 628 uint32_t num_vtable_entries, 629 uint32_t num_8bit_static_fields, 630 uint32_t num_16bit_static_fields, 631 uint32_t num_32bit_static_fields, 632 uint32_t num_64bit_static_fields, 633 uint32_t num_ref_static_fields, 634 size_t pointer_size) { 635 // Space used by java.lang.Class and its instance fields. 636 uint32_t size = sizeof(Class); 637 // Space used by embedded tables. 638 if (has_embedded_tables) { 639 const uint32_t embedded_imt_size = kImtSize * ImTableEntrySize(pointer_size); 640 const uint32_t embedded_vtable_size = num_vtable_entries * VTableEntrySize(pointer_size); 641 size = RoundUp(size + sizeof(uint32_t) /* embedded vtable len */, pointer_size) + 642 embedded_imt_size + embedded_vtable_size; 643 } 644 645 // Space used by reference statics. 646 size += num_ref_static_fields * sizeof(HeapReference<Object>); 647 if (!IsAligned<8>(size) && num_64bit_static_fields > 0) { 648 uint32_t gap = 8 - (size & 0x7); 649 size += gap; // will be padded 650 // Shuffle 4-byte fields forward. 651 while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) { 652 --num_32bit_static_fields; 653 gap -= sizeof(uint32_t); 654 } 655 // Shuffle 2-byte fields forward. 656 while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) { 657 --num_16bit_static_fields; 658 gap -= sizeof(uint16_t); 659 } 660 // Shuffle byte fields forward. 661 while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) { 662 --num_8bit_static_fields; 663 gap -= sizeof(uint8_t); 664 } 665 } 666 // Guaranteed to be at least 4 byte aligned. No need for further alignments. 667 // Space used for primitive static fields. 668 size += num_8bit_static_fields * sizeof(uint8_t) + num_16bit_static_fields * sizeof(uint16_t) + 669 num_32bit_static_fields * sizeof(uint32_t) + num_64bit_static_fields * sizeof(uint64_t); 670 return size; 671} 672 673template <typename Visitor> 674inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { 675 VisitInstanceFieldsReferences(klass, visitor); 676 // Right after a class is allocated, but not yet loaded 677 // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it 678 // and scan it. IsTemp() may call Class::GetAccessFlags() but may 679 // fail in the DCHECK in Class::GetAccessFlags() because the class 680 // status is kStatusNotReady. To avoid it, rely on IsResolved() 681 // only. This is fine because a temp class never goes into the 682 // kStatusResolved state. 683 if (IsResolved()) { 684 // Temp classes don't ever populate imt/vtable or static fields and they are not even 685 // allocated with the right size for those. Also, unresolved classes don't have fields 686 // linked yet. 687 VisitStaticFieldsReferences(this, visitor); 688 } 689 // Since this class is reachable, we must also visit the associated roots when we scan it. 690 VisitNativeRoots(visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); 691} 692 693template<ReadBarrierOption kReadBarrierOption> 694inline bool Class::IsReferenceClass() const { 695 return this == Reference::GetJavaLangRefReference<kReadBarrierOption>(); 696} 697 698template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 699inline bool Class::IsClassClass() { 700 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> 701 template GetClass<kVerifyFlags, kReadBarrierOption>(); 702 return this == java_lang_Class; 703} 704 705inline const DexFile& Class::GetDexFile() { 706 return *GetDexCache()->GetDexFile(); 707} 708 709inline bool Class::DescriptorEquals(const char* match) { 710 if (IsArrayClass()) { 711 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1); 712 } else if (IsPrimitive()) { 713 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0; 714 } else if (IsProxyClass()) { 715 return ProxyDescriptorEquals(match); 716 } else { 717 const DexFile& dex_file = GetDexFile(); 718 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); 719 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0; 720 } 721} 722 723inline void Class::AssertInitializedOrInitializingInThread(Thread* self) { 724 if (kIsDebugBuild && !IsInitialized()) { 725 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus(); 726 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this) 727 << " is initializing in a different thread"; 728 } 729} 730 731inline ObjectArray<Class>* Class::GetInterfaces() { 732 CHECK(IsProxyClass()); 733 // First static field. 734 auto* field = GetStaticField(0); 735 DCHECK_STREQ(field->GetName(), "interfaces"); 736 MemberOffset field_offset = field->GetOffset(); 737 return GetFieldObject<ObjectArray<Class>>(field_offset); 738} 739 740inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() { 741 CHECK(IsProxyClass()); 742 // Second static field. 743 auto* field = GetStaticField(1); 744 DCHECK_STREQ(field->GetName(), "throws"); 745 MemberOffset field_offset = field->GetOffset(); 746 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset); 747} 748 749inline MemberOffset Class::GetDisableIntrinsicFlagOffset() { 750 CHECK(IsReferenceClass()); 751 // First static field 752 auto* field = GetStaticField(0); 753 DCHECK_STREQ(field->GetName(), "disableIntrinsic"); 754 return field->GetOffset(); 755} 756 757inline MemberOffset Class::GetSlowPathFlagOffset() { 758 CHECK(IsReferenceClass()); 759 // Second static field 760 auto* field = GetStaticField(1); 761 DCHECK_STREQ(field->GetName(), "slowPathEnabled"); 762 return field->GetOffset(); 763} 764 765inline bool Class::GetSlowPathEnabled() { 766 return GetFieldBoolean(GetSlowPathFlagOffset()); 767} 768 769inline void Class::SetSlowPath(bool enabled) { 770 SetFieldBoolean<false, false>(GetSlowPathFlagOffset(), enabled); 771} 772 773inline void Class::InitializeClassVisitor::operator()( 774 mirror::Object* obj, size_t usable_size) const { 775 DCHECK_LE(class_size_, usable_size); 776 // Avoid AsClass as object is not yet in live bitmap or allocation stack. 777 mirror::Class* klass = down_cast<mirror::Class*>(obj); 778 // DCHECK(klass->IsClass()); 779 klass->SetClassSize(class_size_); 780 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. 781 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. 782 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. 783} 784 785inline void Class::SetAccessFlags(uint32_t new_access_flags) { 786 // Called inside a transaction when setting pre-verified flag during boot image compilation. 787 if (Runtime::Current()->IsActiveTransaction()) { 788 SetField32<true>(AccessFlagsOffset(), new_access_flags); 789 } else { 790 SetField32<false>(AccessFlagsOffset(), new_access_flags); 791 } 792} 793 794inline void Class::SetClassFlags(uint32_t new_flags) { 795 if (Runtime::Current()->IsActiveTransaction()) { 796 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); 797 } else { 798 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_flags_), new_flags); 799 } 800} 801 802inline uint32_t Class::NumDirectInterfaces() { 803 if (IsPrimitive()) { 804 return 0; 805 } else if (IsArrayClass()) { 806 return 2; 807 } else if (IsProxyClass()) { 808 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces(); 809 return interfaces != nullptr ? interfaces->GetLength() : 0; 810 } else { 811 const DexFile::TypeList* interfaces = GetInterfaceTypeList(); 812 if (interfaces == nullptr) { 813 return 0; 814 } else { 815 return interfaces->Size(); 816 } 817 } 818} 819 820inline void Class::SetDexCacheStrings(GcRoot<String>* new_dex_cache_strings) { 821 SetFieldPtr<false>(DexCacheStringsOffset(), new_dex_cache_strings); 822} 823 824inline GcRoot<String>* Class::GetDexCacheStrings() { 825 return GetFieldPtr<GcRoot<String>*>(DexCacheStringsOffset()); 826} 827 828template<class Visitor> 829void mirror::Class::VisitNativeRoots(Visitor& visitor, size_t pointer_size) { 830 for (ArtField& field : GetSFieldsUnchecked()) { 831 // Visit roots first in case the declaring class gets moved. 832 field.VisitRoots(visitor); 833 if (kIsDebugBuild && IsResolved()) { 834 CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); 835 } 836 } 837 for (ArtField& field : GetIFieldsUnchecked()) { 838 // Visit roots first in case the declaring class gets moved. 839 field.VisitRoots(visitor); 840 if (kIsDebugBuild && IsResolved()) { 841 CHECK_EQ(field.GetDeclaringClass(), this) << GetStatus(); 842 } 843 } 844 for (ArtMethod& method : GetDirectMethods(pointer_size)) { 845 method.VisitRoots(visitor, pointer_size); 846 } 847 for (ArtMethod& method : GetVirtualMethods(pointer_size)) { 848 method.VisitRoots(visitor, pointer_size); 849 } 850} 851 852inline IterationRange<StrideIterator<ArtMethod>> Class::GetDirectMethods(size_t pointer_size) { 853 CheckPointerSize(pointer_size); 854 return MakeIterationRangeFromLengthPrefixedArray(GetDirectMethodsPtrUnchecked(), 855 ArtMethod::Size(pointer_size), 856 ArtMethod::Alignment(pointer_size)); 857} 858 859inline IterationRange<StrideIterator<ArtMethod>> Class::GetVirtualMethods(size_t pointer_size) { 860 CheckPointerSize(pointer_size); 861 return MakeIterationRangeFromLengthPrefixedArray(GetVirtualMethodsPtrUnchecked(), 862 ArtMethod::Size(pointer_size), 863 ArtMethod::Alignment(pointer_size)); 864} 865 866inline IterationRange<StrideIterator<ArtField>> Class::GetIFields() { 867 return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtr()); 868} 869 870inline IterationRange<StrideIterator<ArtField>> Class::GetSFields() { 871 return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtr()); 872} 873 874inline IterationRange<StrideIterator<ArtField>> Class::GetIFieldsUnchecked() { 875 return MakeIterationRangeFromLengthPrefixedArray(GetIFieldsPtrUnchecked()); 876} 877 878inline IterationRange<StrideIterator<ArtField>> Class::GetSFieldsUnchecked() { 879 return MakeIterationRangeFromLengthPrefixedArray(GetSFieldsPtrUnchecked()); 880} 881 882inline MemberOffset Class::EmbeddedImTableOffset(size_t pointer_size) { 883 CheckPointerSize(pointer_size); 884 // Round up since we want the embedded imt and vtable to be pointer size aligned in case 64 bits. 885 // Add 32 bits for embedded vtable length. 886 return MemberOffset( 887 RoundUp(EmbeddedVTableLengthOffset().Uint32Value() + sizeof(uint32_t), pointer_size)); 888} 889 890inline MemberOffset Class::EmbeddedVTableOffset(size_t pointer_size) { 891 CheckPointerSize(pointer_size); 892 return MemberOffset(EmbeddedImTableOffset(pointer_size).Uint32Value() + 893 kImtSize * ImTableEntrySize(pointer_size)); 894} 895 896inline void Class::CheckPointerSize(size_t pointer_size) { 897 DCHECK(ValidPointerSize(pointer_size)) << pointer_size; 898 DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); 899} 900 901template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 902inline Class* Class::GetComponentType() { 903 return GetFieldObject<Class, kVerifyFlags, kReadBarrierOption>(ComponentTypeOffset()); 904} 905 906template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 907inline bool Class::IsArrayClass() { 908 return GetComponentType<kVerifyFlags, kReadBarrierOption>() != nullptr; 909} 910 911inline bool Class::IsAssignableFrom(Class* src) { 912 DCHECK(src != nullptr); 913 if (this == src) { 914 // Can always assign to things of the same type. 915 return true; 916 } else if (IsObjectClass()) { 917 // Can assign any reference to java.lang.Object. 918 return !src->IsPrimitive(); 919 } else if (IsInterface()) { 920 return src->Implements(this); 921 } else if (src->IsArrayClass()) { 922 return IsAssignableFromArray(src); 923 } else { 924 return !src->IsInterface() && src->IsSubClass(this); 925 } 926} 927 928inline uint32_t Class::NumDirectMethods() { 929 LengthPrefixedArray<ArtMethod>* arr = GetDirectMethodsPtrUnchecked(); 930 return arr != nullptr ? arr->Length() : 0u; 931} 932 933inline uint32_t Class::NumVirtualMethods() { 934 LengthPrefixedArray<ArtMethod>* arr = GetVirtualMethodsPtrUnchecked(); 935 return arr != nullptr ? arr->Length() : 0u; 936} 937 938inline uint32_t Class::NumInstanceFields() { 939 LengthPrefixedArray<ArtField>* arr = GetIFieldsPtrUnchecked(); 940 return arr != nullptr ? arr->Length() : 0u; 941} 942 943inline uint32_t Class::NumStaticFields() { 944 LengthPrefixedArray<ArtField>* arr = GetSFieldsPtrUnchecked(); 945 return arr != nullptr ? arr->Length() : 0u; 946} 947 948} // namespace mirror 949} // namespace art 950 951#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ 952