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