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