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_ART_METHOD_H_ 18 #define ART_RUNTIME_ART_METHOD_H_ 19 20 #include <cstddef> 21 #include <limits> 22 23 #include <android-base/logging.h> 24 #include <jni.h> 25 26 #include "base/array_ref.h" 27 #include "base/bit_utils.h" 28 #include "base/casts.h" 29 #include "base/enums.h" 30 #include "base/macros.h" 31 #include "base/runtime_debug.h" 32 #include "dex/dex_file_structs.h" 33 #include "dex/modifiers.h" 34 #include "dex/primitive.h" 35 #include "gc_root.h" 36 #include "obj_ptr.h" 37 #include "offsets.h" 38 #include "read_barrier_option.h" 39 40 namespace art { 41 42 class CodeItemDataAccessor; 43 class CodeItemDebugInfoAccessor; 44 class CodeItemInstructionAccessor; 45 class DexFile; 46 template<class T> class Handle; 47 class ImtConflictTable; 48 enum InvokeType : uint32_t; 49 union JValue; 50 class OatQuickMethodHeader; 51 class ProfilingInfo; 52 class ScopedObjectAccessAlreadyRunnable; 53 class ShadowFrame; 54 class Signature; 55 56 namespace mirror { 57 class Array; 58 class Class; 59 class ClassLoader; 60 class DexCache; 61 class IfTable; 62 class Object; 63 template <typename MirrorType> class ObjectArray; 64 class PointerArray; 65 class String; 66 67 template <typename T> struct NativeDexCachePair; 68 using MethodDexCachePair = NativeDexCachePair<ArtMethod>; 69 using MethodDexCacheType = std::atomic<MethodDexCachePair>; 70 } // namespace mirror 71 72 class ArtMethod final { 73 public: 74 // Should the class state be checked on sensitive operations? 75 DECLARE_RUNTIME_DEBUG_FLAG(kCheckDeclaringClassState); 76 77 // The runtime dex_method_index is kDexNoIndex. To lower dependencies, we use this 78 // constexpr, and ensure that the value is correct in art_method.cc. 79 static constexpr uint32_t kRuntimeMethodDexMethodIndex = 0xFFFFFFFF; 80 ArtMethod()81 ArtMethod() : access_flags_(0), dex_code_item_offset_(0), dex_method_index_(0), 82 method_index_(0), hotness_count_(0) { } 83 ArtMethod(ArtMethod * src,PointerSize image_pointer_size)84 ArtMethod(ArtMethod* src, PointerSize image_pointer_size) { 85 CopyFrom(src, image_pointer_size); 86 } 87 88 static ArtMethod* FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, 89 jobject jlr_method) 90 REQUIRES_SHARED(Locks::mutator_lock_); 91 92 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 93 ALWAYS_INLINE ObjPtr<mirror::Class> GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_); 94 95 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 96 ALWAYS_INLINE ObjPtr<mirror::Class> GetDeclaringClassUnchecked() 97 REQUIRES_SHARED(Locks::mutator_lock_); 98 GetDeclaringClassAddressWithoutBarrier()99 mirror::CompressedReference<mirror::Object>* GetDeclaringClassAddressWithoutBarrier() { 100 return declaring_class_.AddressWithoutBarrier(); 101 } 102 103 void SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) 104 REQUIRES_SHARED(Locks::mutator_lock_); 105 106 bool CASDeclaringClass(ObjPtr<mirror::Class> expected_class, ObjPtr<mirror::Class> desired_class) 107 REQUIRES_SHARED(Locks::mutator_lock_); 108 DeclaringClassOffset()109 static constexpr MemberOffset DeclaringClassOffset() { 110 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, declaring_class_)); 111 } 112 GetAccessFlags()113 uint32_t GetAccessFlags() const { 114 return access_flags_.load(std::memory_order_relaxed); 115 } 116 117 // This version should only be called when it's certain there is no 118 // concurrency so there is no need to guarantee atomicity. For example, 119 // before the method is linked. SetAccessFlags(uint32_t new_access_flags)120 void SetAccessFlags(uint32_t new_access_flags) REQUIRES_SHARED(Locks::mutator_lock_) { 121 access_flags_.store(new_access_flags, std::memory_order_relaxed); 122 } 123 AccessFlagsOffset()124 static constexpr MemberOffset AccessFlagsOffset() { 125 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, access_flags_)); 126 } 127 128 // Approximate what kind of method call would be used for this method. 129 InvokeType GetInvokeType() REQUIRES_SHARED(Locks::mutator_lock_); 130 131 // Returns true if the method is declared public. IsPublic()132 bool IsPublic() const { 133 return (GetAccessFlags() & kAccPublic) != 0; 134 } 135 136 // Returns true if the method is declared private. IsPrivate()137 bool IsPrivate() const { 138 return (GetAccessFlags() & kAccPrivate) != 0; 139 } 140 141 // Returns true if the method is declared static. IsStatic()142 bool IsStatic() const { 143 return (GetAccessFlags() & kAccStatic) != 0; 144 } 145 146 // Returns true if the method is a constructor according to access flags. IsConstructor()147 bool IsConstructor() const { 148 return (GetAccessFlags() & kAccConstructor) != 0; 149 } 150 151 // Returns true if the method is a class initializer according to access flags. IsClassInitializer()152 bool IsClassInitializer() const { 153 return IsConstructor() && IsStatic(); 154 } 155 156 // Returns true if the method is static, private, or a constructor. IsDirect()157 bool IsDirect() const { 158 return IsDirect(GetAccessFlags()); 159 } 160 IsDirect(uint32_t access_flags)161 static bool IsDirect(uint32_t access_flags) { 162 constexpr uint32_t direct = kAccStatic | kAccPrivate | kAccConstructor; 163 return (access_flags & direct) != 0; 164 } 165 166 // Returns true if the method is declared synchronized. IsSynchronized()167 bool IsSynchronized() const { 168 constexpr uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; 169 return (GetAccessFlags() & synchonized) != 0; 170 } 171 IsFinal()172 bool IsFinal() const { 173 return (GetAccessFlags() & kAccFinal) != 0; 174 } 175 IsIntrinsic()176 bool IsIntrinsic() const { 177 return (GetAccessFlags() & kAccIntrinsic) != 0; 178 } 179 180 ALWAYS_INLINE void SetIntrinsic(uint32_t intrinsic) REQUIRES_SHARED(Locks::mutator_lock_); 181 GetIntrinsic()182 uint32_t GetIntrinsic() const { 183 static const int kAccFlagsShift = CTZ(kAccIntrinsicBits); 184 static_assert(IsPowerOfTwo((kAccIntrinsicBits >> kAccFlagsShift) + 1), 185 "kAccIntrinsicBits are not continuous"); 186 static_assert((kAccIntrinsic & kAccIntrinsicBits) == 0, 187 "kAccIntrinsic overlaps kAccIntrinsicBits"); 188 DCHECK(IsIntrinsic()); 189 return (GetAccessFlags() & kAccIntrinsicBits) >> kAccFlagsShift; 190 } 191 192 void SetNotIntrinsic() REQUIRES_SHARED(Locks::mutator_lock_); 193 IsCopied()194 bool IsCopied() const { 195 static_assert((kAccCopied & (kAccIntrinsic | kAccIntrinsicBits)) == 0, 196 "kAccCopied conflicts with intrinsic modifier"); 197 const bool copied = (GetAccessFlags() & kAccCopied) != 0; 198 // (IsMiranda() || IsDefaultConflicting()) implies copied 199 DCHECK(!(IsMiranda() || IsDefaultConflicting()) || copied) 200 << "Miranda or default-conflict methods must always be copied."; 201 return copied; 202 } 203 IsMiranda()204 bool IsMiranda() const { 205 // The kAccMiranda flag value is used with a different meaning for native methods and methods 206 // marked kAccCompileDontBother, so we need to check these flags as well. 207 return (GetAccessFlags() & (kAccNative | kAccMiranda | kAccCompileDontBother)) == kAccMiranda; 208 } 209 210 // Returns true if invoking this method will not throw an AbstractMethodError or 211 // IncompatibleClassChangeError. IsInvokable()212 bool IsInvokable() const { 213 return !IsAbstract() && !IsDefaultConflicting(); 214 } 215 IsPreCompiled()216 bool IsPreCompiled() const { 217 if (IsIntrinsic()) { 218 // kAccCompileDontBother overlaps with kAccIntrinsicBits. 219 return false; 220 } 221 uint32_t expected = (kAccPreCompiled | kAccCompileDontBother); 222 return (GetAccessFlags() & expected) == expected; 223 } 224 SetPreCompiled()225 void SetPreCompiled() REQUIRES_SHARED(Locks::mutator_lock_) { 226 DCHECK(IsInvokable()); 227 DCHECK(IsCompilable()); 228 AddAccessFlags(kAccPreCompiled | kAccCompileDontBother); 229 } 230 ClearPreCompiled()231 void ClearPreCompiled() REQUIRES_SHARED(Locks::mutator_lock_) { 232 ClearAccessFlags(kAccPreCompiled | kAccCompileDontBother); 233 } 234 IsCompilable()235 bool IsCompilable() const { 236 if (IsIntrinsic()) { 237 // kAccCompileDontBother overlaps with kAccIntrinsicBits. 238 return true; 239 } 240 if (IsPreCompiled()) { 241 return true; 242 } 243 return (GetAccessFlags() & kAccCompileDontBother) == 0; 244 } 245 ClearDontCompile()246 void ClearDontCompile() REQUIRES_SHARED(Locks::mutator_lock_) { 247 DCHECK(!IsMiranda()); 248 ClearAccessFlags(kAccCompileDontBother); 249 } 250 SetDontCompile()251 void SetDontCompile() REQUIRES_SHARED(Locks::mutator_lock_) { 252 DCHECK(!IsMiranda()); 253 AddAccessFlags(kAccCompileDontBother); 254 } 255 256 // A default conflict method is a special sentinel method that stands for a conflict between 257 // multiple default methods. It cannot be invoked, throwing an IncompatibleClassChangeError if one 258 // attempts to do so. IsDefaultConflicting()259 bool IsDefaultConflicting() const { 260 if (IsIntrinsic()) { 261 return false; 262 } 263 return (GetAccessFlags() & kAccDefaultConflict) != 0u; 264 } 265 266 // This is set by the class linker. IsDefault()267 bool IsDefault() const { 268 static_assert((kAccDefault & (kAccIntrinsic | kAccIntrinsicBits)) == 0, 269 "kAccDefault conflicts with intrinsic modifier"); 270 return (GetAccessFlags() & kAccDefault) != 0; 271 } 272 IsObsolete()273 bool IsObsolete() const { 274 return (GetAccessFlags() & kAccObsoleteMethod) != 0; 275 } 276 SetIsObsolete()277 void SetIsObsolete() REQUIRES_SHARED(Locks::mutator_lock_) { 278 AddAccessFlags(kAccObsoleteMethod); 279 } 280 IsNative()281 bool IsNative() const { 282 return (GetAccessFlags() & kAccNative) != 0; 283 } 284 285 // Checks to see if the method was annotated with @dalvik.annotation.optimization.FastNative. IsFastNative()286 bool IsFastNative() const { 287 // The presence of the annotation is checked by ClassLinker and recorded in access flags. 288 // The kAccFastNative flag value is used with a different meaning for non-native methods, 289 // so we need to check the kAccNative flag as well. 290 constexpr uint32_t mask = kAccFastNative | kAccNative; 291 return (GetAccessFlags() & mask) == mask; 292 } 293 294 // Checks to see if the method was annotated with @dalvik.annotation.optimization.CriticalNative. IsCriticalNative()295 bool IsCriticalNative() const { 296 // The presence of the annotation is checked by ClassLinker and recorded in access flags. 297 // The kAccCriticalNative flag value is used with a different meaning for non-native methods, 298 // so we need to check the kAccNative flag as well. 299 constexpr uint32_t mask = kAccCriticalNative | kAccNative; 300 return (GetAccessFlags() & mask) == mask; 301 } 302 IsAbstract()303 bool IsAbstract() const { 304 return (GetAccessFlags() & kAccAbstract) != 0; 305 } 306 IsSynthetic()307 bool IsSynthetic() const { 308 return (GetAccessFlags() & kAccSynthetic) != 0; 309 } 310 IsVarargs()311 bool IsVarargs() const { 312 return (GetAccessFlags() & kAccVarargs) != 0; 313 } 314 315 bool IsProxyMethod() REQUIRES_SHARED(Locks::mutator_lock_); 316 317 bool IsPolymorphicSignature() REQUIRES_SHARED(Locks::mutator_lock_); 318 UseFastInterpreterToInterpreterInvoke()319 bool UseFastInterpreterToInterpreterInvoke() const { 320 // The bit is applicable only if the method is not intrinsic. 321 constexpr uint32_t mask = kAccFastInterpreterToInterpreterInvoke | kAccIntrinsic; 322 return (GetAccessFlags() & mask) == kAccFastInterpreterToInterpreterInvoke; 323 } 324 SetFastInterpreterToInterpreterInvokeFlag()325 void SetFastInterpreterToInterpreterInvokeFlag() REQUIRES_SHARED(Locks::mutator_lock_) { 326 DCHECK(!IsIntrinsic()); 327 AddAccessFlags(kAccFastInterpreterToInterpreterInvoke); 328 } 329 ClearFastInterpreterToInterpreterInvokeFlag()330 void ClearFastInterpreterToInterpreterInvokeFlag() REQUIRES_SHARED(Locks::mutator_lock_) { 331 if (!IsIntrinsic()) { 332 ClearAccessFlags(kAccFastInterpreterToInterpreterInvoke); 333 } 334 } 335 SkipAccessChecks()336 bool SkipAccessChecks() const { 337 // The kAccSkipAccessChecks flag value is used with a different meaning for native methods, 338 // so we need to check the kAccNative flag as well. 339 return (GetAccessFlags() & (kAccSkipAccessChecks | kAccNative)) == kAccSkipAccessChecks; 340 } 341 SetSkipAccessChecks()342 void SetSkipAccessChecks() REQUIRES_SHARED(Locks::mutator_lock_) { 343 // SkipAccessChecks() is applicable only to non-native methods. 344 DCHECK(!IsNative()); 345 AddAccessFlags(kAccSkipAccessChecks); 346 } ClearSkipAccessChecks()347 void ClearSkipAccessChecks() REQUIRES_SHARED(Locks::mutator_lock_) { 348 // SkipAccessChecks() is applicable only to non-native methods. 349 DCHECK(!IsNative()); 350 ClearAccessFlags(kAccSkipAccessChecks); 351 } 352 PreviouslyWarm()353 bool PreviouslyWarm() const { 354 if (IsIntrinsic()) { 355 // kAccPreviouslyWarm overlaps with kAccIntrinsicBits. 356 return true; 357 } 358 return (GetAccessFlags() & kAccPreviouslyWarm) != 0; 359 } 360 SetPreviouslyWarm()361 void SetPreviouslyWarm() REQUIRES_SHARED(Locks::mutator_lock_) { 362 if (IsIntrinsic()) { 363 // kAccPreviouslyWarm overlaps with kAccIntrinsicBits. 364 return; 365 } 366 AddAccessFlags(kAccPreviouslyWarm); 367 } 368 369 // Should this method be run in the interpreter and count locks (e.g., failed structured- 370 // locking verification)? MustCountLocks()371 bool MustCountLocks() const { 372 if (IsIntrinsic()) { 373 return false; 374 } 375 return (GetAccessFlags() & kAccMustCountLocks) != 0; 376 } 377 ClearMustCountLocks()378 void ClearMustCountLocks() REQUIRES_SHARED(Locks::mutator_lock_) { 379 ClearAccessFlags(kAccMustCountLocks); 380 } 381 SetMustCountLocks()382 void SetMustCountLocks() REQUIRES_SHARED(Locks::mutator_lock_) { 383 AddAccessFlags(kAccMustCountLocks); 384 ClearAccessFlags(kAccSkipAccessChecks); 385 } 386 387 // Returns true if this method could be overridden by a default method. 388 bool IsOverridableByDefaultMethod() REQUIRES_SHARED(Locks::mutator_lock_); 389 390 bool CheckIncompatibleClassChange(InvokeType type) REQUIRES_SHARED(Locks::mutator_lock_); 391 392 // Throws the error that would result from trying to invoke this method (i.e. 393 // IncompatibleClassChangeError or AbstractMethodError). Only call if !IsInvokable(); 394 void ThrowInvocationTimeError() REQUIRES_SHARED(Locks::mutator_lock_); 395 396 uint16_t GetMethodIndex() REQUIRES_SHARED(Locks::mutator_lock_); 397 398 // Doesn't do erroneous / unresolved class checks. 399 uint16_t GetMethodIndexDuringLinking() REQUIRES_SHARED(Locks::mutator_lock_); 400 GetVtableIndex()401 size_t GetVtableIndex() REQUIRES_SHARED(Locks::mutator_lock_) { 402 return GetMethodIndex(); 403 } 404 SetMethodIndex(uint16_t new_method_index)405 void SetMethodIndex(uint16_t new_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { 406 // Not called within a transaction. 407 method_index_ = new_method_index; 408 } 409 DexMethodIndexOffset()410 static constexpr MemberOffset DexMethodIndexOffset() { 411 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, dex_method_index_)); 412 } 413 MethodIndexOffset()414 static constexpr MemberOffset MethodIndexOffset() { 415 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, method_index_)); 416 } 417 ImtIndexOffset()418 static constexpr MemberOffset ImtIndexOffset() { 419 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, imt_index_)); 420 } 421 GetCodeItemOffset()422 uint32_t GetCodeItemOffset() const { 423 return dex_code_item_offset_; 424 } 425 SetCodeItemOffset(uint32_t new_code_off)426 void SetCodeItemOffset(uint32_t new_code_off) REQUIRES_SHARED(Locks::mutator_lock_) { 427 // Not called within a transaction. 428 dex_code_item_offset_ = new_code_off; 429 } 430 431 // Number of 32bit registers that would be required to hold all the arguments 432 static size_t NumArgRegisters(const char* shorty); 433 GetDexMethodIndex()434 ALWAYS_INLINE uint32_t GetDexMethodIndex() const { 435 return dex_method_index_; 436 } 437 SetDexMethodIndex(uint32_t new_idx)438 void SetDexMethodIndex(uint32_t new_idx) REQUIRES_SHARED(Locks::mutator_lock_) { 439 // Not called within a transaction. 440 dex_method_index_ = new_idx; 441 } 442 443 // Lookup the Class from the type index into this method's dex cache. 444 ObjPtr<mirror::Class> LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) 445 REQUIRES_SHARED(Locks::mutator_lock_); 446 // Resolve the Class from the type index into this method's dex cache. 447 ObjPtr<mirror::Class> ResolveClassFromTypeIndex(dex::TypeIndex type_idx) 448 REQUIRES_SHARED(Locks::mutator_lock_); 449 450 // Returns true if this method has the same name and signature of the other method. 451 bool HasSameNameAndSignature(ArtMethod* other) REQUIRES_SHARED(Locks::mutator_lock_); 452 453 // Find the method that this method overrides. 454 ArtMethod* FindOverriddenMethod(PointerSize pointer_size) 455 REQUIRES_SHARED(Locks::mutator_lock_); 456 457 // Find the method index for this method within other_dexfile. If this method isn't present then 458 // return dex::kDexNoIndex. The name_and_signature_idx MUST refer to a MethodId with the same 459 // name and signature in the other_dexfile, such as the method index used to resolve this method 460 // in the other_dexfile. 461 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, 462 uint32_t name_and_signature_idx) 463 REQUIRES_SHARED(Locks::mutator_lock_); 464 465 void Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result, const char* shorty) 466 REQUIRES_SHARED(Locks::mutator_lock_); 467 GetEntryPointFromQuickCompiledCode()468 const void* GetEntryPointFromQuickCompiledCode() const { 469 return GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize); 470 } 471 ALWAYS_INLINE GetEntryPointFromQuickCompiledCodePtrSize(PointerSize pointer_size)472 const void* GetEntryPointFromQuickCompiledCodePtrSize(PointerSize pointer_size) const { 473 return GetNativePointer<const void*>( 474 EntryPointFromQuickCompiledCodeOffset(pointer_size), pointer_size); 475 } 476 SetEntryPointFromQuickCompiledCode(const void * entry_point_from_quick_compiled_code)477 void SetEntryPointFromQuickCompiledCode(const void* entry_point_from_quick_compiled_code) 478 REQUIRES_SHARED(Locks::mutator_lock_) { 479 SetEntryPointFromQuickCompiledCodePtrSize(entry_point_from_quick_compiled_code, 480 kRuntimePointerSize); 481 } SetEntryPointFromQuickCompiledCodePtrSize(const void * entry_point_from_quick_compiled_code,PointerSize pointer_size)482 ALWAYS_INLINE void SetEntryPointFromQuickCompiledCodePtrSize( 483 const void* entry_point_from_quick_compiled_code, PointerSize pointer_size) 484 REQUIRES_SHARED(Locks::mutator_lock_) { 485 SetNativePointer(EntryPointFromQuickCompiledCodeOffset(pointer_size), 486 entry_point_from_quick_compiled_code, 487 pointer_size); 488 // We might want to invoke compiled code, so don't use the fast path. 489 ClearFastInterpreterToInterpreterInvokeFlag(); 490 } 491 492 // Registers the native method and returns the new entry point. NB The returned entry point might 493 // be different from the native_method argument if some MethodCallback modifies it. 494 const void* RegisterNative(const void* native_method) 495 REQUIRES_SHARED(Locks::mutator_lock_) WARN_UNUSED; 496 497 void UnregisterNative() REQUIRES_SHARED(Locks::mutator_lock_); 498 DataOffset(PointerSize pointer_size)499 static constexpr MemberOffset DataOffset(PointerSize pointer_size) { 500 return MemberOffset(PtrSizedFieldsOffset(pointer_size) + OFFSETOF_MEMBER( 501 PtrSizedFields, data_) / sizeof(void*) * static_cast<size_t>(pointer_size)); 502 } 503 EntryPointFromJniOffset(PointerSize pointer_size)504 static constexpr MemberOffset EntryPointFromJniOffset(PointerSize pointer_size) { 505 return DataOffset(pointer_size); 506 } 507 EntryPointFromQuickCompiledCodeOffset(PointerSize pointer_size)508 static constexpr MemberOffset EntryPointFromQuickCompiledCodeOffset(PointerSize pointer_size) { 509 return MemberOffset(PtrSizedFieldsOffset(pointer_size) + OFFSETOF_MEMBER( 510 PtrSizedFields, entry_point_from_quick_compiled_code_) / sizeof(void*) 511 * static_cast<size_t>(pointer_size)); 512 } 513 GetImtConflictTable(PointerSize pointer_size)514 ImtConflictTable* GetImtConflictTable(PointerSize pointer_size) const { 515 DCHECK(IsRuntimeMethod()); 516 return reinterpret_cast<ImtConflictTable*>(GetDataPtrSize(pointer_size)); 517 } 518 SetImtConflictTable(ImtConflictTable * table,PointerSize pointer_size)519 ALWAYS_INLINE void SetImtConflictTable(ImtConflictTable* table, PointerSize pointer_size) 520 REQUIRES_SHARED(Locks::mutator_lock_) { 521 DCHECK(IsRuntimeMethod()); 522 SetDataPtrSize(table, pointer_size); 523 } 524 GetProfilingInfo(PointerSize pointer_size)525 ProfilingInfo* GetProfilingInfo(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_) { 526 if (UNLIKELY(IsNative() || IsProxyMethod() || !IsInvokable())) { 527 return nullptr; 528 } 529 return reinterpret_cast<ProfilingInfo*>(GetDataPtrSize(pointer_size)); 530 } 531 SetProfilingInfo(ProfilingInfo * info)532 ALWAYS_INLINE void SetProfilingInfo(ProfilingInfo* info) REQUIRES_SHARED(Locks::mutator_lock_) { 533 SetDataPtrSize(info, kRuntimePointerSize); 534 } 535 SetProfilingInfoPtrSize(ProfilingInfo * info,PointerSize pointer_size)536 ALWAYS_INLINE void SetProfilingInfoPtrSize(ProfilingInfo* info, PointerSize pointer_size) 537 REQUIRES_SHARED(Locks::mutator_lock_) { 538 SetDataPtrSize(info, pointer_size); 539 } 540 ProfilingInfoOffset()541 static MemberOffset ProfilingInfoOffset() { 542 DCHECK(IsImagePointerSize(kRuntimePointerSize)); 543 return DataOffset(kRuntimePointerSize); 544 } 545 546 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 547 ALWAYS_INLINE bool HasSingleImplementation() REQUIRES_SHARED(Locks::mutator_lock_); 548 SetHasSingleImplementation(bool single_impl)549 ALWAYS_INLINE void SetHasSingleImplementation(bool single_impl) 550 REQUIRES_SHARED(Locks::mutator_lock_) { 551 DCHECK(!IsIntrinsic()) << "conflict with intrinsic bits"; 552 if (single_impl) { 553 AddAccessFlags(kAccSingleImplementation); 554 } else { 555 ClearAccessFlags(kAccSingleImplementation); 556 } 557 } 558 HasSingleImplementationFlag()559 ALWAYS_INLINE bool HasSingleImplementationFlag() const { 560 return (GetAccessFlags() & kAccSingleImplementation) != 0; 561 } 562 563 // Takes a method and returns a 'canonical' one if the method is default (and therefore 564 // potentially copied from some other class). For example, this ensures that the debugger does not 565 // get confused as to which method we are in. 566 ArtMethod* GetCanonicalMethod(PointerSize pointer_size = kRuntimePointerSize) 567 REQUIRES_SHARED(Locks::mutator_lock_); 568 569 ArtMethod* GetSingleImplementation(PointerSize pointer_size); 570 SetSingleImplementation(ArtMethod * method,PointerSize pointer_size)571 ALWAYS_INLINE void SetSingleImplementation(ArtMethod* method, PointerSize pointer_size) 572 REQUIRES_SHARED(Locks::mutator_lock_) { 573 DCHECK(!IsNative()); 574 // Non-abstract method's single implementation is just itself. 575 DCHECK(IsAbstract()); 576 SetDataPtrSize(method, pointer_size); 577 } 578 GetEntryPointFromJni()579 void* GetEntryPointFromJni() const { 580 DCHECK(IsNative()); 581 return GetEntryPointFromJniPtrSize(kRuntimePointerSize); 582 } 583 GetEntryPointFromJniPtrSize(PointerSize pointer_size)584 ALWAYS_INLINE void* GetEntryPointFromJniPtrSize(PointerSize pointer_size) const { 585 return GetDataPtrSize(pointer_size); 586 } 587 SetEntryPointFromJni(const void * entrypoint)588 void SetEntryPointFromJni(const void* entrypoint) 589 REQUIRES_SHARED(Locks::mutator_lock_) { 590 DCHECK(IsNative()); 591 SetEntryPointFromJniPtrSize(entrypoint, kRuntimePointerSize); 592 } 593 SetEntryPointFromJniPtrSize(const void * entrypoint,PointerSize pointer_size)594 ALWAYS_INLINE void SetEntryPointFromJniPtrSize(const void* entrypoint, PointerSize pointer_size) 595 REQUIRES_SHARED(Locks::mutator_lock_) { 596 SetDataPtrSize(entrypoint, pointer_size); 597 } 598 GetDataPtrSize(PointerSize pointer_size)599 ALWAYS_INLINE void* GetDataPtrSize(PointerSize pointer_size) const { 600 DCHECK(IsImagePointerSize(pointer_size)); 601 return GetNativePointer<void*>(DataOffset(pointer_size), pointer_size); 602 } 603 SetDataPtrSize(const void * data,PointerSize pointer_size)604 ALWAYS_INLINE void SetDataPtrSize(const void* data, PointerSize pointer_size) 605 REQUIRES_SHARED(Locks::mutator_lock_) { 606 DCHECK(IsImagePointerSize(pointer_size)); 607 SetNativePointer(DataOffset(pointer_size), data, pointer_size); 608 } 609 610 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal 611 // conventions for a method of managed code. Returns false for Proxy methods. IsRuntimeMethod()612 ALWAYS_INLINE bool IsRuntimeMethod() const { 613 return dex_method_index_ == kRuntimeMethodDexMethodIndex; 614 } 615 616 // Is this a hand crafted method used for something like describing callee saves? 617 bool IsCalleeSaveMethod() REQUIRES_SHARED(Locks::mutator_lock_); 618 619 bool IsResolutionMethod() REQUIRES_SHARED(Locks::mutator_lock_); 620 621 bool IsImtUnimplementedMethod() REQUIRES_SHARED(Locks::mutator_lock_); 622 623 // Find the catch block for the given exception type and dex_pc. When a catch block is found, 624 // indicates whether the found catch block is responsible for clearing the exception or whether 625 // a move-exception instruction is present. 626 uint32_t FindCatchBlock(Handle<mirror::Class> exception_type, uint32_t dex_pc, 627 bool* has_no_move_exception) 628 REQUIRES_SHARED(Locks::mutator_lock_); 629 630 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires. 631 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename RootVisitorType> 632 void VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) NO_THREAD_SAFETY_ANALYSIS; 633 634 const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_); 635 636 const char* GetDeclaringClassDescriptor() REQUIRES_SHARED(Locks::mutator_lock_); 637 638 ALWAYS_INLINE const char* GetShorty() REQUIRES_SHARED(Locks::mutator_lock_); 639 640 const char* GetShorty(uint32_t* out_length) REQUIRES_SHARED(Locks::mutator_lock_); 641 642 const Signature GetSignature() REQUIRES_SHARED(Locks::mutator_lock_); 643 644 ALWAYS_INLINE const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_); 645 646 ALWAYS_INLINE std::string_view GetNameView() REQUIRES_SHARED(Locks::mutator_lock_); 647 648 ObjPtr<mirror::String> ResolveNameString() REQUIRES_SHARED(Locks::mutator_lock_); 649 650 const dex::CodeItem* GetCodeItem() REQUIRES_SHARED(Locks::mutator_lock_); 651 652 bool IsResolvedTypeIdx(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_); 653 654 int32_t GetLineNumFromDexPC(uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_); 655 656 const dex::ProtoId& GetPrototype() REQUIRES_SHARED(Locks::mutator_lock_); 657 658 const dex::TypeList* GetParameterTypeList() REQUIRES_SHARED(Locks::mutator_lock_); 659 660 const char* GetDeclaringClassSourceFile() REQUIRES_SHARED(Locks::mutator_lock_); 661 662 uint16_t GetClassDefIndex() REQUIRES_SHARED(Locks::mutator_lock_); 663 664 const dex::ClassDef& GetClassDef() REQUIRES_SHARED(Locks::mutator_lock_); 665 666 ALWAYS_INLINE size_t GetNumberOfParameters() REQUIRES_SHARED(Locks::mutator_lock_); 667 668 const char* GetReturnTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_); 669 670 ALWAYS_INLINE Primitive::Type GetReturnTypePrimitive() REQUIRES_SHARED(Locks::mutator_lock_); 671 672 const char* GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) 673 REQUIRES_SHARED(Locks::mutator_lock_); 674 675 // Lookup return type. 676 ObjPtr<mirror::Class> LookupResolvedReturnType() REQUIRES_SHARED(Locks::mutator_lock_); 677 // Resolve return type. May cause thread suspension due to GetClassFromTypeIdx 678 // calling ResolveType this caused a large number of bugs at call sites. 679 ObjPtr<mirror::Class> ResolveReturnType() REQUIRES_SHARED(Locks::mutator_lock_); 680 681 ObjPtr<mirror::ClassLoader> GetClassLoader() REQUIRES_SHARED(Locks::mutator_lock_); 682 683 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 684 ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_); 685 ObjPtr<mirror::DexCache> GetObsoleteDexCache() REQUIRES_SHARED(Locks::mutator_lock_); 686 687 ALWAYS_INLINE ArtMethod* GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) 688 REQUIRES_SHARED(Locks::mutator_lock_); 689 ALWAYS_INLINE ArtMethod* GetInterfaceMethodIfProxy(PointerSize pointer_size) 690 REQUIRES_SHARED(Locks::mutator_lock_); 691 692 ArtMethod* GetNonObsoleteMethod() REQUIRES_SHARED(Locks::mutator_lock_); 693 694 // May cause thread suspension due to class resolution. 695 bool EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) 696 REQUIRES_SHARED(Locks::mutator_lock_); 697 698 // Size of an instance of this native class. Size(PointerSize pointer_size)699 static size_t Size(PointerSize pointer_size) { 700 return PtrSizedFieldsOffset(pointer_size) + 701 (sizeof(PtrSizedFields) / sizeof(void*)) * static_cast<size_t>(pointer_size); 702 } 703 704 // Alignment of an instance of this native class. Alignment(PointerSize pointer_size)705 static size_t Alignment(PointerSize pointer_size) { 706 // The ArtMethod alignment is the same as image pointer size. This differs from 707 // alignof(ArtMethod) if cross-compiling with pointer_size != sizeof(void*). 708 return static_cast<size_t>(pointer_size); 709 } 710 711 void CopyFrom(ArtMethod* src, PointerSize image_pointer_size) 712 REQUIRES_SHARED(Locks::mutator_lock_); 713 714 ALWAYS_INLINE void SetCounter(uint16_t hotness_count) REQUIRES_SHARED(Locks::mutator_lock_); 715 716 ALWAYS_INLINE uint16_t GetCounter() REQUIRES_SHARED(Locks::mutator_lock_); 717 MaxCounter()718 ALWAYS_INLINE static constexpr uint16_t MaxCounter() { 719 return std::numeric_limits<decltype(hotness_count_)>::max(); 720 } 721 722 ALWAYS_INLINE uint32_t GetImtIndex() REQUIRES_SHARED(Locks::mutator_lock_); 723 724 void CalculateAndSetImtIndex() REQUIRES_SHARED(Locks::mutator_lock_); 725 HotnessCountOffset()726 static constexpr MemberOffset HotnessCountOffset() { 727 return MemberOffset(OFFSETOF_MEMBER(ArtMethod, hotness_count_)); 728 } 729 730 ArrayRef<const uint8_t> GetQuickenedInfo() REQUIRES_SHARED(Locks::mutator_lock_); 731 uint16_t GetIndexFromQuickening(uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_); 732 733 // Returns the method header for the compiled code containing 'pc'. Note that runtime 734 // methods will return null for this method, as they are not oat based. 735 const OatQuickMethodHeader* GetOatQuickMethodHeader(uintptr_t pc) 736 REQUIRES_SHARED(Locks::mutator_lock_); 737 738 // Get compiled code for the method, return null if no code exists. 739 const void* GetOatMethodQuickCode(PointerSize pointer_size) 740 REQUIRES_SHARED(Locks::mutator_lock_); 741 742 // Returns whether the method has any compiled code, JIT or AOT. 743 bool HasAnyCompiledCode() REQUIRES_SHARED(Locks::mutator_lock_); 744 745 // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or 746 // "a.b.C.m(II)V" (depending on the value of 'with_signature'). 747 static std::string PrettyMethod(ArtMethod* m, bool with_signature = true) 748 REQUIRES_SHARED(Locks::mutator_lock_); 749 std::string PrettyMethod(bool with_signature = true) 750 REQUIRES_SHARED(Locks::mutator_lock_); 751 // Returns the JNI native function name for the non-overloaded method 'm'. 752 std::string JniShortName() 753 REQUIRES_SHARED(Locks::mutator_lock_); 754 // Returns the JNI native function name for the overloaded method 'm'. 755 std::string JniLongName() 756 REQUIRES_SHARED(Locks::mutator_lock_); 757 758 // Update entry points by passing them through the visitor. 759 template <typename Visitor> 760 ALWAYS_INLINE void UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) 761 REQUIRES_SHARED(Locks::mutator_lock_); 762 763 // Visit the individual members of an ArtMethod. Used by imgdiag. 764 // As imgdiag does not support mixing instruction sets or pointer sizes (e.g., using imgdiag32 765 // to inspect 64-bit images, etc.), we can go beneath the accessors directly to the class members. 766 template <typename VisitorFunc> VisitMembers(VisitorFunc & visitor)767 void VisitMembers(VisitorFunc& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { 768 DCHECK(IsImagePointerSize(kRuntimePointerSize)); 769 visitor(this, &declaring_class_, "declaring_class_"); 770 visitor(this, &access_flags_, "access_flags_"); 771 visitor(this, &dex_code_item_offset_, "dex_code_item_offset_"); 772 visitor(this, &dex_method_index_, "dex_method_index_"); 773 visitor(this, &method_index_, "method_index_"); 774 visitor(this, &hotness_count_, "hotness_count_"); 775 visitor(this, &ptr_sized_fields_.data_, "ptr_sized_fields_.data_"); 776 visitor(this, 777 &ptr_sized_fields_.entry_point_from_quick_compiled_code_, 778 "ptr_sized_fields_.entry_point_from_quick_compiled_code_"); 779 } 780 781 // Returns the dex instructions of the code item for the art method. Returns an empty array for 782 // the null code item case. 783 ALWAYS_INLINE CodeItemInstructionAccessor DexInstructions() 784 REQUIRES_SHARED(Locks::mutator_lock_); 785 786 // Returns the dex code item data section of the DexFile for the art method. 787 ALWAYS_INLINE CodeItemDataAccessor DexInstructionData() 788 REQUIRES_SHARED(Locks::mutator_lock_); 789 790 // Returns the dex code item debug info section of the DexFile for the art method. 791 ALWAYS_INLINE CodeItemDebugInfoAccessor DexInstructionDebugInfo() 792 REQUIRES_SHARED(Locks::mutator_lock_); 793 DeclaringClassRoot()794 GcRoot<mirror::Class>& DeclaringClassRoot() { 795 return declaring_class_; 796 } 797 798 protected: 799 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". 800 // The class we are a part of. 801 GcRoot<mirror::Class> declaring_class_; 802 803 // Access flags; low 16 bits are defined by spec. 804 // Getting and setting this flag needs to be atomic when concurrency is 805 // possible, e.g. after this method's class is linked. Such as when setting 806 // verifier flags and single-implementation flag. 807 std::atomic<std::uint32_t> access_flags_; 808 809 /* Dex file fields. The defining dex file is available via declaring_class_->dex_cache_ */ 810 811 // Offset to the CodeItem. 812 uint32_t dex_code_item_offset_; 813 814 // Index into method_ids of the dex file associated with this method. 815 uint32_t dex_method_index_; 816 817 /* End of dex file fields. */ 818 819 // Entry within a dispatch table for this method. For static/direct methods the index is into 820 // the declaringClass.directMethods, for virtual methods the vtable and for interface methods the 821 // ifTable. 822 uint16_t method_index_; 823 824 union { 825 // Non-abstract methods: The hotness we measure for this method. Not atomic, 826 // as we allow missing increments: if the method is hot, we will see it eventually. 827 uint16_t hotness_count_; 828 // Abstract methods: IMT index (bitwise negated) or zero if it was not cached. 829 // The negation is needed to distinguish zero index and missing cached entry. 830 uint16_t imt_index_; 831 }; 832 833 // Fake padding field gets inserted here. 834 835 // Must be the last fields in the method. 836 struct PtrSizedFields { 837 // Depending on the method type, the data is 838 // - native method: pointer to the JNI function registered to this method 839 // or a function to resolve the JNI function, 840 // - conflict method: ImtConflictTable, 841 // - abstract/interface method: the single-implementation if any, 842 // - proxy method: the original interface method or constructor, 843 // - other methods: the profiling data. 844 void* data_; 845 846 // Method dispatch from quick compiled code invokes this pointer which may cause bridging into 847 // the interpreter. 848 void* entry_point_from_quick_compiled_code_; 849 } ptr_sized_fields_; 850 851 private: 852 uint16_t FindObsoleteDexClassDefIndex() REQUIRES_SHARED(Locks::mutator_lock_); 853 PtrSizedFieldsOffset(PointerSize pointer_size)854 static constexpr size_t PtrSizedFieldsOffset(PointerSize pointer_size) { 855 // Round up to pointer size for padding field. Tested in art_method.cc. 856 return RoundUp(offsetof(ArtMethod, hotness_count_) + sizeof(hotness_count_), 857 static_cast<size_t>(pointer_size)); 858 } 859 860 // Compare given pointer size to the image pointer size. 861 static bool IsImagePointerSize(PointerSize pointer_size); 862 863 dex::TypeIndex GetReturnTypeIndex() REQUIRES_SHARED(Locks::mutator_lock_); 864 865 template<typename T> GetNativePointer(MemberOffset offset,PointerSize pointer_size)866 ALWAYS_INLINE T GetNativePointer(MemberOffset offset, PointerSize pointer_size) const { 867 static_assert(std::is_pointer<T>::value, "T must be a pointer type"); 868 const auto addr = reinterpret_cast<uintptr_t>(this) + offset.Uint32Value(); 869 if (pointer_size == PointerSize::k32) { 870 return reinterpret_cast<T>(*reinterpret_cast<const uint32_t*>(addr)); 871 } else { 872 auto v = *reinterpret_cast<const uint64_t*>(addr); 873 return reinterpret_cast<T>(dchecked_integral_cast<uintptr_t>(v)); 874 } 875 } 876 877 template<typename T> SetNativePointer(MemberOffset offset,T new_value,PointerSize pointer_size)878 ALWAYS_INLINE void SetNativePointer(MemberOffset offset, T new_value, PointerSize pointer_size) 879 REQUIRES_SHARED(Locks::mutator_lock_) { 880 static_assert(std::is_pointer<T>::value, "T must be a pointer type"); 881 const auto addr = reinterpret_cast<uintptr_t>(this) + offset.Uint32Value(); 882 if (pointer_size == PointerSize::k32) { 883 uintptr_t ptr = reinterpret_cast<uintptr_t>(new_value); 884 *reinterpret_cast<uint32_t*>(addr) = dchecked_integral_cast<uint32_t>(ptr); 885 } else { 886 *reinterpret_cast<uint64_t*>(addr) = reinterpret_cast<uintptr_t>(new_value); 887 } 888 } 889 IsValidIntrinsicUpdate(uint32_t modifier)890 static inline bool IsValidIntrinsicUpdate(uint32_t modifier) { 891 return (((modifier & kAccIntrinsic) == kAccIntrinsic) && 892 (((modifier & ~(kAccIntrinsic | kAccIntrinsicBits)) == 0))); 893 } 894 OverlapsIntrinsicBits(uint32_t modifier)895 static inline bool OverlapsIntrinsicBits(uint32_t modifier) { 896 return (modifier & kAccIntrinsicBits) != 0; 897 } 898 899 // This setter guarantees atomicity. AddAccessFlags(uint32_t flag)900 void AddAccessFlags(uint32_t flag) REQUIRES_SHARED(Locks::mutator_lock_) { 901 DCHECK(!IsIntrinsic() || !OverlapsIntrinsicBits(flag) || IsValidIntrinsicUpdate(flag)); 902 // None of the readers rely ordering. 903 access_flags_.fetch_or(flag, std::memory_order_relaxed); 904 } 905 906 // This setter guarantees atomicity. ClearAccessFlags(uint32_t flag)907 void ClearAccessFlags(uint32_t flag) REQUIRES_SHARED(Locks::mutator_lock_) { 908 DCHECK(!IsIntrinsic() || !OverlapsIntrinsicBits(flag) || IsValidIntrinsicUpdate(flag)); 909 access_flags_.fetch_and(~flag, std::memory_order_relaxed); 910 } 911 912 // Used by GetName and GetNameView to share common code. 913 const char* GetRuntimeMethodName() REQUIRES_SHARED(Locks::mutator_lock_); 914 915 DISALLOW_COPY_AND_ASSIGN(ArtMethod); // Need to use CopyFrom to deal with 32 vs 64 bits. 916 }; 917 918 class MethodCallback { 919 public: ~MethodCallback()920 virtual ~MethodCallback() {} 921 922 virtual void RegisterNativeMethod(ArtMethod* method, 923 const void* original_implementation, 924 /*out*/void** new_implementation) 925 REQUIRES_SHARED(Locks::mutator_lock_) = 0; 926 }; 927 928 } // namespace art 929 930 #endif // ART_RUNTIME_ART_METHOD_H_ 931