blob: ac22f07a34b0a8d616e97ab0fac45ad67b447ec2 [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"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070025#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010026#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/code_item_accessors-inl.h"
28#include "dex/dex_file-inl.h"
29#include "dex/dex_file_annotations.h"
30#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080031#include "dex/invoke_type.h"
David Sehr67bf42e2018-02-26 16:43:04 -080032#include "dex/primitive.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "gc_root-inl.h"
David Brazdil87144352018-01-24 12:50:01 +000034#include "intrinsics_enum.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010035#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010037#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070038#include "mirror/object-inl.h"
39#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000040#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010041#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070042#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010043#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070044#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010045#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070047#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
49namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080051template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010052inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070053 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080054 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070055}
56
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080057template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010058inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
59 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 if (kIsDebugBuild) {
61 if (!IsRuntimeMethod()) {
62 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 } else {
64 CHECK(result == nullptr) << this;
65 }
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 return result;
68}
69
Mathieu Chartier28357fa2016-10-18 16:27:40 -070070inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Vladimir Markod93e3742018-07-18 10:58:13 +010074inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
75 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070076 GcRoot<mirror::Class> expected_root(expected_class);
77 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +000078 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
79 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070080}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000083 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070087inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070089}
90
Vladimir Markob45528c2017-07-27 14:14:28 +010091inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +000092 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +000093 ObjPtr<mirror::Class> type =
94 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
95 DCHECK(!Thread::Current()->IsExceptionPending());
96 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +010097}
98
99inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000100 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
101 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
102 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800103}
104
Brian Carlstromea46f952013-07-30 01:26:50 -0700105inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 switch (type) {
107 case kStatic:
108 return !IsStatic();
109 case kDirect:
110 return !IsDirect() || IsStatic();
111 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700112 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
113 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100114 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700115 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700118 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700119 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100121 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
123 }
124 default:
125 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700126 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128}
129
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 if (!IsRuntimeMethod()) {
132 return false;
133 }
134 Runtime* runtime = Runtime::Current();
135 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700136 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
137 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 result = true;
139 break;
140 }
141 }
142 return result;
143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 bool result = this == Runtime::Current()->GetResolutionMethod();
147 // Check that if we do think it is phony it looks like the resolution method.
148 DCHECK(!result || IsRuntimeMethod());
149 return result;
150}
Jeff Hao88474b42013-10-23 16:24:40 -0700151
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700152inline bool ArtMethod::IsImtUnimplementedMethod() {
153 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
154 // Check that if we do think it is phony it looks like the imt unimplemented method.
155 DCHECK(!result || IsRuntimeMethod());
156 return result;
157}
158
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700159inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800160 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
161 // from-space dex file pointer it will be equal to the to-space copy.
162 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700163}
164
165inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700167 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700168 return "<runtime method>";
169 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 DCHECK(!IsProxyMethod());
171 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700172 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
173}
174
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800175inline const char* ArtMethod::GetShorty() {
176 uint32_t unused_length;
177 return GetShorty(&unused_length);
178}
179
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700180inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000181 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100183 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700184}
185
186inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700188 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 DCHECK(!IsProxyMethod());
190 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700191 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
192 }
193 return Signature::NoSignature();
194}
195
Ian Rogers1ff3c982014-08-12 02:30:58 -0700196inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700198 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 DCHECK(!IsProxyMethod());
200 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700201 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
202 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 Runtime* const runtime = Runtime::Current();
204 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700205 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700207 return "<runtime internal imt conflict method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700208 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700209 return "<runtime internal callee-save all registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700210 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700211 return "<runtime internal callee-save reference registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700212 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700213 return "<runtime internal callee-save reference and argument registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700214 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
215 return "<runtime internal save-every-register method>";
Mingyao Yang0a87a652017-04-12 13:43:15 -0700216 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
217 return "<runtime internal save-every-register method for clinit>";
218 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
219 return "<runtime internal save-every-register method for suspend check>";
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700220 } else {
221 return "<unknown runtime internal method>";
222 }
223}
224
Vladimir Marko18090d12018-06-01 16:53:12 +0100225inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
226 DCHECK(!IsProxyMethod());
227 const DexFile::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
228 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
229}
230
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700231inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800232 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700233}
234
Vladimir Marko942fd312017-01-16 20:52:19 +0000235inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100237 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700238}
239
240inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700242 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700244 }
David Sehr9323e6e2016-09-13 08:58:35 -0700245 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700246}
247
248inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 DCHECK(!IsProxyMethod());
250 const DexFile* dex_file = GetDexFile();
251 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252}
253
254inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000255 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000257 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
258 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700259 return dex_file->GetProtoParameters(proto);
260}
261
262inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 DCHECK(!IsProxyMethod());
264 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265}
266
267inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000269 if (LIKELY(!IsObsolete())) {
270 return GetDeclaringClass()->GetDexClassDefIndex();
271 } else {
272 return FindObsoleteDexClassDefIndex();
273 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700274}
275
276inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 DCHECK(!IsProxyMethod());
278 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279}
280
Orion Hodson58143d22018-02-20 08:44:20 +0000281inline size_t ArtMethod::GetNumberOfParameters() {
282 constexpr size_t return_type_count = 1u;
283 return strlen(GetShorty()) - return_type_count;
284}
285
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700286inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 DCHECK(!IsProxyMethod());
288 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000289 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700290}
291
Alex Lightd7661582017-05-01 13:48:16 -0700292inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
293 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
294}
295
Andreas Gampea5b09a62016-11-17 15:21:22 -0800296inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 DCHECK(!IsProxyMethod());
298 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700299 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
300}
301
302inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 DCHECK(!IsProxyMethod());
304 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305}
306
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800307template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700308inline mirror::DexCache* ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100309 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100310 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800311 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800312 } else {
313 DCHECK(!IsProxyMethod());
314 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800315 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700316}
317
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700318inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000319 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800320 // Avoid read barrier since the from-space version of the class will have the correct proxy class
321 // flags since they are constant for the lifetime of the class.
322 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700323}
324
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000325inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
326 DCHECK(IsProxyMethod());
327 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
328 // which may give false negatives while visiting references for a non-CC moving GC.
329 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
330}
331
Andreas Gampe542451c2016-07-26 09:02:02 -0700332inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700333 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700334 return this;
335 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000336 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
337 // We can check that the proxy class implements the interface only if the proxy class
338 // is resolved, otherwise the interface table is not yet initialized.
339 DCHECK(!GetDeclaringClass()->IsResolved() ||
340 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700341 return interface_method;
342}
343
Vladimir Markob45528c2017-07-27 14:14:28 +0100344inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700345 DCHECK(!IsProxyMethod());
346 const DexFile* dex_file = GetDexFile();
347 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
348 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100349 return proto_id.return_type_idx_;
350}
351
352inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
353 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
354}
355
356inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
357 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700358}
359
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300360template <ReadBarrierOption kReadBarrierOption>
Mingyao Yang063fc772016-08-02 11:02:54 -0700361inline bool ArtMethod::HasSingleImplementation() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100362 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700363 // We don't set kAccSingleImplementation for these cases since intrinsic
364 // can use the flag also.
365 return true;
366 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100367 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700368}
369
David Brazdil166546c2018-04-23 13:50:38 +0100370inline HiddenApiAccessFlags::ApiList ArtMethod::GetHiddenApiAccessFlags()
371 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil2e6f69c2018-04-19 12:41:04 +0100372 if (UNLIKELY(IsIntrinsic())) {
373 switch (static_cast<Intrinsics>(GetIntrinsic())) {
374 case Intrinsics::kSystemArrayCopyChar:
375 case Intrinsics::kStringGetCharsNoCheck:
376 case Intrinsics::kReferenceGetReferent:
377 // These intrinsics are on the light greylist and will fail a DCHECK in
378 // SetIntrinsic() if their flags change on the respective dex methods.
379 // Note that the DCHECK currently won't fail if the dex methods are
380 // whitelisted, e.g. in the core image (b/77733081). As a result, we
381 // might print warnings but we won't change the semantics.
382 return HiddenApiAccessFlags::kLightGreylist;
383 case Intrinsics::kVarHandleFullFence:
384 case Intrinsics::kVarHandleAcquireFence:
385 case Intrinsics::kVarHandleReleaseFence:
386 case Intrinsics::kVarHandleLoadLoadFence:
387 case Intrinsics::kVarHandleStoreStoreFence:
388 case Intrinsics::kVarHandleCompareAndExchange:
389 case Intrinsics::kVarHandleCompareAndExchangeAcquire:
390 case Intrinsics::kVarHandleCompareAndExchangeRelease:
391 case Intrinsics::kVarHandleCompareAndSet:
392 case Intrinsics::kVarHandleGet:
393 case Intrinsics::kVarHandleGetAcquire:
394 case Intrinsics::kVarHandleGetAndAdd:
395 case Intrinsics::kVarHandleGetAndAddAcquire:
396 case Intrinsics::kVarHandleGetAndAddRelease:
397 case Intrinsics::kVarHandleGetAndBitwiseAnd:
398 case Intrinsics::kVarHandleGetAndBitwiseAndAcquire:
399 case Intrinsics::kVarHandleGetAndBitwiseAndRelease:
400 case Intrinsics::kVarHandleGetAndBitwiseOr:
401 case Intrinsics::kVarHandleGetAndBitwiseOrAcquire:
402 case Intrinsics::kVarHandleGetAndBitwiseOrRelease:
403 case Intrinsics::kVarHandleGetAndBitwiseXor:
404 case Intrinsics::kVarHandleGetAndBitwiseXorAcquire:
405 case Intrinsics::kVarHandleGetAndBitwiseXorRelease:
406 case Intrinsics::kVarHandleGetAndSet:
407 case Intrinsics::kVarHandleGetAndSetAcquire:
408 case Intrinsics::kVarHandleGetAndSetRelease:
409 case Intrinsics::kVarHandleGetOpaque:
410 case Intrinsics::kVarHandleGetVolatile:
411 case Intrinsics::kVarHandleSet:
412 case Intrinsics::kVarHandleSetOpaque:
413 case Intrinsics::kVarHandleSetRelease:
414 case Intrinsics::kVarHandleSetVolatile:
415 case Intrinsics::kVarHandleWeakCompareAndSet:
416 case Intrinsics::kVarHandleWeakCompareAndSetAcquire:
417 case Intrinsics::kVarHandleWeakCompareAndSetPlain:
418 case Intrinsics::kVarHandleWeakCompareAndSetRelease:
419 // These intrinsics are on the blacklist and will fail a DCHECK in
420 // SetIntrinsic() if their flags change on the respective dex methods.
421 // Note that the DCHECK currently won't fail if the dex methods are
422 // whitelisted, e.g. in the core image (b/77733081). Given that they are
423 // exclusively VarHandle intrinsics, they should not be used outside
424 // tests that do not enable hidden API checks.
425 return HiddenApiAccessFlags::kBlacklist;
426 default:
427 // Remaining intrinsics are public API. We DCHECK that in SetIntrinsic().
428 return HiddenApiAccessFlags::kWhitelist;
429 }
430 } else {
431 return HiddenApiAccessFlags::DecodeFromRuntime(GetAccessFlags());
David Brazdilaa129ff2018-01-30 16:11:02 +0000432 }
433}
434
Mingyao Yang063fc772016-08-02 11:02:54 -0700435inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700436 // Currently we only do intrinsics for static/final methods or methods of final
437 // classes. We don't set kHasSingleImplementation for those methods.
438 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
439 "Potential conflict with kAccSingleImplementation";
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100440 static const int kAccFlagsShift = CTZ(kAccIntrinsicBits);
441 DCHECK_LE(intrinsic, kAccIntrinsicBits >> kAccFlagsShift);
442 uint32_t intrinsic_bits = intrinsic << kAccFlagsShift;
443 uint32_t new_value = (GetAccessFlags() & ~kAccIntrinsicBits) | kAccIntrinsic | intrinsic_bits;
Mingyao Yang063fc772016-08-02 11:02:54 -0700444 if (kIsDebugBuild) {
445 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
446 bool is_constructor = IsConstructor();
447 bool is_synchronized = IsSynchronized();
448 bool skip_access_checks = SkipAccessChecks();
449 bool is_fast_native = IsFastNative();
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100450 bool is_critical_native = IsCriticalNative();
Mingyao Yang063fc772016-08-02 11:02:54 -0700451 bool is_copied = IsCopied();
452 bool is_miranda = IsMiranda();
453 bool is_default = IsDefault();
454 bool is_default_conflict = IsDefaultConflicting();
455 bool is_compilable = IsCompilable();
456 bool must_count_locks = MustCountLocks();
David Brazdil2e6f69c2018-04-19 12:41:04 +0100457 HiddenApiAccessFlags::ApiList hidden_api_flags = GetHiddenApiAccessFlags();
Mingyao Yang063fc772016-08-02 11:02:54 -0700458 SetAccessFlags(new_value);
459 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
460 DCHECK_EQ(is_constructor, IsConstructor());
461 DCHECK_EQ(is_synchronized, IsSynchronized());
462 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
463 DCHECK_EQ(is_fast_native, IsFastNative());
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100464 DCHECK_EQ(is_critical_native, IsCriticalNative());
Mingyao Yang063fc772016-08-02 11:02:54 -0700465 DCHECK_EQ(is_copied, IsCopied());
466 DCHECK_EQ(is_miranda, IsMiranda());
467 DCHECK_EQ(is_default, IsDefault());
468 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
469 DCHECK_EQ(is_compilable, IsCompilable());
470 DCHECK_EQ(must_count_locks, MustCountLocks());
David Brazdil2e6f69c2018-04-19 12:41:04 +0100471 // Only DCHECK that we have preserved the hidden API access flags if the
472 // original method was not on the whitelist. This is because the core image
473 // does not have the access flags set (b/77733081). It is fine to hard-code
474 // these because (a) warnings on greylist do not change semantics, and
475 // (b) only VarHandle intrinsics are blacklisted at the moment and they
476 // should not be used outside tests with disabled API checks.
477 if (hidden_api_flags != HiddenApiAccessFlags::kWhitelist) {
478 DCHECK_EQ(hidden_api_flags, GetHiddenApiAccessFlags());
David Brazdil87144352018-01-24 12:50:01 +0000479 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700480 } else {
481 SetAccessFlags(new_value);
482 }
483}
484
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700485template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700486void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700487 if (LIKELY(!declaring_class_.IsNull())) {
488 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Vladimir Markod93e3742018-07-18 10:58:13 +0100489 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000490 if (UNLIKELY(klass->IsProxyClass())) {
491 // For normal methods, dex cache shortcuts will be visited through the declaring class.
492 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000493 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000494 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000495 interface_method->VisitRoots(visitor, pointer_size);
496 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100497 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800498}
499
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800500template <typename Visitor>
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100501inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100502 ObjPtr<mirror::Class> old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
503 ObjPtr<mirror::Class> new_class = visitor(old_class.Ptr());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800504 if (old_class != new_class) {
505 SetDeclaringClass(new_class);
506 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800507}
508
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100509template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700510inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100511 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800512 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800513 const void* new_native_code = visitor(old_native_code);
514 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800515 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800516 }
517 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700518 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800519 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800520 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800521 const void* new_code = visitor(old_code);
522 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800523 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800524 }
525}
526
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800527inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000528 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
529}
530
531inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
532 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
533}
534
535inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
536 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800537}
538
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539} // namespace art
540
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541#endif // ART_RUNTIME_ART_METHOD_INL_H_