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