blob: f4a076cf13ae21e1d52a3f70f10129e13aa27f74 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "base/stringpiece.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "class-inl.h"
22#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070023#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "interpreter/interpreter.h"
26#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070027#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "object-inl.h"
29#include "object_array.h"
30#include "object_array-inl.h"
31#include "string.h"
32#include "object_utils.h"
33
34namespace art {
35namespace mirror {
36
Brian Carlstromea46f952013-07-30 01:26:50 -070037extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
38extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Jeff Hao5d917302013-02-27 17:57:33 -080039
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040// TODO: get global references for these
Brian Carlstromea46f952013-07-30 01:26:50 -070041Class* ArtMethod::java_lang_reflect_ArtMethod_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042
Mathieu Chartierc528dba2013-11-26 12:00:11 -080043void ArtMethod::VisitRoots(RootVisitor* visitor, void* arg) {
44 if (java_lang_reflect_ArtMethod_ != nullptr) {
45 java_lang_reflect_ArtMethod_ = down_cast<mirror::Class*>(
46 visitor(java_lang_reflect_ArtMethod_, arg));
47 }
48}
49
Brian Carlstromea46f952013-07-30 01:26:50 -070050InvokeType ArtMethod::GetInvokeType() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 // TODO: kSuper?
52 if (GetDeclaringClass()->IsInterface()) {
53 return kInterface;
54 } else if (IsStatic()) {
55 return kStatic;
56 } else if (IsDirect()) {
57 return kDirect;
58 } else {
59 return kVirtual;
60 }
61}
62
Brian Carlstromea46f952013-07-30 01:26:50 -070063void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
64 CHECK(java_lang_reflect_ArtMethod_ == NULL);
65 CHECK(java_lang_reflect_ArtMethod != NULL);
66 java_lang_reflect_ArtMethod_ = java_lang_reflect_ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067}
68
Brian Carlstromea46f952013-07-30 01:26:50 -070069void ArtMethod::ResetClass() {
70 CHECK(java_lang_reflect_ArtMethod_ != NULL);
71 java_lang_reflect_ArtMethod_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Brian Carlstromea46f952013-07-30 01:26:50 -070074void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
75 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 new_dex_cache_strings, false);
77}
78
Brian Carlstromea46f952013-07-30 01:26:50 -070079void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) {
80 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 new_dex_cache_methods, false);
82}
83
Brian Carlstromea46f952013-07-30 01:26:50 -070084void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
85 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 new_dex_cache_classes, false);
87}
88
Brian Carlstromea46f952013-07-30 01:26:50 -070089size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 CHECK_LE(1, shorty.length());
91 uint32_t num_registers = 0;
92 for (int i = 1; i < shorty.length(); ++i) {
93 char ch = shorty[i];
94 if (ch == 'D' || ch == 'J') {
95 num_registers += 2;
96 } else {
97 num_registers += 1;
98 }
99 }
100 return num_registers;
101}
102
Brian Carlstromea46f952013-07-30 01:26:50 -0700103bool ArtMethod::IsProxyMethod() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 return GetDeclaringClass()->IsProxyClass();
105}
106
Brian Carlstromea46f952013-07-30 01:26:50 -0700107ArtMethod* ArtMethod::FindOverriddenMethod() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 if (IsStatic()) {
109 return NULL;
110 }
111 Class* declaring_class = GetDeclaringClass();
112 Class* super_class = declaring_class->GetSuperClass();
113 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700114 ObjectArray<ArtMethod>* super_class_vtable = super_class->GetVTable();
115 ArtMethod* result = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 // Did this method override a super class method? If so load the result from the super class'
117 // vtable
118 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
119 result = super_class_vtable->Get(method_index);
120 } else {
121 // Method didn't override superclass method so search interfaces
122 if (IsProxyMethod()) {
123 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
124 CHECK_EQ(result,
125 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
126 } else {
127 MethodHelper mh(this);
128 MethodHelper interface_mh;
129 IfTable* iftable = GetDeclaringClass()->GetIfTable();
130 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
131 Class* interface = iftable->GetInterface(i);
132 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700133 ArtMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 interface_mh.ChangeMethod(interface_method);
135 if (mh.HasSameNameAndSignature(&interface_mh)) {
136 result = interface_method;
137 break;
138 }
139 }
140 }
141 }
142 }
143#ifndef NDEBUG
144 MethodHelper result_mh(result);
145 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
146#endif
147 return result;
148}
149
Brian Carlstromea46f952013-07-30 01:26:50 -0700150uintptr_t ArtMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
152 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153}
154
Brian Carlstromea46f952013-07-30 01:26:50 -0700155uint32_t ArtMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800156#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers1809a722013-08-09 22:05:32 -0700157 MappingTable table(GetMappingTable());
158 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
160 return DexFile::kDexNoIndex; // Special no mapping case
161 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
163 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers1809a722013-08-09 22:05:32 -0700164 // Assume the caller wants a pc-to-dex mapping so check here first.
165 typedef MappingTable::PcToDexIterator It;
166 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
167 if (cur.NativePcOffset() == sought_offset) {
168 return cur.DexPc();
169 }
170 }
171 // Now check dex-to-pc mappings.
172 typedef MappingTable::DexToPcIterator It2;
173 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
174 if (cur.NativePcOffset() == sought_offset) {
175 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 }
177 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
179 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
180 << ") in " << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 return DexFile::kDexNoIndex;
182#else
183 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
184 return static_cast<uint32_t>(pc);
185#endif
186}
187
Brian Carlstromea46f952013-07-30 01:26:50 -0700188uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) const {
Ian Rogers1809a722013-08-09 22:05:32 -0700189 MappingTable table(GetMappingTable());
190 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 DCHECK_EQ(dex_pc, 0U);
192 return 0; // Special no mapping/pc == 0 case
193 }
Ian Rogers1809a722013-08-09 22:05:32 -0700194 // Assume the caller wants a dex-to-pc mapping so check here first.
195 typedef MappingTable::DexToPcIterator It;
196 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
197 if (cur.DexPc() == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
Ian Rogers1809a722013-08-09 22:05:32 -0700199 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 }
201 }
Ian Rogers1809a722013-08-09 22:05:32 -0700202 // Now check pc-to-dex mappings.
203 typedef MappingTable::PcToDexIterator It2;
204 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
205 if (cur.DexPc() == dex_pc) {
206 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
207 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
208 }
209 }
210 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 << " in " << PrettyMethod(this);
212 return 0;
213}
214
Brian Carlstromea46f952013-07-30 01:26:50 -0700215uint32_t ArtMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc,
216 bool* has_no_move_exception) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 MethodHelper mh(this);
218 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700219 // Default to handler not found.
220 uint32_t found_dex_pc = DexFile::kDexNoIndex;
221 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
223 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
224 // Catch all case
225 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700226 found_dex_pc = it.GetHandlerAddress();
227 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 }
229 // Does this catch exception type apply?
230 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
231 if (iter_exception_type == NULL) {
232 // The verifier should take care of resolving all exception classes early
233 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700234 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700236 found_dex_pc = it.GetHandlerAddress();
237 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 }
239 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700240 if (found_dex_pc != DexFile::kDexNoIndex) {
241 const Instruction* first_catch_instr =
242 Instruction::At(&mh.GetCodeItem()->insns_[found_dex_pc]);
243 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
244 }
245 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246}
247
Brian Carlstromea46f952013-07-30 01:26:50 -0700248void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
249 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 if (kIsDebugBuild) {
251 self->AssertThreadSuspensionIsAllowable();
252 CHECK_EQ(kRunnable, self->GetState());
253 }
254
255 // Push a transition back into managed code onto the linked list in thread.
256 ManagedStack fragment;
257 self->PushManagedStackFragment(&fragment);
258
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700260 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700261 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
263 if (result != NULL) {
264 result->SetJ(0);
265 }
266 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 const bool kLogInvocationStartAndReturn = false;
Jeff Haoaa4a7932013-05-13 11:28:27 -0700268 if (GetEntryPointFromCompiledCode() != NULL) {
Jeff Hao790ad902013-05-22 15:02:08 -0700269 if (kLogInvocationStartAndReturn) {
270 LOG(INFO) << StringPrintf("Invoking '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
271 }
Jeff Hao5d917302013-02-27 17:57:33 -0800272#ifdef ART_USE_PORTABLE_COMPILER
Jeff Hao790ad902013-05-22 15:02:08 -0700273 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800274#else
Jeff Hao790ad902013-05-22 15:02:08 -0700275 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800276#endif
Jeff Hao790ad902013-05-22 15:02:08 -0700277 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException(NULL)) == -1)) {
278 // Unusual case where we were running LLVM generated code and an
279 // exception was thrown to force the activations to be removed from the
280 // stack. Continue execution in the interpreter.
281 self->ClearException();
282 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
283 self->SetTopOfStack(NULL, 0);
284 self->SetTopOfShadowStack(shadow_frame);
285 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
286 }
287 if (kLogInvocationStartAndReturn) {
288 LOG(INFO) << StringPrintf("Returned '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800289 }
290 } else {
291 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
Jeff Haoaa4a7932013-05-13 11:28:27 -0700292 << "' code=" << reinterpret_cast<const void*>(GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800293 if (result != NULL) {
294 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 }
296 }
297 }
298
299 // Pop transition.
300 self->PopManagedStackFragment(fragment);
301}
302
Brian Carlstromea46f952013-07-30 01:26:50 -0700303bool ArtMethod::IsRegistered() const {
304 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, native_method_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 CHECK(native_method != NULL);
Jeff Hao79fe5392013-04-24 18:41:58 -0700306 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 return native_method != jni_stub;
308}
309
Ian Rogers848871b2013-08-05 10:56:33 -0700310extern "C" void art_work_around_app_jni_bugs(JNIEnv*, jobject);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700311void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 DCHECK(Thread::Current() == self);
313 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700314 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 CHECK(native_method != NULL) << PrettyMethod(this);
316 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700317 if (is_fast) {
318 SetAccessFlags(GetAccessFlags() | kAccFastNative);
319 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 SetNativeMethod(native_method);
321 } else {
322 // We've been asked to associate this method with the given native method but are working
323 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
324 // the native method to runtime support and store the target somewhere runtime support will
325 // find it.
Ian Rogers848871b2013-08-05 10:56:33 -0700326#if defined(__i386__)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 UNIMPLEMENTED(FATAL);
Ian Rogers848871b2013-08-05 10:56:33 -0700328#else
329 SetNativeMethod(reinterpret_cast<void*>(art_work_around_app_jni_bugs));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330#endif
Brian Carlstromea46f952013-07-30 01:26:50 -0700331 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, gc_map_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 reinterpret_cast<const uint8_t*>(native_method), false);
333 }
334}
335
Brian Carlstromea46f952013-07-30 01:26:50 -0700336void ArtMethod::UnregisterNative(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700337 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 // restore stub to lookup native pointer via dlsym
Ian Rogers1eb512d2013-10-18 15:42:20 -0700339 RegisterNative(self, GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340}
341
Brian Carlstromea46f952013-07-30 01:26:50 -0700342void ArtMethod::SetNativeMethod(const void* native_method) {
343 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, native_method_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 native_method, false);
345}
346
347} // namespace mirror
348} // namespace art