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