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