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