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