blob: 26f6f345d54751b6d1d71995e25f303ba2b69031 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromea46f952013-07-30 01:26:50 -070017#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Ian Rogers62f05122014-03-21 11:21:29 -070020#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/stringpiece.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "class-inl.h"
24#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070025#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/entrypoint_utils.h"
27#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070033#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "object_array-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070035#include "object_array.h"
36#include "object-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070037#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "string.h"
Ian Rogers62f05122014-03-21 11:21:29 -070039#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41namespace art {
42namespace mirror {
43
Ian Rogers0177e532014-02-11 16:30:46 -080044extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
45 const char*);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000046#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Ian Rogers936b37f2014-02-14 00:52:24 -080047extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
48 const char*);
49#endif
Jeff Hao5d917302013-02-27 17:57:33 -080050
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070052GcRoot<Class> ArtMethod::java_lang_reflect_ArtMethod_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070054ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
55 jobject jlr_method) {
Ian Rogers62f05122014-03-21 11:21:29 -070056 mirror::ArtField* f =
57 soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
58 mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod();
59 DCHECK(method != nullptr);
60 return method;
61}
62
63
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080064void ArtMethod::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080065 java_lang_reflect_ArtMethod_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080066}
67
Ian Rogers6b14d552014-10-28 21:50:58 -070068mirror::String* ArtMethod::GetNameAsString(Thread* self) {
69 mirror::ArtMethod* method = GetInterfaceMethodIfProxy();
70 const DexFile* dex_file = method->GetDexFile();
71 uint32_t dex_method_idx = method->GetDexMethodIndex();
72 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
73 StackHandleScope<1> hs(self);
74 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
75 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
76 dex_cache);
77}
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 // TODO: kSuper?
81 if (GetDeclaringClass()->IsInterface()) {
82 return kInterface;
83 } else if (IsStatic()) {
84 return kStatic;
85 } else if (IsDirect()) {
86 return kDirect;
87 } else {
88 return kVirtual;
89 }
90}
91
Brian Carlstromea46f952013-07-30 01:26:50 -070092void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070093 CHECK(java_lang_reflect_ArtMethod_.IsNull());
Ian Rogersf2247512014-12-02 16:17:08 -080094 CHECK(java_lang_reflect_ArtMethod != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070095 java_lang_reflect_ArtMethod_ = GcRoot<Class>(java_lang_reflect_ArtMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096}
97
Brian Carlstromea46f952013-07-30 01:26:50 -070098void ArtMethod::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070099 CHECK(!java_lang_reflect_ArtMethod_.IsNull());
100 java_lang_reflect_ArtMethod_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101}
102
Brian Carlstromea46f952013-07-30 01:26:50 -0700103size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -0700104 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700106 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 char ch = shorty[i];
108 if (ch == 'D' || ch == 'J') {
109 num_registers += 2;
110 } else {
111 num_registers += 1;
112 }
113 }
114 return num_registers;
115}
116
Ian Rogersf2247512014-12-02 16:17:08 -0800117static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
120 const DexFile* dex_file = method1->GetDexFile();
121 const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex());
122 if (method1->GetDexCache() == method2->GetDexCache()) {
123 const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex());
124 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
125 }
126 const DexFile* dex_file2 = method2->GetDexFile();
127 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex());
128 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
129 return false; // Name mismatch.
130 }
131 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
132}
133
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800136 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 }
138 Class* declaring_class = GetDeclaringClass();
139 Class* super_class = declaring_class->GetSuperClass();
140 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800141 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 // Did this method override a super class method? If so load the result from the super class'
143 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700144 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
145 result = super_class->GetVTableEntry(method_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 } else {
147 // Method didn't override superclass method so search interfaces
148 if (IsProxyMethod()) {
149 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
150 CHECK_EQ(result,
151 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
152 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800154 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 Class* interface = iftable->GetInterface(i);
156 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Ian Rogersf2247512014-12-02 16:17:08 -0800157 mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j);
158 if (HasSameNameAndSignature(this, interface_method)) {
159 result = interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 break;
161 }
162 }
163 }
164 }
165 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700166 if (kIsDebugBuild) {
Ian Rogersf2247512014-12-02 16:17:08 -0800167 DCHECK(result == nullptr || HasSameNameAndSignature(this, result));
Jeff Haof0a3f092014-07-24 16:26:09 -0700168 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 return result;
170}
171
Ian Rogerse0a02da2014-12-02 14:10:53 -0800172uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
173 uint32_t name_and_signature_idx) {
174 const DexFile* dexfile = GetDexFile();
175 const uint32_t dex_method_idx = GetDexMethodIndex();
176 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
177 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
178 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
179 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
180 if (dexfile == &other_dexfile) {
181 return dex_method_idx;
182 }
183 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
184 const DexFile::StringId* other_descriptor =
185 other_dexfile.FindStringId(mid_declaring_class_descriptor);
186 if (other_descriptor != nullptr) {
187 const DexFile::TypeId* other_type_id =
188 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
189 if (other_type_id != nullptr) {
190 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
191 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
192 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
193 if (other_mid != nullptr) {
194 return other_dexfile.GetIndexForMethodId(*other_mid);
195 }
196 }
197 }
198 return DexFile::kDexNoIndex;
199}
200
Dave Allisonb373e092014-02-20 16:06:36 -0800201uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800202 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000203 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
204 if (IsOptimized(sizeof(void*))) {
205 uint32_t ret = GetStackMap(sought_offset).GetDexPc();
206 return ret;
207 }
208
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800209 MappingTable table(entry_point != nullptr ?
210 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700211 if (table.TotalSize() == 0) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100212 // NOTE: Special methods (see Mir2Lir::GenSpecialCase()) have an empty mapping
213 // but they have no suspend checks and, consequently, we never call ToDexPc() for them.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
215 return DexFile::kDexNoIndex; // Special no mapping case
216 }
Ian Rogers1809a722013-08-09 22:05:32 -0700217 // Assume the caller wants a pc-to-dex mapping so check here first.
218 typedef MappingTable::PcToDexIterator It;
219 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
220 if (cur.NativePcOffset() == sought_offset) {
221 return cur.DexPc();
222 }
223 }
224 // Now check dex-to-pc mappings.
225 typedef MappingTable::DexToPcIterator It2;
226 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
227 if (cur.NativePcOffset() == sought_offset) {
228 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 }
230 }
Dave Allisonb373e092014-02-20 16:06:36 -0800231 if (abort_on_failure) {
232 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100233 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800234 << " current entry_point=" << GetQuickOatEntryPoint(sizeof(void*))
Dave Allisonb373e092014-02-20 16:06:36 -0800235 << ") in " << PrettyMethod(this);
236 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238}
239
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000240uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800241 const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
242 MappingTable table(entry_point != nullptr ?
243 GetMappingTable(EntryPointToCodePointer(entry_point), sizeof(void*)) : nullptr);
Ian Rogers1809a722013-08-09 22:05:32 -0700244 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 DCHECK_EQ(dex_pc, 0U);
246 return 0; // Special no mapping/pc == 0 case
247 }
Ian Rogers1809a722013-08-09 22:05:32 -0700248 // Assume the caller wants a dex-to-pc mapping so check here first.
249 typedef MappingTable::DexToPcIterator It;
250 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
251 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100252 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 }
254 }
Ian Rogers1809a722013-08-09 22:05:32 -0700255 // Now check pc-to-dex mappings.
256 typedef MappingTable::PcToDexIterator It2;
257 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
258 if (cur.DexPc() == dex_pc) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100259 return reinterpret_cast<uintptr_t>(entry_point) + cur.NativePcOffset();
Ian Rogers1809a722013-08-09 22:05:32 -0700260 }
261 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000262 if (abort_on_failure) {
263 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
264 << " in " << PrettyMethod(this);
265 }
266 return UINTPTR_MAX;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267}
268
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700269uint32_t ArtMethod::FindCatchBlock(Handle<ArtMethod> h_this, Handle<Class> exception_type,
270 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700271 const DexFile::CodeItem* code_item = h_this->GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700272 // Set aside the exception while we resolve its type.
273 Thread* self = Thread::Current();
274 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 StackHandleScope<1> hs(self);
276 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Sebastien Hertz9f102032014-05-23 08:59:42 +0200277 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Jeff Haoaa961912014-04-22 13:54:32 -0700278 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700279 // Default to handler not found.
280 uint32_t found_dex_pc = DexFile::kDexNoIndex;
281 // Iterate over the catch handlers associated with dex_pc.
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?
Ian Rogersa0485602014-12-02 15:48:04 -0800290 Class* iter_exception_type = h_this->GetClassFromTypeIndex(iter_type_idx, true);
Ian Rogers822266b2014-05-29 16:55:06 -0700291 if (UNLIKELY(iter_exception_type == nullptr)) {
292 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
293 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700294 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700295 self->ClearException();
296 // Delete any long jump context as this routine is called during a stack walk which will
297 // release its in use context at the end.
298 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700300 << DescriptorToDot(h_this->GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700302 found_dex_pc = it.GetHandlerAddress();
303 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 }
305 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700306 if (found_dex_pc != DexFile::kDexNoIndex) {
307 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700308 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700309 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
310 }
Jeff Haoaa961912014-04-22 13:54:32 -0700311 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700312 if (exception.Get() != nullptr) {
313 self->SetException(throw_location, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200314 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Jeff Haoaa961912014-04-22 13:54:32 -0700315 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700316 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317}
318
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700319void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
320 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
321 return;
322 }
323 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
324 return;
325 }
326 const void* code = GetEntryPointFromQuickCompiledCode();
327 if (code == GetQuickInstrumentationEntryPoint()) {
328 return;
329 }
330 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
331 if (class_linker->IsQuickToInterpreterBridge(code) ||
332 class_linker->IsQuickResolutionStub(code)) {
333 return;
334 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800335 // If we are the JIT then we may have just compiled the method after the
336 // IsQuickToInterpreterBridge check.
337 jit::Jit* const jit = Runtime::Current()->GetJit();
338 if (jit != nullptr &&
339 jit->GetCodeCache()->ContainsCodePtr(reinterpret_cast<const void*>(code))) {
340 return;
341 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700342 /*
343 * During a stack walk, a return PC may point past-the-end of the code
344 * in the case that the last instruction is a call that isn't expected to
345 * return. Thus, we check <= code + GetCodeSize().
346 *
347 * NOTE: For Thumb both pc and code are offset by 1 indicating the Thumb state.
348 */
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800349 CHECK(PcIsWithinQuickCode(reinterpret_cast<uintptr_t>(code), pc))
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700350 << PrettyMethod(this)
351 << " pc=" << std::hex << pc
352 << " code=" << code
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800353 << " size=" << GetCodeSize(
354 EntryPointToCodePointer(reinterpret_cast<const void*>(code)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700355}
356
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700357bool ArtMethod::IsEntrypointInterpreter() {
358 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800359 const void* oat_quick_code = class_linker->GetOatMethodQuickCodeFor(this);
360 return oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode();
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700361}
362
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800363const void* ArtMethod::GetQuickOatEntryPoint(size_t pointer_size) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800364 if (IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700365 return nullptr;
366 }
367 Runtime* runtime = Runtime::Current();
368 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800369 const void* code = runtime->GetInstrumentation()->GetQuickCodeFor(this, pointer_size);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700370 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
371 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
372 // for non-native methods.
373 if (class_linker->IsQuickToInterpreterBridge(code) ||
374 class_linker->IsQuickGenericJniStub(code)) {
375 return nullptr;
376 }
377 return code;
378}
379
380#ifndef NDEBUG
381uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc, const void* quick_entry_point) {
382 CHECK_NE(quick_entry_point, GetQuickToInterpreterBridge());
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800383 CHECK_EQ(quick_entry_point, Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*)));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700384 return pc - reinterpret_cast<uintptr_t>(quick_entry_point);
385}
386#endif
387
Brian Carlstromea46f952013-07-30 01:26:50 -0700388void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800389 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700390 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
391 ThrowStackOverflowError(self);
392 return;
393 }
394
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 if (kIsDebugBuild) {
396 self->AssertThreadSuspensionIsAllowable();
397 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700398 CHECK_STREQ(GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 }
400
401 // Push a transition back into managed code onto the linked list in thread.
402 ManagedStack fragment;
403 self->PushManagedStackFragment(&fragment);
404
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700406 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700407 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700408 if (IsStatic()) {
409 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
410 } else {
411 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
412 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 }
414 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800417 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700418 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800419 LOG(INFO) << StringPrintf("Invoking '%s' quick code=%p", PrettyMethod(this).c_str(),
420 GetEntryPointFromQuickCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700421 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700422
Elliott Hughes956af0f2014-12-11 14:34:28 -0800423 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800424 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
425 DCHECK(!runtime->UseJit());
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700426 CHECK(IsEntrypointInterpreter())
427 << "Don't call compiled code when -Xint " << PrettyMethod(this);
428 }
429
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000430#if defined(__LP64__) || defined(__arm__) || defined(__i386__)
Elliott Hughes956af0f2014-12-11 14:34:28 -0800431 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800432 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800433 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800434 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800435 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800436#else
437 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
438#endif
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200439 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
440 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700441 // exception was thrown to force the activations to be removed from the
442 // stack. Continue execution in the interpreter.
443 self->ClearException();
444 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700445 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700446 self->SetTopOfShadowStack(shadow_frame);
447 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
448 }
449 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800450 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
451 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800452 }
453 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800454 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800455 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800456 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457 }
458 }
459 }
460
461 // Pop transition.
462 self->PopManagedStackFragment(fragment);
463}
464
Ian Rogers1a102182014-12-02 17:49:19 -0800465// Counts the number of references in the parameter list of the corresponding method.
466// Note: Thus does _not_ include "this" for non-static methods.
467static uint32_t GetNumberOfReferenceArgsWithoutReceiver(ArtMethod* method)
468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
469 uint32_t shorty_len;
470 const char* shorty = method->GetShorty(&shorty_len);
471 uint32_t refs = 0;
472 for (uint32_t i = 1; i < shorty_len ; ++i) {
473 if (shorty[i] == 'L') {
474 refs++;
475 }
476 }
477 return refs;
478}
479
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700480QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700481 Runtime* runtime = Runtime::Current();
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200482
483 if (UNLIKELY(IsAbstract())) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700484 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
485 }
Daniel Mihalyie14f2b32014-10-10 18:24:11 +0200486
487 // For Proxy method we add special handling for the direct method case (there is only one
488 // direct method - constructor). Direct method is cloned from original
489 // java.lang.reflect.Proxy class together with code and as a result it is executed as usual
490 // quick compiled method without any stubs. So the frame info should be returned as it is a
491 // quick method not a stub. However, if instrumentation stubs are installed, the
492 // instrumentation->GetQuickCodeFor() returns the artQuickProxyInvokeHandler instead of an
493 // oat code pointer, thus we have to add a special case here.
494 if (UNLIKELY(IsProxyMethod())) {
495 if (IsDirect()) {
496 CHECK(IsConstructor());
497 return GetQuickFrameInfo(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
498 } else {
499 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
500 }
501 }
502
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700503 if (UNLIKELY(IsRuntimeMethod())) {
504 return runtime->GetRuntimeMethodFrameInfo(this);
505 }
506
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800507 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this, sizeof(void*));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700508 ClassLinker* class_linker = runtime->GetClassLinker();
509 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
510 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
511 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
512 DCHECK(!class_linker->IsQuickToInterpreterBridge(entry_point));
513
514 if (class_linker->IsQuickGenericJniStub(entry_point)) {
515 // Generic JNI frame.
516 DCHECK(IsNative());
517 StackHandleScope<1> hs(Thread::Current());
Ian Rogers1a102182014-12-02 17:49:19 -0800518 uint32_t handle_refs = GetNumberOfReferenceArgsWithoutReceiver(this) + 1;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700519 size_t scope_size = HandleScope::SizeOf(handle_refs);
520 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
521
522 // Callee saves + handle scope + method ref + alignment
523 size_t frame_size = RoundUp(callee_info.FrameSizeInBytes() + scope_size
524 - sizeof(void*) // callee-save frame stores a whole method pointer
525 + sizeof(StackReference<mirror::ArtMethod>),
526 kStackAlignment);
527
528 return QuickMethodFrameInfo(frame_size, callee_info.CoreSpillMask(), callee_info.FpSpillMask());
529 }
530
531 const void* code_pointer = EntryPointToCodePointer(entry_point);
532 return GetQuickFrameInfo(code_pointer);
533}
534
535void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700537 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800538 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700539 if (is_fast) {
540 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800542 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543}
544
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700545void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700546 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700548 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549}
550
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551} // namespace mirror
552} // namespace art