blob: 5a1b3e84aded17a854224be2bc7d0fa96b92224a [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
2 * Copyright (C) 2012 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 "callee_save_frame.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020018#include "dex_instruction-inl.h"
Ian Rogers7655f292013-07-29 11:07:13 -070019#include "entrypoints/entrypoint_utils.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070022#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object-inl.h"
24#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025
26namespace art {
27
Ian Rogers137e88f2012-10-08 17:46:47 -070028// Determine target of interface dispatch. This object is known non-null.
Brian Carlstromea46f952013-07-30 01:26:50 -070029extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -070031 mirror::ArtMethod* caller_method,
32 Thread* self, mirror::ArtMethod** sp)
Ian Rogers137e88f2012-10-08 17:46:47 -070033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070034 mirror::ArtMethod* method;
Ian Rogers8b2c0b92013-09-19 02:56:49 -070035 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Ian Rogers137e88f2012-10-08 17:46:47 -070036 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogersa6389412012-10-11 21:35:03 -070037 if (UNLIKELY(method == NULL)) {
38 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
39 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
40 caller_method);
41 return 0; // Failure.
42 }
Ian Rogers137e88f2012-10-08 17:46:47 -070043 } else {
44 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
45 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
46 // Determine method index from calling dex instruction.
47#if defined(__arm__)
48 // On entry the stack pointed by sp is:
49 // | argN | |
50 // | ... | |
51 // | arg4 | |
52 // | arg3 spill | | Caller's frame
53 // | arg2 spill | |
54 // | arg1 spill | |
55 // | Method* | ---
56 // | LR |
57 // | ... | callee saves
58 // | R3 | arg3
59 // | R2 | arg2
60 // | R1 | arg1
61 // | R0 |
62 // | Method* | <- sp
63 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
64 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
65 uintptr_t caller_pc = regs[10];
66#elif defined(__i386__)
67 // On entry the stack pointed by sp is:
68 // | argN | |
69 // | ... | |
70 // | arg4 | |
71 // | arg3 spill | | Caller's frame
72 // | arg2 spill | |
73 // | arg1 spill | |
74 // | Method* | ---
75 // | Return |
76 // | EBP,ESI,EDI | callee saves
77 // | EBX | arg3
78 // | EDX | arg2
79 // | ECX | arg1
80 // | EAX/Method* | <- sp
81 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
82 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
83 uintptr_t caller_pc = regs[7];
jeffhaofa147e22012-10-12 17:03:32 -070084#elif defined(__mips__)
85 // On entry the stack pointed by sp is:
86 // | argN | |
87 // | ... | |
88 // | arg4 | |
89 // | arg3 spill | | Caller's frame
90 // | arg2 spill | |
91 // | arg1 spill | |
92 // | Method* | ---
93 // | RA |
94 // | ... | callee saves
95 // | A3 | arg3
96 // | A2 | arg2
97 // | A1 | arg1
98 // | A0/Method* | <- sp
Jeff Hao1f3bc2f2013-04-30 15:17:19 -070099 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
jeffhaofa147e22012-10-12 17:03:32 -0700100 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
Jeff Hao1f3bc2f2013-04-30 15:17:19 -0700101 uintptr_t caller_pc = regs[15];
Ian Rogers137e88f2012-10-08 17:46:47 -0700102#else
103 UNIMPLEMENTED(FATAL);
104 uintptr_t caller_pc = 0;
105#endif
106 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
107 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
108 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
109 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
110 Instruction::Code instr_code = instr->Opcode();
111 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
112 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
113 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200114 uint32_t dex_method_idx;
115 if (instr_code == Instruction::INVOKE_INTERFACE) {
116 dex_method_idx = instr->VRegB_35c();
117 } else {
118 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
119 dex_method_idx = instr->VRegB_3rc();
120 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200121 method = FindMethodFromCode<kInterface, false>(dex_method_idx, this_object, caller_method, self);
Ian Rogers137e88f2012-10-08 17:46:47 -0700122 if (UNLIKELY(method == NULL)) {
123 CHECK(self->IsExceptionPending());
Ian Rogersa6389412012-10-11 21:35:03 -0700124 return 0; // Failure.
Ian Rogers137e88f2012-10-08 17:46:47 -0700125 }
126 }
Jeff Haoaa4a7932013-05-13 11:28:27 -0700127 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers137e88f2012-10-08 17:46:47 -0700128
129#ifndef NDEBUG
130 // When we return, the caller will branch to this address, so it had better not be 0!
131 if (UNLIKELY(code == NULL)) {
132 MethodHelper mh(method);
133 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
134 << " location: " << mh.GetDexFile().GetLocation();
135 }
136#endif
137
138 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
139 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
140 uint64_t result = ((code_uint << 32) | method_uint);
141 return result;
142}
143
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200144template<InvokeType type, bool access_check>
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100145uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
146 mirror::ArtMethod* caller_method,
147 Thread* self, mirror::ArtMethod** sp) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700148 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 access_check, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700150 if (UNLIKELY(method == NULL)) {
151 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200152 method = FindMethodFromCode<type, access_check>(method_idx, this_object, caller_method, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700153 if (UNLIKELY(method == NULL)) {
154 CHECK(self->IsExceptionPending());
155 return 0; // failure
156 }
157 }
158 DCHECK(!self->IsExceptionPending());
Jeff Haoaa4a7932013-05-13 11:28:27 -0700159 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers57b86d42012-03-27 16:05:41 -0700160
Ian Rogers137e88f2012-10-08 17:46:47 -0700161#ifndef NDEBUG
Ian Rogers57b86d42012-03-27 16:05:41 -0700162 // When we return, the caller will branch to this address, so it had better not be 0!
Mathieu Chartiera92f9712012-07-23 10:56:42 -0700163 if (UNLIKELY(code == NULL)) {
164 MethodHelper mh(method);
165 LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method)
166 << " location: " << mh.GetDexFile().GetLocation();
167 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700168#endif
Ian Rogers57b86d42012-03-27 16:05:41 -0700169
170 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
171 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
172 uint64_t result = ((code_uint << 32) | method_uint);
173 return result;
174}
175
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200176// Explicit template declarations of artInvokeCommon for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100177#define EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, _access_check) \
178 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
179 uint64_t artInvokeCommon<_type, _access_check>(uint32_t method_idx, \
180 mirror::Object* this_object, \
181 mirror::ArtMethod* caller_method, \
182 Thread* self, mirror::ArtMethod** sp)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200183
184#define EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(_type) \
185 EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, false); \
186 EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, true)
187
188EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kStatic);
189EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kDirect);
190EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kVirtual);
191EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kSuper);
192EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kInterface);
193
194#undef EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL
195#undef EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL
196
Ian Rogers57b86d42012-03-27 16:05:41 -0700197// See comments in runtime_support_asm.S
Ian Rogers57b86d42012-03-27 16:05:41 -0700198extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700200 mirror::ArtMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700201 Thread* self,
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 mirror::ArtMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200204 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700205}
206
207
208extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700210 mirror::ArtMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700211 Thread* self,
Brian Carlstromea46f952013-07-30 01:26:50 -0700212 mirror::ArtMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200214 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700215}
216
217extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700219 mirror::ArtMethod* caller_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 Thread* self,
Brian Carlstromea46f952013-07-30 01:26:50 -0700221 mirror::ArtMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200223 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700224}
225
226extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700228 mirror::ArtMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700229 Thread* self,
Brian Carlstromea46f952013-07-30 01:26:50 -0700230 mirror::ArtMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200232 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700233}
234
235extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700237 mirror::ArtMethod* caller_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 Thread* self,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 mirror::ArtMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200241 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700242}
243
244} // namespace art