art_method.cc revision 0398e171f206cd3b140a358ac31b0a3760380df1
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 17#include "art_method.h" 18 19#include "arch/context.h" 20#include "art_field-inl.h" 21#include "art_method-inl.h" 22#include "base/stringpiece.h" 23#include "class-inl.h" 24#include "dex_file-inl.h" 25#include "dex_instruction.h" 26#include "gc/accounting/card_table-inl.h" 27#include "interpreter/interpreter.h" 28#include "jni_internal.h" 29#include "mapping_table.h" 30#include "method_helper.h" 31#include "object_array-inl.h" 32#include "object_array.h" 33#include "object-inl.h" 34#include "scoped_thread_state_change.h" 35#include "string.h" 36#include "well_known_classes.h" 37 38namespace art { 39namespace mirror { 40 41extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char); 42extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, 43 const char*); 44#ifdef __LP64__ 45extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, 46 const char*); 47#endif 48 49// TODO: get global references for these 50Class* ArtMethod::java_lang_reflect_ArtMethod_ = NULL; 51 52ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa, 53 jobject jlr_method) { 54 mirror::ArtField* f = 55 soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod); 56 mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod(); 57 DCHECK(method != nullptr); 58 return method; 59} 60 61 62void ArtMethod::VisitRoots(RootCallback* callback, void* arg) { 63 if (java_lang_reflect_ArtMethod_ != nullptr) { 64 callback(reinterpret_cast<mirror::Object**>(&java_lang_reflect_ArtMethod_), arg, 0, 65 kRootStickyClass); 66 } 67} 68 69InvokeType ArtMethod::GetInvokeType() { 70 // TODO: kSuper? 71 if (GetDeclaringClass()->IsInterface()) { 72 return kInterface; 73 } else if (IsStatic()) { 74 return kStatic; 75 } else if (IsDirect()) { 76 return kDirect; 77 } else { 78 return kVirtual; 79 } 80} 81 82void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) { 83 CHECK(java_lang_reflect_ArtMethod_ == NULL); 84 CHECK(java_lang_reflect_ArtMethod != NULL); 85 java_lang_reflect_ArtMethod_ = java_lang_reflect_ArtMethod; 86} 87 88void ArtMethod::ResetClass() { 89 CHECK(java_lang_reflect_ArtMethod_ != NULL); 90 java_lang_reflect_ArtMethod_ = NULL; 91} 92 93void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { 94 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_), 95 new_dex_cache_strings); 96} 97 98void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) { 99 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_), 100 new_dex_cache_methods); 101} 102 103void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { 104 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_), 105 new_dex_cache_classes); 106} 107 108size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) { 109 CHECK_LE(1, shorty.length()); 110 uint32_t num_registers = 0; 111 for (int i = 1; i < shorty.length(); ++i) { 112 char ch = shorty[i]; 113 if (ch == 'D' || ch == 'J') { 114 num_registers += 2; 115 } else { 116 num_registers += 1; 117 } 118 } 119 return num_registers; 120} 121 122bool ArtMethod::IsProxyMethod() { 123 return GetDeclaringClass()->IsProxyClass(); 124} 125 126ArtMethod* ArtMethod::FindOverriddenMethod() { 127 if (IsStatic()) { 128 return NULL; 129 } 130 Class* declaring_class = GetDeclaringClass(); 131 Class* super_class = declaring_class->GetSuperClass(); 132 uint16_t method_index = GetMethodIndex(); 133 ArtMethod* result = NULL; 134 // Did this method override a super class method? If so load the result from the super class' 135 // vtable 136 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) { 137 result = super_class->GetVTableEntry(method_index); 138 } else { 139 // Method didn't override superclass method so search interfaces 140 if (IsProxyMethod()) { 141 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex()); 142 CHECK_EQ(result, 143 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); 144 } else { 145 StackHandleScope<2> hs(Thread::Current()); 146 MethodHelper mh(hs.NewHandle(this)); 147 MethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); 148 IfTable* iftable = GetDeclaringClass()->GetIfTable(); 149 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) { 150 Class* interface = iftable->GetInterface(i); 151 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { 152 interface_mh.ChangeMethod(interface->GetVirtualMethod(j)); 153 if (mh.HasSameNameAndSignature(&interface_mh)) { 154 result = interface_mh.GetMethod(); 155 break; 156 } 157 } 158 } 159 } 160 } 161 if (kIsDebugBuild) { 162 StackHandleScope<2> hs(Thread::Current()); 163 MethodHelper result_mh(hs.NewHandle(result)); 164 MethodHelper this_mh(hs.NewHandle(this)); 165 DCHECK(result == nullptr || this_mh.HasSameNameAndSignature(&result_mh)); 166 } 167 return result; 168} 169 170uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) { 171 if (IsPortableCompiled()) { 172 // Portable doesn't use the machine pc, we just use dex pc instead. 173 return static_cast<uint32_t>(pc); 174 } 175 const void* entry_point = GetQuickOatEntryPoint(); 176 MappingTable table( 177 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr); 178 if (table.TotalSize() == 0) { 179 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping 180 // but they have no suspend checks and, consequently, we never call ToDexPc() for them. 181 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this); 182 return DexFile::kDexNoIndex; // Special no mapping case 183 } 184 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point); 185 // Assume the caller wants a pc-to-dex mapping so check here first. 186 typedef MappingTable::PcToDexIterator It; 187 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 188 if (cur.NativePcOffset() == sought_offset) { 189 return cur.DexPc(); 190 } 191 } 192 // Now check dex-to-pc mappings. 193 typedef MappingTable::DexToPcIterator It2; 194 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 195 if (cur.NativePcOffset() == sought_offset) { 196 return cur.DexPc(); 197 } 198 } 199 if (abort_on_failure) { 200 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset) 201 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point 202 << ") in " << PrettyMethod(this); 203 } 204 return DexFile::kDexNoIndex; 205} 206 207uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) { 208 const void* entry_point = GetQuickOatEntryPoint(); 209 MappingTable table( 210 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr); 211 if (table.TotalSize() == 0) { 212 DCHECK_EQ(dex_pc, 0U); 213 return 0; // Special no mapping/pc == 0 case 214 } 215 // Assume the caller wants a dex-to-pc mapping so check here first. 216 typedef MappingTable::DexToPcIterator It; 217 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 218 if (cur.DexPc() == dex_pc) { 219 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset(); 220 } 221 } 222 // Now check pc-to-dex mappings. 223 typedef MappingTable::PcToDexIterator It2; 224 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 225 if (cur.DexPc() == dex_pc) { 226 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset(); 227 } 228 } 229 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc 230 << " in " << PrettyMethod(this); 231 return 0; 232} 233 234uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type, 235 uint32_t dex_pc, bool* has_no_move_exception) { 236 MethodHelper mh(h_this); 237 const DexFile::CodeItem* code_item = h_this->GetCodeItem(); 238 // Set aside the exception while we resolve its type. 239 Thread* self = Thread::Current(); 240 ThrowLocation throw_location; 241 StackHandleScope<1> hs(self); 242 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location))); 243 bool is_exception_reported = self->IsExceptionReportedToInstrumentation(); 244 self->ClearException(); 245 // Default to handler not found. 246 uint32_t found_dex_pc = DexFile::kDexNoIndex; 247 // Iterate over the catch handlers associated with dex_pc. 248 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { 249 uint16_t iter_type_idx = it.GetHandlerTypeIndex(); 250 // Catch all case 251 if (iter_type_idx == DexFile::kDexNoIndex16) { 252 found_dex_pc = it.GetHandlerAddress(); 253 break; 254 } 255 // Does this catch exception type apply? 256 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx); 257 if (UNLIKELY(iter_exception_type == nullptr)) { 258 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was 259 // removed by a pro-guard like tool. 260 // Note: this is not RI behavior. RI would have failed when loading the class. 261 self->ClearException(); 262 // Delete any long jump context as this routine is called during a stack walk which will 263 // release its in use context at the end. 264 delete self->GetLongJumpContext(); 265 LOG(WARNING) << "Unresolved exception class when finding catch block: " 266 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx)); 267 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) { 268 found_dex_pc = it.GetHandlerAddress(); 269 break; 270 } 271 } 272 if (found_dex_pc != DexFile::kDexNoIndex) { 273 const Instruction* first_catch_instr = 274 Instruction::At(&code_item->insns_[found_dex_pc]); 275 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION); 276 } 277 // Put the exception back. 278 if (exception.Get() != nullptr) { 279 self->SetException(throw_location, exception.Get()); 280 self->SetExceptionReportedToInstrumentation(is_exception_reported); 281 } 282 return found_dex_pc; 283} 284 285void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result, 286 const char* shorty) { 287 if (kIsDebugBuild) { 288 self->AssertThreadSuspensionIsAllowable(); 289 CHECK_EQ(kRunnable, self->GetState()); 290 CHECK_STREQ(GetShorty(), shorty); 291 } 292 293 // Push a transition back into managed code onto the linked list in thread. 294 ManagedStack fragment; 295 self->PushManagedStackFragment(&fragment); 296 297 Runtime* runtime = Runtime::Current(); 298 // Call the invoke stub, passing everything as arguments. 299 if (UNLIKELY(!runtime->IsStarted())) { 300 if (IsStatic()) { 301 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result); 302 } else { 303 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr(); 304 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result); 305 } 306 } else { 307 const bool kLogInvocationStartAndReturn = false; 308 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr; 309 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr; 310 if (LIKELY(have_quick_code || have_portable_code)) { 311 if (kLogInvocationStartAndReturn) { 312 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(), 313 have_quick_code ? "quick" : "portable", 314 have_quick_code ? GetEntryPointFromQuickCompiledCode() 315 : GetEntryPointFromPortableCompiledCode()); 316 } 317 if (!IsPortableCompiled()) { 318#ifdef __LP64__ 319 if (!IsStatic()) { 320 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); 321 } else { 322 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty); 323 } 324#else 325 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty); 326#endif 327 } else { 328 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]); 329 } 330 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) { 331 // Unusual case where we were running generated code and an 332 // exception was thrown to force the activations to be removed from the 333 // stack. Continue execution in the interpreter. 334 self->ClearException(); 335 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result); 336 self->SetTopOfStack(nullptr, 0); 337 self->SetTopOfShadowStack(shadow_frame); 338 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result); 339 } 340 if (kLogInvocationStartAndReturn) { 341 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(), 342 have_quick_code ? "quick" : "portable", 343 have_quick_code ? GetEntryPointFromQuickCompiledCode() 344 : GetEntryPointFromPortableCompiledCode()); 345 } 346 } else { 347 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null"; 348 if (result != NULL) { 349 result->SetJ(0); 350 } 351 } 352 } 353 354 // Pop transition. 355 self->PopManagedStackFragment(fragment); 356} 357 358void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) { 359 DCHECK(Thread::Current() == self); 360 CHECK(IsNative()) << PrettyMethod(this); 361 CHECK(!IsFastNative()) << PrettyMethod(this); 362 CHECK(native_method != NULL) << PrettyMethod(this); 363 if (is_fast) { 364 SetAccessFlags(GetAccessFlags() | kAccFastNative); 365 } 366 SetNativeMethod(native_method); 367} 368 369void ArtMethod::UnregisterNative(Thread* self) { 370 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this); 371 // restore stub to lookup native pointer via dlsym 372 RegisterNative(self, GetJniDlsymLookupStub(), false); 373} 374 375} // namespace mirror 376} // namespace art 377