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