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