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 | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "base/stringpiece.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 22 | #include "class-inl.h" |
| 23 | #include "dex_file-inl.h" |
Ian Rogers | c449aa8 | 2013-07-29 14:35:46 -0700 | [diff] [blame] | 24 | #include "dex_instruction.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 25 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "interpreter/interpreter.h" |
| 27 | #include "jni_internal.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 28 | #include "mapping_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "object-inl.h" |
| 30 | #include "object_array.h" |
| 31 | #include "object_array-inl.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 32 | #include "scoped_thread_state_change.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "string.h" |
| 34 | #include "object_utils.h" |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | namespace mirror { |
| 39 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 40 | extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 41 | extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 42 | const char*); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 43 | #ifdef __LP64__ |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 44 | extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, |
| 45 | const char*); |
| 46 | #endif |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 47 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | // TODO: get global references for these |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 49 | Class* ArtMethod::java_lang_reflect_ArtMethod_ = NULL; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 51 | ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccess& soa, jobject jlr_method) { |
| 52 | mirror::ArtField* f = |
| 53 | soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod); |
| 54 | mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod(); |
| 55 | DCHECK(method != nullptr); |
| 56 | return method; |
| 57 | } |
| 58 | |
| 59 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 60 | void ArtMethod::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 61 | if (java_lang_reflect_ArtMethod_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 62 | callback(reinterpret_cast<mirror::Object**>(&java_lang_reflect_ArtMethod_), arg, 0, |
| 63 | kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 67 | InvokeType ArtMethod::GetInvokeType() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | // TODO: kSuper? |
| 69 | if (GetDeclaringClass()->IsInterface()) { |
| 70 | return kInterface; |
| 71 | } else if (IsStatic()) { |
| 72 | return kStatic; |
| 73 | } else if (IsDirect()) { |
| 74 | return kDirect; |
| 75 | } else { |
| 76 | return kVirtual; |
| 77 | } |
| 78 | } |
| 79 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 80 | void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) { |
| 81 | CHECK(java_lang_reflect_ArtMethod_ == NULL); |
| 82 | CHECK(java_lang_reflect_ArtMethod != NULL); |
| 83 | java_lang_reflect_ArtMethod_ = java_lang_reflect_ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 86 | void ArtMethod::ResetClass() { |
| 87 | CHECK(java_lang_reflect_ArtMethod_ != NULL); |
| 88 | java_lang_reflect_ArtMethod_ = NULL; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 91 | void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 92 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 93 | new_dex_cache_strings); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 96 | void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 97 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 98 | new_dex_cache_methods); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 101 | void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 102 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 103 | new_dex_cache_classes); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 106 | size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | CHECK_LE(1, shorty.length()); |
| 108 | uint32_t num_registers = 0; |
| 109 | for (int i = 1; i < shorty.length(); ++i) { |
| 110 | char ch = shorty[i]; |
| 111 | if (ch == 'D' || ch == 'J') { |
| 112 | num_registers += 2; |
| 113 | } else { |
| 114 | num_registers += 1; |
| 115 | } |
| 116 | } |
| 117 | return num_registers; |
| 118 | } |
| 119 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 120 | bool ArtMethod::IsProxyMethod() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | return GetDeclaringClass()->IsProxyClass(); |
| 122 | } |
| 123 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 124 | ArtMethod* ArtMethod::FindOverriddenMethod() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | if (IsStatic()) { |
| 126 | return NULL; |
| 127 | } |
| 128 | Class* declaring_class = GetDeclaringClass(); |
| 129 | Class* super_class = declaring_class->GetSuperClass(); |
| 130 | uint16_t method_index = GetMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 131 | ObjectArray<ArtMethod>* super_class_vtable = super_class->GetVTable(); |
| 132 | ArtMethod* result = NULL; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | // Did this method override a super class method? If so load the result from the super class' |
| 134 | // vtable |
| 135 | if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) { |
| 136 | result = super_class_vtable->Get(method_index); |
| 137 | } else { |
| 138 | // Method didn't override superclass method so search interfaces |
| 139 | if (IsProxyMethod()) { |
| 140 | result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex()); |
| 141 | CHECK_EQ(result, |
| 142 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
| 143 | } else { |
| 144 | MethodHelper mh(this); |
| 145 | MethodHelper interface_mh; |
| 146 | IfTable* iftable = GetDeclaringClass()->GetIfTable(); |
| 147 | for (size_t i = 0; i < iftable->Count() && result == NULL; i++) { |
| 148 | Class* interface = iftable->GetInterface(i); |
| 149 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 150 | ArtMethod* interface_method = interface->GetVirtualMethod(j); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | interface_mh.ChangeMethod(interface_method); |
| 152 | if (mh.HasSameNameAndSignature(&interface_mh)) { |
| 153 | result = interface_method; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | #ifndef NDEBUG |
| 161 | MethodHelper result_mh(result); |
| 162 | DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh)); |
| 163 | #endif |
| 164 | return result; |
| 165 | } |
| 166 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | uintptr_t ArtMethod::NativePcOffset(const uintptr_t pc) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 168 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this); |
| 169 | return pc - reinterpret_cast<uintptr_t>(code); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 172 | uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 173 | if (IsPortableCompiled()) { |
| 174 | // Portable doesn't use the machine pc, we just use dex pc instead. |
| 175 | return static_cast<uint32_t>(pc); |
| 176 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 177 | MappingTable table(GetMappingTable()); |
| 178 | if (table.TotalSize() == 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 179 | DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this); |
| 180 | return DexFile::kDexNoIndex; // Special no mapping case |
| 181 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 182 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this); |
| 183 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 184 | // Assume the caller wants a pc-to-dex mapping so check here first. |
| 185 | typedef MappingTable::PcToDexIterator It; |
| 186 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 187 | if (cur.NativePcOffset() == sought_offset) { |
| 188 | return cur.DexPc(); |
| 189 | } |
| 190 | } |
| 191 | // Now check dex-to-pc mappings. |
| 192 | typedef MappingTable::DexToPcIterator It2; |
| 193 | for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 194 | if (cur.NativePcOffset() == sought_offset) { |
| 195 | return cur.DexPc(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 198 | if (abort_on_failure) { |
| 199 | LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset) |
| 200 | << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code |
| 201 | << ") in " << PrettyMethod(this); |
| 202 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 203 | return DexFile::kDexNoIndex; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 206 | uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 207 | MappingTable table(GetMappingTable()); |
| 208 | if (table.TotalSize() == 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | DCHECK_EQ(dex_pc, 0U); |
| 210 | return 0; // Special no mapping/pc == 0 case |
| 211 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 212 | // Assume the caller wants a dex-to-pc mapping so check here first. |
| 213 | typedef MappingTable::DexToPcIterator It; |
| 214 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 215 | if (cur.DexPc() == dex_pc) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 216 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 217 | return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 220 | // Now check pc-to-dex mappings. |
| 221 | typedef MappingTable::PcToDexIterator It2; |
| 222 | for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 223 | if (cur.DexPc() == dex_pc) { |
| 224 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this); |
| 225 | return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset(); |
| 226 | } |
| 227 | } |
| 228 | LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 229 | << " in " << PrettyMethod(this); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 233 | uint32_t ArtMethod::FindCatchBlock(Handle<Class>& exception_type, uint32_t dex_pc, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 234 | bool* has_no_move_exception) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 235 | MethodHelper mh(this); |
| 236 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 237 | // Set aside the exception while we resolve its type. |
| 238 | Thread* self = Thread::Current(); |
| 239 | ThrowLocation throw_location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 240 | StackHandleScope<1> hs(self); |
| 241 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location))); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 242 | self->ClearException(); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 243 | // Default to handler not found. |
| 244 | uint32_t found_dex_pc = DexFile::kDexNoIndex; |
| 245 | // Iterate over the catch handlers associated with dex_pc. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 247 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
| 248 | // Catch all case |
| 249 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 250 | found_dex_pc = it.GetHandlerAddress(); |
| 251 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | } |
| 253 | // Does this catch exception type apply? |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 254 | Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 255 | if (exception_type.Get() == nullptr) { |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 256 | self->ClearException(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 257 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 258 | << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 259 | } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 260 | found_dex_pc = it.GetHandlerAddress(); |
| 261 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 264 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 265 | const Instruction* first_catch_instr = |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 266 | Instruction::At(&code_item->insns_[found_dex_pc]); |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 267 | *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION); |
| 268 | } |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 269 | // Put the exception back. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 270 | if (exception.Get() != nullptr) { |
| 271 | self->SetException(throw_location, exception.Get()); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 272 | } |
Ian Rogers | 9e8f45e | 2013-07-31 10:58:53 -0700 | [diff] [blame] | 273 | return found_dex_pc; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 276 | 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] | 277 | const char* shorty) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 278 | if (kIsDebugBuild) { |
| 279 | self->AssertThreadSuspensionIsAllowable(); |
| 280 | CHECK_EQ(kRunnable, self->GetState()); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 281 | CHECK_STREQ(MethodHelper(this).GetShorty(), shorty); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Push a transition back into managed code onto the linked list in thread. |
| 285 | ManagedStack fragment; |
| 286 | self->PushManagedStackFragment(&fragment); |
| 287 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 288 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame] | 289 | // Call the invoke stub, passing everything as arguments. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 290 | if (UNLIKELY(!runtime->IsStarted())) { |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 291 | if (IsStatic()) { |
| 292 | art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result); |
| 293 | } else { |
| 294 | Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr(); |
| 295 | art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 296 | } |
| 297 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 298 | const bool kLogInvocationStartAndReturn = false; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 299 | bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; |
| 300 | bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr; |
| 301 | if (LIKELY(have_quick_code || have_portable_code)) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 302 | if (kLogInvocationStartAndReturn) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 303 | LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(), |
| 304 | have_quick_code ? "quick" : "portable", |
| 305 | have_quick_code ? GetEntryPointFromQuickCompiledCode() |
| 306 | : GetEntryPointFromPortableCompiledCode()); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 307 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 308 | if (!IsPortableCompiled()) { |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 309 | #ifdef __LP64__ |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 310 | if (!IsStatic()) { |
| 311 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
| 312 | } else { |
| 313 | (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); |
| 314 | } |
| 315 | #else |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 316 | (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 317 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 318 | } else { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 319 | (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 320 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 321 | if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) { |
| 322 | // Unusual case where we were running generated code and an |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 323 | // exception was thrown to force the activations to be removed from the |
| 324 | // stack. Continue execution in the interpreter. |
| 325 | self->ClearException(); |
| 326 | ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 327 | self->SetTopOfStack(nullptr, 0); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 328 | self->SetTopOfShadowStack(shadow_frame); |
| 329 | interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result); |
| 330 | } |
| 331 | if (kLogInvocationStartAndReturn) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 332 | LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(), |
| 333 | have_quick_code ? "quick" : "portable", |
| 334 | have_quick_code ? GetEntryPointFromQuickCompiledCode() |
| 335 | : GetEntryPointFromPortableCompiledCode()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 336 | } |
| 337 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 338 | LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null"; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 339 | if (result != NULL) { |
| 340 | result->SetJ(0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Pop transition. |
| 346 | self->PopManagedStackFragment(fragment); |
| 347 | } |
| 348 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 349 | bool ArtMethod::IsRegistered() { |
| 350 | void* native_method = |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 351 | GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 352 | CHECK(native_method != nullptr); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 353 | void* jni_stub = GetJniDlsymLookupStub(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 354 | return native_method != jni_stub; |
| 355 | } |
| 356 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 357 | void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 358 | DCHECK(Thread::Current() == self); |
| 359 | CHECK(IsNative()) << PrettyMethod(this); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 360 | CHECK(!IsFastNative()) << PrettyMethod(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | CHECK(native_method != NULL) << PrettyMethod(this); |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 362 | if (is_fast) { |
| 363 | SetAccessFlags(GetAccessFlags() | kAccFastNative); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 364 | } |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 365 | SetNativeMethod(native_method); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 368 | void ArtMethod::UnregisterNative(Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 369 | CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 370 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 371 | RegisterNative(self, GetJniDlsymLookupStub(), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 374 | const void* ArtMethod::GetOatCodePointer() { |
| 375 | if (IsPortableCompiled() || IsNative() || IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) { |
| 376 | return nullptr; |
| 377 | } |
| 378 | Runtime* runtime = Runtime::Current(); |
| 379 | const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this); |
| 380 | // On failure, instead of nullptr we get the quick-to-interpreter-bridge (but not the trampoline). |
| 381 | DCHECK(entry_point != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker())); |
| 382 | if (entry_point == GetQuickToInterpreterBridge()) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | return EntryPointToCodePointer(entry_point); |
| 386 | } |
| 387 | |
| 388 | const uint8_t* ArtMethod::GetMappingTable() { |
| 389 | const void* code = GetOatCodePointer(); |
| 390 | if (code == nullptr) { |
| 391 | return nullptr; |
| 392 | } |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 393 | uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].mapping_table_offset_; |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 394 | if (UNLIKELY(offset == 0u)) { |
| 395 | return nullptr; |
| 396 | } |
| 397 | return reinterpret_cast<const uint8_t*>(code) - offset; |
| 398 | } |
| 399 | |
| 400 | const uint8_t* ArtMethod::GetVmapTable() { |
| 401 | const void* code = GetOatCodePointer(); |
| 402 | if (code == nullptr) { |
| 403 | return nullptr; |
| 404 | } |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 405 | uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].vmap_table_offset_; |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 406 | if (UNLIKELY(offset == 0u)) { |
| 407 | return nullptr; |
| 408 | } |
| 409 | return reinterpret_cast<const uint8_t*>(code) - offset; |
| 410 | } |
| 411 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 412 | } // namespace mirror |
| 413 | } // namespace art |