blob: f74814c406778d19347b66b5dbd2794de79b679f [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
154static const void* GetOatCode(const AbstractMethod* m)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 Runtime* runtime = Runtime::Current();
157 const void* code = m->GetCode();
158 // Peel off any method tracing trampoline.
159 if (runtime->IsMethodTracingActive() && runtime->GetInstrumentation()->GetSavedCodeFromMap(m) != NULL) {
160 code = runtime->GetInstrumentation()->GetSavedCodeFromMap(m);
161 }
162 // Peel off any resolution stub.
163 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
164 code = runtime->GetClassLinker()->GetOatCodeFor(m);
165 }
166 return code;
167}
168
169uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
170 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
171}
172
173// Find the lowest-address native safepoint pc for a given dex pc
174uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800175#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 const uint32_t* mapping_table = GetPcToDexMappingTable();
177 if (mapping_table == NULL) {
178 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
179 return DexFile::kDexNoIndex; // Special no mapping case
180 }
181 size_t mapping_table_length = GetPcToDexMappingTableLength();
182 for (size_t i = 0; i < mapping_table_length; i += 2) {
183 if (mapping_table[i + 1] == dex_pc) {
184 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
185 }
186 }
187 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
188 << " in " << PrettyMethod(this);
189 return 0;
190#else
191 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
192 return static_cast<uint32_t>(dex_pc);
193#endif
194}
195
196uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800197#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 const uint32_t* mapping_table = GetPcToDexMappingTable();
199 if (mapping_table == NULL) {
200 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
201 return DexFile::kDexNoIndex; // Special no mapping case
202 }
203 size_t mapping_table_length = GetPcToDexMappingTableLength();
204 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
205 for (size_t i = 0; i < mapping_table_length; i += 2) {
206 if (mapping_table[i] == sought_offset) {
207 return mapping_table[i + 1];
208 }
209 }
210 LOG(ERROR) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
211 << "(PC " << reinterpret_cast<void*>(pc) << ") in " << PrettyMethod(this);
212 return DexFile::kDexNoIndex;
213#else
214 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
215 return static_cast<uint32_t>(pc);
216#endif
217}
218
219uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
220 const uint32_t* mapping_table = GetDexToPcMappingTable();
221 if (mapping_table == NULL) {
222 DCHECK_EQ(dex_pc, 0U);
223 return 0; // Special no mapping/pc == 0 case
224 }
225 size_t mapping_table_length = GetDexToPcMappingTableLength();
226 for (size_t i = 0; i < mapping_table_length; i += 2) {
227 uint32_t map_offset = mapping_table[i];
228 uint32_t map_dex_offset = mapping_table[i + 1];
229 if (map_dex_offset == dex_pc) {
230 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
231 }
232 }
233 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
234 << " in " << PrettyMethod(this);
235 return 0;
236}
237
238uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
239 MethodHelper mh(this);
240 const DexFile::CodeItem* code_item = mh.GetCodeItem();
241 // Iterate over the catch handlers associated with dex_pc
242 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
243 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
244 // Catch all case
245 if (iter_type_idx == DexFile::kDexNoIndex16) {
246 return it.GetHandlerAddress();
247 }
248 // Does this catch exception type apply?
249 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
250 if (iter_exception_type == NULL) {
251 // The verifier should take care of resolving all exception classes early
252 LOG(WARNING) << "Unresolved exception class when finding catch block: "
253 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
254 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
255 return it.GetHandlerAddress();
256 }
257 }
258 // Handler not found
259 return DexFile::kDexNoIndex;
260}
261
Jeff Hao5d917302013-02-27 17:57:33 -0800262void AbstractMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Jeff Hao6474d192013-03-26 14:08:09 -0700263 char result_type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 if (kIsDebugBuild) {
265 self->AssertThreadSuspensionIsAllowable();
266 CHECK_EQ(kRunnable, self->GetState());
267 }
268
269 // Push a transition back into managed code onto the linked list in thread.
270 ManagedStack fragment;
271 self->PushManagedStackFragment(&fragment);
272
Jeff Hao74180ca2013-03-27 15:29:11 -0700273 // Call the invoke stub, passing everything as arguments.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 if (UNLIKELY(!Runtime::Current()->IsStarted())){
275 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
276 if (result != NULL) {
277 result->SetJ(0);
278 }
279 } else {
280 bool interpret = self->ReadFlag(kEnterInterpreter) && !IsNative() && !IsProxyMethod();
281 const bool kLogInvocationStartAndReturn = false;
Jeff Hao5d917302013-02-27 17:57:33 -0800282 if (GetCode() != NULL) {
283 if (!interpret) {
284 if (kLogInvocationStartAndReturn) {
Jeff Hao74180ca2013-03-27 15:29:11 -0700285 LOG(INFO) << StringPrintf("Invoking '%s' code=%p", PrettyMethod(this).c_str(), GetCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800286 }
Jeff Hao5d917302013-02-27 17:57:33 -0800287#ifdef ART_USE_PORTABLE_COMPILER
Jeff Hao6474d192013-03-26 14:08:09 -0700288 (*art_portable_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800289#else
Jeff Hao6474d192013-03-26 14:08:09 -0700290 (*art_quick_invoke_stub)(this, args, args_size, self, result, result_type);
Jeff Hao5d917302013-02-27 17:57:33 -0800291#endif
292 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException()) == -1)) {
293 // Unusual case where we were running LLVM generated code and an
294 // exception was thrown to force the activations to be removed from the
295 // stack. Continue execution in the interpreter.
296 JValue value;
297 self->ClearException();
298 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(&value);
299 self->SetTopOfShadowStack(shadow_frame);
300 interpreter::EnterInterpreterFromLLVM(self, shadow_frame, result);
301 }
302 if (kLogInvocationStartAndReturn) {
Jeff Hao74180ca2013-03-27 15:29:11 -0700303 LOG(INFO) << StringPrintf("Returned '%s' code=%p", PrettyMethod(this).c_str(), GetCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800304 }
305 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 if (kLogInvocationStartAndReturn) {
307 LOG(INFO) << "Interpreting " << PrettyMethod(this) << "'";
308 }
Jeff Hao5d917302013-02-27 17:57:33 -0800309 if (this->IsStatic()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700310 art::interpreter::EnterInterpreterFromInvoke(self, this, NULL, args, result);
Jeff Hao5d917302013-02-27 17:57:33 -0800311 } else {
312 Object* receiver = reinterpret_cast<Object*>(args[0]);
Jeff Hao6474d192013-03-26 14:08:09 -0700313 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Jeff Hao5d917302013-02-27 17:57:33 -0800314 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 if (kLogInvocationStartAndReturn) {
316 LOG(INFO) << "Returned '" << PrettyMethod(this) << "'";
317 }
Jeff Hao5d917302013-02-27 17:57:33 -0800318 }
319 } else {
320 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
Jeff Hao74180ca2013-03-27 15:29:11 -0700321 << "' code=" << reinterpret_cast<const void*>(GetCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800322 if (result != NULL) {
323 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 }
325 }
326 }
327
328 // Pop transition.
329 self->PopManagedStackFragment(fragment);
330}
331
332bool AbstractMethod::IsRegistered() const {
333 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
334 CHECK(native_method != NULL);
335 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
336 return native_method != jni_stub;
337}
338
339void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
340 DCHECK(Thread::Current() == self);
341 CHECK(IsNative()) << PrettyMethod(this);
342 CHECK(native_method != NULL) << PrettyMethod(this);
343 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
344 SetNativeMethod(native_method);
345 } else {
346 // We've been asked to associate this method with the given native method but are working
347 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
348 // the native method to runtime support and store the target somewhere runtime support will
349 // find it.
Ian Rogersc928de92013-02-27 14:30:44 -0800350#if defined(__arm__) && !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 SetNativeMethod(native_method);
352#else
353 UNIMPLEMENTED(FATAL);
354#endif
355 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
356 reinterpret_cast<const uint8_t*>(native_method), false);
357 }
358}
359
360void AbstractMethod::UnregisterNative(Thread* self) {
361 CHECK(IsNative()) << PrettyMethod(this);
362 // restore stub to lookup native pointer via dlsym
363 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
364}
365
366void AbstractMethod::SetNativeMethod(const void* native_method) {
367 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
368 native_method, false);
369}
370
371} // namespace mirror
372} // namespace art