blob: 3c70a9261293e9fb763d03eaf964af88b7864682 [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
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000019#include <algorithm>
Andreas Gampe479b1de2016-07-19 18:27:17 -070020#include <cstddef>
21
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include "android-base/stringprintf.h"
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"
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000025#include "base/enums.h"
26#include "base/stl_util.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070027#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010028#include "class_root-inl.h"
Ulyana Trafimoviche886d682020-07-16 15:09:38 +000029#include "code_simulator.h"
30#include "code_simulator_container.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070031#include "debugger.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070032#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080033#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file-inl.h"
35#include "dex/dex_file_exception_helpers.h"
36#include "dex/dex_instruction.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080037#include "dex/signature-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070038#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/accounting/card_table-inl.h"
David Brazdil85865692018-10-30 17:26:20 +000040#include "hidden_api.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042#include "jit/jit.h"
43#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010044#include "jit/profiling_info.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010045#include "jni/jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070046#include "mirror/class-inl.h"
Vladimir Markobb206de2019-03-28 10:30:32 +000047#include "mirror/class_ext-inl.h"
Neil Fuller0e844392016-09-08 13:43:31 +010048#include "mirror/executable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070049#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "mirror/object_array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070051#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000052#include "oat_file-inl.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000053#include "quicken_info.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070054#include "runtime_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070055#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010056#include "vdex_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057
58namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059
Andreas Gampe46ee31b2016-12-14 10:11:49 -080060using android::base::StringPrintf;
61
Ian Rogers0177e532014-02-11 16:30:46 -080062extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
63 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080064extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
65 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080066
Andreas Gampec6ea7d02017-02-01 16:46:28 -080067// Enforce that we he have the right index for runtime methods.
Andreas Gampee2abbc62017-09-15 11:59:26 -070068static_assert(ArtMethod::kRuntimeMethodDexMethodIndex == dex::kDexNoIndex,
Andreas Gampec6ea7d02017-02-01 16:46:28 -080069 "Wrong runtime-method dex method index");
70
Alex Light97e78032017-06-27 17:51:55 -070071ArtMethod* ArtMethod::GetCanonicalMethod(PointerSize pointer_size) {
Alex Lightfd4a79c2019-06-21 08:45:16 -070072 if (LIKELY(!IsCopied())) {
Alex Light97e78032017-06-27 17:51:55 -070073 return this;
74 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010075 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
Vladimir Markoba118822017-06-12 15:41:56 +010076 DCHECK(declaring_class->IsInterface());
Vladimir Marko813a8632018-11-29 16:17:01 +000077 ArtMethod* ret = declaring_class->FindInterfaceMethod(GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +010078 GetDexMethodIndex(),
79 pointer_size);
Alex Light97e78032017-06-27 17:51:55 -070080 DCHECK(ret != nullptr);
81 return ret;
82 }
83}
84
Alex Light4ba388a2017-01-27 10:26:49 -080085ArtMethod* ArtMethod::GetNonObsoleteMethod() {
Alex Light4ba388a2017-01-27 10:26:49 -080086 if (LIKELY(!IsObsolete())) {
87 return this;
Vladimir Marko813a8632018-11-29 16:17:01 +000088 }
89 DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
90 if (IsDirect()) {
Alex Light4ba388a2017-01-27 10:26:49 -080091 return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()];
92 } else {
93 return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize);
94 }
95}
96
Mingyao Yange8fcd012017-01-20 10:43:30 -080097ArtMethod* ArtMethod::GetSingleImplementation(PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +010098 if (!IsAbstract()) {
Mingyao Yang063fc772016-08-02 11:02:54 -070099 // A non-abstract's single implementation is itself.
100 return this;
101 }
Mingyao Yange8fcd012017-01-20 10:43:30 -0800102 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
Mingyao Yang063fc772016-08-02 11:02:54 -0700103}
104
Ulyana Trafimoviche886d682020-07-16 15:09:38 +0000105bool ArtMethod::CanBeSimulated() REQUIRES_SHARED(Locks::mutator_lock_) {
106 CodeSimulatorContainer* simulator = Thread::Current()->GetSimulator();
107 return simulator->Get()->CanSimulate(this);
108}
109
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700110ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
111 jobject jlr_method) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700112 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method);
Neil Fuller0e844392016-09-08 13:43:31 +0100113 DCHECK(executable != nullptr);
114 return executable->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -0700115}
116
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000117ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache() {
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000118 PointerSize pointer_size = kRuntimePointerSize;
Alex Lighta01de592016-11-15 10:43:06 -0800119 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
120 DCHECK(IsObsolete());
121 ObjPtr<mirror::ClassExt> ext(GetDeclaringClass()->GetExtData());
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000122 ObjPtr<mirror::PointerArray> obsolete_methods(ext.IsNull() ? nullptr : ext->GetObsoleteMethods());
123 int32_t len = (obsolete_methods.IsNull() ? 0 : obsolete_methods->GetLength());
124 DCHECK(len == 0 || len == ext->GetObsoleteDexCaches()->GetLength())
125 << "len=" << len << " ext->GetObsoleteDexCaches()=" << ext->GetObsoleteDexCaches();
Alex Light0b772572016-12-02 17:27:31 -0800126 // Using kRuntimePointerSize (instead of using the image's pointer size) is fine since images
127 // should never have obsolete methods in them so they should always be the same.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000128 DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
Alex Lighta01de592016-11-15 10:43:06 -0800129 for (int32_t i = 0; i < len; i++) {
130 if (this == obsolete_methods->GetElementPtrSize<ArtMethod*>(i, pointer_size)) {
131 return ext->GetObsoleteDexCaches()->Get(i);
132 }
133 }
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000134 CHECK(GetDeclaringClass()->IsObsoleteObject())
135 << "This non-structurally obsolete method does not appear in the obsolete map of its class: "
136 << GetDeclaringClass()->PrettyClass() << " Searched " << len << " caches.";
137 CHECK_EQ(this,
138 std::clamp(this,
139 &(*GetDeclaringClass()->GetMethods(pointer_size).begin()),
140 &(*GetDeclaringClass()->GetMethods(pointer_size).end())))
141 << "class is marked as structurally obsolete method but not found in normal obsolete-map "
142 << "despite not being the original method pointer for " << GetDeclaringClass()->PrettyClass();
143 return GetDeclaringClass()->GetDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800144}
145
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000146uint16_t ArtMethod::FindObsoleteDexClassDefIndex() {
147 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
148 DCHECK(IsObsolete());
149 const DexFile* dex_file = GetDexFile();
150 const dex::TypeIndex declaring_class_type = dex_file->GetMethodId(GetDexMethodIndex()).class_idx_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800151 const dex::ClassDef* class_def = dex_file->FindClassDef(declaring_class_type);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000152 CHECK(class_def != nullptr);
153 return dex_file->GetIndexForClassDef(*class_def);
154}
155
Alex Light9139e002015-10-09 15:59:48 -0700156void ArtMethod::ThrowInvocationTimeError() {
157 DCHECK(!IsInvokable());
158 // NOTE: IsDefaultConflicting must be first since the actual method might or might not be abstract
159 // due to the way we select it.
160 if (IsDefaultConflicting()) {
161 ThrowIncompatibleClassChangeErrorForMethodConflict(this);
162 } else {
163 DCHECK(IsAbstract());
164 ThrowAbstractMethodError(this);
165 }
166}
167
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 // TODO: kSuper?
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000170 if (IsStatic()) {
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +0000171 return kStatic;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000172 } else if (GetDeclaringClass()->IsInterface()) {
173 return kInterface;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 } else if (IsDirect()) {
175 return kDirect;
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100176 } else if (IsPolymorphicSignature()) {
177 return kPolymorphic;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 } else {
179 return kVirtual;
180 }
181}
182
Vladimir Marko6e781582019-02-04 10:58:06 +0000183size_t ArtMethod::NumArgRegisters(const char* shorty) {
184 CHECK_NE(shorty[0], '\0');
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 uint32_t num_registers = 0;
Vladimir Marko6e781582019-02-04 10:58:06 +0000186 for (const char* s = shorty + 1; *s != '\0'; ++s) {
187 if (*s == 'D' || *s == 'J') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 num_registers += 2;
189 } else {
190 num_registers += 1;
191 }
192 }
193 return num_registers;
194}
195
Alex Light6c8467f2015-11-20 15:03:26 -0800196bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700197 ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature");
Alex Light6c8467f2015-11-20 15:03:26 -0800198 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800199 const dex::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
Alex Light6c8467f2015-11-20 15:03:26 -0800200 if (GetDexCache() == other->GetDexCache()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800201 const dex::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800202 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
203 }
Alex Light6c8467f2015-11-20 15:03:26 -0800204 const DexFile* dex_file2 = other->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800205 const dex::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
Andreas Gampeb8bca302019-01-02 10:28:38 -0800206 if (!DexFile::StringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
Ian Rogersf2247512014-12-02 16:17:08 -0800207 return false; // Name mismatch.
208 }
209 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
210}
211
Andreas Gampe542451c2016-07-26 09:02:02 -0700212ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800214 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 }
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100216 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
217 ObjPtr<mirror::Class> super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800219 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 // Did this method override a super class method? If so load the result from the super class'
221 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700222 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 } else {
225 // Method didn't override superclass method so search interfaces
226 if (IsProxyMethod()) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100227 result = GetInterfaceMethodIfProxy(pointer_size);
228 DCHECK(result != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 } else {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000230 ObjPtr<mirror::IfTable> iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800231 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100232 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Alex Light51a64d52015-12-17 13:55:59 -0800233 for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
234 if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
235 result = &interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 break;
237 }
238 }
239 }
240 }
241 }
Alex Light6c8467f2015-11-20 15:03:26 -0800242 DCHECK(result == nullptr ||
Alex Light51a64d52015-12-17 13:55:59 -0800243 GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
244 result->GetInterfaceMethodIfProxy(pointer_size)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 return result;
246}
247
Ian Rogerse0a02da2014-12-02 14:10:53 -0800248uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
249 uint32_t name_and_signature_idx) {
250 const DexFile* dexfile = GetDexFile();
251 const uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800252 const dex::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
253 const dex::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800254 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
255 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
256 if (dexfile == &other_dexfile) {
257 return dex_method_idx;
258 }
259 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800260 const dex::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700261 if (other_type_id != nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800262 const dex::MethodId* other_mid = other_dexfile.FindMethodId(
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700263 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
264 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
265 if (other_mid != nullptr) {
266 return other_dexfile.GetIndexForMethodId(*other_mid);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800267 }
268 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700269 return dex::kDexNoIndex;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800270}
271
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700273 uint32_t dex_pc, bool* has_no_move_exception) {
Jeff Haoaa961912014-04-22 13:54:32 -0700274 // Set aside the exception while we resolve its type.
275 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700276 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000277 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700278 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700279 // Default to handler not found.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700280 uint32_t found_dex_pc = dex::kDexNoIndex;
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700281 // Iterate over the catch handlers associated with dex_pc.
David Sehr0225f8e2018-01-31 08:52:24 +0000282 CodeItemDataAccessor accessor(DexInstructionData());
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800283 for (CatchHandlerIterator it(accessor, dex_pc); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800284 dex::TypeIndex iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 // Catch all case
Andreas Gampea5b09a62016-11-17 15:21:22 -0800286 if (!iter_type_idx.IsValid()) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700287 found_dex_pc = it.GetHandlerAddress();
288 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
290 // Does this catch exception type apply?
Vladimir Markob45528c2017-07-27 14:14:28 +0100291 ObjPtr<mirror::Class> iter_exception_type = ResolveClassFromTypeIndex(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700292 if (UNLIKELY(iter_exception_type == nullptr)) {
293 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
294 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700295 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700296 self->ClearException();
297 // Delete any long jump context as this routine is called during a stack walk which will
298 // release its in use context at the end.
299 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700302 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700303 found_dex_pc = it.GetHandlerAddress();
304 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 }
306 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700307 if (found_dex_pc != dex::kDexNoIndex) {
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800308 const Instruction& first_catch_instr = accessor.InstructionAt(found_dex_pc);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800309 *has_no_move_exception = (first_catch_instr.Opcode() != Instruction::MOVE_EXCEPTION);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700310 }
Jeff Haoaa961912014-04-22 13:54:32 -0700311 // Put the exception back.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800312 if (exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000313 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700314 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700315 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316}
317
Brian Carlstromea46f952013-07-30 01:26:50 -0700318void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800319 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700320 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
321 ThrowStackOverflowError(self);
322 return;
323 }
324
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325 if (kIsDebugBuild) {
326 self->AssertThreadSuspensionIsAllowable();
327 CHECK_EQ(kRunnable, self->GetState());
Andreas Gampe542451c2016-07-26 09:02:02 -0700328 CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 }
330
331 // Push a transition back into managed code onto the linked list in thread.
332 ManagedStack fragment;
333 self->PushManagedStackFragment(&fragment);
334
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700336 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200337 // If the runtime is not yet started or it is required by the debugger, then perform the
Aart Bik01223202016-05-05 15:10:42 -0700338 // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
339 // cycling around the various JIT/Interpreter methods that handle method invocation.
Alex Light3dacdd62019-03-12 15:45:47 +0000340 if (UNLIKELY(!runtime->IsStarted() ||
Alex Lightfc588092020-01-23 15:39:08 -0800341 (self->IsForceInterpreter() && !IsNative() && !IsProxyMethod() && IsInvokable()))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700342 if (IsStatic()) {
Aart Bik01223202016-05-05 15:10:42 -0700343 art::interpreter::EnterInterpreterFromInvoke(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700344 self, this, nullptr, args, result, /*stay_in_interpreter=*/ true);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700345 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 mirror::Object* receiver =
347 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Aart Bik01223202016-05-05 15:10:42 -0700348 art::interpreter::EnterInterpreterFromInvoke(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700349 self, this, receiver, args + 1, result, /*stay_in_interpreter=*/ true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 }
351 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700352 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353
354 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800355 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800356 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700357 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 LOG(INFO) << StringPrintf(
David Sehr709b0702016-10-13 09:12:37 -0700359 "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700361 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700362
Elliott Hughes956af0f2014-12-11 14:34:28 -0800363 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Ulyana Trafimoviche886d682020-07-16 15:09:38 +0000364 if (kIsDebugBuild &&
365 runtime->GetInstrumentation()->IsForcedInterpretOnly() &&
366 !Runtime::SimulatorMode()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100367 CHECK(!runtime->UseJitCompilation());
Alex Lightdb01a092017-04-03 15:39:55 -0700368 const void* oat_quick_code =
369 (IsNative() || !IsInvokable() || IsProxyMethod() || IsObsolete())
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100370 ? nullptr
371 : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100372 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
David Sehr709b0702016-10-13 09:12:37 -0700373 << "Don't call compiled code when -Xint " << PrettyMethod();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700374 }
375
Ulyana Trafimoviche886d682020-07-16 15:09:38 +0000376 if (Runtime::SimulatorMode() && CanBeSimulated()) {
377 CodeSimulatorContainer* simulator = Thread::Current()->GetSimulator();
378 simulator->Get()->Invoke(this, args, args_size, self, result, shorty, IsStatic());
379 } else if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800380 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800381 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800382 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800383 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000384 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200385 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700386 // exception was thrown to force the activations to be removed from the
387 // stack. Continue execution in the interpreter.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000388 self->DeoptimizeWithDeoptimizationException(result);
Jeff Hao790ad902013-05-22 15:02:08 -0700389 }
390 if (kLogInvocationStartAndReturn) {
David Sehr709b0702016-10-13 09:12:37 -0700391 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(),
Elliott Hughes956af0f2014-12-11 14:34:28 -0800392 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800393 }
394 } else {
David Sehr709b0702016-10-13 09:12:37 -0700395 LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800396 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800397 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 }
399 }
400 }
401
402 // Pop transition.
403 self->PopManagedStackFragment(fragment);
404}
405
Alex Light9139e002015-10-09 15:59:48 -0700406bool ArtMethod::IsOverridableByDefaultMethod() {
407 return GetDeclaringClass()->IsInterface();
408}
409
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000410bool ArtMethod::IsPolymorphicSignature() {
411 // Methods with a polymorphic signature have constraints that they
412 // are native and varargs and belong to either MethodHandle or VarHandle.
413 if (!IsNative() || !IsVarargs()) {
414 return false;
415 }
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100416 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
417 Runtime::Current()->GetClassLinker()->GetClassRoots();
418 ObjPtr<mirror::Class> cls = GetDeclaringClass();
419 return (cls == GetClassRoot<mirror::MethodHandle>(class_roots) ||
420 cls == GetClassRoot<mirror::VarHandle>(class_roots));
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000421}
422
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100423static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file,
424 uint16_t class_def_idx,
425 uint32_t method_idx) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700426 ClassAccessor accessor(dex_file, class_def_idx);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700427 uint32_t class_def_method_index = 0u;
428 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
429 if (method.GetIndex() == method_idx) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100430 return class_def_method_index;
431 }
432 class_def_method_index++;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100433 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100434 LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
435 UNREACHABLE();
436}
437
Alex Lighteee0bd42017-02-14 15:31:45 +0000438// We use the method's DexFile and declaring class name to find the OatMethod for an obsolete
439// method. This is extremely slow but we need it if we want to be able to have obsolete native
440// methods since we need this to find the size of its stack frames.
441//
442// NB We could (potentially) do this differently and rely on the way the transformation is applied
443// in order to use the entrypoint to find this information. However, for debugging reasons (most
444// notably making sure that new invokes of obsolete methods fail) we choose to instead get the data
445// directly from the dex file.
446static const OatFile::OatMethod FindOatMethodFromDexFileFor(ArtMethod* method, bool* found)
447 REQUIRES_SHARED(Locks::mutator_lock_) {
448 DCHECK(method->IsObsolete() && method->IsNative());
449 const DexFile* dex_file = method->GetDexFile();
450
451 // recreate the class_def_index from the descriptor.
452 std::string descriptor_storage;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800453 const dex::TypeId* declaring_class_type_id =
Alex Lighteee0bd42017-02-14 15:31:45 +0000454 dex_file->FindTypeId(method->GetDeclaringClass()->GetDescriptor(&descriptor_storage));
455 CHECK(declaring_class_type_id != nullptr);
456 dex::TypeIndex declaring_class_type_index = dex_file->GetIndexForTypeId(*declaring_class_type_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800457 const dex::ClassDef* declaring_class_type_def =
Alex Lighteee0bd42017-02-14 15:31:45 +0000458 dex_file->FindClassDef(declaring_class_type_index);
459 CHECK(declaring_class_type_def != nullptr);
460 uint16_t declaring_class_def_index = dex_file->GetIndexForClassDef(*declaring_class_type_def);
461
462 size_t oat_method_index = GetOatMethodIndexFromMethodIndex(*dex_file,
463 declaring_class_def_index,
464 method->GetDexMethodIndex());
465
466 OatFile::OatClass oat_class = OatFile::FindOatClass(*dex_file,
467 declaring_class_def_index,
468 found);
469 if (!(*found)) {
470 return OatFile::OatMethod::Invalid();
471 }
472 return oat_class.GetOatMethod(oat_method_index);
473}
474
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100475static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method,
476 PointerSize pointer_size,
477 bool* found)
478 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000479 if (UNLIKELY(method->IsObsolete())) {
480 // We shouldn't be calling this with obsolete methods except for native obsolete methods for
481 // which we need to use the oat method to figure out how large the quick frame is.
482 DCHECK(method->IsNative()) << "We should only be finding the OatMethod of obsolete methods in "
483 << "order to allow stack walking. Other obsolete methods should "
484 << "never need to access this information.";
485 DCHECK_EQ(pointer_size, kRuntimePointerSize) << "Obsolete method in compiler!";
486 return FindOatMethodFromDexFileFor(method, found);
487 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100488 // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
489 // method for direct methods (or virtual methods made direct).
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100490 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100491 size_t oat_method_index;
492 if (method->IsStatic() || method->IsDirect()) {
493 // Simple case where the oat method index was stashed at load time.
494 oat_method_index = method->GetMethodIndex();
495 } else {
496 // Compute the oat_method_index by search for its position in the declared virtual methods.
497 oat_method_index = declaring_class->NumDirectMethods();
498 bool found_virtual = false;
499 for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) {
500 // Check method index instead of identity in case of duplicate method definitions.
501 if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) {
502 found_virtual = true;
503 break;
504 }
505 oat_method_index++;
506 }
507 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
David Sehr709b0702016-10-13 09:12:37 -0700508 << method->PrettyMethod();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100509 }
510 DCHECK_EQ(oat_method_index,
Vladimir Marko813a8632018-11-29 16:17:01 +0000511 GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100512 method->GetDeclaringClass()->GetDexClassDefIndex(),
513 method->GetDexMethodIndex()));
Vladimir Marko813a8632018-11-29 16:17:01 +0000514 OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100515 declaring_class->GetDexClassDefIndex(),
516 found);
517 if (!(*found)) {
518 return OatFile::OatMethod::Invalid();
519 }
520 return oat_class.GetOatMethod(oat_method_index);
521}
522
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700523bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000524 const DexFile* dex_file = GetDexFile();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700525 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
526 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800527 const dex::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700528 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800529 auto param_len = params != nullptr ? params->GetLength() : 0u;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700530 if (param_len != count) {
531 return false;
532 }
533 auto* cl = Runtime::Current()->GetClassLinker();
534 for (size_t i = 0; i < count; ++i) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000535 dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_;
536 ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700537 if (type == nullptr) {
538 Thread::Current()->AssertPendingException();
539 return false;
540 }
541 if (type != params->GetWithoutChecks(i)) {
542 return false;
543 }
544 }
545 return true;
546}
547
Mathieu Chartier210531f2018-01-12 10:15:51 -0800548ArrayRef<const uint8_t> ArtMethod::GetQuickenedInfo() {
Vladimir Marko813a8632018-11-29 16:17:01 +0000549 const DexFile& dex_file = *GetDexFile();
Andreas Gampeb40d3612018-06-26 15:49:42 -0700550 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Hans Boehm206348c2018-12-05 11:11:33 -0800551 if (oat_dex_file == nullptr) {
Mathieu Chartier210531f2018-01-12 10:15:51 -0800552 return ArrayRef<const uint8_t>();
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100553 }
Hans Boehm206348c2018-12-05 11:11:33 -0800554 return oat_dex_file->GetQuickenedInfoOf(dex_file, GetDexMethodIndex());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100555}
556
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000557uint16_t ArtMethod::GetIndexFromQuickening(uint32_t dex_pc) {
558 ArrayRef<const uint8_t> data = GetQuickenedInfo();
559 if (data.empty()) {
560 return DexFile::kDexNoIndex16;
561 }
562 QuickenInfoTable table(data);
563 uint32_t quicken_index = 0;
564 for (const DexInstructionPcPair& pair : DexInstructions()) {
565 if (pair.DexPc() == dex_pc) {
566 return table.GetData(quicken_index);
567 }
568 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
569 ++quicken_index;
570 }
571 }
572 return DexFile::kDexNoIndex16;
573}
574
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100575const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000576 // Our callers should make sure they don't pass the instrumentation exit pc,
577 // as this method does not look at the side instrumentation stack.
578 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
579
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000580 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100581 return nullptr;
582 }
583
Ulyana Trafimoviche886d682020-07-16 15:09:38 +0000584 if (Runtime::SimulatorMode()) {
585 return nullptr;
586 }
587
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100588 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100589 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700590 CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100591 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100592
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000593 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
594 DCHECK(IsProxyMethod() && !IsConstructor());
595 // The proxy entry point does not have any method header.
596 return nullptr;
597 }
598
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100599 // Check whether the current entry point contains this pc.
Vladimir Marko2196c652017-11-30 16:16:07 +0000600 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
601 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
David Srbecky774232a2019-04-03 17:46:32 +0100602 !class_linker->IsQuickToInterpreterBridge(existing_entry_point) &&
603 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100604 OatQuickMethodHeader* method_header =
605 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100606
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100607 if (method_header->Contains(pc)) {
608 return method_header;
609 }
610 }
611
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000612 if (OatQuickMethodHeader::NterpMethodHeader != nullptr &&
613 OatQuickMethodHeader::NterpMethodHeader->Contains(pc)) {
614 return OatQuickMethodHeader::NterpMethodHeader;
615 }
616
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100617 // Check whether the pc is in the JIT code cache.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100618 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100619 if (jit != nullptr) {
620 jit::JitCodeCache* code_cache = jit->GetCodeCache();
621 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
622 if (method_header != nullptr) {
623 DCHECK(method_header->Contains(pc));
624 return method_header;
625 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000626 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
David Sehr709b0702016-10-13 09:12:37 -0700627 << PrettyMethod()
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000628 << ", pc=" << std::hex << pc
629 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
630 << ", copy=" << std::boolalpha << IsCopied()
631 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100632 }
633 }
634
635 // The code has to be in an oat file.
636 bool found;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100637 OatFile::OatMethod oat_method =
638 FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100639 if (!found) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000640 if (IsNative()) {
641 // We are running the GenericJNI stub. The entrypoint may point
642 // to different entrypoints or to a JIT-compiled JNI stub.
643 DCHECK(class_linker->IsQuickGenericJniStub(existing_entry_point) ||
644 class_linker->IsQuickResolutionStub(existing_entry_point) ||
645 existing_entry_point == GetQuickInstrumentationEntryPoint() ||
646 (jit != nullptr && jit->GetCodeCache()->ContainsPc(existing_entry_point)));
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000647 return nullptr;
648 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100649 // Only for unit tests.
650 // TODO(ngeoffray): Update these tests to pass the right pc?
651 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
652 }
653 const void* oat_entry_point = oat_method.GetQuickCode();
654 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
David Sehr709b0702016-10-13 09:12:37 -0700655 DCHECK(IsNative()) << PrettyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100656 return nullptr;
657 }
658
659 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
660 if (pc == 0) {
661 // This is a downcall, it can only happen for a native method.
662 DCHECK(IsNative());
663 return method_header;
664 }
665
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100666 DCHECK(method_header->Contains(pc))
David Sehr709b0702016-10-13 09:12:37 -0700667 << PrettyMethod()
Roland Levillain0b671c02016-08-19 12:02:34 +0100668 << " " << std::hex << pc << " " << oat_entry_point
Mingyao Yang063fc772016-08-02 11:02:54 -0700669 << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100670 return method_header;
671}
672
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100673const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) {
674 bool found;
675 OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found);
676 if (found) {
677 return oat_method.GetQuickCode();
678 }
679 return nullptr;
680}
681
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000682bool ArtMethod::HasAnyCompiledCode() {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100683 if (IsNative() || !IsInvokable() || IsProxyMethod()) {
684 return false;
685 }
686
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000687 // Check whether the JIT has compiled it.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100688 Runtime* runtime = Runtime::Current();
689 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000690 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
691 return true;
692 }
693
694 // Check whether we have AOT code.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100695 return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr;
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000696}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000697
David Brazdil85865692018-10-30 17:26:20 +0000698void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
699 // Currently we only do intrinsics for static/final methods or methods of final
700 // classes. We don't set kHasSingleImplementation for those methods.
701 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
702 "Potential conflict with kAccSingleImplementation";
703 static const int kAccFlagsShift = CTZ(kAccIntrinsicBits);
704 DCHECK_LE(intrinsic, kAccIntrinsicBits >> kAccFlagsShift);
705 uint32_t intrinsic_bits = intrinsic << kAccFlagsShift;
706 uint32_t new_value = (GetAccessFlags() & ~kAccIntrinsicBits) | kAccIntrinsic | intrinsic_bits;
707 if (kIsDebugBuild) {
708 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
709 bool is_constructor = IsConstructor();
710 bool is_synchronized = IsSynchronized();
711 bool skip_access_checks = SkipAccessChecks();
712 bool is_fast_native = IsFastNative();
713 bool is_critical_native = IsCriticalNative();
714 bool is_copied = IsCopied();
715 bool is_miranda = IsMiranda();
716 bool is_default = IsDefault();
717 bool is_default_conflict = IsDefaultConflicting();
718 bool is_compilable = IsCompilable();
719 bool must_count_locks = MustCountLocks();
David Brazdil31cc2792019-04-10 00:31:15 +0100720 // Recompute flags instead of getting them from the current access flags because
721 // access flags may have been changed to deduplicate warning messages (b/129063331).
722 uint32_t hiddenapi_flags = hiddenapi::CreateRuntimeFlags(this);
David Brazdil85865692018-10-30 17:26:20 +0000723 SetAccessFlags(new_value);
724 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
725 DCHECK_EQ(is_constructor, IsConstructor());
726 DCHECK_EQ(is_synchronized, IsSynchronized());
727 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
728 DCHECK_EQ(is_fast_native, IsFastNative());
729 DCHECK_EQ(is_critical_native, IsCriticalNative());
730 DCHECK_EQ(is_copied, IsCopied());
731 DCHECK_EQ(is_miranda, IsMiranda());
732 DCHECK_EQ(is_default, IsDefault());
733 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
734 DCHECK_EQ(is_compilable, IsCompilable());
735 DCHECK_EQ(must_count_locks, MustCountLocks());
736 // Only DCHECK that we have preserved the hidden API access flags if the
737 // original method was not on the whitelist. This is because the core image
David Brazdil31cc2792019-04-10 00:31:15 +0100738 // does not have the access flags set (b/77733081).
David Brazdil90faceb2018-12-14 14:36:15 +0000739 if ((hiddenapi_flags & kAccHiddenapiBits) != kAccPublicApi) {
David Brazdil85865692018-10-30 17:26:20 +0000740 DCHECK_EQ(hiddenapi_flags, hiddenapi::GetRuntimeFlags(this)) << PrettyMethod();
741 }
742 } else {
743 SetAccessFlags(new_value);
744 }
745}
746
David Brazdil904e75a2018-05-15 13:45:08 +0100747void ArtMethod::SetNotIntrinsic() {
748 if (!IsIntrinsic()) {
749 return;
750 }
751
David Brazdil85865692018-10-30 17:26:20 +0000752 // Read the existing hiddenapi flags.
753 uint32_t hiddenapi_runtime_flags = hiddenapi::GetRuntimeFlags(this);
David Brazdil904e75a2018-05-15 13:45:08 +0100754
755 // Clear intrinsic-related access flags.
756 ClearAccessFlags(kAccIntrinsic | kAccIntrinsicBits);
757
758 // Re-apply hidden API access flags now that the method is not an intrinsic.
David Brazdil85865692018-10-30 17:26:20 +0000759 SetAccessFlags(GetAccessFlags() | hiddenapi_runtime_flags);
760 DCHECK_EQ(hiddenapi_runtime_flags, hiddenapi::GetRuntimeFlags(this));
David Brazdil904e75a2018-05-15 13:45:08 +0100761}
762
Andreas Gampe542451c2016-07-26 09:02:02 -0700763void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000764 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
765 Size(image_pointer_size));
766 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
767
768 // If the entry point of the method we are copying from is from JIT code, we just
Vladimir Marko2196c652017-11-30 16:16:07 +0000769 // put the entry point of the new method to interpreter or GenericJNI. We could set
770 // the entry point to the JIT code, but this would require taking the JIT code cache
771 // lock to notify it, which we do not want at this level.
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000772 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100773 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000774 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000775 SetEntryPointFromQuickCompiledCodePtrSize(
776 src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(),
777 image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000778 }
779 }
780 // Clear the profiling info for the same reasons as the JIT code.
781 if (!src->IsNative()) {
782 SetProfilingInfoPtrSize(nullptr, image_pointer_size);
783 }
784 // Clear hotness to let the JIT properly decide when to compile this method.
785 hotness_count_ = 0;
786}
787
Andreas Gampe542451c2016-07-26 09:02:02 -0700788bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700789 // Hijack this function to get access to PtrSizedFieldsOffset.
790 //
791 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
792 // 64-bit builds.
793 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700794 static_assert(
795 (sizeof(void*) != 4) ||
796 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
797 "Unexpected 32-bit class layout.");
798 static_assert(
799 (sizeof(void*) != 8) ||
800 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
801 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700802
Andreas Gampe75f08852016-07-19 08:06:07 -0700803 Runtime* runtime = Runtime::Current();
804 if (runtime == nullptr) {
805 return true;
806 }
807 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
808}
809
David Sehr709b0702016-10-13 09:12:37 -0700810std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) {
811 if (m == nullptr) {
812 return "null";
813 }
814 return m->PrettyMethod(with_signature);
815}
816
817std::string ArtMethod::PrettyMethod(bool with_signature) {
Vladimir Markob8a55f82017-09-21 16:21:43 +0100818 if (UNLIKELY(IsRuntimeMethod())) {
819 std::string result = GetDeclaringClassDescriptor();
820 result += '.';
821 result += GetName();
822 // Do not add "<no signature>" even if `with_signature` is true.
823 return result;
David Sehr709b0702016-10-13 09:12:37 -0700824 }
Vladimir Markob8a55f82017-09-21 16:21:43 +0100825 ArtMethod* m =
826 GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
Alex Lightcbc9dda2018-12-07 15:38:29 -0800827 std::string res(m->GetDexFile()->PrettyMethod(m->GetDexMethodIndex(), with_signature));
828 if (with_signature && m->IsObsolete()) {
829 return "<OBSOLETE> " + res;
830 } else {
831 return res;
832 }
David Sehr709b0702016-10-13 09:12:37 -0700833}
834
835std::string ArtMethod::JniShortName() {
Alex Light888a59e2017-01-25 11:41:41 -0800836 return GetJniShortName(GetDeclaringClassDescriptor(), GetName());
David Sehr709b0702016-10-13 09:12:37 -0700837}
838
839std::string ArtMethod::JniLongName() {
840 std::string long_name;
841 long_name += JniShortName();
842 long_name += "__";
843
844 std::string signature(GetSignature().ToString());
845 signature.erase(0, 1);
846 signature.erase(signature.begin() + signature.find(')'), signature.end());
847
848 long_name += MangleForJni(signature);
849
850 return long_name;
851}
852
Eric Holkabdb4592019-05-16 08:33:12 -0700853const char* ArtMethod::GetRuntimeMethodName() {
854 Runtime* const runtime = Runtime::Current();
855 if (this == runtime->GetResolutionMethod()) {
856 return "<runtime internal resolution method>";
857 } else if (this == runtime->GetImtConflictMethod()) {
858 return "<runtime internal imt conflict method>";
859 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
860 return "<runtime internal callee-save all registers method>";
861 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
862 return "<runtime internal callee-save reference registers method>";
863 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
864 return "<runtime internal callee-save reference and argument registers method>";
865 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
866 return "<runtime internal save-every-register method>";
867 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
868 return "<runtime internal save-every-register method for clinit>";
869 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
870 return "<runtime internal save-every-register method for suspend check>";
871 } else {
872 return "<unknown runtime internal method>";
873 }
874}
875
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800876// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
877// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
878template <ReadBarrierOption kReadBarrierOption>
879ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
880 NO_THREAD_SAFETY_ANALYSIS {
881 CHECK(method->IsRuntimeMethod() ||
882 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
883 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
884}
885
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800886} // namespace art