Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 17 | #include "art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Andreas Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 23 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 24 | #include "arch/context.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 26 | #include "base/enums.h" |
| 27 | #include "base/stl_util.h" |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 29 | #include "class_root-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 30 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 31 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 32 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "dex/dex_file-inl.h" |
| 34 | #include "dex/dex_file_exception_helpers.h" |
| 35 | #include "dex/dex_instruction.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 36 | #include "dex/signature-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/runtime_asm_entrypoints.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | #include "gc/accounting/card_table-inl.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 39 | #include "hidden_api.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 41 | #include "jit/jit.h" |
| 42 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 43 | #include "jit/profiling_info.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 44 | #include "jni/jni_internal.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 45 | #include "mirror/class-inl.h" |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 46 | #include "mirror/class_ext-inl.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 47 | #include "mirror/executable.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 48 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 49 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 50 | #include "mirror/string.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 51 | #include "oat_file-inl.h" |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 52 | #include "quicken_info.h" |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 53 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 54 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 55 | #include "vdex_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | |
| 57 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 59 | using android::base::StringPrintf; |
| 60 | |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 61 | extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 62 | const char*); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 63 | extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 64 | const char*); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 65 | |
Andrew Scull | 530f09a | 2020-07-22 11:52:06 +0100 | [diff] [blame] | 66 | // Enforce that we have the right index for runtime methods. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 67 | static_assert(ArtMethod::kRuntimeMethodDexMethodIndex == dex::kDexNoIndex, |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 68 | "Wrong runtime-method dex method index"); |
| 69 | |
Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 70 | ArtMethod* ArtMethod::GetCanonicalMethod(PointerSize pointer_size) { |
Alex Light | fd4a79c | 2019-06-21 08:45:16 -0700 | [diff] [blame] | 71 | if (LIKELY(!IsCopied())) { |
Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 72 | return this; |
| 73 | } else { |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 74 | ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 75 | DCHECK(declaring_class->IsInterface()); |
Vladimir Marko | 813a863 | 2018-11-29 16:17:01 +0000 | [diff] [blame] | 76 | ArtMethod* ret = declaring_class->FindInterfaceMethod(GetDexCache(), |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 77 | GetDexMethodIndex(), |
| 78 | pointer_size); |
Alex Light | 97e7803 | 2017-06-27 17:51:55 -0700 | [diff] [blame] | 79 | DCHECK(ret != nullptr); |
| 80 | return ret; |
| 81 | } |
| 82 | } |
| 83 | |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 84 | ArtMethod* ArtMethod::GetNonObsoleteMethod() { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 85 | if (LIKELY(!IsObsolete())) { |
| 86 | return this; |
Vladimir Marko | 813a863 | 2018-11-29 16:17:01 +0000 | [diff] [blame] | 87 | } |
| 88 | DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 89 | if (IsDirect()) { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 90 | return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()]; |
| 91 | } else { |
| 92 | return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize); |
| 93 | } |
| 94 | } |
| 95 | |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 96 | ArtMethod* ArtMethod::GetSingleImplementation(PointerSize pointer_size) { |
Nicolas Geoffray | 8731e70 | 2021-04-06 12:11:59 +0100 | [diff] [blame] | 97 | if (IsInvokable()) { |
| 98 | // An invokable method single implementation is itself. |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 99 | return this; |
| 100 | } |
Nicolas Geoffray | 8731e70 | 2021-04-06 12:11:59 +0100 | [diff] [blame] | 101 | DCHECK(!IsDefaultConflicting()); |
| 102 | ArtMethod* m = reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size)); |
| 103 | CHECK(m == nullptr || !m->IsDefaultConflicting()); |
| 104 | return m; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 107 | ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, |
| 108 | jobject jlr_method) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 109 | ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method); |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 110 | DCHECK(executable != nullptr); |
| 111 | return executable->GetArtMethod(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 114 | ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache() { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 115 | PointerSize pointer_size = kRuntimePointerSize; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 116 | DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod(); |
| 117 | DCHECK(IsObsolete()); |
| 118 | ObjPtr<mirror::ClassExt> ext(GetDeclaringClass()->GetExtData()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 119 | 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 Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 123 | // 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 Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 125 | DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 126 | 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 Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 131 | 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 Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 143 | uint16_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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 148 | const dex::ClassDef* class_def = dex_file->FindClassDef(declaring_class_type); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 149 | CHECK(class_def != nullptr); |
| 150 | return dex_file->GetIndexForClassDef(*class_def); |
| 151 | } |
| 152 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 153 | void ArtMethod::ThrowInvocationTimeError() { |
| 154 | DCHECK(!IsInvokable()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 155 | if (IsDefaultConflicting()) { |
| 156 | ThrowIncompatibleClassChangeErrorForMethodConflict(this); |
| 157 | } else { |
| 158 | DCHECK(IsAbstract()); |
| 159 | ThrowAbstractMethodError(this); |
| 160 | } |
| 161 | } |
| 162 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 163 | InvokeType ArtMethod::GetInvokeType() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 164 | // TODO: kSuper? |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 165 | if (IsStatic()) { |
Nicolas Geoffray | 12abcbd | 2016-06-06 15:51:58 +0000 | [diff] [blame] | 166 | return kStatic; |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 167 | } else if (GetDeclaringClass()->IsInterface()) { |
| 168 | return kInterface; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | } else if (IsDirect()) { |
| 170 | return kDirect; |
Orion Hodson | 81daf3d | 2020-09-03 14:01:51 +0100 | [diff] [blame] | 171 | } else if (IsSignaturePolymorphic()) { |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 172 | return kPolymorphic; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | } else { |
| 174 | return kVirtual; |
| 175 | } |
| 176 | } |
| 177 | |
Vladimir Marko | 6e78158 | 2019-02-04 10:58:06 +0000 | [diff] [blame] | 178 | size_t ArtMethod::NumArgRegisters(const char* shorty) { |
| 179 | CHECK_NE(shorty[0], '\0'); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | uint32_t num_registers = 0; |
Vladimir Marko | 6e78158 | 2019-02-04 10:58:06 +0000 | [diff] [blame] | 181 | for (const char* s = shorty + 1; *s != '\0'; ++s) { |
| 182 | if (*s == 'D' || *s == 'J') { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 183 | num_registers += 2; |
| 184 | } else { |
| 185 | num_registers += 1; |
| 186 | } |
| 187 | } |
| 188 | return num_registers; |
| 189 | } |
| 190 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 191 | bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 192 | ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature"); |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 193 | const DexFile* dex_file = GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 194 | const dex::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex()); |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 195 | if (GetDexCache() == other->GetDexCache()) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 196 | const dex::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 197 | return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_; |
| 198 | } |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 199 | const DexFile* dex_file2 = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 200 | const dex::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex()); |
Andreas Gampe | b8bca30 | 2019-01-02 10:28:38 -0800 | [diff] [blame] | 201 | if (!DexFile::StringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 202 | return false; // Name mismatch. |
| 203 | } |
| 204 | return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2); |
| 205 | } |
| 206 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 207 | ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 208 | if (IsStatic()) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 209 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 210 | } |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 211 | ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(); |
| 212 | ObjPtr<mirror::Class> super_class = declaring_class->GetSuperClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 213 | uint16_t method_index = GetMethodIndex(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 214 | ArtMethod* result = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 215 | // Did this method override a super class method? If so load the result from the super class' |
| 216 | // vtable |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 217 | if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 218 | result = super_class->GetVTableEntry(method_index, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 219 | } else { |
| 220 | // Method didn't override superclass method so search interfaces |
| 221 | if (IsProxyMethod()) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 222 | result = GetInterfaceMethodIfProxy(pointer_size); |
| 223 | DCHECK(result != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | } else { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 225 | ObjPtr<mirror::IfTable> iftable = GetDeclaringClass()->GetIfTable(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 226 | for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) { |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 227 | ObjPtr<mirror::Class> interface = iftable->GetInterface(i); |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 228 | for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) { |
| 229 | if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) { |
| 230 | result = &interface_method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 237 | DCHECK(result == nullptr || |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 238 | GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature( |
| 239 | result->GetInterfaceMethodIfProxy(pointer_size))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 240 | return result; |
| 241 | } |
| 242 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 243 | uint32_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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 247 | 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 Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 249 | 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 255 | const dex::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 256 | if (other_type_id != nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 257 | const dex::MethodId* other_mid = other_dexfile.FindMethodId( |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 258 | *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 Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 264 | return dex::kDexNoIndex; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 267 | uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 268 | uint32_t dex_pc, bool* has_no_move_exception) { |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 269 | // Set aside the exception while we resolve its type. |
| 270 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 271 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 272 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 273 | self->ClearException(); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 274 | // Default to handler not found. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 275 | uint32_t found_dex_pc = dex::kDexNoIndex; |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 276 | // Iterate over the catch handlers associated with dex_pc. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 277 | CodeItemDataAccessor accessor(DexInstructionData()); |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 278 | for (CatchHandlerIterator it(accessor, dex_pc); it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 279 | dex::TypeIndex iter_type_idx = it.GetHandlerTypeIndex(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 280 | // Catch all case |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 281 | if (!iter_type_idx.IsValid()) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 282 | found_dex_pc = it.GetHandlerAddress(); |
| 283 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 284 | } |
| 285 | // Does this catch exception type apply? |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 286 | ObjPtr<mirror::Class> iter_exception_type = ResolveClassFromTypeIndex(iter_type_idx); |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame] | 287 | 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 Gampe | 72b3e43 | 2014-05-13 21:42:05 -0700 | [diff] [blame] | 290 | // Note: this is not RI behavior. RI would have failed when loading the class. |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame] | 291 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 295 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 296 | << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 297 | } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 298 | found_dex_pc = it.GetHandlerAddress(); |
| 299 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 302 | if (found_dex_pc != dex::kDexNoIndex) { |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 303 | const Instruction& first_catch_instr = accessor.InstructionAt(found_dex_pc); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 304 | *has_no_move_exception = (first_catch_instr.Opcode() != Instruction::MOVE_EXCEPTION); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 305 | } |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 306 | // Put the exception back. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 307 | if (exception != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 308 | self->SetException(exception.Get()); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 309 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 310 | return found_dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 313 | void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result, |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 314 | const char* shorty) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 315 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
| 316 | ThrowStackOverflowError(self); |
| 317 | return; |
| 318 | } |
| 319 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 320 | if (kIsDebugBuild) { |
| 321 | self->AssertThreadSuspensionIsAllowable(); |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 322 | CHECK_EQ(ThreadState::kRunnable, self->GetState()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 323 | CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Push a transition back into managed code onto the linked list in thread. |
| 327 | ManagedStack fragment; |
| 328 | self->PushManagedStackFragment(&fragment); |
| 329 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 330 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 331 | // Call the invoke stub, passing everything as arguments. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 332 | // If the runtime is not yet started or it is required by the debugger, then perform the |
Aart Bik | 0122320 | 2016-05-05 15:10:42 -0700 | [diff] [blame] | 333 | // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent |
| 334 | // cycling around the various JIT/Interpreter methods that handle method invocation. |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 335 | if (UNLIKELY(!runtime->IsStarted() || |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 336 | (self->IsForceInterpreter() && !IsNative() && !IsProxyMethod() && IsInvokable()))) { |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 337 | if (IsStatic()) { |
Aart Bik | 0122320 | 2016-05-05 15:10:42 -0700 | [diff] [blame] | 338 | art::interpreter::EnterInterpreterFromInvoke( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 339 | self, this, nullptr, args, result, /*stay_in_interpreter=*/ true); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 340 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 341 | mirror::Object* receiver = |
| 342 | reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr(); |
Aart Bik | 0122320 | 2016-05-05 15:10:42 -0700 | [diff] [blame] | 343 | art::interpreter::EnterInterpreterFromInvoke( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 344 | self, this, receiver, args + 1, result, /*stay_in_interpreter=*/ true); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 345 | } |
| 346 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 347 | DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 348 | |
| 349 | constexpr bool kLogInvocationStartAndReturn = false; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 350 | bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 351 | if (LIKELY(have_quick_code)) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 352 | if (kLogInvocationStartAndReturn) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 353 | LOG(INFO) << StringPrintf( |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 354 | "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 355 | GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0)); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 356 | } |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 357 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 358 | // Ensure that we won't be accidentally calling quick compiled code when -Xint. |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 359 | if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 360 | CHECK(!runtime->UseJitCompilation()); |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 361 | const void* oat_quick_code = |
| 362 | (IsNative() || !IsInvokable() || IsProxyMethod() || IsObsolete()) |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 363 | ? nullptr |
| 364 | : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 365 | CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 366 | << "Don't call compiled code when -Xint " << PrettyMethod(); |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 369 | if (!IsStatic()) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 370 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 371 | } else { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 372 | (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 373 | } |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 374 | if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 375 | // Unusual case where we were running generated code and an |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 376 | // exception was thrown to force the activations to be removed from the |
| 377 | // stack. Continue execution in the interpreter. |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 378 | self->DeoptimizeWithDeoptimizationException(result); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 379 | } |
| 380 | if (kLogInvocationStartAndReturn) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 381 | LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 382 | GetEntryPointFromQuickCompiledCode()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 383 | } |
| 384 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 385 | LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null"; |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 386 | if (result != nullptr) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 387 | result->SetJ(0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Pop transition. |
| 393 | self->PopManagedStackFragment(fragment); |
| 394 | } |
| 395 | |
Orion Hodson | 81daf3d | 2020-09-03 14:01:51 +0100 | [diff] [blame] | 396 | bool ArtMethod::IsSignaturePolymorphic() { |
Orion Hodson | eb4d19b | 2017-11-06 15:49:23 +0000 | [diff] [blame] | 397 | // 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 Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 402 | 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 Hodson | eb4d19b | 2017-11-06 15:49:23 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 409 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, |
| 410 | uint16_t class_def_idx, |
| 411 | uint32_t method_idx) { |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 412 | ClassAccessor accessor(dex_file, class_def_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 413 | uint32_t class_def_method_index = 0u; |
| 414 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
| 415 | if (method.GetIndex() == method_idx) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 416 | return class_def_method_index; |
| 417 | } |
| 418 | class_def_method_index++; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 419 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 420 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
| 421 | UNREACHABLE(); |
| 422 | } |
| 423 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 424 | // 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. |
| 432 | static 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 439 | const dex::TypeId* declaring_class_type_id = |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 440 | 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 443 | const dex::ClassDef* declaring_class_type_def = |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 444 | 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 Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 461 | static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, |
| 462 | PointerSize pointer_size, |
| 463 | bool* found) |
| 464 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 465 | 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 Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 474 | // 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 Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 476 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 477 | 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 494 | << method->PrettyMethod(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 495 | } |
| 496 | DCHECK_EQ(oat_method_index, |
Vladimir Marko | 813a863 | 2018-11-29 16:17:01 +0000 | [diff] [blame] | 497 | GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(), |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 498 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
| 499 | method->GetDexMethodIndex())); |
Vladimir Marko | 813a863 | 2018-11-29 16:17:01 +0000 | [diff] [blame] | 500 | OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(), |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 501 | 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 Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 509 | bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 510 | const DexFile* dex_file = GetDexFile(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 511 | const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 512 | const auto& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 513 | const dex::TypeList* proto_params = dex_file->GetProtoParameters(proto_id); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 514 | auto count = proto_params != nullptr ? proto_params->Size() : 0u; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 515 | auto param_len = params != nullptr ? params->GetLength() : 0u; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 516 | if (param_len != count) { |
| 517 | return false; |
| 518 | } |
| 519 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 520 | for (size_t i = 0; i < count; ++i) { |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 521 | dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_; |
| 522 | ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 523 | 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 Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 534 | const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) { |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 535 | // 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 Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 539 | if (IsRuntimeMethod()) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 540 | return nullptr; |
| 541 | } |
| 542 | |
| 543 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 544 | const void* existing_entry_point = GetEntryPointFromQuickCompiledCode(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 545 | CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 546 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 547 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 548 | 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 Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 554 | // Check whether the current entry point contains this pc. |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 555 | if (!class_linker->IsQuickGenericJniStub(existing_entry_point) && |
| 556 | !class_linker->IsQuickResolutionStub(existing_entry_point) && |
David Srbecky | 774232a | 2019-04-03 17:46:32 +0100 | [diff] [blame] | 557 | !class_linker->IsQuickToInterpreterBridge(existing_entry_point) && |
Nicolas Geoffray | 248d5c4 | 2021-09-13 09:53:10 +0100 | [diff] [blame] | 558 | existing_entry_point != GetQuickInstrumentationEntryPoint() && |
| 559 | existing_entry_point != GetInvokeObsoleteMethodStub()) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 560 | OatQuickMethodHeader* method_header = |
| 561 | OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 562 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 563 | if (method_header->Contains(pc)) { |
| 564 | return method_header; |
| 565 | } |
| 566 | } |
| 567 | |
Nicolas Geoffray | 409d1db | 2021-11-12 16:27:10 +0000 | [diff] [blame] | 568 | if (OatQuickMethodHeader::IsNterpPc(pc)) { |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 569 | return OatQuickMethodHeader::NterpMethodHeader; |
| 570 | } |
| 571 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 572 | // Check whether the pc is in the JIT code cache. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 573 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 574 | 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 Geoffray | 2a524bd | 2016-03-01 12:18:47 +0000 | [diff] [blame] | 581 | DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc))) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 582 | << PrettyMethod() |
Nicolas Geoffray | 2a524bd | 2016-03-01 12:18:47 +0000 | [diff] [blame] | 583 | << ", 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 Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | // The code has to be in an oat file. |
| 591 | bool found; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 592 | OatFile::OatMethod oat_method = |
| 593 | FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 594 | if (!found) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 595 | 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 Marko | e77141a | 2021-01-29 16:44:55 +0000 | [diff] [blame] | 601 | (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 Geoffray | 49e4396 | 2015-10-28 16:16:16 +0000 | [diff] [blame] | 605 | return nullptr; |
| 606 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 607 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 613 | DCHECK(IsNative()) << PrettyMethod(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 614 | 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 Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 624 | DCHECK(method_header->Contains(pc)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 625 | << PrettyMethod() |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 626 | << " " << std::hex << pc << " " << oat_entry_point |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 627 | << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 628 | return method_header; |
| 629 | } |
| 630 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 631 | const 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 Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 640 | bool ArtMethod::HasAnyCompiledCode() { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 641 | if (IsNative() || !IsInvokable() || IsProxyMethod()) { |
| 642 | return false; |
| 643 | } |
| 644 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 645 | // Check whether the JIT has compiled it. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 646 | Runtime* runtime = Runtime::Current(); |
| 647 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 648 | if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) { |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | // Check whether we have AOT code. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 653 | return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr; |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 654 | } |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 655 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 656 | void 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 Brazdil | 31cc279 | 2019-04-10 00:31:15 +0100 | [diff] [blame] | 678 | // 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 681 | 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 Hodson | 611d71c | 2021-08-27 17:00:10 +0100 | [diff] [blame] | 695 | // original method was not in the SDK list. This is because the core image |
David Brazdil | 31cc279 | 2019-04-10 00:31:15 +0100 | [diff] [blame] | 696 | // does not have the access flags set (b/77733081). |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 697 | if ((hiddenapi_flags & kAccHiddenapiBits) != kAccPublicApi) { |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 698 | DCHECK_EQ(hiddenapi_flags, hiddenapi::GetRuntimeFlags(this)) << PrettyMethod(); |
| 699 | } |
| 700 | } else { |
| 701 | SetAccessFlags(new_value); |
| 702 | } |
| 703 | } |
| 704 | |
David Brazdil | 904e75a | 2018-05-15 13:45:08 +0100 | [diff] [blame] | 705 | void ArtMethod::SetNotIntrinsic() { |
| 706 | if (!IsIntrinsic()) { |
| 707 | return; |
| 708 | } |
| 709 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 710 | // Read the existing hiddenapi flags. |
| 711 | uint32_t hiddenapi_runtime_flags = hiddenapi::GetRuntimeFlags(this); |
David Brazdil | 904e75a | 2018-05-15 13:45:08 +0100 | [diff] [blame] | 712 | |
| 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 717 | SetAccessFlags(GetAccessFlags() | hiddenapi_runtime_flags); |
| 718 | DCHECK_EQ(hiddenapi_runtime_flags, hiddenapi::GetRuntimeFlags(this)); |
David Brazdil | 904e75a | 2018-05-15 13:45:08 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 721 | void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 722 | 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 Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 727 | // 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 Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 730 | Runtime* runtime = Runtime::Current(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 731 | if (runtime->UseJitCompilation()) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 732 | if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 733 | SetEntryPointFromQuickCompiledCodePtrSize( |
| 734 | src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(), |
| 735 | image_pointer_size); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
Nicolas Geoffray | 8731e70 | 2021-04-06 12:11:59 +0100 | [diff] [blame] | 738 | if (interpreter::IsNterpSupported() && |
| 739 | (GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size) == |
| 740 | interpreter::GetNterpEntryPoint())) { |
Nicolas Geoffray | ab7ecce | 2020-07-29 13:30:24 +0100 | [diff] [blame] | 741 | // 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 Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 747 | if (!src->HasCodeItem() && !src->IsNative()) { |
Nicolas Geoffray | ab7ecce | 2020-07-29 13:30:24 +0100 | [diff] [blame] | 748 | SetDataPtrSize(nullptr, image_pointer_size); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 749 | } |
| 750 | // Clear hotness to let the JIT properly decide when to compile this method. |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 751 | ResetCounter(runtime->GetJITOptions()->GetWarmupThreshold()); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 754 | bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) { |
Andreas Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 755 | // 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 Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 760 | 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 Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 768 | |
Andreas Gampe | 75f0885 | 2016-07-19 08:06:07 -0700 | [diff] [blame] | 769 | Runtime* runtime = Runtime::Current(); |
| 770 | if (runtime == nullptr) { |
| 771 | return true; |
| 772 | } |
| 773 | return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size; |
| 774 | } |
| 775 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 776 | std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) { |
| 777 | if (m == nullptr) { |
| 778 | return "null"; |
| 779 | } |
| 780 | return m->PrettyMethod(with_signature); |
| 781 | } |
| 782 | |
| 783 | std::string ArtMethod::PrettyMethod(bool with_signature) { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 784 | 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 790 | } |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 791 | ArtMethod* m = |
| 792 | GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Alex Light | cbc9dda | 2018-12-07 15:38:29 -0800 | [diff] [blame] | 793 | 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | std::string ArtMethod::JniShortName() { |
Alex Light | 888a59e | 2017-01-25 11:41:41 -0800 | [diff] [blame] | 802 | return GetJniShortName(GetDeclaringClassDescriptor(), GetName()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | std::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 Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 819 | const 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 | |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 842 | void ArtMethod::SetCodeItem(const dex::CodeItem* code_item, bool is_compact_dex_code_item) { |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 843 | 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 = |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 847 | reinterpret_cast<uintptr_t>(code_item) | (is_compact_dex_code_item ? 1 : 0); |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 848 | SetDataPtrSize(reinterpret_cast<void*>(data), kRuntimePointerSize); |
| 849 | } |
| 850 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 851 | // 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. |
| 853 | template <ReadBarrierOption kReadBarrierOption> |
| 854 | ALWAYS_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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 861 | } // namespace art |