blob: 575ea03519004b3c909d7b5ee4864dc12bfdc6b8 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080050InvokeType ArtMethod::GetInvokeType() {
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103bool ArtMethod::IsProxyMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 return GetDeclaringClass()->IsProxyClass();
105}
106
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107ArtMethod* ArtMethod::FindOverriddenMethod() {
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800150uintptr_t ArtMethod::NativePcOffset(const uintptr_t pc) {
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155uint32_t ArtMethod::ToDexPc(const uintptr_t pc) {
156 if (IsPortableCompiled()) {
157 // Portable doesn't use the machine pc, we just use dex pc instead.
158 return static_cast<uint32_t>(pc);
159 }
Ian Rogers1809a722013-08-09 22:05:32 -0700160 MappingTable table(GetMappingTable());
161 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
163 return DexFile::kDexNoIndex; // Special no mapping case
164 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
166 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers1809a722013-08-09 22:05:32 -0700167 // Assume the caller wants a pc-to-dex mapping so check here first.
168 typedef MappingTable::PcToDexIterator It;
169 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
170 if (cur.NativePcOffset() == sought_offset) {
171 return cur.DexPc();
172 }
173 }
174 // Now check dex-to-pc mappings.
175 typedef MappingTable::DexToPcIterator It2;
176 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
177 if (cur.NativePcOffset() == sought_offset) {
178 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 }
180 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800182 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
183 << ") in " << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185}
186
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) {
Ian Rogers1809a722013-08-09 22:05:32 -0700188 MappingTable table(GetMappingTable());
189 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 DCHECK_EQ(dex_pc, 0U);
191 return 0; // Special no mapping/pc == 0 case
192 }
Ian Rogers1809a722013-08-09 22:05:32 -0700193 // Assume the caller wants a dex-to-pc mapping so check here first.
194 typedef MappingTable::DexToPcIterator It;
195 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
196 if (cur.DexPc() == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
Ian Rogers1809a722013-08-09 22:05:32 -0700198 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 }
200 }
Ian Rogers1809a722013-08-09 22:05:32 -0700201 // Now check pc-to-dex mappings.
202 typedef MappingTable::PcToDexIterator It2;
203 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
204 if (cur.DexPc() == dex_pc) {
205 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
206 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
207 }
208 }
209 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 << " in " << PrettyMethod(this);
211 return 0;
212}
213
Brian Carlstromea46f952013-07-30 01:26:50 -0700214uint32_t ArtMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 bool* has_no_move_exception) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 MethodHelper mh(this);
217 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700218 // Default to handler not found.
219 uint32_t found_dex_pc = DexFile::kDexNoIndex;
220 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
222 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
223 // Catch all case
224 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700225 found_dex_pc = it.GetHandlerAddress();
226 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 }
228 // Does this catch exception type apply?
229 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
230 if (iter_exception_type == NULL) {
231 // The verifier should take care of resolving all exception classes early
232 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700233 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700235 found_dex_pc = it.GetHandlerAddress();
236 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 }
238 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700239 if (found_dex_pc != DexFile::kDexNoIndex) {
240 const Instruction* first_catch_instr =
241 Instruction::At(&mh.GetCodeItem()->insns_[found_dex_pc]);
242 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
243 }
244 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245}
246
Brian Carlstromea46f952013-07-30 01:26:50 -0700247void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
248 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 if (kIsDebugBuild) {
250 self->AssertThreadSuspensionIsAllowable();
251 CHECK_EQ(kRunnable, self->GetState());
252 }
253
254 // Push a transition back into managed code onto the linked list in thread.
255 ManagedStack fragment;
256 self->PushManagedStackFragment(&fragment);
257
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700259 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700260 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
262 if (result != NULL) {
263 result->SetJ(0);
264 }
265 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
268 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
269 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700270 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
272 have_quick_code ? "quick" : "portable",
273 have_quick_code ? GetEntryPointFromQuickCompiledCode()
274 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700275 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800276 if (!IsPortableCompiled()) {
277 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
278 } else {
279 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
280 }
281 if (UNLIKELY(reinterpret_cast<intptr_t>(self->GetException(NULL)) == -1)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700282 // Unusual case where we were running LLVM generated code and an
283 // exception was thrown to force the activations to be removed from the
284 // stack. Continue execution in the interpreter.
285 self->ClearException();
286 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
287 self->SetTopOfStack(NULL, 0);
288 self->SetTopOfShadowStack(shadow_frame);
289 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
290 }
291 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
293 have_quick_code ? "quick" : "portable",
294 have_quick_code ? GetEntryPointFromQuickCompiledCode()
295 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800296 }
297 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800299 if (result != NULL) {
300 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 }
302 }
303 }
304
305 // Pop transition.
306 self->PopManagedStackFragment(fragment);
307}
308
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309bool ArtMethod::IsRegistered() {
310 void* native_method =
311 GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_), false);
312 CHECK(native_method != nullptr);
Jeff Hao79fe5392013-04-24 18:41:58 -0700313 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 return native_method != jni_stub;
315}
316
Ian Rogers848871b2013-08-05 10:56:33 -0700317extern "C" void art_work_around_app_jni_bugs(JNIEnv*, jobject);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700318void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 DCHECK(Thread::Current() == self);
320 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700321 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 CHECK(native_method != NULL) << PrettyMethod(this);
323 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700324 if (is_fast) {
325 SetAccessFlags(GetAccessFlags() | kAccFastNative);
326 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 SetNativeMethod(native_method);
328 } else {
329 // We've been asked to associate this method with the given native method but are working
330 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
331 // the native method to runtime support and store the target somewhere runtime support will
332 // find it.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333#if defined(__i386__) || defined(__x86_64__)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 UNIMPLEMENTED(FATAL);
Ian Rogers848871b2013-08-05 10:56:33 -0700335#else
336 SetNativeMethod(reinterpret_cast<void*>(art_work_around_app_jni_bugs));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337#endif
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, gc_map_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 reinterpret_cast<const uint8_t*>(native_method), false);
340 }
341}
342
Brian Carlstromea46f952013-07-30 01:26:50 -0700343void ArtMethod::UnregisterNative(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700344 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 // restore stub to lookup native pointer via dlsym
Ian Rogers1eb512d2013-10-18 15:42:20 -0700346 RegisterNative(self, GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347}
348
Brian Carlstromea46f952013-07-30 01:26:50 -0700349void ArtMethod::SetNativeMethod(const void* native_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 native_method, false);
352}
353
354} // namespace mirror
355} // namespace art