Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "runtime_support.h" |
| 18 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 19 | #include "dex_cache.h" |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 20 | #include "dex_verifier.h" |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 21 | #include "macros.h" |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 22 | #include "reflection.h" |
| 23 | #include "ScopedLocalRef.h" |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 24 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 27 | // Place a special frame at the TOS that will save the callee saves for the given type |
| 28 | static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 29 | // Be aware the store below may well stomp on an incoming argument |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 30 | *sp = Runtime::Current()->GetCalleeSaveMethod(type); |
| 31 | self->SetTopOfStack(sp, 0); |
| 32 | } |
| 33 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 34 | // Temporary debugging hook for compiler. |
| 35 | extern void DebugMe(Method* method, uint32_t info) { |
| 36 | LOG(INFO) << "DebugMe"; |
| 37 | if (method != NULL) { |
| 38 | LOG(INFO) << PrettyMethod(method); |
| 39 | } |
| 40 | LOG(INFO) << "Info: " << info; |
| 41 | } |
| 42 | |
Brian Carlstrom | 6fd03fb | 2011-10-17 16:11:00 -0700 | [diff] [blame] | 43 | extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) { |
| 44 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 45 | Class* c = o->GetClass(); |
| 46 | if (UNLIKELY(c->IsFinalizable())) { |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 47 | Heap::AddFinalizerReference(self, o); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 48 | } |
| 49 | /* |
| 50 | * NOTE: once debugger/profiler support is added, we'll need to check |
| 51 | * here and branch to actual compiled object.<init> to handle any |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 52 | * breakpoint/logging activities if either is active. |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 53 | */ |
Brian Carlstrom | 6fd03fb | 2011-10-17 16:11:00 -0700 | [diff] [blame] | 54 | return self->IsExceptionPending() ? -1 : 0; |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 57 | // Return value helper for jobject return types |
| 58 | extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
Brian Carlstrom | 6f495f2 | 2011-10-10 15:05:03 -0700 | [diff] [blame] | 59 | if (thread->IsExceptionPending()) { |
| 60 | return NULL; |
| 61 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 62 | return thread->DecodeJObject(obj); |
| 63 | } |
| 64 | |
| 65 | extern void* FindNativeMethod(Thread* thread) { |
| 66 | DCHECK(Thread::Current() == thread); |
| 67 | |
| 68 | Method* method = const_cast<Method*>(thread->GetCurrentMethod()); |
| 69 | DCHECK(method != NULL); |
| 70 | |
| 71 | // Lookup symbol address for method, on failure we'll return NULL with an |
| 72 | // exception set, otherwise we return the address of the method we found. |
| 73 | void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method); |
| 74 | if (native_code == NULL) { |
| 75 | DCHECK(thread->IsExceptionPending()); |
| 76 | return NULL; |
| 77 | } else { |
| 78 | // Register so that future calls don't come here |
| 79 | method->RegisterNative(native_code); |
| 80 | return native_code; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Called by generated call to throw an exception |
| 85 | extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) { |
| 86 | /* |
| 87 | * exception may be NULL, in which case this routine should |
| 88 | * throw NPE. NOTE: this is a convenience for generated code, |
| 89 | * which previously did the null check inline and constructed |
| 90 | * and threw a NPE if NULL. This routine responsible for setting |
| 91 | * exception_ in thread and delivering the exception. |
| 92 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 93 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 94 | if (exception == NULL) { |
| 95 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception"); |
| 96 | } else { |
| 97 | thread->SetException(exception); |
| 98 | } |
| 99 | thread->DeliverException(); |
| 100 | } |
| 101 | |
| 102 | // Deliver an exception that's pending on thread helping set up a callee save frame on the way |
| 103 | extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 104 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 105 | thread->DeliverException(); |
| 106 | } |
| 107 | |
| 108 | // Called by generated call to throw a NPE exception |
| 109 | extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 110 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 111 | thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 112 | thread->DeliverException(); |
| 113 | } |
| 114 | |
| 115 | // Called by generated call to throw an arithmetic divide by zero exception |
| 116 | extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 117 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 118 | thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero"); |
| 119 | thread->DeliverException(); |
| 120 | } |
| 121 | |
| 122 | // Called by generated call to throw an arithmetic divide by zero exception |
| 123 | extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 124 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
| 125 | thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 126 | "length=%d; index=%d", limit, index); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 127 | thread->DeliverException(); |
| 128 | } |
| 129 | |
| 130 | // Called by the AbstractMethodError stub (not runtime support) |
| 131 | extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 132 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
| 133 | thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", |
| 134 | "abstract method \"%s\"", PrettyMethod(method).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 135 | thread->DeliverException(); |
| 136 | } |
| 137 | |
| 138 | extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 139 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 140 | thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 141 | thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;", |
| 142 | "stack size %zdkb; default stack size: %zdkb", |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 143 | thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 144 | thread->ResetDefaultStackEnd(); // Return to default stack size |
| 145 | thread->DeliverException(); |
| 146 | } |
| 147 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 148 | static std::string ClassNameFromIndex(Method* method, uint32_t ref, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 149 | verifier::VerifyErrorRefType ref_type, bool access) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 150 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 151 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 152 | |
| 153 | uint16_t type_idx = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 154 | if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 155 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
| 156 | type_idx = id.class_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 157 | } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 158 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
| 159 | type_idx = id.class_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 160 | } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 161 | type_idx = ref; |
| 162 | } else { |
| 163 | CHECK(false) << static_cast<int>(ref_type); |
| 164 | } |
| 165 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 166 | std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx))); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 167 | if (!access) { |
| 168 | return class_name; |
| 169 | } |
| 170 | |
| 171 | std::string result; |
| 172 | result += "tried to access class "; |
| 173 | result += class_name; |
| 174 | result += " from class "; |
| 175 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 176 | return result; |
| 177 | } |
| 178 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 179 | static std::string FieldNameFromIndex(const Method* method, uint32_t ref, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 180 | verifier::VerifyErrorRefType ref_type, bool access) { |
| 181 | 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] | 182 | |
| 183 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 184 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 185 | |
| 186 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 187 | std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 188 | const char* field_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 189 | if (!access) { |
| 190 | return class_name + "." + field_name; |
| 191 | } |
| 192 | |
| 193 | std::string result; |
| 194 | result += "tried to access field "; |
| 195 | result += class_name + "." + field_name; |
| 196 | result += " from class "; |
| 197 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 198 | return result; |
| 199 | } |
| 200 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 201 | static std::string MethodNameFromIndex(const Method* method, uint32_t ref, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 202 | verifier::VerifyErrorRefType ref_type, bool access) { |
| 203 | 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] | 204 | |
| 205 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 206 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 207 | |
| 208 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 209 | std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 210 | const char* method_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 211 | if (!access) { |
| 212 | return class_name + "." + method_name; |
| 213 | } |
| 214 | |
| 215 | std::string result; |
| 216 | result += "tried to access method "; |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 217 | result += class_name + "." + method_name + ":" + |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 218 | dex_file.CreateMethodSignature(id.proto_idx_, NULL); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 219 | result += " from class "; |
| 220 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | 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] | 225 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 226 | 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] | 227 | frame.Next(); |
| 228 | Method* method = frame.GetMethod(); |
| 229 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 230 | verifier::VerifyErrorRefType ref_type = |
| 231 | static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 232 | |
| 233 | const char* exception_class = "Ljava/lang/VerifyError;"; |
| 234 | std::string msg; |
| 235 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 236 | switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) { |
| 237 | case verifier::VERIFY_ERROR_NO_CLASS: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 238 | exception_class = "Ljava/lang/NoClassDefFoundError;"; |
| 239 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 240 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 241 | case verifier::VERIFY_ERROR_NO_FIELD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 242 | exception_class = "Ljava/lang/NoSuchFieldError;"; |
| 243 | msg = FieldNameFromIndex(method, ref, ref_type, false); |
| 244 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 245 | case verifier::VERIFY_ERROR_NO_METHOD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 246 | exception_class = "Ljava/lang/NoSuchMethodError;"; |
| 247 | msg = MethodNameFromIndex(method, ref, ref_type, false); |
| 248 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 249 | case verifier::VERIFY_ERROR_ACCESS_CLASS: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 250 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 251 | msg = ClassNameFromIndex(method, ref, ref_type, true); |
| 252 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 253 | case verifier::VERIFY_ERROR_ACCESS_FIELD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 254 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 255 | msg = FieldNameFromIndex(method, ref, ref_type, true); |
| 256 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 257 | case verifier::VERIFY_ERROR_ACCESS_METHOD: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 258 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 259 | msg = MethodNameFromIndex(method, ref, ref_type, true); |
| 260 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 261 | case verifier::VERIFY_ERROR_CLASS_CHANGE: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 262 | exception_class = "Ljava/lang/IncompatibleClassChangeError;"; |
| 263 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 264 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 265 | case verifier::VERIFY_ERROR_INSTANTIATION: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 266 | exception_class = "Ljava/lang/InstantiationError;"; |
| 267 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 268 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 269 | case verifier::VERIFY_ERROR_GENERIC: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 270 | // Generic VerifyError; use default exception, no message. |
| 271 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 272 | case verifier::VERIFY_ERROR_NONE: |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 273 | CHECK(false); |
| 274 | break; |
| 275 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 276 | self->ThrowNewException(exception_class, msg.c_str()); |
| 277 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 281 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 282 | LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 283 | thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 284 | thread->DeliverException(); |
| 285 | } |
| 286 | |
| 287 | extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 288 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 289 | LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 290 | thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 291 | thread->DeliverException(); |
| 292 | } |
| 293 | |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 294 | extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 295 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 296 | Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 297 | frame.Next(); |
| 298 | Method* method = frame.GetMethod(); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 299 | self->ThrowNewException("Ljava/lang/NoSuchMethodError;", |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 300 | MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str()); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 301 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 305 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 306 | LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode"; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 307 | thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 308 | thread->DeliverException(); |
| 309 | } |
| 310 | |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 311 | void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread, |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 312 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 313 | // TODO: this code is specific to ARM |
| 314 | // On entry the stack pointed by sp is: |
| 315 | // | argN | | |
| 316 | // | ... | | |
| 317 | // | arg4 | | |
| 318 | // | arg3 spill | | Caller's frame |
| 319 | // | arg2 spill | | |
| 320 | // | arg1 spill | | |
| 321 | // | Method* | --- |
| 322 | // | LR | |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 323 | // | ... | callee saves |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 324 | // | R3 | arg3 |
| 325 | // | R2 | arg2 |
| 326 | // | R1 | arg1 |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 327 | // | R0 | |
| 328 | // | Method* | <- sp |
| 329 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); |
| 330 | DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 331 | Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48); |
| 332 | uintptr_t caller_pc = regs[10]; |
| 333 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 334 | // Start new JNI local reference state |
| 335 | JNIEnvExt* env = thread->GetJniEnv(); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 336 | ScopedJniEnvLocalRefState env_state(env); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 337 | // Discover shorty (avoid GCs) |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 338 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 339 | const char* shorty = linker->MethodShorty(method_idx, *caller_sp); |
| 340 | size_t shorty_len = strlen(shorty); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 341 | size_t args_in_regs = 0; |
| 342 | for (size_t i = 1; i < shorty_len; i++) { |
| 343 | char c = shorty[i]; |
| 344 | args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1); |
| 345 | if (args_in_regs > 3) { |
| 346 | args_in_regs = 3; |
| 347 | break; |
| 348 | } |
| 349 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 350 | bool is_static; |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 351 | if (type == Runtime::kUnknownMethod) { |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 352 | Method* caller = *caller_sp; |
Ian Rogers | df9a782 | 2011-10-11 16:53:22 -0700 | [diff] [blame] | 353 | // less two as return address may span into next dex instruction |
| 354 | uint32_t dex_pc = caller->ToDexPC(caller_pc - 2); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 355 | const DexFile& dex_file = Runtime::Current()->GetClassLinker() |
| 356 | ->FindDexFile(caller->GetDeclaringClass()->GetDexCache()); |
| 357 | const DexFile::CodeItem* code = dex_file.GetCodeItem(caller->GetCodeItemOffset()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 358 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 359 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 360 | Instruction::Code instr_code = instr->Opcode(); |
| 361 | is_static = (instr_code == Instruction::INVOKE_STATIC) || |
| 362 | (instr_code == Instruction::INVOKE_STATIC_RANGE); |
| 363 | DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) || |
| 364 | (instr_code == Instruction::INVOKE_DIRECT_RANGE)); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 365 | } else { |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 366 | is_static = type == Runtime::kStaticMethod; |
| 367 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 368 | // Place into local references incoming arguments from the caller's register arguments |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 369 | 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] | 370 | if (!is_static) { |
| 371 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 372 | cur_arg++; |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 373 | if (args_in_regs < 3) { |
| 374 | // If we thought we had fewer than 3 arguments in registers, account for the receiver |
| 375 | args_in_regs++; |
| 376 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 377 | AddLocalReference<jobject>(env, obj); |
| 378 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 379 | size_t shorty_index = 1; // skip return value |
| 380 | // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip |
| 381 | // R0) |
| 382 | while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) { |
| 383 | char c = shorty[shorty_index]; |
| 384 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 385 | if (c == 'L') { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 386 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 387 | AddLocalReference<jobject>(env, obj); |
| 388 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 389 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
| 390 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 391 | // Place into local references incoming arguments from the caller's stack arguments |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 392 | 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] | 393 | while (shorty_index < shorty_len) { |
| 394 | char c = shorty[shorty_index]; |
| 395 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 396 | if (c == 'L') { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 397 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 398 | AddLocalReference<jobject>(env, obj); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 399 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 400 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 401 | } |
| 402 | // Resolve method filling in dex cache |
| 403 | Method* called = linker->ResolveMethod(method_idx, *caller_sp, true); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 404 | if (LIKELY(!thread->IsExceptionPending())) { |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 405 | // Update CodeAndDirectMethod table |
| 406 | Method* caller = *caller_sp; |
| 407 | DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache(); |
| 408 | dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 409 | // We got this far, ensure that the declaring class is initialized |
| 410 | linker->EnsureInitialized(called->GetDeclaringClass(), true); |
| 411 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 412 | void* code; |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 413 | if (UNLIKELY(thread->IsExceptionPending())) { |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 414 | // Something went wrong in ResolveMethod or EnsureInitialized, |
| 415 | // go into deliver exception with the pending exception in r0 |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 416 | code = reinterpret_cast<void*>(art_deliver_exception_from_code); |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 417 | regs[0] = reinterpret_cast<uintptr_t>(thread->GetException()); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 418 | thread->ClearException(); |
| 419 | } else { |
| 420 | // Expect class to at least be initializing |
| 421 | CHECK(called->GetDeclaringClass()->IsInitializing()); |
| 422 | // Set up entry into main method |
Brian Carlstrom | b2062cf | 2011-11-03 01:24:44 -0700 | [diff] [blame] | 423 | regs[0] = reinterpret_cast<uintptr_t>(called); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 424 | code = const_cast<void*>(called->GetCode()); |
| 425 | } |
| 426 | return code; |
| 427 | } |
| 428 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 429 | // Fast path field resolution that can't throw exceptions |
| 430 | static Field* FindFieldFast(uint32_t field_idx, const Method* referrer) { |
| 431 | Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx); |
| 432 | if (UNLIKELY(resolved_field == NULL)) { |
| 433 | return NULL; |
| 434 | } |
| 435 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 436 | // Check class is initilaized or initializing |
| 437 | if (UNLIKELY(!fields_class->IsInitializing())) { |
| 438 | return NULL; |
| 439 | } |
| 440 | return resolved_field; |
| 441 | } |
| 442 | |
| 443 | // Slow path field resolution and declaring class initialization |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 444 | Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) { |
| 445 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 446 | Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
| 447 | if (LIKELY(resolved_field != NULL)) { |
| 448 | Class* fields_class = resolved_field->GetDeclaringClass(); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 449 | // If the class is already initializing, we must be inside <clinit>, or |
| 450 | // we'd still be waiting for the lock. |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 451 | if (fields_class->IsInitializing()) { |
| 452 | return resolved_field; |
| 453 | } |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 454 | if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 455 | return resolved_field; |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer, |
| 463 | Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 464 | Field* resolved_field = FindFieldFast(field_idx, referrer); |
| 465 | if (UNLIKELY(resolved_field == NULL)) { |
| 466 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 467 | resolved_field = FindFieldFromCode(field_idx, referrer, false); |
| 468 | } |
| 469 | return resolved_field; |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 473 | Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 474 | Field* field = FindFieldFast(field_idx, referrer); |
| 475 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 476 | if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int32_t))) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 477 | return field->Get32(NULL); |
| 478 | } |
| 479 | } |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 480 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 481 | field = FindFieldFromCode(field_idx, referrer, true); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 482 | if (field != NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 483 | if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int32_t)) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 484 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 485 | "Attempted read of 32-bit primitive on field '%s'", |
| 486 | PrettyField(field, true).c_str()); |
| 487 | } else { |
| 488 | return field->Get32(NULL); |
| 489 | } |
| 490 | } |
| 491 | return 0; // Will throw exception by checking with Thread::Current |
| 492 | } |
| 493 | |
| 494 | extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 495 | Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 496 | Field* field = FindFieldFast(field_idx, referrer); |
| 497 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 498 | if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int64_t))) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 499 | return field->Get64(NULL); |
| 500 | } |
| 501 | } |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 502 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 503 | field = FindFieldFromCode(field_idx, referrer, true); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 504 | if (field != NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 505 | if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int64_t)) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 506 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 507 | "Attempted read of 64-bit primitive on field '%s'", |
| 508 | PrettyField(field, true).c_str()); |
| 509 | } else { |
| 510 | return field->Get64(NULL); |
| 511 | } |
| 512 | } |
| 513 | return 0; // Will throw exception by checking with Thread::Current |
| 514 | } |
| 515 | |
| 516 | extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 517 | Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 518 | Field* field = FindFieldFast(field_idx, referrer); |
| 519 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 520 | if (LIKELY(!field->IsPrimitiveType())) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 521 | return field->GetObj(NULL); |
| 522 | } |
| 523 | } |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 524 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 525 | field = FindFieldFromCode(field_idx, referrer, true); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 526 | if (field != NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 527 | if (field->IsPrimitiveType()) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 528 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 529 | "Attempted read of reference on primitive field '%s'", |
| 530 | PrettyField(field, true).c_str()); |
| 531 | } else { |
| 532 | return field->GetObj(NULL); |
| 533 | } |
| 534 | } |
| 535 | return NULL; // Will throw exception by checking with Thread::Current |
| 536 | } |
| 537 | |
| 538 | extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 539 | uint32_t new_value, Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 540 | Field* field = FindFieldFast(field_idx, referrer); |
| 541 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 542 | if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int32_t))) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 543 | field->Set32(NULL, new_value); |
| 544 | return 0; // success |
| 545 | } |
| 546 | } |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 547 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 548 | field = FindFieldFromCode(field_idx, referrer, true); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 549 | if (field != NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 550 | if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int32_t)) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 551 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 552 | "Attempted write of 32-bit primitive to field '%s'", |
| 553 | PrettyField(field, true).c_str()); |
| 554 | } else { |
| 555 | field->Set32(NULL, new_value); |
| 556 | return 0; // success |
| 557 | } |
| 558 | } |
| 559 | return -1; // failure |
| 560 | } |
| 561 | |
| 562 | extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 563 | uint64_t new_value, Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 564 | Field* field = FindFieldFast(field_idx, referrer); |
| 565 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 566 | if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int64_t))) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 567 | field->Set64(NULL, new_value); |
| 568 | return 0; // success |
| 569 | } |
| 570 | } |
| 571 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 572 | field = FindFieldFromCode(field_idx, referrer, true); |
| 573 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 574 | if (UNLIKELY(!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int64_t))) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 575 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 576 | "Attempted write of 64-bit primitive to field '%s'", |
| 577 | PrettyField(field, true).c_str()); |
| 578 | } else { |
| 579 | field->Set64(NULL, new_value); |
| 580 | return 0; // success |
| 581 | } |
| 582 | } |
| 583 | return -1; // failure |
| 584 | } |
| 585 | |
| 586 | extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 587 | Object* new_value, Thread* self, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 588 | Field* field = FindFieldFast(field_idx, referrer); |
| 589 | if (LIKELY(field != NULL)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 590 | if (LIKELY(!field->IsPrimitiveType())) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 591 | field->SetObj(NULL, new_value); |
| 592 | return 0; // success |
| 593 | } |
| 594 | } |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 595 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 596 | field = FindFieldFromCode(field_idx, referrer, true); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 597 | if (field != NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 598 | if (field->IsPrimitiveType()) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 599 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 600 | "Attempted write of reference to primitive field '%s'", |
| 601 | PrettyField(field, true).c_str()); |
| 602 | } else { |
| 603 | field->SetObj(NULL, new_value); |
| 604 | return 0; // success |
| 605 | } |
| 606 | } |
| 607 | return -1; // failure |
| 608 | } |
| 609 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 610 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 611 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 612 | extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method, |
| 613 | Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 614 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 615 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 616 | Runtime* runtime = Runtime::Current(); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 617 | if (UNLIKELY(klass == NULL)) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 618 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 619 | if (klass == NULL) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 620 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 621 | return NULL; // Failure |
| 622 | } |
| 623 | } |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 624 | if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) { |
| 625 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 626 | return NULL; // Failure |
| 627 | } |
| 628 | return klass->AllocObject(); |
| 629 | } |
| 630 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 631 | extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method, |
| 632 | Thread* self, Method** sp) { |
| 633 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 634 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 635 | Runtime* runtime = Runtime::Current(); |
| 636 | if (UNLIKELY(klass == NULL)) { |
| 637 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
| 638 | if (klass == NULL) { |
| 639 | DCHECK(self->IsExceptionPending()); |
| 640 | return NULL; // Failure |
| 641 | } |
| 642 | } |
| 643 | Class* referrer = method->GetDeclaringClass(); |
| 644 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 645 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", "illegal class access: '%s' -> '%s'", |
| 646 | PrettyDescriptor(referrer->GetDescriptor()).c_str(), |
| 647 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 648 | return NULL; // Failure |
| 649 | } |
| 650 | if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) { |
| 651 | DCHECK(self->IsExceptionPending()); |
| 652 | return NULL; // Failure |
| 653 | } |
| 654 | return klass->AllocObject(); |
buzbee | cc4540e | 2011-10-27 13:06:03 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 657 | Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 658 | Thread* self) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 659 | if (UNLIKELY(component_count < 0)) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 660 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 661 | return NULL; // Failure |
| 662 | } |
| 663 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 664 | 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] | 665 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 666 | if (klass == NULL) { // Error |
| 667 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 668 | return NULL; // Failure |
| 669 | } |
| 670 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 671 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 672 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 673 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 674 | "Bad filled array request for type %s", |
| 675 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 676 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 677 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 678 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 679 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 680 | } |
| 681 | return NULL; // Failure |
| 682 | } else { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 683 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 684 | return Array::Alloc(klass, component_count); |
| 685 | } |
| 686 | } |
| 687 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 688 | // Helper function to alloc array for OP_FILLED_NEW_ARRAY |
| 689 | extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, |
| 690 | int32_t component_count, Thread* self, Method** sp) { |
| 691 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 692 | return CheckAndAllocArrayFromCode(type_idx, method, component_count, self); |
| 693 | } |
| 694 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 695 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 696 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 697 | extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 698 | Thread* self, Method** sp) { |
| 699 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 700 | if (UNLIKELY(component_count < 0)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 701 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 702 | component_count); |
| 703 | return NULL; // Failure |
| 704 | } |
| 705 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 706 | 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] | 707 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 708 | if (klass == NULL) { // Error |
| 709 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 710 | return NULL; // Failure |
| 711 | } |
| 712 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 713 | } |
| 714 | return Array::Alloc(klass, component_count); |
| 715 | } |
| 716 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 717 | // Assignable test for code, won't throw. Null and equality tests already performed |
| 718 | uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) { |
| 719 | DCHECK(klass != NULL); |
| 720 | DCHECK(ref_class != NULL); |
| 721 | return klass->IsAssignableFrom(ref_class) ? 1 : 0; |
| 722 | } |
| 723 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 724 | // 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] | 725 | 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] | 726 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 727 | DCHECK(b->IsClass()) << PrettyClass(b); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 728 | if (LIKELY(b->IsAssignableFrom(a))) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 729 | return 0; // Success |
| 730 | } else { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 731 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 732 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 733 | "%s cannot be cast to %s", |
| 734 | PrettyDescriptor(a->GetDescriptor()).c_str(), |
| 735 | PrettyDescriptor(b->GetDescriptor()).c_str()); |
| 736 | return -1; // Failure |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 741 | // Returns 0 on success and -1 if an exception is pending. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 742 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class, |
| 743 | Thread* self, Method** sp) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 744 | DCHECK(array_class != NULL); |
| 745 | // element can't be NULL as we catch this is screened in runtime_support |
| 746 | Class* element_class = element->GetClass(); |
| 747 | Class* component_type = array_class->GetComponentType(); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 748 | if (LIKELY(component_type->IsAssignableFrom(element_class))) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 749 | return 0; // Success |
| 750 | } else { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 751 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 752 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 753 | "Cannot store an object of type %s in to an array of type %s", |
| 754 | PrettyDescriptor(element_class->GetDescriptor()).c_str(), |
| 755 | PrettyDescriptor(array_class->GetDescriptor()).c_str()); |
| 756 | return -1; // Failure |
| 757 | } |
| 758 | } |
| 759 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 760 | Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) { |
| 761 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 762 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 763 | if (UNLIKELY(klass == NULL)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 764 | CHECK(self->IsExceptionPending()); |
| 765 | return NULL; // Failure - Indicate to caller to deliver exception |
| 766 | } |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 767 | DCHECK(referrer->GetDeclaringClass()->CanAccess(klass)); |
| 768 | // If we are the <clinit> of this class, just return our storage. |
| 769 | // |
| 770 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 771 | // running. |
| 772 | if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) { |
| 773 | return klass; |
| 774 | } |
| 775 | if (!class_linker->EnsureInitialized(klass, true)) { |
| 776 | CHECK(self->IsExceptionPending()); |
| 777 | return NULL; // Failure - Indicate to caller to deliver exception |
| 778 | } |
| 779 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 780 | return klass; |
| 781 | } |
| 782 | |
| 783 | Class* InitializeStaticStorageAndVerifyAccess(uint32_t type_idx, const Method* referrer, |
| 784 | Thread* self) { |
| 785 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 786 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 787 | if (UNLIKELY(klass == NULL)) { |
| 788 | CHECK(self->IsExceptionPending()); |
| 789 | return NULL; // Failure - Indicate to caller to deliver exception |
| 790 | } |
| 791 | // Perform access check |
| 792 | if (UNLIKELY(!referrer->GetDeclaringClass()->CanAccess(klass))) { |
| 793 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 794 | "Class %s is inaccessible to method %s", |
| 795 | PrettyDescriptor(klass->GetDescriptor()).c_str(), |
| 796 | PrettyMethod(referrer, true).c_str()); |
| 797 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 798 | // If we are the <clinit> of this class, just return our storage. |
| 799 | // |
| 800 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 801 | // running. |
| 802 | if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) { |
| 803 | return klass; |
| 804 | } |
| 805 | if (!class_linker->EnsureInitialized(klass, true)) { |
| 806 | CHECK(self->IsExceptionPending()); |
| 807 | return NULL; // Failure - Indicate to caller to deliver exception |
| 808 | } |
| 809 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 810 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 813 | extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer, |
| 814 | Thread* self, Method** sp) { |
| 815 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 816 | return InitializeStaticStorage(type_idx, referrer, self); |
| 817 | } |
| 818 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 819 | extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self, |
| 820 | Method** sp) { |
| 821 | // Called when method->dex_cache_resolved_types_[] misses |
| 822 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 823 | return InitializeStaticStorage(type_idx, referrer, self); |
| 824 | } |
| 825 | |
Ian Rogers | b093c6b | 2011-10-31 16:19:55 -0700 | [diff] [blame] | 826 | extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx, |
| 827 | const Method* referrer, Thread* self, |
| 828 | Method** sp) { |
| 829 | // Called when caller isn't guaranteed to have access to a type and the dex cache may be |
| 830 | // unpopulated |
| 831 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 832 | return InitializeStaticStorageAndVerifyAccess(type_idx, referrer, self); |
| 833 | } |
| 834 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 835 | // TODO: placeholder. Helper function to resolve virtual method |
| 836 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
| 837 | /* |
| 838 | * Slow-path handler on invoke virtual method path in which |
| 839 | * base method is unresolved at compile-time. Doesn't need to |
| 840 | * return anything - just either ensure that |
| 841 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 842 | * throw and unwind. The caller will restart call sequence |
| 843 | * from the beginning. |
| 844 | */ |
| 845 | } |
| 846 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 847 | String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) { |
| 848 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 849 | return class_linker->ResolveString(string_idx, referrer); |
| 850 | } |
| 851 | |
| 852 | extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx, |
| 853 | Thread* self, Method** sp) { |
| 854 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 855 | return ResolveStringFromCode(referrer, string_idx); |
| 856 | } |
| 857 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 858 | extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) { |
| 859 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 860 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 861 | // MonitorExit may throw exception |
| 862 | return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */; |
| 863 | } |
| 864 | |
| 865 | extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) { |
| 866 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
| 867 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 868 | obj->MonitorEnter(thread); // May block |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 869 | DCHECK(thread->HoldsLock(obj)); |
| 870 | // Only possible exception is NPE and is handled before entry |
| 871 | DCHECK(!thread->IsExceptionPending()); |
| 872 | } |
| 873 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 874 | void CheckSuspendFromCode(Thread* thread) { |
| 875 | // Called when thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 876 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 877 | } |
| 878 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 879 | extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) { |
| 880 | // 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] | 881 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 882 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 887 | * not of sufficient length. |
| 888 | * |
| 889 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 890 | * little-endian ordering. Require that oat2dex do any required swapping |
| 891 | * so this routine can get by with a memcpy(). |
| 892 | * |
| 893 | * Format of the data: |
| 894 | * ushort ident = 0x0300 magic value |
| 895 | * ushort width width of each element in the table |
| 896 | * uint size number of elements in the table |
| 897 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 898 | * padding at the end) |
| 899 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 900 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table, |
| 901 | Thread* self, Method** sp) { |
| 902 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 903 | DCHECK_EQ(table[0], 0x0300); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 904 | if (UNLIKELY(array == NULL)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 905 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 906 | "null array in fill array"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 907 | return -1; // Error |
| 908 | } |
| 909 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 910 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 911 | if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 912 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 913 | "failed array fill. length=%d; index=%d", array->GetLength(), size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 914 | return -1; // Error |
| 915 | } |
| 916 | uint16_t width = table[1]; |
| 917 | uint32_t size_in_bytes = size * width; |
| 918 | memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes); |
| 919 | return 0; // Success |
| 920 | } |
| 921 | |
| 922 | // See comments in runtime_support_asm.S |
| 923 | extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx, |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 924 | Object* this_object, |
| 925 | Method* caller_method, |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 926 | Thread* thread, Method** sp) { |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 927 | Method* interface_method = caller_method->GetDexCacheResolvedMethods()->Get(method_idx); |
| 928 | Method* found_method = NULL; // The found method |
| 929 | if (LIKELY(interface_method != NULL && this_object != NULL)) { |
Ian Rogers | b04f69f | 2011-10-17 00:40:54 -0700 | [diff] [blame] | 930 | found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, false); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 931 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 932 | if (UNLIKELY(found_method == NULL)) { |
| 933 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs); |
| 934 | if (this_object == NULL) { |
| 935 | thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 936 | "null receiver during interface dispatch"); |
| 937 | return 0; |
| 938 | } |
| 939 | if (interface_method == NULL) { |
| 940 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 941 | interface_method = class_linker->ResolveMethod(method_idx, caller_method, false); |
| 942 | if (interface_method == NULL) { |
| 943 | // Could not resolve interface method. Throw error and unwind |
| 944 | CHECK(thread->IsExceptionPending()); |
| 945 | return 0; |
| 946 | } |
| 947 | } |
Ian Rogers | b04f69f | 2011-10-17 00:40:54 -0700 | [diff] [blame] | 948 | found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, true); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 949 | if (found_method == NULL) { |
| 950 | CHECK(thread->IsExceptionPending()); |
| 951 | return 0; |
| 952 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 953 | } |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 954 | const void* code = found_method->GetCode(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 955 | |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 956 | uint32_t method_uint = reinterpret_cast<uint32_t>(found_method); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 957 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 958 | uint64_t result = ((code_uint << 32) | method_uint); |
| 959 | return result; |
| 960 | } |
| 961 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 962 | static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) { |
| 963 | ScopedLocalRef<jclass> jlr_UTE_class(env, |
| 964 | env->FindClass("java/lang/reflect/UndeclaredThrowableException")); |
| 965 | if (jlr_UTE_class.get() == NULL) { |
| 966 | LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\""; |
| 967 | } else { |
| 968 | jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>", |
| 969 | "(Ljava/lang/Throwable;)V"); |
| 970 | jthrowable jexception = AddLocalReference<jthrowable>(env, exception); |
| 971 | ScopedLocalRef<jthrowable> jlr_UTE(env, |
| 972 | reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor, |
| 973 | jexception))); |
| 974 | int rc = env->Throw(jlr_UTE.get()); |
| 975 | if (rc != JNI_OK) { |
| 976 | LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\""; |
| 977 | } |
| 978 | } |
| 979 | CHECK(self->IsExceptionPending()); |
| 980 | } |
| 981 | |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 982 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 983 | // which is responsible for recording callee save registers. We explicitly handlerize incoming |
| 984 | // reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke |
| 985 | // the invocation handler which is a field within the proxy object receiver. |
| 986 | extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver, |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 987 | Thread* self, byte* stack_args) { |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 988 | // Register the top of the managed stack |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 989 | Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12); |
| 990 | DCHECK_EQ(*proxy_sp, proxy_method); |
| 991 | self->SetTopOfStack(proxy_sp, 0); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 992 | // TODO: ARM specific |
| 993 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u); |
| 994 | // Start new JNI local reference state |
| 995 | JNIEnvExt* env = self->GetJniEnv(); |
| 996 | ScopedJniEnvLocalRefState env_state(env); |
| 997 | // Create local ref. copies of proxy method and the receiver |
| 998 | jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver); |
| 999 | jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method); |
| 1000 | |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1001 | // Placing into local references incoming arguments from the caller's register arguments, |
| 1002 | // replacing original Object* with jobject |
| 1003 | const size_t num_params = proxy_method->NumArgs(); |
| 1004 | size_t args_in_regs = 0; |
| 1005 | for (size_t i = 1; i < num_params; i++) { // skip receiver |
| 1006 | args_in_regs = args_in_regs + (proxy_method->IsParamALongOrDouble(i) ? 2 : 1); |
| 1007 | if (args_in_regs > 2) { |
| 1008 | args_in_regs = 2; |
| 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | size_t cur_arg = 0; // current stack location to read |
| 1013 | size_t param_index = 1; // skip receiver |
| 1014 | while (cur_arg < args_in_regs && param_index < num_params) { |
| 1015 | if (proxy_method->IsParamAReference(param_index)) { |
| 1016 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1017 | jobject jobj = AddLocalReference<jobject>(env, obj); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1018 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1019 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1020 | cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1); |
| 1021 | param_index++; |
| 1022 | } |
| 1023 | // Placing into local references incoming arguments from the caller's stack arguments |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1024 | 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] | 1025 | while (param_index < num_params) { |
| 1026 | if (proxy_method->IsParamAReference(param_index)) { |
| 1027 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
| 1028 | jobject jobj = AddLocalReference<jobject>(env, obj); |
| 1029 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1030 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1031 | cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1); |
| 1032 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1033 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1034 | // Set up arguments array and place in local IRT during boxing (which may allocate/GC) |
| 1035 | jvalue args_jobj[3]; |
| 1036 | args_jobj[0].l = rcvr_jobj; |
| 1037 | args_jobj[1].l = proxy_method_jobj; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1038 | // Args array, if no arguments then NULL (don't include receiver in argument count) |
| 1039 | args_jobj[2].l = NULL; |
| 1040 | ObjectArray<Object>* args = NULL; |
| 1041 | if ((num_params - 1) > 0) { |
| 1042 | args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1043 | if (args == NULL) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1044 | CHECK(self->IsExceptionPending()); |
| 1045 | return; |
| 1046 | } |
| 1047 | args_jobj[2].l = AddLocalReference<jobjectArray>(env, args); |
| 1048 | } |
| 1049 | // Convert proxy method into expected interface method |
| 1050 | Method* interface_method = proxy_method->FindOverriddenMethod(); |
| 1051 | CHECK(interface_method != NULL); |
| 1052 | args_jobj[1].l = AddLocalReference<jobject>(env, interface_method); |
| 1053 | LOG(INFO) << "Interface method is " << PrettyMethod(interface_method, true); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1054 | // Box arguments |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1055 | cur_arg = 0; // reset stack location to read to start |
| 1056 | // reset index, will index into param type array which doesn't include the receiver |
| 1057 | param_index = 0; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1058 | ObjectArray<Class>* param_types = interface_method->GetJavaParameterTypes(); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1059 | CHECK(param_types != NULL); |
| 1060 | // Check number of parameter types agrees with number from the Method - less 1 for the receiver. |
| 1061 | CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1); |
| 1062 | while (cur_arg < args_in_regs && param_index < (num_params - 1)) { |
| 1063 | Class* param_type = param_types->Get(param_index); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1064 | Object* obj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1065 | if (!param_type->IsPrimitive()) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1066 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1067 | } else { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1068 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
| 1069 | if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) { |
| 1070 | // 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] | 1071 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize)); |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 1072 | val.j = (val.j & 0xffffffffULL) | (high_half << 32); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1073 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1074 | BoxPrimitive(env, param_type->GetPrimitiveType(), val); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1075 | if (self->IsExceptionPending()) { |
| 1076 | return; |
| 1077 | } |
| 1078 | obj = val.l; |
| 1079 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1080 | args->Set(param_index, obj); |
| 1081 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 1082 | param_index++; |
| 1083 | } |
| 1084 | // Placing into local references incoming arguments from the caller's stack arguments |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1085 | cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3 |
| 1086 | while (param_index < (num_params - 1)) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1087 | Class* param_type = param_types->Get(param_index); |
| 1088 | Object* obj; |
| 1089 | if (!param_type->IsPrimitive()) { |
| 1090 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
| 1091 | } else { |
| 1092 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1093 | BoxPrimitive(env, param_type->GetPrimitiveType(), val); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1094 | if (self->IsExceptionPending()) { |
| 1095 | return; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1096 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1097 | obj = val.l; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1098 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 1099 | args->Set(param_index, obj); |
| 1100 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 1101 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1102 | } |
| 1103 | // Get the InvocationHandler method and the field that holds it within the Proxy object |
| 1104 | static jmethodID inv_hand_invoke_mid = NULL; |
| 1105 | static jfieldID proxy_inv_hand_fid = NULL; |
| 1106 | if (proxy_inv_hand_fid == NULL) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1107 | ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy")); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1108 | 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] | 1109 | ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler")); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1110 | inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke", |
| 1111 | "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); |
| 1112 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1113 | DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy"))); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1114 | jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid); |
| 1115 | // Call InvocationHandler.invoke |
| 1116 | jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj); |
| 1117 | // Place result in stack args |
| 1118 | if (!self->IsExceptionPending()) { |
| 1119 | Object* result_ref = self->DecodeJObject(result); |
| 1120 | if (result_ref != NULL) { |
| 1121 | JValue result_unboxed; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1122 | UnboxPrimitive(env, result_ref, interface_method->GetReturnType(), result_unboxed); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1123 | *reinterpret_cast<JValue*>(stack_args) = result_unboxed; |
| 1124 | } else { |
| 1125 | *reinterpret_cast<jobject*>(stack_args) = NULL; |
| 1126 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1127 | } else { |
| 1128 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 1129 | // a UndeclaredThrowableException. |
| 1130 | Throwable* exception = self->GetException(); |
| 1131 | self->ClearException(); |
| 1132 | if (!exception->IsCheckedException()) { |
| 1133 | self->SetException(exception); |
| 1134 | } else { |
| 1135 | ObjectArray<Class>* declared_exceptions = proxy_method->GetExceptionTypes(); |
| 1136 | Class* exception_class = exception->GetClass(); |
| 1137 | bool declares_exception = false; |
| 1138 | for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
| 1139 | Class* declared_exception = declared_exceptions->Get(i); |
| 1140 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 1141 | } |
| 1142 | if (declares_exception) { |
| 1143 | self->SetException(exception); |
| 1144 | } else { |
| 1145 | ThrowNewUndeclaredThrowableException(self, env, exception); |
| 1146 | } |
| 1147 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1151 | /* |
| 1152 | * Float/double conversion requires clamping to min and max of integer form. If |
| 1153 | * target doesn't support this normally, use these. |
| 1154 | */ |
| 1155 | int64_t D2L(double d) { |
| 1156 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 1157 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 1158 | if (d >= kMaxLong) |
| 1159 | return (int64_t)0x7fffffffffffffffULL; |
| 1160 | else if (d <= kMinLong) |
| 1161 | return (int64_t)0x8000000000000000ULL; |
| 1162 | else if (d != d) // NaN case |
| 1163 | return 0; |
| 1164 | else |
| 1165 | return (int64_t)d; |
| 1166 | } |
| 1167 | |
| 1168 | int64_t F2L(float f) { |
| 1169 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 1170 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 1171 | if (f >= kMaxLong) |
| 1172 | return (int64_t)0x7fffffffffffffffULL; |
| 1173 | else if (f <= kMinLong) |
| 1174 | return (int64_t)0x8000000000000000ULL; |
| 1175 | else if (f != f) // NaN case |
| 1176 | return 0; |
| 1177 | else |
| 1178 | return (int64_t)f; |
| 1179 | } |
| 1180 | |
| 1181 | } // namespace art |