blob: 24212462597cdcb86d34e3821110e1801908a4b0 [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)) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -070093 if (self->IsThreadSuspensionAllowable()) {
94 ScopedObjectAccess soa(self);
95 CHECK(IsRuntimeMethod() ||
96 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
97 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
98 }
Andreas Gampecbc96b82015-09-30 20:05:24 +000099 } else {
100 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
101 // runnable state (e.g., during GC).
102 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800103 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000104 }
105 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107}
108
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
111 GetDeclaringClass()->IsErroneous());
112 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113}
114
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700115inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700117}
118
Ian Rogersef7d42f2014-01-06 12:55:46 -0800119inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
121 GetDeclaringClass()->IsErroneous());
122 return dex_method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123}
124
Matthew Gharrity465ecc82016-07-19 21:32:52 +0000125inline uint32_t ArtMethod::GetImtIndex() {
126 return GetDexMethodIndex() % ImTable::kSize;
127}
128
Andreas Gampe542451c2016-07-26 09:02:02 -0700129inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100130 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
131 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700132}
133
Andreas Gampe542451c2016-07-26 09:02:02 -0700134inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
135 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
137 // without accessing the DexCache and we don't want to do that in release build.
138 DCHECK_LT(method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700139 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()
Vladimir Marko05792b92015-08-03 11:56:49 +0100140 ->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700141 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700143 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 if (LIKELY(method != nullptr)) {
145 auto* declaring_class = method->GetDeclaringClass();
146 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
147 return method;
148 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700149 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700151}
152
Andreas Gampe542451c2016-07-26 09:02:02 -0700153inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
154 ArtMethod* new_method,
155 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100156 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
157 // without accessing the DexCache and we don't want to do that in release build.
158 DCHECK_LT(method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700159 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()
Vladimir Marko05792b92015-08-03 11:56:49 +0100160 ->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700162 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100163 method_index,
164 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700165 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700166}
167
Andreas Gampe542451c2016-07-26 09:02:02 -0700168inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100169 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700170}
171
Vladimir Marko05792b92015-08-03 11:56:49 +0100172inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700173 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100174 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700175}
176
Andreas Gampe542451c2016-07-26 09:02:02 -0700177inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100178 return GetDexCacheResolvedMethods(pointer_size) ==
179 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700180}
181
Andreas Gampe542451c2016-07-26 09:02:02 -0700182inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100183 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
184 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700185}
186
Andreas Gampe58a5af82014-07-31 16:23:49 -0700187template <bool kWithCheck>
Andreas Gampe542451c2016-07-26 09:02:02 -0700188inline mirror::Class* ArtMethod::GetDexCacheResolvedType(uint32_t type_index,
189 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100190 if (kWithCheck) {
191 mirror::DexCache* dex_cache =
Andreas Gampe542451c2016-07-26 09:02:02 -0700192 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()->GetDexCache();
Vladimir Marko05792b92015-08-03 11:56:49 +0100193 if (UNLIKELY(type_index >= dex_cache->NumResolvedTypes())) {
194 ThrowArrayIndexOutOfBoundsException(type_index, dex_cache->NumResolvedTypes());
195 return nullptr;
196 }
197 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700198 mirror::Class* klass = GetDexCacheResolvedTypes(pointer_size)[type_index].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700199 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
200}
201
Andreas Gampe542451c2016-07-26 09:02:02 -0700202inline bool ArtMethod::HasDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100203 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700204}
205
Vladimir Marko05792b92015-08-03 11:56:49 +0100206inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700207 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100208 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700209}
210
Andreas Gampe542451c2016-07-26 09:02:02 -0700211inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100212 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700213}
214
Vladimir Marko05792b92015-08-03 11:56:49 +0100215inline mirror::Class* ArtMethod::GetClassFromTypeIndex(uint16_t type_idx,
216 bool resolve,
Andreas Gampe542451c2016-07-26 09:02:02 -0700217 PointerSize pointer_size) {
218 mirror::Class* type = GetDexCacheResolvedType(type_idx, pointer_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800219 if (type == nullptr && resolve) {
220 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
221 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
222 }
223 return type;
224}
225
Brian Carlstromea46f952013-07-30 01:26:50 -0700226inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 switch (type) {
228 case kStatic:
229 return !IsStatic();
230 case kDirect:
231 return !IsDirect() || IsStatic();
232 case kVirtual: {
Alex Lighteb7c1442015-08-31 13:17:42 -0700233 // We have an error if we are direct or a non-default, non-miranda interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 mirror::Class* methods_class = GetDeclaringClass();
Alex Lighteb7c1442015-08-31 13:17:42 -0700235 return IsDirect() || (methods_class->IsInterface() && !IsDefault() && !IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 }
237 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700238 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700239 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
243 }
244 default:
245 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700246 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 }
248}
249
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252}
253
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 if (!IsRuntimeMethod()) {
256 return false;
257 }
258 Runtime* runtime = Runtime::Current();
259 bool result = false;
260 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
261 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
262 result = true;
263 break;
264 }
265 }
266 return result;
267}
268
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 bool result = this == Runtime::Current()->GetResolutionMethod();
271 // Check that if we do think it is phony it looks like the resolution method.
272 DCHECK(!result || IsRuntimeMethod());
273 return result;
274}
Jeff Hao88474b42013-10-23 16:24:40 -0700275
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700276inline bool ArtMethod::IsImtUnimplementedMethod() {
277 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
278 // Check that if we do think it is phony it looks like the imt unimplemented method.
279 DCHECK(!result || IsRuntimeMethod());
280 return result;
281}
282
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700284 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285}
286
287inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700289 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
290 return "<runtime method>";
291 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 DCHECK(!IsProxyMethod());
293 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700294 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
295}
296
297inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000298 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 const DexFile* dex_file = GetDexFile();
300 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700301}
302
303inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 DCHECK(!IsProxyMethod());
307 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700308 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
309 }
310 return Signature::NoSignature();
311}
312
Ian Rogers1ff3c982014-08-12 02:30:58 -0700313inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700315 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 DCHECK(!IsProxyMethod());
317 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700318 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
319 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 Runtime* const runtime = Runtime::Current();
321 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700322 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700324 return "<runtime internal imt conflict method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700326 return "<runtime internal callee-save all registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700328 return "<runtime internal callee-save reference registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700330 return "<runtime internal callee-save reference and argument registers method>";
331 } else {
332 return "<unknown runtime internal method>";
333 }
334}
335
336inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Mathieu Chartier12b3dd72014-12-11 13:25:33 -0800337 return GetDeclaringClass()->GetDexFile().GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700338}
339
Andreas Gampe542451c2016-07-26 09:02:02 -0700340inline bool ArtMethod::IsResolvedTypeIdx(uint16_t type_idx, PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 DCHECK(!IsProxyMethod());
Andreas Gampe542451c2016-07-26 09:02:02 -0700342 return GetDexCacheResolvedType(type_idx, pointer_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343}
344
345inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700349 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 return GetDexFile()->GetLineNumFromPC(this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700351}
352
353inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354 DCHECK(!IsProxyMethod());
355 const DexFile* dex_file = GetDexFile();
356 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700357}
358
359inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000360 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000362 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
363 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700364 return dex_file->GetProtoParameters(proto);
365}
366
367inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 DCHECK(!IsProxyMethod());
369 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700370}
371
372inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 DCHECK(!IsProxyMethod());
374 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700375}
376
377inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 DCHECK(!IsProxyMethod());
379 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700380}
381
382inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 DCHECK(!IsProxyMethod());
384 const DexFile* dex_file = GetDexFile();
385 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
387 uint16_t return_type_idx = proto_id.return_type_idx_;
388 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(return_type_idx));
389}
390
391inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 DCHECK(!IsProxyMethod());
393 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700394 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
395}
396
397inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 DCHECK(!IsProxyMethod());
399 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700400}
401
402inline mirror::DexCache* ArtMethod::GetDexCache() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000403 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 return GetDeclaringClass()->GetDexCache();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405}
406
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700407template<ReadBarrierOption kReadBarrierOption>
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700408inline bool ArtMethod::IsProxyMethod() {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700409 return GetDeclaringClass<kReadBarrierOption>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700410}
411
Andreas Gampe542451c2016-07-26 09:02:02 -0700412inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700413 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700414 return this;
415 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700416 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100417 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
418 GetDexCacheResolvedMethods(pointer_size),
419 GetDexMethodIndex(),
420 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700421 DCHECK(interface_method != nullptr);
422 DCHECK_EQ(interface_method,
423 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
424 return interface_method;
425}
426
Vladimir Marko05792b92015-08-03 11:56:49 +0100427inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700428 PointerSize pointer_size) {
429 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
430 new_dex_cache_methods,
431 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700432}
433
Vladimir Marko05792b92015-08-03 11:56:49 +0100434inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
Andreas Gampe542451c2016-07-26 09:02:02 -0700435 PointerSize pointer_size) {
436 SetNativePointer(DexCacheResolvedTypesOffset(pointer_size), new_dex_cache_types, pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700437}
438
Andreas Gampe542451c2016-07-26 09:02:02 -0700439inline mirror::Class* ArtMethod::GetReturnType(bool resolve, PointerSize pointer_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700440 DCHECK(!IsProxyMethod());
441 const DexFile* dex_file = GetDexFile();
442 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
443 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
444 uint16_t return_type_idx = proto_id.return_type_idx_;
Andreas Gampe542451c2016-07-26 09:02:02 -0700445 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, pointer_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700446 if (type == nullptr && resolve) {
447 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
448 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
449 }
450 return type;
451}
452
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700453template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700454void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700455 if (LIKELY(!declaring_class_.IsNull())) {
456 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
457 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000458 if (UNLIKELY(klass->IsProxyClass())) {
459 // For normal methods, dex cache shortcuts will be visited through the declaring class.
460 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700461 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000462 GetDexCacheResolvedMethods(pointer_size),
463 GetDexMethodIndex(),
464 pointer_size);
465 DCHECK(interface_method != nullptr);
466 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700467 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
468 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000469 interface_method->VisitRoots(visitor, pointer_size);
470 }
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100471 // We know we don't have profiling information if the class hasn't been verified. Note
472 // that this check also ensures the IsNative call can be made, as IsNative expects a fully
473 // created class (and not a retired one).
474 if (klass->IsVerified()) {
475 // Runtime methods and native methods use the same field as the profiling info for
476 // storing their own data (jni entrypoint for native methods, and ImtConflictTable for
477 // some runtime methods).
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700478 if (!IsNative<kReadBarrierOption>() && !IsRuntimeMethod()) {
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100479 ProfilingInfo* profiling_info = GetProfilingInfo(pointer_size);
480 if (profiling_info != nullptr) {
481 profiling_info->VisitRoots(visitor);
482 }
Hiroshi Yamauchib79eb752016-03-07 13:56:33 -0800483 }
484 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100485 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800486}
487
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800488template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800489inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700490 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800491 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800492 mirror::Class* new_class = visitor(old_class);
493 if (old_class != new_class) {
494 SetDeclaringClass(new_class);
495 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800496 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800497 ArtMethod** new_methods = visitor(old_methods);
498 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800499 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800500 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800501 GcRoot<mirror::Class>* old_types = GetDexCacheResolvedTypes(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800502 GcRoot<mirror::Class>* new_types = visitor(old_types);
503 if (old_types != new_types) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800504 SetDexCacheResolvedTypes(new_types, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800505 }
506}
507
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800508template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700509inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800510 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800511 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800512 const void* new_native_code = visitor(old_native_code);
513 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800514 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800515 }
516 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700517 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800518 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800519 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800520 const void* new_code = visitor(old_code);
521 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800522 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800523 }
524}
525
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526} // namespace art
527
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528#endif // ART_RUNTIME_ART_METHOD_INL_H_