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