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" |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 33 | #include "jit/profiling_info.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "jni_internal.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 35 | #include "mapping_table.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "mirror/abstract_method.h" |
| 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/object_array-inl.h" |
| 39 | #include "mirror/object-inl.h" |
| 40 | #include "mirror/string.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 41 | #include "oat_file-inl.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 43 | #include "well_known_classes.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | |
| 45 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 47 | extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 48 | const char*); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 49 | extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 50 | const char*); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 51 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 52 | ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, |
| 53 | jobject jlr_method) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 54 | auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method); |
| 55 | DCHECK(abstract_method != nullptr); |
| 56 | return abstract_method->GetArtMethod(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 59 | mirror::String* ArtMethod::GetNameAsString(Thread* self) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 60 | CHECK(!IsProxyMethod()); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 61 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 62 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache())); |
| 63 | auto* dex_file = dex_cache->GetDexFile(); |
| 64 | uint32_t dex_method_idx = GetDexMethodIndex(); |
| 65 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 66 | return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_, |
| 67 | dex_cache); |
| 68 | } |
| 69 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 70 | InvokeType ArtMethod::GetInvokeType() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | // TODO: kSuper? |
| 72 | if (GetDeclaringClass()->IsInterface()) { |
| 73 | return kInterface; |
| 74 | } else if (IsStatic()) { |
| 75 | return kStatic; |
| 76 | } else if (IsDirect()) { |
| 77 | return kDirect; |
| 78 | } else { |
| 79 | return kVirtual; |
| 80 | } |
| 81 | } |
| 82 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 83 | size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) { |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 84 | CHECK_LE(1U, shorty.length()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | uint32_t num_registers = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 86 | for (size_t i = 1; i < shorty.length(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | char ch = shorty[i]; |
| 88 | if (ch == 'D' || ch == 'J') { |
| 89 | num_registers += 2; |
| 90 | } else { |
| 91 | num_registers += 1; |
| 92 | } |
| 93 | } |
| 94 | return num_registers; |
| 95 | } |
| 96 | |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 97 | static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 98 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 99 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature"); |
| 100 | const DexFile* dex_file = method1->GetDexFile(); |
| 101 | const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex()); |
| 102 | if (method1->GetDexCache() == method2->GetDexCache()) { |
| 103 | const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex()); |
| 104 | return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_; |
| 105 | } |
| 106 | const DexFile* dex_file2 = method2->GetDexFile(); |
| 107 | const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex()); |
| 108 | if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) { |
| 109 | return false; // Name mismatch. |
| 110 | } |
| 111 | return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2); |
| 112 | } |
| 113 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 114 | ArtMethod* ArtMethod::FindOverriddenMethod(size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | if (IsStatic()) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 116 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 118 | mirror::Class* declaring_class = GetDeclaringClass(); |
| 119 | mirror::Class* super_class = declaring_class->GetSuperClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | uint16_t method_index = GetMethodIndex(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 121 | ArtMethod* result = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | // Did this method override a super class method? If so load the result from the super class' |
| 123 | // vtable |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 124 | if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 125 | result = super_class->GetVTableEntry(method_index, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | } else { |
| 127 | // Method didn't override superclass method so search interfaces |
| 128 | if (IsProxyMethod()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 129 | result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size), |
| 130 | GetDexMethodIndex(), |
| 131 | pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | CHECK_EQ(result, |
| 133 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
| 134 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable(); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 136 | for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | mirror::Class* interface = iftable->GetInterface(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 138 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 139 | ArtMethod* interface_method = interface->GetVirtualMethod(j, pointer_size); |
| 140 | if (HasSameNameAndSignature( |
| 141 | this, interface_method->GetInterfaceMethodIfProxy(sizeof(void*)))) { |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 142 | result = interface_method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 149 | DCHECK(result == nullptr || HasSameNameAndSignature( |
| 150 | GetInterfaceMethodIfProxy(sizeof(void*)), result->GetInterfaceMethodIfProxy(sizeof(void*)))); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | return result; |
| 152 | } |
| 153 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 154 | uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 155 | uint32_t name_and_signature_idx) { |
| 156 | const DexFile* dexfile = GetDexFile(); |
| 157 | const uint32_t dex_method_idx = GetDexMethodIndex(); |
| 158 | const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); |
| 159 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
| 160 | DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 161 | DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 162 | if (dexfile == &other_dexfile) { |
| 163 | return dex_method_idx; |
| 164 | } |
| 165 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 166 | const DexFile::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor); |
| 167 | if (other_type_id != nullptr) { |
| 168 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 169 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 170 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 171 | if (other_mid != nullptr) { |
| 172 | return other_dexfile.GetIndexForMethodId(*other_mid); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | return DexFile::kDexNoIndex; |
| 176 | } |
| 177 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 178 | uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type, |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 179 | uint32_t dex_pc, bool* has_no_move_exception) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 180 | const DexFile::CodeItem* code_item = GetCodeItem(); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 181 | // Set aside the exception while we resolve its type. |
| 182 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 183 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 184 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 185 | self->ClearException(); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 186 | // Default to handler not found. |
| 187 | uint32_t found_dex_pc = DexFile::kDexNoIndex; |
| 188 | // Iterate over the catch handlers associated with dex_pc. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 189 | size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 190 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 191 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
| 192 | // Catch all case |
| 193 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 194 | found_dex_pc = it.GetHandlerAddress(); |
| 195 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 196 | } |
| 197 | // Does this catch exception type apply? |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 198 | mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx, |
| 199 | true /* resolve */, |
| 200 | pointer_size); |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame] | 201 | if (UNLIKELY(iter_exception_type == nullptr)) { |
| 202 | // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was |
| 203 | // removed by a pro-guard like tool. |
Andreas Gampe | 72b3e43 | 2014-05-13 21:42:05 -0700 | [diff] [blame] | 204 | // 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] | 205 | self->ClearException(); |
| 206 | // Delete any long jump context as this routine is called during a stack walk which will |
| 207 | // release its in use context at the end. |
| 208 | delete self->GetLongJumpContext(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 210 | << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 211 | } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 212 | found_dex_pc = it.GetHandlerAddress(); |
| 213 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 216 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 217 | const Instruction* first_catch_instr = |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 218 | Instruction::At(&code_item->insns_[found_dex_pc]); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 219 | *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION); |
| 220 | } |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 221 | // Put the exception back. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 222 | if (exception.Get() != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 223 | self->SetException(exception.Get()); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 224 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 225 | return found_dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 228 | 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] | 229 | const char* shorty) { |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 230 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
| 231 | ThrowStackOverflowError(self); |
| 232 | return; |
| 233 | } |
| 234 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 235 | if (kIsDebugBuild) { |
| 236 | self->AssertThreadSuspensionIsAllowable(); |
| 237 | CHECK_EQ(kRunnable, self->GetState()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 238 | CHECK_STREQ(GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(), shorty); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Push a transition back into managed code onto the linked list in thread. |
| 242 | ManagedStack fragment; |
| 243 | self->PushManagedStackFragment(&fragment); |
| 244 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 245 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 246 | // Call the invoke stub, passing everything as arguments. |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 247 | // If the runtime is not yet started or it is required by the debugger, then perform the |
| 248 | // Invocation by the interpreter. |
| 249 | if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) { |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 250 | if (IsStatic()) { |
| 251 | art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result); |
| 252 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 253 | mirror::Object* receiver = |
| 254 | reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr(); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 255 | art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 256 | } |
| 257 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 258 | DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), sizeof(void*)); |
| 259 | |
| 260 | constexpr bool kLogInvocationStartAndReturn = false; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 261 | bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 262 | if (LIKELY(have_quick_code)) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 263 | if (kLogInvocationStartAndReturn) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 264 | LOG(INFO) << StringPrintf( |
| 265 | "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(), |
| 266 | GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0)); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 267 | } |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 268 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 269 | // 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] | 270 | if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 271 | CHECK(!runtime->UseJit()); |
| 272 | const void* oat_quick_code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(this); |
| 273 | CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode()) |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 274 | << "Don't call compiled code when -Xint " << PrettyMethod(this); |
| 275 | } |
| 276 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 277 | if (!IsStatic()) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 278 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 279 | } else { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 280 | (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 281 | } |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 282 | if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 283 | // Unusual case where we were running generated code and an |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 284 | // exception was thrown to force the activations to be removed from the |
| 285 | // stack. Continue execution in the interpreter. |
| 286 | self->ClearException(); |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 287 | ShadowFrame* shadow_frame = |
| 288 | self->PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame); |
Sebastien Hertz | 0747466 | 2015-08-25 15:12:33 +0000 | [diff] [blame] | 289 | mirror::Throwable* pending_exception = nullptr; |
| 290 | self->PopDeoptimizationContext(result, &pending_exception); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 291 | self->SetTopOfStack(nullptr); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 292 | self->SetTopOfShadowStack(shadow_frame); |
Sebastien Hertz | 0747466 | 2015-08-25 15:12:33 +0000 | [diff] [blame] | 293 | |
| 294 | // Restore the exception that was pending before deoptimization then interpret the |
| 295 | // deoptimized frames. |
| 296 | if (pending_exception != nullptr) { |
| 297 | self->SetException(pending_exception); |
| 298 | } |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 299 | interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result); |
| 300 | } |
| 301 | if (kLogInvocationStartAndReturn) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 302 | LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(), |
| 303 | GetEntryPointFromQuickCompiledCode()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 304 | } |
| 305 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 306 | LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null"; |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 307 | if (result != nullptr) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 308 | result->SetJ(0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Pop transition. |
| 314 | self->PopManagedStackFragment(fragment); |
| 315 | } |
| 316 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 317 | void ArtMethod::RegisterNative(const void* native_method, bool is_fast) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 318 | CHECK(IsNative()) << PrettyMethod(this); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 319 | CHECK(!IsFastNative()) << PrettyMethod(this); |
Ian Rogers | f224751 | 2014-12-02 16:17:08 -0800 | [diff] [blame] | 320 | CHECK(native_method != nullptr) << PrettyMethod(this); |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 321 | if (is_fast) { |
| 322 | SetAccessFlags(GetAccessFlags() | kAccFastNative); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 323 | } |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 324 | SetEntryPointFromJni(native_method); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 327 | void ArtMethod::UnregisterNative() { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 328 | CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 330 | RegisterNative(GetJniDlsymLookupStub(), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 333 | bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) { |
| 334 | auto* dex_cache = GetDexCache(); |
| 335 | auto* dex_file = dex_cache->GetDexFile(); |
| 336 | const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 337 | const auto& proto_id = dex_file->GetMethodPrototype(method_id); |
| 338 | const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id); |
| 339 | auto count = proto_params != nullptr ? proto_params->Size() : 0u; |
| 340 | auto param_len = params.Get() != nullptr ? params->GetLength() : 0u; |
| 341 | if (param_len != count) { |
| 342 | return false; |
| 343 | } |
| 344 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 345 | for (size_t i = 0; i < count; ++i) { |
| 346 | auto type_idx = proto_params->GetTypeItem(i).type_idx_; |
| 347 | auto* type = cl->ResolveType(type_idx, this); |
| 348 | if (type == nullptr) { |
| 349 | Thread::Current()->AssertPendingException(); |
| 350 | return false; |
| 351 | } |
| 352 | if (type != params->GetWithoutChecks(i)) { |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 359 | const uint8_t* ArtMethod::GetQuickenedInfo() { |
| 360 | bool found = false; |
| 361 | OatFile::OatMethod oat_method = |
| 362 | Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found); |
| 363 | if (!found || (oat_method.GetQuickCode() != nullptr)) { |
| 364 | return nullptr; |
| 365 | } |
| 366 | return oat_method.GetVmapTable(); |
| 367 | } |
| 368 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 369 | const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) { |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame^] | 370 | // Our callers should make sure they don't pass the instrumentation exit pc, |
| 371 | // as this method does not look at the side instrumentation stack. |
| 372 | DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())); |
| 373 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 374 | if (IsRuntimeMethod()) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 375 | return nullptr; |
| 376 | } |
| 377 | |
| 378 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 379 | const void* existing_entry_point = GetEntryPointFromQuickCompiledCode(); |
| 380 | DCHECK(existing_entry_point != nullptr); |
| 381 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 382 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 383 | if (class_linker->IsQuickGenericJniStub(existing_entry_point)) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 384 | // The generic JNI does not have any method header. |
| 385 | return nullptr; |
| 386 | } |
| 387 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 388 | if (existing_entry_point == GetQuickProxyInvokeHandler()) { |
| 389 | DCHECK(IsProxyMethod() && !IsConstructor()); |
| 390 | // The proxy entry point does not have any method header. |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 394 | // Check whether the current entry point contains this pc. |
| 395 | if (!class_linker->IsQuickResolutionStub(existing_entry_point) && |
| 396 | !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) { |
| 397 | OatQuickMethodHeader* method_header = |
| 398 | OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 399 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 400 | if (method_header->Contains(pc)) { |
| 401 | return method_header; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Check whether the pc is in the JIT code cache. |
| 406 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 407 | if (jit != nullptr) { |
| 408 | jit::JitCodeCache* code_cache = jit->GetCodeCache(); |
| 409 | OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this); |
| 410 | if (method_header != nullptr) { |
| 411 | DCHECK(method_header->Contains(pc)); |
| 412 | return method_header; |
| 413 | } else { |
| 414 | DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc))) << std::hex << pc; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // The code has to be in an oat file. |
| 419 | bool found; |
| 420 | OatFile::OatMethod oat_method = class_linker->FindOatMethodFor(this, &found); |
| 421 | if (!found) { |
Nicolas Geoffray | 49e4396 | 2015-10-28 16:16:16 +0000 | [diff] [blame] | 422 | if (class_linker->IsQuickResolutionStub(existing_entry_point)) { |
| 423 | // We are running the generic jni stub, but the entry point of the method has not |
| 424 | // been updated yet. |
Nicolas Geoffray | 703c282 | 2015-10-30 12:23:16 +0000 | [diff] [blame] | 425 | DCHECK_EQ(pc, 0u) << "Should be a downcall"; |
| 426 | DCHECK(IsNative()); |
| 427 | return nullptr; |
| 428 | } |
| 429 | if (existing_entry_point == GetQuickInstrumentationEntryPoint()) { |
| 430 | // We are running the generic jni stub, but the method is being instrumented. |
| 431 | DCHECK_EQ(pc, 0u) << "Should be a downcall"; |
Nicolas Geoffray | 49e4396 | 2015-10-28 16:16:16 +0000 | [diff] [blame] | 432 | DCHECK(IsNative()); |
| 433 | return nullptr; |
| 434 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 435 | // Only for unit tests. |
| 436 | // TODO(ngeoffray): Update these tests to pass the right pc? |
| 437 | return OatQuickMethodHeader::FromEntryPoint(existing_entry_point); |
| 438 | } |
| 439 | const void* oat_entry_point = oat_method.GetQuickCode(); |
| 440 | if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) { |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame^] | 441 | DCHECK(IsNative()) << PrettyMethod(this); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 442 | return nullptr; |
| 443 | } |
| 444 | |
| 445 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point); |
| 446 | if (pc == 0) { |
| 447 | // This is a downcall, it can only happen for a native method. |
| 448 | DCHECK(IsNative()); |
| 449 | return method_header; |
| 450 | } |
| 451 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 452 | DCHECK(method_header->Contains(pc)) |
| 453 | << PrettyMethod(this) |
| 454 | << std::hex << pc << " " << oat_entry_point |
| 455 | << " " << (uintptr_t)(method_header->code_ + method_header->code_size_); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 456 | return method_header; |
| 457 | } |
| 458 | |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 459 | |
| 460 | void ArtMethod::CopyFrom(ArtMethod* src, size_t image_pointer_size) { |
| 461 | memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src), |
| 462 | Size(image_pointer_size)); |
| 463 | declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass()); |
| 464 | |
| 465 | // If the entry point of the method we are copying from is from JIT code, we just |
| 466 | // put the entry point of the new method to interpreter. We could set the entry point |
| 467 | // to the JIT code, but this would require taking the JIT code cache lock to notify |
| 468 | // it, which we do not want at this level. |
| 469 | Runtime* runtime = Runtime::Current(); |
| 470 | if (runtime->GetJit() != nullptr) { |
| 471 | if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) { |
| 472 | SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size); |
| 473 | } |
| 474 | } |
| 475 | // Clear the profiling info for the same reasons as the JIT code. |
| 476 | if (!src->IsNative()) { |
| 477 | SetProfilingInfoPtrSize(nullptr, image_pointer_size); |
| 478 | } |
| 479 | // Clear hotness to let the JIT properly decide when to compile this method. |
| 480 | hotness_count_ = 0; |
| 481 | } |
| 482 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 483 | } // namespace art |