blob: 5cf0e0f90cd478c78853c102869457584cddc42c [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"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000035#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010036#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070037#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010038#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010040#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070041#include "scoped_thread_state_change-inl.h"
Andreas Gampecbc96b82015-09-30 20:05:24 +000042#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010043#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
45namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080047template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070048inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070049 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080050 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070051}
52
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080053template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070054inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080055 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 if (kIsDebugBuild) {
57 if (!IsRuntimeMethod()) {
58 CHECK(result != nullptr) << this;
Andreas Gampeb1106e22017-02-23 11:34:48 -080059 if (kCheckDeclaringClassState) {
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000060 if (!(result->IsIdxLoaded() || result->IsErroneous())) {
61 LOG(FATAL_WITHOUT_ABORT) << "Class status: " << result->GetStatus();
62 LOG(FATAL) << result->PrettyClass();
63 }
Andreas Gampeb1106e22017-02-23 11:34:48 -080064 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 } else {
66 CHECK(result == nullptr) << this;
67 }
68 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 return result;
70}
71
Mathieu Chartier28357fa2016-10-18 16:27:40 -070072inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074}
75
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070076inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
77 mirror::Class* desired_class) {
78 GcRoot<mirror::Class> expected_root(expected_class);
79 GcRoot<mirror::Class> desired_root(desired_class);
80 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
81 CompareExchangeStrongSequentiallyConsistent(
82 expected_root, desired_root);
83}
84
Andreas Gampecbc96b82015-09-30 20:05:24 +000085// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
86// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080087template <ReadBarrierOption kReadBarrierOption>
88ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
89 NO_THREAD_SAFETY_ANALYSIS {
90 CHECK(method->IsRuntimeMethod() ||
91 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
92 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000093}
94
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080095template <ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080096inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampeb1106e22017-02-23 11:34:48 -080097 if (kCheckDeclaringClassState) {
Andreas Gampecbc96b82015-09-30 20:05:24 +000098 Thread* self = Thread::Current();
99 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -0700100 if (self->IsThreadSuspensionAllowable()) {
101 ScopedObjectAccess soa(self);
102 CHECK(IsRuntimeMethod() ||
103 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
104 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
105 }
Andreas Gampecbc96b82015-09-30 20:05:24 +0000106 } else {
107 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
108 // runnable state (e.g., during GC).
109 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800110 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000111 }
112 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700113 return access_flags_.load(std::memory_order_relaxed);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114}
115
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000117 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119}
120
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700121inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700123}
124
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125inline uint32_t ArtMethod::GetDexMethodIndex() {
Andreas Gampeb1106e22017-02-23 11:34:48 -0800126 if (kCheckDeclaringClassState) {
127 CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
128 GetDeclaringClass()->IsErroneous());
129 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000130 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131}
132
Andreas Gampe542451c2016-07-26 09:02:02 -0700133inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100134 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
135 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700136}
137
Andreas Gampe542451c2016-07-26 09:02:02 -0700138inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
139 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100140 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
141 // without accessing the DexCache and we don't want to do that in release build.
142 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800143 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700144 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100145 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700146 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 if (LIKELY(method != nullptr)) {
148 auto* declaring_class = method->GetDeclaringClass();
149 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
150 return method;
151 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700152 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700154}
155
Andreas Gampe542451c2016-07-26 09:02:02 -0700156inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
157 ArtMethod* new_method,
158 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100159 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
160 // without accessing the DexCache and we don't want to do that in release build.
161 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800162 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700164 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100165 method_index,
166 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700167 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700168}
169
Andreas Gampe542451c2016-07-26 09:02:02 -0700170inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100171 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700172}
173
Vladimir Marko05792b92015-08-03 11:56:49 +0100174inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700175 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100176 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700177}
178
Andreas Gampe542451c2016-07-26 09:02:02 -0700179inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100180 return GetDexCacheResolvedMethods(pointer_size) ==
181 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700182}
183
Vladimir Marko942fd312017-01-16 20:52:19 +0000184inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx, bool resolve) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000185 // TODO: Refactor this function into two functions, Resolve...() and Lookup...()
186 // so that we can properly annotate it with no-suspension possible / suspension possible.
Vladimir Marko942fd312017-01-16 20:52:19 +0000187 ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
188 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000189 if (UNLIKELY(type == nullptr)) {
Vladimir Marko942fd312017-01-16 20:52:19 +0000190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000191 if (resolve) {
192 type = class_linker->ResolveType(type_idx, this);
193 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
194 } else {
195 type = class_linker->LookupResolvedType(
196 *dex_cache->GetDexFile(), type_idx, dex_cache, GetClassLoader());
197 }
Ian Rogersa0485602014-12-02 15:48:04 -0800198 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000199 return type.Ptr();
Ian Rogersa0485602014-12-02 15:48:04 -0800200}
201
Brian Carlstromea46f952013-07-30 01:26:50 -0700202inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 switch (type) {
204 case kStatic:
205 return !IsStatic();
206 case kDirect:
207 return !IsDirect() || IsStatic();
208 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700209 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
210 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700212 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 }
214 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700215 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700216 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
220 }
221 default:
222 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700223 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 }
225}
226
Ian Rogersef7d42f2014-01-06 12:55:46 -0800227inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229}
230
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 if (!IsRuntimeMethod()) {
233 return false;
234 }
235 Runtime* runtime = Runtime::Current();
236 bool result = false;
237 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
238 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
239 result = true;
240 break;
241 }
242 }
243 return result;
244}
245
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 bool result = this == Runtime::Current()->GetResolutionMethod();
248 // Check that if we do think it is phony it looks like the resolution method.
249 DCHECK(!result || IsRuntimeMethod());
250 return result;
251}
Jeff Hao88474b42013-10-23 16:24:40 -0700252
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700253inline bool ArtMethod::IsImtUnimplementedMethod() {
254 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
255 // Check that if we do think it is phony it looks like the imt unimplemented method.
256 DCHECK(!result || IsRuntimeMethod());
257 return result;
258}
259
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700260inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800261 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
262 // from-space dex file pointer it will be equal to the to-space copy.
263 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700264}
265
266inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700268 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
269 return "<runtime method>";
270 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 DCHECK(!IsProxyMethod());
272 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700273 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
274}
275
276inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000277 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 const DexFile* dex_file = GetDexFile();
279 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280}
281
282inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700283 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700284 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 DCHECK(!IsProxyMethod());
286 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700287 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
288 }
289 return Signature::NoSignature();
290}
291
Ian Rogers1ff3c982014-08-12 02:30:58 -0700292inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700294 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 DCHECK(!IsProxyMethod());
296 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700297 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
298 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 Runtime* const runtime = Runtime::Current();
300 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700301 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700303 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100304 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100306 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700307 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100308 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700309 return "<runtime internal callee-save reference and argument registers method>";
310 } else {
311 return "<unknown runtime internal method>";
312 }
313}
314
315inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800316 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700317}
318
Vladimir Marko942fd312017-01-16 20:52:19 +0000319inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 DCHECK(!IsProxyMethod());
Vladimir Marko942fd312017-01-16 20:52:19 +0000321 return GetClassFromTypeIndex(type_idx, /* resolve */ false) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700322}
323
324inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700326 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700328 }
David Sehr9323e6e2016-09-13 08:58:35 -0700329 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700330}
331
332inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK(!IsProxyMethod());
334 const DexFile* dex_file = GetDexFile();
335 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700336}
337
338inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000339 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000341 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
342 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343 return dex_file->GetProtoParameters(proto);
344}
345
346inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 DCHECK(!IsProxyMethod());
348 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700349}
350
351inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000353 if (LIKELY(!IsObsolete())) {
354 return GetDeclaringClass()->GetDexClassDefIndex();
355 } else {
356 return FindObsoleteDexClassDefIndex();
357 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700358}
359
360inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 DCHECK(!IsProxyMethod());
362 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700363}
364
365inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 DCHECK(!IsProxyMethod());
367 const DexFile* dex_file = GetDexFile();
368 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700369 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800370 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371}
372
Andreas Gampea5b09a62016-11-17 15:21:22 -0800373inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374 DCHECK(!IsProxyMethod());
375 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700376 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
377}
378
379inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380 DCHECK(!IsProxyMethod());
381 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700382}
383
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800384template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700385inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800386 if (LIKELY(!IsObsolete())) {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800387 mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
388 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800389 } else {
390 DCHECK(!IsProxyMethod());
391 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800392 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700393}
394
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700395inline bool ArtMethod::IsProxyMethod() {
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800396 // Avoid read barrier since the from-space version of the class will have the correct proxy class
397 // flags since they are constant for the lifetime of the class.
398 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700399}
400
Andreas Gampe542451c2016-07-26 09:02:02 -0700401inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700402 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700403 return this;
404 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100405 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
406 GetDexCacheResolvedMethods(pointer_size),
407 GetDexMethodIndex(),
408 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700409 DCHECK(interface_method != nullptr);
410 DCHECK_EQ(interface_method,
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800411 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700412 return interface_method;
413}
414
Vladimir Marko05792b92015-08-03 11:56:49 +0100415inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700416 PointerSize pointer_size) {
417 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
418 new_dex_cache_methods,
419 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700420}
421
Vladimir Marko942fd312017-01-16 20:52:19 +0000422inline mirror::Class* ArtMethod::GetReturnType(bool resolve) {
Ian Rogersded66a02014-10-28 18:12:55 -0700423 DCHECK(!IsProxyMethod());
424 const DexFile* dex_file = GetDexFile();
425 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
426 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800427 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000428 return GetClassFromTypeIndex(return_type_idx, resolve);
Ian Rogersded66a02014-10-28 18:12:55 -0700429}
430
Mingyao Yang063fc772016-08-02 11:02:54 -0700431inline bool ArtMethod::HasSingleImplementation() {
432 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
433 // We don't set kAccSingleImplementation for these cases since intrinsic
434 // can use the flag also.
435 return true;
436 }
437 return (GetAccessFlags() & kAccSingleImplementation) != 0;
438}
439
440inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
441 DCHECK(IsUint<8>(intrinsic));
442 // Currently we only do intrinsics for static/final methods or methods of final
443 // classes. We don't set kHasSingleImplementation for those methods.
444 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
445 "Potential conflict with kAccSingleImplementation";
446 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
447 kAccIntrinsic |
448 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
449 if (kIsDebugBuild) {
450 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
451 bool is_constructor = IsConstructor();
452 bool is_synchronized = IsSynchronized();
453 bool skip_access_checks = SkipAccessChecks();
454 bool is_fast_native = IsFastNative();
455 bool is_copied = IsCopied();
456 bool is_miranda = IsMiranda();
457 bool is_default = IsDefault();
458 bool is_default_conflict = IsDefaultConflicting();
459 bool is_compilable = IsCompilable();
460 bool must_count_locks = MustCountLocks();
461 SetAccessFlags(new_value);
462 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
463 DCHECK_EQ(is_constructor, IsConstructor());
464 DCHECK_EQ(is_synchronized, IsSynchronized());
465 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
466 DCHECK_EQ(is_fast_native, IsFastNative());
467 DCHECK_EQ(is_copied, IsCopied());
468 DCHECK_EQ(is_miranda, IsMiranda());
469 DCHECK_EQ(is_default, IsDefault());
470 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
471 DCHECK_EQ(is_compilable, IsCompilable());
472 DCHECK_EQ(must_count_locks, MustCountLocks());
473 } else {
474 SetAccessFlags(new_value);
475 }
476}
477
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700478template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700479void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700480 if (LIKELY(!declaring_class_.IsNull())) {
481 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
482 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000483 if (UNLIKELY(klass->IsProxyClass())) {
484 // For normal methods, dex cache shortcuts will be visited through the declaring class.
485 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700486 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000487 GetDexCacheResolvedMethods(pointer_size),
488 GetDexMethodIndex(),
489 pointer_size);
490 DCHECK(interface_method != nullptr);
491 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700492 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
493 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000494 interface_method->VisitRoots(visitor, pointer_size);
495 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100496 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800497}
498
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800499template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800500inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700501 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800502 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800503 mirror::Class* new_class = visitor(old_class);
504 if (old_class != new_class) {
505 SetDeclaringClass(new_class);
506 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800507 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800508 ArtMethod** new_methods = visitor(old_methods);
509 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800510 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800511 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800512}
513
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800514template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700515inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800516 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800517 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800518 const void* new_native_code = visitor(old_native_code);
519 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800520 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800521 }
522 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700523 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800524 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800525 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800526 const void* new_code = visitor(old_code);
527 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800528 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800529 }
530}
531
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532} // namespace art
533
Mathieu Chartiere401d142015-04-22 13:56:20 -0700534#endif // ART_RUNTIME_ART_METHOD_INL_H_