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