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