blob: c2ab29e0312e63702ee25d0d2fe504af06171d8b [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 Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "gc/card_table-inl.h"
24#include "interpreter/interpreter.h"
25#include "jni_internal.h"
26#include "object-inl.h"
27#include "object_array.h"
28#include "object_array-inl.h"
29#include "string.h"
30#include "object_utils.h"
31
32namespace art {
33namespace mirror {
34
Jeff Hao6474d192013-03-26 14:08:09 -070035extern "C" void art_portable_invoke_stub(AbstractMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
36extern "C" void art_quick_invoke_stub(AbstractMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Jeff Hao5d917302013-02-27 17:57:33 -080037
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038// TODO: get global references for these
39Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
40Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
41
42InvokeType AbstractMethod::GetInvokeType() const {
43 // TODO: kSuper?
44 if (GetDeclaringClass()->IsInterface()) {
45 return kInterface;
46 } else if (IsStatic()) {
47 return kStatic;
48 } else if (IsDirect()) {
49 return kDirect;
50 } else {
51 return kVirtual;
52 }
53}
54
55void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
56 CHECK(java_lang_reflect_Constructor_ == NULL);
57 CHECK(java_lang_reflect_Constructor != NULL);
58 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
59
60 CHECK(java_lang_reflect_Method_ == NULL);
61 CHECK(java_lang_reflect_Method != NULL);
62 java_lang_reflect_Method_ = java_lang_reflect_Method;
63}
64
65void AbstractMethod::ResetClasses() {
66 CHECK(java_lang_reflect_Constructor_ != NULL);
67 java_lang_reflect_Constructor_ = NULL;
68
69 CHECK(java_lang_reflect_Method_ != NULL);
70 java_lang_reflect_Method_ = NULL;
71}
72
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
74 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
75 new_dex_cache_strings, false);
76}
77
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
79 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
80 new_dex_cache_methods, false);
81}
82
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
84 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
85 new_dex_cache_classes, false);
86}
87
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
89 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
90 new_value, false);
91}
92
93size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
94 CHECK_LE(1, shorty.length());
95 uint32_t num_registers = 0;
96 for (int i = 1; i < shorty.length(); ++i) {
97 char ch = shorty[i];
98 if (ch == 'D' || ch == 'J') {
99 num_registers += 2;
100 } else {
101 num_registers += 1;
102 }
103 }
104 return num_registers;
105}
106
107bool AbstractMethod::IsProxyMethod() const {
108 return GetDeclaringClass()->IsProxyClass();
109}
110
111AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
112 if (IsStatic()) {
113 return NULL;
114 }
115 Class* declaring_class = GetDeclaringClass();
116 Class* super_class = declaring_class->GetSuperClass();
117 uint16_t method_index = GetMethodIndex();
118 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
119 AbstractMethod* result = NULL;
120 // Did this method override a super class method? If so load the result from the super class'
121 // vtable
122 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
123 result = super_class_vtable->Get(method_index);
124 } else {
125 // Method didn't override superclass method so search interfaces
126 if (IsProxyMethod()) {
127 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
128 CHECK_EQ(result,
129 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
130 } else {
131 MethodHelper mh(this);
132 MethodHelper interface_mh;
133 IfTable* iftable = GetDeclaringClass()->GetIfTable();
134 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
135 Class* interface = iftable->GetInterface(i);
136 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
137 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
138 interface_mh.ChangeMethod(interface_method);
139 if (mh.HasSameNameAndSignature(&interface_mh)) {
140 result = interface_method;
141 break;
142 }
143 }
144 }
145 }
146 }
147#ifndef NDEBUG
148 MethodHelper result_mh(result);
149 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
150#endif
151 return result;
152}
153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
156 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157}
158
159// Find the lowest-address native safepoint pc for a given dex pc
160uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800161#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 const uint32_t* mapping_table = GetPcToDexMappingTable();
163 if (mapping_table == NULL) {
164 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
165 return DexFile::kDexNoIndex; // Special no mapping case
166 }
167 size_t mapping_table_length = GetPcToDexMappingTableLength();
168 for (size_t i = 0; i < mapping_table_length; i += 2) {
169 if (mapping_table[i + 1] == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
171 return mapping_table[i] + reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 }
173 }
174 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
175 << " in " << PrettyMethod(this);
176 return 0;
177#else
178 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
179 return static_cast<uint32_t>(dex_pc);
180#endif
181}
182
183uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800184#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 const uint32_t* mapping_table = GetPcToDexMappingTable();
186 if (mapping_table == NULL) {
187 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
188 return DexFile::kDexNoIndex; // Special no mapping case
189 }
190 size_t mapping_table_length = GetPcToDexMappingTableLength();
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
192 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 for (size_t i = 0; i < mapping_table_length; i += 2) {
194 if (mapping_table[i] == sought_offset) {
195 return mapping_table[i + 1];
196 }
197 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
199 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
200 << ") in " << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 return DexFile::kDexNoIndex;
202#else
203 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
204 return static_cast<uint32_t>(pc);
205#endif
206}
207
208uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
209 const uint32_t* mapping_table = GetDexToPcMappingTable();
210 if (mapping_table == NULL) {
211 DCHECK_EQ(dex_pc, 0U);
212 return 0; // Special no mapping/pc == 0 case
213 }
214 size_t mapping_table_length = GetDexToPcMappingTableLength();
215 for (size_t i = 0; i < mapping_table_length; i += 2) {
216 uint32_t map_offset = mapping_table[i];
217 uint32_t map_dex_offset = mapping_table[i + 1];
218 if (map_dex_offset == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
220 return reinterpret_cast<uintptr_t>(code) + map_offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 }
222 }
223 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
224 << " in " << PrettyMethod(this);
225 return 0;
226}
227
228uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
229 MethodHelper mh(this);
230 const DexFile::CodeItem* code_item = mh.GetCodeItem();
231 // Iterate over the catch handlers associated with dex_pc
232 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
233 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
234 // Catch all case
235 if (iter_type_idx == DexFile::kDexNoIndex16) {
236 return it.GetHandlerAddress();
237 }
238 // Does this catch exception type apply?
239 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
240 if (iter_exception_type == NULL) {
241 // The verifier should take care of resolving all exception classes early
242 LOG(WARNING) << "Unresolved exception class when finding catch block: "
243 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
244 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
245 return it.GetHandlerAddress();
246 }
247 }
248 // Handler not found
249 return DexFile::kDexNoIndex;
250}
251
Jeff Hao5d917302013-02-27 17:57:33 -0800252void AbstractMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Jeff Hao6474d192013-03-26 14:08:09 -0700253 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 if (kIsDebugBuild) {
255 self->AssertThreadSuspensionIsAllowable();
256 CHECK_EQ(kRunnable, self->GetState());
257 }
258
259 // Push a transition back into managed code onto the linked list in thread.
260 ManagedStack fragment;
261 self->PushManagedStackFragment(&fragment);
262
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700264 // Call the invoke stub, passing everything as arguments.
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 if (UNLIKELY(!runtime->IsStarted())){
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
267 if (result != NULL) {
268 result->SetJ(0);
269 }
270 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 const bool kLogInvocationStartAndReturn = false;
Jeff Haoaa4a7932013-05-13 11:28:27 -0700272 if (GetEntryPointFromCompiledCode() != NULL) {
Jeff Hao790ad902013-05-22 15:02:08 -0700273 if (kLogInvocationStartAndReturn) {
274 LOG(INFO) << StringPrintf("Invoking '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
275 }
Jeff Hao5d917302013-02-27 17:57:33 -0800276#ifdef ART_USE_PORTABLE_COMPILER
Jeff Hao790ad902013-05-22 15:02:08 -0700277 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800278#else
Jeff Hao790ad902013-05-22 15:02:08 -0700279 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800280#endif
Jeff Hao790ad902013-05-22 15:02:08 -0700281 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException(NULL)) == -1)) {
282 // 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) {
292 LOG(INFO) << StringPrintf("Returned '%s' code=%p", PrettyMethod(this).c_str(), GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800293 }
294 } else {
295 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
Jeff Haoaa4a7932013-05-13 11:28:27 -0700296 << "' code=" << reinterpret_cast<const void*>(GetEntryPointFromCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800297 if (result != NULL) {
298 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 }
300 }
301 }
302
303 // Pop transition.
304 self->PopManagedStackFragment(fragment);
305}
306
307bool AbstractMethod::IsRegistered() const {
308 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
309 CHECK(native_method != NULL);
Jeff Hao79fe5392013-04-24 18:41:58 -0700310 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 return native_method != jni_stub;
312}
313
314void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
315 DCHECK(Thread::Current() == self);
316 CHECK(IsNative()) << PrettyMethod(this);
317 CHECK(native_method != NULL) << PrettyMethod(this);
318 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
319 SetNativeMethod(native_method);
320 } else {
321 // We've been asked to associate this method with the given native method but are working
322 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
323 // the native method to runtime support and store the target somewhere runtime support will
324 // find it.
Ian Rogersc928de92013-02-27 14:30:44 -0800325#if defined(__arm__) && !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 SetNativeMethod(native_method);
327#else
328 UNIMPLEMENTED(FATAL);
329#endif
Jeff Hao16743632013-05-08 10:59:04 -0700330 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, gc_map_),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 reinterpret_cast<const uint8_t*>(native_method), false);
332 }
333}
334
335void AbstractMethod::UnregisterNative(Thread* self) {
336 CHECK(IsNative()) << PrettyMethod(this);
337 // restore stub to lookup native pointer via dlsym
Jeff Hao79fe5392013-04-24 18:41:58 -0700338 RegisterNative(self, GetJniDlsymLookupStub());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339}
340
341void AbstractMethod::SetNativeMethod(const void* native_method) {
342 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
343 native_method, false);
344}
345
346} // namespace mirror
347} // namespace art