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