blob: 950f1aa9f45b4eeba7f14f4070d570534b9e033d [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() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000112 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114}
115
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700116inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
122 GetDeclaringClass()->IsErroneous());
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000123 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124}
125
Andreas Gampe542451c2016-07-26 09:02:02 -0700126inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100127 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
128 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700129}
130
Andreas Gampe542451c2016-07-26 09:02:02 -0700131inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
132 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100133 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
134 // without accessing the DexCache and we don't want to do that in release build.
135 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800136 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700137 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700139 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 if (LIKELY(method != nullptr)) {
141 auto* declaring_class = method->GetDeclaringClass();
142 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
143 return method;
144 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700145 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700147}
148
Andreas Gampe542451c2016-07-26 09:02:02 -0700149inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
150 ArtMethod* new_method,
151 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100152 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
153 // without accessing the DexCache and we don't want to do that in release build.
154 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800155 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700157 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100158 method_index,
159 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700160 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700161}
162
Andreas Gampe542451c2016-07-26 09:02:02 -0700163inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100164 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700165}
166
Vladimir Marko05792b92015-08-03 11:56:49 +0100167inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700168 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100169 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700170}
171
Andreas Gampe542451c2016-07-26 09:02:02 -0700172inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100173 return GetDexCacheResolvedMethods(pointer_size) ==
174 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700175}
176
Vladimir Marko942fd312017-01-16 20:52:19 +0000177inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx, bool resolve) {
178 ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
179 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
Vladimir Markod16363a2017-02-01 14:09:13 +0000180 if (UNLIKELY(type == nullptr) && resolve) {
Vladimir Marko942fd312017-01-16 20:52:19 +0000181 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markod16363a2017-02-01 14:09:13 +0000182 type = class_linker->ResolveType(type_idx, this);
183 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersa0485602014-12-02 15:48:04 -0800184 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000185 return type.Ptr();
Ian Rogersa0485602014-12-02 15:48:04 -0800186}
187
Brian Carlstromea46f952013-07-30 01:26:50 -0700188inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 switch (type) {
190 case kStatic:
191 return !IsStatic();
192 case kDirect:
193 return !IsDirect() || IsStatic();
194 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700195 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
196 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700198 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 }
200 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700201 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700202 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
206 }
207 default:
208 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700209 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 }
211}
212
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215}
216
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 if (!IsRuntimeMethod()) {
219 return false;
220 }
221 Runtime* runtime = Runtime::Current();
222 bool result = false;
223 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
224 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
225 result = true;
226 break;
227 }
228 }
229 return result;
230}
231
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 bool result = this == Runtime::Current()->GetResolutionMethod();
234 // Check that if we do think it is phony it looks like the resolution method.
235 DCHECK(!result || IsRuntimeMethod());
236 return result;
237}
Jeff Hao88474b42013-10-23 16:24:40 -0700238
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700239inline bool ArtMethod::IsImtUnimplementedMethod() {
240 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
241 // Check that if we do think it is phony it looks like the imt unimplemented method.
242 DCHECK(!result || IsRuntimeMethod());
243 return result;
244}
245
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700246inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800247 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
248 // from-space dex file pointer it will be equal to the to-space copy.
249 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700250}
251
252inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700254 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
255 return "<runtime method>";
256 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 DCHECK(!IsProxyMethod());
258 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700259 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
260}
261
262inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000263 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 const DexFile* dex_file = GetDexFile();
265 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700266}
267
268inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700270 if (dex_method_idx != DexFile::kDexNoIndex) {
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->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
274 }
275 return Signature::NoSignature();
276}
277
Ian Rogers1ff3c982014-08-12 02:30:58 -0700278inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 DCHECK(!IsProxyMethod());
282 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
284 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 Runtime* const runtime = Runtime::Current();
286 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700287 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700289 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100290 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700291 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100292 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700293 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100294 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700295 return "<runtime internal callee-save reference and argument registers method>";
296 } else {
297 return "<unknown runtime internal method>";
298 }
299}
300
301inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800302 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700303}
304
Vladimir Marko942fd312017-01-16 20:52:19 +0000305inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 DCHECK(!IsProxyMethod());
Vladimir Marko942fd312017-01-16 20:52:19 +0000307 return GetClassFromTypeIndex(type_idx, /* resolve */ false) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700308}
309
310inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700312 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700314 }
David Sehr9323e6e2016-09-13 08:58:35 -0700315 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700316}
317
318inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 DCHECK(!IsProxyMethod());
320 const DexFile* dex_file = GetDexFile();
321 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700322}
323
324inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000325 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000327 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
328 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700329 return dex_file->GetProtoParameters(proto);
330}
331
332inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK(!IsProxyMethod());
334 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700335}
336
337inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 DCHECK(!IsProxyMethod());
339 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340}
341
342inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 DCHECK(!IsProxyMethod());
344 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345}
346
347inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 DCHECK(!IsProxyMethod());
349 const DexFile* dex_file = GetDexFile();
350 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700351 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800352 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700353}
354
Andreas Gampea5b09a62016-11-17 15:21:22 -0800355inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356 DCHECK(!IsProxyMethod());
357 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700358 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
359}
360
361inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700362 DCHECK(!IsProxyMethod());
363 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700364}
365
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800366template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700367inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800368 if (LIKELY(!IsObsolete())) {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800369 mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
370 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800371 } else {
372 DCHECK(!IsProxyMethod());
373 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800374 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700375}
376
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700377inline bool ArtMethod::IsProxyMethod() {
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800378 // Avoid read barrier since the from-space version of the class will have the correct proxy class
379 // flags since they are constant for the lifetime of the class.
380 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700381}
382
Andreas Gampe542451c2016-07-26 09:02:02 -0700383inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700384 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700385 return this;
386 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100387 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
388 GetDexCacheResolvedMethods(pointer_size),
389 GetDexMethodIndex(),
390 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700391 DCHECK(interface_method != nullptr);
392 DCHECK_EQ(interface_method,
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800393 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700394 return interface_method;
395}
396
Vladimir Marko05792b92015-08-03 11:56:49 +0100397inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700398 PointerSize pointer_size) {
399 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
400 new_dex_cache_methods,
401 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700402}
403
Vladimir Marko942fd312017-01-16 20:52:19 +0000404inline mirror::Class* ArtMethod::GetReturnType(bool resolve) {
Ian Rogersded66a02014-10-28 18:12:55 -0700405 DCHECK(!IsProxyMethod());
406 const DexFile* dex_file = GetDexFile();
407 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
408 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800409 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000410 return GetClassFromTypeIndex(return_type_idx, resolve);
Ian Rogersded66a02014-10-28 18:12:55 -0700411}
412
Mingyao Yang063fc772016-08-02 11:02:54 -0700413inline bool ArtMethod::HasSingleImplementation() {
414 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
415 // We don't set kAccSingleImplementation for these cases since intrinsic
416 // can use the flag also.
417 return true;
418 }
419 return (GetAccessFlags() & kAccSingleImplementation) != 0;
420}
421
422inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
423 DCHECK(IsUint<8>(intrinsic));
424 // Currently we only do intrinsics for static/final methods or methods of final
425 // classes. We don't set kHasSingleImplementation for those methods.
426 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
427 "Potential conflict with kAccSingleImplementation";
428 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
429 kAccIntrinsic |
430 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
431 if (kIsDebugBuild) {
432 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
433 bool is_constructor = IsConstructor();
434 bool is_synchronized = IsSynchronized();
435 bool skip_access_checks = SkipAccessChecks();
436 bool is_fast_native = IsFastNative();
437 bool is_copied = IsCopied();
438 bool is_miranda = IsMiranda();
439 bool is_default = IsDefault();
440 bool is_default_conflict = IsDefaultConflicting();
441 bool is_compilable = IsCompilable();
442 bool must_count_locks = MustCountLocks();
443 SetAccessFlags(new_value);
444 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
445 DCHECK_EQ(is_constructor, IsConstructor());
446 DCHECK_EQ(is_synchronized, IsSynchronized());
447 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
448 DCHECK_EQ(is_fast_native, IsFastNative());
449 DCHECK_EQ(is_copied, IsCopied());
450 DCHECK_EQ(is_miranda, IsMiranda());
451 DCHECK_EQ(is_default, IsDefault());
452 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
453 DCHECK_EQ(is_compilable, IsCompilable());
454 DCHECK_EQ(must_count_locks, MustCountLocks());
455 } else {
456 SetAccessFlags(new_value);
457 }
458}
459
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700460template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700461void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700462 if (LIKELY(!declaring_class_.IsNull())) {
463 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
464 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000465 if (UNLIKELY(klass->IsProxyClass())) {
466 // For normal methods, dex cache shortcuts will be visited through the declaring class.
467 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700468 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000469 GetDexCacheResolvedMethods(pointer_size),
470 GetDexMethodIndex(),
471 pointer_size);
472 DCHECK(interface_method != nullptr);
473 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700474 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
475 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000476 interface_method->VisitRoots(visitor, pointer_size);
477 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100478 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800479}
480
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800481template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800482inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700483 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800484 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800485 mirror::Class* new_class = visitor(old_class);
486 if (old_class != new_class) {
487 SetDeclaringClass(new_class);
488 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800489 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800490 ArtMethod** new_methods = visitor(old_methods);
491 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800492 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800493 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800494}
495
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800496template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700497inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800498 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800499 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800500 const void* new_native_code = visitor(old_native_code);
501 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800502 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800503 }
504 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700505 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800506 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800507 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800508 const void* new_code = visitor(old_code);
509 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800510 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800511 }
512}
513
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514} // namespace art
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516#endif // ART_RUNTIME_ART_METHOD_INL_H_