blob: c78a851b0e03b0182dc1df0f073f6fadedef2796 [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 "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070024#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070025#include "entrypoints/entrypoint_utils.h"
26#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "jit/jit.h"
30#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070032#include "mapping_table.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/abstract_method.h"
34#include "mirror/class-inl.h"
35#include "mirror/object_array-inl.h"
36#include "mirror/object-inl.h"
37#include "mirror/string.h"
Ian Rogers62f05122014-03-21 11:21:29 -070038#include "scoped_thread_state_change.h"
Ian Rogers62f05122014-03-21 11:21:29 -070039#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042
Ian Rogers0177e532014-02-11 16:30:46 -080043extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
44 const char*);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000045#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Ian Rogers936b37f2014-02-14 00:52:24 -080046extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
47 const char*);
48#endif
Jeff Hao5d917302013-02-27 17:57:33 -080049
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070050ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
51 jobject jlr_method) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070052 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method);
53 DCHECK(abstract_method != nullptr);
54 return abstract_method->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -070055}
56
Ian Rogers6b14d552014-10-28 21:50:58 -070057mirror::String* ArtMethod::GetNameAsString(Thread* self) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070058 CHECK(!IsProxyMethod());
Ian Rogers6b14d552014-10-28 21:50:58 -070059 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
61 auto* dex_file = dex_cache->GetDexFile();
62 uint32_t dex_method_idx = GetDexMethodIndex();
63 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
Ian Rogers6b14d552014-10-28 21:50:58 -070064 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
65 dex_cache);
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 -070081size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070082 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070084 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 char ch = shorty[i];
86 if (ch == 'D' || ch == 'J') {
87 num_registers += 2;
88 } else {
89 num_registers += 1;
90 }
91 }
92 return num_registers;
93}
94
Ian Rogersf2247512014-12-02 16:17:08 -080095static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
97 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
98 const DexFile* dex_file = method1->GetDexFile();
99 const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex());
100 if (method1->GetDexCache() == method2->GetDexCache()) {
101 const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex());
102 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
103 }
104 const DexFile* dex_file2 = method2->GetDexFile();
105 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex());
106 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
107 return false; // Name mismatch.
108 }
109 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112ArtMethod* ArtMethod::FindOverriddenMethod(size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800114 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 mirror::Class* declaring_class = GetDeclaringClass();
117 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800119 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 // Did this method override a super class method? If so load the result from the super class'
121 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700122 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 } else {
125 // Method didn't override superclass method so search interfaces
126 if (IsProxyMethod()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 result = GetDexCacheResolvedMethods()->GetElementPtrSize<ArtMethod*>(
128 GetDexMethodIndex(), pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 CHECK_EQ(result,
130 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
131 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800133 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 ArtMethod* interface_method = interface->GetVirtualMethod(j, pointer_size);
137 if (HasSameNameAndSignature(
138 this, interface_method->GetInterfaceMethodIfProxy(sizeof(void*)))) {
Ian Rogersf2247512014-12-02 16:17:08 -0800139 result = interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 break;
141 }
142 }
143 }
144 }
145 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 DCHECK(result == nullptr || HasSameNameAndSignature(
147 GetInterfaceMethodIfProxy(sizeof(void*)), result->GetInterfaceMethodIfProxy(sizeof(void*))));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 return result;
149}
150
Ian Rogerse0a02da2014-12-02 14:10:53 -0800151uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
152 uint32_t name_and_signature_idx) {
153 const DexFile* dexfile = GetDexFile();
154 const uint32_t dex_method_idx = GetDexMethodIndex();
155 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
156 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
157 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
158 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
159 if (dexfile == &other_dexfile) {
160 return dex_method_idx;
161 }
162 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
163 const DexFile::StringId* other_descriptor =
164 other_dexfile.FindStringId(mid_declaring_class_descriptor);
165 if (other_descriptor != nullptr) {
166 const DexFile::TypeId* other_type_id =
167 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
168 if (other_type_id != nullptr) {
169 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
170 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
171 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
172 if (other_mid != nullptr) {
173 return other_dexfile.GetIndexForMethodId(*other_mid);
174 }
175 }
176 }
177 return DexFile::kDexNoIndex;
178}
179
Dave Allisonb373e092014-02-20 16:06:36 -0800180uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800181 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000182 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
183 if (IsOptimized(sizeof(void*))) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000184 CodeInfo code_info = GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100185 StackMapEncoding encoding = code_info.ExtractEncoding();
186 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100187 if (stack_map.IsValid()) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100188 return stack_map.GetDexPc(encoding);
Ian Rogers1809a722013-08-09 22:05:32 -0700189 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100190 } else {
191 MappingTable table(entry_point != nullptr ?
192 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
193 if (table.TotalSize() == 0) {
194 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
195 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
196 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
197 return DexFile::kDexNoIndex; // Special no mapping case
198 }
199 // Assume the caller wants a pc-to-dex mapping so check here first.
200 typedef MappingTable::PcToDexIterator It;
201 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
202 if (cur.NativePcOffset() == sought_offset) {
203 return cur.DexPc();
204 }
205 }
206 // Now check dex-to-pc mappings.
207 typedef MappingTable::DexToPcIterator It2;
208 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
209 if (cur.NativePcOffset() == sought_offset) {
210 return cur.DexPc();
211 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 }
213 }
Dave Allisonb373e092014-02-20 16:06:36 -0800214 if (abort_on_failure) {
215 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100216 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 << " current entry_point=" << GetQuickOatEntryPoint(sizeof(void*))
Dave Allisonb373e092014-02-20 16:06:36 -0800218 << ") in " << PrettyMethod(this);
219 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221}
222
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000223uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800224 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
225 MappingTable table(entry_point != nullptr ?
226 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700227 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 DCHECK_EQ(dex_pc, 0U);
229 return 0; // Special no mapping/pc == 0 case
230 }
Ian Rogers1809a722013-08-09 22:05:32 -0700231 // Assume the caller wants a dex-to-pc mapping so check here first.
232 typedef MappingTable::DexToPcIterator It;
233 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
234 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100235 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 }
237 }
Ian Rogers1809a722013-08-09 22:05:32 -0700238 // Now check pc-to-dex mappings.
239 typedef MappingTable::PcToDexIterator It2;
240 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
241 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100242 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700243 }
244 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000245 if (abort_on_failure) {
246 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
247 << " in " << PrettyMethod(this);
248 }
249 return UINTPTR_MAX;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250}
251
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700253 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700255 // Set aside the exception while we resolve its type.
256 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700257 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000258 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700259 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700260 // Default to handler not found.
261 uint32_t found_dex_pc = DexFile::kDexNoIndex;
262 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
264 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
265 // Catch all case
266 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700267 found_dex_pc = it.GetHandlerAddress();
268 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 }
270 // Does this catch exception type apply?
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx, true);
Ian Rogers822266b2014-05-29 16:55:06 -0700272 if (UNLIKELY(iter_exception_type == nullptr)) {
273 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
274 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700275 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700276 self->ClearException();
277 // Delete any long jump context as this routine is called during a stack walk which will
278 // release its in use context at the end.
279 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700283 found_dex_pc = it.GetHandlerAddress();
284 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 }
286 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700287 if (found_dex_pc != DexFile::kDexNoIndex) {
288 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700289 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700290 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
291 }
Jeff Haoaa961912014-04-22 13:54:32 -0700292 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700293 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000294 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700295 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700296 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297}
298
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700299void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
300 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
301 return;
302 }
303 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
304 return;
305 }
306 const void* code = GetEntryPointFromQuickCompiledCode();
307 if (code == GetQuickInstrumentationEntryPoint()) {
308 return;
309 }
310 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
311 if (class_linker->IsQuickToInterpreterBridge(code) ||
312 class_linker->IsQuickResolutionStub(code)) {
313 return;
314 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800315 // If we are the JIT then we may have just compiled the method after the
316 // IsQuickToInterpreterBridge check.
317 jit::Jit* const jit = Runtime::Current()->GetJit();
318 if (jit != nullptr &&
319 jit->GetCodeCache()->ContainsCodePtr(reinterpret_cast<const void*>(code))) {
320 return;
321 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700322 /*
323 * During a stack walk, a return PC may point past-the-end of the code
324 * in the case that the last instruction is a call that isn't expected to
325 * return. Thus, we check <= code + GetCodeSize().
326 *
327 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
328 */
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800329 CHECK(PcIsWithinQuickCode(reinterpret_cast<uintptr_t>(code), pc))
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700330 << PrettyMethod(this)
331 << " pc=" << std::hex << pc
332 << " code=" << code
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800333 << " size=" << GetCodeSize(
334 EntryPointToCodePointer(reinterpret_cast<const void*>(code)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700335}
336
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700337bool ArtMethod::IsEntrypointInterpreter() {
338 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800339 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
340 return oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700341}
342
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800343const void* ArtMethod::GetQuickOatEntryPoint(size_t pointer_size) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800344 if (IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700345 return nullptr;
346 }
347 Runtime* runtime = Runtime::Current();
348 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800349 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700350 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700351 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
352 // for non-native methods.
353 if (class_linker->IsQuickToInterpreterBridge(code) ||
354 class_linker->IsQuickGenericJniStub(code)) {
355 return nullptr;
356 }
357 return code;
358}
359
360#ifndef NDEBUG
361uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
362 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 CHECK_EQ(quick_entry_point,
364 Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700365 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
366}
367#endif
368
Brian Carlstromea46f952013-07-30 01:26:50 -0700369void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800370 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700371 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
372 ThrowStackOverflowError(self);
373 return;
374 }
375
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 if (kIsDebugBuild) {
377 self->AssertThreadSuspensionIsAllowable();
378 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 CHECK_STREQ(GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 }
381
382 // Push a transition back into managed code onto the linked list in thread.
383 ManagedStack fragment;
384 self->PushManagedStackFragment(&fragment);
385
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700387 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200388 // If the runtime is not yet started or it is required by the debugger, then perform the
389 // Invocation by the interpreter.
390 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700391 if (IsStatic()) {
392 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
393 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 mirror::Object* receiver =
395 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Ian Rogers5d27faf2014-05-02 17:17:18 -0700396 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 }
398 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
400
401 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800402 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800403 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700404 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 LOG(INFO) << StringPrintf(
406 "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(),
407 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700408 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700409
Elliott Hughes956af0f2014-12-11 14:34:28 -0800410 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800411 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
412 DCHECK(!runtime->UseJit());
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700413 CHECK(IsEntrypointInterpreter())
414 << "Don't call compiled code when -Xint " << PrettyMethod(this);
415 }
416
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000417#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Elliott Hughes956af0f2014-12-11 14:34:28 -0800418 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800419 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800421 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800423#else
424 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
425#endif
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000426 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200427 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700428 // exception was thrown to force the activations to be removed from the
429 // stack. Continue execution in the interpreter.
430 self->ClearException();
Sebastien Hertzf7958692015-06-09 14:09:14 +0200431 ShadowFrame* shadow_frame =
432 self->PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700433 result->SetJ(self->PopDeoptimizationReturnValue().GetJ());
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700434 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700435 self->SetTopOfShadowStack(shadow_frame);
436 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
437 }
438 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800439 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
440 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800441 }
442 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800443 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800444 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800445 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 }
447 }
448 }
449
450 // Pop transition.
451 self->PopManagedStackFragment(fragment);
452}
453
Ian Rogers1a102182014-12-02 17:49:19 -0800454// Counts the number of references in the parameter list of the corresponding method.
455// Note: Thus does _not_ include "this" for non-static methods.
456static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
458 uint32_t shorty_len;
459 const char* shorty = method->GetShorty(&shorty_len);
460 uint32_t refs = 0;
461 for (uint32_t i = 1; i < shorty_len ; ++i) {
462 if (shorty[i] == 'L') {
463 refs++;
464 }
465 }
466 return refs;
467}
468
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700469QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700470 Runtime* runtime = Runtime::Current();
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200471
472 if (UNLIKELY(IsAbstract())) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700473 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
474 }
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200475
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 // This goes before IsProxyMethod since runtime methods have a null declaring class.
477 if (UNLIKELY(IsRuntimeMethod())) {
478 return runtime->GetRuntimeMethodFrameInfo(this);
479 }
480
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200481 // For Proxy method we add special handling for the direct method case (there is only one
482 // direct method - constructor). Direct method is cloned from original
483 // java.lang.reflect.Proxy class together with code and as a result it is executed as usual
484 // quick compiled method without any stubs. So the frame info should be returned as it is a
485 // quick method not a stub. However, if instrumentation stubs are installed, the
486 // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an
487 // oat code pointer, thus we have to add a special case here.
488 if (UNLIKELY(IsProxyMethod())) {
489 if (IsDirect()) {
490 CHECK(IsConstructor());
491 return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
492 } else {
493 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
494 }
495 }
496
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800497 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700498 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700499 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700500 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
501 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
502 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
503
504 if (class_linker->IsQuickGenericJniStub(entry_point)) {
505 // Generic JNI frame.
506 DCHECK(IsNative());
Ian Rogers1a102182014-12-02 17:49:19 -0800507 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(this) + 1;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700508 size_t scope_size = HandleScope::SizeOf(handle_refs);
509 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
510
511 // Callee saves + handle scope + method ref + alignment
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
513 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() - sizeof(void*) +
514 sizeof(ArtMethod*) + scope_size, kStackAlignment);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700515 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
516 }
517
518 const void* code_pointer = EntryPointToCodePointer(entry_point);
519 return GetQuickFrameInfo(code_pointer);
520}
521
522void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700524 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800525 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700526 if (is_fast) {
527 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800529 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530}
531
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700532void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700533 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700535 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536}
537
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700538bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
539 auto* dex_cache = GetDexCache();
540 auto* dex_file = dex_cache->GetDexFile();
541 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
542 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
543 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
544 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
545 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
546 if (param_len != count) {
547 return false;
548 }
549 auto* cl = Runtime::Current()->GetClassLinker();
550 for (size_t i = 0; i < count; ++i) {
551 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
552 auto* type = cl->ResolveType(type_idx, this);
553 if (type == nullptr) {
554 Thread::Current()->AssertPendingException();
555 return false;
556 }
557 if (type != params->GetWithoutChecks(i)) {
558 return false;
559 }
560 }
561 return true;
562}
563
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564} // namespace art