blob: 2d7129abad1586824c9d2802244271ebdbd7b58b [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();
322 CHECK_EQ(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
Alex Light9139e002015-10-09 15:59:48 -0700396bool ArtMethod::IsOverridableByDefaultMethod() {
397 return GetDeclaringClass()->IsInterface();
398}
399
Orion Hodson81daf3d2020-09-03 14:01:51 +0100400bool ArtMethod::IsSignaturePolymorphic() {
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000401 // Methods with a polymorphic signature have constraints that they
402 // are native and varargs and belong to either MethodHandle or VarHandle.
403 if (!IsNative() || !IsVarargs()) {
404 return false;
405 }
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100406 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
407 Runtime::Current()->GetClassLinker()->GetClassRoots();
408 ObjPtr<mirror::Class> cls = GetDeclaringClass();
409 return (cls == GetClassRoot<mirror::MethodHandle>(class_roots) ||
410 cls == GetClassRoot<mirror::VarHandle>(class_roots));
Orion Hodsoneb4d19b2017-11-06 15:49:23 +0000411}
412
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100413static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file,
414 uint16_t class_def_idx,
415 uint32_t method_idx) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700416 ClassAccessor accessor(dex_file, class_def_idx);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700417 uint32_t class_def_method_index = 0u;
418 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
419 if (method.GetIndex() == method_idx) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100420 return class_def_method_index;
421 }
422 class_def_method_index++;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100423 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100424 LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
425 UNREACHABLE();
426}
427
Alex Lighteee0bd42017-02-14 15:31:45 +0000428// We use the method's DexFile and declaring class name to find the OatMethod for an obsolete
429// method. This is extremely slow but we need it if we want to be able to have obsolete native
430// methods since we need this to find the size of its stack frames.
431//
432// NB We could (potentially) do this differently and rely on the way the transformation is applied
433// in order to use the entrypoint to find this information. However, for debugging reasons (most
434// notably making sure that new invokes of obsolete methods fail) we choose to instead get the data
435// directly from the dex file.
436static const OatFile::OatMethod FindOatMethodFromDexFileFor(ArtMethod* method, bool* found)
437 REQUIRES_SHARED(Locks::mutator_lock_) {
438 DCHECK(method->IsObsolete() && method->IsNative());
439 const DexFile* dex_file = method->GetDexFile();
440
441 // recreate the class_def_index from the descriptor.
442 std::string descriptor_storage;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800443 const dex::TypeId* declaring_class_type_id =
Alex Lighteee0bd42017-02-14 15:31:45 +0000444 dex_file->FindTypeId(method->GetDeclaringClass()->GetDescriptor(&descriptor_storage));
445 CHECK(declaring_class_type_id != nullptr);
446 dex::TypeIndex declaring_class_type_index = dex_file->GetIndexForTypeId(*declaring_class_type_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800447 const dex::ClassDef* declaring_class_type_def =
Alex Lighteee0bd42017-02-14 15:31:45 +0000448 dex_file->FindClassDef(declaring_class_type_index);
449 CHECK(declaring_class_type_def != nullptr);
450 uint16_t declaring_class_def_index = dex_file->GetIndexForClassDef(*declaring_class_type_def);
451
452 size_t oat_method_index = GetOatMethodIndexFromMethodIndex(*dex_file,
453 declaring_class_def_index,
454 method->GetDexMethodIndex());
455
456 OatFile::OatClass oat_class = OatFile::FindOatClass(*dex_file,
457 declaring_class_def_index,
458 found);
459 if (!(*found)) {
460 return OatFile::OatMethod::Invalid();
461 }
462 return oat_class.GetOatMethod(oat_method_index);
463}
464
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100465static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method,
466 PointerSize pointer_size,
467 bool* found)
468 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lighteee0bd42017-02-14 15:31:45 +0000469 if (UNLIKELY(method->IsObsolete())) {
470 // We shouldn't be calling this with obsolete methods except for native obsolete methods for
471 // which we need to use the oat method to figure out how large the quick frame is.
472 DCHECK(method->IsNative()) << "We should only be finding the OatMethod of obsolete methods in "
473 << "order to allow stack walking. Other obsolete methods should "
474 << "never need to access this information.";
475 DCHECK_EQ(pointer_size, kRuntimePointerSize) << "Obsolete method in compiler!";
476 return FindOatMethodFromDexFileFor(method, found);
477 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100478 // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
479 // method for direct methods (or virtual methods made direct).
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100480 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100481 size_t oat_method_index;
482 if (method->IsStatic() || method->IsDirect()) {
483 // Simple case where the oat method index was stashed at load time.
484 oat_method_index = method->GetMethodIndex();
485 } else {
486 // Compute the oat_method_index by search for its position in the declared virtual methods.
487 oat_method_index = declaring_class->NumDirectMethods();
488 bool found_virtual = false;
489 for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) {
490 // Check method index instead of identity in case of duplicate method definitions.
491 if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) {
492 found_virtual = true;
493 break;
494 }
495 oat_method_index++;
496 }
497 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
David Sehr709b0702016-10-13 09:12:37 -0700498 << method->PrettyMethod();
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100499 }
500 DCHECK_EQ(oat_method_index,
Vladimir Marko813a8632018-11-29 16:17:01 +0000501 GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100502 method->GetDeclaringClass()->GetDexClassDefIndex(),
503 method->GetDexMethodIndex()));
Vladimir Marko813a8632018-11-29 16:17:01 +0000504 OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100505 declaring_class->GetDexClassDefIndex(),
506 found);
507 if (!(*found)) {
508 return OatFile::OatMethod::Invalid();
509 }
510 return oat_class.GetOatMethod(oat_method_index);
511}
512
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700513bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000514 const DexFile* dex_file = GetDexFile();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700515 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
516 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800517 const dex::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700518 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800519 auto param_len = params != nullptr ? params->GetLength() : 0u;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700520 if (param_len != count) {
521 return false;
522 }
523 auto* cl = Runtime::Current()->GetClassLinker();
524 for (size_t i = 0; i < count; ++i) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000525 dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_;
526 ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700527 if (type == nullptr) {
528 Thread::Current()->AssertPendingException();
529 return false;
530 }
531 if (type != params->GetWithoutChecks(i)) {
532 return false;
533 }
534 }
535 return true;
536}
537
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100538const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000539 // Our callers should make sure they don't pass the instrumentation exit pc,
540 // as this method does not look at the side instrumentation stack.
541 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
542
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000543 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100544 return nullptr;
545 }
546
547 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100548 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700549 CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100550 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100551
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000552 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
553 DCHECK(IsProxyMethod() && !IsConstructor());
554 // The proxy entry point does not have any method header.
555 return nullptr;
556 }
557
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100558 // Check whether the current entry point contains this pc.
Vladimir Marko2196c652017-11-30 16:16:07 +0000559 if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
560 !class_linker->IsQuickResolutionStub(existing_entry_point) &&
David Srbecky774232a2019-04-03 17:46:32 +0100561 !class_linker->IsQuickToInterpreterBridge(existing_entry_point) &&
562 existing_entry_point != GetQuickInstrumentationEntryPoint()) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100563 OatQuickMethodHeader* method_header =
564 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100565
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100566 if (method_header->Contains(pc)) {
567 return method_header;
568 }
569 }
570
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000571 if (OatQuickMethodHeader::NterpMethodHeader != nullptr &&
572 OatQuickMethodHeader::NterpMethodHeader->Contains(pc)) {
573 return OatQuickMethodHeader::NterpMethodHeader;
574 }
575
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100576 // Check whether the pc is in the JIT code cache.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100577 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100578 if (jit != nullptr) {
579 jit::JitCodeCache* code_cache = jit->GetCodeCache();
580 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
581 if (method_header != nullptr) {
582 DCHECK(method_header->Contains(pc));
583 return method_header;
584 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000585 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
David Sehr709b0702016-10-13 09:12:37 -0700586 << PrettyMethod()
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000587 << ", pc=" << std::hex << pc
588 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
589 << ", copy=" << std::boolalpha << IsCopied()
590 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100591 }
592 }
593
594 // The code has to be in an oat file.
595 bool found;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100596 OatFile::OatMethod oat_method =
597 FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100598 if (!found) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000599 if (IsNative()) {
600 // We are running the GenericJNI stub. The entrypoint may point
601 // to different entrypoints or to a JIT-compiled JNI stub.
602 DCHECK(class_linker->IsQuickGenericJniStub(existing_entry_point) ||
603 class_linker->IsQuickResolutionStub(existing_entry_point) ||
604 existing_entry_point == GetQuickInstrumentationEntryPoint() ||
Vladimir Markoe77141a2021-01-29 16:44:55 +0000605 (jit != nullptr && jit->GetCodeCache()->ContainsPc(existing_entry_point)))
606 << " entrypoint: " << existing_entry_point
607 << " size: " << OatQuickMethodHeader::FromEntryPoint(existing_entry_point)->GetCodeSize()
608 << " pc: " << reinterpret_cast<const void*>(pc);
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000609 return nullptr;
610 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100611 // Only for unit tests.
612 // TODO(ngeoffray): Update these tests to pass the right pc?
613 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
614 }
615 const void* oat_entry_point = oat_method.GetQuickCode();
616 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
David Sehr709b0702016-10-13 09:12:37 -0700617 DCHECK(IsNative()) << PrettyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100618 return nullptr;
619 }
620
621 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
622 if (pc == 0) {
623 // This is a downcall, it can only happen for a native method.
624 DCHECK(IsNative());
625 return method_header;
626 }
627
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100628 DCHECK(method_header->Contains(pc))
David Sehr709b0702016-10-13 09:12:37 -0700629 << PrettyMethod()
Roland Levillain0b671c02016-08-19 12:02:34 +0100630 << " " << std::hex << pc << " " << oat_entry_point
Mingyao Yang063fc772016-08-02 11:02:54 -0700631 << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100632 return method_header;
633}
634
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100635const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) {
636 bool found;
637 OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found);
638 if (found) {
639 return oat_method.GetQuickCode();
640 }
641 return nullptr;
642}
643
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000644bool ArtMethod::HasAnyCompiledCode() {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100645 if (IsNative() || !IsInvokable() || IsProxyMethod()) {
646 return false;
647 }
648
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000649 // Check whether the JIT has compiled it.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100650 Runtime* runtime = Runtime::Current();
651 jit::Jit* jit = runtime->GetJit();
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000652 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
653 return true;
654 }
655
656 // Check whether we have AOT code.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100657 return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr;
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000658}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000659
David Brazdil85865692018-10-30 17:26:20 +0000660void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
661 // Currently we only do intrinsics for static/final methods or methods of final
662 // classes. We don't set kHasSingleImplementation for those methods.
663 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
664 "Potential conflict with kAccSingleImplementation";
665 static const int kAccFlagsShift = CTZ(kAccIntrinsicBits);
666 DCHECK_LE(intrinsic, kAccIntrinsicBits >> kAccFlagsShift);
667 uint32_t intrinsic_bits = intrinsic << kAccFlagsShift;
668 uint32_t new_value = (GetAccessFlags() & ~kAccIntrinsicBits) | kAccIntrinsic | intrinsic_bits;
669 if (kIsDebugBuild) {
670 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
671 bool is_constructor = IsConstructor();
672 bool is_synchronized = IsSynchronized();
673 bool skip_access_checks = SkipAccessChecks();
674 bool is_fast_native = IsFastNative();
675 bool is_critical_native = IsCriticalNative();
676 bool is_copied = IsCopied();
677 bool is_miranda = IsMiranda();
678 bool is_default = IsDefault();
679 bool is_default_conflict = IsDefaultConflicting();
680 bool is_compilable = IsCompilable();
681 bool must_count_locks = MustCountLocks();
David Brazdil31cc2792019-04-10 00:31:15 +0100682 // Recompute flags instead of getting them from the current access flags because
683 // access flags may have been changed to deduplicate warning messages (b/129063331).
684 uint32_t hiddenapi_flags = hiddenapi::CreateRuntimeFlags(this);
David Brazdil85865692018-10-30 17:26:20 +0000685 SetAccessFlags(new_value);
686 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
687 DCHECK_EQ(is_constructor, IsConstructor());
688 DCHECK_EQ(is_synchronized, IsSynchronized());
689 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
690 DCHECK_EQ(is_fast_native, IsFastNative());
691 DCHECK_EQ(is_critical_native, IsCriticalNative());
692 DCHECK_EQ(is_copied, IsCopied());
693 DCHECK_EQ(is_miranda, IsMiranda());
694 DCHECK_EQ(is_default, IsDefault());
695 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
696 DCHECK_EQ(is_compilable, IsCompilable());
697 DCHECK_EQ(must_count_locks, MustCountLocks());
698 // Only DCHECK that we have preserved the hidden API access flags if the
Orion Hodson611d71c2021-08-27 17:00:10 +0100699 // original method was not in the SDK list. This is because the core image
David Brazdil31cc2792019-04-10 00:31:15 +0100700 // does not have the access flags set (b/77733081).
David Brazdil90faceb2018-12-14 14:36:15 +0000701 if ((hiddenapi_flags & kAccHiddenapiBits) != kAccPublicApi) {
David Brazdil85865692018-10-30 17:26:20 +0000702 DCHECK_EQ(hiddenapi_flags, hiddenapi::GetRuntimeFlags(this)) << PrettyMethod();
703 }
704 } else {
705 SetAccessFlags(new_value);
706 }
707}
708
David Brazdil904e75a2018-05-15 13:45:08 +0100709void ArtMethod::SetNotIntrinsic() {
710 if (!IsIntrinsic()) {
711 return;
712 }
713
David Brazdil85865692018-10-30 17:26:20 +0000714 // Read the existing hiddenapi flags.
715 uint32_t hiddenapi_runtime_flags = hiddenapi::GetRuntimeFlags(this);
David Brazdil904e75a2018-05-15 13:45:08 +0100716
717 // Clear intrinsic-related access flags.
718 ClearAccessFlags(kAccIntrinsic | kAccIntrinsicBits);
719
720 // Re-apply hidden API access flags now that the method is not an intrinsic.
David Brazdil85865692018-10-30 17:26:20 +0000721 SetAccessFlags(GetAccessFlags() | hiddenapi_runtime_flags);
722 DCHECK_EQ(hiddenapi_runtime_flags, hiddenapi::GetRuntimeFlags(this));
David Brazdil904e75a2018-05-15 13:45:08 +0100723}
724
Andreas Gampe542451c2016-07-26 09:02:02 -0700725void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000726 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
727 Size(image_pointer_size));
728 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
729
730 // If the entry point of the method we are copying from is from JIT code, we just
Vladimir Marko2196c652017-11-30 16:16:07 +0000731 // put the entry point of the new method to interpreter or GenericJNI. We could set
732 // the entry point to the JIT code, but this would require taking the JIT code cache
733 // lock to notify it, which we do not want at this level.
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000734 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100735 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000736 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000737 SetEntryPointFromQuickCompiledCodePtrSize(
738 src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(),
739 image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000740 }
741 }
Nicolas Geoffray8731e702021-04-06 12:11:59 +0100742 if (interpreter::IsNterpSupported() &&
743 (GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size) ==
744 interpreter::GetNterpEntryPoint())) {
Nicolas Geoffrayab7ecce2020-07-29 13:30:24 +0100745 // If the entrypoint is nterp, it's too early to check if the new method
746 // will support it. So for simplicity, use the interpreter bridge.
747 SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size);
748 }
749
750 // Clear the data pointer, it will be set if needed by the caller.
Nicolas Geoffray47171752020-08-31 15:03:20 +0100751 if (!src->HasCodeItem() && !src->IsNative()) {
Nicolas Geoffrayab7ecce2020-07-29 13:30:24 +0100752 SetDataPtrSize(nullptr, image_pointer_size);
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000753 }
754 // Clear hotness to let the JIT properly decide when to compile this method.
755 hotness_count_ = 0;
756}
757
Andreas Gampe542451c2016-07-26 09:02:02 -0700758bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700759 // Hijack this function to get access to PtrSizedFieldsOffset.
760 //
761 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
762 // 64-bit builds.
763 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700764 static_assert(
765 (sizeof(void*) != 4) ||
766 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
767 "Unexpected 32-bit class layout.");
768 static_assert(
769 (sizeof(void*) != 8) ||
770 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
771 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700772
Andreas Gampe75f08852016-07-19 08:06:07 -0700773 Runtime* runtime = Runtime::Current();
774 if (runtime == nullptr) {
775 return true;
776 }
777 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
778}
779
David Sehr709b0702016-10-13 09:12:37 -0700780std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) {
781 if (m == nullptr) {
782 return "null";
783 }
784 return m->PrettyMethod(with_signature);
785}
786
787std::string ArtMethod::PrettyMethod(bool with_signature) {
Vladimir Markob8a55f82017-09-21 16:21:43 +0100788 if (UNLIKELY(IsRuntimeMethod())) {
789 std::string result = GetDeclaringClassDescriptor();
790 result += '.';
791 result += GetName();
792 // Do not add "<no signature>" even if `with_signature` is true.
793 return result;
David Sehr709b0702016-10-13 09:12:37 -0700794 }
Vladimir Markob8a55f82017-09-21 16:21:43 +0100795 ArtMethod* m =
796 GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
Alex Lightcbc9dda2018-12-07 15:38:29 -0800797 std::string res(m->GetDexFile()->PrettyMethod(m->GetDexMethodIndex(), with_signature));
798 if (with_signature && m->IsObsolete()) {
799 return "<OBSOLETE> " + res;
800 } else {
801 return res;
802 }
David Sehr709b0702016-10-13 09:12:37 -0700803}
804
805std::string ArtMethod::JniShortName() {
Alex Light888a59e2017-01-25 11:41:41 -0800806 return GetJniShortName(GetDeclaringClassDescriptor(), GetName());
David Sehr709b0702016-10-13 09:12:37 -0700807}
808
809std::string ArtMethod::JniLongName() {
810 std::string long_name;
811 long_name += JniShortName();
812 long_name += "__";
813
814 std::string signature(GetSignature().ToString());
815 signature.erase(0, 1);
816 signature.erase(signature.begin() + signature.find(')'), signature.end());
817
818 long_name += MangleForJni(signature);
819
820 return long_name;
821}
822
Eric Holkabdb4592019-05-16 08:33:12 -0700823const char* ArtMethod::GetRuntimeMethodName() {
824 Runtime* const runtime = Runtime::Current();
825 if (this == runtime->GetResolutionMethod()) {
826 return "<runtime internal resolution method>";
827 } else if (this == runtime->GetImtConflictMethod()) {
828 return "<runtime internal imt conflict method>";
829 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
830 return "<runtime internal callee-save all registers method>";
831 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
832 return "<runtime internal callee-save reference registers method>";
833 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
834 return "<runtime internal callee-save reference and argument registers method>";
835 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
836 return "<runtime internal save-every-register method>";
837 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
838 return "<runtime internal save-every-register method for clinit>";
839 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
840 return "<runtime internal save-every-register method for suspend check>";
841 } else {
842 return "<unknown runtime internal method>";
843 }
844}
845
Nicolas Geoffraye1d2dce2020-09-21 10:06:31 +0100846void ArtMethod::SetCodeItem(const dex::CodeItem* code_item) {
847 DCHECK(HasCodeItem());
848 // We mark the lowest bit for the interpreter to know whether it's executing a
849 // method in a compact or standard dex file.
850 uintptr_t data =
851 reinterpret_cast<uintptr_t>(code_item) | (GetDexFile()->IsCompactDexFile() ? 1 : 0);
852 SetDataPtrSize(reinterpret_cast<void*>(data), kRuntimePointerSize);
853}
854
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800855// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
856// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
857template <ReadBarrierOption kReadBarrierOption>
858ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
859 NO_THREAD_SAFETY_ANALYSIS {
860 CHECK(method->IsRuntimeMethod() ||
861 method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
862 method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
863}
864
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865} // namespace art