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" |
| 20 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
| 23 | // Temporary debugging hook for compiler. |
| 24 | extern void DebugMe(Method* method, uint32_t info) { |
| 25 | LOG(INFO) << "DebugMe"; |
| 26 | if (method != NULL) { |
| 27 | LOG(INFO) << PrettyMethod(method); |
| 28 | } |
| 29 | LOG(INFO) << "Info: " << info; |
| 30 | } |
| 31 | |
| 32 | // Return value helper for jobject return types |
| 33 | extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
| 34 | return thread->DecodeJObject(obj); |
| 35 | } |
| 36 | |
| 37 | extern void* FindNativeMethod(Thread* thread) { |
| 38 | DCHECK(Thread::Current() == thread); |
| 39 | |
| 40 | Method* method = const_cast<Method*>(thread->GetCurrentMethod()); |
| 41 | DCHECK(method != NULL); |
| 42 | |
| 43 | // Lookup symbol address for method, on failure we'll return NULL with an |
| 44 | // exception set, otherwise we return the address of the method we found. |
| 45 | void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method); |
| 46 | if (native_code == NULL) { |
| 47 | DCHECK(thread->IsExceptionPending()); |
| 48 | return NULL; |
| 49 | } else { |
| 50 | // Register so that future calls don't come here |
| 51 | method->RegisterNative(native_code); |
| 52 | return native_code; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Called by generated call to throw an exception |
| 57 | extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) { |
| 58 | /* |
| 59 | * exception may be NULL, in which case this routine should |
| 60 | * throw NPE. NOTE: this is a convenience for generated code, |
| 61 | * which previously did the null check inline and constructed |
| 62 | * and threw a NPE if NULL. This routine responsible for setting |
| 63 | * exception_ in thread and delivering the exception. |
| 64 | */ |
| 65 | // Place a special frame at the TOS that will save all callee saves |
| 66 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 67 | thread->SetTopOfStack(sp, 0); |
| 68 | if (exception == NULL) { |
| 69 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception"); |
| 70 | } else { |
| 71 | thread->SetException(exception); |
| 72 | } |
| 73 | thread->DeliverException(); |
| 74 | } |
| 75 | |
| 76 | // Deliver an exception that's pending on thread helping set up a callee save frame on the way |
| 77 | extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) { |
| 78 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 79 | thread->SetTopOfStack(sp, 0); |
| 80 | thread->DeliverException(); |
| 81 | } |
| 82 | |
| 83 | // Called by generated call to throw a NPE exception |
| 84 | extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) { |
| 85 | // Place a special frame at the TOS that will save all callee saves |
| 86 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 87 | thread->SetTopOfStack(sp, 0); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 88 | thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL); |
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 an arithmetic divide by zero exception |
| 93 | extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) { |
| 94 | // Place a special frame at the TOS that will save all callee saves |
| 95 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 96 | thread->SetTopOfStack(sp, 0); |
| 97 | thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero"); |
| 98 | thread->DeliverException(); |
| 99 | } |
| 100 | |
| 101 | // Called by generated call to throw an arithmetic divide by zero exception |
| 102 | extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) { |
| 103 | // Place a special frame at the TOS that will save all callee saves |
| 104 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 105 | thread->SetTopOfStack(sp, 0); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 106 | thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", "length=%d; index=%d", |
| 107 | limit, index); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 108 | thread->DeliverException(); |
| 109 | } |
| 110 | |
| 111 | // Called by the AbstractMethodError stub (not runtime support) |
| 112 | extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) { |
| 113 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 114 | thread->SetTopOfStack(sp, 0); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 115 | thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", "abstract method \"%s\"", |
| 116 | PrettyMethod(method).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 117 | thread->DeliverException(); |
| 118 | } |
| 119 | |
| 120 | extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) { |
| 121 | // Place a special frame at the TOS that will save all callee saves |
| 122 | Runtime* runtime = Runtime::Current(); |
| 123 | *sp = runtime->GetCalleeSaveMethod(); |
| 124 | thread->SetTopOfStack(sp, 0); |
| 125 | thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 126 | thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;", |
| 127 | "stack size %zdkb; default stack size: %zdkb", |
| 128 | thread->GetStackSize() / KB, runtime->GetDefaultStackSize() / KB); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 129 | thread->ResetDefaultStackEnd(); // Return to default stack size |
| 130 | thread->DeliverException(); |
| 131 | } |
| 132 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 133 | std::string ClassNameFromIndex(Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) { |
| 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 | |
| 163 | std::string FieldNameFromIndex(const Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) { |
| 164 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_FIELD)); |
| 165 | |
| 166 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 167 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 168 | |
| 169 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
| 170 | std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_))); |
| 171 | const char* field_name = dex_file.dexStringById(id.name_idx_); |
| 172 | if (!access) { |
| 173 | return class_name + "." + field_name; |
| 174 | } |
| 175 | |
| 176 | std::string result; |
| 177 | result += "tried to access field "; |
| 178 | result += class_name + "." + field_name; |
| 179 | result += " from class "; |
| 180 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | std::string MethodNameFromIndex(const Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) { |
| 185 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_METHOD)); |
| 186 | |
| 187 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 188 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 189 | |
| 190 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
| 191 | std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_))); |
| 192 | const char* method_name = dex_file.dexStringById(id.name_idx_); |
| 193 | if (!access) { |
| 194 | return class_name + "." + method_name; |
| 195 | } |
| 196 | |
| 197 | std::string result; |
| 198 | result += "tried to access method "; |
| 199 | result += class_name + "." + method_name + ":" + dex_file.CreateMethodDescriptor(id.proto_idx_, NULL); |
| 200 | result += " from class "; |
| 201 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 206 | // Place a special frame at the TOS that will save all callee saves |
| 207 | Runtime* runtime = Runtime::Current(); |
| 208 | *sp = runtime->GetCalleeSaveMethod(); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 209 | self->SetTopOfStack(sp, 0); |
| 210 | |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 211 | // We need the calling method as context to interpret 'ref'. |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 212 | Frame frame = self->GetTopOfStack(); |
| 213 | frame.Next(); |
| 214 | Method* method = frame.GetMethod(); |
| 215 | |
| 216 | DexVerifier::VerifyErrorRefType ref_type = static_cast<DexVerifier::VerifyErrorRefType>(kind >> kVerifyErrorRefTypeShift); |
| 217 | |
| 218 | const char* exception_class = "Ljava/lang/VerifyError;"; |
| 219 | std::string msg; |
| 220 | |
| 221 | switch (static_cast<DexVerifier::VerifyError>(kind & ~(0xff << kVerifyErrorRefTypeShift))) { |
| 222 | case DexVerifier::VERIFY_ERROR_NO_CLASS: |
| 223 | exception_class = "Ljava/lang/NoClassDefFoundError;"; |
| 224 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 225 | break; |
| 226 | case DexVerifier::VERIFY_ERROR_NO_FIELD: |
| 227 | exception_class = "Ljava/lang/NoSuchFieldError;"; |
| 228 | msg = FieldNameFromIndex(method, ref, ref_type, false); |
| 229 | break; |
| 230 | case DexVerifier::VERIFY_ERROR_NO_METHOD: |
| 231 | exception_class = "Ljava/lang/NoSuchMethodError;"; |
| 232 | msg = MethodNameFromIndex(method, ref, ref_type, false); |
| 233 | break; |
| 234 | case DexVerifier::VERIFY_ERROR_ACCESS_CLASS: |
| 235 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 236 | msg = ClassNameFromIndex(method, ref, ref_type, true); |
| 237 | break; |
| 238 | case DexVerifier::VERIFY_ERROR_ACCESS_FIELD: |
| 239 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 240 | msg = FieldNameFromIndex(method, ref, ref_type, true); |
| 241 | break; |
| 242 | case DexVerifier::VERIFY_ERROR_ACCESS_METHOD: |
| 243 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 244 | msg = MethodNameFromIndex(method, ref, ref_type, true); |
| 245 | break; |
| 246 | case DexVerifier::VERIFY_ERROR_CLASS_CHANGE: |
| 247 | exception_class = "Ljava/lang/IncompatibleClassChangeError;"; |
| 248 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 249 | break; |
| 250 | case DexVerifier::VERIFY_ERROR_INSTANTIATION: |
| 251 | exception_class = "Ljava/lang/InstantiationError;"; |
| 252 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 253 | break; |
| 254 | case DexVerifier::VERIFY_ERROR_GENERIC: |
| 255 | // Generic VerifyError; use default exception, no message. |
| 256 | break; |
| 257 | case DexVerifier::VERIFY_ERROR_NONE: |
| 258 | CHECK(false); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | self->ThrowNewException(exception_class, msg.c_str()); |
| 263 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) { |
| 267 | // Place a special frame at the TOS that will save all callee saves |
| 268 | Runtime* runtime = Runtime::Current(); |
| 269 | *sp = runtime->GetCalleeSaveMethod(); |
| 270 | thread->SetTopOfStack(sp, 0); |
| 271 | LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 272 | thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 273 | thread->DeliverException(); |
| 274 | } |
| 275 | |
| 276 | extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) { |
| 277 | // Place a special frame at the TOS that will save all callee saves |
| 278 | Runtime* runtime = Runtime::Current(); |
| 279 | *sp = runtime->GetCalleeSaveMethod(); |
| 280 | thread->SetTopOfStack(sp, 0); |
| 281 | LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 282 | thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 283 | thread->DeliverException(); |
| 284 | } |
| 285 | |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 286 | extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 287 | // Place a special frame at the TOS that will save all callee saves |
| 288 | Runtime* runtime = Runtime::Current(); |
| 289 | *sp = runtime->GetCalleeSaveMethod(); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 290 | self->SetTopOfStack(sp, 0); |
| 291 | |
| 292 | // We need the calling method as context for the method_idx. |
| 293 | Frame frame = self->GetTopOfStack(); |
| 294 | frame.Next(); |
| 295 | Method* method = frame.GetMethod(); |
| 296 | |
| 297 | self->ThrowNewException("Ljava/lang/NoSuchMethodError;", |
| 298 | MethodNameFromIndex(method, method_idx, DexVerifier::VERIFY_ERROR_REF_METHOD, false).c_str()); |
| 299 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) { |
| 303 | LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode"; |
| 304 | // Place a special frame at the TOS that will save all callee saves |
| 305 | Runtime* runtime = Runtime::Current(); |
| 306 | *sp = runtime->GetCalleeSaveMethod(); |
| 307 | thread->SetTopOfStack(sp, 0); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 308 | thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 309 | thread->DeliverException(); |
| 310 | } |
| 311 | |
| 312 | // TODO: placeholder. Helper function to type |
| 313 | Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 314 | /* |
| 315 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 316 | * Returns initialized type. Does not return normally if an exception |
| 317 | * is thrown, but instead initiates the catch. Should be similar to |
| 318 | * ClassLinker::InitializeStaticStorageFromCode. |
| 319 | */ |
| 320 | UNIMPLEMENTED(FATAL); |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | // TODO: placeholder. Helper function to resolve virtual method |
| 325 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
| 326 | /* |
| 327 | * Slow-path handler on invoke virtual method path in which |
| 328 | * base method is unresolved at compile-time. Doesn't need to |
| 329 | * return anything - just either ensure that |
| 330 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 331 | * throw and unwind. The caller will restart call sequence |
| 332 | * from the beginning. |
| 333 | */ |
| 334 | } |
| 335 | |
| 336 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 337 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
| 338 | extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method) { |
| 339 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 340 | if (klass == NULL) { |
| 341 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 342 | if (klass == NULL) { |
| 343 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 344 | return NULL; // Failure |
| 345 | } |
| 346 | } |
| 347 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(klass, true)) { |
| 348 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 349 | return NULL; // Failure |
| 350 | } |
| 351 | return klass->AllocObject(); |
| 352 | } |
| 353 | |
| 354 | // Helper function to alloc array for OP_FILLED_NEW_ARRAY |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame^] | 355 | extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 356 | int32_t component_count) { |
| 357 | if (component_count < 0) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 358 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 359 | component_count); |
| 360 | return NULL; // Failure |
| 361 | } |
| 362 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 363 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 364 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 365 | if (klass == NULL) { // Error |
| 366 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 367 | return NULL; // Failure |
| 368 | } |
| 369 | } |
| 370 | if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) { |
| 371 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 372 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 373 | "Bad filled array request for type %s", |
| 374 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 375 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 376 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 377 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 378 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 379 | } |
| 380 | return NULL; // Failure |
| 381 | } else { |
| 382 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 383 | return Array::Alloc(klass, component_count); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 388 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame^] | 389 | extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 390 | if (component_count < 0) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 391 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 392 | component_count); |
| 393 | return NULL; // Failure |
| 394 | } |
| 395 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 396 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 397 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 398 | if (klass == NULL) { // Error |
| 399 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 400 | return NULL; // Failure |
| 401 | } |
| 402 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 403 | } |
| 404 | return Array::Alloc(klass, component_count); |
| 405 | } |
| 406 | |
| 407 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
| 408 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b) { |
| 409 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 410 | DCHECK(b->IsClass()) << PrettyClass(b); |
| 411 | if (b->IsAssignableFrom(a)) { |
| 412 | return 0; // Success |
| 413 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 414 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 415 | "%s cannot be cast to %s", |
| 416 | PrettyDescriptor(a->GetDescriptor()).c_str(), |
| 417 | PrettyDescriptor(b->GetDescriptor()).c_str()); |
| 418 | return -1; // Failure |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 423 | // Returns 0 on success and -1 if an exception is pending. |
| 424 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class) { |
| 425 | DCHECK(array_class != NULL); |
| 426 | // element can't be NULL as we catch this is screened in runtime_support |
| 427 | Class* element_class = element->GetClass(); |
| 428 | Class* component_type = array_class->GetComponentType(); |
| 429 | if (component_type->IsAssignableFrom(element_class)) { |
| 430 | return 0; // Success |
| 431 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 432 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 433 | "Cannot store an object of type %s in to an array of type %s", |
| 434 | PrettyDescriptor(element_class->GetDescriptor()).c_str(), |
| 435 | PrettyDescriptor(array_class->GetDescriptor()).c_str()); |
| 436 | return -1; // Failure |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | extern "C" int artUnlockObjectFromCode(Thread* thread, Object* obj) { |
| 441 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 442 | return obj->MonitorExit(thread) ? 0 /* Success */ : -1 /* Failure */; |
| 443 | } |
| 444 | |
| 445 | void LockObjectFromCode(Thread* thread, Object* obj) { |
| 446 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 447 | obj->MonitorEnter(thread); |
| 448 | DCHECK(thread->HoldsLock(obj)); |
| 449 | // Only possible exception is NPE and is handled before entry |
| 450 | DCHECK(!thread->IsExceptionPending()); |
| 451 | } |
| 452 | |
| 453 | extern "C" void artCheckSuspendFromCode(Thread* thread) { |
| 454 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 459 | * not of sufficient length. |
| 460 | * |
| 461 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 462 | * little-endian ordering. Require that oat2dex do any required swapping |
| 463 | * so this routine can get by with a memcpy(). |
| 464 | * |
| 465 | * Format of the data: |
| 466 | * ushort ident = 0x0300 magic value |
| 467 | * ushort width width of each element in the table |
| 468 | * uint size number of elements in the table |
| 469 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 470 | * padding at the end) |
| 471 | */ |
| 472 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table) { |
| 473 | DCHECK_EQ(table[0], 0x0300); |
| 474 | if (array == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 475 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 476 | "null array in fill array"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 477 | return -1; // Error |
| 478 | } |
| 479 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 480 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
| 481 | if (static_cast<int32_t>(size) > array->GetLength()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 482 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 483 | "failed array fill. length=%d; index=%d", array->GetLength(), size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 484 | return -1; // Error |
| 485 | } |
| 486 | uint16_t width = table[1]; |
| 487 | uint32_t size_in_bytes = size * width; |
| 488 | memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes); |
| 489 | return 0; // Success |
| 490 | } |
| 491 | |
| 492 | // See comments in runtime_support_asm.S |
| 493 | extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx, |
| 494 | Object* this_object , |
| 495 | Method* caller_method) { |
| 496 | Thread* thread = Thread::Current(); |
| 497 | if (this_object == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 498 | thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 499 | "null receiver during interface dispatch"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 503 | Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false); |
| 504 | if (interface_method == NULL) { |
| 505 | // Could not resolve interface method. Throw error and unwind |
| 506 | CHECK(thread->IsExceptionPending()); |
| 507 | return 0; |
| 508 | } |
| 509 | Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 510 | if (method == NULL) { |
| 511 | CHECK(thread->IsExceptionPending()); |
| 512 | return 0; |
| 513 | } |
| 514 | const void* code = method->GetCode(); |
| 515 | |
| 516 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 517 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 518 | uint64_t result = ((code_uint << 32) | method_uint); |
| 519 | return result; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Float/double conversion requires clamping to min and max of integer form. If |
| 524 | * target doesn't support this normally, use these. |
| 525 | */ |
| 526 | int64_t D2L(double d) { |
| 527 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 528 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 529 | if (d >= kMaxLong) |
| 530 | return (int64_t)0x7fffffffffffffffULL; |
| 531 | else if (d <= kMinLong) |
| 532 | return (int64_t)0x8000000000000000ULL; |
| 533 | else if (d != d) // NaN case |
| 534 | return 0; |
| 535 | else |
| 536 | return (int64_t)d; |
| 537 | } |
| 538 | |
| 539 | int64_t F2L(float f) { |
| 540 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 541 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 542 | if (f >= kMaxLong) |
| 543 | return (int64_t)0x7fffffffffffffffULL; |
| 544 | else if (f <= kMinLong) |
| 545 | return (int64_t)0x8000000000000000ULL; |
| 546 | else if (f != f) // NaN case |
| 547 | return 0; |
| 548 | else |
| 549 | return (int64_t)f; |
| 550 | } |
| 551 | |
| 552 | } // namespace art |