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