blob: 202fa2f07c5e40d77df048fd374afb909b2e278b [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"
20#include "class-inl.h"
21#include "base/stringpiece.h"
22#include "gc/card_table-inl.h"
23#include "interpreter/interpreter.h"
24#include "jni_internal.h"
25#include "object-inl.h"
26#include "object_array.h"
27#include "object_array-inl.h"
28#include "string.h"
29#include "object_utils.h"
30
31namespace art {
32namespace mirror {
33
34// TODO: get global references for these
35Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
36Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
37
38InvokeType AbstractMethod::GetInvokeType() const {
39 // TODO: kSuper?
40 if (GetDeclaringClass()->IsInterface()) {
41 return kInterface;
42 } else if (IsStatic()) {
43 return kStatic;
44 } else if (IsDirect()) {
45 return kDirect;
46 } else {
47 return kVirtual;
48 }
49}
50
51void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
52 CHECK(java_lang_reflect_Constructor_ == NULL);
53 CHECK(java_lang_reflect_Constructor != NULL);
54 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
55
56 CHECK(java_lang_reflect_Method_ == NULL);
57 CHECK(java_lang_reflect_Method != NULL);
58 java_lang_reflect_Method_ = java_lang_reflect_Method;
59}
60
61void AbstractMethod::ResetClasses() {
62 CHECK(java_lang_reflect_Constructor_ != NULL);
63 java_lang_reflect_Constructor_ = NULL;
64
65 CHECK(java_lang_reflect_Method_ != NULL);
66 java_lang_reflect_Method_ = NULL;
67}
68
69ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
70 return GetFieldObject<ObjectArray<String>*>(
71 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
72}
73
74void 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
79ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
80 return GetFieldObject<ObjectArray<AbstractMethod>*>(
81 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
82}
83
84void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
85 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
86 new_dex_cache_methods, false);
87}
88
89ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
90 return GetFieldObject<ObjectArray<Class>*>(
91 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
92}
93
94void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
95 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
96 new_dex_cache_classes, false);
97}
98
99ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
100 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
101 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
102 false);
103}
104
105void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
106 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
107 new_value, false);
108}
109
110size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
111 CHECK_LE(1, shorty.length());
112 uint32_t num_registers = 0;
113 for (int i = 1; i < shorty.length(); ++i) {
114 char ch = shorty[i];
115 if (ch == 'D' || ch == 'J') {
116 num_registers += 2;
117 } else {
118 num_registers += 1;
119 }
120 }
121 return num_registers;
122}
123
124bool AbstractMethod::IsProxyMethod() const {
125 return GetDeclaringClass()->IsProxyClass();
126}
127
128AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
129 if (IsStatic()) {
130 return NULL;
131 }
132 Class* declaring_class = GetDeclaringClass();
133 Class* super_class = declaring_class->GetSuperClass();
134 uint16_t method_index = GetMethodIndex();
135 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
136 AbstractMethod* result = NULL;
137 // Did this method override a super class method? If so load the result from the super class'
138 // vtable
139 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
140 result = super_class_vtable->Get(method_index);
141 } else {
142 // Method didn't override superclass method so search interfaces
143 if (IsProxyMethod()) {
144 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
145 CHECK_EQ(result,
146 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
147 } else {
148 MethodHelper mh(this);
149 MethodHelper interface_mh;
150 IfTable* iftable = GetDeclaringClass()->GetIfTable();
151 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
152 Class* interface = iftable->GetInterface(i);
153 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
154 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
155 interface_mh.ChangeMethod(interface_method);
156 if (mh.HasSameNameAndSignature(&interface_mh)) {
157 result = interface_method;
158 break;
159 }
160 }
161 }
162 }
163 }
164#ifndef NDEBUG
165 MethodHelper result_mh(result);
166 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
167#endif
168 return result;
169}
170
171static const void* GetOatCode(const AbstractMethod* m)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
173 Runtime* runtime = Runtime::Current();
174 const void* code = m->GetCode();
175 // Peel off any method tracing trampoline.
176 if (runtime->IsMethodTracingActive() && runtime->GetInstrumentation()->GetSavedCodeFromMap(m) != NULL) {
177 code = runtime->GetInstrumentation()->GetSavedCodeFromMap(m);
178 }
179 // Peel off any resolution stub.
180 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
181 code = runtime->GetClassLinker()->GetOatCodeFor(m);
182 }
183 return code;
184}
185
186uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
187 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
188}
189
190// Find the lowest-address native safepoint pc for a given dex pc
191uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800192#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 const uint32_t* mapping_table = GetPcToDexMappingTable();
194 if (mapping_table == NULL) {
195 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
196 return DexFile::kDexNoIndex; // Special no mapping case
197 }
198 size_t mapping_table_length = GetPcToDexMappingTableLength();
199 for (size_t i = 0; i < mapping_table_length; i += 2) {
200 if (mapping_table[i + 1] == dex_pc) {
201 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
202 }
203 }
204 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
205 << " in " << PrettyMethod(this);
206 return 0;
207#else
208 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
209 return static_cast<uint32_t>(dex_pc);
210#endif
211}
212
213uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
Ian Rogersc928de92013-02-27 14:30:44 -0800214#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 const uint32_t* mapping_table = GetPcToDexMappingTable();
216 if (mapping_table == NULL) {
217 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
218 return DexFile::kDexNoIndex; // Special no mapping case
219 }
220 size_t mapping_table_length = GetPcToDexMappingTableLength();
221 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
222 for (size_t i = 0; i < mapping_table_length; i += 2) {
223 if (mapping_table[i] == sought_offset) {
224 return mapping_table[i + 1];
225 }
226 }
227 LOG(ERROR) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
228 << "(PC " << reinterpret_cast<void*>(pc) << ") in " << PrettyMethod(this);
229 return DexFile::kDexNoIndex;
230#else
231 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
232 return static_cast<uint32_t>(pc);
233#endif
234}
235
236uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
237 const uint32_t* mapping_table = GetDexToPcMappingTable();
238 if (mapping_table == NULL) {
239 DCHECK_EQ(dex_pc, 0U);
240 return 0; // Special no mapping/pc == 0 case
241 }
242 size_t mapping_table_length = GetDexToPcMappingTableLength();
243 for (size_t i = 0; i < mapping_table_length; i += 2) {
244 uint32_t map_offset = mapping_table[i];
245 uint32_t map_dex_offset = mapping_table[i + 1];
246 if (map_dex_offset == dex_pc) {
247 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
248 }
249 }
250 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
251 << " in " << PrettyMethod(this);
252 return 0;
253}
254
255uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
256 MethodHelper mh(this);
257 const DexFile::CodeItem* code_item = mh.GetCodeItem();
258 // Iterate over the catch handlers associated with dex_pc
259 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
260 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
261 // Catch all case
262 if (iter_type_idx == DexFile::kDexNoIndex16) {
263 return it.GetHandlerAddress();
264 }
265 // Does this catch exception type apply?
266 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
267 if (iter_exception_type == NULL) {
268 // The verifier should take care of resolving all exception classes early
269 LOG(WARNING) << "Unresolved exception class when finding catch block: "
270 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
271 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
272 return it.GetHandlerAddress();
273 }
274 }
275 // Handler not found
276 return DexFile::kDexNoIndex;
277}
278
279void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) {
280 if (kIsDebugBuild) {
281 self->AssertThreadSuspensionIsAllowable();
282 CHECK_EQ(kRunnable, self->GetState());
283 }
284
285 // Push a transition back into managed code onto the linked list in thread.
286 ManagedStack fragment;
287 self->PushManagedStackFragment(&fragment);
288
289 // Call the invoke stub associated with the method.
290 // Pass everything as arguments.
291 AbstractMethod::InvokeStub* stub = GetInvokeStub();
292
293 if (UNLIKELY(!Runtime::Current()->IsStarted())){
294 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
295 if (result != NULL) {
296 result->SetJ(0);
297 }
298 } else {
299 bool interpret = self->ReadFlag(kEnterInterpreter) && !IsNative() && !IsProxyMethod();
300 const bool kLogInvocationStartAndReturn = false;
301 if (!interpret && GetCode() != NULL && stub != NULL) {
302 if (kLogInvocationStartAndReturn) {
303 LOG(INFO) << StringPrintf("Invoking '%s' code=%p stub=%p",
304 PrettyMethod(this).c_str(), GetCode(), stub);
305 }
306 (*stub)(this, receiver, self, args, result);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800307 if (UNLIKELY(reinterpret_cast<int32_t>(self->GetException()) == -1)) {
308 // Unusual case where we were running LLVM generated code and an
309 // exception was thrown to force the activations to be removed from the
310 // stack. Continue execution in the interpreter.
311 JValue value;
312 self->ClearException();
313 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(&value);
314 self->SetTopOfShadowStack(shadow_frame);
315 interpreter::EnterInterpreterFromLLVM(self, shadow_frame, result);
316 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 if (kLogInvocationStartAndReturn) {
318 LOG(INFO) << StringPrintf("Returned '%s' code=%p stub=%p",
319 PrettyMethod(this).c_str(), GetCode(), stub);
320 }
321 } else {
322 const bool kInterpretMethodsWithNoCode = false;
323 if (interpret || kInterpretMethodsWithNoCode) {
324 if (kLogInvocationStartAndReturn) {
325 LOG(INFO) << "Interpreting " << PrettyMethod(this) << "'";
326 }
327 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args, result);
328 if (kLogInvocationStartAndReturn) {
329 LOG(INFO) << "Returned '" << PrettyMethod(this) << "'";
330 }
331 } else {
332 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
333 << "' code=" << reinterpret_cast<const void*>(GetCode())
334 << " stub=" << reinterpret_cast<void*>(stub);
335 if (result != NULL) {
336 result->SetJ(0);
337 }
338 }
339 }
340 }
341
342 // Pop transition.
343 self->PopManagedStackFragment(fragment);
344}
345
346bool AbstractMethod::IsRegistered() const {
347 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
348 CHECK(native_method != NULL);
349 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
350 return native_method != jni_stub;
351}
352
353void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
354 DCHECK(Thread::Current() == self);
355 CHECK(IsNative()) << PrettyMethod(this);
356 CHECK(native_method != NULL) << PrettyMethod(this);
357 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
358 SetNativeMethod(native_method);
359 } else {
360 // We've been asked to associate this method with the given native method but are working
361 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
362 // the native method to runtime support and store the target somewhere runtime support will
363 // find it.
Ian Rogersc928de92013-02-27 14:30:44 -0800364#if defined(__arm__) && !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 SetNativeMethod(native_method);
366#else
367 UNIMPLEMENTED(FATAL);
368#endif
369 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
370 reinterpret_cast<const uint8_t*>(native_method), false);
371 }
372}
373
374void AbstractMethod::UnregisterNative(Thread* self) {
375 CHECK(IsNative()) << PrettyMethod(this);
376 // restore stub to lookup native pointer via dlsym
377 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
378}
379
380void AbstractMethod::SetNativeMethod(const void* native_method) {
381 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
382 native_method, false);
383}
384
385} // namespace mirror
386} // namespace art