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