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