blob: 9518c9d79759ad9edca2413532ca2be30459f4a1 [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
Mathieu Chartierfc58af42015-04-16 18:00:39 -070019#include "abstract_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070020#include "arch/context.h"
Ian Rogers62f05122014-03-21 11:21:29 -070021#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "base/stringpiece.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "class-inl.h"
25#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070026#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/entrypoint_utils.h"
28#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "jit/jit.h"
32#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070034#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070036#include "object_array.h"
37#include "object-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070038#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "string.h"
Ian Rogers62f05122014-03-21 11:21:29 -070040#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42namespace art {
43namespace mirror {
44
Ian Rogers0177e532014-02-11 16:30:46 -080045extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
46 const char*);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000047#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Ian Rogers936b37f2014-02-14 00:52:24 -080048extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
49 const char*);
50#endif
Jeff Hao5d917302013-02-27 17:57:33 -080051
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070053GcRoot<Class> ArtMethod::java_lang_reflect_ArtMethod_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070055ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
56 jobject jlr_method) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070057 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method);
58 DCHECK(abstract_method != nullptr);
59 return abstract_method->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -070060}
61
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070062void ArtMethod::VisitRoots(RootVisitor* visitor) {
63 java_lang_reflect_ArtMethod_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080064}
65
Ian Rogers6b14d552014-10-28 21:50:58 -070066mirror::String* ArtMethod::GetNameAsString(Thread* self) {
67 mirror::ArtMethod* method = GetInterfaceMethodIfProxy();
68 const DexFile* dex_file = method->GetDexFile();
69 uint32_t dex_method_idx = method->GetDexMethodIndex();
70 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
71 StackHandleScope<1> hs(self);
72 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
73 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
74 dex_cache);
75}
76
Ian Rogersef7d42f2014-01-06 12:55:46 -080077InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 // TODO: kSuper?
79 if (GetDeclaringClass()->IsInterface()) {
80 return kInterface;
81 } else if (IsStatic()) {
82 return kStatic;
83 } else if (IsDirect()) {
84 return kDirect;
85 } else {
86 return kVirtual;
87 }
88}
89
Brian Carlstromea46f952013-07-30 01:26:50 -070090void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070091 CHECK(java_lang_reflect_ArtMethod_.IsNull());
Ian Rogersf2247512014-12-02 16:17:08 -080092 CHECK(java_lang_reflect_ArtMethod != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070093 java_lang_reflect_ArtMethod_ = GcRoot<Class>(java_lang_reflect_ArtMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094}
95
Brian Carlstromea46f952013-07-30 01:26:50 -070096void ArtMethod::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070097 CHECK(!java_lang_reflect_ArtMethod_.IsNull());
98 java_lang_reflect_ArtMethod_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099}
100
Brian Carlstromea46f952013-07-30 01:26:50 -0700101size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -0700102 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700104 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 char ch = shorty[i];
106 if (ch == 'D' || ch == 'J') {
107 num_registers += 2;
108 } else {
109 num_registers += 1;
110 }
111 }
112 return num_registers;
113}
114
Ian Rogersf2247512014-12-02 16:17:08 -0800115static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
118 const DexFile* dex_file = method1->GetDexFile();
119 const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex());
120 if (method1->GetDexCache() == method2->GetDexCache()) {
121 const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex());
122 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
123 }
124 const DexFile* dex_file2 = method2->GetDexFile();
125 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex());
126 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
127 return false; // Name mismatch.
128 }
129 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800134 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 }
136 Class* declaring_class = GetDeclaringClass();
137 Class* super_class = declaring_class->GetSuperClass();
138 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800139 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 // Did this method override a super class method? If so load the result from the super class'
141 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700142 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
143 result = super_class->GetVTableEntry(method_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 } else {
145 // Method didn't override superclass method so search interfaces
146 if (IsProxyMethod()) {
147 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
148 CHECK_EQ(result,
149 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
150 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800152 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 Class* interface = iftable->GetInterface(i);
154 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Ian Rogersf2247512014-12-02 16:17:08 -0800155 mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j);
156 if (HasSameNameAndSignature(this, interface_method)) {
157 result = interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 break;
159 }
160 }
161 }
162 }
163 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700164 if (kIsDebugBuild) {
Ian Rogersf2247512014-12-02 16:17:08 -0800165 DCHECK(result == nullptr || HasSameNameAndSignature(this, result));
Jeff Haof0a3f092014-07-24 16:26:09 -0700166 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 return result;
168}
169
Ian Rogerse0a02da2014-12-02 14:10:53 -0800170uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
171 uint32_t name_and_signature_idx) {
172 const DexFile* dexfile = GetDexFile();
173 const uint32_t dex_method_idx = GetDexMethodIndex();
174 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
175 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
176 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
177 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
178 if (dexfile == &other_dexfile) {
179 return dex_method_idx;
180 }
181 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
182 const DexFile::StringId* other_descriptor =
183 other_dexfile.FindStringId(mid_declaring_class_descriptor);
184 if (other_descriptor != nullptr) {
185 const DexFile::TypeId* other_type_id =
186 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
187 if (other_type_id != nullptr) {
188 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
189 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
190 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
191 if (other_mid != nullptr) {
192 return other_dexfile.GetIndexForMethodId(*other_mid);
193 }
194 }
195 }
196 return DexFile::kDexNoIndex;
197}
198
Dave Allisonb373e092014-02-20 16:06:36 -0800199uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800200 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000201 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
202 if (IsOptimized(sizeof(void*))) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000203 CodeInfo code_info = GetOptimizedCodeInfo();
204 return code_info.GetStackMapForNativePcOffset(sought_offset).GetDexPc(code_info);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000205 }
206
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800207 MappingTable table(entry_point != nullptr ?
208 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700209 if (table.TotalSize() == 0) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100210 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
211 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
213 return DexFile::kDexNoIndex; // Special no mapping case
214 }
Ian Rogers1809a722013-08-09 22:05:32 -0700215 // Assume the caller wants a pc-to-dex mapping so check here first.
216 typedef MappingTable::PcToDexIterator It;
217 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
218 if (cur.NativePcOffset() == sought_offset) {
219 return cur.DexPc();
220 }
221 }
222 // Now check dex-to-pc mappings.
223 typedef MappingTable::DexToPcIterator It2;
224 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
225 if (cur.NativePcOffset() == sought_offset) {
226 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 }
228 }
Dave Allisonb373e092014-02-20 16:06:36 -0800229 if (abort_on_failure) {
230 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100231 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800232 << " current entry_point=" << GetQuickOatEntryPoint(sizeof(void*))
Dave Allisonb373e092014-02-20 16:06:36 -0800233 << ") in " << PrettyMethod(this);
234 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236}
237
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000238uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800239 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
240 MappingTable table(entry_point != nullptr ?
241 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700242 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 DCHECK_EQ(dex_pc, 0U);
244 return 0; // Special no mapping/pc == 0 case
245 }
Ian Rogers1809a722013-08-09 22:05:32 -0700246 // Assume the caller wants a dex-to-pc mapping so check here first.
247 typedef MappingTable::DexToPcIterator It;
248 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
249 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100250 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
252 }
Ian Rogers1809a722013-08-09 22:05:32 -0700253 // Now check pc-to-dex mappings.
254 typedef MappingTable::PcToDexIterator It2;
255 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
256 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100257 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700258 }
259 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000260 if (abort_on_failure) {
261 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
262 << " in " << PrettyMethod(this);
263 }
264 return UINTPTR_MAX;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265}
266
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700267uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type,
268 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700269 const DexFile::CodeItem* code_item = h_this->GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700270 // Set aside the exception while we resolve its type.
271 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700272 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000273 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700274 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700275 // Default to handler not found.
276 uint32_t found_dex_pc = DexFile::kDexNoIndex;
277 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
279 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
280 // Catch all case
281 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700282 found_dex_pc = it.GetHandlerAddress();
283 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 }
285 // Does this catch exception type apply?
Ian Rogersa0485602014-12-02 15:48:04 -0800286 Class* iter_exception_type = h_this->GetClassFromTypeIndex(iter_type_idx, true);
Ian Rogers822266b2014-05-29 16:55:06 -0700287 if (UNLIKELY(iter_exception_type == nullptr)) {
288 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
289 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700290 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700291 self->ClearException();
292 // Delete any long jump context as this routine is called during a stack walk which will
293 // release its in use context at the end.
294 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700296 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700297 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700298 found_dex_pc = it.GetHandlerAddress();
299 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 }
301 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700302 if (found_dex_pc != DexFile::kDexNoIndex) {
303 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700304 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700305 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
306 }
Jeff Haoaa961912014-04-22 13:54:32 -0700307 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700308 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000309 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700310 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700311 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312}
313
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700314void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
315 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
316 return;
317 }
318 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
319 return;
320 }
321 const void* code = GetEntryPointFromQuickCompiledCode();
322 if (code == GetQuickInstrumentationEntryPoint()) {
323 return;
324 }
325 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
326 if (class_linker->IsQuickToInterpreterBridge(code) ||
327 class_linker->IsQuickResolutionStub(code)) {
328 return;
329 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800330 // If we are the JIT then we may have just compiled the method after the
331 // IsQuickToInterpreterBridge check.
332 jit::Jit* const jit = Runtime::Current()->GetJit();
333 if (jit != nullptr &&
334 jit->GetCodeCache()->ContainsCodePtr(reinterpret_cast<const void*>(code))) {
335 return;
336 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700337 /*
338 * During a stack walk, a return PC may point past-the-end of the code
339 * in the case that the last instruction is a call that isn't expected to
340 * return. Thus, we check <= code + GetCodeSize().
341 *
342 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
343 */
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800344 CHECK(PcIsWithinQuickCode(reinterpret_cast<uintptr_t>(code), pc))
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700345 << PrettyMethod(this)
346 << " pc=" << std::hex << pc
347 << " code=" << code
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800348 << " size=" << GetCodeSize(
349 EntryPointToCodePointer(reinterpret_cast<const void*>(code)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700350}
351
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700352bool ArtMethod::IsEntrypointInterpreter() {
353 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800354 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
355 return oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700356}
357
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800358const void* ArtMethod::GetQuickOatEntryPoint(size_t pointer_size) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800359 if (IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700360 return nullptr;
361 }
362 Runtime* runtime = Runtime::Current();
363 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800364 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700366 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
367 // for non-native methods.
368 if (class_linker->IsQuickToInterpreterBridge(code) ||
369 class_linker->IsQuickGenericJniStub(code)) {
370 return nullptr;
371 }
372 return code;
373}
374
375#ifndef NDEBUG
376uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
377 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800378 CHECK_EQ(quick_entry_point, Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700379 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
380}
381#endif
382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800384 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700385 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
386 ThrowStackOverflowError(self);
387 return;
388 }
389
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 if (kIsDebugBuild) {
391 self->AssertThreadSuspensionIsAllowable();
392 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700393 CHECK_STREQ(GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 }
395
396 // Push a transition back into managed code onto the linked list in thread.
397 ManagedStack fragment;
398 self->PushManagedStackFragment(&fragment);
399
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700401 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200402 // If the runtime is not yet started or it is required by the debugger, then perform the
403 // Invocation by the interpreter.
404 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700405 if (IsStatic()) {
406 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
407 } else {
408 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
409 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 }
411 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800413 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800414 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700415 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800416 LOG(INFO) << StringPrintf("Invoking '%s' quick code=%p", PrettyMethod(this).c_str(),
417 GetEntryPointFromQuickCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700418 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700419
Elliott Hughes956af0f2014-12-11 14:34:28 -0800420 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800421 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
422 DCHECK(!runtime->UseJit());
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700423 CHECK(IsEntrypointInterpreter())
424 << "Don't call compiled code when -Xint " << PrettyMethod(this);
425 }
426
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000427#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Elliott Hughes956af0f2014-12-11 14:34:28 -0800428 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800429 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800431 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800432 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800433#else
434 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
435#endif
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000436 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200437 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700438 // exception was thrown to force the activations to be removed from the
439 // stack. Continue execution in the interpreter.
440 self->ClearException();
441 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700442 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700443 self->SetTopOfShadowStack(shadow_frame);
444 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
445 }
446 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800447 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
448 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800449 }
450 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800451 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800452 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800453 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 }
455 }
456 }
457
458 // Pop transition.
459 self->PopManagedStackFragment(fragment);
460}
461
Ian Rogers1a102182014-12-02 17:49:19 -0800462// Counts the number of references in the parameter list of the corresponding method.
463// Note: Thus does _not_ include "this" for non-static methods.
464static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
466 uint32_t shorty_len;
467 const char* shorty = method->GetShorty(&shorty_len);
468 uint32_t refs = 0;
469 for (uint32_t i = 1; i < shorty_len ; ++i) {
470 if (shorty[i] == 'L') {
471 refs++;
472 }
473 }
474 return refs;
475}
476
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700477QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700478 Runtime* runtime = Runtime::Current();
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200479
480 if (UNLIKELY(IsAbstract())) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700481 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
482 }
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200483
484 // For Proxy method we add special handling for the direct method case (there is only one
485 // direct method - constructor). Direct method is cloned from original
486 // java.lang.reflect.Proxy class together with code and as a result it is executed as usual
487 // quick compiled method without any stubs. So the frame info should be returned as it is a
488 // quick method not a stub. However, if instrumentation stubs are installed, the
489 // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an
490 // oat code pointer, thus we have to add a special case here.
491 if (UNLIKELY(IsProxyMethod())) {
492 if (IsDirect()) {
493 CHECK(IsConstructor());
494 return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
495 } else {
496 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
497 }
498 }
499
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700500 if (UNLIKELY(IsRuntimeMethod())) {
501 return runtime->GetRuntimeMethodFrameInfo(this);
502 }
503
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800504 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700505 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700506 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700507 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
508 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
509 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
510
511 if (class_linker->IsQuickGenericJniStub(entry_point)) {
512 // Generic JNI frame.
513 DCHECK(IsNative());
Ian Rogers1a102182014-12-02 17:49:19 -0800514 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(this) + 1;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700515 size_t scope_size = HandleScope::SizeOf(handle_refs);
516 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
517
518 // Callee saves + handle scope + method ref + alignment
519 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() + scope_size
520 - sizeof(void*) // callee-save frame stores a whole method pointer
521 + sizeof(StackReference<mirror::ArtMethod>),
522 kStackAlignment);
523
524 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
525 }
526
527 const void* code_pointer = EntryPointToCodePointer(entry_point);
528 return GetQuickFrameInfo(code_pointer);
529}
530
531void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700533 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800534 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700535 if (is_fast) {
536 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800538 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539}
540
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700541void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700542 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700544 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545}
546
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700547bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
548 auto* dex_cache = GetDexCache();
549 auto* dex_file = dex_cache->GetDexFile();
550 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
551 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
552 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
553 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
554 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
555 if (param_len != count) {
556 return false;
557 }
558 auto* cl = Runtime::Current()->GetClassLinker();
559 for (size_t i = 0; i < count; ++i) {
560 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
561 auto* type = cl->ResolveType(type_idx, this);
562 if (type == nullptr) {
563 Thread::Current()->AssertPendingException();
564 return false;
565 }
566 if (type != params->GetWithoutChecks(i)) {
567 return false;
568 }
569 }
570 return true;
571}
572
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573} // namespace mirror
574} // namespace art