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 !IsPolymorphicSignature();
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 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
227 }
228
IsResolvedTypeIdx(dex::TypeIndex type_idx)229 inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
230 DCHECK(!IsProxyMethod());
231 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
232 }
233
GetLineNumFromDexPC(uint32_t dex_pc)234 inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
235 DCHECK(!IsProxyMethod());
236 if (dex_pc == dex::kDexNoIndex) {
237 return IsNative() ? -2 : -1;
238 }
239 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
240 }
241
GetPrototype()242 inline const dex::ProtoId& ArtMethod::GetPrototype() {
243 DCHECK(!IsProxyMethod());
244 const DexFile* dex_file = GetDexFile();
245 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
246 }
247
GetParameterTypeList()248 inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
249 DCHECK(!IsProxyMethod());
250 const DexFile* dex_file = GetDexFile();
251 const dex::ProtoId& proto = dex_file->GetMethodPrototype(
252 dex_file->GetMethodId(GetDexMethodIndex()));
253 return dex_file->GetProtoParameters(proto);
254 }
255
GetDeclaringClassSourceFile()256 inline const char* ArtMethod::GetDeclaringClassSourceFile() {
257 DCHECK(!IsProxyMethod());
258 return GetDeclaringClass()->GetSourceFile();
259 }
260
GetClassDefIndex()261 inline uint16_t ArtMethod::GetClassDefIndex() {
262 DCHECK(!IsProxyMethod());
263 if (LIKELY(!IsObsolete())) {
264 return GetDeclaringClass()->GetDexClassDefIndex();
265 } else {
266 return FindObsoleteDexClassDefIndex();
267 }
268 }
269
GetClassDef()270 inline const dex::ClassDef& ArtMethod::GetClassDef() {
271 DCHECK(!IsProxyMethod());
272 return GetDexFile()->GetClassDef(GetClassDefIndex());
273 }
274
GetNumberOfParameters()275 inline size_t ArtMethod::GetNumberOfParameters() {
276 constexpr size_t return_type_count = 1u;
277 return strlen(GetShorty()) - return_type_count;
278 }
279
GetReturnTypeDescriptor()280 inline const char* ArtMethod::GetReturnTypeDescriptor() {
281 DCHECK(!IsProxyMethod());
282 const DexFile* dex_file = GetDexFile();
283 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
284 }
285
GetReturnTypePrimitive()286 inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
287 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
288 }
289
GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx)290 inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
291 DCHECK(!IsProxyMethod());
292 const DexFile* dex_file = GetDexFile();
293 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
294 }
295
GetClassLoader()296 inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
297 DCHECK(!IsProxyMethod());
298 return GetDeclaringClass()->GetClassLoader();
299 }
300
301 template <ReadBarrierOption kReadBarrierOption>
GetDexCache()302 inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
303 if (LIKELY(!IsObsolete())) {
304 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
305 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
306 } else {
307 DCHECK(!IsProxyMethod());
308 return GetObsoleteDexCache();
309 }
310 }
311
IsProxyMethod()312 inline bool ArtMethod::IsProxyMethod() {
313 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
314 // No read barrier needed, we're reading the constant declaring class only to read
315 // the constant proxy flag. See ReadBarrierOption.
316 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
317 }
318
GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size)319 inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
320 DCHECK(IsProxyMethod());
321 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
322 // which may give false negatives while visiting references for a non-CC moving GC.
323 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
324 }
325
GetInterfaceMethodIfProxy(PointerSize pointer_size)326 inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
327 if (LIKELY(!IsProxyMethod())) {
328 return this;
329 }
330 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
331 // We can check that the proxy class implements the interface only if the proxy class
332 // is resolved, otherwise the interface table is not yet initialized.
333 DCHECK(!GetDeclaringClass()->IsResolved() ||
334 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
335 return interface_method;
336 }
337
GetReturnTypeIndex()338 inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
339 DCHECK(!IsProxyMethod());
340 const DexFile* dex_file = GetDexFile();
341 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
342 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
343 return proto_id.return_type_idx_;
344 }
345
LookupResolvedReturnType()346 inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
347 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
348 }
349
ResolveReturnType()350 inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
351 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
352 }
353
354 template <ReadBarrierOption kReadBarrierOption>
HasSingleImplementation()355 inline bool ArtMethod::HasSingleImplementation() {
356 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
357 // We don't set kAccSingleImplementation for these cases since intrinsic
358 // can use the flag also.
359 return true;
360 }
361 return (GetAccessFlags() & kAccSingleImplementation) != 0;
362 }
363
364 template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
VisitRoots(RootVisitorType & visitor,PointerSize pointer_size)365 void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
366 if (LIKELY(!declaring_class_.IsNull())) {
367 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
368 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
369 if (UNLIKELY(klass->IsProxyClass())) {
370 // For normal methods, dex cache shortcuts will be visited through the declaring class.
371 // However, for proxies we need to keep the interface method alive, so we visit its roots.
372 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
373 DCHECK(interface_method != nullptr);
374 interface_method->VisitRoots(visitor, pointer_size);
375 }
376 }
377 }
378
379 template <typename Visitor>
UpdateEntrypoints(const Visitor & visitor,PointerSize pointer_size)380 inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
381 if (IsNative()) {
382 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
383 const void* new_native_code = visitor(old_native_code);
384 if (old_native_code != new_native_code) {
385 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
386 }
387 } else {
388 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
389 }
390 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
391 const void* new_code = visitor(old_code);
392 if (old_code != new_code) {
393 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
394 }
395 }
396
DexInstructions()397 inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
398 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
399 }
400
DexInstructionData()401 inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
402 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
403 }
404
DexInstructionDebugInfo()405 inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
406 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
407 }
408
SetCounter(uint16_t hotness_count)409 inline void ArtMethod::SetCounter(uint16_t hotness_count) {
410 DCHECK(!IsAbstract()) << PrettyMethod();
411 hotness_count_ = hotness_count;
412 }
413
GetCounter()414 inline uint16_t ArtMethod::GetCounter() {
415 DCHECK(!IsAbstract()) << PrettyMethod();
416 return hotness_count_;
417 }
418
GetImtIndex()419 inline uint32_t ArtMethod::GetImtIndex() {
420 if (LIKELY(IsAbstract() && imt_index_ != 0)) {
421 uint16_t imt_index = ~imt_index_;
422 DCHECK_EQ(imt_index, ImTable::GetImtIndex(this)) << PrettyMethod();
423 return imt_index;
424 } else {
425 return ImTable::GetImtIndex(this);
426 }
427 }
428
CalculateAndSetImtIndex()429 inline void ArtMethod::CalculateAndSetImtIndex() {
430 DCHECK(IsAbstract()) << PrettyMethod();
431 imt_index_ = ~ImTable::GetImtIndex(this);
432 }
433
434 } // namespace art
435
436 #endif // ART_RUNTIME_ART_METHOD_INL_H_
437