blob: 499b7fe8fb194967783c0dcf9054d8a6c7830f7d [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"
David Sehr9323e6e2016-09-13 08:58:35 -070027#include "dex_file_annotations.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080028#include "dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "gc_root-inl.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010030#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010032#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/object-inl.h"
34#include "mirror/object_array.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010035#include "oat.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010036#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010038#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampecbc96b82015-09-30 20:05:24 +000040#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010041#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042
43namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080045template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070046inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070047 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080048 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070049}
50
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080051template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070052inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080053 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 if (kIsDebugBuild) {
55 if (!IsRuntimeMethod()) {
56 CHECK(result != nullptr) << this;
57 CHECK(result->IsIdxLoaded() || result->IsErroneous())
David Sehr709b0702016-10-13 09:12:37 -070058 << result->GetStatus() << " " << result->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 } else {
60 CHECK(result == nullptr) << this;
61 }
62 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 return result;
64}
65
Mathieu Chartiere401d142015-04-22 13:56:20 -070066inline void ArtMethod::SetDeclaringClass(mirror::Class* new_declaring_class) {
67 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068}
69
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070070inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
71 mirror::Class* desired_class) {
72 GcRoot<mirror::Class> expected_root(expected_class);
73 GcRoot<mirror::Class> desired_root(desired_class);
74 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
75 CompareExchangeStrongSequentiallyConsistent(
76 expected_root, desired_root);
77}
78
Andreas Gampecbc96b82015-09-30 20:05:24 +000079// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
80// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080081template <ReadBarrierOption kReadBarrierOption>
82ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
83 NO_THREAD_SAFETY_ANALYSIS {
84 CHECK(method->IsRuntimeMethod() ||
85 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
86 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000087}
88
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080089template <ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080090inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampecbc96b82015-09-30 20:05:24 +000091 if (kIsDebugBuild) {
92 Thread* self = Thread::Current();
93 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -070094 if (self->IsThreadSuspensionAllowable()) {
95 ScopedObjectAccess soa(self);
96 CHECK(IsRuntimeMethod() ||
97 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
98 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
99 }
Andreas Gampecbc96b82015-09-30 20:05:24 +0000100 } else {
101 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
102 // runnable state (e.g., during GC).
103 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800104 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000105 }
106 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108}
109
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
112 GetDeclaringClass()->IsErroneous());
113 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114}
115
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700116inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
122 GetDeclaringClass()->IsErroneous());
123 return dex_method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124}
125
Andreas Gampe542451c2016-07-26 09:02:02 -0700126inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100127 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
128 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700129}
130
Andreas Gampe542451c2016-07-26 09:02:02 -0700131inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
132 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100133 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
134 // without accessing the DexCache and we don't want to do that in release build.
135 DCHECK_LT(method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700136 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 ->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700138 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100139 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700140 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 if (LIKELY(method != nullptr)) {
142 auto* declaring_class = method->GetDeclaringClass();
143 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
144 return method;
145 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700146 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700148}
149
Andreas Gampe542451c2016-07-26 09:02:02 -0700150inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
151 ArtMethod* new_method,
152 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100153 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
154 // without accessing the DexCache and we don't want to do that in release build.
155 DCHECK_LT(method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700156 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()
Vladimir Marko05792b92015-08-03 11:56:49 +0100157 ->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700159 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100160 method_index,
161 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700162 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700163}
164
Andreas Gampe542451c2016-07-26 09:02:02 -0700165inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100166 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700167}
168
Vladimir Marko05792b92015-08-03 11:56:49 +0100169inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700170 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100171 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700172}
173
Andreas Gampe542451c2016-07-26 09:02:02 -0700174inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100175 return GetDexCacheResolvedMethods(pointer_size) ==
176 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700177}
178
Andreas Gampe542451c2016-07-26 09:02:02 -0700179inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100180 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
181 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700182}
183
Andreas Gampe58a5af82014-07-31 16:23:49 -0700184template <bool kWithCheck>
Andreas Gampe542451c2016-07-26 09:02:02 -0700185inline mirror::Class* ArtMethod::GetDexCacheResolvedType(uint32_t type_index,
186 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100187 if (kWithCheck) {
188 mirror::DexCache* dex_cache =
Andreas Gampe542451c2016-07-26 09:02:02 -0700189 GetInterfaceMethodIfProxy(pointer_size)->GetDeclaringClass()->GetDexCache();
Vladimir Marko05792b92015-08-03 11:56:49 +0100190 if (UNLIKELY(type_index >= dex_cache->NumResolvedTypes())) {
191 ThrowArrayIndexOutOfBoundsException(type_index, dex_cache->NumResolvedTypes());
192 return nullptr;
193 }
194 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700195 mirror::Class* klass = GetDexCacheResolvedTypes(pointer_size)[type_index].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700196 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
197}
198
Andreas Gampe542451c2016-07-26 09:02:02 -0700199inline bool ArtMethod::HasDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100200 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700201}
202
Vladimir Marko05792b92015-08-03 11:56:49 +0100203inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700204 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100205 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700206}
207
Andreas Gampe542451c2016-07-26 09:02:02 -0700208inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100209 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700210}
211
Vladimir Marko05792b92015-08-03 11:56:49 +0100212inline mirror::Class* ArtMethod::GetClassFromTypeIndex(uint16_t type_idx,
213 bool resolve,
Andreas Gampe542451c2016-07-26 09:02:02 -0700214 PointerSize pointer_size) {
215 mirror::Class* type = GetDexCacheResolvedType(type_idx, pointer_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800216 if (type == nullptr && resolve) {
217 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
218 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
219 }
220 return type;
221}
222
Brian Carlstromea46f952013-07-30 01:26:50 -0700223inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 switch (type) {
225 case kStatic:
226 return !IsStatic();
227 case kDirect:
228 return !IsDirect() || IsStatic();
229 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700230 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
231 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700233 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 }
235 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700236 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700237 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
241 }
242 default:
243 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700244 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 }
246}
247
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250}
251
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 if (!IsRuntimeMethod()) {
254 return false;
255 }
256 Runtime* runtime = Runtime::Current();
257 bool result = false;
258 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
259 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
260 result = true;
261 break;
262 }
263 }
264 return result;
265}
266
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 bool result = this == Runtime::Current()->GetResolutionMethod();
269 // Check that if we do think it is phony it looks like the resolution method.
270 DCHECK(!result || IsRuntimeMethod());
271 return result;
272}
Jeff Hao88474b42013-10-23 16:24:40 -0700273
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700274inline bool ArtMethod::IsImtUnimplementedMethod() {
275 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
276 // Check that if we do think it is phony it looks like the imt unimplemented method.
277 DCHECK(!result || IsRuntimeMethod());
278 return result;
279}
280
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700281inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700282 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283}
284
285inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700287 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
288 return "<runtime method>";
289 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 DCHECK(!IsProxyMethod());
291 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700292 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
293}
294
295inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000296 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 const DexFile* dex_file = GetDexFile();
298 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700299}
300
301inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700303 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 DCHECK(!IsProxyMethod());
305 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
307 }
308 return Signature::NoSignature();
309}
310
Ian Rogers1ff3c982014-08-12 02:30:58 -0700311inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700313 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 DCHECK(!IsProxyMethod());
315 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700316 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
317 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 Runtime* const runtime = Runtime::Current();
319 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700320 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700322 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100323 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700324 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100325 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700326 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100327 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700328 return "<runtime internal callee-save reference and argument registers method>";
329 } else {
330 return "<unknown runtime internal method>";
331 }
332}
333
334inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Mathieu Chartier12b3dd72014-12-11 13:25:33 -0800335 return GetDeclaringClass()->GetDexFile().GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700336}
337
Andreas Gampe542451c2016-07-26 09:02:02 -0700338inline bool ArtMethod::IsResolvedTypeIdx(uint16_t type_idx, PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 DCHECK(!IsProxyMethod());
Andreas Gampe542451c2016-07-26 09:02:02 -0700340 return GetDexCacheResolvedType(type_idx, pointer_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700341}
342
343inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347 }
David Sehr9323e6e2016-09-13 08:58:35 -0700348 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700349}
350
351inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 DCHECK(!IsProxyMethod());
353 const DexFile* dex_file = GetDexFile();
354 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700355}
356
357inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000358 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000360 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
361 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362 return dex_file->GetProtoParameters(proto);
363}
364
365inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 DCHECK(!IsProxyMethod());
367 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368}
369
370inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 DCHECK(!IsProxyMethod());
372 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700373}
374
375inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700376 DCHECK(!IsProxyMethod());
377 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700378}
379
380inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 DCHECK(!IsProxyMethod());
382 const DexFile* dex_file = GetDexFile();
383 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700384 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
385 uint16_t return_type_idx = proto_id.return_type_idx_;
386 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(return_type_idx));
387}
388
389inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390 DCHECK(!IsProxyMethod());
391 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700392 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
393}
394
395inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 DCHECK(!IsProxyMethod());
397 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700398}
399
400inline mirror::DexCache* ArtMethod::GetDexCache() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000401 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 return GetDeclaringClass()->GetDexCache();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700403}
404
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700405template<ReadBarrierOption kReadBarrierOption>
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700406inline bool ArtMethod::IsProxyMethod() {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700407 return GetDeclaringClass<kReadBarrierOption>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700408}
409
Andreas Gampe542451c2016-07-26 09:02:02 -0700410inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700411 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700412 return this;
413 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700414 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100415 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
416 GetDexCacheResolvedMethods(pointer_size),
417 GetDexMethodIndex(),
418 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700419 DCHECK(interface_method != nullptr);
420 DCHECK_EQ(interface_method,
421 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
422 return interface_method;
423}
424
Vladimir Marko05792b92015-08-03 11:56:49 +0100425inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700426 PointerSize pointer_size) {
427 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
428 new_dex_cache_methods,
429 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700430}
431
Vladimir Marko05792b92015-08-03 11:56:49 +0100432inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
Andreas Gampe542451c2016-07-26 09:02:02 -0700433 PointerSize pointer_size) {
434 SetNativePointer(DexCacheResolvedTypesOffset(pointer_size), new_dex_cache_types, pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700435}
436
Andreas Gampe542451c2016-07-26 09:02:02 -0700437inline mirror::Class* ArtMethod::GetReturnType(bool resolve, PointerSize pointer_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700438 DCHECK(!IsProxyMethod());
439 const DexFile* dex_file = GetDexFile();
440 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
441 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
442 uint16_t return_type_idx = proto_id.return_type_idx_;
Andreas Gampe542451c2016-07-26 09:02:02 -0700443 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, pointer_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700444 if (type == nullptr && resolve) {
445 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
446 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
447 }
448 return type;
449}
450
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700451template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700452void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700453 if (LIKELY(!declaring_class_.IsNull())) {
454 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
455 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000456 if (UNLIKELY(klass->IsProxyClass())) {
457 // For normal methods, dex cache shortcuts will be visited through the declaring class.
458 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700459 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000460 GetDexCacheResolvedMethods(pointer_size),
461 GetDexMethodIndex(),
462 pointer_size);
463 DCHECK(interface_method != nullptr);
464 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700465 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
466 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000467 interface_method->VisitRoots(visitor, pointer_size);
468 }
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100469 // We know we don't have profiling information if the class hasn't been verified. Note
470 // that this check also ensures the IsNative call can be made, as IsNative expects a fully
471 // created class (and not a retired one).
472 if (klass->IsVerified()) {
473 // Runtime methods and native methods use the same field as the profiling info for
474 // storing their own data (jni entrypoint for native methods, and ImtConflictTable for
475 // some runtime methods).
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700476 if (!IsNative<kReadBarrierOption>() && !IsRuntimeMethod()) {
Nicolas Geoffray022dd862016-05-04 09:51:24 +0100477 ProfilingInfo* profiling_info = GetProfilingInfo(pointer_size);
478 if (profiling_info != nullptr) {
479 profiling_info->VisitRoots(visitor);
480 }
Hiroshi Yamauchib79eb752016-03-07 13:56:33 -0800481 }
482 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100483 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800484}
485
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800486template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800487inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700488 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800489 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800490 mirror::Class* new_class = visitor(old_class);
491 if (old_class != new_class) {
492 SetDeclaringClass(new_class);
493 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800494 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800495 ArtMethod** new_methods = visitor(old_methods);
496 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800497 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800498 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800499 GcRoot<mirror::Class>* old_types = GetDexCacheResolvedTypes(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800500 GcRoot<mirror::Class>* new_types = visitor(old_types);
501 if (old_types != new_types) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800502 SetDexCacheResolvedTypes(new_types, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800503 }
504}
505
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800506template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700507inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800508 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800509 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800510 const void* new_native_code = visitor(old_native_code);
511 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800512 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800513 }
514 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700515 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800516 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800517 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800518 const void* new_code = visitor(old_code);
519 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800520 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800521 }
522}
523
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524} // namespace art
525
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526#endif // ART_RUNTIME_ART_METHOD_INL_H_