blob: aa10a17615226ea693174fb09693c951741779b5 [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*);
Stuart Monteithb95a5342014-03-12 13:32:32 +000046#ifdef __LP64__
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 -070094void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010095 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070096 new_dex_cache_strings);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097}
98
Brian Carlstromea46f952013-07-30 01:26:50 -070099void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100100 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700101 new_dex_cache_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102}
103
Brian Carlstromea46f952013-07-30 01:26:50 -0700104void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100105 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700106 new_dex_cache_classes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107}
108
Brian Carlstromea46f952013-07-30 01:26:50 -0700109size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -0700110 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700112 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 char ch = shorty[i];
114 if (ch == 'D' || ch == 'J') {
115 num_registers += 2;
116 } else {
117 num_registers += 1;
118 }
119 }
120 return num_registers;
121}
122
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123bool ArtMethod::IsProxyMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 return GetDeclaringClass()->IsProxyClass();
125}
126
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 if (IsStatic()) {
129 return NULL;
130 }
131 Class* declaring_class = GetDeclaringClass();
132 Class* super_class = declaring_class->GetSuperClass();
133 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700134 ArtMethod* result = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 // Did this method override a super class method? If so load the result from the super class'
136 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700137 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
138 result = super_class->GetVTableEntry(method_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 } else {
140 // Method didn't override superclass method so search interfaces
141 if (IsProxyMethod()) {
142 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
143 CHECK_EQ(result,
144 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
145 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700146 StackHandleScope<2> hs(Thread::Current());
147 MethodHelper mh(hs.NewHandle(this));
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700148 MutableMethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 IfTable* iftable = GetDeclaringClass()->GetIfTable();
150 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
151 Class* interface = iftable->GetInterface(i);
152 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700153 interface_mh.ChangeMethod(interface->GetVirtualMethod(j));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 if (mh.HasSameNameAndSignature(&interface_mh)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700155 result = interface_mh.GetMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 break;
157 }
158 }
159 }
160 }
161 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700162 if (kIsDebugBuild) {
163 StackHandleScope<2> hs(Thread::Current());
164 MethodHelper result_mh(hs.NewHandle(result));
165 MethodHelper this_mh(hs.NewHandle(this));
166 DCHECK(result == nullptr || this_mh.HasSameNameAndSignature(&result_mh));
167 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 return result;
169}
170
Dave Allisonb373e092014-02-20 16:06:36 -0800171uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800172 if (IsPortableCompiled()) {
173 // Portable doesn't use the machine pc, we just use dex pc instead.
174 return static_cast<uint32_t>(pc);
175 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100176 const void* entry_point = GetQuickOatEntryPoint();
177 MappingTable table(
178 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700179 if (table.TotalSize() == 0) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100180 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
181 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
183 return DexFile::kDexNoIndex; // Special no mapping case
184 }
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100185 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
Ian Rogers1809a722013-08-09 22:05:32 -0700186 // Assume the caller wants a pc-to-dex mapping so check here first.
187 typedef MappingTable::PcToDexIterator It;
188 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
189 if (cur.NativePcOffset() == sought_offset) {
190 return cur.DexPc();
191 }
192 }
193 // Now check dex-to-pc mappings.
194 typedef MappingTable::DexToPcIterator It2;
195 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
196 if (cur.NativePcOffset() == sought_offset) {
197 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 }
199 }
Dave Allisonb373e092014-02-20 16:06:36 -0800200 if (abort_on_failure) {
201 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100202 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Dave Allisonb373e092014-02-20 16:06:36 -0800203 << ") in " << PrettyMethod(this);
204 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206}
207
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700208uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100209 const void* entry_point = GetQuickOatEntryPoint();
210 MappingTable table(
211 entry_point != nullptr ? GetMappingTable(EntryPointToCodePointer(entry_point)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700212 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 DCHECK_EQ(dex_pc, 0U);
214 return 0; // Special no mapping/pc == 0 case
215 }
Ian Rogers1809a722013-08-09 22:05:32 -0700216 // Assume the caller wants a dex-to-pc mapping so check here first.
217 typedef MappingTable::DexToPcIterator It;
218 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
219 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100220 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 }
222 }
Ian Rogers1809a722013-08-09 22:05:32 -0700223 // Now check pc-to-dex mappings.
224 typedef MappingTable::PcToDexIterator It2;
225 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
226 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100227 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700228 }
229 }
230 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 << " in " << PrettyMethod(this);
232 return 0;
233}
234
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700235uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type,
236 uint32_t dex_pc, bool* has_no_move_exception) {
237 MethodHelper mh(h_this);
238 const DexFile::CodeItem* code_item = h_this->GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700239 // Set aside the exception while we resolve its type.
240 Thread* self = Thread::Current();
241 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700242 StackHandleScope<1> hs(self);
243 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Sebastien Hertz9f102032014-05-23 08:59:42 +0200244 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Jeff Haoaa961912014-04-22 13:54:32 -0700245 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700246 // Default to handler not found.
247 uint32_t found_dex_pc = DexFile::kDexNoIndex;
248 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
250 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
251 // Catch all case
252 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700253 found_dex_pc = it.GetHandlerAddress();
254 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 }
256 // Does this catch exception type apply?
Jeff Haoaa961912014-04-22 13:54:32 -0700257 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
Ian Rogers822266b2014-05-29 16:55:06 -0700258 if (UNLIKELY(iter_exception_type == nullptr)) {
259 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
260 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700261 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700262 self->ClearException();
263 // Delete any long jump context as this routine is called during a stack walk which will
264 // release its in use context at the end.
265 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700267 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700268 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700269 found_dex_pc = it.GetHandlerAddress();
270 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 }
272 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700273 if (found_dex_pc != DexFile::kDexNoIndex) {
274 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700275 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700276 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
277 }
Jeff Haoaa961912014-04-22 13:54:32 -0700278 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700279 if (exception.Get() != nullptr) {
280 self->SetException(throw_location, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200281 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Jeff Haoaa961912014-04-22 13:54:32 -0700282 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700283 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284}
285
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700286void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
287 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
288 return;
289 }
290 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
291 return;
292 }
293 const void* code = GetEntryPointFromQuickCompiledCode();
294 if (code == GetQuickInstrumentationEntryPoint()) {
295 return;
296 }
297 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
298 if (class_linker->IsQuickToInterpreterBridge(code) ||
299 class_linker->IsQuickResolutionStub(code)) {
300 return;
301 }
302 /*
303 * During a stack walk, a return PC may point past-the-end of the code
304 * in the case that the last instruction is a call that isn't expected to
305 * return. Thus, we check <= code + GetCodeSize().
306 *
307 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
308 */
309 CHECK(PcIsWithinQuickCode(pc))
310 << PrettyMethod(this)
311 << " pc=" << std::hex << pc
312 << " code=" << code
313 << " size=" << GetCodeSize();
314}
315
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700316bool ArtMethod::IsEntrypointInterpreter() {
317 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
318 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
319 const void* oat_portable_code = class_linker->GetOatMethodPortableCodeFor(this);
320 if (!IsPortableCompiled()) { // Quick.
321 return oat_quick_code == nullptr ||
322 oat_quick_code != GetEntryPointFromQuickCompiledCode();
323 } else { // Portable.
324 return oat_portable_code == nullptr ||
325 oat_portable_code != GetEntryPointFromPortableCompiledCode();
326 }
327}
328
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700329const void* ArtMethod::GetQuickOatEntryPoint() {
330 if (IsPortableCompiled() || IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
331 return nullptr;
332 }
333 Runtime* runtime = Runtime::Current();
334 ClassLinker* class_linker = runtime->GetClassLinker();
335 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this);
336 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
337 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
338 // for non-native methods.
339 if (class_linker->IsQuickToInterpreterBridge(code) ||
340 class_linker->IsQuickGenericJniStub(code)) {
341 return nullptr;
342 }
343 return code;
344}
345
346#ifndef NDEBUG
347uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
348 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
349 CHECK_EQ(quick_entry_point, Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this));
350 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
351}
352#endif
353
Brian Carlstromea46f952013-07-30 01:26:50 -0700354void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800355 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700356 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
357 ThrowStackOverflowError(self);
358 return;
359 }
360
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 if (kIsDebugBuild) {
362 self->AssertThreadSuspensionIsAllowable();
363 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700364 CHECK_STREQ(GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 }
366
367 // Push a transition back into managed code onto the linked list in thread.
368 ManagedStack fragment;
369 self->PushManagedStackFragment(&fragment);
370
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700372 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700373 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700374 if (IsStatic()) {
375 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
376 } else {
377 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
378 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 }
380 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800382 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
383 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
384 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700385 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800386 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
387 have_quick_code ? "quick" : "portable",
388 have_quick_code ? GetEntryPointFromQuickCompiledCode()
389 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700390 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700391
392 // Ensure that we won't be accidentally calling quick/portable compiled code when -Xint.
393 if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly()) {
394 CHECK(IsEntrypointInterpreter())
395 << "Don't call compiled code when -Xint " << PrettyMethod(this);
396 }
397
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 if (!IsPortableCompiled()) {
Stuart Monteithb95a5342014-03-12 13:32:32 +0000399#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 if (!IsStatic()) {
401 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
402 } else {
403 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
404 }
405#else
Ian Rogers0177e532014-02-11 16:30:46 -0800406 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogers936b37f2014-02-14 00:52:24 -0800407#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408 } else {
Ian Rogers0177e532014-02-11 16:30:46 -0800409 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800410 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200411 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
412 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700413 // exception was thrown to force the activations to be removed from the
414 // stack. Continue execution in the interpreter.
415 self->ClearException();
416 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200417 self->SetTopOfStack(nullptr, 0);
Jeff Hao790ad902013-05-22 15:02:08 -0700418 self->SetTopOfShadowStack(shadow_frame);
419 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
420 }
421 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
423 have_quick_code ? "quick" : "portable",
424 have_quick_code ? GetEntryPointFromQuickCompiledCode()
425 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800426 }
427 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800428 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800429 if (result != NULL) {
430 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 }
432 }
433 }
434
435 // Pop transition.
436 self->PopManagedStackFragment(fragment);
437}
438
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700439QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
440 if (UNLIKELY(IsPortableCompiled())) {
441 // Portable compiled dex bytecode or jni stub.
442 return QuickMethodFrameInfo(kStackAlignment, 0u, 0u);
443 }
444 Runtime* runtime = Runtime::Current();
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200445
446 if (UNLIKELY(IsAbstract())) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700447 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
448 }
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200449
450 // For Proxy method we add special handling for the direct method case (there is only one
451 // direct method - constructor). Direct method is cloned from original
452 // java.lang.reflect.Proxy class together with code and as a result it is executed as usual
453 // quick compiled method without any stubs. So the frame info should be returned as it is a
454 // quick method not a stub. However, if instrumentation stubs are installed, the
455 // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an
456 // oat code pointer, thus we have to add a special case here.
457 if (UNLIKELY(IsProxyMethod())) {
458 if (IsDirect()) {
459 CHECK(IsConstructor());
460 return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
461 } else {
462 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
463 }
464 }
465
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700466 if (UNLIKELY(IsRuntimeMethod())) {
467 return runtime->GetRuntimeMethodFrameInfo(this);
468 }
469
470 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this);
471 ClassLinker* class_linker = runtime->GetClassLinker();
472 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
473 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
474 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
475 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
476
477 if (class_linker->IsQuickGenericJniStub(entry_point)) {
478 // Generic JNI frame.
479 DCHECK(IsNative());
480 StackHandleScope<1> hs(Thread::Current());
481 uint32_t handle_refs =
482 MethodHelper(hs.NewHandle(this)).GetNumberOfReferenceArgsWithoutReceiver() + 1;
483 size_t scope_size = HandleScope::SizeOf(handle_refs);
484 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
485
486 // Callee saves + handle scope + method ref + alignment
487 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() + scope_size
488 - sizeof(void*) // callee-save frame stores a whole method pointer
489 + sizeof(StackReference<mirror::ArtMethod>),
490 kStackAlignment);
491
492 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
493 }
494
495 const void* code_pointer = EntryPointToCodePointer(entry_point);
496 return GetQuickFrameInfo(code_pointer);
497}
498
499void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700501 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700503 if (is_fast) {
504 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 }
Ian Rogers987560f2014-04-22 11:42:59 -0700506 SetNativeMethod(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507}
508
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700509void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700510 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700512 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513}
514
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515} // namespace mirror
516} // namespace art