blob: ddc16fb5e25cbf7e1c4735de9aeaad15ca97a21d [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 Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/entrypoint_utils.h"
27#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "interpreter/interpreter.h"
30#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070031#include "mapping_table.h"
Ian Rogers1ff3c982014-08-12 02:30:58 -070032#include "method_helper-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070034#include "object_array.h"
35#include "object-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070036#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "string.h"
Ian Rogers62f05122014-03-21 11:21:29 -070038#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039
40namespace art {
41namespace mirror {
42
Brian Carlstromea46f952013-07-30 01:26:50 -070043extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Ian Rogers0177e532014-02-11 16:30:46 -080044extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
45 const char*);
Zheng Xu5667fdb2014-10-23 18:29:55 +080046#if defined(__LP64__) || defined(__arm__)
Ian Rogers936b37f2014-02-14 00:52:24 -080047extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
48 const char*);
49#endif
Jeff Hao5d917302013-02-27 17:57:33 -080050
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070052GcRoot<Class> ArtMethod::java_lang_reflect_ArtMethod_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070054ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
55 jobject jlr_method) {
Ian Rogers62f05122014-03-21 11:21:29 -070056 mirror::ArtField* f =
57 soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
58 mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod();
59 DCHECK(method != nullptr);
60 return method;
61}
62
63
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080064void ArtMethod::VisitRoots(RootCallback* callback, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070065 if (!java_lang_reflect_ArtMethod_.IsNull()) {
66 java_lang_reflect_ArtMethod_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080067 }
68}
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 // TODO: kSuper?
72 if (GetDeclaringClass()->IsInterface()) {
73 return kInterface;
74 } else if (IsStatic()) {
75 return kStatic;
76 } else if (IsDirect()) {
77 return kDirect;
78 } else {
79 return kVirtual;
80 }
81}
82
Brian Carlstromea46f952013-07-30 01:26:50 -070083void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070084 CHECK(java_lang_reflect_ArtMethod_.IsNull());
Brian Carlstromea46f952013-07-30 01:26:50 -070085 CHECK(java_lang_reflect_ArtMethod != NULL);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070086 java_lang_reflect_ArtMethod_ = GcRoot<Class>(java_lang_reflect_ArtMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087}
88
Brian Carlstromea46f952013-07-30 01:26:50 -070089void ArtMethod::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070090 CHECK(!java_lang_reflect_ArtMethod_.IsNull());
91 java_lang_reflect_ArtMethod_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092}
93
Brian Carlstromea46f952013-07-30 01:26:50 -070094size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070095 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070097 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 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 Rogersef7d42f2014-01-06 12:55:46 -0800108ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 if (IsStatic()) {
110 return NULL;
111 }
112 Class* declaring_class = GetDeclaringClass();
113 Class* super_class = declaring_class->GetSuperClass();
114 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700115 ArtMethod* result = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 // Did this method override a super class method? If so load the result from the super class'
117 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700118 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
119 result = super_class->GetVTableEntry(method_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 } 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 {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700127 StackHandleScope<2> hs(Thread::Current());
128 MethodHelper mh(hs.NewHandle(this));
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700129 MutableMethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 IfTable* iftable = GetDeclaringClass()->GetIfTable();
131 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
132 Class* interface = iftable->GetInterface(i);
133 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700134 interface_mh.ChangeMethod(interface->GetVirtualMethod(j));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 if (mh.HasSameNameAndSignature(&interface_mh)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700136 result = interface_mh.GetMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 break;
138 }
139 }
140 }
141 }
142 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700143 if (kIsDebugBuild) {
144 StackHandleScope<2> hs(Thread::Current());
145 MethodHelper result_mh(hs.NewHandle(result));
146 MethodHelper this_mh(hs.NewHandle(this));
147 DCHECK(result == nullptr || this_mh.HasSameNameAndSignature(&result_mh));
148 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 return result;
150}
151
Dave Allisonb373e092014-02-20 16:06:36 -0800152uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 if (IsPortableCompiled()) {
154 // Portable doesn't use the machine pc, we just use dex pc instead.
155 return static_cast<uint32_t>(pc);
156 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100157 const void* entry_point = GetQuickOatEntryPoint();
158 MappingTable table(
159 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700160 if (table.TotalSize() == 0) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100161 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
162 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
164 return DexFile::kDexNoIndex; // Special no mapping case
165 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100166 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
Ian Rogers1809a722013-08-09 22:05:32 -0700167 // 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800179 }
180 }
Dave Allisonb373e092014-02-20 16:06:36 -0800181 if (abort_on_failure) {
182 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100183 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Dave Allisonb373e092014-02-20 16:06:36 -0800184 << ") in " << PrettyMethod(this);
185 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187}
188
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700189uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100190 const void* entry_point = GetQuickOatEntryPoint();
191 MappingTable table(
192 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700193 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 DCHECK_EQ(dex_pc, 0U);
195 return 0; // Special no mapping/pc == 0 case
196 }
Ian Rogers1809a722013-08-09 22:05:32 -0700197 // Assume the caller wants a dex-to-pc mapping so check here first.
198 typedef MappingTable::DexToPcIterator It;
199 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
200 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100201 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 }
203 }
Ian Rogers1809a722013-08-09 22:05:32 -0700204 // Now check pc-to-dex mappings.
205 typedef MappingTable::PcToDexIterator It2;
206 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
207 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100208 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700209 }
210 }
211 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 << " in " << PrettyMethod(this);
213 return 0;
214}
215
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700216uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type,
217 uint32_t dex_pc, bool* has_no_move_exception) {
218 MethodHelper mh(h_this);
219 const DexFile::CodeItem* code_item = h_this->GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700220 // Set aside the exception while we resolve its type.
221 Thread* self = Thread::Current();
222 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700223 StackHandleScope<1> hs(self);
224 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Sebastien Hertz9f102032014-05-23 08:59:42 +0200225 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Jeff Haoaa961912014-04-22 13:54:32 -0700226 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700227 // Default to handler not found.
228 uint32_t found_dex_pc = DexFile::kDexNoIndex;
229 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
231 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
232 // Catch all case
233 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700234 found_dex_pc = it.GetHandlerAddress();
235 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 }
237 // Does this catch exception type apply?
Jeff Haoaa961912014-04-22 13:54:32 -0700238 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700239 if (UNLIKELY(iter_exception_type == nullptr)) {
240 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
241 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700242 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700243 self->ClearException();
244 // Delete any long jump context as this routine is called during a stack walk which will
245 // release its in use context at the end.
246 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700248 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700250 found_dex_pc = it.GetHandlerAddress();
251 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
253 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700254 if (found_dex_pc != DexFile::kDexNoIndex) {
255 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700256 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700257 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
258 }
Jeff Haoaa961912014-04-22 13:54:32 -0700259 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700260 if (exception.Get() != nullptr) {
261 self->SetException(throw_location, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200262 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Jeff Haoaa961912014-04-22 13:54:32 -0700263 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700264 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265}
266
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700267void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
268 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
269 return;
270 }
271 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
272 return;
273 }
274 const void* code = GetEntryPointFromQuickCompiledCode();
275 if (code == GetQuickInstrumentationEntryPoint()) {
276 return;
277 }
278 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
279 if (class_linker->IsQuickToInterpreterBridge(code) ||
280 class_linker->IsQuickResolutionStub(code)) {
281 return;
282 }
283 /*
284 * During a stack walk, a return PC may point past-the-end of the code
285 * in the case that the last instruction is a call that isn't expected to
286 * return. Thus, we check <= code + GetCodeSize().
287 *
288 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
289 */
290 CHECK(PcIsWithinQuickCode(pc))
291 << PrettyMethod(this)
292 << " pc=" << std::hex << pc
293 << " code=" << code
294 << " size=" << GetCodeSize();
295}
296
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700297bool ArtMethod::IsEntrypointInterpreter() {
298 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700299 if (!IsPortableCompiled()) { // Quick.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700300 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700301 return oat_quick_code == nullptr ||
302 oat_quick_code != GetEntryPointFromQuickCompiledCode();
303 } else { // Portable.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700304 const void* oat_portable_code = class_linker->GetOatMethodPortableCodeFor(this);
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700305 return oat_portable_code == nullptr ||
306 oat_portable_code != GetEntryPointFromPortableCompiledCode();
307 }
308}
309
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700310const void* ArtMethod::GetQuickOatEntryPoint() {
311 if (IsPortableCompiled() || IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
312 return nullptr;
313 }
314 Runtime* runtime = Runtime::Current();
315 ClassLinker* class_linker = runtime->GetClassLinker();
316 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this);
317 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
318 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
319 // for non-native methods.
320 if (class_linker->IsQuickToInterpreterBridge(code) ||
321 class_linker->IsQuickGenericJniStub(code)) {
322 return nullptr;
323 }
324 return code;
325}
326
327#ifndef NDEBUG
328uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
329 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
330 CHECK_EQ(quick_entry_point, Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this));
331 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
332}
333#endif
334
Brian Carlstromea46f952013-07-30 01:26:50 -0700335void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800336 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700337 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
338 ThrowStackOverflowError(self);
339 return;
340 }
341
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 if (kIsDebugBuild) {
343 self->AssertThreadSuspensionIsAllowable();
344 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345 CHECK_STREQ(GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346 }
347
348 // Push a transition back into managed code onto the linked list in thread.
349 ManagedStack fragment;
350 self->PushManagedStackFragment(&fragment);
351
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700353 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700354 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700355 if (IsStatic()) {
356 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
357 } else {
358 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
359 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 }
361 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
364 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
365 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700366 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
368 have_quick_code ? "quick" : "portable",
369 have_quick_code ? GetEntryPointFromQuickCompiledCode()
370 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700371 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700372
373 // Ensure that we won't be accidentally calling quick/portable compiled code when -Xint.
374 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly()) {
375 CHECK(IsEntrypointInterpreter())
376 << "Don't call compiled code when -Xint " << PrettyMethod(this);
377 }
378
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379 if (!IsPortableCompiled()) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800380#if defined(__LP64__) || defined(__arm__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 if (!IsStatic()) {
382 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
383 } else {
384 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
385 }
386#else
Ian Rogers0177e532014-02-11 16:30:46 -0800387 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogers936b37f2014-02-14 00:52:24 -0800388#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800389 } else {
Ian Rogers0177e532014-02-11 16:30:46 -0800390 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200392 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
393 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700394 // exception was thrown to force the activations to be removed from the
395 // stack. Continue execution in the interpreter.
396 self->ClearException();
397 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700398 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700399 self->SetTopOfShadowStack(shadow_frame);
400 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
401 }
402 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800403 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
404 have_quick_code ? "quick" : "portable",
405 have_quick_code ? GetEntryPointFromQuickCompiledCode()
406 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800407 }
408 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800409 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800410 if (result != NULL) {
411 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 }
413 }
414 }
415
416 // Pop transition.
417 self->PopManagedStackFragment(fragment);
418}
419
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700420QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
421 if (UNLIKELY(IsPortableCompiled())) {
422 // Portable compiled dex bytecode or jni stub.
423 return QuickMethodFrameInfo(kStackAlignment, 0u, 0u);
424 }
425 Runtime* runtime = Runtime::Current();
426 // For Proxy method we exclude direct method (there is only one direct method - constructor).
427 // Direct method is cloned from original java.lang.reflect.Proxy class together with code
428 // and as a result it is executed as usual quick compiled method without any stubs.
429 // So the frame info should be returned as it is a quick method not a stub.
430 if (UNLIKELY(IsAbstract()) || UNLIKELY(IsProxyMethod() && !IsDirect())) {
431 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
432 }
433 if (UNLIKELY(IsRuntimeMethod())) {
434 return runtime->GetRuntimeMethodFrameInfo(this);
435 }
436
437 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this);
438 ClassLinker* class_linker = runtime->GetClassLinker();
439 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
440 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
441 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
442 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
443
444 if (class_linker->IsQuickGenericJniStub(entry_point)) {
445 // Generic JNI frame.
446 DCHECK(IsNative());
447 StackHandleScope<1> hs(Thread::Current());
448 uint32_t handle_refs =
449 MethodHelper(hs.NewHandle(this)).GetNumberOfReferenceArgsWithoutReceiver() + 1;
450 size_t scope_size = HandleScope::SizeOf(handle_refs);
451 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
452
453 // Callee saves + handle scope + method ref + alignment
454 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() + scope_size
455 - sizeof(void*) // callee-save frame stores a whole method pointer
456 + sizeof(StackReference<mirror::ArtMethod>),
457 kStackAlignment);
458
459 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
460 }
461
462 const void* code_pointer = EntryPointToCodePointer(entry_point);
463 return GetQuickFrameInfo(code_pointer);
464}
465
466void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700468 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700470 if (is_fast) {
471 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 }
Ian Rogers987560f2014-04-22 11:42:59 -0700473 SetNativeMethod(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474}
475
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700476void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700477 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700479 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480}
481
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482} // namespace mirror
483} // namespace art