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_INL_H_
18 #define ART_RUNTIME_ART_METHOD_INL_H_
19 
20 #include "art_method.h"
21 
22 #include "art_field.h"
23 #include "base/callee_save_type.h"
24 #include "class_linker-inl.h"
25 #include "common_throws.h"
26 #include "dex/code_item_accessors-inl.h"
27 #include "dex/dex_file-inl.h"
28 #include "dex/dex_file_annotations.h"
29 #include "dex/dex_file_types.h"
30 #include "dex/invoke_type.h"
31 #include "dex/primitive.h"
32 #include "dex/signature.h"
33 #include "gc_root-inl.h"
34 #include "imtable-inl.h"
35 #include "intrinsics_enum.h"
36 #include "jit/profiling_info.h"
37 #include "mirror/class-inl.h"
38 #include "mirror/dex_cache-inl.h"
39 #include "mirror/object-inl.h"
40 #include "mirror/object_array.h"
41 #include "mirror/string.h"
42 #include "obj_ptr-inl.h"
43 #include "quick/quick_method_frame_info.h"
44 #include "read_barrier-inl.h"
45 #include "runtime-inl.h"
46 #include "thread-current-inl.h"
47 
48 namespace art {
49 
50 template <ReadBarrierOption kReadBarrierOption>
GetDeclaringClassUnchecked()51 inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
52   GcRootSource gc_root_source(this);
53   return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
54 }
55 
56 template <ReadBarrierOption kReadBarrierOption>
GetDeclaringClass()57 inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
58   ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
59   if (kIsDebugBuild) {
60     if (!IsRuntimeMethod()) {
61       CHECK(result != nullptr) << this;
62     } else {
63       CHECK(result == nullptr) << this;
64     }
65   }
66   return result;
67 }
68 
SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class)69 inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
70   declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
71 }
72 
CASDeclaringClass(ObjPtr<mirror::Class> expected_class,ObjPtr<mirror::Class> desired_class)73 inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
74                                          ObjPtr<mirror::Class> desired_class) {
75   GcRoot<mirror::Class> expected_root(expected_class);
76   GcRoot<mirror::Class> desired_root(desired_class);
77   auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
78   return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
79 }
80 
GetMethodIndex()81 inline uint16_t ArtMethod::GetMethodIndex() {
82   DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
83   return method_index_;
84 }
85 
GetMethodIndexDuringLinking()86 inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
87   return method_index_;
88 }
89 
LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx)90 inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
91   ScopedAssertNoThreadSuspension ants(__FUNCTION__);
92   ObjPtr<mirror::Class> type =
93       Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
94   DCHECK(!Thread::Current()->IsExceptionPending());
95   return type;
96 }
97 
ResolveClassFromTypeIndex(dex::TypeIndex type_idx)98 inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
99   ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
100   DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
101   return type;
102 }
103 
CheckIncompatibleClassChange(InvokeType type)104 inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
105   switch (type) {
106     case kStatic:
107       return !IsStatic();
108     case kDirect:
109       return !IsDirect() || IsStatic();
110     case kVirtual: {
111       // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
112       // method.
113       ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
114       return IsDirect() || (methods_class->IsInterface() && !IsCopied());
115     }
116     case kSuper:
117       // Constructors and static methods are called with invoke-direct.
118       return IsConstructor() || IsStatic();
119     case kInterface: {
120       ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
121       return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
122     }
123     case kPolymorphic:
124       return !IsSignaturePolymorphic();
125     default:
126       LOG(FATAL) << "Unreachable - invocation type: " << type;
127       UNREACHABLE();
128   }
129 }
130 
IsCalleeSaveMethod()131 inline bool ArtMethod::IsCalleeSaveMethod() {
132   if (!IsRuntimeMethod()) {
133     return false;
134   }
135   Runtime* runtime = Runtime::Current();
136   bool result = false;
137   for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
138     if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
139       result = true;
140       break;
141     }
142   }
143   return result;
144 }
145 
IsResolutionMethod()146 inline bool ArtMethod::IsResolutionMethod() {
147   bool result = this == Runtime::Current()->GetResolutionMethod();
148   // Check that if we do think it is phony it looks like the resolution method.
149   DCHECK(!result || IsRuntimeMethod());
150   return result;
151 }
152 
IsImtUnimplementedMethod()153 inline bool ArtMethod::IsImtUnimplementedMethod() {
154   bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
155   // Check that if we do think it is phony it looks like the imt unimplemented method.
156   DCHECK(!result || IsRuntimeMethod());
157   return result;
158 }
159 
GetDexFile()160 inline const DexFile* ArtMethod::GetDexFile() {
161   // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
162   // from-space dex file pointer it will be equal to the to-space copy.
163   return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
164 }
165 
GetDeclaringClassDescriptor()166 inline const char* ArtMethod::GetDeclaringClassDescriptor() {
167   uint32_t dex_method_idx = GetDexMethodIndex();
168   if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
169     return "<runtime method>";
170   }
171   DCHECK(!IsProxyMethod());
172   const DexFile* dex_file = GetDexFile();
173   return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
174 }
175 
GetShorty()176 inline const char* ArtMethod::GetShorty() {
177   uint32_t unused_length;
178   return GetShorty(&unused_length);
179 }
180 
GetShorty(uint32_t * out_length)181 inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
182   DCHECK(!IsProxyMethod());
183   const DexFile* dex_file = GetDexFile();
184   return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
185 }
186 
GetSignature()187 inline const Signature ArtMethod::GetSignature() {
188   uint32_t dex_method_idx = GetDexMethodIndex();
189   if (dex_method_idx != dex::kDexNoIndex) {
190     DCHECK(!IsProxyMethod());
191     const DexFile* dex_file = GetDexFile();
192     return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
193   }
194   return Signature::NoSignature();
195 }
196 
GetName()197 inline const char* ArtMethod::GetName() {
198   uint32_t dex_method_idx = GetDexMethodIndex();
199   if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
200     DCHECK(!IsProxyMethod());
201     const DexFile* dex_file = GetDexFile();
202     return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
203   }
204   return GetRuntimeMethodName();
205 }
206 
GetNameView()207 inline std::string_view ArtMethod::GetNameView() {
208   uint32_t dex_method_idx = GetDexMethodIndex();
209   if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
210     DCHECK(!IsProxyMethod());
211     const DexFile* dex_file = GetDexFile();
212     uint32_t length = 0;
213     const char* name = dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx), &length);
214     return StringViewFromUtf16Length(name, length);
215   }
216   return GetRuntimeMethodName();
217 }
218 
ResolveNameString()219 inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
220   DCHECK(!IsProxyMethod());
221   const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
222   return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
223 }
224 
GetCodeItem()225 inline const dex::CodeItem* ArtMethod::GetCodeItem() {
226   if (!HasCodeItem()) {
227     return nullptr;
228   }
229   Runtime* runtime = Runtime::Current();
230   PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
231   return runtime->IsAotCompiler()
232       ? GetDexFile()->GetCodeItem(reinterpret_cast32<uint32_t>(GetDataPtrSize(pointer_size)))
233       : reinterpret_cast<const dex::CodeItem*>(
234           reinterpret_cast<uintptr_t>(GetDataPtrSize(pointer_size)) & ~1);
235 }
236 
IsResolvedTypeIdx(dex::TypeIndex type_idx)237 inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
238   DCHECK(!IsProxyMethod());
239   return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
240 }
241 
GetLineNumFromDexPC(uint32_t dex_pc)242 inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
243   DCHECK(!IsProxyMethod());
244   if (dex_pc == dex::kDexNoIndex) {
245     return IsNative() ? -2 : -1;
246   }
247   return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
248 }
249 
GetPrototype()250 inline const dex::ProtoId& ArtMethod::GetPrototype() {
251   DCHECK(!IsProxyMethod());
252   const DexFile* dex_file = GetDexFile();
253   return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
254 }
255 
GetParameterTypeList()256 inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
257   DCHECK(!IsProxyMethod());
258   const DexFile* dex_file = GetDexFile();
259   const dex::ProtoId& proto = dex_file->GetMethodPrototype(
260       dex_file->GetMethodId(GetDexMethodIndex()));
261   return dex_file->GetProtoParameters(proto);
262 }
263 
GetDeclaringClassSourceFile()264 inline const char* ArtMethod::GetDeclaringClassSourceFile() {
265   DCHECK(!IsProxyMethod());
266   return GetDeclaringClass()->GetSourceFile();
267 }
268 
GetClassDefIndex()269 inline uint16_t ArtMethod::GetClassDefIndex() {
270   DCHECK(!IsProxyMethod());
271   if (LIKELY(!IsObsolete())) {
272     return GetDeclaringClass()->GetDexClassDefIndex();
273   } else {
274     return FindObsoleteDexClassDefIndex();
275   }
276 }
277 
GetClassDef()278 inline const dex::ClassDef& ArtMethod::GetClassDef() {
279   DCHECK(!IsProxyMethod());
280   return GetDexFile()->GetClassDef(GetClassDefIndex());
281 }
282 
GetNumberOfParameters()283 inline size_t ArtMethod::GetNumberOfParameters() {
284   constexpr size_t return_type_count = 1u;
285   return strlen(GetShorty()) - return_type_count;
286 }
287 
GetReturnTypeDescriptor()288 inline const char* ArtMethod::GetReturnTypeDescriptor() {
289   DCHECK(!IsProxyMethod());
290   const DexFile* dex_file = GetDexFile();
291   return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
292 }
293 
GetReturnTypePrimitive()294 inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
295   return Primitive::GetType(GetReturnTypeDescriptor()[0]);
296 }
297 
GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx)298 inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
299   DCHECK(!IsProxyMethod());
300   const DexFile* dex_file = GetDexFile();
301   return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
302 }
303 
GetClassLoader()304 inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
305   DCHECK(!IsProxyMethod());
306   return GetDeclaringClass()->GetClassLoader();
307 }
308 
309 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()310 inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
311   if (LIKELY(!IsObsolete())) {
312     ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
313     return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
314   } else {
315     DCHECK(!IsProxyMethod());
316     return GetObsoleteDexCache();
317   }
318 }
319 
IsProxyMethod()320 inline bool ArtMethod::IsProxyMethod() {
321   DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
322   // No read barrier needed, we're reading the constant declaring class only to read
323   // the constant proxy flag. See ReadBarrierOption.
324   return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
325 }
326 
GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size)327 inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
328   DCHECK(IsProxyMethod());
329   // Do not check IsAssignableFrom() here as it relies on raw reference comparison
330   // which may give false negatives while visiting references for a non-CC moving GC.
331   return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
332 }
333 
GetInterfaceMethodIfProxy(PointerSize pointer_size)334 inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
335   if (LIKELY(!IsProxyMethod())) {
336     return this;
337   }
338   ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
339   // We can check that the proxy class implements the interface only if the proxy class
340   // is resolved, otherwise the interface table is not yet initialized.
341   DCHECK(!GetDeclaringClass()->IsResolved() ||
342          interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
343   return interface_method;
344 }
345 
GetReturnTypeIndex()346 inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
347   DCHECK(!IsProxyMethod());
348   const DexFile* dex_file = GetDexFile();
349   const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
350   const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
351   return proto_id.return_type_idx_;
352 }
353 
LookupResolvedReturnType()354 inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
355   return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
356 }
357 
ResolveReturnType()358 inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
359   return ResolveClassFromTypeIndex(GetReturnTypeIndex());
360 }
361 
362 template <ReadBarrierOption kReadBarrierOption>
HasSingleImplementation()363 inline bool ArtMethod::HasSingleImplementation() {
364   if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
365     // We don't set kAccSingleImplementation for these cases since intrinsic
366     // can use the flag also.
367     return true;
368   }
369   return (GetAccessFlags() & kAccSingleImplementation) != 0;
370 }
371 
372 template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
VisitRoots(RootVisitorType & visitor,PointerSize pointer_size)373 void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
374   if (LIKELY(!declaring_class_.IsNull())) {
375     visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
376     ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
377     if (UNLIKELY(klass->IsProxyClass())) {
378       // For normal methods, dex cache shortcuts will be visited through the declaring class.
379       // However, for proxies we need to keep the interface method alive, so we visit its roots.
380       ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
381       DCHECK(interface_method != nullptr);
382       interface_method->VisitRoots(visitor, pointer_size);
383     }
384   }
385 }
386 
387 template <typename Visitor>
UpdateEntrypoints(const Visitor & visitor,PointerSize pointer_size)388 inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
389   if (IsNative()) {
390     const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
391     const void* new_native_code = visitor(old_native_code);
392     if (old_native_code != new_native_code) {
393       SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
394     }
395   }
396   const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
397   const void* new_code = visitor(old_code);
398   if (old_code != new_code) {
399     SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
400   }
401 }
402 
DexInstructions()403 inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
404   return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
405 }
406 
DexInstructionData()407 inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
408   return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
409 }
410 
DexInstructionDebugInfo()411 inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
412   return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
413 }
414 
SetCounter(uint16_t hotness_count)415 inline void ArtMethod::SetCounter(uint16_t hotness_count) {
416   DCHECK(!IsAbstract());
417   hotness_count_ = hotness_count;
418 }
419 
GetCounter()420 inline uint16_t ArtMethod::GetCounter() {
421   DCHECK(!IsAbstract());
422   return hotness_count_;
423 }
424 
GetImtIndex()425 inline uint32_t ArtMethod::GetImtIndex() {
426   if (LIKELY(IsAbstract())) {
427     return imt_index_;
428   } else {
429     return ImTable::GetImtIndex(this);
430   }
431 }
432 
CalculateAndSetImtIndex()433 inline void ArtMethod::CalculateAndSetImtIndex() {
434   DCHECK(IsAbstract()) << PrettyMethod();
435   imt_index_ = ImTable::GetImtIndex(this);
436 }
437 
438 }  // namespace art
439 
440 #endif  // ART_RUNTIME_ART_METHOD_INL_H_
441