Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 18 | #include "dex_instruction-inl.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 19 | #include "entrypoints/entrypoint_utils.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 22 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/object-inl.h" |
| 24 | #include "mirror/object_array-inl.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 28 | // Determine target of interface dispatch. This object is known non-null. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 29 | extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 31 | mirror::ArtMethod* caller_method, |
| 32 | Thread* self, mirror::ArtMethod** sp) |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 33 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 34 | mirror::ArtMethod* method; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 35 | if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) { |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 36 | method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
Ian Rogers | a638941 | 2012-10-11 21:35:03 -0700 | [diff] [blame] | 37 | 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 Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 43 | } 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]; |
jeffhao | fa147e2 | 2012-10-12 17:03:32 -0700 | [diff] [blame] | 84 | #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 Hao | 1f3bc2f | 2013-04-30 15:17:19 -0700 | [diff] [blame] | 99 | DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
jeffhao | fa147e2 | 2012-10-12 17:03:32 -0700 | [diff] [blame] | 100 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); |
Jeff Hao | 1f3bc2f | 2013-04-30 15:17:19 -0700 | [diff] [blame] | 101 | uintptr_t caller_pc = regs[15]; |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 102 | #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 Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 114 | 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 Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 121 | method = FindMethodFromCode<kInterface, false>(dex_method_idx, this_object, caller_method, self); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 122 | if (UNLIKELY(method == NULL)) { |
| 123 | CHECK(self->IsExceptionPending()); |
Ian Rogers | a638941 | 2012-10-11 21:35:03 -0700 | [diff] [blame] | 124 | return 0; // Failure. |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 127 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 128 | |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 129 | // When we return, the caller will branch to this address, so it had better not be 0! |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 130 | if (kIsDebugBuild && UNLIKELY(code == nullptr)) { |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 131 | MethodHelper mh(method); |
| 132 | LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method) |
| 133 | << " location: " << mh.GetDexFile().GetLocation(); |
| 134 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 135 | #ifdef __LP64__ |
| 136 | UNIMPLEMENTED(FATAL); |
| 137 | return 0; |
| 138 | #else |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 139 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 140 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 141 | uint64_t result = ((code_uint << 32) | method_uint); |
| 142 | return result; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 143 | #endif |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 146 | template<InvokeType type, bool access_check> |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 147 | uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, |
| 148 | mirror::ArtMethod* caller_method, |
| 149 | Thread* self, mirror::ArtMethod** sp) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 150 | mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | access_check, type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 152 | if (UNLIKELY(method == NULL)) { |
| 153 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 154 | method = FindMethodFromCode<type, access_check>(method_idx, this_object, caller_method, self); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 155 | if (UNLIKELY(method == NULL)) { |
| 156 | CHECK(self->IsExceptionPending()); |
| 157 | return 0; // failure |
| 158 | } |
| 159 | } |
| 160 | DCHECK(!self->IsExceptionPending()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 161 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 162 | |
| 163 | // When we return, the caller will branch to this address, so it had better not be 0! |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 164 | if (kIsDebugBuild && UNLIKELY(code == NULL)) { |
Mathieu Chartier | a92f971 | 2012-07-23 10:56:42 -0700 | [diff] [blame] | 165 | MethodHelper mh(method); |
| 166 | LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method) |
| 167 | << " location: " << mh.GetDexFile().GetLocation(); |
| 168 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 169 | #ifdef __LP64__ |
| 170 | UNIMPLEMENTED(FATAL); |
| 171 | return 0; |
| 172 | #else |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 173 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 174 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 175 | uint64_t result = ((code_uint << 32) | method_uint); |
| 176 | return result; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 177 | #endif |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 180 | // Explicit template declarations of artInvokeCommon for all invoke types. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 181 | #define EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, _access_check) \ |
| 182 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 183 | uint64_t artInvokeCommon<_type, _access_check>(uint32_t method_idx, \ |
| 184 | mirror::Object* this_object, \ |
| 185 | mirror::ArtMethod* caller_method, \ |
| 186 | Thread* self, mirror::ArtMethod** sp) |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 187 | |
| 188 | #define EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(_type) \ |
| 189 | EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, false); \ |
| 190 | EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL(_type, true) |
| 191 | |
| 192 | EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kStatic); |
| 193 | EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kDirect); |
| 194 | EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kVirtual); |
| 195 | EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kSuper); |
| 196 | EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL(kInterface); |
| 197 | |
| 198 | #undef EXPLICIT_ART_INVOKE_COMMON_TYPED_TEMPLATE_DECL |
| 199 | #undef EXPLICIT_ART_INVOKE_COMMON_TEMPLATE_DECL |
| 200 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 201 | // See comments in runtime_support_asm.S |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 202 | extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 203 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 204 | mirror::ArtMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 205 | Thread* self, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 206 | mirror::ArtMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 207 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 208 | return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
| 212 | extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 213 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 214 | mirror::ArtMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 215 | Thread* self, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 216 | mirror::ArtMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 217 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 218 | return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 222 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 223 | mirror::ArtMethod* caller_method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | Thread* self, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 225 | mirror::ArtMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 226 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 227 | return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 231 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 232 | mirror::ArtMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 233 | Thread* self, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 234 | mirror::ArtMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 235 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 236 | return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 240 | mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 241 | mirror::ArtMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 242 | Thread* self, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 243 | mirror::ArtMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 244 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 245 | return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | } // namespace art |