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