blob: 685e26c78d601dc22dc57615cfe081d8589c72c3 [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;
Andreas Gampeb1106e22017-02-23 11:34:48 -080058 if (kCheckDeclaringClassState) {
59 CHECK(result->IsIdxLoaded() || result->IsErroneous())
60 << result->GetStatus() << " " << result->PrettyClass();
61 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 } else {
63 CHECK(result == nullptr) << this;
64 }
65 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 return result;
67}
68
Mathieu Chartier28357fa2016-10-18 16:27:40 -070069inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070073inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
74 mirror::Class* desired_class) {
75 GcRoot<mirror::Class> expected_root(expected_class);
76 GcRoot<mirror::Class> desired_root(desired_class);
77 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
78 CompareExchangeStrongSequentiallyConsistent(
79 expected_root, desired_root);
80}
81
Andreas Gampecbc96b82015-09-30 20:05:24 +000082// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
83// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080084template <ReadBarrierOption kReadBarrierOption>
85ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
86 NO_THREAD_SAFETY_ANALYSIS {
87 CHECK(method->IsRuntimeMethod() ||
88 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
89 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
Andreas Gampecbc96b82015-09-30 20:05:24 +000090}
91
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080092template <ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080093inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampeb1106e22017-02-23 11:34:48 -080094 if (kCheckDeclaringClassState) {
Andreas Gampecbc96b82015-09-30 20:05:24 +000095 Thread* self = Thread::Current();
96 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Mathieu Chartier10b218d2016-07-25 17:48:52 -070097 if (self->IsThreadSuspensionAllowable()) {
98 ScopedObjectAccess soa(self);
99 CHECK(IsRuntimeMethod() ||
100 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
101 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
102 }
Andreas Gampecbc96b82015-09-30 20:05:24 +0000103 } else {
104 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
105 // runnable state (e.g., during GC).
106 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800107 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
Andreas Gampecbc96b82015-09-30 20:05:24 +0000108 }
109 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700110 return access_flags_.load(std::memory_order_relaxed);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111}
112
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000114 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116}
117
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700118inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700120}
121
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122inline uint32_t ArtMethod::GetDexMethodIndex() {
Andreas Gampeb1106e22017-02-23 11:34:48 -0800123 if (kCheckDeclaringClassState) {
124 CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
125 GetDeclaringClass()->IsErroneous());
126 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000127 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128}
129
Andreas Gampe542451c2016-07-26 09:02:02 -0700130inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100131 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
132 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700133}
134
Andreas Gampe542451c2016-07-26 09:02:02 -0700135inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
136 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
138 // without accessing the DexCache and we don't want to do that in release build.
139 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800140 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700141 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700143 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 if (LIKELY(method != nullptr)) {
145 auto* declaring_class = method->GetDeclaringClass();
146 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
147 return method;
148 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700149 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700151}
152
Andreas Gampe542451c2016-07-26 09:02:02 -0700153inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
154 ArtMethod* new_method,
155 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100156 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
157 // without accessing the DexCache and we don't want to do that in release build.
158 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800159 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700161 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100162 method_index,
163 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700164 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700165}
166
Andreas Gampe542451c2016-07-26 09:02:02 -0700167inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100168 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700169}
170
Vladimir Marko05792b92015-08-03 11:56:49 +0100171inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700172 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100173 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700174}
175
Andreas Gampe542451c2016-07-26 09:02:02 -0700176inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100177 return GetDexCacheResolvedMethods(pointer_size) ==
178 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700179}
180
Vladimir Marko942fd312017-01-16 20:52:19 +0000181inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx, bool resolve) {
Vladimir Markobfb80d22017-02-14 14:08:12 +0000182 // TODO: Refactor this function into two functions, Resolve...() and Lookup...()
183 // so that we can properly annotate it with no-suspension possible / suspension possible.
Vladimir Marko942fd312017-01-16 20:52:19 +0000184 ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
185 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
Vladimir Markobfb80d22017-02-14 14:08:12 +0000186 if (UNLIKELY(type == nullptr)) {
Vladimir Marko942fd312017-01-16 20:52:19 +0000187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markobfb80d22017-02-14 14:08:12 +0000188 if (resolve) {
189 type = class_linker->ResolveType(type_idx, this);
190 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
191 } else {
192 type = class_linker->LookupResolvedType(
193 *dex_cache->GetDexFile(), type_idx, dex_cache, GetClassLoader());
194 }
Ian Rogersa0485602014-12-02 15:48:04 -0800195 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000196 return type.Ptr();
Ian Rogersa0485602014-12-02 15:48:04 -0800197}
198
Brian Carlstromea46f952013-07-30 01:26:50 -0700199inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 switch (type) {
201 case kStatic:
202 return !IsStatic();
203 case kDirect:
204 return !IsDirect() || IsStatic();
205 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700206 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
207 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700209 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 }
211 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700212 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700213 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
217 }
218 default:
219 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700220 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 }
222}
223
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226}
227
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 if (!IsRuntimeMethod()) {
230 return false;
231 }
232 Runtime* runtime = Runtime::Current();
233 bool result = false;
234 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
235 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
236 result = true;
237 break;
238 }
239 }
240 return result;
241}
242
Ian Rogersef7d42f2014-01-06 12:55:46 -0800243inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 bool result = this == Runtime::Current()->GetResolutionMethod();
245 // Check that if we do think it is phony it looks like the resolution method.
246 DCHECK(!result || IsRuntimeMethod());
247 return result;
248}
Jeff Hao88474b42013-10-23 16:24:40 -0700249
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700250inline bool ArtMethod::IsImtUnimplementedMethod() {
251 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
252 // Check that if we do think it is phony it looks like the imt unimplemented method.
253 DCHECK(!result || IsRuntimeMethod());
254 return result;
255}
256
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700257inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800258 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
259 // from-space dex file pointer it will be equal to the to-space copy.
260 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700261}
262
263inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
266 return "<runtime method>";
267 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 DCHECK(!IsProxyMethod());
269 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700270 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
271}
272
273inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000274 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 const DexFile* dex_file = GetDexFile();
276 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700277}
278
279inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700281 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 DCHECK(!IsProxyMethod());
283 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700284 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
285 }
286 return Signature::NoSignature();
287}
288
Ian Rogers1ff3c982014-08-12 02:30:58 -0700289inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700291 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 DCHECK(!IsProxyMethod());
293 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700294 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
295 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 Runtime* const runtime = Runtime::Current();
297 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700298 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700300 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100301 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700302 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100303 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700304 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100305 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306 return "<runtime internal callee-save reference and argument registers method>";
307 } else {
308 return "<unknown runtime internal method>";
309 }
310}
311
312inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800313 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700314}
315
Vladimir Marko942fd312017-01-16 20:52:19 +0000316inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 DCHECK(!IsProxyMethod());
Vladimir Marko942fd312017-01-16 20:52:19 +0000318 return GetClassFromTypeIndex(type_idx, /* resolve */ false) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700319}
320
321inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700323 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700325 }
David Sehr9323e6e2016-09-13 08:58:35 -0700326 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700327}
328
329inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 DCHECK(!IsProxyMethod());
331 const DexFile* dex_file = GetDexFile();
332 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700333}
334
335inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000336 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000338 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
339 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340 return dex_file->GetProtoParameters(proto);
341}
342
343inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 DCHECK(!IsProxyMethod());
345 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700346}
347
348inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 DCHECK(!IsProxyMethod());
350 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700351}
352
353inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354 DCHECK(!IsProxyMethod());
355 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700356}
357
358inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 DCHECK(!IsProxyMethod());
360 const DexFile* dex_file = GetDexFile();
361 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800363 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700364}
365
Andreas Gampea5b09a62016-11-17 15:21:22 -0800366inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367 DCHECK(!IsProxyMethod());
368 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700369 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
370}
371
372inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 DCHECK(!IsProxyMethod());
374 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700375}
376
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800377template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700378inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800379 if (LIKELY(!IsObsolete())) {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800380 mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
381 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800382 } else {
383 DCHECK(!IsProxyMethod());
384 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800385 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386}
387
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700388inline bool ArtMethod::IsProxyMethod() {
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800389 // Avoid read barrier since the from-space version of the class will have the correct proxy class
390 // flags since they are constant for the lifetime of the class.
391 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700392}
393
Andreas Gampe542451c2016-07-26 09:02:02 -0700394inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700395 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396 return this;
397 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100398 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
399 GetDexCacheResolvedMethods(pointer_size),
400 GetDexMethodIndex(),
401 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700402 DCHECK(interface_method != nullptr);
403 DCHECK_EQ(interface_method,
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800404 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405 return interface_method;
406}
407
Vladimir Marko05792b92015-08-03 11:56:49 +0100408inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700409 PointerSize pointer_size) {
410 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
411 new_dex_cache_methods,
412 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700413}
414
Vladimir Marko942fd312017-01-16 20:52:19 +0000415inline mirror::Class* ArtMethod::GetReturnType(bool resolve) {
Ian Rogersded66a02014-10-28 18:12:55 -0700416 DCHECK(!IsProxyMethod());
417 const DexFile* dex_file = GetDexFile();
418 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
419 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800420 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000421 return GetClassFromTypeIndex(return_type_idx, resolve);
Ian Rogersded66a02014-10-28 18:12:55 -0700422}
423
Mingyao Yang063fc772016-08-02 11:02:54 -0700424inline bool ArtMethod::HasSingleImplementation() {
425 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
426 // We don't set kAccSingleImplementation for these cases since intrinsic
427 // can use the flag also.
428 return true;
429 }
430 return (GetAccessFlags() & kAccSingleImplementation) != 0;
431}
432
433inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
434 DCHECK(IsUint<8>(intrinsic));
435 // Currently we only do intrinsics for static/final methods or methods of final
436 // classes. We don't set kHasSingleImplementation for those methods.
437 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
438 "Potential conflict with kAccSingleImplementation";
439 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
440 kAccIntrinsic |
441 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
442 if (kIsDebugBuild) {
443 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
444 bool is_constructor = IsConstructor();
445 bool is_synchronized = IsSynchronized();
446 bool skip_access_checks = SkipAccessChecks();
447 bool is_fast_native = IsFastNative();
448 bool is_copied = IsCopied();
449 bool is_miranda = IsMiranda();
450 bool is_default = IsDefault();
451 bool is_default_conflict = IsDefaultConflicting();
452 bool is_compilable = IsCompilable();
453 bool must_count_locks = MustCountLocks();
454 SetAccessFlags(new_value);
455 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
456 DCHECK_EQ(is_constructor, IsConstructor());
457 DCHECK_EQ(is_synchronized, IsSynchronized());
458 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
459 DCHECK_EQ(is_fast_native, IsFastNative());
460 DCHECK_EQ(is_copied, IsCopied());
461 DCHECK_EQ(is_miranda, IsMiranda());
462 DCHECK_EQ(is_default, IsDefault());
463 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
464 DCHECK_EQ(is_compilable, IsCompilable());
465 DCHECK_EQ(must_count_locks, MustCountLocks());
466 } else {
467 SetAccessFlags(new_value);
468 }
469}
470
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700471template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700472void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700473 if (LIKELY(!declaring_class_.IsNull())) {
474 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
475 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000476 if (UNLIKELY(klass->IsProxyClass())) {
477 // For normal methods, dex cache shortcuts will be visited through the declaring class.
478 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700479 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000480 GetDexCacheResolvedMethods(pointer_size),
481 GetDexMethodIndex(),
482 pointer_size);
483 DCHECK(interface_method != nullptr);
484 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700485 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
486 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000487 interface_method->VisitRoots(visitor, pointer_size);
488 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100489 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800490}
491
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800492template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800493inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700494 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800495 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800496 mirror::Class* new_class = visitor(old_class);
497 if (old_class != new_class) {
498 SetDeclaringClass(new_class);
499 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800500 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800501 ArtMethod** new_methods = visitor(old_methods);
502 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800503 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800504 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800505}
506
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800507template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700508inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800509 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800510 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800511 const void* new_native_code = visitor(old_native_code);
512 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800513 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800514 }
515 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700516 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800517 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800518 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800519 const void* new_code = visitor(old_code);
520 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800521 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800522 }
523}
524
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525} // namespace art
526
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527#endif // ART_RUNTIME_ART_METHOD_INL_H_