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 
DataOffset(PointerSize pointer_size)492   static constexpr MemberOffset DataOffset(PointerSize pointer_size) {
493     return MemberOffset(PtrSizedFieldsOffset(pointer_size) + OFFSETOF_MEMBER(
494         PtrSizedFields, data_) / sizeof(void*) * static_cast<size_t>(pointer_size));
495   }
496 
EntryPointFromJniOffset(PointerSize pointer_size)497   static constexpr MemberOffset EntryPointFromJniOffset(PointerSize pointer_size) {
498     return DataOffset(pointer_size);
499   }
500 
EntryPointFromQuickCompiledCodeOffset(PointerSize pointer_size)501   static constexpr MemberOffset EntryPointFromQuickCompiledCodeOffset(PointerSize pointer_size) {
502     return MemberOffset(PtrSizedFieldsOffset(pointer_size) + OFFSETOF_MEMBER(
503         PtrSizedFields, entry_point_from_quick_compiled_code_) / sizeof(void*)
504             * static_cast<size_t>(pointer_size));
505   }
506 
GetImtConflictTable(PointerSize pointer_size)507   ImtConflictTable* GetImtConflictTable(PointerSize pointer_size) const {
508     DCHECK(IsRuntimeMethod());
509     return reinterpret_cast<ImtConflictTable*>(GetDataPtrSize(pointer_size));
510   }
511 
SetImtConflictTable(ImtConflictTable * table,PointerSize pointer_size)512   ALWAYS_INLINE void SetImtConflictTable(ImtConflictTable* table, PointerSize pointer_size)
513       REQUIRES_SHARED(Locks::mutator_lock_) {
514     DCHECK(IsRuntimeMethod());
515     SetDataPtrSize(table, pointer_size);
516   }
517 
GetProfilingInfo(PointerSize pointer_size)518   ProfilingInfo* GetProfilingInfo(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_) {
519     if (UNLIKELY(IsNative() || IsProxyMethod() || !IsInvokable())) {
520       return nullptr;
521     }
522     return reinterpret_cast<ProfilingInfo*>(GetDataPtrSize(pointer_size));
523   }
524 
SetProfilingInfo(ProfilingInfo * info)525   ALWAYS_INLINE void SetProfilingInfo(ProfilingInfo* info) REQUIRES_SHARED(Locks::mutator_lock_) {
526     SetProfilingInfoPtrSize(info, kRuntimePointerSize);
527   }
528 
SetProfilingInfoPtrSize(ProfilingInfo * info,PointerSize pointer_size)529   ALWAYS_INLINE void SetProfilingInfoPtrSize(ProfilingInfo* info, PointerSize pointer_size)
530       REQUIRES_SHARED(Locks::mutator_lock_) {
531     DCHECK(!IsProxyMethod());
532     DCHECK(!IsNative());
533     DCHECK(IsInvokable());
534     SetDataPtrSize(info, pointer_size);
535   }
536 
ProfilingInfoOffset()537   static MemberOffset ProfilingInfoOffset() {
538     DCHECK(IsImagePointerSize(kRuntimePointerSize));
539     return DataOffset(kRuntimePointerSize);
540   }
541 
542   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
543   ALWAYS_INLINE bool HasSingleImplementation() REQUIRES_SHARED(Locks::mutator_lock_);
544 
SetHasSingleImplementation(bool single_impl)545   ALWAYS_INLINE void SetHasSingleImplementation(bool single_impl)
546       REQUIRES_SHARED(Locks::mutator_lock_) {
547     DCHECK(!IsIntrinsic()) << "conflict with intrinsic bits";
548     if (single_impl) {
549       AddAccessFlags(kAccSingleImplementation);
550     } else {
551       ClearAccessFlags(kAccSingleImplementation);
552     }
553   }
554 
HasSingleImplementationFlag()555   ALWAYS_INLINE bool HasSingleImplementationFlag() const {
556     return (GetAccessFlags() & kAccSingleImplementation) != 0;
557   }
558 
559   // Takes a method and returns a 'canonical' one if the method is default (and therefore
560   // potentially copied from some other class). For example, this ensures that the debugger does not
561   // get confused as to which method we are in.
562   ArtMethod* GetCanonicalMethod(PointerSize pointer_size = kRuntimePointerSize)
563       REQUIRES_SHARED(Locks::mutator_lock_);
564 
565   ArtMethod* GetSingleImplementation(PointerSize pointer_size);
566 
SetSingleImplementation(ArtMethod * method,PointerSize pointer_size)567   ALWAYS_INLINE void SetSingleImplementation(ArtMethod* method, PointerSize pointer_size)
568       REQUIRES_SHARED(Locks::mutator_lock_) {
569     DCHECK(!IsNative());
570     // Non-abstract method's single implementation is just itself.
571     DCHECK(IsAbstract());
572     SetDataPtrSize(method, pointer_size);
573   }
574 
GetEntryPointFromJni()575   void* GetEntryPointFromJni() const {
576     DCHECK(IsNative());
577     return GetEntryPointFromJniPtrSize(kRuntimePointerSize);
578   }
579 
GetEntryPointFromJniPtrSize(PointerSize pointer_size)580   ALWAYS_INLINE void* GetEntryPointFromJniPtrSize(PointerSize pointer_size) const {
581     return GetDataPtrSize(pointer_size);
582   }
583 
SetEntryPointFromJni(const void * entrypoint)584   void SetEntryPointFromJni(const void* entrypoint)
585       REQUIRES_SHARED(Locks::mutator_lock_) {
586     // The resolution method also has a JNI entrypoint for direct calls from
587     // compiled code to the JNI dlsym lookup stub for @CriticalNative.
588     DCHECK(IsNative() || IsRuntimeMethod());
589     SetEntryPointFromJniPtrSize(entrypoint, kRuntimePointerSize);
590   }
591 
SetEntryPointFromJniPtrSize(const void * entrypoint,PointerSize pointer_size)592   ALWAYS_INLINE void SetEntryPointFromJniPtrSize(const void* entrypoint, PointerSize pointer_size)
593       REQUIRES_SHARED(Locks::mutator_lock_) {
594     SetDataPtrSize(entrypoint, pointer_size);
595   }
596 
GetDataPtrSize(PointerSize pointer_size)597   ALWAYS_INLINE void* GetDataPtrSize(PointerSize pointer_size) const {
598     DCHECK(IsImagePointerSize(pointer_size));
599     return GetNativePointer<void*>(DataOffset(pointer_size), pointer_size);
600   }
601 
SetDataPtrSize(const void * data,PointerSize pointer_size)602   ALWAYS_INLINE void SetDataPtrSize(const void* data, PointerSize pointer_size)
603       REQUIRES_SHARED(Locks::mutator_lock_) {
604     DCHECK(IsImagePointerSize(pointer_size));
605     SetNativePointer(DataOffset(pointer_size), data, pointer_size);
606   }
607 
608   // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
609   // conventions for a method of managed code. Returns false for Proxy methods.
IsRuntimeMethod()610   ALWAYS_INLINE bool IsRuntimeMethod() const {
611     return dex_method_index_ == kRuntimeMethodDexMethodIndex;
612   }
613 
614   // Is this a hand crafted method used for something like describing callee saves?
615   bool IsCalleeSaveMethod() REQUIRES_SHARED(Locks::mutator_lock_);
616 
617   bool IsResolutionMethod() REQUIRES_SHARED(Locks::mutator_lock_);
618 
619   bool IsImtUnimplementedMethod() REQUIRES_SHARED(Locks::mutator_lock_);
620 
621   // Find the catch block for the given exception type and dex_pc. When a catch block is found,
622   // indicates whether the found catch block is responsible for clearing the exception or whether
623   // a move-exception instruction is present.
624   uint32_t FindCatchBlock(Handle<mirror::Class> exception_type, uint32_t dex_pc,
625                           bool* has_no_move_exception)
626       REQUIRES_SHARED(Locks::mutator_lock_);
627 
628   // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
629   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier, typename RootVisitorType>
630   void VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) NO_THREAD_SAFETY_ANALYSIS;
631 
632   const DexFile* GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_);
633 
634   const char* GetDeclaringClassDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
635 
636   ALWAYS_INLINE const char* GetShorty() REQUIRES_SHARED(Locks::mutator_lock_);
637 
638   const char* GetShorty(uint32_t* out_length) REQUIRES_SHARED(Locks::mutator_lock_);
639 
640   const Signature GetSignature() REQUIRES_SHARED(Locks::mutator_lock_);
641 
642   ALWAYS_INLINE const char* GetName() REQUIRES_SHARED(Locks::mutator_lock_);
643 
644   ALWAYS_INLINE std::string_view GetNameView() REQUIRES_SHARED(Locks::mutator_lock_);
645 
646   ObjPtr<mirror::String> ResolveNameString() REQUIRES_SHARED(Locks::mutator_lock_);
647 
648   const dex::CodeItem* GetCodeItem() REQUIRES_SHARED(Locks::mutator_lock_);
649 
650   bool IsResolvedTypeIdx(dex::TypeIndex type_idx) REQUIRES_SHARED(Locks::mutator_lock_);
651 
652   int32_t GetLineNumFromDexPC(uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_);
653 
654   const dex::ProtoId& GetPrototype() REQUIRES_SHARED(Locks::mutator_lock_);
655 
656   const dex::TypeList* GetParameterTypeList() REQUIRES_SHARED(Locks::mutator_lock_);
657 
658   const char* GetDeclaringClassSourceFile() REQUIRES_SHARED(Locks::mutator_lock_);
659 
660   uint16_t GetClassDefIndex() REQUIRES_SHARED(Locks::mutator_lock_);
661 
662   const dex::ClassDef& GetClassDef() REQUIRES_SHARED(Locks::mutator_lock_);
663 
664   ALWAYS_INLINE size_t GetNumberOfParameters() REQUIRES_SHARED(Locks::mutator_lock_);
665 
666   const char* GetReturnTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
667 
668   ALWAYS_INLINE Primitive::Type GetReturnTypePrimitive() REQUIRES_SHARED(Locks::mutator_lock_);
669 
670   const char* GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx)
671       REQUIRES_SHARED(Locks::mutator_lock_);
672 
673   // Lookup return type.
674   ObjPtr<mirror::Class> LookupResolvedReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
675   // Resolve return type. May cause thread suspension due to GetClassFromTypeIdx
676   // calling ResolveType this caused a large number of bugs at call sites.
677   ObjPtr<mirror::Class> ResolveReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
678 
679   ObjPtr<mirror::ClassLoader> GetClassLoader() REQUIRES_SHARED(Locks::mutator_lock_);
680 
681   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
682   ObjPtr<mirror::DexCache> GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
683   ObjPtr<mirror::DexCache> GetObsoleteDexCache() REQUIRES_SHARED(Locks::mutator_lock_);
684 
685   ALWAYS_INLINE ArtMethod* GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size)
686       REQUIRES_SHARED(Locks::mutator_lock_);
687   ALWAYS_INLINE ArtMethod* GetInterfaceMethodIfProxy(PointerSize pointer_size)
688       REQUIRES_SHARED(Locks::mutator_lock_);
689 
690   ArtMethod* GetNonObsoleteMethod() REQUIRES_SHARED(Locks::mutator_lock_);
691 
692   // May cause thread suspension due to class resolution.
693   bool EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params)
694       REQUIRES_SHARED(Locks::mutator_lock_);
695 
696   // Size of an instance of this native class.
Size(PointerSize pointer_size)697   static size_t Size(PointerSize pointer_size) {
698     return PtrSizedFieldsOffset(pointer_size) +
699         (sizeof(PtrSizedFields) / sizeof(void*)) * static_cast<size_t>(pointer_size);
700   }
701 
702   // Alignment of an instance of this native class.
Alignment(PointerSize pointer_size)703   static size_t Alignment(PointerSize pointer_size) {
704     // The ArtMethod alignment is the same as image pointer size. This differs from
705     // alignof(ArtMethod) if cross-compiling with pointer_size != sizeof(void*).
706     return static_cast<size_t>(pointer_size);
707   }
708 
709   void CopyFrom(ArtMethod* src, PointerSize image_pointer_size)
710       REQUIRES_SHARED(Locks::mutator_lock_);
711 
712   ALWAYS_INLINE void SetCounter(uint16_t hotness_count) REQUIRES_SHARED(Locks::mutator_lock_);
713 
714   ALWAYS_INLINE uint16_t GetCounter() REQUIRES_SHARED(Locks::mutator_lock_);
715 
MaxCounter()716   ALWAYS_INLINE static constexpr uint16_t MaxCounter() {
717     return std::numeric_limits<decltype(hotness_count_)>::max();
718   }
719 
720   ALWAYS_INLINE uint32_t GetImtIndex() REQUIRES_SHARED(Locks::mutator_lock_);
721 
722   void CalculateAndSetImtIndex() REQUIRES_SHARED(Locks::mutator_lock_);
723 
HotnessCountOffset()724   static constexpr MemberOffset HotnessCountOffset() {
725     return MemberOffset(OFFSETOF_MEMBER(ArtMethod, hotness_count_));
726   }
727 
728   ArrayRef<const uint8_t> GetQuickenedInfo() REQUIRES_SHARED(Locks::mutator_lock_);
729   uint16_t GetIndexFromQuickening(uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_);
730 
731   // Returns the method header for the compiled code containing 'pc'. Note that runtime
732   // methods will return null for this method, as they are not oat based.
733   const OatQuickMethodHeader* GetOatQuickMethodHeader(uintptr_t pc)
734       REQUIRES_SHARED(Locks::mutator_lock_);
735 
736   // Get compiled code for the method, return null if no code exists.
737   const void* GetOatMethodQuickCode(PointerSize pointer_size)
738       REQUIRES_SHARED(Locks::mutator_lock_);
739 
740   // Returns whether the method has any compiled code, JIT or AOT.
741   bool HasAnyCompiledCode() REQUIRES_SHARED(Locks::mutator_lock_);
742 
743   // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
744   // "a.b.C.m(II)V" (depending on the value of 'with_signature').
745   static std::string PrettyMethod(ArtMethod* m, bool with_signature = true)
746       REQUIRES_SHARED(Locks::mutator_lock_);
747   std::string PrettyMethod(bool with_signature = true)
748       REQUIRES_SHARED(Locks::mutator_lock_);
749   // Returns the JNI native function name for the non-overloaded method 'm'.
750   std::string JniShortName()
751       REQUIRES_SHARED(Locks::mutator_lock_);
752   // Returns the JNI native function name for the overloaded method 'm'.
753   std::string JniLongName()
754       REQUIRES_SHARED(Locks::mutator_lock_);
755 
756   // Update entry points by passing them through the visitor.
757   template <typename Visitor>
758   ALWAYS_INLINE void UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size)
759       REQUIRES_SHARED(Locks::mutator_lock_);
760 
761   // Visit the individual members of an ArtMethod.  Used by imgdiag.
762   // As imgdiag does not support mixing instruction sets or pointer sizes (e.g., using imgdiag32
763   // to inspect 64-bit images, etc.), we can go beneath the accessors directly to the class members.
764   template <typename VisitorFunc>
VisitMembers(VisitorFunc & visitor)765   void VisitMembers(VisitorFunc& visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
766     DCHECK(IsImagePointerSize(kRuntimePointerSize));
767     visitor(this, &declaring_class_, "declaring_class_");
768     visitor(this, &access_flags_, "access_flags_");
769     visitor(this, &dex_code_item_offset_, "dex_code_item_offset_");
770     visitor(this, &dex_method_index_, "dex_method_index_");
771     visitor(this, &method_index_, "method_index_");
772     visitor(this, &hotness_count_, "hotness_count_");
773     visitor(this, &ptr_sized_fields_.data_, "ptr_sized_fields_.data_");
774     visitor(this,
775             &ptr_sized_fields_.entry_point_from_quick_compiled_code_,
776             "ptr_sized_fields_.entry_point_from_quick_compiled_code_");
777   }
778 
779   // Returns the dex instructions of the code item for the art method. Returns an empty array for
780   // the null code item case.
781   ALWAYS_INLINE CodeItemInstructionAccessor DexInstructions()
782       REQUIRES_SHARED(Locks::mutator_lock_);
783 
784   // Returns the dex code item data section of the DexFile for the art method.
785   ALWAYS_INLINE CodeItemDataAccessor DexInstructionData()
786       REQUIRES_SHARED(Locks::mutator_lock_);
787 
788   // Returns the dex code item debug info section of the DexFile for the art method.
789   ALWAYS_INLINE CodeItemDebugInfoAccessor DexInstructionDebugInfo()
790       REQUIRES_SHARED(Locks::mutator_lock_);
791 
DeclaringClassRoot()792   GcRoot<mirror::Class>& DeclaringClassRoot() {
793     return declaring_class_;
794   }
795 
796  protected:
797   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
798   // The class we are a part of.
799   GcRoot<mirror::Class> declaring_class_;
800 
801   // Access flags; low 16 bits are defined by spec.
802   // Getting and setting this flag needs to be atomic when concurrency is
803   // possible, e.g. after this method's class is linked. Such as when setting
804   // verifier flags and single-implementation flag.
805   std::atomic<std::uint32_t> access_flags_;
806 
807   /* Dex file fields. The defining dex file is available via declaring_class_->dex_cache_ */
808 
809   // Offset to the CodeItem.
810   uint32_t dex_code_item_offset_;
811 
812   // Index into method_ids of the dex file associated with this method.
813   uint32_t dex_method_index_;
814 
815   /* End of dex file fields. */
816 
817   // Entry within a dispatch table for this method. For static/direct methods the index is into
818   // the declaringClass.directMethods, for virtual methods the vtable and for interface methods the
819   // ifTable.
820   uint16_t method_index_;
821 
822   union {
823     // Non-abstract methods: The hotness we measure for this method. Not atomic,
824     // as we allow missing increments: if the method is hot, we will see it eventually.
825     uint16_t hotness_count_;
826     // Abstract methods: IMT index (bitwise negated) or zero if it was not cached.
827     // The negation is needed to distinguish zero index and missing cached entry.
828     uint16_t imt_index_;
829   };
830 
831   // Fake padding field gets inserted here.
832 
833   // Must be the last fields in the method.
834   struct PtrSizedFields {
835     // Depending on the method type, the data is
836     //   - native method: pointer to the JNI function registered to this method
837     //                    or a function to resolve the JNI function,
838     //   - resolution method: pointer to a function to resolve the method and
839     //                        the JNI function for @CriticalNative.
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