blob: 9c207400a2f0461707a0144e866d8720178eee9d [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"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070036#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010037#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010039#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Andreas Gampecbc96b82015-09-30 20:05:24 +000041#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010042#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043
44namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080046template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070047inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070048 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080049 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070050}
51
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080052template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070053inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080054 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070055 if (kIsDebugBuild) {
56 if (!IsRuntimeMethod()) {
57 CHECK(result != nullptr) << this;
58 CHECK(result->IsIdxLoaded() || result->IsErroneous())
David Sehr709b0702016-10-13 09:12:37 -070059 << result->GetStatus() << " " << result->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 } else {
61 CHECK(result == nullptr) << this;
62 }
63 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 return result;
65}
66
Mathieu Chartier28357fa2016-10-18 16:27:40 -070067inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069}
70
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070071inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
72 mirror::Class* desired_class) {
73 GcRoot<mirror::Class> expected_root(expected_class);
74 GcRoot<mirror::Class> desired_root(desired_class);
75 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
76 CompareExchangeStrongSequentiallyConsistent(
77 expected_root, desired_root);
78}
79
Andreas Gampecbc96b82015-09-30 20:05:24 +000080// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
81// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080082template <ReadBarrierOption kReadBarrierOption>
83ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
84 NO_THREAD_SAFETY_ANALYSIS {
85 CHECK(method->IsRuntimeMethod() ||
86 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
87 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000088}
89
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080090template <ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080091inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampecbc96b82015-09-30 20:05:24 +000092 if (kIsDebugBuild) {
93 Thread* self = Thread::Current();
94 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -070095 if (self->IsThreadSuspensionAllowable()) {
96 ScopedObjectAccess soa(self);
97 CHECK(IsRuntimeMethod() ||
98 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
99 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
100 }
Andreas Gampecbc96b82015-09-30 20:05:24 +0000101 } else {
102 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
103 // runnable state (e.g., during GC).
104 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800105 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000106 }
107 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700108 return access_flags_.load(std::memory_order_relaxed);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109}
110
Ian Rogersef7d42f2014-01-06 12:55:46 -0800111inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
113 GetDeclaringClass()->IsErroneous());
114 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115}
116
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700117inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700119}
120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
123 GetDeclaringClass()->IsErroneous());
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000124 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125}
126
Andreas Gampe542451c2016-07-26 09:02:02 -0700127inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100128 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
129 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700130}
131
Andreas Gampe542451c2016-07-26 09:02:02 -0700132inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
133 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100134 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
135 // without accessing the DexCache and we don't want to do that in release build.
136 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800137 GetInterfaceMethodIfProxy(pointer_size)->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,
Alex Lightdba61482016-12-21 08:20:29 -0800156 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700158 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100159 method_index,
160 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700161 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700162}
163
Andreas Gampe542451c2016-07-26 09:02:02 -0700164inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100165 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700166}
167
Vladimir Marko05792b92015-08-03 11:56:49 +0100168inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700169 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100170 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700171}
172
Andreas Gampe542451c2016-07-26 09:02:02 -0700173inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100174 return GetDexCacheResolvedMethods(pointer_size) ==
175 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700176}
177
Andreas Gampe542451c2016-07-26 09:02:02 -0700178inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100179 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
180 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700181}
182
Andreas Gampe58a5af82014-07-31 16:23:49 -0700183template <bool kWithCheck>
Andreas Gampea5b09a62016-11-17 15:21:22 -0800184inline mirror::Class* ArtMethod::GetDexCacheResolvedType(dex::TypeIndex type_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700185 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100186 if (kWithCheck) {
Alex Lightdba61482016-12-21 08:20:29 -0800187 mirror::DexCache* dex_cache = GetInterfaceMethodIfProxy(pointer_size)->GetDexCache();
Andreas Gampea5b09a62016-11-17 15:21:22 -0800188 if (UNLIKELY(type_index.index_ >= dex_cache->NumResolvedTypes())) {
189 ThrowArrayIndexOutOfBoundsException(type_index.index_, dex_cache->NumResolvedTypes());
Vladimir Marko05792b92015-08-03 11:56:49 +0100190 return nullptr;
191 }
192 }
Andreas Gampea5b09a62016-11-17 15:21:22 -0800193 mirror::Class* klass = GetDexCacheResolvedTypes(pointer_size)[type_index.index_].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700194 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
195}
196
Andreas Gampe542451c2016-07-26 09:02:02 -0700197inline bool ArtMethod::HasDexCacheResolvedTypes(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100198 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700199}
200
Vladimir Marko05792b92015-08-03 11:56:49 +0100201inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700202 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100203 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700204}
205
Andreas Gampe542451c2016-07-26 09:02:02 -0700206inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100207 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700208}
209
Andreas Gampea5b09a62016-11-17 15:21:22 -0800210inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx,
Vladimir Marko05792b92015-08-03 11:56:49 +0100211 bool resolve,
Andreas Gampe542451c2016-07-26 09:02:02 -0700212 PointerSize pointer_size) {
213 mirror::Class* type = GetDexCacheResolvedType(type_idx, pointer_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800214 if (type == nullptr && resolve) {
215 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
216 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
217 }
218 return type;
219}
220
Brian Carlstromea46f952013-07-30 01:26:50 -0700221inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 switch (type) {
223 case kStatic:
224 return !IsStatic();
225 case kDirect:
226 return !IsDirect() || IsStatic();
227 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700228 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
229 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700231 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 }
233 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700234 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700235 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
239 }
240 default:
241 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700242 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 }
244}
245
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248}
249
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 if (!IsRuntimeMethod()) {
252 return false;
253 }
254 Runtime* runtime = Runtime::Current();
255 bool result = false;
256 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
257 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
258 result = true;
259 break;
260 }
261 }
262 return result;
263}
264
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 bool result = this == Runtime::Current()->GetResolutionMethod();
267 // Check that if we do think it is phony it looks like the resolution method.
268 DCHECK(!result || IsRuntimeMethod());
269 return result;
270}
Jeff Hao88474b42013-10-23 16:24:40 -0700271
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700272inline bool ArtMethod::IsImtUnimplementedMethod() {
273 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
274 // Check that if we do think it is phony it looks like the imt unimplemented method.
275 DCHECK(!result || IsRuntimeMethod());
276 return result;
277}
278
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700280 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700281}
282
283inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
286 return "<runtime method>";
287 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 DCHECK(!IsProxyMethod());
289 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700290 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
291}
292
293inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000294 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 const DexFile* dex_file = GetDexFile();
296 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700297}
298
299inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700301 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 DCHECK(!IsProxyMethod());
303 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700304 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
305 }
306 return Signature::NoSignature();
307}
308
Ian Rogers1ff3c982014-08-12 02:30:58 -0700309inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700311 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 DCHECK(!IsProxyMethod());
313 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700314 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
315 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 Runtime* const runtime = Runtime::Current();
317 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700318 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700320 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100321 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700322 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100323 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700324 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100325 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700326 return "<runtime internal callee-save reference and argument registers method>";
327 } else {
328 return "<unknown runtime internal method>";
329 }
330}
331
332inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800333 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700334}
335
Andreas Gampea5b09a62016-11-17 15:21:22 -0800336inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx, PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK(!IsProxyMethod());
Andreas Gampe542451c2016-07-26 09:02:02 -0700338 return GetDexCacheResolvedType(type_idx, pointer_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700339}
340
341inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345 }
David Sehr9323e6e2016-09-13 08:58:35 -0700346 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347}
348
349inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 DCHECK(!IsProxyMethod());
351 const DexFile* dex_file = GetDexFile();
352 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700353}
354
355inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000356 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700357 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000358 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
359 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700360 return dex_file->GetProtoParameters(proto);
361}
362
363inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700364 DCHECK(!IsProxyMethod());
365 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700366}
367
368inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 DCHECK(!IsProxyMethod());
370 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371}
372
373inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374 DCHECK(!IsProxyMethod());
375 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700376}
377
378inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 DCHECK(!IsProxyMethod());
380 const DexFile* dex_file = GetDexFile();
381 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700382 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800383 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700384}
385
Andreas Gampea5b09a62016-11-17 15:21:22 -0800386inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387 DCHECK(!IsProxyMethod());
388 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700389 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
390}
391
392inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 DCHECK(!IsProxyMethod());
394 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700395}
396
397inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800398 if (LIKELY(!IsObsolete())) {
Nicolas Geoffrayf6abcda2016-12-21 09:26:18 +0000399 return GetDeclaringClass()->GetDexCache();
Alex Lightdba61482016-12-21 08:20:29 -0800400 } else {
401 DCHECK(!IsProxyMethod());
402 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800403 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700404}
405
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700406template<ReadBarrierOption kReadBarrierOption>
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700407inline bool ArtMethod::IsProxyMethod() {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700408 return GetDeclaringClass<kReadBarrierOption>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700409}
410
Andreas Gampe542451c2016-07-26 09:02:02 -0700411inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700412 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700413 return this;
414 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700415 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100416 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
417 GetDexCacheResolvedMethods(pointer_size),
418 GetDexMethodIndex(),
419 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700420 DCHECK(interface_method != nullptr);
421 DCHECK_EQ(interface_method,
422 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
423 return interface_method;
424}
425
Vladimir Marko05792b92015-08-03 11:56:49 +0100426inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700427 PointerSize pointer_size) {
428 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
429 new_dex_cache_methods,
430 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700431}
432
Vladimir Marko05792b92015-08-03 11:56:49 +0100433inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
Andreas Gampe542451c2016-07-26 09:02:02 -0700434 PointerSize pointer_size) {
435 SetNativePointer(DexCacheResolvedTypesOffset(pointer_size), new_dex_cache_types, pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700436}
437
Andreas Gampe542451c2016-07-26 09:02:02 -0700438inline mirror::Class* ArtMethod::GetReturnType(bool resolve, PointerSize pointer_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700439 DCHECK(!IsProxyMethod());
440 const DexFile* dex_file = GetDexFile();
441 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
442 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800443 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Andreas Gampe542451c2016-07-26 09:02:02 -0700444 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, pointer_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700445 if (type == nullptr && resolve) {
446 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
447 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
448 }
449 return type;
450}
451
Mingyao Yang063fc772016-08-02 11:02:54 -0700452inline bool ArtMethod::HasSingleImplementation() {
453 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
454 // We don't set kAccSingleImplementation for these cases since intrinsic
455 // can use the flag also.
456 return true;
457 }
458 return (GetAccessFlags() & kAccSingleImplementation) != 0;
459}
460
461inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
462 DCHECK(IsUint<8>(intrinsic));
463 // Currently we only do intrinsics for static/final methods or methods of final
464 // classes. We don't set kHasSingleImplementation for those methods.
465 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
466 "Potential conflict with kAccSingleImplementation";
467 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
468 kAccIntrinsic |
469 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
470 if (kIsDebugBuild) {
471 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
472 bool is_constructor = IsConstructor();
473 bool is_synchronized = IsSynchronized();
474 bool skip_access_checks = SkipAccessChecks();
475 bool is_fast_native = IsFastNative();
476 bool is_copied = IsCopied();
477 bool is_miranda = IsMiranda();
478 bool is_default = IsDefault();
479 bool is_default_conflict = IsDefaultConflicting();
480 bool is_compilable = IsCompilable();
481 bool must_count_locks = MustCountLocks();
482 SetAccessFlags(new_value);
483 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
484 DCHECK_EQ(is_constructor, IsConstructor());
485 DCHECK_EQ(is_synchronized, IsSynchronized());
486 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
487 DCHECK_EQ(is_fast_native, IsFastNative());
488 DCHECK_EQ(is_copied, IsCopied());
489 DCHECK_EQ(is_miranda, IsMiranda());
490 DCHECK_EQ(is_default, IsDefault());
491 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
492 DCHECK_EQ(is_compilable, IsCompilable());
493 DCHECK_EQ(must_count_locks, MustCountLocks());
494 } else {
495 SetAccessFlags(new_value);
496 }
497}
498
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700499template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700500void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700501 if (LIKELY(!declaring_class_.IsNull())) {
502 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
503 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000504 if (UNLIKELY(klass->IsProxyClass())) {
505 // For normal methods, dex cache shortcuts will be visited through the declaring class.
506 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700507 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000508 GetDexCacheResolvedMethods(pointer_size),
509 GetDexMethodIndex(),
510 pointer_size);
511 DCHECK(interface_method != nullptr);
512 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700513 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
514 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000515 interface_method->VisitRoots(visitor, pointer_size);
516 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100517 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800518}
519
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800520template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800521inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700522 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800523 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800524 mirror::Class* new_class = visitor(old_class);
525 if (old_class != new_class) {
526 SetDeclaringClass(new_class);
527 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800528 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800529 ArtMethod** new_methods = visitor(old_methods);
530 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800531 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800532 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800533 GcRoot<mirror::Class>* old_types = GetDexCacheResolvedTypes(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800534 GcRoot<mirror::Class>* new_types = visitor(old_types);
535 if (old_types != new_types) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800536 SetDexCacheResolvedTypes(new_types, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800537 }
538}
539
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800540template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700541inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800542 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800543 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800544 const void* new_native_code = visitor(old_native_code);
545 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800546 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800547 }
548 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700549 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800550 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800551 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800552 const void* new_code = visitor(old_code);
553 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800554 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800555 }
556}
557
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558} // namespace art
559
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560#endif // ART_RUNTIME_ART_METHOD_INL_H_