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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "arch/context.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 20 | #include "art_field-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "base/stringpiece.h" |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 23 | #include "class_linker-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Ian Rogers | c449aa8 | 2013-07-29 14:35:46 -0700 | [diff] [blame] | 26 | #include "dex_instruction.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 27 | #include "entrypoints/entrypoint_utils.h" |
| 28 | #include "entrypoints/runtime_asm_entrypoints.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | #include "jit/jit.h" |
| 32 | #include "jit/jit_code_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "jni_internal.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 34 | #include "mapping_table.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "mirror/abstract_method.h" |
| 36 | #include "mirror/class-inl.h" |
| 37 | #include "mirror/object_array-inl.h" |
| 38 | #include "mirror/object-inl.h" |
| 39 | #include "mirror/string.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 40 | #include "oat_file-inl.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | |
| 44 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 46 | extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 47 | const char*); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 48 | extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 49 | const char*); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 50 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 51 | ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, |
| 52 | jobject jlr_method) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 53 | auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method); |
| 54 | DCHECK(abstract_method != nullptr); |
| 55 | return abstract_method->GetArtMethod(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 58 | mirror::String* ArtMethod::GetNameAsString(Thread* self) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 59 | CHECK(!IsProxyMethod()); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 60 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 61 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache())); |
| 62 | auto* dex_file = dex_cache->GetDexFile(); |
| 63 | uint32_t dex_method_idx = GetDexMethodIndex(); |
| 64 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 65 | return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_, |
| 66 | dex_cache); |
| 67 | } |
| 68 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 69 | InvokeType ArtMethod::GetInvokeType() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | // TODO: kSuper? |
| 71 | if (GetDeclaringClass()->IsInterface()) { |
| 72 | return kInterface; |
| 73 | } else if (IsStatic()) { |
| 74 | return kStatic; |
| 75 | } else if (IsDirect()) { |
| 76 | return kDirect; |
| 77 | } else { |
| 78 | return kVirtual; |
| 79 | } |
| 80 | } |
| 81 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 82 | size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) { |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 83 | CHECK_LE(1U, shorty.length()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | uint32_t num_registers = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 85 | for (size_t i = 1; i < shorty.length(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | char ch = shorty[i]; |
| 87 | if (ch == 'D' || ch == 'J') { |
| 88 | num_registers += 2; |
| 89 | } else { |
| 90 | num_registers += 1; |
| 91 | } |
| 92 | } |
| 93 | return num_registers; |
| 94 | } |
| 95 | |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 96 | static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 97 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 98 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature"); |
| 99 | const DexFile* dex_file = method1->GetDexFile(); |
| 100 | const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex()); |
| 101 | if (method1->GetDexCache() == method2->GetDexCache()) { |
| 102 | const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex()); |
| 103 | return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_; |
| 104 | } |
| 105 | const DexFile* dex_file2 = method2->GetDexFile(); |
| 106 | const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex()); |
| 107 | if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) { |
| 108 | return false; // Name mismatch. |
| 109 | } |
| 110 | return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2); |
| 111 | } |
| 112 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 113 | ArtMethod* ArtMethod::FindOverriddenMethod(size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | if (IsStatic()) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 115 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 117 | mirror::Class* declaring_class = GetDeclaringClass(); |
| 118 | mirror::Class* super_class = declaring_class->GetSuperClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | uint16_t method_index = GetMethodIndex(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 120 | ArtMethod* result = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | // Did this method override a super class method? If so load the result from the super class' |
| 122 | // vtable |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 123 | if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | result = super_class->GetVTableEntry(method_index, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | } else { |
| 126 | // Method didn't override superclass method so search interfaces |
| 127 | if (IsProxyMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 128 | result = GetDexCacheResolvedMethods()->GetElementPtrSize<ArtMethod*>( |
| 129 | GetDexMethodIndex(), pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 130 | CHECK_EQ(result, |
| 131 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
| 132 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 133 | mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 134 | for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | mirror::Class* interface = iftable->GetInterface(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 136 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | ArtMethod* interface_method = interface->GetVirtualMethod(j, pointer_size); |
| 138 | if (HasSameNameAndSignature( |
| 139 | this, interface_method->GetInterfaceMethodIfProxy(sizeof(void*)))) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 140 | result = interface_method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | DCHECK(result == nullptr || HasSameNameAndSignature( |
| 148 | GetInterfaceMethodIfProxy(sizeof(void*)), result->GetInterfaceMethodIfProxy(sizeof(void*)))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 149 | return result; |
| 150 | } |
| 151 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 152 | uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 153 | uint32_t name_and_signature_idx) { |
| 154 | const DexFile* dexfile = GetDexFile(); |
| 155 | const uint32_t dex_method_idx = GetDexMethodIndex(); |
| 156 | const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); |
| 157 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
| 158 | DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 159 | DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 160 | if (dexfile == &other_dexfile) { |
| 161 | return dex_method_idx; |
| 162 | } |
| 163 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
| 164 | const DexFile::StringId* other_descriptor = |
| 165 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 166 | if (other_descriptor != nullptr) { |
| 167 | const DexFile::TypeId* other_type_id = |
| 168 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 169 | if (other_type_id != nullptr) { |
| 170 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 171 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 172 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 173 | if (other_mid != nullptr) { |
| 174 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return DexFile::kDexNoIndex; |
| 179 | } |
| 180 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 181 | uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 182 | const void* entry_point = GetQuickOatEntryPoint(sizeof(void*)); |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 183 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point); |
| 184 | if (IsOptimized(sizeof(void*))) { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 185 | CodeInfo code_info = GetOptimizedCodeInfo(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 186 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 187 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset, encoding); |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 188 | if (stack_map.IsValid()) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 189 | return stack_map.GetDexPc(encoding); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 190 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 191 | } else { |
| 192 | MappingTable table(entry_point != nullptr ? |
| 193 | GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr); |
| 194 | if (table.TotalSize() == 0) { |
| 195 | // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping |
| 196 | // but they have no suspend checks and, consequently, we never call ToDexPc() for them. |
| 197 | DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this); |
| 198 | return DexFile::kDexNoIndex; // Special no mapping case |
| 199 | } |
| 200 | // Assume the caller wants a pc-to-dex mapping so check here first. |
| 201 | typedef MappingTable::PcToDexIterator It; |
| 202 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 203 | if (cur.NativePcOffset() == sought_offset) { |
| 204 | return cur.DexPc(); |
| 205 | } |
| 206 | } |
| 207 | // Now check dex-to-pc mappings. |
| 208 | typedef MappingTable::DexToPcIterator It2; |
| 209 | for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 210 | if (cur.NativePcOffset() == sought_offset) { |
| 211 | return cur.DexPc(); |
| 212 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 215 | if (abort_on_failure) { |
| 216 | LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset) |
Vladimir Marko | 4c1c510 | 2014-05-14 16:51:16 +0100 | [diff] [blame] | 217 | << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 218 | << " current entry_point=" << GetQuickOatEntryPoint(sizeof(void*)) |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 219 | << ") in " << PrettyMethod(this); |
| 220 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | return DexFile::kDexNoIndex; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 224 | uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 225 | const void* entry_point = GetQuickOatEntryPoint(sizeof(void*)); |
| 226 | MappingTable table(entry_point != nullptr ? |
| 227 | GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 228 | if (table.TotalSize() == 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 229 | DCHECK_EQ(dex_pc, 0U); |
| 230 | return 0; // Special no mapping/pc == 0 case |
| 231 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 232 | // Assume the caller wants a dex-to-pc mapping so check here first. |
| 233 | typedef MappingTable::DexToPcIterator It; |
| 234 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 235 | if (cur.DexPc() == dex_pc) { |
Vladimir Marko | 4c1c510 | 2014-05-14 16:51:16 +0100 | [diff] [blame] | 236 | return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 239 | // Now check pc-to-dex mappings. |
| 240 | typedef MappingTable::PcToDexIterator It2; |
| 241 | for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 242 | if (cur.DexPc() == dex_pc) { |
Vladimir Marko | 4c1c510 | 2014-05-14 16:51:16 +0100 | [diff] [blame] | 243 | return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset(); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 246 | if (abort_on_failure) { |
| 247 | LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc |
| 248 | << " in " << PrettyMethod(this); |
| 249 | } |
| 250 | return UINTPTR_MAX; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 253 | uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 254 | uint32_t dex_pc, bool* has_no_move_exception) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 255 | const DexFile::CodeItem* code_item = GetCodeItem(); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 256 | // Set aside the exception while we resolve its type. |
| 257 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 258 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 259 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 260 | self->ClearException(); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 261 | // Default to handler not found. |
| 262 | uint32_t found_dex_pc = DexFile::kDexNoIndex; |
| 263 | // Iterate over the catch handlers associated with dex_pc. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 265 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
| 266 | // Catch all case |
| 267 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 268 | found_dex_pc = it.GetHandlerAddress(); |
| 269 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 270 | } |
| 271 | // Does this catch exception type apply? |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 272 | mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx, true); |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame] | 273 | if (UNLIKELY(iter_exception_type == nullptr)) { |
| 274 | // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was |
| 275 | // removed by a pro-guard like tool. |
Andreas Gampe | 72b3e43 | 2014-05-13 21:42:05 -0700 | [diff] [blame] | 276 | // 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] | 277 | self->ClearException(); |
| 278 | // Delete any long jump context as this routine is called during a stack walk which will |
| 279 | // release its in use context at the end. |
| 280 | delete self->GetLongJumpContext(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 281 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 282 | << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 283 | } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 284 | found_dex_pc = it.GetHandlerAddress(); |
| 285 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 288 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 289 | const Instruction* first_catch_instr = |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 290 | Instruction::At(&code_item->insns_[found_dex_pc]); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 291 | *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION); |
| 292 | } |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 293 | // Put the exception back. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 294 | if (exception.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 295 | self->SetException(exception.Get()); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 296 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 297 | return found_dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 300 | void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) { |
| 301 | if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) { |
| 302 | return; |
| 303 | } |
| 304 | if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) { |
| 305 | return; |
| 306 | } |
| 307 | const void* code = GetEntryPointFromQuickCompiledCode(); |
| 308 | if (code == GetQuickInstrumentationEntryPoint()) { |
| 309 | return; |
| 310 | } |
| 311 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 312 | if (class_linker->IsQuickToInterpreterBridge(code) || |
| 313 | class_linker->IsQuickResolutionStub(code)) { |
| 314 | return; |
| 315 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 316 | // If we are the JIT then we may have just compiled the method after the |
| 317 | // IsQuickToInterpreterBridge check. |
| 318 | jit::Jit* const jit = Runtime::Current()->GetJit(); |
| 319 | if (jit != nullptr && |
| 320 | jit->GetCodeCache()->ContainsCodePtr(reinterpret_cast<const void*>(code))) { |
| 321 | return; |
| 322 | } |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 323 | /* |
| 324 | * During a stack walk, a return PC may point past-the-end of the code |
| 325 | * in the case that the last instruction is a call that isn't expected to |
| 326 | * return. Thus, we check <= code + GetCodeSize(). |
| 327 | * |
| 328 | * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state. |
| 329 | */ |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 330 | CHECK(PcIsWithinQuickCode(reinterpret_cast<uintptr_t>(code), pc)) |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 331 | << PrettyMethod(this) |
| 332 | << " pc=" << std::hex << pc |
| 333 | << " code=" << code |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 334 | << " size=" << GetCodeSize( |
| 335 | EntryPointToCodePointer(reinterpret_cast<const void*>(code))); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 338 | bool ArtMethod::IsEntrypointInterpreter() { |
| 339 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 340 | const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this); |
| 341 | return oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode(); |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 344 | const void* ArtMethod::GetQuickOatEntryPoint(size_t pointer_size) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 345 | if (IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 346 | return nullptr; |
| 347 | } |
| 348 | Runtime* runtime = Runtime::Current(); |
| 349 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 350 | const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this, pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 351 | // On failure, instead of null we get the quick-generic-jni-trampoline for native method |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 352 | // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline) |
| 353 | // for non-native methods. |
| 354 | if (class_linker->IsQuickToInterpreterBridge(code) || |
| 355 | class_linker->IsQuickGenericJniStub(code)) { |
| 356 | return nullptr; |
| 357 | } |
| 358 | return code; |
| 359 | } |
| 360 | |
| 361 | #ifndef NDEBUG |
| 362 | uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) { |
| 363 | CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 364 | CHECK_EQ(quick_entry_point, |
| 365 | Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*))); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 366 | return pc - reinterpret_cast<uintptr_t>(quick_entry_point); |
| 367 | } |
| 368 | #endif |
| 369 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 370 | 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] | 371 | const char* shorty) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 372 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
| 373 | ThrowStackOverflowError(self); |
| 374 | return; |
| 375 | } |
| 376 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 377 | if (kIsDebugBuild) { |
| 378 | self->AssertThreadSuspensionIsAllowable(); |
| 379 | CHECK_EQ(kRunnable, self->GetState()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 380 | CHECK_STREQ(GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(), shorty); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Push a transition back into managed code onto the linked list in thread. |
| 384 | ManagedStack fragment; |
| 385 | self->PushManagedStackFragment(&fragment); |
| 386 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 387 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 388 | // Call the invoke stub, passing everything as arguments. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 389 | // If the runtime is not yet started or it is required by the debugger, then perform the |
| 390 | // Invocation by the interpreter. |
| 391 | if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) { |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 392 | if (IsStatic()) { |
| 393 | art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result); |
| 394 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 395 | mirror::Object* receiver = |
| 396 | reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr(); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 397 | art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 398 | } |
| 399 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 400 | DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), sizeof(void*)); |
| 401 | |
| 402 | constexpr bool kLogInvocationStartAndReturn = false; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 403 | bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 404 | if (LIKELY(have_quick_code)) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 405 | if (kLogInvocationStartAndReturn) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | LOG(INFO) << StringPrintf( |
| 407 | "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(), |
| 408 | GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0)); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 409 | } |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 410 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 411 | // Ensure that we won't be accidentally calling quick compiled code when -Xint. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 412 | if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
| 413 | DCHECK(!runtime->UseJit()); |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 414 | CHECK(IsEntrypointInterpreter()) |
| 415 | << "Don't call compiled code when -Xint " << PrettyMethod(this); |
| 416 | } |
| 417 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 418 | if (!IsStatic()) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 419 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 420 | } else { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 421 | (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 422 | } |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 423 | if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 424 | // Unusual case where we were running generated code and an |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 425 | // exception was thrown to force the activations to be removed from the |
| 426 | // stack. Continue execution in the interpreter. |
| 427 | self->ClearException(); |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 428 | ShadowFrame* shadow_frame = |
| 429 | self->PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame); |
Sebastien Hertz | 0747466 | 2015-08-25 15:12:33 +0000 | [diff] [blame^] | 430 | mirror::Throwable* pending_exception = nullptr; |
| 431 | self->PopDeoptimizationContext(result, &pending_exception); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 432 | self->SetTopOfStack(nullptr); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 433 | self->SetTopOfShadowStack(shadow_frame); |
Sebastien Hertz | 0747466 | 2015-08-25 15:12:33 +0000 | [diff] [blame^] | 434 | |
| 435 | // Restore the exception that was pending before deoptimization then interpret the |
| 436 | // deoptimized frames. |
| 437 | if (pending_exception != nullptr) { |
| 438 | self->SetException(pending_exception); |
| 439 | } |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 440 | interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result); |
| 441 | } |
| 442 | if (kLogInvocationStartAndReturn) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 443 | LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(), |
| 444 | GetEntryPointFromQuickCompiledCode()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 445 | } |
| 446 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 447 | LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null"; |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 448 | if (result != nullptr) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 449 | result->SetJ(0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // Pop transition. |
| 455 | self->PopManagedStackFragment(fragment); |
| 456 | } |
| 457 | |
Ian Rogers | 1a10218 | 2014-12-02 17:49:19 -0800 | [diff] [blame] | 458 | // Counts the number of references in the parameter list of the corresponding method. |
| 459 | // Note: Thus does _not_ include "this" for non-static methods. |
| 460 | static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 461 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 1a10218 | 2014-12-02 17:49:19 -0800 | [diff] [blame] | 462 | uint32_t shorty_len; |
| 463 | const char* shorty = method->GetShorty(&shorty_len); |
| 464 | uint32_t refs = 0; |
| 465 | for (uint32_t i = 1; i < shorty_len ; ++i) { |
| 466 | if (shorty[i] == 'L') { |
| 467 | refs++; |
| 468 | } |
| 469 | } |
| 470 | return refs; |
| 471 | } |
| 472 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 473 | QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 474 | Runtime* runtime = Runtime::Current(); |
Daniel Mihalyi | e14f2b3 | 2014-10-10 18:24:11 +0200 | [diff] [blame] | 475 | |
| 476 | if (UNLIKELY(IsAbstract())) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 477 | return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
| 478 | } |
Daniel Mihalyi | e14f2b3 | 2014-10-10 18:24:11 +0200 | [diff] [blame] | 479 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 480 | // This goes before IsProxyMethod since runtime methods have a null declaring class. |
| 481 | if (UNLIKELY(IsRuntimeMethod())) { |
| 482 | return runtime->GetRuntimeMethodFrameInfo(this); |
| 483 | } |
| 484 | |
Daniel Mihalyi | e14f2b3 | 2014-10-10 18:24:11 +0200 | [diff] [blame] | 485 | // For Proxy method we add special handling for the direct method case (there is only one |
| 486 | // direct method - constructor). Direct method is cloned from original |
| 487 | // java.lang.reflect.Proxy class together with code and as a result it is executed as usual |
| 488 | // quick compiled method without any stubs. So the frame info should be returned as it is a |
| 489 | // quick method not a stub. However, if instrumentation stubs are installed, the |
| 490 | // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an |
| 491 | // oat code pointer, thus we have to add a special case here. |
| 492 | if (UNLIKELY(IsProxyMethod())) { |
| 493 | if (IsDirect()) { |
| 494 | CHECK(IsConstructor()); |
| 495 | return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode())); |
| 496 | } else { |
| 497 | return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
| 498 | } |
| 499 | } |
| 500 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 501 | const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 502 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 503 | // On failure, instead of null we get the quick-generic-jni-trampoline for native method |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 504 | // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline) |
| 505 | // for non-native methods. And we really shouldn't see a failure for non-native methods here. |
| 506 | DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point)); |
| 507 | |
| 508 | if (class_linker->IsQuickGenericJniStub(entry_point)) { |
| 509 | // Generic JNI frame. |
| 510 | DCHECK(IsNative()); |
Ian Rogers | 1a10218 | 2014-12-02 17:49:19 -0800 | [diff] [blame] | 511 | uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(this) + 1; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 512 | size_t scope_size = HandleScope::SizeOf(handle_refs); |
| 513 | QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs); |
| 514 | |
| 515 | // Callee saves + handle scope + method ref + alignment |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 516 | // Note: -sizeof(void*) since callee-save frame stores a whole method pointer. |
| 517 | size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() - sizeof(void*) + |
| 518 | sizeof(ArtMethod*) + scope_size, kStackAlignment); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 519 | return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask()); |
| 520 | } |
| 521 | |
| 522 | const void* code_pointer = EntryPointToCodePointer(entry_point); |
| 523 | return GetQuickFrameInfo(code_pointer); |
| 524 | } |
| 525 | |
| 526 | void ArtMethod::RegisterNative(const void* native_method, bool is_fast) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | CHECK(IsNative()) << PrettyMethod(this); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 528 | CHECK(!IsFastNative()) << PrettyMethod(this); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 529 | CHECK(native_method != nullptr) << PrettyMethod(this); |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 530 | if (is_fast) { |
| 531 | SetAccessFlags(GetAccessFlags() | kAccFastNative); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 532 | } |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 533 | SetEntryPointFromJni(native_method); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 536 | void ArtMethod::UnregisterNative() { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 537 | CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 538 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 539 | RegisterNative(GetJniDlsymLookupStub(), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 542 | bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) { |
| 543 | auto* dex_cache = GetDexCache(); |
| 544 | auto* dex_file = dex_cache->GetDexFile(); |
| 545 | const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 546 | const auto& proto_id = dex_file->GetMethodPrototype(method_id); |
| 547 | const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id); |
| 548 | auto count = proto_params != nullptr ? proto_params->Size() : 0u; |
| 549 | auto param_len = params.Get() != nullptr ? params->GetLength() : 0u; |
| 550 | if (param_len != count) { |
| 551 | return false; |
| 552 | } |
| 553 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 554 | for (size_t i = 0; i < count; ++i) { |
| 555 | auto type_idx = proto_params->GetTypeItem(i).type_idx_; |
| 556 | auto* type = cl->ResolveType(type_idx, this); |
| 557 | if (type == nullptr) { |
| 558 | Thread::Current()->AssertPendingException(); |
| 559 | return false; |
| 560 | } |
| 561 | if (type != params->GetWithoutChecks(i)) { |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 | return true; |
| 566 | } |
| 567 | |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 568 | const uint8_t* ArtMethod::GetQuickenedInfo() { |
| 569 | bool found = false; |
| 570 | OatFile::OatMethod oat_method = |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 571 | Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found); |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 572 | if (!found || (oat_method.GetQuickCode() != nullptr)) { |
| 573 | return nullptr; |
| 574 | } |
| 575 | return oat_method.GetVmapTable(); |
| 576 | } |
| 577 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 578 | } // namespace art |