blob: c0c9a5558c07266f8229c06426b004c9b7dfd06a [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 Hao5d917302013-02-27 17:57:33 -080035extern "C" void art_quick_invoke_stub(AbstractMethod*, uint32_t*, uint32_t,
36 Thread*, JValue*, JValue*);
37
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,
263 JValue* float_result) {
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
273 // Call the invoke stub associated with the method.
274 // Pass everything as arguments.
275 AbstractMethod::InvokeStub* stub = GetInvokeStub();
276
277 if (UNLIKELY(!Runtime::Current()->IsStarted())){
278 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
279 if (result != NULL) {
280 result->SetJ(0);
Jeff Hao5d917302013-02-27 17:57:33 -0800281 float_result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 }
283 } else {
284 bool interpret = self->ReadFlag(kEnterInterpreter) && !IsNative() && !IsProxyMethod();
285 const bool kLogInvocationStartAndReturn = false;
Jeff Hao5d917302013-02-27 17:57:33 -0800286 if (GetCode() != NULL) {
287 if (!interpret) {
288 if (kLogInvocationStartAndReturn) {
289 LOG(INFO) << StringPrintf("Invoking '%s' code=%p stub=%p",
290 PrettyMethod(this).c_str(), GetCode(), stub);
291 }
292 // TODO: Temporary to keep portable working while stubs are removed from quick.
293#ifdef ART_USE_PORTABLE_COMPILER
294 MethodHelper mh(this);
295 const char* shorty = mh.GetShorty();
296 uint32_t shorty_len = mh.GetShortyLength();
297 UniquePtr<JValue[]> jvalue_args(new JValue[shorty_len - 1]);
298 Object* receiver = NULL;
299 uint32_t* ptr = args;
300 if (!this->IsStatic()) {
301 receiver = reinterpret_cast<Object*>(*ptr);
302 ptr++;
303 }
304 for (uint32_t i = 1; i < shorty_len; i++) {
305 if ((shorty[i] == 'J') || (shorty[i] == 'D')) {
306 jvalue_args[i - 1].SetJ(*((uint64_t*)ptr));
307 ptr++;
308 } else {
309 jvalue_args[i - 1].SetI(*ptr);
310 }
311 ptr++;
312 }
313 if (mh.IsReturnFloatOrDouble()) {
314 (*stub)(this, receiver, self, jvalue_args.get(), float_result);
315 } else {
316 (*stub)(this, receiver, self, jvalue_args.get(), result);
317 }
318#else
319 (*art_quick_invoke_stub)(this, args, args_size, self, result, float_result);
320#endif
321 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException()) == -1)) {
322 // Unusual case where we were running LLVM generated code and an
323 // exception was thrown to force the activations to be removed from the
324 // stack. Continue execution in the interpreter.
325 JValue value;
326 self->ClearException();
327 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(&value);
328 self->SetTopOfShadowStack(shadow_frame);
329 interpreter::EnterInterpreterFromLLVM(self, shadow_frame, result);
330 }
331 if (kLogInvocationStartAndReturn) {
332 LOG(INFO) << StringPrintf("Returned '%s' code=%p stub=%p",
333 PrettyMethod(this).c_str(), GetCode(), stub);
334 }
335 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 if (kLogInvocationStartAndReturn) {
337 LOG(INFO) << "Interpreting " << PrettyMethod(this) << "'";
338 }
Jeff Hao5d917302013-02-27 17:57:33 -0800339 if (this->IsStatic()) {
340 art::interpreter::EnterInterpreterFromInvoke(self, this, NULL, args,
341 result, float_result);
342 } else {
343 Object* receiver = reinterpret_cast<Object*>(args[0]);
344 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1,
345 result, float_result);
346 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 if (kLogInvocationStartAndReturn) {
348 LOG(INFO) << "Returned '" << PrettyMethod(this) << "'";
349 }
Jeff Hao5d917302013-02-27 17:57:33 -0800350 }
351 } else {
352 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
353 << "' code=" << reinterpret_cast<const void*>(GetCode())
354 << " stub=" << reinterpret_cast<void*>(stub);
355 if (result != NULL) {
356 result->SetJ(0);
357 float_result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 }
359 }
360 }
361
362 // Pop transition.
363 self->PopManagedStackFragment(fragment);
364}
365
366bool AbstractMethod::IsRegistered() const {
367 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
368 CHECK(native_method != NULL);
369 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
370 return native_method != jni_stub;
371}
372
373void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
374 DCHECK(Thread::Current() == self);
375 CHECK(IsNative()) << PrettyMethod(this);
376 CHECK(native_method != NULL) << PrettyMethod(this);
377 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
378 SetNativeMethod(native_method);
379 } else {
380 // We've been asked to associate this method with the given native method but are working
381 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
382 // the native method to runtime support and store the target somewhere runtime support will
383 // find it.
Ian Rogersc928de92013-02-27 14:30:44 -0800384#if defined(__arm__) && !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 SetNativeMethod(native_method);
386#else
387 UNIMPLEMENTED(FATAL);
388#endif
389 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
390 reinterpret_cast<const uint8_t*>(native_method), false);
391 }
392}
393
394void AbstractMethod::UnregisterNative(Thread* self) {
395 CHECK(IsNative()) << PrettyMethod(this);
396 // restore stub to lookup native pointer via dlsym
397 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
398}
399
400void AbstractMethod::SetNativeMethod(const void* native_method) {
401 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
402 native_method, false);
403}
404
405} // namespace mirror
406} // namespace art