blob: 7ddaa7edc3c674abc6330afee8d0f5a86c40f1d8 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070017#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Andreas Gampe479b1de2016-07-19 18:27:17 -070019#include <cstddef>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "arch/context.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "base/stringpiece.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070026#include "class_linker-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070027#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070029#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010035#include "jit/profiling_info.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080038#include "mirror/class_ext.h"
Neil Fuller0e844392016-09-08 13:43:31 +010039#include "mirror/executable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070040#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "mirror/object_array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070042#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000043#include "oat_file-inl.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070044#include "runtime_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070045#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010046#include "vdex_file.h"
Ian Rogers62f05122014-03-21 11:21:29 -070047#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
49namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051using android::base::StringPrintf;
52
Ian Rogers0177e532014-02-11 16:30:46 -080053extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
54 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080055extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
56 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080057
Andreas Gampeaea05c12017-05-19 08:45:02 -070058DEFINE_RUNTIME_DEBUG_FLAG(ArtMethod, kCheckDeclaringClassState);
59
Andreas Gampec6ea7d02017-02-01 16:46:28 -080060// Enforce that we he have the right index for runtime methods.
Andreas Gampee2abbc62017-09-15 11:59:26 -070061static_assert(ArtMethod::kRuntimeMethodDexMethodIndex == dex::kDexNoIndex,
Andreas Gampec6ea7d02017-02-01 16:46:28 -080062 "Wrong runtime-method dex method index");
63
Alex Light97e78032017-06-27 17:51:55 -070064ArtMethod* ArtMethod::GetCanonicalMethod(PointerSize pointer_size) {
65 if (LIKELY(!IsDefault())) {
66 return this;
67 } else {
68 mirror::Class* declaring_class = GetDeclaringClass();
Vladimir Markoba118822017-06-12 15:41:56 +010069 DCHECK(declaring_class->IsInterface());
70 ArtMethod* ret = declaring_class->FindInterfaceMethod(declaring_class->GetDexCache(),
71 GetDexMethodIndex(),
72 pointer_size);
Alex Light97e78032017-06-27 17:51:55 -070073 DCHECK(ret != nullptr);
74 return ret;
75 }
76}
77
Alex Light4ba388a2017-01-27 10:26:49 -080078ArtMethod* ArtMethod::GetNonObsoleteMethod() {
79 DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
80 if (LIKELY(!IsObsolete())) {
81 return this;
82 } else if (IsDirect()) {
83 return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()];
84 } else {
85 return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize);
86 }
87}
88
Mingyao Yange8fcd012017-01-20 10:43:30 -080089ArtMethod* ArtMethod::GetSingleImplementation(PointerSize pointer_size) {
Mingyao Yang063fc772016-08-02 11:02:54 -070090 if (!IsAbstract()) {
91 // A non-abstract's single implementation is itself.
92 return this;
93 }
Mingyao Yange8fcd012017-01-20 10:43:30 -080094 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
Mingyao Yang063fc772016-08-02 11:02:54 -070095}
96
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070097ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
98 jobject jlr_method) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070099 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method);
Neil Fuller0e844392016-09-08 13:43:31 +0100100 DCHECK(executable != nullptr);
101 return executable->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -0700102}
103
Alex Lighta01de592016-11-15 10:43:06 -0800104mirror::DexCache* ArtMethod::GetObsoleteDexCache() {
105 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
106 DCHECK(IsObsolete());
107 ObjPtr<mirror::ClassExt> ext(GetDeclaringClass()->GetExtData());
108 CHECK(!ext.IsNull());
109 ObjPtr<mirror::PointerArray> obsolete_methods(ext->GetObsoleteMethods());
110 CHECK(!obsolete_methods.IsNull());
111 DCHECK(ext->GetObsoleteDexCaches() != nullptr);
112 int32_t len = obsolete_methods->GetLength();
113 DCHECK_EQ(len, ext->GetObsoleteDexCaches()->GetLength());
Alex Light0b772572016-12-02 17:27:31 -0800114 // Using kRuntimePointerSize (instead of using the image's pointer size) is fine since images
115 // should never have obsolete methods in them so they should always be the same.
Alex Lighta01de592016-11-15 10:43:06 -0800116 PointerSize pointer_size = kRuntimePointerSize;
117 DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
118 for (int32_t i = 0; i < len; i++) {
119 if (this == obsolete_methods->GetElementPtrSize<ArtMethod*>(i, pointer_size)) {
120 return ext->GetObsoleteDexCaches()->Get(i);
121 }
122 }
123 LOG(FATAL) << "This method does not appear in the obsolete map of its class!";
124 UNREACHABLE();
125}
126
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000127uint16_t ArtMethod::FindObsoleteDexClassDefIndex() {
128 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
129 DCHECK(IsObsolete());
130 const DexFile* dex_file = GetDexFile();
131 const dex::TypeIndex declaring_class_type = dex_file->GetMethodId(GetDexMethodIndex()).class_idx_;
132 const DexFile::ClassDef* class_def = dex_file->FindClassDef(declaring_class_type);
133 CHECK(class_def != nullptr);
134 return dex_file->GetIndexForClassDef(*class_def);
135}
136
Vladimir Marko28e012a2017-12-07 11:22:59 +0000137ObjPtr<mirror::String> ArtMethod::GetNameAsString(Thread* self) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138 CHECK(!IsProxyMethod());
Ian Rogers6b14d552014-10-28 21:50:58 -0700139 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
141 auto* dex_file = dex_cache->GetDexFile();
142 uint32_t dex_method_idx = GetDexMethodIndex();
143 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000144 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, dex_cache);
Ian Rogers6b14d552014-10-28 21:50:58 -0700145}
146
Alex Light9139e002015-10-09 15:59:48 -0700147void ArtMethod::ThrowInvocationTimeError() {
148 DCHECK(!IsInvokable());
149 // NOTE: IsDefaultConflicting must be first since the actual method might or might not be abstract
150 // due to the way we select it.
151 if (IsDefaultConflicting()) {
152 ThrowIncompatibleClassChangeErrorForMethodConflict(this);
153 } else {
154 DCHECK(IsAbstract());
155 ThrowAbstractMethodError(this);
156 }
157}
158
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 // TODO: kSuper?
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000161 if (IsStatic()) {
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +0000162 return kStatic;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000163 } else if (GetDeclaringClass()->IsInterface()) {
164 return kInterface;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 } else if (IsDirect()) {
166 return kDirect;
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100167 } else if (IsPolymorphicSignature()) {
168 return kPolymorphic;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 } else {
170 return kVirtual;
171 }
172}
173
Brian Carlstromea46f952013-07-30 01:26:50 -0700174size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -0700175 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700177 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 char ch = shorty[i];
179 if (ch == 'D' || ch == 'J') {
180 num_registers += 2;
181 } else {
182 num_registers += 1;
183 }
184 }
185 return num_registers;
186}
187
Alex Light6c8467f2015-11-20 15:03:26 -0800188bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700189 ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature");
Alex Light6c8467f2015-11-20 15:03:26 -0800190 const DexFile* dex_file = GetDexFile();
191 const DexFile::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
192 if (GetDexCache() == other->GetDexCache()) {
193 const DexFile::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800194 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
195 }
Alex Light6c8467f2015-11-20 15:03:26 -0800196 const DexFile* dex_file2 = other->GetDexFile();
197 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800198 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
199 return false; // Name mismatch.
200 }
201 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
202}
203
Andreas Gampe542451c2016-07-26 09:02:02 -0700204ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800206 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 mirror::Class* declaring_class = GetDeclaringClass();
209 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800211 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 // Did this method override a super class method? If so load the result from the super class'
213 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700214 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 } else {
217 // Method didn't override superclass method so search interfaces
218 if (IsProxyMethod()) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100219 result = GetInterfaceMethodIfProxy(pointer_size);
220 DCHECK(result != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800223 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224 mirror::Class* interface = iftable->GetInterface(i);
Alex Light51a64d52015-12-17 13:55:59 -0800225 for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
226 if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
227 result = &interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 break;
229 }
230 }
231 }
232 }
233 }
Alex Light6c8467f2015-11-20 15:03:26 -0800234 DCHECK(result == nullptr ||
Alex Light51a64d52015-12-17 13:55:59 -0800235 GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
236 result->GetInterfaceMethodIfProxy(pointer_size)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 return result;
238}
239
Ian Rogerse0a02da2014-12-02 14:10:53 -0800240uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
241 uint32_t name_and_signature_idx) {
242 const DexFile* dexfile = GetDexFile();
243 const uint32_t dex_method_idx = GetDexMethodIndex();
244 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
245 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
246 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
247 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
248 if (dexfile == &other_dexfile) {
249 return dex_method_idx;
250 }
251 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700252 const DexFile::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
253 if (other_type_id != nullptr) {
254 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
255 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
256 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
257 if (other_mid != nullptr) {
258 return other_dexfile.GetIndexForMethodId(*other_mid);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800259 }
260 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700261 return dex::kDexNoIndex;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800262}
263
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700267 // Set aside the exception while we resolve its type.
268 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700269 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000270 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700271 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700272 // Default to handler not found.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700273 uint32_t found_dex_pc = dex::kDexNoIndex;
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700274 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800276 dex::TypeIndex iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 // Catch all case
Andreas Gampea5b09a62016-11-17 15:21:22 -0800278 if (!iter_type_idx.IsValid()) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700279 found_dex_pc = it.GetHandlerAddress();
280 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 }
282 // Does this catch exception type apply?
Vladimir Markob45528c2017-07-27 14:14:28 +0100283 ObjPtr<mirror::Class> iter_exception_type = ResolveClassFromTypeIndex(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700284 if (UNLIKELY(iter_exception_type == nullptr)) {
285 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
286 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700287 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700288 self->ClearException();
289 // Delete any long jump context as this routine is called during a stack walk which will
290 // release its in use context at the end.
291 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700294 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700295 found_dex_pc = it.GetHandlerAddress();
296 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 }
298 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700299 if (found_dex_pc != dex::kDexNoIndex) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800300 const Instruction& first_catch_instr = DexInstructions().InstructionAt(found_dex_pc);
301 *has_no_move_exception = (first_catch_instr.Opcode() != Instruction::MOVE_EXCEPTION);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700302 }
Jeff Haoaa961912014-04-22 13:54:32 -0700303 // Put the exception back.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800304 if (exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000305 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700306 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700307 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308}
309
Brian Carlstromea46f952013-07-30 01:26:50 -0700310void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800311 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700312 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
313 ThrowStackOverflowError(self);
314 return;
315 }
316
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 if (kIsDebugBuild) {
318 self->AssertThreadSuspensionIsAllowable();
319 CHECK_EQ(kRunnable, self->GetState());
Andreas Gampe542451c2016-07-26 09:02:02 -0700320 CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 }
322
323 // Push a transition back into managed code onto the linked list in thread.
324 ManagedStack fragment;
325 self->PushManagedStackFragment(&fragment);
326
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700328 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200329 // If the runtime is not yet started or it is required by the debugger, then perform the
Aart Bik01223202016-05-05 15:10:42 -0700330 // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
331 // cycling around the various JIT/Interpreter methods that handle method invocation.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200332 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700333 if (IsStatic()) {
Aart Bik01223202016-05-05 15:10:42 -0700334 art::interpreter::EnterInterpreterFromInvoke(
335 self, this, nullptr, args, result, /*stay_in_interpreter*/ true);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700336 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 mirror::Object* receiver =
338 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Aart Bik01223202016-05-05 15:10:42 -0700339 art::interpreter::EnterInterpreterFromInvoke(
340 self, this, receiver, args + 1, result, /*stay_in_interpreter*/ true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 }
342 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700343 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344
345 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800347 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700348 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 LOG(INFO) << StringPrintf(
David Sehr709b0702016-10-13 09:12:37 -0700350 "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700352 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700353
Elliott Hughes956af0f2014-12-11 14:34:28 -0800354 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800355 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100356 CHECK(!runtime->UseJitCompilation());
Alex Lightdb01a092017-04-03 15:39:55 -0700357 const void* oat_quick_code =
358 (IsNative() || !IsInvokable() || IsProxyMethod() || IsObsolete())
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100359 ? nullptr
360 : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100361 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
David Sehr709b0702016-10-13 09:12:37 -0700362 << "Don't call compiled code when -Xint " << PrettyMethod();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700363 }
364
Elliott Hughes956af0f2014-12-11 14:34:28 -0800365 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800366 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800368 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800369 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000370 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200371 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700372 // exception was thrown to force the activations to be removed from the
373 // stack. Continue execution in the interpreter.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000374 self->DeoptimizeWithDeoptimizationException(result);
Jeff Hao790ad902013-05-22 15:02:08 -0700375 }
376 if (kLogInvocationStartAndReturn) {
David Sehr709b0702016-10-13 09:12:37 -0700377 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(),
Elliott Hughes956af0f2014-12-11 14:34:28 -0800378 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800379 }
380 } else {
David Sehr709b0702016-10-13 09:12:37 -0700381 LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800382 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800383 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 }
385 }
386 }
387
388 // Pop transition.
389 self->PopManagedStackFragment(fragment);
390}
391
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100392const void* ArtMethod::RegisterNative(const void* native_method) {
David Sehr709b0702016-10-13 09:12:37 -0700393 CHECK(IsNative()) << PrettyMethod();
David Sehr709b0702016-10-13 09:12:37 -0700394 CHECK(native_method != nullptr) << PrettyMethod();
Alex Lightd78ddec2017-04-18 15:20:38 -0700395 void* new_native_method = nullptr;
396 Runtime::Current()->GetRuntimeCallbacks()->RegisterNativeMethod(this,
397 native_method,
398 /*out*/&new_native_method);
399 SetEntryPointFromJni(new_native_method);
400 return new_native_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401}
402
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700403void ArtMethod::UnregisterNative() {
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100404 CHECK(IsNative()) << PrettyMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 // restore stub to lookup native pointer via dlsym
Alex Lightd78ddec2017-04-18 15:20:38 -0700406 SetEntryPointFromJni(GetJniDlsymLookupStub());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407}
408
Alex Light9139e002015-10-09 15:59:48 -0700409bool ArtMethod::IsOverridableByDefaultMethod() {
410 return GetDeclaringClass()->IsInterface();
411}
412
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000413bool ArtMethod::IsPolymorphicSignature() {
414 // Methods with a polymorphic signature have constraints that they
415 // are native and varargs and belong to either MethodHandle or VarHandle.
416 if (!IsNative() || !IsVarargs()) {
417 return false;
418 }
419 mirror::Class* cls = GetDeclaringClass();
420 return (cls == WellKnownClasses::ToClass(WellKnownClasses::java_lang_invoke_MethodHandle) ||
421 cls == WellKnownClasses::ToClass(WellKnownClasses::java_lang_invoke_VarHandle));
422}
423
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100424static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file,
425 uint16_t class_def_idx,
426 uint32_t method_idx) {
427 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
428 const uint8_t* class_data = dex_file.GetClassData(class_def);
429 CHECK(class_data != nullptr);
430 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700431 it.SkipAllFields();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100432 // Process methods
433 size_t class_def_method_index = 0;
434 while (it.HasNextDirectMethod()) {
435 if (it.GetMemberIndex() == method_idx) {
436 return class_def_method_index;
437 }
438 class_def_method_index++;
439 it.Next();
440 }
441 while (it.HasNextVirtualMethod()) {
442 if (it.GetMemberIndex() == method_idx) {
443 return class_def_method_index;
444 }
445 class_def_method_index++;
446 it.Next();
447 }
448 DCHECK(!it.HasNext());
449 LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
450 UNREACHABLE();
451}
452
Alex Lighteee0bd42017-02-14 15:31:45 +0000453// We use the method's DexFile and declaring class name to find the OatMethod for an obsolete
454// method. This is extremely slow but we need it if we want to be able to have obsolete native
455// methods since we need this to find the size of its stack frames.
456//
457// NB We could (potentially) do this differently and rely on the way the transformation is applied
458// in order to use the entrypoint to find this information. However, for debugging reasons (most
459// notably making sure that new invokes of obsolete methods fail) we choose to instead get the data
460// directly from the dex file.
461static const OatFile::OatMethod FindOatMethodFromDexFileFor(ArtMethod* method, bool* found)
462 REQUIRES_SHARED(Locks::mutator_lock_) {
463 DCHECK(method->IsObsolete() && method->IsNative());
464 const DexFile* dex_file = method->GetDexFile();
465
466 // recreate the class_def_index from the descriptor.
467 std::string descriptor_storage;
468 const DexFile::TypeId* declaring_class_type_id =
469 dex_file->FindTypeId(method->GetDeclaringClass()->GetDescriptor(&descriptor_storage));
470 CHECK(declaring_class_type_id != nullptr);
471 dex::TypeIndex declaring_class_type_index = dex_file->GetIndexForTypeId(*declaring_class_type_id);
472 const DexFile::ClassDef* declaring_class_type_def =
473 dex_file->FindClassDef(declaring_class_type_index);
474 CHECK(declaring_class_type_def != nullptr);
475 uint16_t declaring_class_def_index = dex_file->GetIndexForClassDef(*declaring_class_type_def);
476
477 size_t oat_method_index = GetOatMethodIndexFromMethodIndex(*dex_file,
478 declaring_class_def_index,
479 method->GetDexMethodIndex());
480
481 OatFile::OatClass oat_class = OatFile::FindOatClass(*dex_file,
482 declaring_class_def_index,
483 found);
484 if (!(*found)) {
485 return OatFile::OatMethod::Invalid();
486 }
487 return oat_class.GetOatMethod(oat_method_index);
488}
489
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100490static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method,
491 PointerSize pointer_size,
492 bool* found)
493 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000494 if (UNLIKELY(method->IsObsolete())) {
495 // We shouldn't be calling this with obsolete methods except for native obsolete methods for
496 // which we need to use the oat method to figure out how large the quick frame is.
497 DCHECK(method->IsNative()) << "We should only be finding the OatMethod of obsolete methods in "
498 << "order to allow stack walking. Other obsolete methods should "
499 << "never need to access this information.";
500 DCHECK_EQ(pointer_size, kRuntimePointerSize) << "Obsolete method in compiler!";
501 return FindOatMethodFromDexFileFor(method, found);
502 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100503 // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
504 // method for direct methods (or virtual methods made direct).
505 mirror::Class* declaring_class = method->GetDeclaringClass();
506 size_t oat_method_index;
507 if (method->IsStatic() || method->IsDirect()) {
508 // Simple case where the oat method index was stashed at load time.
509 oat_method_index = method->GetMethodIndex();
510 } else {
511 // Compute the oat_method_index by search for its position in the declared virtual methods.
512 oat_method_index = declaring_class->NumDirectMethods();
513 bool found_virtual = false;
514 for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) {
515 // Check method index instead of identity in case of duplicate method definitions.
516 if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) {
517 found_virtual = true;
518 break;
519 }
520 oat_method_index++;
521 }
522 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
David Sehr709b0702016-10-13 09:12:37 -0700523 << method->PrettyMethod();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100524 }
525 DCHECK_EQ(oat_method_index,
526 GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(),
527 method->GetDeclaringClass()->GetDexClassDefIndex(),
528 method->GetDexMethodIndex()));
529 OatFile::OatClass oat_class = OatFile::FindOatClass(*declaring_class->GetDexCache()->GetDexFile(),
530 declaring_class->GetDexClassDefIndex(),
531 found);
532 if (!(*found)) {
533 return OatFile::OatMethod::Invalid();
534 }
535 return oat_class.GetOatMethod(oat_method_index);
536}
537
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700538bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
539 auto* dex_cache = GetDexCache();
540 auto* dex_file = dex_cache->GetDexFile();
541 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
542 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
543 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
544 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800545 auto param_len = params != nullptr ? params->GetLength() : 0u;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700546 if (param_len != count) {
547 return false;
548 }
549 auto* cl = Runtime::Current()->GetClassLinker();
550 for (size_t i = 0; i < count; ++i) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000551 dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_;
552 ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700553 if (type == nullptr) {
554 Thread::Current()->AssertPendingException();
555 return false;
556 }
557 if (type != params->GetWithoutChecks(i)) {
558 return false;
559 }
560 }
561 return true;
562}
563
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000564const uint8_t* ArtMethod::GetQuickenedInfo() {
565 const DexFile& dex_file = GetDeclaringClass()->GetDexFile();
566 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
567 if (oat_dex_file == nullptr || (oat_dex_file->GetOatFile() == nullptr)) {
568 return nullptr;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100569 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000570 return oat_dex_file->GetOatFile()->GetVdexFile()->GetQuickenedInfoOf(
571 dex_file, GetCodeItemOffset());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100572}
573
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100574const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000575 // Our callers should make sure they don't pass the instrumentation exit pc,
576 // as this method does not look at the side instrumentation stack.
577 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
578
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000579 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100580 return nullptr;
581 }
582
583 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100584 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700585 CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100586 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100587
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000588 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
589 DCHECK(IsProxyMethod() && !IsConstructor());
590 // The proxy entry point does not have any method header.
591 return nullptr;
592 }
593
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100594 // Check whether the current entry point contains this pc.
Vladimir Marko2196c652017-11-30 16:16:07 +0000595 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
596 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100597 !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) {
598 OatQuickMethodHeader* method_header =
599 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100600
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100601 if (method_header->Contains(pc)) {
602 return method_header;
603 }
604 }
605
606 // Check whether the pc is in the JIT code cache.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100607 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100608 if (jit != nullptr) {
609 jit::JitCodeCache* code_cache = jit->GetCodeCache();
610 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
611 if (method_header != nullptr) {
612 DCHECK(method_header->Contains(pc));
613 return method_header;
614 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000615 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
David Sehr709b0702016-10-13 09:12:37 -0700616 << PrettyMethod()
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000617 << ", pc=" << std::hex << pc
618 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
619 << ", copy=" << std::boolalpha << IsCopied()
620 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100621 }
622 }
623
624 // The code has to be in an oat file.
625 bool found;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100626 OatFile::OatMethod oat_method =
627 FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100628 if (!found) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000629 if (IsNative()) {
630 // We are running the GenericJNI stub. The entrypoint may point
631 // to different entrypoints or to a JIT-compiled JNI stub.
632 DCHECK(class_linker->IsQuickGenericJniStub(existing_entry_point) ||
633 class_linker->IsQuickResolutionStub(existing_entry_point) ||
634 existing_entry_point == GetQuickInstrumentationEntryPoint() ||
635 (jit != nullptr && jit->GetCodeCache()->ContainsPc(existing_entry_point)));
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000636 return nullptr;
637 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100638 // Only for unit tests.
639 // TODO(ngeoffray): Update these tests to pass the right pc?
640 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
641 }
642 const void* oat_entry_point = oat_method.GetQuickCode();
643 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
David Sehr709b0702016-10-13 09:12:37 -0700644 DCHECK(IsNative()) << PrettyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100645 return nullptr;
646 }
647
648 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
649 if (pc == 0) {
650 // This is a downcall, it can only happen for a native method.
651 DCHECK(IsNative());
652 return method_header;
653 }
654
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100655 DCHECK(method_header->Contains(pc))
David Sehr709b0702016-10-13 09:12:37 -0700656 << PrettyMethod()
Roland Levillain0b671c02016-08-19 12:02:34 +0100657 << " " << std::hex << pc << " " << oat_entry_point
Mingyao Yang063fc772016-08-02 11:02:54 -0700658 << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100659 return method_header;
660}
661
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100662const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) {
663 bool found;
664 OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found);
665 if (found) {
666 return oat_method.GetQuickCode();
667 }
668 return nullptr;
669}
670
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000671bool ArtMethod::HasAnyCompiledCode() {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100672 if (IsNative() || !IsInvokable() || IsProxyMethod()) {
673 return false;
674 }
675
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000676 // Check whether the JIT has compiled it.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100677 Runtime* runtime = Runtime::Current();
678 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000679 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
680 return true;
681 }
682
683 // Check whether we have AOT code.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100684 return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr;
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000685}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000686
Andreas Gampe542451c2016-07-26 09:02:02 -0700687void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000688 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
689 Size(image_pointer_size));
690 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
691
692 // If the entry point of the method we are copying from is from JIT code, we just
Vladimir Marko2196c652017-11-30 16:16:07 +0000693 // put the entry point of the new method to interpreter or GenericJNI. We could set
694 // the entry point to the JIT code, but this would require taking the JIT code cache
695 // lock to notify it, which we do not want at this level.
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000696 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100697 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000698 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000699 SetEntryPointFromQuickCompiledCodePtrSize(
700 src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(),
701 image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000702 }
703 }
704 // Clear the profiling info for the same reasons as the JIT code.
705 if (!src->IsNative()) {
706 SetProfilingInfoPtrSize(nullptr, image_pointer_size);
707 }
708 // Clear hotness to let the JIT properly decide when to compile this method.
709 hotness_count_ = 0;
710}
711
Andreas Gampe542451c2016-07-26 09:02:02 -0700712bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700713 // Hijack this function to get access to PtrSizedFieldsOffset.
714 //
715 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
716 // 64-bit builds.
717 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700718 static_assert(
719 (sizeof(void*) != 4) ||
720 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
721 "Unexpected 32-bit class layout.");
722 static_assert(
723 (sizeof(void*) != 8) ||
724 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
725 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700726
Andreas Gampe75f08852016-07-19 08:06:07 -0700727 Runtime* runtime = Runtime::Current();
728 if (runtime == nullptr) {
729 return true;
730 }
731 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
732}
733
David Sehr709b0702016-10-13 09:12:37 -0700734std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) {
735 if (m == nullptr) {
736 return "null";
737 }
738 return m->PrettyMethod(with_signature);
739}
740
741std::string ArtMethod::PrettyMethod(bool with_signature) {
Vladimir Markob8a55f82017-09-21 16:21:43 +0100742 if (UNLIKELY(IsRuntimeMethod())) {
743 std::string result = GetDeclaringClassDescriptor();
744 result += '.';
745 result += GetName();
746 // Do not add "<no signature>" even if `with_signature` is true.
747 return result;
David Sehr709b0702016-10-13 09:12:37 -0700748 }
Vladimir Markob8a55f82017-09-21 16:21:43 +0100749 ArtMethod* m =
750 GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
751 return m->GetDexFile()->PrettyMethod(m->GetDexMethodIndex(), with_signature);
David Sehr709b0702016-10-13 09:12:37 -0700752}
753
754std::string ArtMethod::JniShortName() {
Alex Light888a59e2017-01-25 11:41:41 -0800755 return GetJniShortName(GetDeclaringClassDescriptor(), GetName());
David Sehr709b0702016-10-13 09:12:37 -0700756}
757
758std::string ArtMethod::JniLongName() {
759 std::string long_name;
760 long_name += JniShortName();
761 long_name += "__";
762
763 std::string signature(GetSignature().ToString());
764 signature.erase(0, 1);
765 signature.erase(signature.begin() + signature.find(')'), signature.end());
766
767 long_name += MangleForJni(signature);
768
769 return long_name;
770}
771
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800772// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
773// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
774template <ReadBarrierOption kReadBarrierOption>
775ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
776 NO_THREAD_SAFETY_ANALYSIS {
777 CHECK(method->IsRuntimeMethod() ||
778 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
779 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
780}
781
782template <ReadBarrierOption kReadBarrierOption> void ArtMethod::GetAccessFlagsDCheck() {
783 if (kCheckDeclaringClassState) {
784 Thread* self = Thread::Current();
785 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
786 if (self->IsThreadSuspensionAllowable()) {
787 ScopedObjectAccess soa(self);
788 CHECK(IsRuntimeMethod() ||
789 GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
790 GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
791 }
792 } else {
793 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
794 // runnable state (e.g., during GC).
795 Locks::mutator_lock_->AssertSharedHeld(self);
796 DoGetAccessFlagsHelper<kReadBarrierOption>(this);
797 }
798 }
799}
800template void ArtMethod::GetAccessFlagsDCheck<ReadBarrierOption::kWithReadBarrier>();
801template void ArtMethod::GetAccessFlagsDCheck<ReadBarrierOption::kWithoutReadBarrier>();
802
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800803} // namespace art