blob: 91258c7e377066f92407143f343968ffabce3278 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#ifndef ART_RUNTIME_ART_METHOD_INL_H_
18#define ART_RUNTIME_ART_METHOD_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021
Andreas Gampe58a5af82014-07-31 16:23:49 -070022#include "art_field.h"
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -070023#include "base/logging.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070024#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010025#include "common_throws.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "dex_file.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "gc_root-inl.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010029#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010031#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/object-inl.h"
33#include "mirror/object_array.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010034#include "oat.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010035#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010037#include "runtime-inl.h"
Andreas Gampecbc96b82015-09-30 20:05:24 +000038#include "scoped_thread_state_change.h"
39#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010040#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080044template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070045inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070046 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080047 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070048}
49
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080050template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070051inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080052 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070053 if (kIsDebugBuild) {
54 if (!IsRuntimeMethod()) {
55 CHECK(result != nullptr) << this;
56 CHECK(result->IsIdxLoaded() || result->IsErroneous())
57 << result->GetStatus() << " " << PrettyClass(result);
58 } else {
59 CHECK(result == nullptr) << this;
60 }
61 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 return result;
63}
64
Mathieu Chartiere401d142015-04-22 13:56:20 -070065inline void ArtMethod::SetDeclaringClass(mirror::Class* new_declaring_class) {
66 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067}
68
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070069inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
70 mirror::Class* desired_class) {
71 GcRoot<mirror::Class> expected_root(expected_class);
72 GcRoot<mirror::Class> desired_root(desired_class);
73 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
74 CompareExchangeStrongSequentiallyConsistent(
75 expected_root, desired_root);
76}
77
Andreas Gampecbc96b82015-09-30 20:05:24 +000078// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
79// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080080template <ReadBarrierOption kReadBarrierOption>
81ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
82 NO_THREAD_SAFETY_ANALYSIS {
83 CHECK(method->IsRuntimeMethod() ||
84 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
85 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000086}
87
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080088template <ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080089inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampecbc96b82015-09-30 20:05:24 +000090 if (kIsDebugBuild) {
91 Thread* self = Thread::Current();
92 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
93 ScopedObjectAccess soa(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080094 CHECK(IsRuntimeMethod() ||
95 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
96 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000097 } else {
98 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
99 // runnable state (e.g., during GC).
100 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800101 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000102 }
103 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105}
106
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
109 GetDeclaringClass()->IsErroneous());
110 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111}
112
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700113inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700115}
116
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
119 GetDeclaringClass()->IsErroneous());
120 return dex_method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121}
122
Vladimir Marko05792b92015-08-03 11:56:49 +0100123inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(size_t pointer_size) {
124 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
125 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700126}
127
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index, size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100129 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
130 // without accessing the DexCache and we don't want to do that in release build.
131 DCHECK_LT(method_index,
132 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
133 ->GetDexCache()->NumResolvedMethods());
134 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
135 method_index,
136 ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 if (LIKELY(method != nullptr)) {
138 auto* declaring_class = method->GetDeclaringClass();
139 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
140 return method;
141 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700142 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700144}
145
Vladimir Marko05792b92015-08-03 11:56:49 +0100146inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index, ArtMethod* new_method,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100148 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
149 // without accessing the DexCache and we don't want to do that in release build.
150 DCHECK_LT(method_index,
151 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
152 ->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100154 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
155 method_index,
156 new_method,
157 ptr_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700158}
159
Vladimir Marko05792b92015-08-03 11:56:49 +0100160inline bool ArtMethod::HasDexCacheResolvedMethods(size_t pointer_size) {
161 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700162}
163
Vladimir Marko05792b92015-08-03 11:56:49 +0100164inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
165 size_t pointer_size) {
166 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700167}
168
Vladimir Marko05792b92015-08-03 11:56:49 +0100169inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, size_t pointer_size) {
170 return GetDexCacheResolvedMethods(pointer_size) ==
171 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700172}
173
Vladimir Marko05792b92015-08-03 11:56:49 +0100174inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(size_t pointer_size) {
175 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
176 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700177}
178
Andreas Gampe58a5af82014-07-31 16:23:49 -0700179template <bool kWithCheck>
Vladimir Marko05792b92015-08-03 11:56:49 +0100180inline mirror::Class* ArtMethod::GetDexCacheResolvedType(uint32_t type_index, size_t ptr_size) {
181 if (kWithCheck) {
182 mirror::DexCache* dex_cache =
183 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()->GetDexCache();
184 if (UNLIKELY(type_index >= dex_cache->NumResolvedTypes())) {
185 ThrowArrayIndexOutOfBoundsException(type_index, dex_cache->NumResolvedTypes());
186 return nullptr;
187 }
188 }
189 mirror::Class* klass = GetDexCacheResolvedTypes(ptr_size)[type_index].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700190 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
191}
192
Vladimir Marko05792b92015-08-03 11:56:49 +0100193inline bool ArtMethod::HasDexCacheResolvedTypes(size_t pointer_size) {
194 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700195}
196
Vladimir Marko05792b92015-08-03 11:56:49 +0100197inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
198 size_t pointer_size) {
199 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700200}
201
Vladimir Marko05792b92015-08-03 11:56:49 +0100202inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, size_t pointer_size) {
203 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700204}
205
Vladimir Marko05792b92015-08-03 11:56:49 +0100206inline mirror::Class* ArtMethod::GetClassFromTypeIndex(uint16_t type_idx,
207 bool resolve,
208 size_t ptr_size) {
209 mirror::Class* type = GetDexCacheResolvedType(type_idx, ptr_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800210 if (type == nullptr && resolve) {
211 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
212 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
213 }
214 return type;
215}
216
Brian Carlstromea46f952013-07-30 01:26:50 -0700217inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 switch (type) {
219 case kStatic:
220 return !IsStatic();
221 case kDirect:
222 return !IsDirect() || IsStatic();
223 case kVirtual: {
Alex Lighteb7c1442015-08-31 13:17:42 -0700224 // We have an error if we are direct or a non-default, non-miranda interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 mirror::Class* methods_class = GetDeclaringClass();
Alex Lighteb7c1442015-08-31 13:17:42 -0700226 return IsDirect() || (methods_class->IsInterface() && !IsDefault() && !IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 }
228 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700229 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700230 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
234 }
235 default:
236 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700237 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 }
239}
240
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243}
244
Ian Rogersef7d42f2014-01-06 12:55:46 -0800245inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 if (!IsRuntimeMethod()) {
247 return false;
248 }
249 Runtime* runtime = Runtime::Current();
250 bool result = false;
251 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
252 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
253 result = true;
254 break;
255 }
256 }
257 return result;
258}
259
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 bool result = this == Runtime::Current()->GetResolutionMethod();
262 // Check that if we do think it is phony it looks like the resolution method.
263 DCHECK(!result || IsRuntimeMethod());
264 return result;
265}
Jeff Hao88474b42013-10-23 16:24:40 -0700266
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700267inline bool ArtMethod::IsImtUnimplementedMethod() {
268 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
269 // Check that if we do think it is phony it looks like the imt unimplemented method.
270 DCHECK(!result || IsRuntimeMethod());
271 return result;
272}
273
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700274inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700275 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700276}
277
278inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
281 return "<runtime method>";
282 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700283 DCHECK(!IsProxyMethod());
284 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
286}
287
288inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000289 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 const DexFile* dex_file = GetDexFile();
291 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700292}
293
294inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700296 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 DCHECK(!IsProxyMethod());
298 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700299 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
300 }
301 return Signature::NoSignature();
302}
303
Ian Rogers1ff3c982014-08-12 02:30:58 -0700304inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 DCHECK(!IsProxyMethod());
308 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700309 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
310 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 Runtime* const runtime = Runtime::Current();
312 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700313 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700315 return "<runtime internal imt conflict method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700317 return "<runtime internal callee-save all registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700319 return "<runtime internal callee-save reference registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700321 return "<runtime internal callee-save reference and argument registers method>";
322 } else {
323 return "<unknown runtime internal method>";
324 }
325}
326
327inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Mathieu Chartier12b3dd72014-12-11 13:25:33 -0800328 return GetDeclaringClass()->GetDexFile().GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700329}
330
Vladimir Marko05792b92015-08-03 11:56:49 +0100331inline bool ArtMethod::IsResolvedTypeIdx(uint16_t type_idx, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 DCHECK(!IsProxyMethod());
Vladimir Marko05792b92015-08-03 11:56:49 +0100333 return GetDexCacheResolvedType(type_idx, ptr_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700334}
335
336inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700338 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 return GetDexFile()->GetLineNumFromPC(this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700342}
343
344inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 DCHECK(!IsProxyMethod());
346 const DexFile* dex_file = GetDexFile();
347 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700348}
349
350inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000351 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000353 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
354 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700355 return dex_file->GetProtoParameters(proto);
356}
357
358inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 DCHECK(!IsProxyMethod());
360 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700361}
362
363inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700364 DCHECK(!IsProxyMethod());
365 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700366}
367
368inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 DCHECK(!IsProxyMethod());
370 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371}
372
373inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374 DCHECK(!IsProxyMethod());
375 const DexFile* dex_file = GetDexFile();
376 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700377 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
378 uint16_t return_type_idx = proto_id.return_type_idx_;
379 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(return_type_idx));
380}
381
382inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 DCHECK(!IsProxyMethod());
384 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700385 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
386}
387
388inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 DCHECK(!IsProxyMethod());
390 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700391}
392
393inline mirror::DexCache* ArtMethod::GetDexCache() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000394 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 return GetDeclaringClass()->GetDexCache();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396}
397
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700398template<ReadBarrierOption kReadBarrierOption>
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700399inline bool ArtMethod::IsProxyMethod() {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700400 return GetDeclaringClass<kReadBarrierOption>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700401}
402
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(size_t pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700404 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405 return this;
406 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700407 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100408 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
409 GetDexCacheResolvedMethods(pointer_size),
410 GetDexMethodIndex(),
411 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700412 DCHECK(interface_method != nullptr);
413 DCHECK_EQ(interface_method,
414 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
415 return interface_method;
416}
417
Vladimir Marko05792b92015-08-03 11:56:49 +0100418inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
419 size_t ptr_size) {
420 SetNativePointer(DexCacheResolvedMethodsOffset(ptr_size), new_dex_cache_methods, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700421}
422
Vladimir Marko05792b92015-08-03 11:56:49 +0100423inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
424 size_t ptr_size) {
425 SetNativePointer(DexCacheResolvedTypesOffset(ptr_size), new_dex_cache_types, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700426}
427
Vladimir Marko05792b92015-08-03 11:56:49 +0100428inline mirror::Class* ArtMethod::GetReturnType(bool resolve, size_t ptr_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700429 DCHECK(!IsProxyMethod());
430 const DexFile* dex_file = GetDexFile();
431 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
432 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
433 uint16_t return_type_idx = proto_id.return_type_idx_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100434 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, ptr_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700435 if (type == nullptr && resolve) {
436 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
437 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
438 }
439 return type;
440}
441
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700442template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700443void ArtMethod::VisitRoots(RootVisitorType& visitor, size_t pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700444 if (LIKELY(!declaring_class_.IsNull())) {
445 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
446 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000447 if (UNLIKELY(klass->IsProxyClass())) {
448 // For normal methods, dex cache shortcuts will be visited through the declaring class.
449 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700450 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000451 GetDexCacheResolvedMethods(pointer_size),
452 GetDexMethodIndex(),
453 pointer_size);
454 DCHECK(interface_method != nullptr);
455 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700456 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
457 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000458 interface_method->VisitRoots(visitor, pointer_size);
459 }
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100460 // We know we don't have profiling information if the class hasn't been verified. Note
461 // that this check also ensures the IsNative call can be made, as IsNative expects a fully
462 // created class (and not a retired one).
463 if (klass->IsVerified()) {
464 // Runtime methods and native methods use the same field as the profiling info for
465 // storing their own data (jni entrypoint for native methods, and ImtConflictTable for
466 // some runtime methods).
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700467 if (!IsNative<kReadBarrierOption>() && !IsRuntimeMethod()) {
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100468 ProfilingInfo* profiling_info = GetProfilingInfo(pointer_size);
469 if (profiling_info != nullptr) {
470 profiling_info->VisitRoots(visitor);
471 }
Hiroshi Yamauchib79eb752016-03-07 13:56:33 -0800472 }
473 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100474 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800475}
476
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800477template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800478inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
479 size_t pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800480 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800481 mirror::Class* new_class = visitor(old_class);
482 if (old_class != new_class) {
483 SetDeclaringClass(new_class);
484 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800485 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800486 ArtMethod** new_methods = visitor(old_methods);
487 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800488 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800489 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800490 GcRoot<mirror::Class>* old_types = GetDexCacheResolvedTypes(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800491 GcRoot<mirror::Class>* new_types = visitor(old_types);
492 if (old_types != new_types) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800493 SetDexCacheResolvedTypes(new_types, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800494 }
495}
496
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800497template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800498inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, size_t pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800499 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800500 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800501 const void* new_native_code = visitor(old_native_code);
502 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800503 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800504 }
505 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700506 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800507 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800508 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800509 const void* new_code = visitor(old_code);
510 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800511 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800512 }
513}
514
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515} // namespace art
516
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517#endif // ART_RUNTIME_ART_METHOD_INL_H_