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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | #include "dex_instruction.h" |
| 19 | #include "mirror/class-inl.h" |
| 20 | #include "mirror/abstract_method-inl.h" |
| 21 | #include "mirror/object-inl.h" |
| 22 | #include "mirror/object_array-inl.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 23 | #include "runtime_support.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 27 | // Determine target of interface dispatch. This object is known non-null. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::AbstractMethod* interface_method, |
| 29 | mirror::Object* this_object, |
| 30 | mirror::AbstractMethod* caller_method, |
| 31 | Thread* self, mirror::AbstractMethod** sp) |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 32 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | mirror::AbstractMethod* method; |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 34 | if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex16)) { |
| 35 | method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
Ian Rogers | a638941 | 2012-10-11 21:35:03 -0700 | [diff] [blame] | 36 | if (UNLIKELY(method == NULL)) { |
| 37 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 38 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object, |
| 39 | caller_method); |
| 40 | return 0; // Failure. |
| 41 | } |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 42 | } else { |
| 43 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 44 | DCHECK(interface_method == Runtime::Current()->GetResolutionMethod()); |
| 45 | // Determine method index from calling dex instruction. |
| 46 | #if defined(__arm__) |
| 47 | // On entry the stack pointed by sp is: |
| 48 | // | argN | | |
| 49 | // | ... | | |
| 50 | // | arg4 | | |
| 51 | // | arg3 spill | | Caller's frame |
| 52 | // | arg2 spill | | |
| 53 | // | arg1 spill | | |
| 54 | // | Method* | --- |
| 55 | // | LR | |
| 56 | // | ... | callee saves |
| 57 | // | R3 | arg3 |
| 58 | // | R2 | arg2 |
| 59 | // | R1 | arg1 |
| 60 | // | R0 | |
| 61 | // | Method* | <- sp |
| 62 | DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 63 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); |
| 64 | uintptr_t caller_pc = regs[10]; |
| 65 | #elif defined(__i386__) |
| 66 | // On entry the stack pointed by sp is: |
| 67 | // | argN | | |
| 68 | // | ... | | |
| 69 | // | arg4 | | |
| 70 | // | arg3 spill | | Caller's frame |
| 71 | // | arg2 spill | | |
| 72 | // | arg1 spill | | |
| 73 | // | Method* | --- |
| 74 | // | Return | |
| 75 | // | EBP,ESI,EDI | callee saves |
| 76 | // | EBX | arg3 |
| 77 | // | EDX | arg2 |
| 78 | // | ECX | arg1 |
| 79 | // | EAX/Method* | <- sp |
| 80 | DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 81 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); |
| 82 | uintptr_t caller_pc = regs[7]; |
jeffhao | fa147e2 | 2012-10-12 17:03:32 -0700 | [diff] [blame] | 83 | #elif defined(__mips__) |
| 84 | // On entry the stack pointed by sp is: |
| 85 | // | argN | | |
| 86 | // | ... | | |
| 87 | // | arg4 | | |
| 88 | // | arg3 spill | | Caller's frame |
| 89 | // | arg2 spill | | |
| 90 | // | arg1 spill | | |
| 91 | // | Method* | --- |
| 92 | // | RA | |
| 93 | // | ... | callee saves |
| 94 | // | A3 | arg3 |
| 95 | // | A2 | arg2 |
| 96 | // | A1 | arg1 |
| 97 | // | A0/Method* | <- sp |
Jeff Hao | 1f3bc2f | 2013-04-30 15:17:19 -0700 | [diff] [blame] | 98 | DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
jeffhao | fa147e2 | 2012-10-12 17:03:32 -0700 | [diff] [blame] | 99 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); |
Jeff Hao | 1f3bc2f | 2013-04-30 15:17:19 -0700 | [diff] [blame] | 100 | uintptr_t caller_pc = regs[15]; |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 101 | #else |
| 102 | UNIMPLEMENTED(FATAL); |
| 103 | uintptr_t caller_pc = 0; |
| 104 | #endif |
| 105 | uint32_t dex_pc = caller_method->ToDexPc(caller_pc); |
| 106 | const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem(); |
| 107 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 108 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 109 | Instruction::Code instr_code = instr->Opcode(); |
| 110 | CHECK(instr_code == Instruction::INVOKE_INTERFACE || |
| 111 | instr_code == Instruction::INVOKE_INTERFACE_RANGE) |
| 112 | << "Unexpected call into interface trampoline: " << instr->DumpString(NULL); |
| 113 | DecodedInstruction dec_insn(instr); |
| 114 | uint32_t dex_method_idx = dec_insn.vB; |
| 115 | method = FindMethodFromCode(dex_method_idx, this_object, caller_method, self, |
| 116 | false, kInterface); |
| 117 | if (UNLIKELY(method == NULL)) { |
| 118 | CHECK(self->IsExceptionPending()); |
Ian Rogers | a638941 | 2012-10-11 21:35:03 -0700 | [diff] [blame] | 119 | return 0; // Failure. |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame^] | 122 | const void* code = method->GetEntryPointFromCompiledCode(); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 123 | |
| 124 | #ifndef NDEBUG |
| 125 | // When we return, the caller will branch to this address, so it had better not be 0! |
| 126 | if (UNLIKELY(code == NULL)) { |
| 127 | MethodHelper mh(method); |
| 128 | LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method) |
| 129 | << " location: " << mh.GetDexFile().GetLocation(); |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 134 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 135 | uint64_t result = ((code_uint << 32) | method_uint); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, |
| 141 | mirror::AbstractMethod* caller_method, |
| 142 | Thread* self, mirror::AbstractMethod** sp, bool access_check, |
| 143 | InvokeType type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 144 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 145 | mirror::AbstractMethod* method = FindMethodFast(method_idx, this_object, caller_method, |
| 146 | access_check, type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 147 | if (UNLIKELY(method == NULL)) { |
| 148 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 149 | method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type); |
| 150 | if (UNLIKELY(method == NULL)) { |
| 151 | CHECK(self->IsExceptionPending()); |
| 152 | return 0; // failure |
| 153 | } |
| 154 | } |
| 155 | DCHECK(!self->IsExceptionPending()); |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame^] | 156 | const void* code = method->GetEntryPointFromCompiledCode(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 157 | |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 158 | #ifndef NDEBUG |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 159 | // When we return, the caller will branch to this address, so it had better not be 0! |
Mathieu Chartier | a92f971 | 2012-07-23 10:56:42 -0700 | [diff] [blame] | 160 | if (UNLIKELY(code == NULL)) { |
| 161 | MethodHelper mh(method); |
| 162 | LOG(FATAL) << "Code was NULL in method: " << PrettyMethod(method) |
| 163 | << " location: " << mh.GetDexFile().GetLocation(); |
| 164 | } |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 165 | #endif |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 166 | |
| 167 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 168 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 169 | uint64_t result = ((code_uint << 32) | method_uint); |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | // See comments in runtime_support_asm.S |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 174 | extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 175 | mirror::Object* this_object, |
| 176 | mirror::AbstractMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 177 | Thread* self, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 178 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 179 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 180 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 185 | mirror::Object* this_object, |
| 186 | mirror::AbstractMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 187 | Thread* self, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 188 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 189 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 190 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect); |
| 191 | } |
| 192 | |
| 193 | extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 194 | mirror::Object* this_object, |
| 195 | mirror::AbstractMethod* caller_method, |
| 196 | Thread* self, |
| 197 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 198 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 199 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic); |
| 200 | } |
| 201 | |
| 202 | extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 203 | mirror::Object* this_object, |
| 204 | mirror::AbstractMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 205 | Thread* self, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 206 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 207 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 208 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper); |
| 209 | } |
| 210 | |
| 211 | extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 212 | mirror::Object* this_object, |
| 213 | mirror::AbstractMethod* caller_method, |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 214 | Thread* self, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 215 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 216 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 217 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual); |
| 218 | } |
| 219 | |
| 220 | } // namespace art |