blob: f158680c4e116fa7887842fa305f08954d7414d3 [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
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 Hughes91bf6cd2012-02-14 17:27:48 -080019#include "debugger.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070020#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070021#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070022#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object.h"
24#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070025#include "reflection.h"
jeffhaoe343b762011-12-05 16:36:44 -080026#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070027#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070028
Shih-wei Liao2d831012011-09-28 22:06:53 -070029namespace art {
30
Ian Rogers4f0d07c2011-10-06 23:38:47 -070031// Place a special frame at the TOS that will save the callee saves for the given type
32static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070033 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070034 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
35 self->SetTopOfStack(sp, 0);
36}
37
Ian Rogersa32a6fd2012-02-06 20:18:44 -080038static void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed) {
39 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
40 "illegal class access: '%s' -> '%s'",
41 PrettyDescriptor(referrer).c_str(),
42 PrettyDescriptor(accessed).c_str());
43}
44
45static void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
46 Class* accessed, const Method* caller,
47 const Method* called,
Ian Rogersc8b306f2012-02-17 21:34:44 -080048 InvokeType type) {
49 std::ostringstream type_stream;
50 type_stream << type;
Ian Rogersa32a6fd2012-02-06 20:18:44 -080051 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
52 "illegal class access ('%s' -> '%s')"
53 "in attempt to invoke %s method '%s' from '%s'",
54 PrettyDescriptor(referrer).c_str(),
55 PrettyDescriptor(accessed).c_str(),
Ian Rogersc8b306f2012-02-17 21:34:44 -080056 type_stream.str().c_str(),
Ian Rogersa32a6fd2012-02-06 20:18:44 -080057 PrettyMethod(called).c_str(),
58 PrettyMethod(caller).c_str());
59}
60
61static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
62 const Method* referrer,
63 const Method* interface_method,
64 Object* this_object) {
65 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
66 "class '%s' does not implement interface '%s' in call to '%s' from '%s'",
67 PrettyDescriptor(this_object->GetClass()).c_str(),
68 PrettyDescriptor(interface_method->GetDeclaringClass()).c_str(),
69 PrettyMethod(interface_method).c_str(), PrettyMethod(referrer).c_str());
70}
71
72static void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed) {
73 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
74 "Field '%s' is inaccessible to class '%s'",
75 PrettyField(accessed, false).c_str(),
76 PrettyDescriptor(referrer).c_str());
77}
78
79static void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed) {
80 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
81 "Method '%s' is inaccessible to class '%s'",
82 PrettyMethod(accessed).c_str(),
83 PrettyDescriptor(referrer).c_str());
84}
85
86static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
87 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
88 "Attempt to %s field '%s' on a null object reference",
89 is_read ? "read from" : "write to",
90 PrettyField(field, true).c_str());
91}
92
93static void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller,
Ian Rogersc8b306f2012-02-17 21:34:44 -080094 uint32_t method_idx, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -080095 const DexFile& dex_file =
96 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogersc8b306f2012-02-17 21:34:44 -080097 std::ostringstream type_stream;
98 type_stream << type;
Ian Rogersa32a6fd2012-02-06 20:18:44 -080099 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
100 "Attempt to invoke %s method '%s' from '%s' on a null object reference",
Ian Rogersc8b306f2012-02-17 21:34:44 -0800101 type_stream.str().c_str(),
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800102 PrettyMethod(method_idx, dex_file, true).c_str(),
103 PrettyMethod(caller).c_str());
104}
105
buzbee44b412b2012-02-04 08:50:53 -0800106/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800107 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -0800108 * the method. However, because the offset alone cannot distinguish between
109 * method entry and offset 0 within the method, we'll use an offset of -1
110 * to denote method entry.
111 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800112extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -0800113 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800114 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -0800115}
116
Shih-wei Liao2d831012011-09-28 22:06:53 -0700117// Temporary debugging hook for compiler.
118extern void DebugMe(Method* method, uint32_t info) {
119 LOG(INFO) << "DebugMe";
120 if (method != NULL) {
121 LOG(INFO) << PrettyMethod(method);
122 }
123 LOG(INFO) << "Info: " << info;
124}
125
126// Return value helper for jobject return types
127extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700128 if (thread->IsExceptionPending()) {
129 return NULL;
130 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700131 return thread->DecodeJObject(obj);
132}
133
134extern void* FindNativeMethod(Thread* thread) {
135 DCHECK(Thread::Current() == thread);
136
137 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
138 DCHECK(method != NULL);
139
140 // Lookup symbol address for method, on failure we'll return NULL with an
141 // exception set, otherwise we return the address of the method we found.
142 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
143 if (native_code == NULL) {
144 DCHECK(thread->IsExceptionPending());
145 return NULL;
146 } else {
147 // Register so that future calls don't come here
148 method->RegisterNative(native_code);
149 return native_code;
150 }
151}
152
153// Called by generated call to throw an exception
154extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
155 /*
156 * exception may be NULL, in which case this routine should
157 * throw NPE. NOTE: this is a convenience for generated code,
158 * which previously did the null check inline and constructed
159 * and threw a NPE if NULL. This routine responsible for setting
160 * exception_ in thread and delivering the exception.
161 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700162 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700163 if (exception == NULL) {
164 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
165 } else {
166 thread->SetException(exception);
167 }
168 thread->DeliverException();
169}
170
171// Deliver an exception that's pending on thread helping set up a callee save frame on the way
172extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700173 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700174 thread->DeliverException();
175}
176
177// Called by generated call to throw a NPE exception
178extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700179 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700180 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700181 thread->DeliverException();
182}
183
184// Called by generated call to throw an arithmetic divide by zero exception
185extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700186 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700187 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
188 thread->DeliverException();
189}
190
191// Called by generated call to throw an arithmetic divide by zero exception
192extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700193 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
194 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
195 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700196 thread->DeliverException();
197}
198
199// Called by the AbstractMethodError stub (not runtime support)
200extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700201 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
202 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
203 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700204 thread->DeliverException();
205}
206
207extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700208 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800209 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800210 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800211 artTraceMethodUnwindFromCode(thread);
212 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700213 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700214 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
215 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700216 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700217 thread->ResetDefaultStackEnd(); // Return to default stack size
218 thread->DeliverException();
219}
220
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700221static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700222 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
224 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
225
226 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700227 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700228 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
229 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700230 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700231 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
232 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700233 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700234 type_idx = ref;
235 } else {
236 CHECK(false) << static_cast<int>(ref_type);
237 }
238
Ian Rogers0571d352011-11-03 19:51:38 -0700239 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700240 if (!access) {
241 return class_name;
242 }
243
244 std::string result;
245 result += "tried to access class ";
246 result += class_name;
247 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800248 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700249 return result;
250}
251
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700252static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700253 verifier::VerifyErrorRefType ref_type, bool access) {
254 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700255
256 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
257 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
258
259 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700260 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700261 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700262 if (!access) {
263 return class_name + "." + field_name;
264 }
265
266 std::string result;
267 result += "tried to access field ";
268 result += class_name + "." + field_name;
269 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700271 return result;
272}
273
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700274static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700275 verifier::VerifyErrorRefType ref_type, bool access) {
276 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700277
278 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
279 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
280
281 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700282 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700283 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700284 if (!access) {
285 return class_name + "." + method_name;
286 }
287
288 std::string result;
289 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700290 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700291 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700292 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700294 return result;
295}
296
297extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700298 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
299 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700300 frame.Next();
301 Method* method = frame.GetMethod();
302
Ian Rogersd81871c2011-10-03 13:57:23 -0700303 verifier::VerifyErrorRefType ref_type =
304 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700305
306 const char* exception_class = "Ljava/lang/VerifyError;";
307 std::string msg;
308
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
310 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700311 exception_class = "Ljava/lang/NoClassDefFoundError;";
312 msg = ClassNameFromIndex(method, ref, ref_type, false);
313 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700315 exception_class = "Ljava/lang/NoSuchFieldError;";
316 msg = FieldNameFromIndex(method, ref, ref_type, false);
317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700319 exception_class = "Ljava/lang/NoSuchMethodError;";
320 msg = MethodNameFromIndex(method, ref, ref_type, false);
321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700323 exception_class = "Ljava/lang/IllegalAccessError;";
324 msg = ClassNameFromIndex(method, ref, ref_type, true);
325 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700327 exception_class = "Ljava/lang/IllegalAccessError;";
328 msg = FieldNameFromIndex(method, ref, ref_type, true);
329 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700330 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700331 exception_class = "Ljava/lang/IllegalAccessError;";
332 msg = MethodNameFromIndex(method, ref, ref_type, true);
333 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700334 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700335 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
336 msg = ClassNameFromIndex(method, ref, ref_type, false);
337 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700338 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700339 exception_class = "Ljava/lang/InstantiationError;";
340 msg = ClassNameFromIndex(method, ref, ref_type, false);
341 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700343 // Generic VerifyError; use default exception, no message.
344 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700346 CHECK(false);
347 break;
348 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700349 self->ThrowNewException(exception_class, msg.c_str());
350 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700351}
352
353extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700354 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700355 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700356 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700357 thread->DeliverException();
358}
359
360extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700361 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700362 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700363 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700364 thread->DeliverException();
365}
366
Elliott Hughese1410a22011-10-04 12:10:24 -0700367extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700368 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
369 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700370 frame.Next();
371 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700372 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700374 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700375}
376
377extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700378 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700379 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700380 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700381 thread->DeliverException();
382}
383
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700384void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700385 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700386 // TODO: this code is specific to ARM
387 // On entry the stack pointed by sp is:
388 // | argN | |
389 // | ... | |
390 // | arg4 | |
391 // | arg3 spill | | Caller's frame
392 // | arg2 spill | |
393 // | arg1 spill | |
394 // | Method* | ---
395 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700396 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700397 // | R3 | arg3
398 // | R2 | arg2
399 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700400 // | R0 |
401 // | Method* | <- sp
402 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
403 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
404 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
405 uintptr_t caller_pc = regs[10];
406 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700407 // Start new JNI local reference state
408 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700409 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700410 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700411 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700412 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
413 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700414 size_t args_in_regs = 0;
415 for (size_t i = 1; i < shorty_len; i++) {
416 char c = shorty[i];
417 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
418 if (args_in_regs > 3) {
419 args_in_regs = 3;
420 break;
421 }
422 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700423 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700424 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700425 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700426 // less two as return address may span into next dex instruction
427 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700429 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
430 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700431 Instruction::Code instr_code = instr->Opcode();
432 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
433 (instr_code == Instruction::INVOKE_STATIC_RANGE);
434 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
435 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700436 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700437 is_static = type == Runtime::kStaticMethod;
438 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700439 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700440 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700441 if (!is_static) {
442 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
443 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700444 if (args_in_regs < 3) {
445 // If we thought we had fewer than 3 arguments in registers, account for the receiver
446 args_in_regs++;
447 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700448 AddLocalReference<jobject>(env, obj);
449 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700450 size_t shorty_index = 1; // skip return value
451 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
452 // R0)
453 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
454 char c = shorty[shorty_index];
455 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700456 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700457 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
458 AddLocalReference<jobject>(env, obj);
459 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700460 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
461 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700462 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700463 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700464 while (shorty_index < shorty_len) {
465 char c = shorty[shorty_index];
466 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700467 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700468 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700469 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700470 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700471 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700472 }
473 // Resolve method filling in dex cache
474 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700475 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800476 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800477 // Ensure that the called method's class is initialized
478 Class* called_class = called->GetDeclaringClass();
479 linker->EnsureInitialized(called_class, true);
480 if (LIKELY(called_class->IsInitialized())) {
481 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
482 // is initialized (see test 084-class-init SlowInit)
483 Method* caller = *caller_sp;
484 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
485 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
486 // We got this far, ensure that the declaring class is initialized
487 linker->EnsureInitialized(called->GetDeclaringClass(), true);
488 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800489 } else {
490 // Direct method has been made virtual
491 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
492 "Expected direct method but found virtual: %s",
493 PrettyMethod(called, true).c_str());
494 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700495 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700496 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700497 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700498 // Something went wrong in ResolveMethod or EnsureInitialized,
499 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700500 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700501 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700502 thread->ClearException();
503 } else {
504 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800505 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700506 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700507 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700508 code = const_cast<void*>(called->GetCode());
509 }
510 return code;
511}
512
Ian Rogerscaab8c42011-10-12 12:11:18 -0700513// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800514static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
515 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800516 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700517 if (UNLIKELY(resolved_field == NULL)) {
518 return NULL;
519 }
520 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800521 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700522 if (UNLIKELY(!fields_class->IsInitializing())) {
523 return NULL;
524 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800525 Class* referring_class = referrer->GetDeclaringClass();
526 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
527 !referring_class->CanAccessMember(fields_class,
528 resolved_field->GetAccessFlags()))) {
529 // illegal access
530 return NULL;
531 }
532 FieldHelper fh(resolved_field);
533 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
534 fh.FieldSize() != expected_size)) {
535 return NULL;
536 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700537 return resolved_field;
538}
539
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800540
Ian Rogerscaab8c42011-10-12 12:11:18 -0700541// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800542Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
543 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700544 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700545 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800546 if (UNLIKELY(resolved_field == NULL)) {
547 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
548 return NULL; // failure
549 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700550 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800551 Class* referring_class = referrer->GetDeclaringClass();
552 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800553 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800554 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800555 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
556 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800557 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800558 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800559 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800560 FieldHelper fh(resolved_field);
561 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
562 fh.FieldSize() != expected_size)) {
563 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
564 "Attempted read of %zd-bit %s on field '%s'",
565 expected_size * (32 / sizeof(int32_t)),
566 is_primitive ? "primitive" : "non-primitive",
567 PrettyField(resolved_field, true).c_str());
568 return NULL; // failure
569 } else if (!is_static) {
570 // instance fields must be being accessed on an initialized class
Ian Rogers1bddec32012-02-04 12:27:34 -0800571 return resolved_field;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800572 } else {
573 // If the class is already initializing, we must be inside <clinit>, or
574 // we'd still be waiting for the lock.
575 if (fields_class->IsInitializing()) {
576 return resolved_field;
577 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
578 return resolved_field;
579 } else {
580 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
581 return NULL; // failure
582 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800583 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700584 }
585 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700586}
587
Ian Rogersce9eca62011-10-07 17:11:03 -0700588extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
589 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800590 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700591 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800592 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700593 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700594 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800595 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
596 if (LIKELY(field != NULL)) {
597 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700598 }
599 return 0; // Will throw exception by checking with Thread::Current
600}
601
602extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
603 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800604 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700605 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800606 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700607 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700608 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800609 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
610 if (LIKELY(field != NULL)) {
611 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700612 }
613 return 0; // Will throw exception by checking with Thread::Current
614}
615
616extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
617 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800618 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700619 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800620 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700621 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700622 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800623 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
624 if (LIKELY(field != NULL)) {
625 return field->GetObj(NULL);
626 }
627 return NULL; // Will throw exception by checking with Thread::Current
628}
629
630extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
631 const Method* referrer, Thread* self, Method** sp) {
632 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
633 if (LIKELY(field != NULL && obj != NULL)) {
634 return field->Get32(obj);
635 }
636 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
637 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
638 if (LIKELY(field != NULL)) {
639 if (UNLIKELY(obj == NULL)) {
640 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700641 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800642 return field->Get32(obj);
643 }
644 }
645 return 0; // Will throw exception by checking with Thread::Current
646}
647
648extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
649 const Method* referrer, Thread* self, Method** sp) {
650 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
651 if (LIKELY(field != NULL && obj != NULL)) {
652 return field->Get64(obj);
653 }
654 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
655 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
656 if (LIKELY(field != NULL)) {
657 if (UNLIKELY(obj == NULL)) {
658 ThrowNullPointerExceptionForFieldAccess(self, field, true);
659 } else {
660 return field->Get64(obj);
661 }
662 }
663 return 0; // Will throw exception by checking with Thread::Current
664}
665
666extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
667 const Method* referrer, Thread* self, Method** sp) {
668 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
669 if (LIKELY(field != NULL && obj != NULL)) {
670 return field->GetObj(obj);
671 }
672 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
673 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
674 if (LIKELY(field != NULL)) {
675 if (UNLIKELY(obj == NULL)) {
676 ThrowNullPointerExceptionForFieldAccess(self, field, true);
677 } else {
678 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700679 }
680 }
681 return NULL; // Will throw exception by checking with Thread::Current
682}
683
Ian Rogers1bddec32012-02-04 12:27:34 -0800684extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
685 const Method* referrer, Thread* self, Method** sp) {
686 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700687 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800688 field->Set32(NULL, new_value);
689 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700690 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700691 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800692 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
693 if (LIKELY(field != NULL)) {
694 field->Set32(NULL, new_value);
695 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700696 }
697 return -1; // failure
698}
699
700extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
701 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800702 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700703 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800704 field->Set64(NULL, new_value);
705 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700706 }
707 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800708 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700709 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800710 field->Set64(NULL, new_value);
711 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700712 }
713 return -1; // failure
714}
715
Ian Rogers1bddec32012-02-04 12:27:34 -0800716extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
717 const Method* referrer, Thread* self, Method** sp) {
718 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700719 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800720 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700721 field->SetObj(NULL, new_value);
722 return 0; // success
723 }
724 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700725 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800726 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
727 if (LIKELY(field != NULL)) {
728 field->SetObj(NULL, new_value);
729 return 0; // success
730 }
731 return -1; // failure
732}
733
734extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
735 const Method* referrer, Thread* self, Method** sp) {
736 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
737 if (LIKELY(field != NULL && obj != NULL)) {
738 field->Set32(obj, new_value);
739 return 0; // success
740 }
741 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
742 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
743 if (LIKELY(field != NULL)) {
744 if (UNLIKELY(obj == NULL)) {
745 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700746 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800747 field->Set32(obj, new_value);
748 return 0; // success
749 }
750 }
751 return -1; // failure
752}
753
754extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
755 Thread* self, Method** sp) {
756 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
757 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
758 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
759 if (LIKELY(field != NULL && obj != NULL)) {
760 field->Set64(obj, new_value);
761 return 0; // success
762 }
763 *sp = callee_save;
764 self->SetTopOfStack(sp, 0);
765 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
766 if (LIKELY(field != NULL)) {
767 if (UNLIKELY(obj == NULL)) {
768 ThrowNullPointerExceptionForFieldAccess(self, field, false);
769 } else {
770 field->Set64(obj, new_value);
771 return 0; // success
772 }
773 }
774 return -1; // failure
775}
776
777extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
778 const Method* referrer, Thread* self, Method** sp) {
779 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
780 if (LIKELY(field != NULL && obj != NULL)) {
781 field->SetObj(obj, new_value);
782 return 0; // success
783 }
784 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
785 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
786 if (LIKELY(field != NULL)) {
787 if (UNLIKELY(obj == NULL)) {
788 ThrowNullPointerExceptionForFieldAccess(self, field, false);
789 } else {
790 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700791 return 0; // success
792 }
793 }
794 return -1; // failure
795}
796
Shih-wei Liao2d831012011-09-28 22:06:53 -0700797// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
798// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800799// When verification/compiler hasn't been able to verify access, optionally perform an access
800// check.
801static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
802 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700803 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700804 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700805 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700806 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700807 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700808 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700809 return NULL; // Failure
810 }
811 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800812 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800813 if (UNLIKELY(!klass->IsInstantiable())) {
814 self->ThrowNewException("Ljava/lang/InstantiationError;",
815 PrettyDescriptor(klass).c_str());
816 return NULL; // Failure
817 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800818 Class* referrer = method->GetDeclaringClass();
819 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800820 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800821 return NULL; // Failure
822 }
823 }
buzbee33a129c2011-10-06 16:53:20 -0700824 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
825 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700826 return NULL; // Failure
827 }
828 return klass->AllocObject();
829}
830
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800831extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
832 Thread* self, Method** sp) {
833 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
834 return AllocObjectFromCode(type_idx, method, self, false);
835}
836
Ian Rogers28ad40d2011-10-27 15:19:26 -0700837extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
838 Thread* self, Method** sp) {
839 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800840 return AllocObjectFromCode(type_idx, method, self, true);
841}
842
843// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
844// it cannot be resolved, throw an error. If it can, use it to create an array.
845// When verification/compiler hasn't been able to verify access, optionally perform an access
846// check.
847static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
848 Thread* self, bool access_check) {
849 if (UNLIKELY(component_count < 0)) {
850 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
851 component_count);
852 return NULL; // Failure
853 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700854 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800855 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
856 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
857 if (klass == NULL) { // Error
858 DCHECK(Thread::Current()->IsExceptionPending());
859 return NULL; // Failure
860 }
861 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
862 }
863 if (access_check) {
864 Class* referrer = method->GetDeclaringClass();
865 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800866 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700867 return NULL; // Failure
868 }
869 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800870 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700871}
872
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800873extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
874 Thread* self, Method** sp) {
875 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
876 return AllocArrayFromCode(type_idx, method, component_count, self, false);
877}
878
879extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
880 int32_t component_count,
881 Thread* self, Method** sp) {
882 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
883 return AllocArrayFromCode(type_idx, method, component_count, self, true);
884}
885
886// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700887Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800888 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700889 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700890 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700891 return NULL; // Failure
892 }
893 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700894 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700895 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
896 if (klass == NULL) { // Error
897 DCHECK(Thread::Current()->IsExceptionPending());
898 return NULL; // Failure
899 }
900 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700901 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700902 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700903 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700904 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800905 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700906 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700907 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700908 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700910 }
911 return NULL; // Failure
912 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800913 if (access_check) {
914 Class* referrer = method->GetDeclaringClass();
915 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800916 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800917 return NULL; // Failure
918 }
919 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700920 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700921 return Array::Alloc(klass, component_count);
922 }
923}
924
Ian Rogersce9eca62011-10-07 17:11:03 -0700925extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
926 int32_t component_count, Thread* self, Method** sp) {
927 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800928 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700929}
930
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800931extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
932 int32_t component_count,
933 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700934 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800935 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700936}
937
Ian Rogerscaab8c42011-10-12 12:11:18 -0700938// Assignable test for code, won't throw. Null and equality tests already performed
939uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
940 DCHECK(klass != NULL);
941 DCHECK(ref_class != NULL);
942 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
943}
944
Shih-wei Liao2d831012011-09-28 22:06:53 -0700945// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700946extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700947 DCHECK(a->IsClass()) << PrettyClass(a);
948 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700949 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700950 return 0; // Success
951 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700952 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700953 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700954 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800955 PrettyDescriptor(a).c_str(),
956 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700957 return -1; // Failure
958 }
959}
960
961// Tests whether 'element' can be assigned into an array of type 'array_class'.
962// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700963extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
964 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700965 DCHECK(array_class != NULL);
966 // element can't be NULL as we catch this is screened in runtime_support
967 Class* element_class = element->GetClass();
968 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700969 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700970 return 0; // Success
971 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700972 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700973 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800974 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800975 PrettyDescriptor(element_class).c_str(),
976 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700977 return -1; // Failure
978 }
979}
980
Elliott Hughesf3778f62012-01-26 14:14:35 -0800981Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
982 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700983 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
984 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700985 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700986 CHECK(self->IsExceptionPending());
987 return NULL; // Failure - Indicate to caller to deliver exception
988 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800989 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800990 Class* referring_class = referrer->GetDeclaringClass();
991 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
992 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800993 return NULL; // Failure - Indicate to caller to deliver exception
994 }
995 // If we're just implementing const-class, we shouldn't call <clinit>.
996 if (!can_run_clinit) {
997 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700998 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700999 // If we are the <clinit> of this class, just return our storage.
1000 //
1001 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1002 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001003 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001004 return klass;
1005 }
1006 if (!class_linker->EnsureInitialized(klass, true)) {
1007 CHECK(self->IsExceptionPending());
1008 return NULL; // Failure - Indicate to caller to deliver exception
1009 }
1010 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1011 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001012}
1013
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001014extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1015 Thread* self, Method** sp) {
1016 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001017 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001018}
1019
Ian Rogers28ad40d2011-10-27 15:19:26 -07001020extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1021 Method** sp) {
1022 // Called when method->dex_cache_resolved_types_[] misses
1023 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001024 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001025}
1026
Ian Rogersb093c6b2011-10-31 16:19:55 -07001027extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1028 const Method* referrer, Thread* self,
1029 Method** sp) {
1030 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1031 // unpopulated
1032 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001033 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001034}
1035
buzbee48d72222012-01-11 15:19:51 -08001036// Helper function to resolve virtual method
1037extern "C" Method* artResolveMethodFromCode(Method* referrer,
1038 uint32_t method_idx,
1039 bool is_direct,
1040 Thread* self,
1041 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001042 /*
1043 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001044 * base method is unresolved at compile-time. Caller will
1045 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001046 */
buzbee48d72222012-01-11 15:19:51 -08001047 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1048 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1049 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
buzbee48d72222012-01-11 15:19:51 -08001050 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001051}
1052
Brian Carlstromaded5f72011-10-07 17:15:04 -07001053String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1054 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1055 return class_linker->ResolveString(string_idx, referrer);
1056}
1057
1058extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1059 Thread* self, Method** sp) {
1060 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1061 return ResolveStringFromCode(referrer, string_idx);
1062}
1063
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001064extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1065 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001066 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001067 // MonitorExit may throw exception
1068 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1069}
1070
1071extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1072 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1073 DCHECK(obj != NULL); // Assumed to have been checked before entry
1074 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001075 DCHECK(thread->HoldsLock(obj));
1076 // Only possible exception is NPE and is handled before entry
1077 DCHECK(!thread->IsExceptionPending());
1078}
1079
Ian Rogers4a510d82011-10-09 14:30:24 -07001080void CheckSuspendFromCode(Thread* thread) {
1081 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001082 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1083}
1084
Ian Rogers4a510d82011-10-09 14:30:24 -07001085extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1086 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001087 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001088 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1089}
1090
1091/*
1092 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1093 * not of sufficient length.
1094 *
1095 * NOTE: When dealing with a raw dex file, the data to be copied uses
1096 * little-endian ordering. Require that oat2dex do any required swapping
1097 * so this routine can get by with a memcpy().
1098 *
1099 * Format of the data:
1100 * ushort ident = 0x0300 magic value
1101 * ushort width width of each element in the table
1102 * uint size number of elements in the table
1103 * ubyte data[size*width] table of data values (may contain a single-byte
1104 * padding at the end)
1105 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001106extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1107 Thread* self, Method** sp) {
1108 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001109 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001110 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001111 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1112 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001113 return -1; // Error
1114 }
1115 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1116 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001117 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001118 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1119 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001120 return -1; // Error
1121 }
1122 uint16_t width = table[1];
1123 uint32_t size_in_bytes = size * width;
1124 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1125 return 0; // Success
1126}
1127
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001128// Fast path method resolution that can't throw exceptions
1129static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001130 bool access_check, InvokeType type) {
1131 bool is_direct = type == kStatic || type == kDirect;
1132 if (UNLIKELY(this_object == NULL && !is_direct)) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001133 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001134 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001135 Method* resolved_method =
1136 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1137 if (UNLIKELY(resolved_method == NULL)) {
1138 return NULL;
1139 }
1140 if (access_check) {
1141 Class* methods_class = resolved_method->GetDeclaringClass();
1142 Class* referring_class = referrer->GetDeclaringClass();
1143 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1144 !referring_class->CanAccessMember(methods_class,
1145 resolved_method->GetAccessFlags()))) {
1146 // potential illegal access
1147 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001148 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001149 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001150 if (type == kInterface) { // Most common form of slow path dispatch.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001151 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001152 } else if (is_direct) {
1153 return resolved_method;
1154 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001155 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1156 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001157 DCHECK(type == kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001158 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1159 }
1160}
1161
1162// Slow path method resolution
1163static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001164 Thread* self, bool access_check, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001165 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001166 bool is_direct = type == kStatic || type == kDirect;
1167 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1168 if (UNLIKELY(resolved_method == NULL)) {
1169 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1170 return NULL; // failure
1171 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001172 if (!access_check) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001173 if (is_direct) {
1174 return resolved_method;
1175 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001176 Method* interface_method =
1177 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1178 if (UNLIKELY(interface_method == NULL)) {
1179 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1180 resolved_method,
1181 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001182 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001183 } else {
1184 return interface_method;
1185 }
1186 } else {
1187 ObjectArray<Method>* vtable;
1188 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001189 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001190 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1191 } else {
1192 vtable = this_object->GetClass()->GetVTable();
1193 }
1194 // TODO: eliminate bounds check?
1195 return vtable->Get(vtable_index);
1196 }
1197 } else {
1198 Class* methods_class = resolved_method->GetDeclaringClass();
1199 Class* referring_class = referrer->GetDeclaringClass();
1200 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1201 !referring_class->CanAccessMember(methods_class,
1202 resolved_method->GetAccessFlags()))) {
1203 // The referring class can't access the resolved method, this may occur as a result of a
1204 // protected method being made public by implementing an interface that re-declares the
1205 // method public. Resort to the dex file to determine the correct class for the access check
1206 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1207 methods_class = class_linker->ResolveType(dex_file,
1208 dex_file.GetMethodId(method_idx).class_idx_,
1209 referring_class);
1210 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1211 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001212 referrer, resolved_method, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001213 return NULL; // failure
1214 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1215 resolved_method->GetAccessFlags()))) {
1216 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1217 return NULL; // failure
1218 }
1219 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001220 if (is_direct) {
1221 return resolved_method;
1222 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001223 Method* interface_method =
1224 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1225 if (UNLIKELY(interface_method == NULL)) {
1226 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1227 resolved_method,
1228 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001229 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001230 } else {
1231 return interface_method;
1232 }
1233 } else {
1234 ObjectArray<Method>* vtable;
1235 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001236 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001237 Class* super_class = referring_class->GetSuperClass();
1238 if (LIKELY(super_class != NULL)) {
1239 vtable = referring_class->GetSuperClass()->GetVTable();
1240 } else {
1241 vtable = NULL;
1242 }
1243 } else {
1244 vtable = this_object->GetClass()->GetVTable();
1245 }
1246 if (LIKELY(vtable != NULL &&
1247 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1248 return vtable->GetWithoutChecks(vtable_index);
1249 } else {
1250 // Behavior to agree with that of the verifier
1251 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1252 "attempt to invoke %s method '%s' from '%s'"
1253 " using incorrect form of method dispatch",
Ian Rogersc8b306f2012-02-17 21:34:44 -08001254 (type == kSuper ? "super class" : "virtual"),
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001255 PrettyMethod(resolved_method).c_str(),
1256 PrettyMethod(referrer).c_str());
Ian Rogersc8b306f2012-02-17 21:34:44 -08001257 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001258 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001259 }
1260 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001261 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001262}
1263
1264static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001265 Thread* self, Method** sp, bool access_check, InvokeType type){
1266 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001267 if (UNLIKELY(method == NULL)) {
1268 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001269 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1270 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001271 return 0; // failure
1272 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001273 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001274 if (UNLIKELY(method == NULL)) {
1275 CHECK(self->IsExceptionPending());
1276 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001277 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001278 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001279 // TODO: DCHECK
1280 CHECK(!self->IsExceptionPending());
1281 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001282
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001283 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001284 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1285 uint64_t result = ((code_uint << 32) | method_uint);
1286 return result;
1287}
1288
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001289// See comments in runtime_support_asm.S
1290extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1291 Method* caller_method, Thread* self,
1292 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001293 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001294}
1295
1296extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1297 Object* this_object,
1298 Method* caller_method, Thread* self,
1299 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001300 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1301}
1302
1303
1304extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1305 Object* this_object,
1306 Method* caller_method, Thread* self,
1307 Method** sp) {
1308 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1309}
1310
1311extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1312 Object* this_object,
1313 Method* caller_method, Thread* self,
1314 Method** sp) {
1315 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001316}
1317
1318extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1319 Object* this_object,
1320 Method* caller_method, Thread* self,
1321 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001322 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001323}
1324
1325extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1326 Object* this_object,
1327 Method* caller_method, Thread* self,
1328 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001329 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001330}
1331
Ian Rogers466bb252011-10-14 03:29:56 -07001332static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1333 ScopedLocalRef<jclass> jlr_UTE_class(env,
1334 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1335 if (jlr_UTE_class.get() == NULL) {
1336 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1337 } else {
1338 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1339 "(Ljava/lang/Throwable;)V");
1340 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1341 ScopedLocalRef<jthrowable> jlr_UTE(env,
1342 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1343 jexception)));
1344 int rc = env->Throw(jlr_UTE.get());
1345 if (rc != JNI_OK) {
1346 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1347 }
1348 }
1349 CHECK(self->IsExceptionPending());
1350}
1351
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001352// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1353// which is responsible for recording callee save registers. We explicitly handlerize incoming
1354// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1355// the invocation handler which is a field within the proxy object receiver.
1356extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001357 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001358 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001359 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1360 DCHECK_EQ(*proxy_sp, proxy_method);
1361 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001362 // TODO: ARM specific
1363 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1364 // Start new JNI local reference state
1365 JNIEnvExt* env = self->GetJniEnv();
1366 ScopedJniEnvLocalRefState env_state(env);
1367 // Create local ref. copies of proxy method and the receiver
1368 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1369 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1370
Ian Rogers14b1b242011-10-11 18:54:34 -07001371 // Placing into local references incoming arguments from the caller's register arguments,
1372 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001373 MethodHelper proxy_mh(proxy_method);
1374 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001375 size_t args_in_regs = 0;
1376 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001377 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001378 if (args_in_regs > 2) {
1379 args_in_regs = 2;
1380 break;
1381 }
1382 }
1383 size_t cur_arg = 0; // current stack location to read
1384 size_t param_index = 1; // skip receiver
1385 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001386 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001387 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001388 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001389 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001390 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001391 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001392 param_index++;
1393 }
1394 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001395 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001396 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001397 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001398 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1399 jobject jobj = AddLocalReference<jobject>(env, obj);
1400 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001401 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001402 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001403 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001404 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001405 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1406 jvalue args_jobj[3];
1407 args_jobj[0].l = rcvr_jobj;
1408 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001409 // Args array, if no arguments then NULL (don't include receiver in argument count)
1410 args_jobj[2].l = NULL;
1411 ObjectArray<Object>* args = NULL;
1412 if ((num_params - 1) > 0) {
1413 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001414 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001415 CHECK(self->IsExceptionPending());
1416 return;
1417 }
1418 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1419 }
1420 // Convert proxy method into expected interface method
1421 Method* interface_method = proxy_method->FindOverriddenMethod();
1422 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001423 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001424 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001425 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001426 cur_arg = 0; // reset stack location to read to start
1427 // reset index, will index into param type array which doesn't include the receiver
1428 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001429 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001430 CHECK(param_types != NULL);
1431 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1432 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1433 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1434 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001435 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001436 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001437 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001438 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001439 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1440 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1441 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001442 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001443 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001444 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001445 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001446 if (self->IsExceptionPending()) {
1447 return;
1448 }
1449 obj = val.l;
1450 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001451 args->Set(param_index, obj);
1452 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1453 param_index++;
1454 }
1455 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001456 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1457 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001458 Class* param_type = param_types->Get(param_index);
1459 Object* obj;
1460 if (!param_type->IsPrimitive()) {
1461 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1462 } else {
1463 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001464 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001465 if (self->IsExceptionPending()) {
1466 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001467 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001468 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001469 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001470 args->Set(param_index, obj);
1471 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1472 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001473 }
1474 // Get the InvocationHandler method and the field that holds it within the Proxy object
1475 static jmethodID inv_hand_invoke_mid = NULL;
1476 static jfieldID proxy_inv_hand_fid = NULL;
1477 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001478 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001479 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001480 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001481 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1482 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1483 }
Ian Rogers466bb252011-10-14 03:29:56 -07001484 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001485 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1486 // Call InvocationHandler.invoke
1487 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1488 // Place result in stack args
1489 if (!self->IsExceptionPending()) {
1490 Object* result_ref = self->DecodeJObject(result);
1491 if (result_ref != NULL) {
1492 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001493 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001494 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1495 } else {
1496 *reinterpret_cast<jobject*>(stack_args) = NULL;
1497 }
Ian Rogers466bb252011-10-14 03:29:56 -07001498 } else {
1499 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1500 // a UndeclaredThrowableException.
1501 Throwable* exception = self->GetException();
1502 self->ClearException();
1503 if (!exception->IsCheckedException()) {
1504 self->SetException(exception);
1505 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001506 SynthesizedProxyClass* proxy_class =
1507 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1508 int throws_index = -1;
1509 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1510 for (size_t i = 0; i < num_virt_methods; i++) {
1511 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1512 throws_index = i;
1513 break;
1514 }
1515 }
1516 CHECK_NE(throws_index, -1);
1517 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001518 Class* exception_class = exception->GetClass();
1519 bool declares_exception = false;
1520 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1521 Class* declared_exception = declared_exceptions->Get(i);
1522 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1523 }
1524 if (declares_exception) {
1525 self->SetException(exception);
1526 } else {
1527 ThrowNewUndeclaredThrowableException(self, env, exception);
1528 }
1529 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001530 }
1531}
1532
jeffhaoe343b762011-12-05 16:36:44 -08001533extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001534 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001535 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1536 self->PushTraceStackFrame(trace_frame);
1537
jeffhao2692b572011-12-16 15:42:28 -08001538 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001539
jeffhao2692b572011-12-16 15:42:28 -08001540 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001541}
1542
1543extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001544 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001545 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1546 Method* method = trace_frame.method_;
1547 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001548
jeffhao2692b572011-12-16 15:42:28 -08001549 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001550
1551 return lr;
1552}
1553
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001554uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001555 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001556 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1557 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001558 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001559
jeffhao2692b572011-12-16 15:42:28 -08001560 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001561
1562 return lr;
1563}
1564
Shih-wei Liao2d831012011-09-28 22:06:53 -07001565/*
1566 * Float/double conversion requires clamping to min and max of integer form. If
1567 * target doesn't support this normally, use these.
1568 */
1569int64_t D2L(double d) {
1570 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1571 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1572 if (d >= kMaxLong)
1573 return (int64_t)0x7fffffffffffffffULL;
1574 else if (d <= kMinLong)
1575 return (int64_t)0x8000000000000000ULL;
1576 else if (d != d) // NaN case
1577 return 0;
1578 else
1579 return (int64_t)d;
1580}
1581
1582int64_t F2L(float f) {
1583 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1584 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1585 if (f >= kMaxLong)
1586 return (int64_t)0x7fffffffffffffffULL;
1587 else if (f <= kMinLong)
1588 return (int64_t)0x8000000000000000ULL;
1589 else if (f != f) // NaN case
1590 return 0;
1591 else
1592 return (int64_t)f;
1593}
1594
1595} // namespace art