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