blob: f6f8b5f545facd5055824ff5ba69aab2ad46677b [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"
Ulya Trafimovich5439f052020-07-29 10:03:46 +010023
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "arch/context.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "art_method-inl.h"
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000026#include "base/enums.h"
27#include "base/stl_util.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070028#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010029#include "class_root-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070030#include "debugger.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070031#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080032#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file-inl.h"
34#include "dex/dex_file_exception_helpers.h"
35#include "dex/dex_instruction.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080036#include "dex/signature-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070037#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "gc/accounting/card_table-inl.h"
David Brazdil85865692018-10-30 17:26:20 +000039#include "hidden_api.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041#include "jit/jit.h"
42#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010043#include "jit/profiling_info.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010044#include "jni/jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070045#include "mirror/class-inl.h"
Vladimir Markobb206de2019-03-28 10:30:32 +000046#include "mirror/class_ext-inl.h"
Neil Fuller0e844392016-09-08 13:43:31 +010047#include "mirror/executable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070048#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070049#include "mirror/object_array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070050#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000051#include "oat_file-inl.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000052#include "quicken_info.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070053#include "runtime_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070054#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010055#include "vdex_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056
57namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058
Andreas Gampe46ee31b2016-12-14 10:11:49 -080059using android::base::StringPrintf;
60
Ian Rogers0177e532014-02-11 16:30:46 -080061extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
62 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080063extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
64 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080065
Andrew Scull530f09a2020-07-22 11:52:06 +010066// Enforce that we have the right index for runtime methods.
Andreas Gampee2abbc62017-09-15 11:59:26 -070067static_assert(ArtMethod::kRuntimeMethodDexMethodIndex == dex::kDexNoIndex,
Andreas Gampec6ea7d02017-02-01 16:46:28 -080068 "Wrong runtime-method dex method index");
69
Alex Light97e78032017-06-27 17:51:55 -070070ArtMethod* ArtMethod::GetCanonicalMethod(PointerSize pointer_size) {
Alex Lightfd4a79c2019-06-21 08:45:16 -070071 if (LIKELY(!IsCopied())) {
Alex Light97e78032017-06-27 17:51:55 -070072 return this;
73 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010074 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
Vladimir Markoba118822017-06-12 15:41:56 +010075 DCHECK(declaring_class->IsInterface());
Vladimir Marko813a8632018-11-29 16:17:01 +000076 ArtMethod* ret = declaring_class->FindInterfaceMethod(GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +010077 GetDexMethodIndex(),
78 pointer_size);
Alex Light97e78032017-06-27 17:51:55 -070079 DCHECK(ret != nullptr);
80 return ret;
81 }
82}
83
Alex Light4ba388a2017-01-27 10:26:49 -080084ArtMethod* ArtMethod::GetNonObsoleteMethod() {
Alex Light4ba388a2017-01-27 10:26:49 -080085 if (LIKELY(!IsObsolete())) {
86 return this;
Vladimir Marko813a8632018-11-29 16:17:01 +000087 }
88 DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
89 if (IsDirect()) {
Alex Light4ba388a2017-01-27 10:26:49 -080090 return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()];
91 } else {
92 return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize);
93 }
94}
95
Mingyao Yange8fcd012017-01-20 10:43:30 -080096ArtMethod* ArtMethod::GetSingleImplementation(PointerSize pointer_size) {
Nicolas Geoffray8731e702021-04-06 12:11:59 +010097 if (IsInvokable()) {
98 // An invokable method single implementation is itself.
Mingyao Yang063fc772016-08-02 11:02:54 -070099 return this;
100 }
Nicolas Geoffray8731e702021-04-06 12:11:59 +0100101 DCHECK(!IsDefaultConflicting());
102 ArtMethod* m = reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
103 CHECK(m == nullptr || !m->IsDefaultConflicting());
104 return m;
Mingyao Yang063fc772016-08-02 11:02:54 -0700105}
106
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700107ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
108 jobject jlr_method) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700109 ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method);
Neil Fuller0e844392016-09-08 13:43:31 +0100110 DCHECK(executable != nullptr);
111 return executable->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -0700112}
113
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000114ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache() {
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000115 PointerSize pointer_size = kRuntimePointerSize;
Alex Lighta01de592016-11-15 10:43:06 -0800116 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
117 DCHECK(IsObsolete());
118 ObjPtr<mirror::ClassExt> ext(GetDeclaringClass()->GetExtData());
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000119 ObjPtr<mirror::PointerArray> obsolete_methods(ext.IsNull() ? nullptr : ext->GetObsoleteMethods());
120 int32_t len = (obsolete_methods.IsNull() ? 0 : obsolete_methods->GetLength());
121 DCHECK(len == 0 || len == ext->GetObsoleteDexCaches()->GetLength())
122 << "len=" << len << " ext->GetObsoleteDexCaches()=" << ext->GetObsoleteDexCaches();
Alex Light0b772572016-12-02 17:27:31 -0800123 // Using kRuntimePointerSize (instead of using the image's pointer size) is fine since images
124 // should never have obsolete methods in them so they should always be the same.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000125 DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
Alex Lighta01de592016-11-15 10:43:06 -0800126 for (int32_t i = 0; i < len; i++) {
127 if (this == obsolete_methods->GetElementPtrSize<ArtMethod*>(i, pointer_size)) {
128 return ext->GetObsoleteDexCaches()->Get(i);
129 }
130 }
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000131 CHECK(GetDeclaringClass()->IsObsoleteObject())
132 << "This non-structurally obsolete method does not appear in the obsolete map of its class: "
133 << GetDeclaringClass()->PrettyClass() << " Searched " << len << " caches.";
134 CHECK_EQ(this,
135 std::clamp(this,
136 &(*GetDeclaringClass()->GetMethods(pointer_size).begin()),
137 &(*GetDeclaringClass()->GetMethods(pointer_size).end())))
138 << "class is marked as structurally obsolete method but not found in normal obsolete-map "
139 << "despite not being the original method pointer for " << GetDeclaringClass()->PrettyClass();
140 return GetDeclaringClass()->GetDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800141}
142
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000143uint16_t ArtMethod::FindObsoleteDexClassDefIndex() {
144 DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
145 DCHECK(IsObsolete());
146 const DexFile* dex_file = GetDexFile();
147 const dex::TypeIndex declaring_class_type = dex_file->GetMethodId(GetDexMethodIndex()).class_idx_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800148 const dex::ClassDef* class_def = dex_file->FindClassDef(declaring_class_type);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000149 CHECK(class_def != nullptr);
150 return dex_file->GetIndexForClassDef(*class_def);
151}
152
Alex Light9139e002015-10-09 15:59:48 -0700153void ArtMethod::ThrowInvocationTimeError() {
154 DCHECK(!IsInvokable());
Alex Light9139e002015-10-09 15:59:48 -0700155 if (IsDefaultConflicting()) {
156 ThrowIncompatibleClassChangeErrorForMethodConflict(this);
157 } else {
158 DCHECK(IsAbstract());
159 ThrowAbstractMethodError(this);
160 }
161}
162
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 // TODO: kSuper?
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000165 if (IsStatic()) {
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +0000166 return kStatic;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000167 } else if (GetDeclaringClass()->IsInterface()) {
168 return kInterface;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 } else if (IsDirect()) {
170 return kDirect;
Orion Hodson81daf3d2020-09-03 14:01:51 +0100171 } else if (IsSignaturePolymorphic()) {
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100172 return kPolymorphic;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 } else {
174 return kVirtual;
175 }
176}
177
Vladimir Marko6e781582019-02-04 10:58:06 +0000178size_t ArtMethod::NumArgRegisters(const char* shorty) {
179 CHECK_NE(shorty[0], '\0');
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 uint32_t num_registers = 0;
Vladimir Marko6e781582019-02-04 10:58:06 +0000181 for (const char* s = shorty + 1; *s != '\0'; ++s) {
182 if (*s == 'D' || *s == 'J') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 num_registers += 2;
184 } else {
185 num_registers += 1;
186 }
187 }
188 return num_registers;
189}
190
Alex Light6c8467f2015-11-20 15:03:26 -0800191bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700192 ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature");
Alex Light6c8467f2015-11-20 15:03:26 -0800193 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800194 const dex::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
Alex Light6c8467f2015-11-20 15:03:26 -0800195 if (GetDexCache() == other->GetDexCache()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800196 const dex::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800197 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
198 }
Alex Light6c8467f2015-11-20 15:03:26 -0800199 const DexFile* dex_file2 = other->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800200 const dex::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
Andreas Gampeb8bca302019-01-02 10:28:38 -0800201 if (!DexFile::StringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
Ian Rogersf2247512014-12-02 16:17:08 -0800202 return false; // Name mismatch.
203 }
204 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
205}
206
Andreas Gampe542451c2016-07-26 09:02:02 -0700207ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800208 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800209 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 }
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100211 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
212 ObjPtr<mirror::Class> super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800214 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 // Did this method override a super class method? If so load the result from the super class'
216 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700217 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 } else {
220 // Method didn't override superclass method so search interfaces
221 if (IsProxyMethod()) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100222 result = GetInterfaceMethodIfProxy(pointer_size);
223 DCHECK(result != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 } else {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000225 ObjPtr<mirror::IfTable> iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800226 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100227 ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
Alex Light51a64d52015-12-17 13:55:59 -0800228 for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
229 if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
230 result = &interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 break;
232 }
233 }
234 }
235 }
236 }
Alex Light6c8467f2015-11-20 15:03:26 -0800237 DCHECK(result == nullptr ||
Alex Light51a64d52015-12-17 13:55:59 -0800238 GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
239 result->GetInterfaceMethodIfProxy(pointer_size)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 return result;
241}
242
Ian Rogerse0a02da2014-12-02 14:10:53 -0800243uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
244 uint32_t name_and_signature_idx) {
245 const DexFile* dexfile = GetDexFile();
246 const uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800247 const dex::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
248 const dex::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800249 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
250 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
251 if (dexfile == &other_dexfile) {
252 return dex_method_idx;
253 }
254 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800255 const dex::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700256 if (other_type_id != nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800257 const dex::MethodId* other_mid = other_dexfile.FindMethodId(
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700258 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
259 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
260 if (other_mid != nullptr) {
261 return other_dexfile.GetIndexForMethodId(*other_mid);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800262 }
263 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700264 return dex::kDexNoIndex;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800265}
266
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700268 uint32_t dex_pc, bool* has_no_move_exception) {
Jeff Haoaa961912014-04-22 13:54:32 -0700269 // Set aside the exception while we resolve its type.
270 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700271 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000272 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700273 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700274 // Default to handler not found.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700275 uint32_t found_dex_pc = dex::kDexNoIndex;
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700276 // Iterate over the catch handlers associated with dex_pc.
David Sehr0225f8e2018-01-31 08:52:24 +0000277 CodeItemDataAccessor accessor(DexInstructionData());
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800278 for (CatchHandlerIterator it(accessor, dex_pc); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800279 dex::TypeIndex iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 // Catch all case
Andreas Gampea5b09a62016-11-17 15:21:22 -0800281 if (!iter_type_idx.IsValid()) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700282 found_dex_pc = it.GetHandlerAddress();
283 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 }
285 // Does this catch exception type apply?
Vladimir Markob45528c2017-07-27 14:14:28 +0100286 ObjPtr<mirror::Class> iter_exception_type = ResolveClassFromTypeIndex(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700287 if (UNLIKELY(iter_exception_type == nullptr)) {
288 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
289 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700290 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700291 self->ClearException();
292 // Delete any long jump context as this routine is called during a stack walk which will
293 // release its in use context at the end.
294 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700297 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700298 found_dex_pc = it.GetHandlerAddress();
299 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 }
301 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700302 if (found_dex_pc != dex::kDexNoIndex) {
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800303 const Instruction& first_catch_instr = accessor.InstructionAt(found_dex_pc);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800304 *has_no_move_exception = (first_catch_instr.Opcode() != Instruction::MOVE_EXCEPTION);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700305 }
Jeff Haoaa961912014-04-22 13:54:32 -0700306 // Put the exception back.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800307 if (exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000308 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700309 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700310 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311}
312
Brian Carlstromea46f952013-07-30 01:26:50 -0700313void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800314 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700315 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
316 ThrowStackOverflowError(self);
317 return;
318 }
319
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 if (kIsDebugBuild) {
321 self->AssertThreadSuspensionIsAllowable();
Vladimir Markoddf4fd32021-11-22 16:31:57 +0000322 CHECK_EQ(ThreadState::kRunnable, self->GetState());
Andreas Gampe542451c2016-07-26 09:02:02 -0700323 CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 }
325
326 // Push a transition back into managed code onto the linked list in thread.
327 ManagedStack fragment;
328 self->PushManagedStackFragment(&fragment);
329
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700331 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200332 // If the runtime is not yet started or it is required by the debugger, then perform the
Aart Bik01223202016-05-05 15:10:42 -0700333 // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
334 // cycling around the various JIT/Interpreter methods that handle method invocation.
Alex Light3dacdd62019-03-12 15:45:47 +0000335 if (UNLIKELY(!runtime->IsStarted() ||
Alex Lightfc588092020-01-23 15:39:08 -0800336 (self->IsForceInterpreter() && !IsNative() && !IsProxyMethod() && IsInvokable()))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700337 if (IsStatic()) {
Aart Bik01223202016-05-05 15:10:42 -0700338 art::interpreter::EnterInterpreterFromInvoke(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700339 self, this, nullptr, args, result, /*stay_in_interpreter=*/ true);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700340 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 mirror::Object* receiver =
342 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Aart Bik01223202016-05-05 15:10:42 -0700343 art::interpreter::EnterInterpreterFromInvoke(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700344 self, this, receiver, args + 1, result, /*stay_in_interpreter=*/ true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 }
346 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700347 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348
349 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800351 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700352 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 LOG(INFO) << StringPrintf(
David Sehr709b0702016-10-13 09:12:37 -0700354 "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700356 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700357
Elliott Hughes956af0f2014-12-11 14:34:28 -0800358 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100359 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100360 CHECK(!runtime->UseJitCompilation());
Alex Lightdb01a092017-04-03 15:39:55 -0700361 const void* oat_quick_code =
362 (IsNative() || !IsInvokable() || IsProxyMethod() || IsObsolete())
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100363 ? nullptr
364 : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100365 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
David Sehr709b0702016-10-13 09:12:37 -0700366 << "Don't call compiled code when -Xint " << PrettyMethod();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700367 }
368
Ulya Trafimovich5439f052020-07-29 10:03:46 +0100369 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800370 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800371 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800372 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800373 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000374 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200375 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700376 // exception was thrown to force the activations to be removed from the
377 // stack. Continue execution in the interpreter.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000378 self->DeoptimizeWithDeoptimizationException(result);
Jeff Hao790ad902013-05-22 15:02:08 -0700379 }
380 if (kLogInvocationStartAndReturn) {
David Sehr709b0702016-10-13 09:12:37 -0700381 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(),
Elliott Hughes956af0f2014-12-11 14:34:28 -0800382 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800383 }
384 } else {
David Sehr709b0702016-10-13 09:12:37 -0700385 LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800386 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800387 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 }
389 }
390 }
391
392 // Pop transition.
393 self->PopManagedStackFragment(fragment);
394}
395
Orion Hodson81daf3d2020-09-03 14:01:51 +0100396bool ArtMethod::IsSignaturePolymorphic() {
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000397 // Methods with a polymorphic signature have constraints that they
398 // are native and varargs and belong to either MethodHandle or VarHandle.
399 if (!IsNative() || !IsVarargs()) {
400 return false;
401 }
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100402 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
403 Runtime::Current()->GetClassLinker()->GetClassRoots();
404 ObjPtr<mirror::Class> cls = GetDeclaringClass();
405 return (cls == GetClassRoot<mirror::MethodHandle>(class_roots) ||
406 cls == GetClassRoot<mirror::VarHandle>(class_roots));
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000407}
408
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100409static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file,
410 uint16_t class_def_idx,
411 uint32_t method_idx) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700412 ClassAccessor accessor(dex_file, class_def_idx);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700413 uint32_t class_def_method_index = 0u;
414 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
415 if (method.GetIndex() == method_idx) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100416 return class_def_method_index;
417 }
418 class_def_method_index++;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100419 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100420 LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
421 UNREACHABLE();
422}
423
Alex Lighteee0bd42017-02-14 15:31:45 +0000424// We use the method's DexFile and declaring class name to find the OatMethod for an obsolete
425// method. This is extremely slow but we need it if we want to be able to have obsolete native
426// methods since we need this to find the size of its stack frames.
427//
428// NB We could (potentially) do this differently and rely on the way the transformation is applied
429// in order to use the entrypoint to find this information. However, for debugging reasons (most
430// notably making sure that new invokes of obsolete methods fail) we choose to instead get the data
431// directly from the dex file.
432static const OatFile::OatMethod FindOatMethodFromDexFileFor(ArtMethod* method, bool* found)
433 REQUIRES_SHARED(Locks::mutator_lock_) {
434 DCHECK(method->IsObsolete() && method->IsNative());
435 const DexFile* dex_file = method->GetDexFile();
436
437 // recreate the class_def_index from the descriptor.
438 std::string descriptor_storage;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800439 const dex::TypeId* declaring_class_type_id =
Alex Lighteee0bd42017-02-14 15:31:45 +0000440 dex_file->FindTypeId(method->GetDeclaringClass()->GetDescriptor(&descriptor_storage));
441 CHECK(declaring_class_type_id != nullptr);
442 dex::TypeIndex declaring_class_type_index = dex_file->GetIndexForTypeId(*declaring_class_type_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800443 const dex::ClassDef* declaring_class_type_def =
Alex Lighteee0bd42017-02-14 15:31:45 +0000444 dex_file->FindClassDef(declaring_class_type_index);
445 CHECK(declaring_class_type_def != nullptr);
446 uint16_t declaring_class_def_index = dex_file->GetIndexForClassDef(*declaring_class_type_def);
447
448 size_t oat_method_index = GetOatMethodIndexFromMethodIndex(*dex_file,
449 declaring_class_def_index,
450 method->GetDexMethodIndex());
451
452 OatFile::OatClass oat_class = OatFile::FindOatClass(*dex_file,
453 declaring_class_def_index,
454 found);
455 if (!(*found)) {
456 return OatFile::OatMethod::Invalid();
457 }
458 return oat_class.GetOatMethod(oat_method_index);
459}
460
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100461static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method,
462 PointerSize pointer_size,
463 bool* found)
464 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000465 if (UNLIKELY(method->IsObsolete())) {
466 // We shouldn't be calling this with obsolete methods except for native obsolete methods for
467 // which we need to use the oat method to figure out how large the quick frame is.
468 DCHECK(method->IsNative()) << "We should only be finding the OatMethod of obsolete methods in "
469 << "order to allow stack walking. Other obsolete methods should "
470 << "never need to access this information.";
471 DCHECK_EQ(pointer_size, kRuntimePointerSize) << "Obsolete method in compiler!";
472 return FindOatMethodFromDexFileFor(method, found);
473 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100474 // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
475 // method for direct methods (or virtual methods made direct).
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100476 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100477 size_t oat_method_index;
478 if (method->IsStatic() || method->IsDirect()) {
479 // Simple case where the oat method index was stashed at load time.
480 oat_method_index = method->GetMethodIndex();
481 } else {
482 // Compute the oat_method_index by search for its position in the declared virtual methods.
483 oat_method_index = declaring_class->NumDirectMethods();
484 bool found_virtual = false;
485 for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) {
486 // Check method index instead of identity in case of duplicate method definitions.
487 if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) {
488 found_virtual = true;
489 break;
490 }
491 oat_method_index++;
492 }
493 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
David Sehr709b0702016-10-13 09:12:37 -0700494 << method->PrettyMethod();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100495 }
496 DCHECK_EQ(oat_method_index,
Vladimir Marko813a8632018-11-29 16:17:01 +0000497 GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100498 method->GetDeclaringClass()->GetDexClassDefIndex(),
499 method->GetDexMethodIndex()));
Vladimir Marko813a8632018-11-29 16:17:01 +0000500 OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100501 declaring_class->GetDexClassDefIndex(),
502 found);
503 if (!(*found)) {
504 return OatFile::OatMethod::Invalid();
505 }
506 return oat_class.GetOatMethod(oat_method_index);
507}
508
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700509bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000510 const DexFile* dex_file = GetDexFile();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700511 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
512 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800513 const dex::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700514 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800515 auto param_len = params != nullptr ? params->GetLength() : 0u;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700516 if (param_len != count) {
517 return false;
518 }
519 auto* cl = Runtime::Current()->GetClassLinker();
520 for (size_t i = 0; i < count; ++i) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000521 dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_;
522 ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700523 if (type == nullptr) {
524 Thread::Current()->AssertPendingException();
525 return false;
526 }
527 if (type != params->GetWithoutChecks(i)) {
528 return false;
529 }
530 }
531 return true;
532}
533
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100534const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000535 // Our callers should make sure they don't pass the instrumentation exit pc,
536 // as this method does not look at the side instrumentation stack.
537 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
538
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000539 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100540 return nullptr;
541 }
542
543 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100544 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700545 CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100546 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100547
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000548 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
549 DCHECK(IsProxyMethod() && !IsConstructor());
550 // The proxy entry point does not have any method header.
551 return nullptr;
552 }
553
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100554 // Check whether the current entry point contains this pc.
Vladimir Marko2196c652017-11-30 16:16:07 +0000555 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
556 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
David Srbecky774232a2019-04-03 17:46:32 +0100557 !class_linker->IsQuickToInterpreterBridge(existing_entry_point) &&
Nicolas Geoffray248d5c42021-09-13 09:53:10 +0100558 existing_entry_point != GetQuickInstrumentationEntryPoint() &&
559 existing_entry_point != GetInvokeObsoleteMethodStub()) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100560 OatQuickMethodHeader* method_header =
561 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100562
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100563 if (method_header->Contains(pc)) {
564 return method_header;
565 }
566 }
567
Nicolas Geoffray409d1db2021-11-12 16:27:10 +0000568 if (OatQuickMethodHeader::IsNterpPc(pc)) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000569 return OatQuickMethodHeader::NterpMethodHeader;
570 }
571
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100572 // Check whether the pc is in the JIT code cache.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100573 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100574 if (jit != nullptr) {
575 jit::JitCodeCache* code_cache = jit->GetCodeCache();
576 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
577 if (method_header != nullptr) {
578 DCHECK(method_header->Contains(pc));
579 return method_header;
580 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000581 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
David Sehr709b0702016-10-13 09:12:37 -0700582 << PrettyMethod()
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000583 << ", pc=" << std::hex << pc
584 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
585 << ", copy=" << std::boolalpha << IsCopied()
586 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100587 }
588 }
589
590 // The code has to be in an oat file.
591 bool found;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100592 OatFile::OatMethod oat_method =
593 FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100594 if (!found) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000595 if (IsNative()) {
596 // We are running the GenericJNI stub. The entrypoint may point
597 // to different entrypoints or to a JIT-compiled JNI stub.
598 DCHECK(class_linker->IsQuickGenericJniStub(existing_entry_point) ||
599 class_linker->IsQuickResolutionStub(existing_entry_point) ||
600 existing_entry_point == GetQuickInstrumentationEntryPoint() ||
Vladimir Markoe77141a2021-01-29 16:44:55 +0000601 (jit != nullptr && jit->GetCodeCache()->ContainsPc(existing_entry_point)))
602 << " entrypoint: " << existing_entry_point
603 << " size: " << OatQuickMethodHeader::FromEntryPoint(existing_entry_point)->GetCodeSize()
604 << " pc: " << reinterpret_cast<const void*>(pc);
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000605 return nullptr;
606 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100607 // Only for unit tests.
608 // TODO(ngeoffray): Update these tests to pass the right pc?
609 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
610 }
611 const void* oat_entry_point = oat_method.GetQuickCode();
612 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
David Sehr709b0702016-10-13 09:12:37 -0700613 DCHECK(IsNative()) << PrettyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100614 return nullptr;
615 }
616
617 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
618 if (pc == 0) {
619 // This is a downcall, it can only happen for a native method.
620 DCHECK(IsNative());
621 return method_header;
622 }
623
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100624 DCHECK(method_header->Contains(pc))
David Sehr709b0702016-10-13 09:12:37 -0700625 << PrettyMethod()
Roland Levillain0b671c02016-08-19 12:02:34 +0100626 << " " << std::hex << pc << " " << oat_entry_point
Mingyao Yang063fc772016-08-02 11:02:54 -0700627 << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100628 return method_header;
629}
630
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100631const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) {
632 bool found;
633 OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found);
634 if (found) {
635 return oat_method.GetQuickCode();
636 }
637 return nullptr;
638}
639
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000640bool ArtMethod::HasAnyCompiledCode() {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100641 if (IsNative() || !IsInvokable() || IsProxyMethod()) {
642 return false;
643 }
644
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000645 // Check whether the JIT has compiled it.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100646 Runtime* runtime = Runtime::Current();
647 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000648 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
649 return true;
650 }
651
652 // Check whether we have AOT code.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100653 return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr;
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000654}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000655
David Brazdil85865692018-10-30 17:26:20 +0000656void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
657 // Currently we only do intrinsics for static/final methods or methods of final
658 // classes. We don't set kHasSingleImplementation for those methods.
659 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
660 "Potential conflict with kAccSingleImplementation";
661 static const int kAccFlagsShift = CTZ(kAccIntrinsicBits);
662 DCHECK_LE(intrinsic, kAccIntrinsicBits >> kAccFlagsShift);
663 uint32_t intrinsic_bits = intrinsic << kAccFlagsShift;
664 uint32_t new_value = (GetAccessFlags() & ~kAccIntrinsicBits) | kAccIntrinsic | intrinsic_bits;
665 if (kIsDebugBuild) {
666 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
667 bool is_constructor = IsConstructor();
668 bool is_synchronized = IsSynchronized();
669 bool skip_access_checks = SkipAccessChecks();
670 bool is_fast_native = IsFastNative();
671 bool is_critical_native = IsCriticalNative();
672 bool is_copied = IsCopied();
673 bool is_miranda = IsMiranda();
674 bool is_default = IsDefault();
675 bool is_default_conflict = IsDefaultConflicting();
676 bool is_compilable = IsCompilable();
677 bool must_count_locks = MustCountLocks();
David Brazdil31cc2792019-04-10 00:31:15 +0100678 // Recompute flags instead of getting them from the current access flags because
679 // access flags may have been changed to deduplicate warning messages (b/129063331).
680 uint32_t hiddenapi_flags = hiddenapi::CreateRuntimeFlags(this);
David Brazdil85865692018-10-30 17:26:20 +0000681 SetAccessFlags(new_value);
682 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
683 DCHECK_EQ(is_constructor, IsConstructor());
684 DCHECK_EQ(is_synchronized, IsSynchronized());
685 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
686 DCHECK_EQ(is_fast_native, IsFastNative());
687 DCHECK_EQ(is_critical_native, IsCriticalNative());
688 DCHECK_EQ(is_copied, IsCopied());
689 DCHECK_EQ(is_miranda, IsMiranda());
690 DCHECK_EQ(is_default, IsDefault());
691 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
692 DCHECK_EQ(is_compilable, IsCompilable());
693 DCHECK_EQ(must_count_locks, MustCountLocks());
694 // Only DCHECK that we have preserved the hidden API access flags if the
Orion Hodson611d71c2021-08-27 17:00:10 +0100695 // original method was not in the SDK list. This is because the core image
David Brazdil31cc2792019-04-10 00:31:15 +0100696 // does not have the access flags set (b/77733081).
David Brazdil90faceb2018-12-14 14:36:15 +0000697 if ((hiddenapi_flags & kAccHiddenapiBits) != kAccPublicApi) {
David Brazdil85865692018-10-30 17:26:20 +0000698 DCHECK_EQ(hiddenapi_flags, hiddenapi::GetRuntimeFlags(this)) << PrettyMethod();
699 }
700 } else {
701 SetAccessFlags(new_value);
702 }
703}
704
David Brazdil904e75a2018-05-15 13:45:08 +0100705void ArtMethod::SetNotIntrinsic() {
706 if (!IsIntrinsic()) {
707 return;
708 }
709
David Brazdil85865692018-10-30 17:26:20 +0000710 // Read the existing hiddenapi flags.
711 uint32_t hiddenapi_runtime_flags = hiddenapi::GetRuntimeFlags(this);
David Brazdil904e75a2018-05-15 13:45:08 +0100712
713 // Clear intrinsic-related access flags.
714 ClearAccessFlags(kAccIntrinsic | kAccIntrinsicBits);
715
716 // Re-apply hidden API access flags now that the method is not an intrinsic.
David Brazdil85865692018-10-30 17:26:20 +0000717 SetAccessFlags(GetAccessFlags() | hiddenapi_runtime_flags);
718 DCHECK_EQ(hiddenapi_runtime_flags, hiddenapi::GetRuntimeFlags(this));
David Brazdil904e75a2018-05-15 13:45:08 +0100719}
720
Andreas Gampe542451c2016-07-26 09:02:02 -0700721void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000722 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
723 Size(image_pointer_size));
724 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
725
726 // If the entry point of the method we are copying from is from JIT code, we just
Vladimir Marko2196c652017-11-30 16:16:07 +0000727 // put the entry point of the new method to interpreter or GenericJNI. We could set
728 // the entry point to the JIT code, but this would require taking the JIT code cache
729 // lock to notify it, which we do not want at this level.
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000730 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100731 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000732 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000733 SetEntryPointFromQuickCompiledCodePtrSize(
734 src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(),
735 image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000736 }
737 }
Nicolas Geoffray8731e702021-04-06 12:11:59 +0100738 if (interpreter::IsNterpSupported() &&
739 (GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size) ==
740 interpreter::GetNterpEntryPoint())) {
Nicolas Geoffrayab7ecce2020-07-29 13:30:24 +0100741 // If the entrypoint is nterp, it's too early to check if the new method
742 // will support it. So for simplicity, use the interpreter bridge.
743 SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size);
744 }
745
746 // Clear the data pointer, it will be set if needed by the caller.
Nicolas Geoffray47171752020-08-31 15:03:20 +0100747 if (!src->HasCodeItem() && !src->IsNative()) {
Nicolas Geoffrayab7ecce2020-07-29 13:30:24 +0100748 SetDataPtrSize(nullptr, image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000749 }
750 // Clear hotness to let the JIT properly decide when to compile this method.
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000751 ResetCounter(runtime->GetJITOptions()->GetWarmupThreshold());
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000752}
753
Andreas Gampe542451c2016-07-26 09:02:02 -0700754bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700755 // Hijack this function to get access to PtrSizedFieldsOffset.
756 //
757 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
758 // 64-bit builds.
759 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700760 static_assert(
761 (sizeof(void*) != 4) ||
762 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
763 "Unexpected 32-bit class layout.");
764 static_assert(
765 (sizeof(void*) != 8) ||
766 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
767 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700768
Andreas Gampe75f08852016-07-19 08:06:07 -0700769 Runtime* runtime = Runtime::Current();
770 if (runtime == nullptr) {
771 return true;
772 }
773 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
774}
775
David Sehr709b0702016-10-13 09:12:37 -0700776std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) {
777 if (m == nullptr) {
778 return "null";
779 }
780 return m->PrettyMethod(with_signature);
781}
782
783std::string ArtMethod::PrettyMethod(bool with_signature) {
Vladimir Markob8a55f82017-09-21 16:21:43 +0100784 if (UNLIKELY(IsRuntimeMethod())) {
785 std::string result = GetDeclaringClassDescriptor();
786 result += '.';
787 result += GetName();
788 // Do not add "<no signature>" even if `with_signature` is true.
789 return result;
David Sehr709b0702016-10-13 09:12:37 -0700790 }
Vladimir Markob8a55f82017-09-21 16:21:43 +0100791 ArtMethod* m =
792 GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
Alex Lightcbc9dda2018-12-07 15:38:29 -0800793 std::string res(m->GetDexFile()->PrettyMethod(m->GetDexMethodIndex(), with_signature));
794 if (with_signature && m->IsObsolete()) {
795 return "<OBSOLETE> " + res;
796 } else {
797 return res;
798 }
David Sehr709b0702016-10-13 09:12:37 -0700799}
800
801std::string ArtMethod::JniShortName() {
Alex Light888a59e2017-01-25 11:41:41 -0800802 return GetJniShortName(GetDeclaringClassDescriptor(), GetName());
David Sehr709b0702016-10-13 09:12:37 -0700803}
804
805std::string ArtMethod::JniLongName() {
806 std::string long_name;
807 long_name += JniShortName();
808 long_name += "__";
809
810 std::string signature(GetSignature().ToString());
811 signature.erase(0, 1);
812 signature.erase(signature.begin() + signature.find(')'), signature.end());
813
814 long_name += MangleForJni(signature);
815
816 return long_name;
817}
818
Eric Holkabdb4592019-05-16 08:33:12 -0700819const char* ArtMethod::GetRuntimeMethodName() {
820 Runtime* const runtime = Runtime::Current();
821 if (this == runtime->GetResolutionMethod()) {
822 return "<runtime internal resolution method>";
823 } else if (this == runtime->GetImtConflictMethod()) {
824 return "<runtime internal imt conflict method>";
825 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
826 return "<runtime internal callee-save all registers method>";
827 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
828 return "<runtime internal callee-save reference registers method>";
829 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
830 return "<runtime internal callee-save reference and argument registers method>";
831 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
832 return "<runtime internal save-every-register method>";
833 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
834 return "<runtime internal save-every-register method for clinit>";
835 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
836 return "<runtime internal save-every-register method for suspend check>";
837 } else {
838 return "<unknown runtime internal method>";
839 }
840}
841
zhaoxuyang7156ea22022-01-10 13:58:11 +0800842void ArtMethod::SetCodeItem(const dex::CodeItem* code_item, bool is_compact_dex_code_item) {
Nicolas Geoffraye1d2dce2020-09-21 10:06:31 +0100843 DCHECK(HasCodeItem());
844 // We mark the lowest bit for the interpreter to know whether it's executing a
845 // method in a compact or standard dex file.
846 uintptr_t data =
zhaoxuyang7156ea22022-01-10 13:58:11 +0800847 reinterpret_cast<uintptr_t>(code_item) | (is_compact_dex_code_item ? 1 : 0);
Nicolas Geoffraye1d2dce2020-09-21 10:06:31 +0100848 SetDataPtrSize(reinterpret_cast<void*>(data), kRuntimePointerSize);
849}
850
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800851// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
852// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
853template <ReadBarrierOption kReadBarrierOption>
854ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
855 NO_THREAD_SAFETY_ANALYSIS {
856 CHECK(method->IsRuntimeMethod() ||
857 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
858 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
859}
860
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800861} // namespace art