Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. All Rights Reserved. |
| 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 "runtime_support.h" |
| 18 | |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 19 | #include "debugger.h" |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 20 | #include "dex_cache.h" |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 21 | #include "dex_verifier.h" |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 22 | #include "macros.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 23 | #include "object.h" |
| 24 | #include "object_utils.h" |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 25 | #include "reflection.h" |
Shih-wei Liao | b0ee9d7 | 2012-03-07 16:39:26 -0800 | [diff] [blame] | 26 | #include "runtime_support_common.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 27 | #include "trace.h" |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 28 | #include "ScopedLocalRef.h" |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 29 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 32 | // Place a special frame at the TOS that will save the callee saves for the given type |
| 33 | static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) { |
| 34 | // Be aware the store below may well stomp on an incoming argument |
| 35 | *sp = Runtime::Current()->GetCalleeSaveMethod(type); |
| 36 | self->SetTopOfStack(sp, 0); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 37 | self->VerifyStack(); |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 38 | } |
| 39 | |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 40 | /* |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 41 | * Report location to debugger. Note: dex_pc is the current offset within |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 42 | * the method. However, because the offset alone cannot distinguish between |
| 43 | * method entry and offset 0 within the method, we'll use an offset of -1 |
| 44 | * to denote method entry. |
| 45 | */ |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 46 | extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) { |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 47 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 48 | Dbg::UpdateDebugger(dex_pc, self, sp); |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 51 | // Temporary debugging hook for compiler. |
| 52 | extern void DebugMe(Method* method, uint32_t info) { |
| 53 | LOG(INFO) << "DebugMe"; |
| 54 | if (method != NULL) { |
| 55 | LOG(INFO) << PrettyMethod(method); |
| 56 | } |
| 57 | LOG(INFO) << "Info: " << info; |
| 58 | } |
| 59 | |
| 60 | // Return value helper for jobject return types |
| 61 | extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
Brian Carlstrom | 6f495f2 | 2011-10-10 15:05:03 -0700 | [diff] [blame] | 62 | if (thread->IsExceptionPending()) { |
| 63 | return NULL; |
| 64 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 65 | return thread->DecodeJObject(obj); |
| 66 | } |
| 67 | |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 68 | extern void* FindNativeMethod(Thread* self) { |
| 69 | DCHECK(Thread::Current() == self); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 70 | |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 71 | Method* method = const_cast<Method*>(self->GetCurrentMethod()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 72 | DCHECK(method != NULL); |
| 73 | |
| 74 | // Lookup symbol address for method, on failure we'll return NULL with an |
| 75 | // exception set, otherwise we return the address of the method we found. |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 76 | void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 77 | if (native_code == NULL) { |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 78 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 79 | return NULL; |
| 80 | } else { |
| 81 | // Register so that future calls don't come here |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 82 | method->RegisterNative(self, native_code); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 83 | return native_code; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Called by generated call to throw an exception |
| 88 | extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) { |
| 89 | /* |
| 90 | * exception may be NULL, in which case this routine should |
| 91 | * throw NPE. NOTE: this is a convenience for generated code, |
| 92 | * which previously did the null check inline and constructed |
| 93 | * and threw a NPE if NULL. This routine responsible for setting |
| 94 | * exception_ in thread and delivering the exception. |
| 95 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 96 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 97 | if (exception == NULL) { |
| 98 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception"); |
| 99 | } else { |
| 100 | thread->SetException(exception); |
| 101 | } |
| 102 | thread->DeliverException(); |
| 103 | } |
| 104 | |
| 105 | // Deliver an exception that's pending on thread helping set up a callee save frame on the way |
| 106 | extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 107 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 108 | thread->DeliverException(); |
| 109 | } |
| 110 | |
| 111 | // Called by generated call to throw a NPE exception |
Ian Rogers | 98d3988 | 2012-03-15 01:42:12 -0700 | [diff] [blame] | 112 | extern "C" void artThrowNullPointerExceptionFromCode(Thread* self, Method** sp) { |
| 113 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 114 | Frame fr = self->GetTopOfStack(); |
| 115 | uintptr_t throw_native_pc = fr.GetReturnPC(); |
| 116 | fr.Next(); |
| 117 | Method* throw_method = fr.GetMethod(); |
| 118 | uint32_t dex_pc = throw_method->ToDexPC(throw_native_pc - 2); |
| 119 | const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem(); |
| 120 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 121 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 122 | DecodedInstruction dec_insn(instr); |
| 123 | switch (instr->Opcode()) { |
| 124 | case Instruction::INVOKE_DIRECT: |
| 125 | case Instruction::INVOKE_DIRECT_RANGE: |
| 126 | ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect); |
| 127 | break; |
| 128 | case Instruction::INVOKE_VIRTUAL: |
| 129 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 130 | ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual); |
| 131 | break; |
| 132 | case Instruction::IGET: |
| 133 | case Instruction::IGET_WIDE: |
| 134 | case Instruction::IGET_OBJECT: |
| 135 | case Instruction::IGET_BOOLEAN: |
| 136 | case Instruction::IGET_BYTE: |
| 137 | case Instruction::IGET_CHAR: |
| 138 | case Instruction::IGET_SHORT: { |
| 139 | Field* field = |
| 140 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 141 | ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */); |
| 142 | break; |
| 143 | } |
| 144 | case Instruction::IPUT: |
| 145 | case Instruction::IPUT_WIDE: |
| 146 | case Instruction::IPUT_OBJECT: |
| 147 | case Instruction::IPUT_BOOLEAN: |
| 148 | case Instruction::IPUT_BYTE: |
| 149 | case Instruction::IPUT_CHAR: |
| 150 | case Instruction::IPUT_SHORT: { |
| 151 | Field* field = |
| 152 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 153 | ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */); |
| 154 | break; |
| 155 | } |
| 156 | case Instruction::AGET: |
| 157 | case Instruction::AGET_WIDE: |
| 158 | case Instruction::AGET_OBJECT: |
| 159 | case Instruction::AGET_BOOLEAN: |
| 160 | case Instruction::AGET_BYTE: |
| 161 | case Instruction::AGET_CHAR: |
| 162 | case Instruction::AGET_SHORT: |
| 163 | self->ThrowNewException("Ljava/lang/NullPointerException;", |
| 164 | "Attempt to read from null array"); |
| 165 | break; |
| 166 | case Instruction::APUT: |
| 167 | case Instruction::APUT_WIDE: |
| 168 | case Instruction::APUT_OBJECT: |
| 169 | case Instruction::APUT_BOOLEAN: |
| 170 | case Instruction::APUT_BYTE: |
| 171 | case Instruction::APUT_CHAR: |
| 172 | case Instruction::APUT_SHORT: |
| 173 | self->ThrowNewException("Ljava/lang/NullPointerException;", |
| 174 | "Attempt to write to null array"); |
| 175 | break; |
| 176 | default: { |
| 177 | const DexFile& dex_file = Runtime::Current()->GetClassLinker() |
| 178 | ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache()); |
| 179 | std::string message("Null pointer exception during instruction '"); |
| 180 | message += instr->DumpString(&dex_file); |
| 181 | message += "'"; |
| 182 | self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str()); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Called by generated call to throw an arithmetic divide by zero exception |
| 190 | extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 191 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 192 | thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero"); |
| 193 | thread->DeliverException(); |
| 194 | } |
| 195 | |
| 196 | // Called by generated call to throw an arithmetic divide by zero exception |
| 197 | extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 198 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
| 199 | thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 200 | "length=%d; index=%d", limit, index); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 201 | thread->DeliverException(); |
| 202 | } |
| 203 | |
| 204 | // Called by the AbstractMethodError stub (not runtime support) |
| 205 | extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) { |
Shih-wei Liao | 5b21308 | 2012-03-20 04:26:01 -0700 | [diff] [blame] | 206 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 207 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 5b21308 | 2012-03-20 04:26:01 -0700 | [diff] [blame] | 208 | #endif |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 209 | thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", |
| 210 | "abstract method \"%s\"", PrettyMethod(method).c_str()); |
Shih-wei Liao | 5b21308 | 2012-03-20 04:26:01 -0700 | [diff] [blame] | 211 | #if !defined(ART_USE_LLVM_COMPILER) |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 212 | thread->DeliverException(); |
Shih-wei Liao | 5b21308 | 2012-03-20 04:26:01 -0700 | [diff] [blame] | 213 | #endif |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 216 | extern "C" void artThrowStackOverflowFromCode(Method* /*method*/, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 217 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 218 | // Remove extra entry pushed onto second stack during method tracing |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 219 | if (Runtime::Current()->IsMethodTracingActive()) { |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 220 | TraceMethodUnwindFromCode(thread); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 221 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 222 | thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 223 | thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;", |
| 224 | "stack size %zdkb; default stack size: %zdkb", |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 225 | thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 226 | thread->ResetDefaultStackEnd(); // Return to default stack size |
| 227 | thread->DeliverException(); |
| 228 | } |
| 229 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 230 | static std::string ClassNameFromIndex(Method* method, uint32_t ref, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 231 | verifier::VerifyErrorRefType ref_type, bool access) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 232 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 233 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 234 | |
| 235 | uint16_t type_idx = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 236 | if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 237 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
| 238 | type_idx = id.class_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 239 | } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 240 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
| 241 | type_idx = id.class_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 242 | } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 243 | type_idx = ref; |
| 244 | } else { |
| 245 | CHECK(false) << static_cast<int>(ref_type); |
| 246 | } |
| 247 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 248 | std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx))); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 249 | if (!access) { |
| 250 | return class_name; |
| 251 | } |
| 252 | |
| 253 | std::string result; |
| 254 | result += "tried to access class "; |
| 255 | result += class_name; |
| 256 | result += " from class "; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 257 | result += PrettyDescriptor(method->GetDeclaringClass()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 258 | return result; |
| 259 | } |
| 260 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 261 | static std::string FieldNameFromIndex(const Method* method, uint32_t ref, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 262 | verifier::VerifyErrorRefType ref_type, bool access) { |
| 263 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD)); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 264 | |
| 265 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 266 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 267 | |
| 268 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 269 | std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 270 | const char* field_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 271 | if (!access) { |
| 272 | return class_name + "." + field_name; |
| 273 | } |
| 274 | |
| 275 | std::string result; |
| 276 | result += "tried to access field "; |
| 277 | result += class_name + "." + field_name; |
| 278 | result += " from class "; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 279 | result += PrettyDescriptor(method->GetDeclaringClass()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 280 | return result; |
| 281 | } |
| 282 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 283 | static std::string MethodNameFromIndex(const Method* method, uint32_t ref, |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 284 | verifier::VerifyErrorRefType ref_type, bool access) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 285 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD)); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 286 | |
| 287 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 288 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 289 | |
| 290 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 291 | std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 292 | const char* method_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 293 | if (!access) { |
| 294 | return class_name + "." + method_name; |
| 295 | } |
| 296 | |
| 297 | std::string result; |
| 298 | result += "tried to access method "; |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 299 | result += class_name + "." + method_name + ":" + |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 300 | dex_file.CreateMethodSignature(id.proto_idx_, NULL); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 301 | result += " from class "; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 302 | result += PrettyDescriptor(method->GetDeclaringClass()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
| 306 | extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 307 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 308 | Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref' |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 309 | frame.Next(); |
| 310 | Method* method = frame.GetMethod(); |
| 311 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 312 | verifier::VerifyErrorRefType ref_type = |
| 313 | static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 314 | |
| 315 | const char* exception_class = "Ljava/lang/VerifyError;"; |
| 316 | std::string msg; |
| 317 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 318 | switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) { |
| 319 | case verifier::VERIFY_ERROR_NO_CLASS: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 320 | exception_class = "Ljava/lang/NoClassDefFoundError;"; |
| 321 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 322 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 323 | case verifier::VERIFY_ERROR_NO_FIELD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 324 | exception_class = "Ljava/lang/NoSuchFieldError;"; |
| 325 | msg = FieldNameFromIndex(method, ref, ref_type, false); |
| 326 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 327 | case verifier::VERIFY_ERROR_NO_METHOD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 328 | exception_class = "Ljava/lang/NoSuchMethodError;"; |
| 329 | msg = MethodNameFromIndex(method, ref, ref_type, false); |
| 330 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 331 | case verifier::VERIFY_ERROR_ACCESS_CLASS: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 332 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 333 | msg = ClassNameFromIndex(method, ref, ref_type, true); |
| 334 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 335 | case verifier::VERIFY_ERROR_ACCESS_FIELD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 336 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 337 | msg = FieldNameFromIndex(method, ref, ref_type, true); |
| 338 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 339 | case verifier::VERIFY_ERROR_ACCESS_METHOD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 340 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 341 | msg = MethodNameFromIndex(method, ref, ref_type, true); |
| 342 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 343 | case verifier::VERIFY_ERROR_CLASS_CHANGE: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 344 | exception_class = "Ljava/lang/IncompatibleClassChangeError;"; |
| 345 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 346 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 347 | case verifier::VERIFY_ERROR_INSTANTIATION: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 348 | exception_class = "Ljava/lang/InstantiationError;"; |
| 349 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 350 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 351 | case verifier::VERIFY_ERROR_BAD_CLASS_SOFT: |
| 352 | case verifier::VERIFY_ERROR_BAD_CLASS_HARD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 353 | // Generic VerifyError; use default exception, no message. |
| 354 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 355 | case verifier::VERIFY_ERROR_NONE: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 356 | CHECK(false); |
| 357 | break; |
| 358 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 359 | self->ThrowNewException(exception_class, msg.c_str()); |
| 360 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 364 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 365 | LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 366 | thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 367 | thread->DeliverException(); |
| 368 | } |
| 369 | |
| 370 | extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 371 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 372 | LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 373 | thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 374 | thread->DeliverException(); |
| 375 | } |
| 376 | |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 377 | extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 378 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 379 | Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 380 | frame.Next(); |
| 381 | Method* method = frame.GetMethod(); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 382 | self->ThrowNewException("Ljava/lang/NoSuchMethodError;", |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 383 | MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str()); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 384 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 388 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 389 | LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode"; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 390 | thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 391 | thread->DeliverException(); |
| 392 | } |
| 393 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 394 | const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread, |
| 395 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 396 | // TODO: this code is specific to ARM |
| 397 | // On entry the stack pointed by sp is: |
| 398 | // | argN | | |
| 399 | // | ... | | |
| 400 | // | arg4 | | |
| 401 | // | arg3 spill | | Caller's frame |
| 402 | // | arg2 spill | | |
| 403 | // | arg1 spill | | |
| 404 | // | Method* | --- |
| 405 | // | LR | |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 406 | // | ... | callee saves |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 407 | // | R3 | arg3 |
| 408 | // | R2 | arg2 |
| 409 | // | R1 | arg1 |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 410 | // | R0 | |
| 411 | // | Method* | <- sp |
| 412 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); |
| 413 | DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 414 | Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48); |
| 415 | uintptr_t caller_pc = regs[10]; |
| 416 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 417 | // Start new JNI local reference state |
| 418 | JNIEnvExt* env = thread->GetJniEnv(); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 419 | ScopedJniEnvLocalRefState env_state(env); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 420 | |
| 421 | // Compute details about the called method (avoid GCs) |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 422 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 423 | Method* caller = *caller_sp; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 424 | bool is_static; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 425 | bool is_virtual; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 426 | uint32_t dex_method_idx; |
| 427 | const char* shorty; |
| 428 | uint32_t shorty_len; |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 429 | if (type == Runtime::kUnknownMethod) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 430 | DCHECK(called->IsRuntimeMethod()); |
Ian Rogers | df9a782 | 2011-10-11 16:53:22 -0700 | [diff] [blame] | 431 | // less two as return address may span into next dex instruction |
| 432 | uint32_t dex_pc = caller->ToDexPC(caller_pc - 2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 433 | const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 434 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 435 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 436 | Instruction::Code instr_code = instr->Opcode(); |
| 437 | is_static = (instr_code == Instruction::INVOKE_STATIC) || |
| 438 | (instr_code == Instruction::INVOKE_STATIC_RANGE); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 439 | is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) || |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 440 | (instr_code == Instruction::INVOKE_VIRTUAL_RANGE) || |
| 441 | (instr_code == Instruction::INVOKE_SUPER) || |
| 442 | (instr_code == Instruction::INVOKE_SUPER_RANGE); |
| 443 | DCHECK(is_static || is_virtual || (instr_code == Instruction::INVOKE_DIRECT) || |
| 444 | (instr_code == Instruction::INVOKE_DIRECT_RANGE)); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 445 | DecodedInstruction dec_insn(instr); |
| 446 | dex_method_idx = dec_insn.vB; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 447 | shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 448 | } else { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 449 | DCHECK(!called->IsRuntimeMethod()); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 450 | is_static = type == Runtime::kStaticMethod; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 451 | is_virtual = false; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 452 | dex_method_idx = called->GetDexMethodIndex(); |
| 453 | MethodHelper mh(called); |
| 454 | shorty = mh.GetShorty(); |
| 455 | shorty_len = mh.GetShortyLength(); |
| 456 | } |
| 457 | // Discover shorty (avoid GCs) |
| 458 | size_t args_in_regs = 0; |
| 459 | for (size_t i = 1; i < shorty_len; i++) { |
| 460 | char c = shorty[i]; |
| 461 | args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1); |
| 462 | if (args_in_regs > 3) { |
| 463 | args_in_regs = 3; |
| 464 | break; |
| 465 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 466 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 467 | // Place into local references incoming arguments from the caller's register arguments |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 468 | size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1 |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 469 | if (!is_static) { |
| 470 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 471 | cur_arg++; |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 472 | if (args_in_regs < 3) { |
| 473 | // If we thought we had fewer than 3 arguments in registers, account for the receiver |
| 474 | args_in_regs++; |
| 475 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 476 | AddLocalReference<jobject>(env, obj); |
| 477 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 478 | size_t shorty_index = 1; // skip return value |
| 479 | // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip |
| 480 | // R0) |
| 481 | while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) { |
| 482 | char c = shorty[shorty_index]; |
| 483 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 484 | if (c == 'L') { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 485 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 486 | AddLocalReference<jobject>(env, obj); |
| 487 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 488 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
| 489 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 490 | // Place into local references incoming arguments from the caller's stack arguments |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 491 | cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 492 | while (shorty_index < shorty_len) { |
| 493 | char c = shorty[shorty_index]; |
| 494 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 495 | if (c == 'L') { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 496 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 497 | AddLocalReference<jobject>(env, obj); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 498 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 499 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 500 | } |
| 501 | // Resolve method filling in dex cache |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 502 | if (type == Runtime::kUnknownMethod) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 503 | called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 504 | } |
| 505 | const void* code = NULL; |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 506 | if (LIKELY(!thread->IsExceptionPending())) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 507 | if (LIKELY(called->IsDirect() == !is_virtual)) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 508 | // Ensure that the called method's class is initialized. |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 509 | Class* called_class = called->GetDeclaringClass(); |
| 510 | linker->EnsureInitialized(called_class, true); |
| 511 | if (LIKELY(called_class->IsInitialized())) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 512 | code = called->GetCode(); |
| 513 | } else if (called_class->IsInitializing()) { |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 514 | if (is_static) { |
| 515 | // Class is still initializing, go to oat and grab code (trampoline must be left in place |
| 516 | // until class is initialized to stop races between threads). |
| 517 | code = linker->GetOatCodeFor(called); |
| 518 | } else { |
| 519 | // No trampoline for non-static methods. |
| 520 | code = called->GetCode(); |
| 521 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 522 | } else { |
| 523 | DCHECK(called_class->IsErroneous()); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 524 | } |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 525 | } else { |
| 526 | // Direct method has been made virtual |
| 527 | thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
| 528 | "Expected direct method but found virtual: %s", |
| 529 | PrettyMethod(called, true).c_str()); |
| 530 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 531 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 532 | if (UNLIKELY(code == NULL)) { |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 533 | // Something went wrong in ResolveMethod or EnsureInitialized, |
| 534 | // go into deliver exception with the pending exception in r0 |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 535 | code = reinterpret_cast<void*>(art_deliver_exception_from_code); |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 536 | regs[0] = reinterpret_cast<uintptr_t>(thread->GetException()); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 537 | thread->ClearException(); |
| 538 | } else { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 539 | // Expect class to at least be initializing. |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 540 | DCHECK(called->GetDeclaringClass()->IsInitializing()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 541 | // Don't want infinite recursion. |
| 542 | DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData()); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 543 | // Set up entry into main method |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 544 | regs[0] = reinterpret_cast<uintptr_t>(called); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 545 | } |
| 546 | return code; |
| 547 | } |
| 548 | |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 549 | static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) { |
| 550 | intptr_t value = *arg_ptr; |
| 551 | Object** value_as_jni_rep = reinterpret_cast<Object**>(value); |
| 552 | Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL; |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 553 | CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep)) << value_as_work_around_rep; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 554 | *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep); |
| 555 | } |
| 556 | |
| 557 | extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) { |
| 558 | DCHECK(Thread::Current() == self); |
| 559 | // TODO: this code is specific to ARM |
| 560 | // On entry the stack pointed by sp is: |
| 561 | // | arg3 | <- Calling JNI method's frame (and extra bit for out args) |
| 562 | // | LR | |
| 563 | // | R3 | arg2 |
| 564 | // | R2 | arg1 |
| 565 | // | R1 | jclass/jobject |
| 566 | // | R0 | JNIEnv |
| 567 | // | unused | |
| 568 | // | unused | |
| 569 | // | unused | <- sp |
| 570 | Method* jni_method = self->GetTopOfStack().GetMethod(); |
| 571 | DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method); |
| 572 | intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack |
| 573 | // Fix up this/jclass argument |
| 574 | WorkAroundJniBugsForJobject(arg_ptr); |
| 575 | arg_ptr++; |
| 576 | // Fix up jobject arguments |
| 577 | MethodHelper mh(jni_method); |
| 578 | int reg_num = 2; // Current register being processed, -1 for stack arguments. |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 579 | for (uint32_t i = 1; i < mh.GetShortyLength(); i++) { |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 580 | char shorty_char = mh.GetShorty()[i]; |
| 581 | if (shorty_char == 'L') { |
| 582 | WorkAroundJniBugsForJobject(arg_ptr); |
| 583 | } |
| 584 | if (shorty_char == 'J' || shorty_char == 'D') { |
| 585 | if (reg_num == 2) { |
| 586 | arg_ptr = sp + 8; // skip to out arguments |
| 587 | reg_num = -1; |
| 588 | } else if (reg_num == 3) { |
| 589 | arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned |
| 590 | reg_num = -1; |
| 591 | } else { |
| 592 | DCHECK(reg_num == -1); |
| 593 | if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) { |
| 594 | arg_ptr += 3; // unaligned, pad and move through stack arguments |
| 595 | } else { |
| 596 | arg_ptr += 2; // aligned, move through stack arguments |
| 597 | } |
| 598 | } |
| 599 | } else { |
| 600 | if (reg_num == 2) { |
| 601 | arg_ptr++; // move through register arguments |
| 602 | reg_num++; |
| 603 | } else if (reg_num == 3) { |
| 604 | arg_ptr = sp + 8; // skip to outgoing stack arguments |
| 605 | reg_num = -1; |
| 606 | } else { |
| 607 | DCHECK(reg_num == -1); |
| 608 | arg_ptr++; // move through stack arguments |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | // Load expected destination, see Method::RegisterNative |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 613 | const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw()); |
| 614 | if (UNLIKELY(code == NULL)) { |
| 615 | code = Runtime::Current()->GetJniDlsymLookupStub()->GetData(); |
| 616 | jni_method->RegisterNative(self, code); |
| 617 | } |
| 618 | return code; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 621 | |
| 622 | extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 623 | Thread* self, Method** sp) { |
| 624 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t)); |
| 625 | if (LIKELY(field != NULL)) { |
| 626 | return field->Get32(NULL); |
| 627 | } |
| 628 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 629 | field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t)); |
| 630 | if (LIKELY(field != NULL)) { |
| 631 | return field->Get32(NULL); |
| 632 | } |
| 633 | return 0; // Will throw exception by checking with Thread::Current |
| 634 | } |
| 635 | |
| 636 | extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 637 | Thread* self, Method** sp) { |
| 638 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t)); |
| 639 | if (LIKELY(field != NULL)) { |
| 640 | return field->Get64(NULL); |
| 641 | } |
| 642 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 643 | field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t)); |
| 644 | if (LIKELY(field != NULL)) { |
| 645 | return field->Get64(NULL); |
| 646 | } |
| 647 | return 0; // Will throw exception by checking with Thread::Current |
| 648 | } |
| 649 | |
| 650 | extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 651 | Thread* self, Method** sp) { |
| 652 | Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*)); |
| 653 | if (LIKELY(field != NULL)) { |
| 654 | return field->GetObj(NULL); |
| 655 | } |
| 656 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 657 | field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*)); |
| 658 | if (LIKELY(field != NULL)) { |
| 659 | return field->GetObj(NULL); |
| 660 | } |
| 661 | return NULL; // Will throw exception by checking with Thread::Current |
| 662 | } |
| 663 | |
| 664 | extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj, |
| 665 | const Method* referrer, Thread* self, Method** sp) { |
| 666 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t)); |
| 667 | if (LIKELY(field != NULL && obj != NULL)) { |
| 668 | return field->Get32(obj); |
| 669 | } |
| 670 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 671 | field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t)); |
| 672 | if (LIKELY(field != NULL)) { |
| 673 | if (UNLIKELY(obj == NULL)) { |
| 674 | ThrowNullPointerExceptionForFieldAccess(self, field, true); |
| 675 | } else { |
| 676 | return field->Get32(obj); |
| 677 | } |
| 678 | } |
| 679 | return 0; // Will throw exception by checking with Thread::Current |
| 680 | } |
| 681 | |
| 682 | extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj, |
| 683 | const Method* referrer, Thread* self, Method** sp) { |
| 684 | Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t)); |
| 685 | if (LIKELY(field != NULL && obj != NULL)) { |
| 686 | return field->Get64(obj); |
| 687 | } |
| 688 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 689 | field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t)); |
| 690 | if (LIKELY(field != NULL)) { |
| 691 | if (UNLIKELY(obj == NULL)) { |
| 692 | ThrowNullPointerExceptionForFieldAccess(self, field, true); |
| 693 | } else { |
| 694 | return field->Get64(obj); |
| 695 | } |
| 696 | } |
| 697 | return 0; // Will throw exception by checking with Thread::Current |
| 698 | } |
| 699 | |
| 700 | extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj, |
| 701 | const Method* referrer, Thread* self, Method** sp) { |
| 702 | Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*)); |
| 703 | if (LIKELY(field != NULL && obj != NULL)) { |
| 704 | return field->GetObj(obj); |
| 705 | } |
| 706 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 707 | field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*)); |
| 708 | if (LIKELY(field != NULL)) { |
| 709 | if (UNLIKELY(obj == NULL)) { |
| 710 | ThrowNullPointerExceptionForFieldAccess(self, field, true); |
| 711 | } else { |
| 712 | return field->GetObj(obj); |
| 713 | } |
| 714 | } |
| 715 | return NULL; // Will throw exception by checking with Thread::Current |
| 716 | } |
| 717 | |
| 718 | extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value, |
| 719 | const Method* referrer, Thread* self, Method** sp) { |
| 720 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t)); |
| 721 | if (LIKELY(field != NULL)) { |
| 722 | field->Set32(NULL, new_value); |
| 723 | return 0; // success |
| 724 | } |
| 725 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 726 | field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t)); |
| 727 | if (LIKELY(field != NULL)) { |
| 728 | field->Set32(NULL, new_value); |
| 729 | return 0; // success |
| 730 | } |
| 731 | return -1; // failure |
| 732 | } |
| 733 | |
| 734 | extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 735 | uint64_t new_value, Thread* self, Method** sp) { |
| 736 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t)); |
| 737 | if (LIKELY(field != NULL)) { |
| 738 | field->Set64(NULL, new_value); |
| 739 | return 0; // success |
| 740 | } |
| 741 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 742 | field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t)); |
| 743 | if (LIKELY(field != NULL)) { |
| 744 | field->Set64(NULL, new_value); |
| 745 | return 0; // success |
| 746 | } |
| 747 | return -1; // failure |
| 748 | } |
| 749 | |
| 750 | extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value, |
| 751 | const Method* referrer, Thread* self, Method** sp) { |
| 752 | Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*)); |
| 753 | if (LIKELY(field != NULL)) { |
| 754 | if (LIKELY(!FieldHelper(field).IsPrimitiveType())) { |
| 755 | field->SetObj(NULL, new_value); |
| 756 | return 0; // success |
| 757 | } |
| 758 | } |
| 759 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 760 | field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*)); |
| 761 | if (LIKELY(field != NULL)) { |
| 762 | field->SetObj(NULL, new_value); |
| 763 | return 0; // success |
| 764 | } |
| 765 | return -1; // failure |
| 766 | } |
| 767 | |
| 768 | extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value, |
| 769 | const Method* referrer, Thread* self, Method** sp) { |
| 770 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t)); |
| 771 | if (LIKELY(field != NULL && obj != NULL)) { |
| 772 | field->Set32(obj, new_value); |
| 773 | return 0; // success |
| 774 | } |
| 775 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 776 | field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t)); |
| 777 | if (LIKELY(field != NULL)) { |
| 778 | if (UNLIKELY(obj == NULL)) { |
| 779 | ThrowNullPointerExceptionForFieldAccess(self, field, false); |
| 780 | } else { |
| 781 | field->Set32(obj, new_value); |
| 782 | return 0; // success |
| 783 | } |
| 784 | } |
| 785 | return -1; // failure |
| 786 | } |
| 787 | |
| 788 | extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value, |
| 789 | Thread* self, Method** sp) { |
| 790 | Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly); |
| 791 | Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)]; |
| 792 | Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t)); |
| 793 | if (LIKELY(field != NULL && obj != NULL)) { |
| 794 | field->Set64(obj, new_value); |
| 795 | return 0; // success |
| 796 | } |
| 797 | *sp = callee_save; |
| 798 | self->SetTopOfStack(sp, 0); |
| 799 | field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t)); |
| 800 | if (LIKELY(field != NULL)) { |
| 801 | if (UNLIKELY(obj == NULL)) { |
| 802 | ThrowNullPointerExceptionForFieldAccess(self, field, false); |
| 803 | } else { |
| 804 | field->Set64(obj, new_value); |
| 805 | return 0; // success |
| 806 | } |
| 807 | } |
| 808 | return -1; // failure |
| 809 | } |
| 810 | |
| 811 | extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value, |
| 812 | const Method* referrer, Thread* self, Method** sp) { |
| 813 | Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*)); |
| 814 | if (LIKELY(field != NULL && obj != NULL)) { |
| 815 | field->SetObj(obj, new_value); |
| 816 | return 0; // success |
| 817 | } |
| 818 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 819 | field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*)); |
| 820 | if (LIKELY(field != NULL)) { |
| 821 | if (UNLIKELY(obj == NULL)) { |
| 822 | ThrowNullPointerExceptionForFieldAccess(self, field, false); |
| 823 | } else { |
| 824 | field->SetObj(obj, new_value); |
| 825 | return 0; // success |
| 826 | } |
| 827 | } |
| 828 | return -1; // failure |
| 829 | } |
| 830 | |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 831 | extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method, |
| 832 | Thread* self, Method** sp) { |
| 833 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 834 | return AllocObjectFromCode(type_idx, method, self, false); |
| 835 | } |
| 836 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 837 | extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method, |
| 838 | Thread* self, Method** sp) { |
| 839 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 840 | return AllocObjectFromCode(type_idx, method, self, true); |
| 841 | } |
| 842 | |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 843 | extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 844 | Thread* self, Method** sp) { |
| 845 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 846 | return AllocArrayFromCode(type_idx, method, component_count, self, false); |
| 847 | } |
| 848 | |
| 849 | extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method, |
| 850 | int32_t component_count, |
| 851 | Thread* self, Method** sp) { |
| 852 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 853 | return AllocArrayFromCode(type_idx, method, component_count, self, true); |
| 854 | } |
| 855 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 856 | extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, |
| 857 | int32_t component_count, Thread* self, Method** sp) { |
| 858 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 859 | return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 862 | extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method, |
| 863 | int32_t component_count, |
| 864 | Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 865 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 866 | return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 869 | // Assignable test for code, won't throw. Null and equality tests already performed |
| 870 | uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) { |
| 871 | DCHECK(klass != NULL); |
| 872 | DCHECK(ref_class != NULL); |
| 873 | return klass->IsAssignableFrom(ref_class) ? 1 : 0; |
| 874 | } |
| 875 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 876 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 877 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 878 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 879 | DCHECK(b->IsClass()) << PrettyClass(b); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 880 | if (LIKELY(b->IsAssignableFrom(a))) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 881 | return 0; // Success |
| 882 | } else { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 883 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 884 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 885 | "%s cannot be cast to %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 886 | PrettyDescriptor(a).c_str(), |
| 887 | PrettyDescriptor(b).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 888 | return -1; // Failure |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 893 | // Returns 0 on success and -1 if an exception is pending. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 894 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class, |
| 895 | Thread* self, Method** sp) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 896 | DCHECK(array_class != NULL); |
| 897 | // element can't be NULL as we catch this is screened in runtime_support |
| 898 | Class* element_class = element->GetClass(); |
| 899 | Class* component_type = array_class->GetComponentType(); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 900 | if (LIKELY(component_type->IsAssignableFrom(element_class))) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 901 | return 0; // Success |
| 902 | } else { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 903 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 904 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | d3127d6 | 2012-01-17 13:42:26 -0800 | [diff] [blame] | 905 | "%s cannot be stored in an array of type %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 906 | PrettyDescriptor(element_class).c_str(), |
| 907 | PrettyDescriptor(array_class).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 908 | return -1; // Failure |
| 909 | } |
| 910 | } |
| 911 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 912 | extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer, |
| 913 | Thread* self, Method** sp) { |
| 914 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 915 | return ResolveVerifyAndClinit(type_idx, referrer, self, true, true); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 918 | extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self, |
| 919 | Method** sp) { |
| 920 | // Called when method->dex_cache_resolved_types_[] misses |
| 921 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 922 | return ResolveVerifyAndClinit(type_idx, referrer, self, false, false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 925 | extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx, |
| 926 | const Method* referrer, Thread* self, |
| 927 | Method** sp) { |
| 928 | // Called when caller isn't guaranteed to have access to a type and the dex cache may be |
| 929 | // unpopulated |
| 930 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | f3778f6 | 2012-01-26 14:14:35 -0800 | [diff] [blame] | 931 | return ResolveVerifyAndClinit(type_idx, referrer, self, false, true); |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 934 | extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx, |
| 935 | Thread* self, Method** sp) { |
| 936 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 937 | return ResolveStringFromCode(referrer, string_idx); |
| 938 | } |
| 939 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 940 | extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) { |
| 941 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 942 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 943 | // MonitorExit may throw exception |
| 944 | return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */; |
| 945 | } |
| 946 | |
| 947 | extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) { |
| 948 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
| 949 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 950 | obj->MonitorEnter(thread); // May block |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 951 | DCHECK(thread->HoldsLock(obj)); |
| 952 | // Only possible exception is NPE and is handled before entry |
| 953 | DCHECK(!thread->IsExceptionPending()); |
| 954 | } |
| 955 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 956 | void CheckSuspendFromCode(Thread* thread) { |
| 957 | // Called when thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 958 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 959 | } |
| 960 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 961 | extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) { |
| 962 | // Called when suspend count check value is 0 and thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 963 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 964 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 969 | * not of sufficient length. |
| 970 | * |
| 971 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 972 | * little-endian ordering. Require that oat2dex do any required swapping |
| 973 | * so this routine can get by with a memcpy(). |
| 974 | * |
| 975 | * Format of the data: |
| 976 | * ushort ident = 0x0300 magic value |
| 977 | * ushort width width of each element in the table |
| 978 | * uint size number of elements in the table |
| 979 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 980 | * padding at the end) |
| 981 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 982 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table, |
| 983 | Thread* self, Method** sp) { |
| 984 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 985 | DCHECK_EQ(table[0], 0x0300); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 986 | if (UNLIKELY(array == NULL)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 987 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 988 | "null array in fill array"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 989 | return -1; // Error |
| 990 | } |
| 991 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 992 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 993 | if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 994 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 995 | "failed array fill. length=%d; index=%d", array->GetLength(), size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 996 | return -1; // Error |
| 997 | } |
| 998 | uint16_t width = table[1]; |
| 999 | uint32_t size_in_bytes = size * width; |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1000 | memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1001 | return 0; // Success |
| 1002 | } |
| 1003 | |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 1004 | static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method, |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1005 | Thread* self, Method** sp, bool access_check, InvokeType type) { |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 1006 | Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type); |
| 1007 | if (UNLIKELY(method == NULL)) { |
| 1008 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 1009 | if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) { |
| 1010 | ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type); |
| 1011 | return 0; // failure |
| 1012 | } |
| 1013 | method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type); |
| 1014 | if (UNLIKELY(method == NULL)) { |
| 1015 | CHECK(self->IsExceptionPending()); |
| 1016 | return 0; // failure |
| 1017 | } |
| 1018 | } |
| 1019 | DCHECK(!self->IsExceptionPending()); |
| 1020 | const void* code = method->GetCode(); |
| 1021 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 1022 | // When we return, the caller will branch to this address, so it had better not be 0! |
| 1023 | CHECK(code != NULL) << PrettyMethod(method); |
| 1024 | |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 1025 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 1026 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 1027 | uint64_t result = ((code_uint << 32) | method_uint); |
| 1028 | return result; |
| 1029 | } |
| 1030 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1031 | // See comments in runtime_support_asm.S |
| 1032 | extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object, |
| 1033 | Method* caller_method, Thread* self, |
| 1034 | Method** sp) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1035 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, |
| 1039 | Object* this_object, |
| 1040 | Method* caller_method, Thread* self, |
| 1041 | Method** sp) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1042 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface); |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, |
| 1047 | Object* this_object, |
| 1048 | Method* caller_method, Thread* self, |
| 1049 | Method** sp) { |
| 1050 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect); |
| 1051 | } |
| 1052 | |
| 1053 | extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, |
| 1054 | Object* this_object, |
| 1055 | Method* caller_method, Thread* self, |
| 1056 | Method** sp) { |
| 1057 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, |
| 1061 | Object* this_object, |
| 1062 | Method* caller_method, Thread* self, |
| 1063 | Method** sp) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1064 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, |
| 1068 | Object* this_object, |
| 1069 | Method* caller_method, Thread* self, |
| 1070 | Method** sp) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1071 | return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1074 | static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) { |
| 1075 | ScopedLocalRef<jclass> jlr_UTE_class(env, |
| 1076 | env->FindClass("java/lang/reflect/UndeclaredThrowableException")); |
| 1077 | if (jlr_UTE_class.get() == NULL) { |
| 1078 | LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\""; |
| 1079 | } else { |
| 1080 | jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>", |
| 1081 | "(Ljava/lang/Throwable;)V"); |
| 1082 | jthrowable jexception = AddLocalReference<jthrowable>(env, exception); |
| 1083 | ScopedLocalRef<jthrowable> jlr_UTE(env, |
| 1084 | reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor, |
| 1085 | jexception))); |
| 1086 | int rc = env->Throw(jlr_UTE.get()); |
| 1087 | if (rc != JNI_OK) { |
| 1088 | LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\""; |
| 1089 | } |
| 1090 | } |
| 1091 | CHECK(self->IsExceptionPending()); |
| 1092 | } |
| 1093 | |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1094 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 1095 | // which is responsible for recording callee save registers. We explicitly handlerize incoming |
| 1096 | // reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke |
| 1097 | // the invocation handler which is a field within the proxy object receiver. |
| 1098 | extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver, |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1099 | Thread* self, byte* stack_args) { |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1100 | // Register the top of the managed stack |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1101 | Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12); |
| 1102 | DCHECK_EQ(*proxy_sp, proxy_method); |
| 1103 | self->SetTopOfStack(proxy_sp, 0); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1104 | // TODO: ARM specific |
| 1105 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u); |
| 1106 | // Start new JNI local reference state |
| 1107 | JNIEnvExt* env = self->GetJniEnv(); |
| 1108 | ScopedJniEnvLocalRefState env_state(env); |
| 1109 | // Create local ref. copies of proxy method and the receiver |
| 1110 | jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver); |
| 1111 | jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method); |
| 1112 | |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1113 | // Placing into local references incoming arguments from the caller's register arguments, |
| 1114 | // replacing original Object* with jobject |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1115 | MethodHelper proxy_mh(proxy_method); |
| 1116 | const size_t num_params = proxy_mh.NumArgs(); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1117 | size_t args_in_regs = 0; |
| 1118 | for (size_t i = 1; i < num_params; i++) { // skip receiver |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1119 | args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1120 | if (args_in_regs > 2) { |
| 1121 | args_in_regs = 2; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | size_t cur_arg = 0; // current stack location to read |
| 1126 | size_t param_index = 1; // skip receiver |
| 1127 | while (cur_arg < args_in_regs && param_index < num_params) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1128 | if (proxy_mh.IsParamAReference(param_index)) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1129 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1130 | jobject jobj = AddLocalReference<jobject>(env, obj); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1131 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1132 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1133 | cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1134 | param_index++; |
| 1135 | } |
| 1136 | // Placing into local references incoming arguments from the caller's stack arguments |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1137 | cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3 |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1138 | while (param_index < num_params) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1139 | if (proxy_mh.IsParamAReference(param_index)) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1140 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
| 1141 | jobject jobj = AddLocalReference<jobject>(env, obj); |
| 1142 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1143 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1144 | cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1145 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1146 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1147 | // Set up arguments array and place in local IRT during boxing (which may allocate/GC) |
| 1148 | jvalue args_jobj[3]; |
| 1149 | args_jobj[0].l = rcvr_jobj; |
| 1150 | args_jobj[1].l = proxy_method_jobj; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1151 | // Args array, if no arguments then NULL (don't include receiver in argument count) |
| 1152 | args_jobj[2].l = NULL; |
| 1153 | ObjectArray<Object>* args = NULL; |
| 1154 | if ((num_params - 1) > 0) { |
| 1155 | args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1156 | if (args == NULL) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1157 | CHECK(self->IsExceptionPending()); |
| 1158 | return; |
| 1159 | } |
| 1160 | args_jobj[2].l = AddLocalReference<jobjectArray>(env, args); |
| 1161 | } |
| 1162 | // Convert proxy method into expected interface method |
| 1163 | Method* interface_method = proxy_method->FindOverriddenMethod(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1164 | DCHECK(interface_method != NULL); |
| 1165 | DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1166 | args_jobj[1].l = AddLocalReference<jobject>(env, interface_method); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1167 | // Box arguments |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1168 | cur_arg = 0; // reset stack location to read to start |
| 1169 | // reset index, will index into param type array which doesn't include the receiver |
| 1170 | param_index = 0; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1171 | ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes(); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 1172 | if (param_types == NULL) { |
| 1173 | CHECK(self->IsExceptionPending()); |
| 1174 | return; |
| 1175 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1176 | // Check number of parameter types agrees with number from the Method - less 1 for the receiver. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1177 | DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1178 | while (cur_arg < args_in_regs && param_index < (num_params - 1)) { |
| 1179 | Class* param_type = param_types->Get(param_index); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1180 | Object* obj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1181 | if (!param_type->IsPrimitive()) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1182 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1183 | } else { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1184 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
| 1185 | if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) { |
| 1186 | // long/double split over regs and stack, mask in high half from stack arguments |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1187 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize)); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 1188 | val.j = (val.j & 0xffffffffULL) | (high_half << 32); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1189 | } |
Elliott Hughes | dbac309 | 2012-03-16 18:00:30 -0700 | [diff] [blame] | 1190 | BoxPrimitive(param_type->GetPrimitiveType(), val); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1191 | if (self->IsExceptionPending()) { |
| 1192 | return; |
| 1193 | } |
| 1194 | obj = val.l; |
| 1195 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1196 | args->Set(param_index, obj); |
| 1197 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 1198 | param_index++; |
| 1199 | } |
| 1200 | // Placing into local references incoming arguments from the caller's stack arguments |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1201 | cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3 |
| 1202 | while (param_index < (num_params - 1)) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1203 | Class* param_type = param_types->Get(param_index); |
| 1204 | Object* obj; |
| 1205 | if (!param_type->IsPrimitive()) { |
| 1206 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
| 1207 | } else { |
| 1208 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
Elliott Hughes | dbac309 | 2012-03-16 18:00:30 -0700 | [diff] [blame] | 1209 | BoxPrimitive(param_type->GetPrimitiveType(), val); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1210 | if (self->IsExceptionPending()) { |
| 1211 | return; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1212 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1213 | obj = val.l; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1214 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1215 | args->Set(param_index, obj); |
| 1216 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 1217 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1218 | } |
| 1219 | // Get the InvocationHandler method and the field that holds it within the Proxy object |
| 1220 | static jmethodID inv_hand_invoke_mid = NULL; |
| 1221 | static jfieldID proxy_inv_hand_fid = NULL; |
| 1222 | if (proxy_inv_hand_fid == NULL) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1223 | ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy")); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1224 | proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1225 | ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler")); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1226 | inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke", |
| 1227 | "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); |
| 1228 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1229 | DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy"))); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1230 | jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid); |
| 1231 | // Call InvocationHandler.invoke |
| 1232 | jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj); |
| 1233 | // Place result in stack args |
| 1234 | if (!self->IsExceptionPending()) { |
| 1235 | Object* result_ref = self->DecodeJObject(result); |
| 1236 | if (result_ref != NULL) { |
| 1237 | JValue result_unboxed; |
Elliott Hughes | dbac309 | 2012-03-16 18:00:30 -0700 | [diff] [blame] | 1238 | bool unboxed_okay = UnboxPrimitive(result_ref, proxy_mh.GetReturnType(), result_unboxed, "result"); |
Elliott Hughes | 051c9fc | 2012-03-21 12:24:55 -0700 | [diff] [blame] | 1239 | if (!unboxed_okay) { |
| 1240 | self->ClearException(); |
| 1241 | self->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
| 1242 | "Couldn't convert result of type %s to %s", |
| 1243 | PrettyTypeOf(result_ref).c_str(), |
| 1244 | PrettyDescriptor(proxy_mh.GetReturnType()).c_str()); |
| 1245 | return; |
| 1246 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1247 | *reinterpret_cast<JValue*>(stack_args) = result_unboxed; |
| 1248 | } else { |
| 1249 | *reinterpret_cast<jobject*>(stack_args) = NULL; |
| 1250 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1251 | } else { |
| 1252 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 1253 | // a UndeclaredThrowableException. |
| 1254 | Throwable* exception = self->GetException(); |
| 1255 | self->ClearException(); |
| 1256 | if (!exception->IsCheckedException()) { |
| 1257 | self->SetException(exception); |
| 1258 | } else { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 1259 | SynthesizedProxyClass* proxy_class = |
| 1260 | down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass()); |
| 1261 | int throws_index = -1; |
| 1262 | size_t num_virt_methods = proxy_class->NumVirtualMethods(); |
| 1263 | for (size_t i = 0; i < num_virt_methods; i++) { |
| 1264 | if (proxy_class->GetVirtualMethod(i) == proxy_method) { |
| 1265 | throws_index = i; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | CHECK_NE(throws_index, -1); |
| 1270 | ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1271 | Class* exception_class = exception->GetClass(); |
| 1272 | bool declares_exception = false; |
| 1273 | for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
| 1274 | Class* declared_exception = declared_exceptions->Get(i); |
| 1275 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 1276 | } |
| 1277 | if (declares_exception) { |
| 1278 | self->SetException(exception); |
| 1279 | } else { |
| 1280 | ThrowNewUndeclaredThrowableException(self, env, exception); |
| 1281 | } |
| 1282 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1286 | extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1287 | Trace* tracer = Runtime::Current()->GetTracer(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1288 | TraceStackFrame trace_frame = TraceStackFrame(method, lr); |
| 1289 | self->PushTraceStackFrame(trace_frame); |
| 1290 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1291 | tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 1292 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1293 | return tracer->GetSavedCodeFromMap(method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | extern "C" uintptr_t artTraceMethodExitFromCode() { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1297 | Trace* tracer = Runtime::Current()->GetTracer(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1298 | TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame(); |
| 1299 | Method* method = trace_frame.method_; |
| 1300 | uintptr_t lr = trace_frame.return_pc_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 1301 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1302 | tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1303 | |
| 1304 | return lr; |
| 1305 | } |
| 1306 | |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 1307 | uint32_t TraceMethodUnwindFromCode(Thread* self) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1308 | Trace* tracer = Runtime::Current()->GetTracer(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1309 | TraceStackFrame trace_frame = self->PopTraceStackFrame(); |
| 1310 | Method* method = trace_frame.method_; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 1311 | uint32_t lr = trace_frame.return_pc_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 1312 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 1313 | tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 1314 | |
| 1315 | return lr; |
| 1316 | } |
| 1317 | |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 1318 | int CmplFloat(float a, float b) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1319 | if (a == b) { |
| 1320 | return 0; |
| 1321 | } else if (a < b) { |
| 1322 | return -1; |
| 1323 | } else if (a > b) { |
| 1324 | return 1; |
| 1325 | } |
| 1326 | return -1; |
| 1327 | } |
| 1328 | |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 1329 | int CmpgFloat(float a, float b) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1330 | if (a == b) { |
| 1331 | return 0; |
| 1332 | } else if (a < b) { |
| 1333 | return -1; |
| 1334 | } else if (a > b) { |
| 1335 | return 1; |
| 1336 | } |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 1340 | int CmpgDouble(double a, double b) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1341 | if (a == b) { |
| 1342 | return 0; |
| 1343 | } else if (a < b) { |
| 1344 | return -1; |
| 1345 | } else if (a > b) { |
| 1346 | return 1; |
| 1347 | } |
| 1348 | return 1; |
| 1349 | } |
| 1350 | |
Elliott Hughes | 0ece7b9 | 2012-03-09 18:14:40 -0800 | [diff] [blame] | 1351 | int CmplDouble(double a, double b) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1352 | if (a == b) { |
| 1353 | return 0; |
| 1354 | } else if (a < b) { |
| 1355 | return -1; |
| 1356 | } else if (a > b) { |
| 1357 | return 1; |
| 1358 | } |
| 1359 | return -1; |
| 1360 | } |
| 1361 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1362 | /* |
| 1363 | * Float/double conversion requires clamping to min and max of integer form. If |
| 1364 | * target doesn't support this normally, use these. |
| 1365 | */ |
| 1366 | int64_t D2L(double d) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1367 | static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL; |
| 1368 | static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL; |
| 1369 | if (d >= kMaxLong) { |
| 1370 | return (int64_t) 0x7fffffffffffffffULL; |
| 1371 | } else if (d <= kMinLong) { |
| 1372 | return (int64_t) 0x8000000000000000ULL; |
| 1373 | } else if (d != d) { // NaN case |
| 1374 | return 0; |
| 1375 | } else { |
| 1376 | return (int64_t) d; |
| 1377 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | int64_t F2L(float f) { |
Ian Rogers | a433b2e | 2012-03-09 08:40:33 -0800 | [diff] [blame] | 1381 | static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL; |
| 1382 | static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL; |
| 1383 | if (f >= kMaxLong) { |
| 1384 | return (int64_t) 0x7fffffffffffffffULL; |
| 1385 | } else if (f <= kMinLong) { |
| 1386 | return (int64_t) 0x8000000000000000ULL; |
| 1387 | } else if (f != f) { // NaN case |
| 1388 | return 0; |
| 1389 | } else { |
| 1390 | return (int64_t) f; |
| 1391 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | } // namespace art |