blob: cfd7fcd0d6a29d4c8537cca27f73efc391865403 [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"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "gc_root-inl.h"
29#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010030#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/object-inl.h"
32#include "mirror/object_array.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010033#include "oat.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010034#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010036#include "runtime-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010037#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038
39namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
Mathieu Chartiere401d142015-04-22 13:56:20 -070041inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070042 GcRootSource gc_root_source(this);
43 return declaring_class_.Read(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070044}
45
Mathieu Chartiere401d142015-04-22 13:56:20 -070046inline mirror::Class* ArtMethod::GetDeclaringClassNoBarrier() {
47 return declaring_class_.Read<kWithoutReadBarrier>();
Mingyao Yang98d1cc82014-05-15 17:02:16 -070048}
49
Mathieu Chartiere401d142015-04-22 13:56:20 -070050inline mirror::Class* ArtMethod::GetDeclaringClass() {
51 mirror::Class* result = GetDeclaringClassUnchecked();
52 if (kIsDebugBuild) {
53 if (!IsRuntimeMethod()) {
54 CHECK(result != nullptr) << this;
55 CHECK(result->IsIdxLoaded() || result->IsErroneous())
56 << result->GetStatus() << " " << PrettyClass(result);
57 } else {
58 CHECK(result == nullptr) << this;
59 }
60 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 return result;
62}
63
Mathieu Chartiere401d142015-04-22 13:56:20 -070064inline void ArtMethod::SetDeclaringClass(mirror::Class* new_declaring_class) {
65 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066}
67
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070068inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
69 mirror::Class* desired_class) {
70 GcRoot<mirror::Class> expected_root(expected_class);
71 GcRoot<mirror::Class> desired_root(desired_class);
72 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
73 CompareExchangeStrongSequentiallyConsistent(
74 expected_root, desired_root);
75}
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077inline uint32_t ArtMethod::GetAccessFlags() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
79 GetDeclaringClass()->IsErroneous());
80 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081}
82
Ian Rogersef7d42f2014-01-06 12:55:46 -080083inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
85 GetDeclaringClass()->IsErroneous());
86 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087}
88
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070089inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070091}
92
Ian Rogersef7d42f2014-01-06 12:55:46 -080093inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
95 GetDeclaringClass()->IsErroneous());
96 return dex_method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097}
98
Vladimir Marko05792b92015-08-03 11:56:49 +010099inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(size_t pointer_size) {
100 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
101 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700102}
103
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index, size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100105 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
106 // without accessing the DexCache and we don't want to do that in release build.
107 DCHECK_LT(method_index,
108 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
109 ->GetDexCache()->NumResolvedMethods());
110 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
111 method_index,
112 ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113 if (LIKELY(method != nullptr)) {
114 auto* declaring_class = method->GetDeclaringClass();
115 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
116 return method;
117 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700118 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700120}
121
Vladimir Marko05792b92015-08-03 11:56:49 +0100122inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index, ArtMethod* new_method,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100124 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
125 // without accessing the DexCache and we don't want to do that in release build.
126 DCHECK_LT(method_index,
127 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
128 ->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100130 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
131 method_index,
132 new_method,
133 ptr_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700134}
135
Vladimir Marko05792b92015-08-03 11:56:49 +0100136inline bool ArtMethod::HasDexCacheResolvedMethods(size_t pointer_size) {
137 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700138}
139
Vladimir Marko05792b92015-08-03 11:56:49 +0100140inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
141 size_t pointer_size) {
142 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700143}
144
Vladimir Marko05792b92015-08-03 11:56:49 +0100145inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, size_t pointer_size) {
146 return GetDexCacheResolvedMethods(pointer_size) ==
147 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700148}
149
Vladimir Marko05792b92015-08-03 11:56:49 +0100150inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(size_t pointer_size) {
151 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
152 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700153}
154
Andreas Gampe58a5af82014-07-31 16:23:49 -0700155template <bool kWithCheck>
Vladimir Marko05792b92015-08-03 11:56:49 +0100156inline mirror::Class* ArtMethod::GetDexCacheResolvedType(uint32_t type_index, size_t ptr_size) {
157 if (kWithCheck) {
158 mirror::DexCache* dex_cache =
159 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()->GetDexCache();
160 if (UNLIKELY(type_index >= dex_cache->NumResolvedTypes())) {
161 ThrowArrayIndexOutOfBoundsException(type_index, dex_cache->NumResolvedTypes());
162 return nullptr;
163 }
164 }
165 mirror::Class* klass = GetDexCacheResolvedTypes(ptr_size)[type_index].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700166 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
167}
168
Vladimir Marko05792b92015-08-03 11:56:49 +0100169inline bool ArtMethod::HasDexCacheResolvedTypes(size_t pointer_size) {
170 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700171}
172
Vladimir Marko05792b92015-08-03 11:56:49 +0100173inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
174 size_t pointer_size) {
175 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700176}
177
Vladimir Marko05792b92015-08-03 11:56:49 +0100178inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, size_t pointer_size) {
179 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700180}
181
Vladimir Marko05792b92015-08-03 11:56:49 +0100182inline mirror::Class* ArtMethod::GetClassFromTypeIndex(uint16_t type_idx,
183 bool resolve,
184 size_t ptr_size) {
185 mirror::Class* type = GetDexCacheResolvedType(type_idx, ptr_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800186 if (type == nullptr && resolve) {
187 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
188 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
189 }
190 return type;
191}
192
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193inline uint32_t ArtMethod::GetCodeSize() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 return GetCodeSize(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
196}
197
198inline uint32_t ArtMethod::GetCodeSize(const void* code) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100199 if (code == nullptr) {
200 return 0u;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100202 return reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].code_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203}
204
Brian Carlstromea46f952013-07-30 01:26:50 -0700205inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 switch (type) {
207 case kStatic:
208 return !IsStatic();
209 case kDirect:
210 return !IsDirect() || IsStatic();
211 case kVirtual: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 return IsDirect() || (methods_class->IsInterface() && !IsMiranda());
214 }
215 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700216 // Constructors and static methods are called with invoke-direct.
217 // Interface methods cannot be invoked with invoke-super.
218 return IsConstructor() || IsStatic() || GetDeclaringClass()->IsInterface();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
222 }
223 default:
224 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700225 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 }
227}
228
Ian Rogersef7d42f2014-01-06 12:55:46 -0800229inline uint32_t ArtMethod::GetQuickOatCodeOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231 return PointerToLowMemUInt32(GetEntryPointFromQuickCompiledCode());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232}
233
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234inline void ArtMethod::SetQuickOatCodeOffset(uint32_t code_offset) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800236 SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(code_offset));
237}
238
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800239inline const uint8_t* ArtMethod::GetMappingTable(size_t pointer_size) {
240 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100241 if (code_pointer == nullptr) {
242 return nullptr;
243 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800244 return GetMappingTable(code_pointer, pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100245}
246
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800247inline const uint8_t* ArtMethod::GetMappingTable(const void* code_pointer, size_t pointer_size) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100248 DCHECK(code_pointer != nullptr);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800249 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100250 uint32_t offset =
251 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].mapping_table_offset_;
252 if (UNLIKELY(offset == 0u)) {
253 return nullptr;
254 }
255 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
256}
257
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800258inline const uint8_t* ArtMethod::GetVmapTable(size_t pointer_size) {
259 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100260 if (code_pointer == nullptr) {
261 return nullptr;
262 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800263 return GetVmapTable(code_pointer, pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100264}
265
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800266inline const uint8_t* ArtMethod::GetVmapTable(const void* code_pointer, size_t pointer_size) {
267 CHECK(!IsOptimized(pointer_size)) << "Unimplemented vmap table for optimized compiler";
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100268 DCHECK(code_pointer != nullptr);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800269 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100270 uint32_t offset =
271 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].vmap_table_offset_;
272 if (UNLIKELY(offset == 0u)) {
273 return nullptr;
274 }
275 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
276}
277
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100278inline CodeInfo ArtMethod::GetOptimizedCodeInfo() {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800279 DCHECK(IsOptimized(sizeof(void*)));
280 const void* code_pointer = GetQuickOatCodePointer(sizeof(void*));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100281 DCHECK(code_pointer != nullptr);
282 uint32_t offset =
283 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].vmap_table_offset_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 const void* data =
285 reinterpret_cast<const void*>(reinterpret_cast<const uint8_t*>(code_pointer) - offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100286 return CodeInfo(data);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100287}
288
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800289inline const uint8_t* ArtMethod::GetNativeGcMap(size_t pointer_size) {
290 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
291 if (code_pointer == nullptr) {
292 return nullptr;
293 }
294 return GetNativeGcMap(code_pointer, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295}
296
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800297inline const uint8_t* ArtMethod::GetNativeGcMap(const void* code_pointer, size_t pointer_size) {
298 DCHECK(code_pointer != nullptr);
299 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
300 uint32_t offset =
301 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].gc_map_offset_;
302 if (UNLIKELY(offset == 0u)) {
303 return nullptr;
304 }
305 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306}
307
Ian Rogersef7d42f2014-01-06 12:55:46 -0800308inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310}
311
Ian Rogersef7d42f2014-01-06 12:55:46 -0800312inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 if (!IsRuntimeMethod()) {
314 return false;
315 }
316 Runtime* runtime = Runtime::Current();
317 bool result = false;
318 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
319 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
320 result = true;
321 break;
322 }
323 }
324 return result;
325}
326
Ian Rogersef7d42f2014-01-06 12:55:46 -0800327inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 bool result = this == Runtime::Current()->GetResolutionMethod();
329 // Check that if we do think it is phony it looks like the resolution method.
330 DCHECK(!result || IsRuntimeMethod());
331 return result;
332}
Jeff Hao88474b42013-10-23 16:24:40 -0700333
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334inline bool ArtMethod::IsImtConflictMethod() {
Jeff Hao88474b42013-10-23 16:24:40 -0700335 bool result = this == Runtime::Current()->GetImtConflictMethod();
336 // Check that if we do think it is phony it looks like the imt conflict method.
337 DCHECK(!result || IsRuntimeMethod());
338 return result;
339}
Mathieu Chartier4e305412014-02-19 10:54:44 -0800340
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700341inline bool ArtMethod::IsImtUnimplementedMethod() {
342 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
343 // Check that if we do think it is phony it looks like the imt unimplemented method.
344 DCHECK(!result || IsRuntimeMethod());
345 return result;
346}
347
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700348inline uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800349 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(
350 this, sizeof(void*));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100351 return pc - reinterpret_cast<uintptr_t>(code);
352}
353
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100354inline QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo(const void* code_pointer) {
355 DCHECK(code_pointer != nullptr);
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -0700356 if (kIsDebugBuild && !IsProxyMethod()) {
357 CHECK_EQ(code_pointer, GetQuickOatCodePointer(sizeof(void*)));
358 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100359 return reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].frame_info_;
360}
361
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700363 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700364}
365
366inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
369 return "<runtime method>";
370 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 DCHECK(!IsProxyMethod());
372 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700373 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
374}
375
376inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 DCHECK(!IsProxyMethod());
378 const DexFile* dex_file = GetDexFile();
379 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700380}
381
382inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700384 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 DCHECK(!IsProxyMethod());
386 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700387 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
388 }
389 return Signature::NoSignature();
390}
391
Ian Rogers1ff3c982014-08-12 02:30:58 -0700392inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700394 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 DCHECK(!IsProxyMethod());
396 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700397 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
398 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 Runtime* const runtime = Runtime::Current();
400 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700401 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700403 return "<runtime internal imt conflict method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405 return "<runtime internal callee-save all registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700407 return "<runtime internal callee-save reference registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700409 return "<runtime internal callee-save reference and argument registers method>";
410 } else {
411 return "<unknown runtime internal method>";
412 }
413}
414
415inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Mathieu Chartier12b3dd72014-12-11 13:25:33 -0800416 return GetDeclaringClass()->GetDexFile().GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700417}
418
Vladimir Marko05792b92015-08-03 11:56:49 +0100419inline bool ArtMethod::IsResolvedTypeIdx(uint16_t type_idx, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 DCHECK(!IsProxyMethod());
Vladimir Marko05792b92015-08-03 11:56:49 +0100421 return GetDexCacheResolvedType(type_idx, ptr_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700422}
423
424inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700426 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700428 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 return GetDexFile()->GetLineNumFromPC(this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700430}
431
432inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 DCHECK(!IsProxyMethod());
434 const DexFile* dex_file = GetDexFile();
435 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436}
437
438inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 DCHECK(!IsProxyMethod());
440 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700441 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700443 return dex_file->GetProtoParameters(proto);
444}
445
446inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 DCHECK(!IsProxyMethod());
448 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700449}
450
451inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 DCHECK(!IsProxyMethod());
453 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700454}
455
456inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 DCHECK(!IsProxyMethod());
458 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700459}
460
461inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700462 DCHECK(!IsProxyMethod());
463 const DexFile* dex_file = GetDexFile();
464 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
466 uint16_t return_type_idx = proto_id.return_type_idx_;
467 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(return_type_idx));
468}
469
470inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 DCHECK(!IsProxyMethod());
472 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700473 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
474}
475
476inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477 DCHECK(!IsProxyMethod());
478 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479}
480
481inline mirror::DexCache* ArtMethod::GetDexCache() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 DCHECK(!IsProxyMethod());
483 return GetDeclaringClass()->GetDexCache();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700484}
485
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700486inline bool ArtMethod::IsProxyMethod() {
487 return GetDeclaringClass()->IsProxyClass();
488}
489
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(size_t pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700491 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700492 return this;
493 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700494 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100495 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
496 GetDexCacheResolvedMethods(pointer_size),
497 GetDexMethodIndex(),
498 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 DCHECK(interface_method != nullptr);
500 DCHECK_EQ(interface_method,
501 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
502 return interface_method;
503}
504
Vladimir Marko05792b92015-08-03 11:56:49 +0100505inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
506 size_t ptr_size) {
507 SetNativePointer(DexCacheResolvedMethodsOffset(ptr_size), new_dex_cache_methods, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700508}
509
Vladimir Marko05792b92015-08-03 11:56:49 +0100510inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
511 size_t ptr_size) {
512 SetNativePointer(DexCacheResolvedTypesOffset(ptr_size), new_dex_cache_types, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700513}
514
Vladimir Marko05792b92015-08-03 11:56:49 +0100515inline mirror::Class* ArtMethod::GetReturnType(bool resolve, size_t ptr_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700516 DCHECK(!IsProxyMethod());
517 const DexFile* dex_file = GetDexFile();
518 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
519 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
520 uint16_t return_type_idx = proto_id.return_type_idx_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100521 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, ptr_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700522 if (type == nullptr && resolve) {
523 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
524 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
525 }
526 return type;
527}
528
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529template<typename RootVisitorType>
530void ArtMethod::VisitRoots(RootVisitorType& visitor) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100531 ArtMethod* interface_method = nullptr;
532 mirror::Class* klass = declaring_class_.Read();
533 if (UNLIKELY(klass != nullptr && klass->IsProxyClass())) {
534 // For normal methods, dex cache shortcuts will be visited through the declaring class.
535 // However, for proxies we need to keep the interface method alive, so we visit its roots.
536 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
537 interface_method = mirror::DexCache::GetElementPtrSize(
538 GetDexCacheResolvedMethods(pointer_size),
539 GetDexMethodIndex(),
540 pointer_size);
541 DCHECK(interface_method != nullptr);
542 DCHECK_EQ(interface_method,
543 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
544 interface_method->VisitRoots(visitor);
545 }
546
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700547 visitor.VisitRootIfNonNull(declaring_class_.AddressWithoutBarrier());
Mathieu Chartier2d721012014-11-10 11:08:06 -0800548}
549
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550inline void ArtMethod::CopyFrom(const ArtMethod* src, size_t image_pointer_size) {
551 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
Vladimir Marko14632852015-08-17 12:07:23 +0100552 Size(image_pointer_size));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554}
555
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556} // namespace art
557
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558#endif // ART_RUNTIME_ART_METHOD_INL_H_