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