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 | |
Andreas Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 19 | #include <cstddef> |
| 20 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "arch/context.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "base/stringpiece.h" |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 25 | #include "class_linker-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 26 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 28 | #include "dex_file_annotations.h" |
Ian Rogers | c449aa8 | 2013-07-29 14:35:46 -0700 | [diff] [blame] | 29 | #include "dex_instruction.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 30 | #include "entrypoints/runtime_asm_entrypoints.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "jit/jit.h" |
| 34 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 35 | #include "jit/profiling_info.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "jni_internal.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "mirror/class-inl.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 38 | #include "mirror/executable.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "mirror/object_array-inl.h" |
| 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/string.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 42 | #include "oat_file-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 43 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 44 | #include "well_known_classes.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | |
| 46 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 48 | extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 49 | const char*); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 50 | extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 51 | const char*); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 52 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 53 | ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, |
| 54 | jobject jlr_method) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method); |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 56 | DCHECK(executable != nullptr); |
| 57 | return executable->GetArtMethod(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 60 | mirror::String* ArtMethod::GetNameAsString(Thread* self) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 61 | CHECK(!IsProxyMethod()); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 62 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 63 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache())); |
| 64 | auto* dex_file = dex_cache->GetDexFile(); |
| 65 | uint32_t dex_method_idx = GetDexMethodIndex(); |
| 66 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 67 | return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_, |
| 68 | dex_cache); |
| 69 | } |
| 70 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 71 | void ArtMethod::ThrowInvocationTimeError() { |
| 72 | DCHECK(!IsInvokable()); |
| 73 | // NOTE: IsDefaultConflicting must be first since the actual method might or might not be abstract |
| 74 | // due to the way we select it. |
| 75 | if (IsDefaultConflicting()) { |
| 76 | ThrowIncompatibleClassChangeErrorForMethodConflict(this); |
| 77 | } else { |
| 78 | DCHECK(IsAbstract()); |
| 79 | ThrowAbstractMethodError(this); |
| 80 | } |
| 81 | } |
| 82 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 83 | InvokeType ArtMethod::GetInvokeType() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | // TODO: kSuper? |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 85 | if (IsStatic()) { |
Nicolas Geoffray | 12abcbd | 2016-06-06 15:51:58 +0000 | [diff] [blame] | 86 | return kStatic; |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 87 | } else if (GetDeclaringClass()->IsInterface()) { |
| 88 | return kInterface; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | } else if (IsDirect()) { |
| 90 | return kDirect; |
| 91 | } else { |
| 92 | return kVirtual; |
| 93 | } |
| 94 | } |
| 95 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 96 | size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) { |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 97 | CHECK_LE(1U, shorty.length()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | uint32_t num_registers = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 99 | for (size_t i = 1; i < shorty.length(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | char ch = shorty[i]; |
| 101 | if (ch == 'D' || ch == 'J') { |
| 102 | num_registers += 2; |
| 103 | } else { |
| 104 | num_registers += 1; |
| 105 | } |
| 106 | } |
| 107 | return num_registers; |
| 108 | } |
| 109 | |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 110 | bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 111 | ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature"); |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 112 | const DexFile* dex_file = GetDexFile(); |
| 113 | const DexFile::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex()); |
| 114 | if (GetDexCache() == other->GetDexCache()) { |
| 115 | const DexFile::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 116 | return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_; |
| 117 | } |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 118 | const DexFile* dex_file2 = other->GetDexFile(); |
| 119 | const DexFile::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 120 | if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) { |
| 121 | return false; // Name mismatch. |
| 122 | } |
| 123 | return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2); |
| 124 | } |
| 125 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 126 | ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 127 | if (IsStatic()) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 128 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 130 | mirror::Class* declaring_class = GetDeclaringClass(); |
| 131 | mirror::Class* super_class = declaring_class->GetSuperClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | uint16_t method_index = GetMethodIndex(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 133 | ArtMethod* result = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | // Did this method override a super class method? If so load the result from the super class' |
| 135 | // vtable |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 136 | if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | result = super_class->GetVTableEntry(method_index, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 138 | } else { |
| 139 | // Method didn't override superclass method so search interfaces |
| 140 | if (IsProxyMethod()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 141 | result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size), |
| 142 | GetDexMethodIndex(), |
| 143 | pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 144 | CHECK_EQ(result, |
| 145 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
| 146 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 148 | for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 149 | mirror::Class* interface = iftable->GetInterface(i); |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 150 | for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) { |
| 151 | if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) { |
| 152 | result = &interface_method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
Alex Light | 6c8467f | 2015-11-20 15:03:26 -0800 | [diff] [blame] | 159 | DCHECK(result == nullptr || |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 160 | GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature( |
| 161 | result->GetInterfaceMethodIfProxy(pointer_size))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 162 | return result; |
| 163 | } |
| 164 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 165 | uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 166 | uint32_t name_and_signature_idx) { |
| 167 | const DexFile* dexfile = GetDexFile(); |
| 168 | const uint32_t dex_method_idx = GetDexMethodIndex(); |
| 169 | const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); |
| 170 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
| 171 | DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 172 | DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 173 | if (dexfile == &other_dexfile) { |
| 174 | return dex_method_idx; |
| 175 | } |
| 176 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 177 | const DexFile::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor); |
| 178 | if (other_type_id != nullptr) { |
| 179 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 180 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 181 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 182 | if (other_mid != nullptr) { |
| 183 | return other_dexfile.GetIndexForMethodId(*other_mid); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return DexFile::kDexNoIndex; |
| 187 | } |
| 188 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 189 | uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 190 | uint32_t dex_pc, bool* has_no_move_exception) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 191 | const DexFile::CodeItem* code_item = GetCodeItem(); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 192 | // Set aside the exception while we resolve its type. |
| 193 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 194 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 195 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 196 | self->ClearException(); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 197 | // Default to handler not found. |
| 198 | uint32_t found_dex_pc = DexFile::kDexNoIndex; |
| 199 | // Iterate over the catch handlers associated with dex_pc. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 200 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 201 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 202 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
| 203 | // Catch all case |
| 204 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 205 | found_dex_pc = it.GetHandlerAddress(); |
| 206 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 207 | } |
| 208 | // Does this catch exception type apply? |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 209 | mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx, |
| 210 | true /* resolve */, |
| 211 | pointer_size); |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame] | 212 | if (UNLIKELY(iter_exception_type == nullptr)) { |
| 213 | // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was |
| 214 | // removed by a pro-guard like tool. |
Andreas Gampe | 72b3e43 | 2014-05-13 21:42:05 -0700 | [diff] [blame] | 215 | // 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] | 216 | self->ClearException(); |
| 217 | // Delete any long jump context as this routine is called during a stack walk which will |
| 218 | // release its in use context at the end. |
| 219 | delete self->GetLongJumpContext(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 220 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 222 | } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 223 | found_dex_pc = it.GetHandlerAddress(); |
| 224 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 227 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 228 | const Instruction* first_catch_instr = |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 229 | Instruction::At(&code_item->insns_[found_dex_pc]); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 230 | *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION); |
| 231 | } |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 232 | // Put the exception back. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 233 | if (exception.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 234 | self->SetException(exception.Get()); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 235 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 236 | return found_dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 239 | 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] | 240 | const char* shorty) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 241 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
| 242 | ThrowStackOverflowError(self); |
| 243 | return; |
| 244 | } |
| 245 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | if (kIsDebugBuild) { |
| 247 | self->AssertThreadSuspensionIsAllowable(); |
| 248 | CHECK_EQ(kRunnable, self->GetState()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 249 | CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Push a transition back into managed code onto the linked list in thread. |
| 253 | ManagedStack fragment; |
| 254 | self->PushManagedStackFragment(&fragment); |
| 255 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 256 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 257 | // Call the invoke stub, passing everything as arguments. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 258 | // 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] | 259 | // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent |
| 260 | // cycling around the various JIT/Interpreter methods that handle method invocation. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 261 | if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) { |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 262 | if (IsStatic()) { |
Aart Bik | 0122320 | 2016-05-05 15:10:42 -0700 | [diff] [blame] | 263 | art::interpreter::EnterInterpreterFromInvoke( |
| 264 | self, this, nullptr, args, result, /*stay_in_interpreter*/ true); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 265 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 266 | mirror::Object* receiver = |
| 267 | reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr(); |
Aart Bik | 0122320 | 2016-05-05 15:10:42 -0700 | [diff] [blame] | 268 | art::interpreter::EnterInterpreterFromInvoke( |
| 269 | self, this, receiver, args + 1, result, /*stay_in_interpreter*/ true); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 270 | } |
| 271 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 272 | DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 273 | |
| 274 | constexpr bool kLogInvocationStartAndReturn = false; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 275 | bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 276 | if (LIKELY(have_quick_code)) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 277 | if (kLogInvocationStartAndReturn) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 278 | LOG(INFO) << StringPrintf( |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 279 | "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 280 | GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0)); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 281 | } |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 282 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 283 | // 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] | 284 | if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 285 | CHECK(!runtime->UseJitCompilation()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 286 | const void* oat_quick_code = (IsNative() || !IsInvokable() || IsProxyMethod()) |
| 287 | ? nullptr |
| 288 | : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 289 | CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 290 | << "Don't call compiled code when -Xint " << PrettyMethod(); |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 293 | if (!IsStatic()) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 294 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 295 | } else { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 296 | (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 297 | } |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 298 | if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 299 | // Unusual case where we were running generated code and an |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 300 | // exception was thrown to force the activations to be removed from the |
| 301 | // stack. Continue execution in the interpreter. |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 302 | self->DeoptimizeWithDeoptimizationException(result); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 303 | } |
| 304 | if (kLogInvocationStartAndReturn) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 305 | LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 306 | GetEntryPointFromQuickCompiledCode()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 307 | } |
| 308 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 309 | LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null"; |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 310 | if (result != nullptr) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 311 | result->SetJ(0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Pop transition. |
| 317 | self->PopManagedStackFragment(fragment); |
| 318 | } |
| 319 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 320 | void ArtMethod::RegisterNative(const void* native_method, bool is_fast) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 321 | CHECK(IsNative()) << PrettyMethod(); |
| 322 | CHECK(!IsFastNative()) << PrettyMethod(); |
| 323 | CHECK(native_method != nullptr) << PrettyMethod(); |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 324 | if (is_fast) { |
| 325 | SetAccessFlags(GetAccessFlags() | kAccFastNative); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 326 | } |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 327 | SetEntryPointFromJni(native_method); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 330 | void ArtMethod::UnregisterNative() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 331 | CHECK(IsNative() && !IsFastNative()) << PrettyMethod(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 332 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 333 | RegisterNative(GetJniDlsymLookupStub(), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 336 | bool ArtMethod::IsOverridableByDefaultMethod() { |
| 337 | return GetDeclaringClass()->IsInterface(); |
| 338 | } |
| 339 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 340 | bool ArtMethod::IsAnnotatedWithFastNative() { |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 341 | return IsAnnotatedWith(WellKnownClasses::dalvik_annotation_optimization_FastNative, |
| 342 | DexFile::kDexVisibilityBuild); |
| 343 | } |
| 344 | |
| 345 | bool ArtMethod::IsAnnotatedWithCriticalNative() { |
| 346 | return IsAnnotatedWith(WellKnownClasses::dalvik_annotation_optimization_CriticalNative, |
| 347 | DexFile::kDexVisibilityBuild); |
| 348 | } |
| 349 | |
| 350 | bool ArtMethod::IsAnnotatedWith(jclass klass, uint32_t visibility) { |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 351 | Thread* self = Thread::Current(); |
| 352 | ScopedObjectAccess soa(self); |
| 353 | StackHandleScope<1> shs(self); |
| 354 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 355 | ObjPtr<mirror::Class> annotation = soa.Decode<mirror::Class>(klass); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 356 | DCHECK(annotation->IsAnnotation()); |
| 357 | Handle<mirror::Class> annotation_handle(shs.NewHandle(annotation)); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 358 | |
| 359 | // Note: Resolves any method annotations' classes as a side-effect. |
| 360 | // -- This seems allowed by the spec since it says we can preload any classes |
| 361 | // referenced by another classes's constant pool table. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 362 | return annotations::IsMethodAnnotationPresent(this, annotation_handle, visibility); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 365 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, |
| 366 | uint16_t class_def_idx, |
| 367 | uint32_t method_idx) { |
| 368 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
| 369 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
| 370 | CHECK(class_data != nullptr); |
| 371 | ClassDataItemIterator it(dex_file, class_data); |
| 372 | // Skip fields |
| 373 | while (it.HasNextStaticField()) { |
| 374 | it.Next(); |
| 375 | } |
| 376 | while (it.HasNextInstanceField()) { |
| 377 | it.Next(); |
| 378 | } |
| 379 | // Process methods |
| 380 | size_t class_def_method_index = 0; |
| 381 | while (it.HasNextDirectMethod()) { |
| 382 | if (it.GetMemberIndex() == method_idx) { |
| 383 | return class_def_method_index; |
| 384 | } |
| 385 | class_def_method_index++; |
| 386 | it.Next(); |
| 387 | } |
| 388 | while (it.HasNextVirtualMethod()) { |
| 389 | if (it.GetMemberIndex() == method_idx) { |
| 390 | return class_def_method_index; |
| 391 | } |
| 392 | class_def_method_index++; |
| 393 | it.Next(); |
| 394 | } |
| 395 | DCHECK(!it.HasNext()); |
| 396 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
| 397 | UNREACHABLE(); |
| 398 | } |
| 399 | |
| 400 | static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, |
| 401 | PointerSize pointer_size, |
| 402 | bool* found) |
| 403 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 404 | // Although we overwrite the trampoline of non-static methods, we may get here via the resolution |
| 405 | // method for direct methods (or virtual methods made direct). |
| 406 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
| 407 | size_t oat_method_index; |
| 408 | if (method->IsStatic() || method->IsDirect()) { |
| 409 | // Simple case where the oat method index was stashed at load time. |
| 410 | oat_method_index = method->GetMethodIndex(); |
| 411 | } else { |
| 412 | // Compute the oat_method_index by search for its position in the declared virtual methods. |
| 413 | oat_method_index = declaring_class->NumDirectMethods(); |
| 414 | bool found_virtual = false; |
| 415 | for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) { |
| 416 | // Check method index instead of identity in case of duplicate method definitions. |
| 417 | if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) { |
| 418 | found_virtual = true; |
| 419 | break; |
| 420 | } |
| 421 | oat_method_index++; |
| 422 | } |
| 423 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 424 | << method->PrettyMethod(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 425 | } |
| 426 | DCHECK_EQ(oat_method_index, |
| 427 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
| 428 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
| 429 | method->GetDexMethodIndex())); |
| 430 | OatFile::OatClass oat_class = OatFile::FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 431 | declaring_class->GetDexClassDefIndex(), |
| 432 | found); |
| 433 | if (!(*found)) { |
| 434 | return OatFile::OatMethod::Invalid(); |
| 435 | } |
| 436 | return oat_class.GetOatMethod(oat_method_index); |
| 437 | } |
| 438 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 439 | bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) { |
| 440 | auto* dex_cache = GetDexCache(); |
| 441 | auto* dex_file = dex_cache->GetDexFile(); |
| 442 | const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 443 | const auto& proto_id = dex_file->GetMethodPrototype(method_id); |
| 444 | const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id); |
| 445 | auto count = proto_params != nullptr ? proto_params->Size() : 0u; |
| 446 | auto param_len = params.Get() != nullptr ? params->GetLength() : 0u; |
| 447 | if (param_len != count) { |
| 448 | return false; |
| 449 | } |
| 450 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 451 | for (size_t i = 0; i < count; ++i) { |
| 452 | auto type_idx = proto_params->GetTypeItem(i).type_idx_; |
| 453 | auto* type = cl->ResolveType(type_idx, this); |
| 454 | if (type == nullptr) { |
| 455 | Thread::Current()->AssertPendingException(); |
| 456 | return false; |
| 457 | } |
| 458 | if (type != params->GetWithoutChecks(i)) { |
| 459 | return false; |
| 460 | } |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 465 | const uint8_t* ArtMethod::GetQuickenedInfo(PointerSize pointer_size) { |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 466 | bool found = false; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 467 | OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 468 | if (!found || (oat_method.GetQuickCode() != nullptr)) { |
| 469 | return nullptr; |
| 470 | } |
| 471 | return oat_method.GetVmapTable(); |
| 472 | } |
| 473 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 474 | const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) { |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 475 | // Our callers should make sure they don't pass the instrumentation exit pc, |
| 476 | // as this method does not look at the side instrumentation stack. |
| 477 | DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())); |
| 478 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 479 | if (IsRuntimeMethod()) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 480 | return nullptr; |
| 481 | } |
| 482 | |
| 483 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 484 | const void* existing_entry_point = GetEntryPointFromQuickCompiledCode(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 485 | CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 486 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 487 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 488 | if (class_linker->IsQuickGenericJniStub(existing_entry_point)) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 489 | // The generic JNI does not have any method header. |
| 490 | return nullptr; |
| 491 | } |
| 492 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 493 | if (existing_entry_point == GetQuickProxyInvokeHandler()) { |
| 494 | DCHECK(IsProxyMethod() && !IsConstructor()); |
| 495 | // The proxy entry point does not have any method header. |
| 496 | return nullptr; |
| 497 | } |
| 498 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 499 | // Check whether the current entry point contains this pc. |
| 500 | if (!class_linker->IsQuickResolutionStub(existing_entry_point) && |
| 501 | !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) { |
| 502 | OatQuickMethodHeader* method_header = |
| 503 | OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 504 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 505 | if (method_header->Contains(pc)) { |
| 506 | return method_header; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // Check whether the pc is in the JIT code cache. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 511 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 512 | if (jit != nullptr) { |
| 513 | jit::JitCodeCache* code_cache = jit->GetCodeCache(); |
| 514 | OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this); |
| 515 | if (method_header != nullptr) { |
| 516 | DCHECK(method_header->Contains(pc)); |
| 517 | return method_header; |
| 518 | } else { |
Nicolas Geoffray | 2a524bd | 2016-03-01 12:18:47 +0000 | [diff] [blame] | 519 | DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc))) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 520 | << PrettyMethod() |
Nicolas Geoffray | 2a524bd | 2016-03-01 12:18:47 +0000 | [diff] [blame] | 521 | << ", pc=" << std::hex << pc |
| 522 | << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point) |
| 523 | << ", copy=" << std::boolalpha << IsCopied() |
| 524 | << ", proxy=" << std::boolalpha << IsProxyMethod(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | // The code has to be in an oat file. |
| 529 | bool found; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 530 | OatFile::OatMethod oat_method = |
| 531 | FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 532 | if (!found) { |
Nicolas Geoffray | 49e4396 | 2015-10-28 16:16:16 +0000 | [diff] [blame] | 533 | if (class_linker->IsQuickResolutionStub(existing_entry_point)) { |
| 534 | // We are running the generic jni stub, but the entry point of the method has not |
| 535 | // been updated yet. |
Nicolas Geoffray | 703c282 | 2015-10-30 12:23:16 +0000 | [diff] [blame] | 536 | DCHECK_EQ(pc, 0u) << "Should be a downcall"; |
| 537 | DCHECK(IsNative()); |
| 538 | return nullptr; |
| 539 | } |
| 540 | if (existing_entry_point == GetQuickInstrumentationEntryPoint()) { |
| 541 | // We are running the generic jni stub, but the method is being instrumented. |
| 542 | DCHECK_EQ(pc, 0u) << "Should be a downcall"; |
Nicolas Geoffray | 49e4396 | 2015-10-28 16:16:16 +0000 | [diff] [blame] | 543 | DCHECK(IsNative()); |
| 544 | return nullptr; |
| 545 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 546 | // Only for unit tests. |
| 547 | // TODO(ngeoffray): Update these tests to pass the right pc? |
| 548 | return OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
| 549 | } |
| 550 | const void* oat_entry_point = oat_method.GetQuickCode(); |
| 551 | if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 552 | DCHECK(IsNative()) << PrettyMethod(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 553 | return nullptr; |
| 554 | } |
| 555 | |
| 556 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point); |
| 557 | if (pc == 0) { |
| 558 | // This is a downcall, it can only happen for a native method. |
| 559 | DCHECK(IsNative()); |
| 560 | return method_header; |
| 561 | } |
| 562 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 563 | DCHECK(method_header->Contains(pc)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 564 | << PrettyMethod() |
Roland Levillain | 0b671c0 | 2016-08-19 12:02:34 +0100 | [diff] [blame] | 565 | << " " << std::hex << pc << " " << oat_entry_point |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 566 | << " " << (uintptr_t)(method_header->code_ + method_header->code_size_); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 567 | return method_header; |
| 568 | } |
| 569 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 570 | const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) { |
| 571 | bool found; |
| 572 | OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found); |
| 573 | if (found) { |
| 574 | return oat_method.GetQuickCode(); |
| 575 | } |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 579 | bool ArtMethod::HasAnyCompiledCode() { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 580 | if (IsNative() || !IsInvokable() || IsProxyMethod()) { |
| 581 | return false; |
| 582 | } |
| 583 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 584 | // Check whether the JIT has compiled it. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 585 | Runtime* runtime = Runtime::Current(); |
| 586 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 587 | if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) { |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | // Check whether we have AOT code. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 592 | return GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize()) != nullptr; |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 593 | } |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 594 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 595 | void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 596 | memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src), |
| 597 | Size(image_pointer_size)); |
| 598 | declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass()); |
| 599 | |
| 600 | // If the entry point of the method we are copying from is from JIT code, we just |
| 601 | // put the entry point of the new method to interpreter. We could set the entry point |
| 602 | // to the JIT code, but this would require taking the JIT code cache lock to notify |
| 603 | // it, which we do not want at this level. |
| 604 | Runtime* runtime = Runtime::Current(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 605 | if (runtime->UseJitCompilation()) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 606 | if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) { |
| 607 | SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size); |
| 608 | } |
| 609 | } |
| 610 | // Clear the profiling info for the same reasons as the JIT code. |
| 611 | if (!src->IsNative()) { |
| 612 | SetProfilingInfoPtrSize(nullptr, image_pointer_size); |
| 613 | } |
| 614 | // Clear hotness to let the JIT properly decide when to compile this method. |
| 615 | hotness_count_ = 0; |
| 616 | } |
| 617 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 618 | bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) { |
Andreas Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 619 | // Hijack this function to get access to PtrSizedFieldsOffset. |
| 620 | // |
| 621 | // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and |
| 622 | // 64-bit builds. |
| 623 | 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] | 624 | static_assert( |
| 625 | (sizeof(void*) != 4) || |
| 626 | (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)), |
| 627 | "Unexpected 32-bit class layout."); |
| 628 | static_assert( |
| 629 | (sizeof(void*) != 8) || |
| 630 | (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)), |
| 631 | "Unexpected 64-bit class layout."); |
Andreas Gampe | 479b1de | 2016-07-19 18:27:17 -0700 | [diff] [blame] | 632 | |
Andreas Gampe | 75f0885 | 2016-07-19 08:06:07 -0700 | [diff] [blame] | 633 | Runtime* runtime = Runtime::Current(); |
| 634 | if (runtime == nullptr) { |
| 635 | return true; |
| 636 | } |
| 637 | return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size; |
| 638 | } |
| 639 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 640 | std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) { |
| 641 | if (m == nullptr) { |
| 642 | return "null"; |
| 643 | } |
| 644 | return m->PrettyMethod(with_signature); |
| 645 | } |
| 646 | |
| 647 | std::string ArtMethod::PrettyMethod(bool with_signature) { |
| 648 | ArtMethod* m = this; |
| 649 | if (!m->IsRuntimeMethod()) { |
| 650 | m = m->GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 651 | } |
| 652 | std::string result(PrettyDescriptor(m->GetDeclaringClassDescriptor())); |
| 653 | result += '.'; |
| 654 | result += m->GetName(); |
| 655 | if (UNLIKELY(m->IsFastNative())) { |
| 656 | result += "!"; |
| 657 | } |
| 658 | if (with_signature) { |
| 659 | const Signature signature = m->GetSignature(); |
| 660 | std::string sig_as_string(signature.ToString()); |
| 661 | if (signature == Signature::NoSignature()) { |
| 662 | return result + sig_as_string; |
| 663 | } |
| 664 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 665 | PrettyArguments(sig_as_string.c_str()); |
| 666 | } |
| 667 | return result; |
| 668 | } |
| 669 | |
| 670 | std::string ArtMethod::JniShortName() { |
| 671 | std::string class_name(GetDeclaringClassDescriptor()); |
| 672 | // Remove the leading 'L' and trailing ';'... |
| 673 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 674 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
| 675 | class_name.erase(0, 1); |
| 676 | class_name.erase(class_name.size() - 1, 1); |
| 677 | |
| 678 | std::string method_name(GetName()); |
| 679 | |
| 680 | std::string short_name; |
| 681 | short_name += "Java_"; |
| 682 | short_name += MangleForJni(class_name); |
| 683 | short_name += "_"; |
| 684 | short_name += MangleForJni(method_name); |
| 685 | return short_name; |
| 686 | } |
| 687 | |
| 688 | std::string ArtMethod::JniLongName() { |
| 689 | std::string long_name; |
| 690 | long_name += JniShortName(); |
| 691 | long_name += "__"; |
| 692 | |
| 693 | std::string signature(GetSignature().ToString()); |
| 694 | signature.erase(0, 1); |
| 695 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 696 | |
| 697 | long_name += MangleForJni(signature); |
| 698 | |
| 699 | return long_name; |
| 700 | } |
| 701 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 702 | } // namespace art |