blob: 787c76715aa4ca678ec3c3d11de19524fb2a18e6 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromea46f952013-07-30 01:26:50 -070017#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Ian Rogers62f05122014-03-21 11:21:29 -070020#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/stringpiece.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "class-inl.h"
24#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070025#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "interpreter/interpreter.h"
28#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070029#include "mapping_table.h"
Ian Rogers1ff3c982014-08-12 02:30:58 -070030#include "method_helper-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070032#include "object_array.h"
33#include "object-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070034#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "string.h"
Ian Rogers62f05122014-03-21 11:21:29 -070036#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037
38namespace art {
39namespace mirror {
40
Brian Carlstromea46f952013-07-30 01:26:50 -070041extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Ian Rogers0177e532014-02-11 16:30:46 -080042extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
43 const char*);
Stuart Monteithb95a5342014-03-12 13:32:32 +000044#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -080045extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
46 const char*);
47#endif
Jeff Hao5d917302013-02-27 17:57:33 -080048
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070050GcRoot<Class> ArtMethod::java_lang_reflect_ArtMethod_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070052ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
53 jobject jlr_method) {
Ian Rogers62f05122014-03-21 11:21:29 -070054 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
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080062void ArtMethod::VisitRoots(RootCallback* callback, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070063 if (!java_lang_reflect_ArtMethod_.IsNull()) {
64 java_lang_reflect_ArtMethod_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080065 }
66}
67
Ian Rogersef7d42f2014-01-06 12:55:46 -080068InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 // TODO: kSuper?
70 if (GetDeclaringClass()->IsInterface()) {
71 return kInterface;
72 } else if (IsStatic()) {
73 return kStatic;
74 } else if (IsDirect()) {
75 return kDirect;
76 } else {
77 return kVirtual;
78 }
79}
80
Brian Carlstromea46f952013-07-30 01:26:50 -070081void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070082 CHECK(java_lang_reflect_ArtMethod_.IsNull());
Brian Carlstromea46f952013-07-30 01:26:50 -070083 CHECK(java_lang_reflect_ArtMethod != NULL);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070084 java_lang_reflect_ArtMethod_ = GcRoot<Class>(java_lang_reflect_ArtMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Brian Carlstromea46f952013-07-30 01:26:50 -070087void ArtMethod::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070088 CHECK(!java_lang_reflect_ArtMethod_.IsNull());
89 java_lang_reflect_ArtMethod_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090}
91
Brian Carlstromea46f952013-07-30 01:26:50 -070092void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010093 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 new_dex_cache_strings);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095}
96
Brian Carlstromea46f952013-07-30 01:26:50 -070097void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070099 new_dex_cache_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100}
101
Brian Carlstromea46f952013-07-30 01:26:50 -0700102void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700104 new_dex_cache_classes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105}
106
Brian Carlstromea46f952013-07-30 01:26:50 -0700107size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -0700108 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700110 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 char ch = shorty[i];
112 if (ch == 'D' || ch == 'J') {
113 num_registers += 2;
114 } else {
115 num_registers += 1;
116 }
117 }
118 return num_registers;
119}
120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121bool ArtMethod::IsProxyMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return GetDeclaringClass()->IsProxyClass();
123}
124
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 if (IsStatic()) {
127 return NULL;
128 }
129 Class* declaring_class = GetDeclaringClass();
130 Class* super_class = declaring_class->GetSuperClass();
131 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 ArtMethod* result = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 // Did this method override a super class method? If so load the result from the super class'
134 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700135 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
136 result = super_class->GetVTableEntry(method_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 } 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 {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700144 StackHandleScope<2> hs(Thread::Current());
145 MethodHelper mh(hs.NewHandle(this));
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700146 MutableMethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 IfTable* iftable = GetDeclaringClass()->GetIfTable();
148 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
149 Class* interface = iftable->GetInterface(i);
150 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700151 interface_mh.ChangeMethod(interface->GetVirtualMethod(j));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 if (mh.HasSameNameAndSignature(&interface_mh)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700153 result = interface_mh.GetMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 break;
155 }
156 }
157 }
158 }
159 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700160 if (kIsDebugBuild) {
161 StackHandleScope<2> hs(Thread::Current());
162 MethodHelper result_mh(hs.NewHandle(result));
163 MethodHelper this_mh(hs.NewHandle(this));
164 DCHECK(result == nullptr || this_mh.HasSameNameAndSignature(&result_mh));
165 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 return result;
167}
168
Dave Allisonb373e092014-02-20 16:06:36 -0800169uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 if (IsPortableCompiled()) {
171 // Portable doesn't use the machine pc, we just use dex pc instead.
172 return static_cast<uint32_t>(pc);
173 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100174 const void* entry_point = GetQuickOatEntryPoint();
175 MappingTable table(
176 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700177 if (table.TotalSize() == 0) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100178 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
179 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
181 return DexFile::kDexNoIndex; // Special no mapping case
182 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100183 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
Ian Rogers1809a722013-08-09 22:05:32 -0700184 // 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800196 }
197 }
Dave Allisonb373e092014-02-20 16:06:36 -0800198 if (abort_on_failure) {
199 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100200 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Dave Allisonb373e092014-02-20 16:06:36 -0800201 << ") in " << PrettyMethod(this);
202 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204}
205
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100207 const void* entry_point = GetQuickOatEntryPoint();
208 MappingTable table(
209 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700210 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 DCHECK_EQ(dex_pc, 0U);
212 return 0; // Special no mapping/pc == 0 case
213 }
Ian Rogers1809a722013-08-09 22:05:32 -0700214 // Assume the caller wants a dex-to-pc mapping so check here first.
215 typedef MappingTable::DexToPcIterator It;
216 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
217 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100218 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 }
220 }
Ian Rogers1809a722013-08-09 22:05:32 -0700221 // Now check pc-to-dex mappings.
222 typedef MappingTable::PcToDexIterator It2;
223 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
224 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100225 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700226 }
227 }
228 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 << " in " << PrettyMethod(this);
230 return 0;
231}
232
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700233uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type,
234 uint32_t dex_pc, bool* has_no_move_exception) {
235 MethodHelper mh(h_this);
236 const DexFile::CodeItem* code_item = h_this->GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700237 // Set aside the exception while we resolve its type.
238 Thread* self = Thread::Current();
239 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700240 StackHandleScope<1> hs(self);
241 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Sebastien Hertz9f102032014-05-23 08:59:42 +0200242 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Jeff Haoaa961912014-04-22 13:54:32 -0700243 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700244 // Default to handler not found.
245 uint32_t found_dex_pc = DexFile::kDexNoIndex;
246 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
248 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
249 // Catch all case
250 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700251 found_dex_pc = it.GetHandlerAddress();
252 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 }
254 // Does this catch exception type apply?
Jeff Haoaa961912014-04-22 13:54:32 -0700255 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700256 if (UNLIKELY(iter_exception_type == nullptr)) {
257 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
258 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700259 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700260 self->ClearException();
261 // Delete any long jump context as this routine is called during a stack walk which will
262 // release its in use context at the end.
263 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700267 found_dex_pc = it.GetHandlerAddress();
268 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 }
270 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700271 if (found_dex_pc != DexFile::kDexNoIndex) {
272 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700273 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700274 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
275 }
Jeff Haoaa961912014-04-22 13:54:32 -0700276 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700277 if (exception.Get() != nullptr) {
278 self->SetException(throw_location, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200279 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Jeff Haoaa961912014-04-22 13:54:32 -0700280 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700281 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282}
283
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700284bool ArtMethod::IsEntrypointInterpreter() {
285 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
286 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
287 const void* oat_portable_code = class_linker->GetOatMethodPortableCodeFor(this);
288 if (!IsPortableCompiled()) { // Quick.
289 return oat_quick_code == nullptr ||
290 oat_quick_code != GetEntryPointFromQuickCompiledCode();
291 } else { // Portable.
292 return oat_portable_code == nullptr ||
293 oat_portable_code != GetEntryPointFromPortableCompiledCode();
294 }
295}
296
Brian Carlstromea46f952013-07-30 01:26:50 -0700297void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800298 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700299 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
300 ThrowStackOverflowError(self);
301 return;
302 }
303
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 if (kIsDebugBuild) {
305 self->AssertThreadSuspensionIsAllowable();
306 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700307 CHECK_STREQ(GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 }
309
310 // Push a transition back into managed code onto the linked list in thread.
311 ManagedStack fragment;
312 self->PushManagedStackFragment(&fragment);
313
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700315 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700316 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700317 if (IsStatic()) {
318 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
319 } else {
320 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
321 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 }
323 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800325 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
326 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
327 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700328 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800329 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
330 have_quick_code ? "quick" : "portable",
331 have_quick_code ? GetEntryPointFromQuickCompiledCode()
332 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700333 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700334
335 // Ensure that we won't be accidentally calling quick/portable compiled code when -Xint.
336 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly()) {
337 CHECK(IsEntrypointInterpreter())
338 << "Don't call compiled code when -Xint " << PrettyMethod(this);
339 }
340
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 if (!IsPortableCompiled()) {
Stuart Monteithb95a5342014-03-12 13:32:32 +0000342#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -0800343 if (!IsStatic()) {
344 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
345 } else {
346 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
347 }
348#else
Ian Rogers0177e532014-02-11 16:30:46 -0800349 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogers936b37f2014-02-14 00:52:24 -0800350#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800351 } else {
Ian Rogers0177e532014-02-11 16:30:46 -0800352 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800353 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200354 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
355 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700356 // exception was thrown to force the activations to be removed from the
357 // stack. Continue execution in the interpreter.
358 self->ClearException();
359 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200360 self->SetTopOfStack(nullptr, 0);
Jeff Hao790ad902013-05-22 15:02:08 -0700361 self->SetTopOfShadowStack(shadow_frame);
362 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
363 }
364 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800365 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
366 have_quick_code ? "quick" : "portable",
367 have_quick_code ? GetEntryPointFromQuickCompiledCode()
368 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800369 }
370 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800371 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800372 if (result != NULL) {
373 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 }
375 }
376 }
377
378 // Pop transition.
379 self->PopManagedStackFragment(fragment);
380}
381
Ian Rogers1eb512d2013-10-18 15:42:20 -0700382void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 DCHECK(Thread::Current() == self);
384 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700385 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700387 if (is_fast) {
388 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 }
Ian Rogers987560f2014-04-22 11:42:59 -0700390 SetNativeMethod(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391}
392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393void ArtMethod::UnregisterNative(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700394 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 // restore stub to lookup native pointer via dlsym
Ian Rogers1eb512d2013-10-18 15:42:20 -0700396 RegisterNative(self, GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397}
398
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399} // namespace mirror
400} // namespace art