blob: 3e3232b117441da8c8eb68a3275cf440789b1b62 [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"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070023#include "class_linker-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070024#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#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"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010033#include "jit/profiling_info.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070035#include "mapping_table.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/abstract_method.h"
37#include "mirror/class-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000041#include "oat_file-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070042#include "scoped_thread_state_change.h"
Ian Rogers62f05122014-03-21 11:21:29 -070043#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
45namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046
Ian Rogers0177e532014-02-11 16:30:46 -080047extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
48 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080049extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
50 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080051
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070052ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
53 jobject jlr_method) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070054 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method);
55 DCHECK(abstract_method != nullptr);
56 return abstract_method->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -070057}
58
Ian Rogers6b14d552014-10-28 21:50:58 -070059mirror::String* ArtMethod::GetNameAsString(Thread* self) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 CHECK(!IsProxyMethod());
Ian Rogers6b14d552014-10-28 21:50:58 -070061 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
63 auto* dex_file = dex_cache->GetDexFile();
64 uint32_t dex_method_idx = GetDexMethodIndex();
65 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
Ian Rogers6b14d552014-10-28 21:50:58 -070066 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
67 dex_cache);
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 -070083size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070084 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070086 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 char ch = shorty[i];
88 if (ch == 'D' || ch == 'J') {
89 num_registers += 2;
90 } else {
91 num_registers += 1;
92 }
93 }
94 return num_registers;
95}
96
Ian Rogersf2247512014-12-02 16:17:08 -080097static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf2247512014-12-02 16:17:08 -080099 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
100 const DexFile* dex_file = method1->GetDexFile();
101 const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex());
102 if (method1->GetDexCache() == method2->GetDexCache()) {
103 const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex());
104 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
105 }
106 const DexFile* dex_file2 = method2->GetDexFile();
107 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex());
108 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
109 return false; // Name mismatch.
110 }
111 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
112}
113
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114ArtMethod* ArtMethod::FindOverriddenMethod(size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800116 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 mirror::Class* declaring_class = GetDeclaringClass();
119 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800121 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 // Did this method override a super class method? If so load the result from the super class'
123 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700124 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 } else {
127 // Method didn't override superclass method so search interfaces
128 if (IsProxyMethod()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100129 result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
130 GetDexMethodIndex(),
131 pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 CHECK_EQ(result,
133 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
134 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800136 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700139 ArtMethod* interface_method = interface->GetVirtualMethod(j, pointer_size);
140 if (HasSameNameAndSignature(
141 this, interface_method->GetInterfaceMethodIfProxy(sizeof(void*)))) {
Ian Rogersf2247512014-12-02 16:17:08 -0800142 result = interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 break;
144 }
145 }
146 }
147 }
148 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 DCHECK(result == nullptr || HasSameNameAndSignature(
150 GetInterfaceMethodIfProxy(sizeof(void*)), result->GetInterfaceMethodIfProxy(sizeof(void*))));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 return result;
152}
153
Ian Rogerse0a02da2014-12-02 14:10:53 -0800154uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
155 uint32_t name_and_signature_idx) {
156 const DexFile* dexfile = GetDexFile();
157 const uint32_t dex_method_idx = GetDexMethodIndex();
158 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
159 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
160 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
161 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
162 if (dexfile == &other_dexfile) {
163 return dex_method_idx;
164 }
165 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
166 const DexFile::StringId* other_descriptor =
167 other_dexfile.FindStringId(mid_declaring_class_descriptor);
168 if (other_descriptor != nullptr) {
169 const DexFile::TypeId* other_type_id =
170 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
171 if (other_type_id != nullptr) {
172 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
173 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
174 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
175 if (other_mid != nullptr) {
176 return other_dexfile.GetIndexForMethodId(*other_mid);
177 }
178 }
179 }
180 return DexFile::kDexNoIndex;
181}
182
Dave Allisonb373e092014-02-20 16:06:36 -0800183uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800184 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000185 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
186 if (IsOptimized(sizeof(void*))) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000187 CodeInfo code_info = GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100188 StackMapEncoding encoding = code_info.ExtractEncoding();
189 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset, encoding);
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100190 if (stack_map.IsValid()) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100191 return stack_map.GetDexPc(encoding);
Ian Rogers1809a722013-08-09 22:05:32 -0700192 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100193 } else {
194 MappingTable table(entry_point != nullptr ?
195 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
196 if (table.TotalSize() == 0) {
197 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
198 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
199 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
200 return DexFile::kDexNoIndex; // Special no mapping case
201 }
202 // Assume the caller wants a pc-to-dex mapping so check here first.
203 typedef MappingTable::PcToDexIterator It;
204 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
205 if (cur.NativePcOffset() == sought_offset) {
206 return cur.DexPc();
207 }
208 }
209 // Now check dex-to-pc mappings.
210 typedef MappingTable::DexToPcIterator It2;
211 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
212 if (cur.NativePcOffset() == sought_offset) {
213 return cur.DexPc();
214 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 }
216 }
Dave Allisonb373e092014-02-20 16:06:36 -0800217 if (abort_on_failure) {
218 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100219 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220 << " current entry_point=" << GetQuickOatEntryPoint(sizeof(void*))
Dave Allisonb373e092014-02-20 16:06:36 -0800221 << ") in " << PrettyMethod(this);
222 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224}
225
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000226uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800227 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (IsOptimized(sizeof(void*))) {
229 // Optimized code does not have a mapping table. Search for the dex-to-pc
230 // mapping in stack maps.
231 CodeInfo code_info = GetOptimizedCodeInfo();
232 StackMapEncoding encoding = code_info.ExtractEncoding();
233
234 // Assume the caller needs the mapping for a catch handler. If there are
235 // multiple stack maps for this dex_pc, it will hit the catch stack map first.
236 StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc, encoding);
237 if (stack_map.IsValid()) {
238 return reinterpret_cast<uintptr_t>(entry_point) + stack_map.GetNativePcOffset(encoding);
239 }
240 } else {
241 MappingTable table(entry_point != nullptr ?
242 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
243 if (table.TotalSize() == 0) {
244 DCHECK_EQ(dex_pc, 0U);
245 return 0; // Special no mapping/pc == 0 case
246 }
247 // Assume the caller wants a dex-to-pc mapping so check here first.
248 typedef MappingTable::DexToPcIterator It;
249 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
250 if (cur.DexPc() == dex_pc) {
251 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
252 }
253 }
254 // Now check pc-to-dex mappings.
255 typedef MappingTable::PcToDexIterator It2;
256 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
257 if (cur.DexPc() == dex_pc) {
258 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
259 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 }
261 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000262
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000263 if (abort_on_failure) {
264 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
265 << " in " << PrettyMethod(this);
266 }
267 return UINTPTR_MAX;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268}
269
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700271 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700273 // Set aside the exception while we resolve its type.
274 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000276 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700277 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700278 // Default to handler not found.
279 uint32_t found_dex_pc = DexFile::kDexNoIndex;
280 // Iterate over the catch handlers associated with dex_pc.
Vladimir Marko05792b92015-08-03 11:56:49 +0100281 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
283 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
284 // Catch all case
285 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700286 found_dex_pc = it.GetHandlerAddress();
287 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 }
289 // Does this catch exception type apply?
Vladimir Marko05792b92015-08-03 11:56:49 +0100290 mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx,
291 true /* resolve */,
292 pointer_size);
Ian Rogers822266b2014-05-29 16:55:06 -0700293 if (UNLIKELY(iter_exception_type == nullptr)) {
294 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
295 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700296 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700297 self->ClearException();
298 // Delete any long jump context as this routine is called during a stack walk which will
299 // release its in use context at the end.
300 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700303 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700304 found_dex_pc = it.GetHandlerAddress();
305 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 }
307 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700308 if (found_dex_pc != DexFile::kDexNoIndex) {
309 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700310 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700311 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
312 }
Jeff Haoaa961912014-04-22 13:54:32 -0700313 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700314 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000315 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700316 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700317 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318}
319
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700320void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
321 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
322 return;
323 }
324 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
325 return;
326 }
327 const void* code = GetEntryPointFromQuickCompiledCode();
328 if (code == GetQuickInstrumentationEntryPoint()) {
329 return;
330 }
331 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
332 if (class_linker->IsQuickToInterpreterBridge(code) ||
333 class_linker->IsQuickResolutionStub(code)) {
334 return;
335 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800336 // If we are the JIT then we may have just compiled the method after the
337 // IsQuickToInterpreterBridge check.
338 jit::Jit* const jit = Runtime::Current()->GetJit();
339 if (jit != nullptr &&
340 jit->GetCodeCache()->ContainsCodePtr(reinterpret_cast<const void*>(code))) {
341 return;
342 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700343 /*
344 * During a stack walk, a return PC may point past-the-end of the code
345 * in the case that the last instruction is a call that isn't expected to
346 * return. Thus, we check <= code + GetCodeSize().
347 *
348 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
349 */
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800350 CHECK(PcIsWithinQuickCode(reinterpret_cast<uintptr_t>(code), pc))
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700351 << PrettyMethod(this)
352 << " pc=" << std::hex << pc
353 << " code=" << code
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800354 << " size=" << GetCodeSize(
355 EntryPointToCodePointer(reinterpret_cast<const void*>(code)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700356}
357
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700358bool ArtMethod::IsEntrypointInterpreter() {
359 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800360 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
361 return oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700362}
363
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800364const void* ArtMethod::GetQuickOatEntryPoint(size_t pointer_size) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800365 if (IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700366 return nullptr;
367 }
368 Runtime* runtime = Runtime::Current();
369 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800370 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700372 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
373 // for non-native methods.
374 if (class_linker->IsQuickToInterpreterBridge(code) ||
375 class_linker->IsQuickGenericJniStub(code)) {
376 return nullptr;
377 }
378 return code;
379}
380
381#ifndef NDEBUG
382uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
383 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 CHECK_EQ(quick_entry_point,
385 Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700386 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
387}
388#endif
389
Brian Carlstromea46f952013-07-30 01:26:50 -0700390void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800391 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700392 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
393 ThrowStackOverflowError(self);
394 return;
395 }
396
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 if (kIsDebugBuild) {
398 self->AssertThreadSuspensionIsAllowable();
399 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 CHECK_STREQ(GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 }
402
403 // Push a transition back into managed code onto the linked list in thread.
404 ManagedStack fragment;
405 self->PushManagedStackFragment(&fragment);
406
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700408 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200409 // If the runtime is not yet started or it is required by the debugger, then perform the
410 // Invocation by the interpreter.
411 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700412 if (IsStatic()) {
413 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
414 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 mirror::Object* receiver =
416 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Ian Rogers5d27faf2014-05-02 17:17:18 -0700417 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 }
419 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
421
422 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800423 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800424 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700425 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 LOG(INFO) << StringPrintf(
427 "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(),
428 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700429 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700430
Elliott Hughes956af0f2014-12-11 14:34:28 -0800431 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800432 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
433 DCHECK(!runtime->UseJit());
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700434 CHECK(IsEntrypointInterpreter())
435 << "Don't call compiled code when -Xint " << PrettyMethod(this);
436 }
437
Elliott Hughes956af0f2014-12-11 14:34:28 -0800438 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800439 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800441 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800442 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000443 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200444 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700445 // exception was thrown to force the activations to be removed from the
446 // stack. Continue execution in the interpreter.
447 self->ClearException();
Sebastien Hertzf7958692015-06-09 14:09:14 +0200448 ShadowFrame* shadow_frame =
449 self->PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame);
Sebastien Hertz07474662015-08-25 15:12:33 +0000450 mirror::Throwable* pending_exception = nullptr;
451 self->PopDeoptimizationContext(result, &pending_exception);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700452 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700453 self->SetTopOfShadowStack(shadow_frame);
Sebastien Hertz07474662015-08-25 15:12:33 +0000454
455 // Restore the exception that was pending before deoptimization then interpret the
456 // deoptimized frames.
457 if (pending_exception != nullptr) {
458 self->SetException(pending_exception);
459 }
Jeff Hao790ad902013-05-22 15:02:08 -0700460 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
461 }
462 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800463 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
464 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800465 }
466 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800468 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800469 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470 }
471 }
472 }
473
474 // Pop transition.
475 self->PopManagedStackFragment(fragment);
476}
477
Ian Rogers1a102182014-12-02 17:49:19 -0800478// Counts the number of references in the parameter list of the corresponding method.
479// Note: Thus does _not_ include "this" for non-static methods.
480static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700481 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1a102182014-12-02 17:49:19 -0800482 uint32_t shorty_len;
483 const char* shorty = method->GetShorty(&shorty_len);
484 uint32_t refs = 0;
485 for (uint32_t i = 1; i < shorty_len ; ++i) {
486 if (shorty[i] == 'L') {
487 refs++;
488 }
489 }
490 return refs;
491}
492
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700493QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700494 Runtime* runtime = Runtime::Current();
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200495
496 if (UNLIKELY(IsAbstract())) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700497 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
498 }
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200499
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500 // This goes before IsProxyMethod since runtime methods have a null declaring class.
501 if (UNLIKELY(IsRuntimeMethod())) {
502 return runtime->GetRuntimeMethodFrameInfo(this);
503 }
504
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200505 // For Proxy method we add special handling for the direct method case (there is only one
506 // direct method - constructor). Direct method is cloned from original
507 // java.lang.reflect.Proxy class together with code and as a result it is executed as usual
508 // quick compiled method without any stubs. So the frame info should be returned as it is a
509 // quick method not a stub. However, if instrumentation stubs are installed, the
510 // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an
511 // oat code pointer, thus we have to add a special case here.
512 if (UNLIKELY(IsProxyMethod())) {
513 if (IsDirect()) {
514 CHECK(IsConstructor());
515 return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
516 } else {
517 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
518 }
519 }
520
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800521 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700522 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700523 // On failure, instead of null we get the quick-generic-jni-trampoline for native method
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700524 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
525 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
526 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
527
528 if (class_linker->IsQuickGenericJniStub(entry_point)) {
529 // Generic JNI frame.
530 DCHECK(IsNative());
Ian Rogers1a102182014-12-02 17:49:19 -0800531 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(this) + 1;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700532 size_t scope_size = HandleScope::SizeOf(handle_refs);
533 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
534
535 // Callee saves + handle scope + method ref + alignment
Mathieu Chartiere401d142015-04-22 13:56:20 -0700536 // Note: -sizeof(void*) since callee-save frame stores a whole method pointer.
537 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() - sizeof(void*) +
538 sizeof(ArtMethod*) + scope_size, kStackAlignment);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700539 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
540 }
541
542 const void* code_pointer = EntryPointToCodePointer(entry_point);
543 return GetQuickFrameInfo(code_pointer);
544}
545
546void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700548 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800549 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700550 if (is_fast) {
551 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800553 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554}
555
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700556void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700557 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700559 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560}
561
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700562bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
563 auto* dex_cache = GetDexCache();
564 auto* dex_file = dex_cache->GetDexFile();
565 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
566 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
567 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
568 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
569 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
570 if (param_len != count) {
571 return false;
572 }
573 auto* cl = Runtime::Current()->GetClassLinker();
574 for (size_t i = 0; i < count; ++i) {
575 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
576 auto* type = cl->ResolveType(type_idx, this);
577 if (type == nullptr) {
578 Thread::Current()->AssertPendingException();
579 return false;
580 }
581 if (type != params->GetWithoutChecks(i)) {
582 return false;
583 }
584 }
585 return true;
586}
587
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000588const uint8_t* ArtMethod::GetQuickenedInfo() {
589 bool found = false;
590 OatFile::OatMethod oat_method =
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700591 Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found);
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000592 if (!found || (oat_method.GetQuickCode() != nullptr)) {
593 return nullptr;
594 }
595 return oat_method.GetVmapTable();
596}
597
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100598ProfilingInfo* ArtMethod::CreateProfilingInfo() {
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700599 DCHECK(!Runtime::Current()->IsAotCompiler());
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100600 ProfilingInfo* info = ProfilingInfo::Create(this);
601 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(sizeof(void*));
602 uintptr_t pointer = reinterpret_cast<uintptr_t>(this) + offset.Uint32Value();
603 if (!reinterpret_cast<Atomic<ProfilingInfo*>*>(pointer)->
604 CompareExchangeStrongSequentiallyConsistent(nullptr, info)) {
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700605 return GetProfilingInfo(sizeof(void*));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100606 } else {
607 return info;
608 }
609}
610
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611} // namespace art