blob: d08708f8cdf416779769160b5f344d93c73e3f50 [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"
27#include "object-inl.h"
28#include "object_array.h"
29#include "object_array-inl.h"
30#include "string.h"
31#include "object_utils.h"
32
33namespace art {
34namespace mirror {
35
Jeff Hao6474d192013-03-26 14:08:09 -070036extern "C" void art_portable_invoke_stub(AbstractMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
37extern "C" void art_quick_invoke_stub(AbstractMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Jeff Hao5d917302013-02-27 17:57:33 -080038
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039// TODO: get global references for these
40Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
41Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
42
43InvokeType AbstractMethod::GetInvokeType() const {
44 // TODO: kSuper?
45 if (GetDeclaringClass()->IsInterface()) {
46 return kInterface;
47 } else if (IsStatic()) {
48 return kStatic;
49 } else if (IsDirect()) {
50 return kDirect;
51 } else {
52 return kVirtual;
53 }
54}
55
56void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
57 CHECK(java_lang_reflect_Constructor_ == NULL);
58 CHECK(java_lang_reflect_Constructor != NULL);
59 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
60
61 CHECK(java_lang_reflect_Method_ == NULL);
62 CHECK(java_lang_reflect_Method != NULL);
63 java_lang_reflect_Method_ = java_lang_reflect_Method;
64}
65
66void AbstractMethod::ResetClasses() {
67 CHECK(java_lang_reflect_Constructor_ != NULL);
68 java_lang_reflect_Constructor_ = NULL;
69
70 CHECK(java_lang_reflect_Method_ != NULL);
71 java_lang_reflect_Method_ = NULL;
72}
73
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
75 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
76 new_dex_cache_strings, false);
77}
78
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
80 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
81 new_dex_cache_methods, false);
82}
83
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
85 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
86 new_dex_cache_classes, false);
87}
88
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
90 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
91 new_value, false);
92}
93
94size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
95 CHECK_LE(1, shorty.length());
96 uint32_t num_registers = 0;
97 for (int i = 1; i < shorty.length(); ++i) {
98 char ch = shorty[i];
99 if (ch == 'D' || ch == 'J') {
100 num_registers += 2;
101 } else {
102 num_registers += 1;
103 }
104 }
105 return num_registers;
106}
107
108bool AbstractMethod::IsProxyMethod() const {
109 return GetDeclaringClass()->IsProxyClass();
110}
111
112AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
113 if (IsStatic()) {
114 return NULL;
115 }
116 Class* declaring_class = GetDeclaringClass();
117 Class* super_class = declaring_class->GetSuperClass();
118 uint16_t method_index = GetMethodIndex();
119 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
120 AbstractMethod* result = NULL;
121 // Did this method override a super class method? If so load the result from the super class'
122 // vtable
123 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
124 result = super_class_vtable->Get(method_index);
125 } else {
126 // Method didn't override superclass method so search interfaces
127 if (IsProxyMethod()) {
128 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
129 CHECK_EQ(result,
130 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
131 } else {
132 MethodHelper mh(this);
133 MethodHelper interface_mh;
134 IfTable* iftable = GetDeclaringClass()->GetIfTable();
135 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
136 Class* interface = iftable->GetInterface(i);
137 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
138 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
139 interface_mh.ChangeMethod(interface_method);
140 if (mh.HasSameNameAndSignature(&interface_mh)) {
141 result = interface_method;
142 break;
143 }
144 }
145 }
146 }
147 }
148#ifndef NDEBUG
149 MethodHelper result_mh(result);
150 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
151#endif
152 return result;
153}
154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
157 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158}
159
160// Find the lowest-address native safepoint pc for a given dex pc
161uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800162#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 const uint32_t* mapping_table = GetPcToDexMappingTable();
164 if (mapping_table == NULL) {
165 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
166 return DexFile::kDexNoIndex; // Special no mapping case
167 }
168 size_t mapping_table_length = GetPcToDexMappingTableLength();
169 for (size_t i = 0; i < mapping_table_length; i += 2) {
170 if (mapping_table[i + 1] == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800171 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
172 return mapping_table[i] + reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 }
174 }
175 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
176 << " in " << PrettyMethod(this);
177 return 0;
178#else
179 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
180 return static_cast<uint32_t>(dex_pc);
181#endif
182}
183
184uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800185#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 const uint32_t* mapping_table = GetPcToDexMappingTable();
187 if (mapping_table == NULL) {
188 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
189 return DexFile::kDexNoIndex; // Special no mapping case
190 }
191 size_t mapping_table_length = GetPcToDexMappingTableLength();
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
193 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 for (size_t i = 0; i < mapping_table_length; i += 2) {
195 if (mapping_table[i] == sought_offset) {
196 return mapping_table[i + 1];
197 }
198 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
200 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
201 << ") in " << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 return DexFile::kDexNoIndex;
203#else
204 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
205 return static_cast<uint32_t>(pc);
206#endif
207}
208
209uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
210 const uint32_t* mapping_table = GetDexToPcMappingTable();
211 if (mapping_table == NULL) {
212 DCHECK_EQ(dex_pc, 0U);
213 return 0; // Special no mapping/pc == 0 case
214 }
215 size_t mapping_table_length = GetDexToPcMappingTableLength();
216 for (size_t i = 0; i < mapping_table_length; i += 2) {
217 uint32_t map_offset = mapping_table[i];
218 uint32_t map_dex_offset = mapping_table[i + 1];
219 if (map_dex_offset == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
221 return reinterpret_cast<uintptr_t>(code) + map_offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 }
223 }
224 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
225 << " in " << PrettyMethod(this);
226 return 0;
227}
228
Ian Rogersc449aa82013-07-29 14:35:46 -0700229uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc,
230 bool* has_no_move_exception) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 MethodHelper mh(this);
232 const DexFile::CodeItem* code_item = mh.GetCodeItem();
233 // Iterate over the catch handlers associated with dex_pc
234 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
235 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
236 // Catch all case
237 if (iter_type_idx == DexFile::kDexNoIndex16) {
238 return it.GetHandlerAddress();
239 }
240 // Does this catch exception type apply?
241 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
242 if (iter_exception_type == NULL) {
243 // The verifier should take care of resolving all exception classes early
244 LOG(WARNING) << "Unresolved exception class when finding catch block: "
245 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
246 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogersc449aa82013-07-29 14:35:46 -0700247 uint32_t found_dex_pc = it.GetHandlerAddress();
248 const Instruction* first_catch_instr =
249 Instruction::At(&mh.GetCodeItem()->insns_[found_dex_pc]);
250 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
251 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
253 }
254 // Handler not found
255 return DexFile::kDexNoIndex;
256}
257
Jeff Hao5d917302013-02-27 17:57:33 -0800258void AbstractMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Jeff Hao6474d192013-03-26 14:08:09 -0700259 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 if (kIsDebugBuild) {
261 self->AssertThreadSuspensionIsAllowable();
262 CHECK_EQ(kRunnable, self->GetState());
263 }
264
265 // Push a transition back into managed code onto the linked list in thread.
266 ManagedStack fragment;
267 self->PushManagedStackFragment(&fragment);
268
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700270 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700271 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
273 if (result != NULL) {
274 result->SetJ(0);
275 }
276 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 const bool kLogInvocationStartAndReturn = false;
Jeff Haoaa4a7932013-05-13 11:28:27 -0700278 if (GetEntryPointFromCompiledCode() != NULL) {
Jeff Hao790ad902013-05-22 15:02:08 -0700279 if (kLogInvocationStartAndReturn) {
280 LOG(INFO) << StringPrintf("Invoking '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
281 }
Jeff Hao5d917302013-02-27 17:57:33 -0800282#ifdef ART_USE_PORTABLE_COMPILER
Jeff Hao790ad902013-05-22 15:02:08 -0700283 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800284#else
Jeff Hao790ad902013-05-22 15:02:08 -0700285 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800286#endif
Jeff Hao790ad902013-05-22 15:02:08 -0700287 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException(NULL)) == -1)) {
288 // Unusual case where we were running LLVM generated code and an
289 // exception was thrown to force the activations to be removed from the
290 // stack. Continue execution in the interpreter.
291 self->ClearException();
292 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
293 self->SetTopOfStack(NULL, 0);
294 self->SetTopOfShadowStack(shadow_frame);
295 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
296 }
297 if (kLogInvocationStartAndReturn) {
298 LOG(INFO) << StringPrintf("Returned '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800299 }
300 } else {
301 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
Jeff Haoaa4a7932013-05-13 11:28:27 -0700302 << "' code=" << reinterpret_cast<const void*>(GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800303 if (result != NULL) {
304 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 }
306 }
307 }
308
309 // Pop transition.
310 self->PopManagedStackFragment(fragment);
311}
312
313bool AbstractMethod::IsRegistered() const {
314 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
315 CHECK(native_method != NULL);
Jeff Hao79fe5392013-04-24 18:41:58 -0700316 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 return native_method != jni_stub;
318}
319
320void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
321 DCHECK(Thread::Current() == self);
322 CHECK(IsNative()) << PrettyMethod(this);
323 CHECK(native_method != NULL) << PrettyMethod(this);
324 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
325 SetNativeMethod(native_method);
326 } else {
327 // We've been asked to associate this method with the given native method but are working
328 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
329 // the native method to runtime support and store the target somewhere runtime support will
330 // find it.
Ian Rogersc928de92013-02-27 14:30:44 -0800331#if defined(__arm__) && !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 SetNativeMethod(native_method);
333#else
334 UNIMPLEMENTED(FATAL);
335#endif
Jeff Hao16743632013-05-08 10:59:04 -0700336 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, gc_map_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 reinterpret_cast<const uint8_t*>(native_method), false);
338 }
339}
340
341void AbstractMethod::UnregisterNative(Thread* self) {
342 CHECK(IsNative()) << PrettyMethod(this);
343 // restore stub to lookup native pointer via dlsym
Jeff Hao79fe5392013-04-24 18:41:58 -0700344 RegisterNative(self, GetJniDlsymLookupStub());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345}
346
347void AbstractMethod::SetNativeMethod(const void* native_method) {
348 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
349 native_method, false);
350}
351
352} // namespace mirror
353} // namespace art