blob: b3db5c27212d8b106481719b4bc08421d020f94d [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
17#include "abstract_method.h"
18
19#include "abstract_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
Jeff Hao6474d192013-03-26 14:08:09 -070037extern "C" void art_portable_invoke_stub(AbstractMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
38extern "C" void art_quick_invoke_stub(AbstractMethod*, 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
41Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
42Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
43
44InvokeType AbstractMethod::GetInvokeType() const {
45 // TODO: kSuper?
46 if (GetDeclaringClass()->IsInterface()) {
47 return kInterface;
48 } else if (IsStatic()) {
49 return kStatic;
50 } else if (IsDirect()) {
51 return kDirect;
52 } else {
53 return kVirtual;
54 }
55}
56
57void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
58 CHECK(java_lang_reflect_Constructor_ == NULL);
59 CHECK(java_lang_reflect_Constructor != NULL);
60 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
61
62 CHECK(java_lang_reflect_Method_ == NULL);
63 CHECK(java_lang_reflect_Method != NULL);
64 java_lang_reflect_Method_ = java_lang_reflect_Method;
65}
66
67void AbstractMethod::ResetClasses() {
68 CHECK(java_lang_reflect_Constructor_ != NULL);
69 java_lang_reflect_Constructor_ = NULL;
70
71 CHECK(java_lang_reflect_Method_ != NULL);
72 java_lang_reflect_Method_ = NULL;
73}
74
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
76 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
77 new_dex_cache_strings, false);
78}
79
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
81 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
82 new_dex_cache_methods, false);
83}
84
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
86 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
87 new_dex_cache_classes, false);
88}
89
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
91 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
92 new_value, false);
93}
94
95size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
96 CHECK_LE(1, shorty.length());
97 uint32_t num_registers = 0;
98 for (int i = 1; i < shorty.length(); ++i) {
99 char ch = shorty[i];
100 if (ch == 'D' || ch == 'J') {
101 num_registers += 2;
102 } else {
103 num_registers += 1;
104 }
105 }
106 return num_registers;
107}
108
109bool AbstractMethod::IsProxyMethod() const {
110 return GetDeclaringClass()->IsProxyClass();
111}
112
113AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
114 if (IsStatic()) {
115 return NULL;
116 }
117 Class* declaring_class = GetDeclaringClass();
118 Class* super_class = declaring_class->GetSuperClass();
119 uint16_t method_index = GetMethodIndex();
120 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
121 AbstractMethod* result = NULL;
122 // Did this method override a super class method? If so load the result from the super class'
123 // vtable
124 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
125 result = super_class_vtable->Get(method_index);
126 } else {
127 // Method didn't override superclass method so search interfaces
128 if (IsProxyMethod()) {
129 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
130 CHECK_EQ(result,
131 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
132 } else {
133 MethodHelper mh(this);
134 MethodHelper interface_mh;
135 IfTable* iftable = GetDeclaringClass()->GetIfTable();
136 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
137 Class* interface = iftable->GetInterface(i);
138 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
139 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
140 interface_mh.ChangeMethod(interface_method);
141 if (mh.HasSameNameAndSignature(&interface_mh)) {
142 result = interface_method;
143 break;
144 }
145 }
146 }
147 }
148 }
149#ifndef NDEBUG
150 MethodHelper result_mh(result);
151 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
152#endif
153 return result;
154}
155
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800157 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
158 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159}
160
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800162#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers1809a722013-08-09 22:05:32 -0700163 MappingTable table(GetMappingTable());
164 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
166 return DexFile::kDexNoIndex; // Special no mapping case
167 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
169 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers1809a722013-08-09 22:05:32 -0700170 // Assume the caller wants a pc-to-dex mapping so check here first.
171 typedef MappingTable::PcToDexIterator It;
172 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
173 if (cur.NativePcOffset() == sought_offset) {
174 return cur.DexPc();
175 }
176 }
177 // Now check dex-to-pc mappings.
178 typedef MappingTable::DexToPcIterator It2;
179 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
180 if (cur.NativePcOffset() == sought_offset) {
181 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 }
183 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
185 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
186 << ") in " << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 return DexFile::kDexNoIndex;
188#else
189 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
190 return static_cast<uint32_t>(pc);
191#endif
192}
193
194uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Ian Rogers1809a722013-08-09 22:05:32 -0700195 MappingTable table(GetMappingTable());
196 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 DCHECK_EQ(dex_pc, 0U);
198 return 0; // Special no mapping/pc == 0 case
199 }
Ian Rogers1809a722013-08-09 22:05:32 -0700200 // Assume the caller wants a dex-to-pc mapping so check here first.
201 typedef MappingTable::DexToPcIterator It;
202 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
203 if (cur.DexPc() == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800204 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
Ian Rogers1809a722013-08-09 22:05:32 -0700205 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 }
207 }
Ian Rogers1809a722013-08-09 22:05:32 -0700208 // Now check pc-to-dex mappings.
209 typedef MappingTable::PcToDexIterator It2;
210 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
211 if (cur.DexPc() == dex_pc) {
212 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
213 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
214 }
215 }
216 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 << " in " << PrettyMethod(this);
218 return 0;
219}
220
Ian Rogersc449aa82013-07-29 14:35:46 -0700221uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc,
222 bool* has_no_move_exception) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 MethodHelper mh(this);
224 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700225 // Default to handler not found.
226 uint32_t found_dex_pc = DexFile::kDexNoIndex;
227 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
229 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
230 // Catch all case
231 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700232 found_dex_pc = it.GetHandlerAddress();
233 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 }
235 // Does this catch exception type apply?
236 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
237 if (iter_exception_type == NULL) {
238 // The verifier should take care of resolving all exception classes early
239 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700240 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700242 found_dex_pc = it.GetHandlerAddress();
243 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 }
245 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700246 if (found_dex_pc != DexFile::kDexNoIndex) {
247 const Instruction* first_catch_instr =
248 Instruction::At(&mh.GetCodeItem()->insns_[found_dex_pc]);
249 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
250 }
251 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252}
253
Jeff Hao5d917302013-02-27 17:57:33 -0800254void AbstractMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Jeff Hao6474d192013-03-26 14:08:09 -0700255 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 if (kIsDebugBuild) {
257 self->AssertThreadSuspensionIsAllowable();
258 CHECK_EQ(kRunnable, self->GetState());
259 }
260
261 // Push a transition back into managed code onto the linked list in thread.
262 ManagedStack fragment;
263 self->PushManagedStackFragment(&fragment);
264
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700266 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700267 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
269 if (result != NULL) {
270 result->SetJ(0);
271 }
272 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 const bool kLogInvocationStartAndReturn = false;
Jeff Haoaa4a7932013-05-13 11:28:27 -0700274 if (GetEntryPointFromCompiledCode() != NULL) {
Jeff Hao790ad902013-05-22 15:02:08 -0700275 if (kLogInvocationStartAndReturn) {
276 LOG(INFO) << StringPrintf("Invoking '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
277 }
Jeff Hao5d917302013-02-27 17:57:33 -0800278#ifdef ART_USE_PORTABLE_COMPILER
Jeff Hao790ad902013-05-22 15:02:08 -0700279 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800280#else
Jeff Hao790ad902013-05-22 15:02:08 -0700281 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800282#endif
Jeff Hao790ad902013-05-22 15:02:08 -0700283 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException(NULL)) == -1)) {
284 // Unusual case where we were running LLVM generated code and an
285 // exception was thrown to force the activations to be removed from the
286 // stack. Continue execution in the interpreter.
287 self->ClearException();
288 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
289 self->SetTopOfStack(NULL, 0);
290 self->SetTopOfShadowStack(shadow_frame);
291 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
292 }
293 if (kLogInvocationStartAndReturn) {
294 LOG(INFO) << StringPrintf("Returned '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800295 }
296 } else {
297 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
Jeff Haoaa4a7932013-05-13 11:28:27 -0700298 << "' code=" << reinterpret_cast<const void*>(GetEntryPointFromCompiledCode());
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
309bool AbstractMethod::IsRegistered() const {
310 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
311 CHECK(native_method != NULL);
Jeff Hao79fe5392013-04-24 18:41:58 -0700312 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 return native_method != jni_stub;
314}
315
Ian Rogers848871b2013-08-05 10:56:33 -0700316extern "C" void art_work_around_app_jni_bugs(JNIEnv*, jobject);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
318 DCHECK(Thread::Current() == self);
319 CHECK(IsNative()) << PrettyMethod(this);
320 CHECK(native_method != NULL) << PrettyMethod(this);
321 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
322 SetNativeMethod(native_method);
323 } else {
324 // We've been asked to associate this method with the given native method but are working
325 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
326 // the native method to runtime support and store the target somewhere runtime support will
327 // find it.
Ian Rogers848871b2013-08-05 10:56:33 -0700328#if defined(__i386__)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 UNIMPLEMENTED(FATAL);
Ian Rogers848871b2013-08-05 10:56:33 -0700330#else
331 SetNativeMethod(reinterpret_cast<void*>(art_work_around_app_jni_bugs));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332#endif
Jeff Hao16743632013-05-08 10:59:04 -0700333 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, gc_map_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 reinterpret_cast<const uint8_t*>(native_method), false);
335 }
336}
337
338void AbstractMethod::UnregisterNative(Thread* self) {
339 CHECK(IsNative()) << PrettyMethod(this);
340 // restore stub to lookup native pointer via dlsym
Jeff Hao79fe5392013-04-24 18:41:58 -0700341 RegisterNative(self, GetJniDlsymLookupStub());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342}
343
344void AbstractMethod::SetNativeMethod(const void* native_method) {
345 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
346 native_method, false);
347}
348
349} // namespace mirror
350} // namespace art